| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app.timedetector; |
| 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 |
import android.os.TimestampedValue; |
| 25 |
|
| 26 |
import java.io.PrintWriter; |
| 27 |
import java.util.ArrayList; |
| 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 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public final class TelephonyTimeSuggestion implements Parcelable { |
| 56 |
|
| 57 |
|
| 58 |
public static final @NonNull Parcelable.Creator<TelephonyTimeSuggestion> CREATOR = |
| 59 |
new Parcelable.Creator<TelephonyTimeSuggestion>() { |
| 60 |
public TelephonyTimeSuggestion createFromParcel(Parcel in) { |
| 61 |
return TelephonyTimeSuggestion.createFromParcel(in); |
| 62 |
} |
| 63 |
|
| 64 |
public TelephonyTimeSuggestion[] newArray(int size) { |
| 65 |
return new TelephonyTimeSuggestion[size]; |
| 66 |
} |
| 67 |
}; |
| 68 |
|
| 69 |
private final int mSlotIndex; |
| 70 |
@Nullable private final TimestampedValue<Long> mUnixEpochTime; |
| 71 |
@Nullable private ArrayList<String> mDebugInfo; |
| 72 |
|
| 73 |
private TelephonyTimeSuggestion(Builder builder) { |
| 74 |
mSlotIndex = builder.mSlotIndex; |
| 75 |
mUnixEpochTime = builder.mUnixEpochTime; |
| 76 |
mDebugInfo = builder.mDebugInfo != null ? new ArrayList<>(builder.mDebugInfo) : null; |
| 77 |
} |
| 78 |
|
| 79 |
private static TelephonyTimeSuggestion createFromParcel(Parcel in) { |
| 80 |
int slotIndex = in.readInt(); |
| 81 |
TimestampedValue<Long> unixEpochTime = |
| 82 |
in.readParcelable(null , android.os.TimestampedValue.class); |
| 83 |
TelephonyTimeSuggestion suggestion = new TelephonyTimeSuggestion.Builder(slotIndex) |
| 84 |
.setUnixEpochTime(unixEpochTime) |
| 85 |
.build(); |
| 86 |
@SuppressWarnings("unchecked") |
| 87 |
ArrayList<String> debugInfo = (ArrayList<String>) in.readArrayList( |
| 88 |
null , java.lang.String.class); |
| 89 |
if (debugInfo != null) { |
| 90 |
suggestion.addDebugInfo(debugInfo); |
| 91 |
} |
| 92 |
return suggestion; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public static TelephonyTimeSuggestion parseCommandLineArg(@NonNull ShellCommand cmd) |
| 97 |
throws IllegalArgumentException { |
| 98 |
Integer slotIndex = null; |
| 99 |
Long referenceTimeMillis = null; |
| 100 |
Long unixEpochTimeMillis = null; |
| 101 |
String opt; |
| 102 |
while ((opt = cmd.getNextArg()) != null) { |
| 103 |
switch (opt) { |
| 104 |
case "--slot_index": { |
| 105 |
slotIndex = Integer.parseInt(cmd.getNextArgRequired()); |
| 106 |
break; |
| 107 |
} |
| 108 |
case "--reference_time": { |
| 109 |
referenceTimeMillis = Long.parseLong(cmd.getNextArgRequired()); |
| 110 |
break; |
| 111 |
} |
| 112 |
case "--unix_epoch_time": { |
| 113 |
unixEpochTimeMillis = Long.parseLong(cmd.getNextArgRequired()); |
| 114 |
break; |
| 115 |
} |
| 116 |
default: { |
| 117 |
throw new IllegalArgumentException("Unknown option: " + opt); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
if (slotIndex == null) { |
| 123 |
throw new IllegalArgumentException("No slotIndex specified."); |
| 124 |
} |
| 125 |
if (referenceTimeMillis == null) { |
| 126 |
throw new IllegalArgumentException("No referenceTimeMillis specified."); |
| 127 |
} |
| 128 |
if (unixEpochTimeMillis == null) { |
| 129 |
throw new IllegalArgumentException("No unixEpochTimeMillis specified."); |
| 130 |
} |
| 131 |
|
| 132 |
TimestampedValue<Long> timeSignal = |
| 133 |
new TimestampedValue<>(referenceTimeMillis, unixEpochTimeMillis); |
| 134 |
Builder builder = new Builder(slotIndex) |
| 135 |
.setUnixEpochTime(timeSignal) |
| 136 |
.addDebugInfo("Command line injection"); |
| 137 |
return builder.build(); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public static void printCommandLineOpts(PrintWriter pw) { |
| 142 |
pw.println("Telephony suggestion options:"); |
| 143 |
pw.println(" --slot_index <number>"); |
| 144 |
pw.println(" --reference_time <elapsed realtime millis>"); |
| 145 |
pw.println(" --unix_epoch_time <Unix epoch time millis>"); |
| 146 |
pw.println(); |
| 147 |
pw.println("See " + TelephonyTimeSuggestion.class.getName() + " for more information"); |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public int describeContents() { |
| 152 |
return 0; |
| 153 |
} |
| 154 |
|
| 155 |
@Override |
| 156 |
public void writeToParcel(@NonNull Parcel dest, int flags) { |
| 157 |
dest.writeInt(mSlotIndex); |
| 158 |
dest.writeParcelable(mUnixEpochTime, 0); |
| 159 |
dest.writeList(mDebugInfo); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public int getSlotIndex() { |
| 168 |
return mSlotIndex; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
@Nullable |
| 177 |
public TimestampedValue<Long> getUnixEpochTime() { |
| 178 |
return mUnixEpochTime; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
@NonNull |
| 187 |
public List<String> getDebugInfo() { |
| 188 |
return mDebugInfo == null |
| 189 |
? Collections.emptyList() : Collections.unmodifiableList(mDebugInfo); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
public void addDebugInfo(@NonNull String debugInfo) { |
| 198 |
if (mDebugInfo == null) { |
| 199 |
mDebugInfo = new ArrayList<>(); |
| 200 |
} |
| 201 |
mDebugInfo.add(debugInfo); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public void addDebugInfo(@NonNull List<String> debugInfo) { |
| 210 |
if (mDebugInfo == null) { |
| 211 |
mDebugInfo = new ArrayList<>(debugInfo.size()); |
| 212 |
} |
| 213 |
mDebugInfo.addAll(debugInfo); |
| 214 |
} |
| 215 |
|
| 216 |
@Override |
| 217 |
public boolean equals(@Nullable Object o) { |
| 218 |
if (this == o) { |
| 219 |
return true; |
| 220 |
} |
| 221 |
if (o == null || getClass() != o.getClass()) { |
| 222 |
return false; |
| 223 |
} |
| 224 |
TelephonyTimeSuggestion that = (TelephonyTimeSuggestion) o; |
| 225 |
return mSlotIndex == that.mSlotIndex |
| 226 |
&& Objects.equals(mUnixEpochTime, that.mUnixEpochTime); |
| 227 |
} |
| 228 |
|
| 229 |
@Override |
| 230 |
public int hashCode() { |
| 231 |
return Objects.hash(mSlotIndex, mUnixEpochTime); |
| 232 |
} |
| 233 |
|
| 234 |
@Override |
| 235 |
public String toString() { |
| 236 |
return "TelephonyTimeSuggestion{" |
| 237 |
+ "mSlotIndex='" + mSlotIndex + '\'' |
| 238 |
+ ", mUnixEpochTime=" + mUnixEpochTime |
| 239 |
+ ", mDebugInfo=" + mDebugInfo |
| 240 |
+ '}'; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
public static final class Builder { |
| 249 |
private final int mSlotIndex; |
| 250 |
@Nullable private TimestampedValue<Long> mUnixEpochTime; |
| 251 |
@Nullable private List<String> mDebugInfo; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public Builder(int slotIndex) { |
| 259 |
mSlotIndex = slotIndex; |
| 260 |
} |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
@NonNull |
| 268 |
public Builder setUnixEpochTime(@Nullable TimestampedValue<Long> unixEpochTime) { |
| 269 |
if (unixEpochTime != null) { |
| 270 |
|
| 271 |
Objects.requireNonNull(unixEpochTime.getValue()); |
| 272 |
} |
| 273 |
|
| 274 |
mUnixEpochTime = unixEpochTime; |
| 275 |
return this; |
| 276 |
} |
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
@NonNull |
| 284 |
public Builder addDebugInfo(@NonNull String debugInfo) { |
| 285 |
if (mDebugInfo == null) { |
| 286 |
mDebugInfo = new ArrayList<>(); |
| 287 |
} |
| 288 |
mDebugInfo.add(debugInfo); |
| 289 |
return this; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
@NonNull |
| 294 |
public TelephonyTimeSuggestion build() { |
| 295 |
return new TelephonyTimeSuggestion(this); |
| 296 |
} |
| 297 |
} |
| 298 |
} |