| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 , 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 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 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 |
|
| 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 |
} |