From 9e9fe6865de1c4eda671e2525cbe28f83b913a32 Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Mon, 30 May 2022 14:29:31 +0100 Subject: [PATCH] Add command-line support for enabling auto time Adds support for toggling the global auto time setting via the command line. Useful for manual and automated tests, and also makes time_detector consistent with time_zone_detector. This also helps to confirm that the new config-update code should work when used by SettingsUI. Manually tested using a combination of the command line and SettingsUI. Bug: 172891783 Bug: 229740080 Test: atest com.android.server.timedetector android.app.time Test: adb shell cmd time_detector is_auto_detection_enabled Test: adb shell cmd time_detector set_auto_detection_enabled Change-Id: I39ce8c9381c2eef3f0fb5a42b54bc9c68e545f67 --- .../app/timedetector/TimeDetector.java | 6 ++++++ .../TimeDetectorShellCommand.java | 21 +++++++++++++++++++ .../TimeZoneDetectorShellCommand.java | 3 +++ 3 files changed, 30 insertions(+) diff --git a/core/java/android/app/timedetector/TimeDetector.java b/core/java/android/app/timedetector/TimeDetector.java index f0d7776116206..abfba3a54a43d 100644 --- a/core/java/android/app/timedetector/TimeDetector.java +++ b/core/java/android/app/timedetector/TimeDetector.java @@ -43,6 +43,12 @@ public interface TimeDetector { */ String SHELL_COMMAND_IS_AUTO_DETECTION_ENABLED = "is_auto_detection_enabled"; + /** + * A shell command that sets the current "auto time detection" global setting value. + * @hide + */ + String SHELL_COMMAND_SET_AUTO_DETECTION_ENABLED = "set_auto_detection_enabled"; + /** * A shell command that injects a manual time suggestion. * @hide diff --git a/services/core/java/com/android/server/timedetector/TimeDetectorShellCommand.java b/services/core/java/com/android/server/timedetector/TimeDetectorShellCommand.java index adc416bf7d753..211ebe4fbb095 100644 --- a/services/core/java/com/android/server/timedetector/TimeDetectorShellCommand.java +++ b/services/core/java/com/android/server/timedetector/TimeDetectorShellCommand.java @@ -17,6 +17,7 @@ package com.android.server.timedetector; import static android.app.timedetector.TimeDetector.SHELL_COMMAND_IS_AUTO_DETECTION_ENABLED; import static android.app.timedetector.TimeDetector.SHELL_COMMAND_SERVICE_NAME; +import static android.app.timedetector.TimeDetector.SHELL_COMMAND_SET_AUTO_DETECTION_ENABLED; import static android.app.timedetector.TimeDetector.SHELL_COMMAND_SUGGEST_EXTERNAL_TIME; import static android.app.timedetector.TimeDetector.SHELL_COMMAND_SUGGEST_GNSS_TIME; import static android.app.timedetector.TimeDetector.SHELL_COMMAND_SUGGEST_MANUAL_TIME; @@ -28,11 +29,13 @@ import static com.android.server.timedetector.ServerFlags.KEY_TIME_DETECTOR_LOWE import static com.android.server.timedetector.ServerFlags.KEY_TIME_DETECTOR_ORIGIN_PRIORITIES_OVERRIDE; import android.app.time.ExternalTimeSuggestion; +import android.app.time.TimeConfiguration; import android.app.timedetector.GnssTimeSuggestion; import android.app.timedetector.ManualTimeSuggestion; import android.app.timedetector.NetworkTimeSuggestion; import android.app.timedetector.TelephonyTimeSuggestion; import android.os.ShellCommand; +import android.os.UserHandle; import java.io.PrintWriter; import java.util.function.Consumer; @@ -56,6 +59,8 @@ class TimeDetectorShellCommand extends ShellCommand { switch (cmd) { case SHELL_COMMAND_IS_AUTO_DETECTION_ENABLED: return runIsAutoDetectionEnabled(); + case SHELL_COMMAND_SET_AUTO_DETECTION_ENABLED: + return runSetAutoDetectionEnabled(); case SHELL_COMMAND_SUGGEST_MANUAL_TIME: return runSuggestManualTime(); case SHELL_COMMAND_SUGGEST_TELEPHONY_TIME: @@ -81,6 +86,15 @@ class TimeDetectorShellCommand extends ShellCommand { return 0; } + private int runSetAutoDetectionEnabled() { + boolean enabled = Boolean.parseBoolean(getNextArgRequired()); + int userId = UserHandle.USER_CURRENT; + TimeConfiguration configuration = new TimeConfiguration.Builder() + .setAutoDetectionEnabled(enabled) + .build(); + return mInterface.updateConfiguration(userId, configuration) ? 0 : 1; + } + private int runSuggestManualTime() { return runSuggestTime( () -> ManualTimeSuggestion.parseCommandLineArg(this), @@ -136,12 +150,19 @@ class TimeDetectorShellCommand extends ShellCommand { pw.printf(" Print this help text.\n"); pw.printf(" %s\n", SHELL_COMMAND_IS_AUTO_DETECTION_ENABLED); pw.printf(" Prints true/false according to the automatic time detection setting.\n"); + pw.printf(" %s true|false\n", SHELL_COMMAND_SET_AUTO_DETECTION_ENABLED); + pw.printf(" Sets the automatic time detection setting.\n"); pw.println(); pw.printf(" %s \n", SHELL_COMMAND_SUGGEST_MANUAL_TIME); + pw.printf(" Suggests a time as if via the \"manual\" origin.\n"); pw.printf(" %s \n", SHELL_COMMAND_SUGGEST_TELEPHONY_TIME); + pw.printf(" Suggests a time as if via the \"telephony\" origin.\n"); pw.printf(" %s \n", SHELL_COMMAND_SUGGEST_NETWORK_TIME); + pw.printf(" Suggests a time as if via the \"network\" origin.\n"); pw.printf(" %s \n", SHELL_COMMAND_SUGGEST_GNSS_TIME); + pw.printf(" Suggests a time as if via the \"gnss\" origin.\n"); pw.printf(" %s \n", SHELL_COMMAND_SUGGEST_EXTERNAL_TIME); + pw.printf(" Suggests a time as if via the \"external\" origin.\n"); pw.println(); ManualTimeSuggestion.printCommandLineOpts(pw); pw.println(); diff --git a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorShellCommand.java b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorShellCommand.java index 1d72ca5413435..4d808ff231f2a 100644 --- a/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorShellCommand.java +++ b/services/core/java/com/android/server/timezonedetector/TimeZoneDetectorShellCommand.java @@ -221,8 +221,11 @@ class TimeZoneDetectorShellCommand extends ShellCommand { pw.println(); pw.printf(" %s \n", SHELL_COMMAND_SUGGEST_GEO_LOCATION_TIME_ZONE); + pw.printf(" Suggests a time zone as if via the \"location\" origin.\n"); pw.printf(" %s \n", SHELL_COMMAND_SUGGEST_MANUAL_TIME_ZONE); + pw.printf(" Suggests a time zone as if via the \"manual\" origin.\n"); pw.printf(" %s \n", SHELL_COMMAND_SUGGEST_TELEPHONY_TIME_ZONE); + pw.printf(" Suggests a time zone as if via the \"telephony\" origin.\n"); pw.printf(" %s\n", SHELL_COMMAND_DUMP_METRICS); pw.printf(" Dumps the service metrics to stdout for inspection.\n"); pw.println();