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     private static final String SHELL_CMD_PREFIX = "cmd " + SERVICE_NAME + " ";
71 
72     private final DeviceShellCommandExecutor mShellCommandExecutor;
73 
TimeZoneDetectorShellHelper(DeviceShellCommandExecutor shellCommandExecutor)74     public TimeZoneDetectorShellHelper(DeviceShellCommandExecutor shellCommandExecutor) {
75         mShellCommandExecutor = Objects.requireNonNull(shellCommandExecutor);
76     }
77 
78     /** Executes "is_auto_detection_enabled" */
isAutoDetectionEnabled()79     public boolean isAutoDetectionEnabled() throws Exception {
80         return mShellCommandExecutor.executeToBoolean(
81                 SHELL_CMD_PREFIX + SHELL_COMMAND_IS_AUTO_DETECTION_ENABLED);
82     }
83 
84     /** Executes "set_auto_detection_enabled" */
setAutoDetectionEnabled(boolean enabled)85     public void setAutoDetectionEnabled(boolean enabled) throws Exception {
86         String cmd = String.format("%s %s", SHELL_COMMAND_SET_AUTO_DETECTION_ENABLED, enabled);
87         mShellCommandExecutor.executeToTrimmedString(SHELL_CMD_PREFIX + cmd);
88     }
89 
90     /** Executes "is_geo_detection_enabled" */
isGeoDetectionEnabled()91     public boolean isGeoDetectionEnabled() throws Exception {
92         return mShellCommandExecutor.executeToBoolean(
93                 SHELL_CMD_PREFIX + SHELL_COMMAND_IS_GEO_DETECTION_ENABLED);
94     }
95 
96     /** Executes "set_geo_detection_enabled" */
setGeoDetectionEnabled(boolean enabled)97     public void setGeoDetectionEnabled(boolean enabled) throws Exception {
98         String cmd = String.format("%s %s", SHELL_COMMAND_SET_GEO_DETECTION_ENABLED, enabled);
99         mShellCommandExecutor.executeToTrimmedString(SHELL_CMD_PREFIX + cmd);
100     }
101 
102     /** Executes "is_geo_detection_supported" */
isGeoDetectionSupported()103     public boolean isGeoDetectionSupported() throws Exception {
104         return mShellCommandExecutor.executeToBoolean(
105                 SHELL_CMD_PREFIX + SHELL_COMMAND_IS_GEO_DETECTION_SUPPORTED);
106     }
107 
108     /** Executes "is_telephony_detection_supported" */
isTelephonyDetectionSupported()109     public boolean isTelephonyDetectionSupported() throws Exception {
110         return mShellCommandExecutor.executeToBoolean(
111                 SHELL_CMD_PREFIX + SHELL_COMMAND_IS_TELEPHONY_DETECTION_SUPPORTED);
112     }
113 }
114