diff options
| author | Neil Fuller <nfuller@google.com> | 2020-02-21 14:35:30 +0000 |
|---|---|---|
| committer | Neil Fuller <nfuller@google.com> | 2020-03-11 18:44:53 +0000 |
| commit | 37aff8932fe478284209ffda9cd875c092c17941 (patch) | |
| tree | 9b142f208f0e4f25c9bdde7b0ea5670ecee25ecd /core/java | |
| parent | 920105c38ba977c8b56e16e4d969ead3acbc576f (diff) | |
Add command line support for testing tz detection
Add support for "adb shell cmd time_zone_detector".
This allows platform developers and future host tests to simulate
clients like telephony / SettingsUI and make changes to the
TimeZoneDetectorService state to mimic various real-world situations.
Example adb shell invocations:
Withdraw a previous telephony suggestion from slot_index=0:
cmd time_zone_detector suggestTelephonyTimeZone --suggestion --slot_index 0 \
--zone_id "_"
Make a new telephony suggestion from slot_index=0, with a quality of
TelephonyTimeZoneSuggestion.QUALITY_SINGLE_ZONE, and a matchType of
TelephonyTimeZoneSuggestion.MATCH_TYPE_NETWORK_COUNTRY_ONLY:
cmd time_zone_detector suggestTelephonyTimeZone --suggestion --slot_index 0 \
--zone_id "Europe/London" --quality single --match_type country
Make a manual (user) suggestion as if from SettingsUI:
cmd time_zone_detector suggestManualTimeZone --suggestion --zone_id America/Los_Angeles
Bug: 140712361
Test: Various command line invocations.
Test: atest core/tests/coretests/src/android/app/timezonedetector
Merged-In: I0f16868a526d2ea4b17acbd274cb2359f93166f5
Change-Id: I0f16868a526d2ea4b17acbd274cb2359f93166f5
(cherry picked from commit 4879487862e287679423c86c7a2be9ff5608e674)
Diffstat (limited to 'core/java')
| -rw-r--r-- | core/java/android/app/timezonedetector/ManualTimeZoneSuggestion.java | 30 | ||||
| -rw-r--r-- | core/java/android/app/timezonedetector/TelephonyTimeZoneSuggestion.java | 95 |
2 files changed, 125 insertions, 0 deletions
diff --git a/core/java/android/app/timezonedetector/ManualTimeZoneSuggestion.java b/core/java/android/app/timezonedetector/ManualTimeZoneSuggestion.java index 3a9adc72aab1..22e2efb1dedd 100644 --- a/core/java/android/app/timezonedetector/ManualTimeZoneSuggestion.java +++ b/core/java/android/app/timezonedetector/ManualTimeZoneSuggestion.java @@ -20,7 +20,9 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.os.Parcel; import android.os.Parcelable; +import android.os.ShellCommand; +import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -127,4 +129,32 @@ public final class ManualTimeZoneSuggestion implements Parcelable { + ", mDebugInfo=" + mDebugInfo + '}'; } + + /** @hide */ + public static ManualTimeZoneSuggestion parseCommandLineArg(@NonNull ShellCommand cmd) { + String zoneId = null; + String opt; + while ((opt = cmd.getNextArg()) != null) { + switch (opt) { + case "--zone_id": { + zoneId = cmd.getNextArgRequired(); + break; + } + default: { + throw new IllegalArgumentException("Unknown option: " + opt); + } + } + } + ManualTimeZoneSuggestion suggestion = new ManualTimeZoneSuggestion(zoneId); + suggestion.addDebugInfo("Command line injection"); + return suggestion; + } + + /** @hide */ + public static void printCommandLineOpts(@NonNull PrintWriter pw) { + pw.println("Manual suggestion options:"); + pw.println(" --zone_id <Olson ID>"); + pw.println(); + pw.println("See " + ManualTimeZoneSuggestion.class.getName() + " for more information"); + } } diff --git a/core/java/android/app/timezonedetector/TelephonyTimeZoneSuggestion.java b/core/java/android/app/timezonedetector/TelephonyTimeZoneSuggestion.java index 150c01d59899..430462b07c7a 100644 --- a/core/java/android/app/timezonedetector/TelephonyTimeZoneSuggestion.java +++ b/core/java/android/app/timezonedetector/TelephonyTimeZoneSuggestion.java @@ -21,7 +21,10 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.os.Parcel; import android.os.Parcelable; +import android.os.ShellCommand; +import android.text.TextUtils; +import java.io.PrintWriter; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; @@ -392,4 +395,96 @@ public final class TelephonyTimeZoneSuggestion implements Parcelable { return new TelephonyTimeZoneSuggestion(this); } } + + /** @hide */ + public static TelephonyTimeZoneSuggestion parseCommandLineArg(@NonNull ShellCommand cmd) + throws IllegalArgumentException { + Integer slotIndex = null; + String zoneId = null; + Integer quality = null; + Integer matchType = null; + String opt; + while ((opt = cmd.getNextArg()) != null) { + switch (opt) { + case "--slot_index": { + slotIndex = Integer.parseInt(cmd.getNextArgRequired()); + break; + } + case "--zone_id": { + zoneId = cmd.getNextArgRequired(); + break; + } + case "--quality": { + quality = parseQualityCommandLineArg(cmd.getNextArgRequired()); + break; + } + case "--match_type": { + matchType = parseMatchTypeCommandLineArg(cmd.getNextArgRequired()); + break; + } + default: { + throw new IllegalArgumentException("Unknown option: " + opt); + } + } + } + + if (slotIndex == null) { + throw new IllegalArgumentException("No slotIndex specified."); + } + + Builder builder = new Builder(slotIndex); + if (!(TextUtils.isEmpty(zoneId) || "_".equals(zoneId))) { + builder.setZoneId(zoneId); + } + if (quality != null) { + builder.setQuality(quality); + } + if (matchType != null) { + builder.setMatchType(matchType); + } + builder.addDebugInfo("Command line injection"); + return builder.build(); + } + + private static int parseQualityCommandLineArg(@NonNull String arg) { + switch (arg) { + case "single": + return QUALITY_SINGLE_ZONE; + case "multiple_same": + return QUALITY_MULTIPLE_ZONES_WITH_SAME_OFFSET; + case "multiple_different": + return QUALITY_MULTIPLE_ZONES_WITH_DIFFERENT_OFFSETS; + default: + throw new IllegalArgumentException("Unrecognized quality: " + arg); + } + } + + private static int parseMatchTypeCommandLineArg(@NonNull String arg) { + switch (arg) { + case "emulator": + return MATCH_TYPE_EMULATOR_ZONE_ID; + case "country_with_offset": + return MATCH_TYPE_NETWORK_COUNTRY_AND_OFFSET; + case "country": + return MATCH_TYPE_NETWORK_COUNTRY_ONLY; + case "test_network": + return MATCH_TYPE_TEST_NETWORK_OFFSET_ONLY; + default: + throw new IllegalArgumentException("Unrecognized match_type: " + arg); + } + } + + /** @hide */ + public static void printCommandLineOpts(@NonNull PrintWriter pw) { + pw.println("Telephony suggestion options:"); + pw.println(" --slot_index <number>"); + pw.println(" To withdraw a previous suggestion:"); + pw.println(" [--zone_id \"_\"]"); + pw.println(" To make a new suggestion:"); + pw.println(" --zone_id <Olson ID>"); + pw.println(" --quality <single|multiple_same|multiple_different>"); + pw.println(" --match_type <emulator|country_with_offset|country|test_network>"); + pw.println(); + pw.println("See " + TelephonyTimeZoneSuggestion.class.getName() + " for more information"); + } } |
