1 /*
2  * Copyright (C) 2021 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 package android.app.time.cts.shell;
17 
18 import java.util.Objects;
19 
20 /**
21  * A class for interacting with the {@code time_zone_detector} service via the shell "cmd"
22  * command-line interface.
23  */
24 public final class TimeZoneDetectorShellHelper {
25 
26     /**
27      * The name of the service for shell commands.
28      */
29     private static final String SERVICE_NAME = "time_zone_detector";
30 
31     /**
32      * A shell command that prints the current "auto time zone detection" global setting value.
33      */
34     private static final String SHELL_COMMAND_IS_AUTO_DETECTION_ENABLED =
35             "is_auto_detection_enabled";
36 
37     /**
38      * A shell command that sets the current "auto time zone detection" global setting value.
39      */
40     private static final String SHELL_COMMAND_SET_AUTO_DETECTION_ENABLED =
41             "set_auto_detection_enabled";
42 
43     /**
44      * A shell command that prints whether the telephony-based time zone detection feature is
45      * supported on the device.
46      */
47     private static final String SHELL_COMMAND_IS_TELEPHONY_DETECTION_SUPPORTED =
48             "is_telephony_detection_supported";
49 
50     /**
51      * A shell command that prints whether the geolocation-based time zone detection feature is
52      * supported on the device.
53      */
54     private static final String SHELL_COMMAND_IS_GEO_DETECTION_SUPPORTED =
55             "is_geo_detection_supported";
56 
57     /**
58      * A shell command that prints the current user's "location-based time zone detection enabled"
59      * setting.
60      */
61     private static final String SHELL_COMMAND_IS_GEO_DETECTION_ENABLED = "is_geo_detection_enabled";
62 
63     /**
64      * A shell command that sets the current user's "location-based time zone detection enabled"
65      * setting.
66      */
67     private static final String SHELL_COMMAND_SET_GEO_DETECTION_ENABLED =
68             "set_geo_detection_enabled";
69 
70     /**
71      * A shell command that sets the current time zone state for testing.
72      * @hide
73      */
74     private static final String SHELL_COMMAND_SET_TIME_ZONE_STATE = "set_time_zone_state_for_tests";
75 
76     private static final String SHELL_CMD_PREFIX = "cmd " + SERVICE_NAME + " ";
77 
78     private static final String SETTINGS_CMD_PREFIX = "settings ";
79     private static final String SETTING_AUTO_TIME_ZONE_EXPLICIT = "auto_time_zone_explicit";
80     private static final String SETTING_AUTO_TIME_ZONE_EXPLICIT_IS_SET = "1";
81     private static final String SETTINGS_NAMESPACE_GLOBAL = "global";
82 
83     private final DeviceShellCommandExecutor mShellCommandExecutor;
84 
TimeZoneDetectorShellHelper(DeviceShellCommandExecutor shellCommandExecutor)85     public TimeZoneDetectorShellHelper(DeviceShellCommandExecutor shellCommandExecutor) {
86         mShellCommandExecutor = Objects.requireNonNull(shellCommandExecutor);
87     }
88 
89     /** Executes "is_auto_detection_enabled" */
isAutoDetectionEnabled()90     public boolean isAutoDetectionEnabled() throws Exception {
91         return mShellCommandExecutor.executeToBoolean(
92                 SHELL_CMD_PREFIX + SHELL_COMMAND_IS_AUTO_DETECTION_ENABLED);
93     }
94 
95     /** Executes "set_auto_detection_enabled" */
setAutoDetectionEnabled(boolean enabled)96     public void setAutoDetectionEnabled(boolean enabled) throws Exception {
97         String cmd = String.format("%s %s", SHELL_COMMAND_SET_AUTO_DETECTION_ENABLED, enabled);
98         mShellCommandExecutor.executeToTrimmedString(SHELL_CMD_PREFIX + cmd);
99     }
100 
101     /** Executes "is_geo_detection_enabled" */
isGeoDetectionEnabled()102     public boolean isGeoDetectionEnabled() throws Exception {
103         return mShellCommandExecutor.executeToBoolean(
104                 SHELL_CMD_PREFIX + SHELL_COMMAND_IS_GEO_DETECTION_ENABLED);
105     }
106 
107     /** Executes "set_geo_detection_enabled" */
setGeoDetectionEnabled(boolean enabled)108     public void setGeoDetectionEnabled(boolean enabled) throws Exception {
109         String cmd = String.format("%s %s", SHELL_COMMAND_SET_GEO_DETECTION_ENABLED, enabled);
110         mShellCommandExecutor.executeToTrimmedString(SHELL_CMD_PREFIX + cmd);
111     }
112 
113     /** Executes "is_geo_detection_supported" */
isGeoDetectionSupported()114     public boolean isGeoDetectionSupported() throws Exception {
115         return mShellCommandExecutor.executeToBoolean(
116                 SHELL_CMD_PREFIX + SHELL_COMMAND_IS_GEO_DETECTION_SUPPORTED);
117     }
118 
119     /** Executes "is_telephony_detection_supported" */
isTelephonyDetectionSupported()120     public boolean isTelephonyDetectionSupported() throws Exception {
121         return mShellCommandExecutor.executeToBoolean(
122                 SHELL_CMD_PREFIX + SHELL_COMMAND_IS_TELEPHONY_DETECTION_SUPPORTED);
123     }
124 
125     /** Executes "set_time_zone_state_for_tests" */
setTimeZoneState(String timeZoneId, boolean userShouldConfirmId)126     public void setTimeZoneState(String timeZoneId, boolean userShouldConfirmId) throws Exception {
127         String cmd = String.format("%s --zone_id %s"
128                         + " --user_should_confirm_id %s",
129                 SHELL_COMMAND_SET_TIME_ZONE_STATE, timeZoneId,
130                 userShouldConfirmId);
131         mShellCommandExecutor.executeToTrimmedString(SHELL_CMD_PREFIX + cmd);
132     }
133 
isAutoTimeZoneEnabledExplicitly()134     public boolean isAutoTimeZoneEnabledExplicitly() throws Exception {
135         // This command uses the "settings" command line for simplicity, instead of requiring
136         // dedicated command line support from the time_zone_detector.
137         String cmd = String.format("get %s %s", SETTINGS_NAMESPACE_GLOBAL,
138                 SETTING_AUTO_TIME_ZONE_EXPLICIT);
139         String result = mShellCommandExecutor.executeToString(SETTINGS_CMD_PREFIX + cmd);
140         // Expect "null" or "1".
141         return SETTING_AUTO_TIME_ZONE_EXPLICIT_IS_SET.equals(result);
142     }
143 
clearAutoTimeZoneEnabledExplicitly()144     public void clearAutoTimeZoneEnabledExplicitly() throws Exception {
145         // This command uses the "settings" command line for simplicity, instead of requiring
146         // dedicated command line support from the time_zone_detector.
147         String cmd = String.format("delete %s %s", SETTINGS_NAMESPACE_GLOBAL,
148                 SETTING_AUTO_TIME_ZONE_EXPLICIT);
149         mShellCommandExecutor.executeToString(SETTINGS_CMD_PREFIX + cmd);
150     }
151 
setAutoTimeZoneEnabledExplicitly()152     public void setAutoTimeZoneEnabledExplicitly() throws Exception {
153         // This command uses the "settings" command line for simplicity, instead of requiring
154         // dedicated command line support from the time_zone_detector.
155         String cmd = String.format("put %s %s %s", SETTINGS_NAMESPACE_GLOBAL,
156                 SETTING_AUTO_TIME_ZONE_EXPLICIT, SETTING_AUTO_TIME_ZONE_EXPLICIT_IS_SET);
157         mShellCommandExecutor.executeToString(SETTINGS_CMD_PREFIX + cmd);
158     }
159 }
160