Debian Codegraph @

core/java/android/app/timezonedetector/ManualTimeZoneSuggestion.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package android.app.timezonedetector;
18
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.os.Parcel;
22 import android.os.Parcelable;
23 import android.os.ShellCommand;
24
25 import java.io.PrintWriter;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.List;
30 import java.util.Objects;
31
32 /**
33 * A time zone suggestion from a manual (user provided) source.
34 *
35 * <p>{@code zoneId} contains the suggested time zone ID, e.g. "America/Los_Angeles".
36 *
37 * <p>{@code debugInfo} contains debugging metadata associated with the suggestion. This is used to
38 * record why the suggestion exists and how it was entered. This information exists only to aid in
39 * debugging and therefore is used by {@link #toString()}, but it is not for use in detection logic
40 * and is not considered in {@link #hashCode()} or {@link #equals(Object)}.
41 *
42 * @hide
43 */
44 public final class ManualTimeZoneSuggestion implements Parcelable {
45
46 public static final @NonNull Creator<ManualTimeZoneSuggestion> CREATOR =
47 new Creator<ManualTimeZoneSuggestion>() {
48 public ManualTimeZoneSuggestion createFromParcel(Parcel in) {
49 return ManualTimeZoneSuggestion.createFromParcel(in);
50 }
51
52 public ManualTimeZoneSuggestion[] newArray(int size) {
53 return new ManualTimeZoneSuggestion[size];
54 }
55 };
56
57 @NonNull private final String mZoneId;
58 @Nullable private ArrayList<String> mDebugInfo;
59
60 public ManualTimeZoneSuggestion(@NonNull String zoneId) {
61 mZoneId = Objects.requireNonNull(zoneId);
62 }
63
64 private static ManualTimeZoneSuggestion createFromParcel(Parcel in) {
65 String zoneId = in.readString();
66 ManualTimeZoneSuggestion suggestion = new ManualTimeZoneSuggestion(zoneId);
67 @SuppressWarnings("unchecked")
68 ArrayList<String> debugInfo = (ArrayList<String>) in.readArrayList(null /* classLoader */, java.lang.String.class);
69 suggestion.mDebugInfo = debugInfo;
70 return suggestion;
71 }
72
73 @Override
74 public int describeContents() {
75 return 0;
76 }
77
78 @Override
79 public void writeToParcel(@NonNull Parcel dest, int flags) {
80 dest.writeString(mZoneId);
81 dest.writeList(mDebugInfo);
82 }
83
84 @NonNull
85 public String getZoneId() {
86 return mZoneId;
87 }
88
89 @NonNull
90 public List<String> getDebugInfo() {
91 return mDebugInfo == null
92 ? Collections.emptyList() : Collections.unmodifiableList(mDebugInfo);
93 }
94
95 /**
96 * Associates information with the instance that can be useful for debugging / logging. The
97 * information is present in {@link #toString()} but is not considered for
98 * {@link #equals(Object)} and {@link #hashCode()}.
99 */
100 public void addDebugInfo(String... debugInfos) {
101 if (mDebugInfo == null) {
102 mDebugInfo = new ArrayList<>();
103 }
104 mDebugInfo.addAll(Arrays.asList(debugInfos));
105 }
106
107 @Override
108 public boolean equals(@Nullable Object o) {
109 if (this == o) {
110 return true;
111 }
112 if (o == null || getClass() != o.getClass()) {
113 return false;
114 }
115 ManualTimeZoneSuggestion
116 that = (ManualTimeZoneSuggestion) o;
117 return Objects.equals(mZoneId, that.mZoneId);
118 }
119
120 @Override
121 public int hashCode() {
122 return Objects.hash(mZoneId);
123 }
124
125 @Override
126 public String toString() {
127 return "ManualTimeZoneSuggestion{"
128 + "mZoneId=" + mZoneId
129 + ", mDebugInfo=" + mDebugInfo
130 + '}';
131 }
132
133 /** @hide */
134 public static ManualTimeZoneSuggestion parseCommandLineArg(@NonNull ShellCommand cmd) {
135 String zoneId = null;
136 String opt;
137 while ((opt = cmd.getNextArg()) != null) {
138 switch (opt) {
139 case "--zone_id": {
140 zoneId = cmd.getNextArgRequired();
141 break;
142 }
143 default: {
144 throw new IllegalArgumentException("Unknown option: " + opt);
145 }
146 }
147 }
148 ManualTimeZoneSuggestion suggestion = new ManualTimeZoneSuggestion(zoneId);
149 suggestion.addDebugInfo("Command line injection");
150 return suggestion;
151 }
152
153 /** @hide */
154 public static void printCommandLineOpts(@NonNull PrintWriter pw) {
155 pw.println("Manual suggestion options:");
156 pw.println(" --zone_id <Olson ID>");
157 pw.println();
158 pw.println("See " + ManualTimeZoneSuggestion.class.getName() + " for more information");
159 }
160 }