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 static android.app.time.cts.shell.DeviceShellCommandExecutor.parseBytesAsBoolean;
19 
20 import java.util.Objects;
21 
22 /**
23  * A class for interacting with the {@code location} service via the shell "cmd" command-line
24  * interface.
25  */
26 public class LocationShellHelper {
27 
28     /**
29      * The name of the service for shell commands,
30      */
31     private static final String SERVICE_NAME = "location";
32 
33     /**
34      * A shell command that sets the current user's "location enabled" setting value.
35      */
36     private static final String SHELL_COMMAND_SET_LOCATION_ENABLED = "set-location-enabled";
37 
38     /**
39      * A shell command that gets the current user's "location enabled" setting value.
40      */
41     private static final String SHELL_COMMAND_IS_LOCATION_ENABLED = "is-location-enabled";
42 
43     private static final String SHELL_CMD_PREFIX = "cmd " + SERVICE_NAME + " ";
44 
45     private final DeviceShellCommandExecutor mShellCommandExecutor;
46 
LocationShellHelper(DeviceShellCommandExecutor shellCommandExecutor)47     public LocationShellHelper(DeviceShellCommandExecutor shellCommandExecutor) {
48         mShellCommandExecutor = Objects.requireNonNull(shellCommandExecutor);
49     }
50 
51     /** Executes "is-location-enabled". */
isLocationEnabledForCurrentUser()52     public boolean isLocationEnabledForCurrentUser() throws Exception {
53         return mShellCommandExecutor.executeToBoolean(
54                 SHELL_CMD_PREFIX + SHELL_COMMAND_IS_LOCATION_ENABLED);
55     }
56 
57     /** Executes "set-location-enabled". */
setLocationEnabledForCurrentUser(boolean enabled)58     public void setLocationEnabledForCurrentUser(boolean enabled) throws Exception {
59         String cmd = String.format("%s %s", SHELL_COMMAND_SET_LOCATION_ENABLED, enabled);
60         mShellCommandExecutor.executeToTrimmedString(SHELL_CMD_PREFIX + cmd);
61     }
62 }
63