1 /*
2  * Copyright (C) 2023 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 
17 package com.google.android.mobly.snippet.bundled;
18 
19 import android.platform.helpers.HelperAccessor;
20 import android.platform.helpers.IAutoBluetoothSettingsHelper;
21 import android.platform.helpers.IAutoDateTimeSettingsHelper;
22 import android.platform.helpers.IAutoSettingHelper;
23 import android.platform.helpers.SettingsConstants;
24 
25 import com.google.android.mobly.snippet.Snippet;
26 import com.google.android.mobly.snippet.rpc.Rpc;
27 
28 /** Snippet class for exposing Settings APIs. */
29 public class SettingsSnippet implements Snippet {
30 
31     private HelperAccessor<IAutoSettingHelper> mSettingsHelper;
32     private HelperAccessor<IAutoBluetoothSettingsHelper> mBluetoothSettingsHelper;
33 
34     private static String sBluetoothSettings = "OPEN_BLUETOOTH_SETTINGS_WORKFLOW";
35     private HelperAccessor<IAutoDateTimeSettingsHelper> mDateTimeSettingsHelper;
36 
SettingsSnippet()37     public SettingsSnippet() {
38         mSettingsHelper = new HelperAccessor<>(IAutoSettingHelper.class);
39         mBluetoothSettingsHelper = new HelperAccessor<>(IAutoBluetoothSettingsHelper.class);
40         mDateTimeSettingsHelper = new HelperAccessor<>(IAutoDateTimeSettingsHelper.class);
41     }
42 
43     @Rpc(description = "Return whether the bluetooth preference button is checked")
isBluetoothPreferenceChecked()44     public boolean isBluetoothPreferenceChecked() {
45         return mBluetoothSettingsHelper.get().isBluetoothPreferenceChecked();
46     }
47 
48     @Rpc(description = "Return whether the media preference button is checked")
isMediaPreferenceChecked()49     public boolean isMediaPreferenceChecked() {
50         return mBluetoothSettingsHelper.get().isMediaPreferenceChecked();
51     }
52 
53     @Rpc(description = "Return whether the media preference button is enabled")
isMediaPreferenceEnabled()54     public boolean isMediaPreferenceEnabled() {
55         return mBluetoothSettingsHelper.get().isMediaPreferenceEnabled();
56     }
57 
58     @Rpc(description = "Return whether the phone preference button is checked.")
isPhonePreferenceChecked()59     public boolean isPhonePreferenceChecked() {
60         return mBluetoothSettingsHelper.get().isPhonePreferenceChecked();
61     }
62 
63     @Rpc(description = "Return whether the phone preference button is enabled")
isPhonePreferenceEnabled()64     public boolean isPhonePreferenceEnabled() {
65         return mBluetoothSettingsHelper.get().isPhonePreferenceEnabled();
66     }
67 
68     @Rpc(
69             description =
70                     "Get the device summary of a device "
71                             + "whose level two connection screen is currently open.")
getDeviceSummary()72     public String getDeviceSummary() {
73         return mBluetoothSettingsHelper.get().getDeviceSummary();
74     }
75 
76     @Rpc(description = "Open system settings.")
openSystemSettings()77     public void openSystemSettings() {
78         mSettingsHelper.get().openFullSettings();
79     }
80 
81     @Rpc(description = "Open the bluetooth settings (from home screen)")
openBluetoothSettings()82     public void openBluetoothSettings() {
83         mSettingsHelper.get().openSetting(sBluetoothSettings);
84     }
85 
86     /** Press the phone preference toggle button */
87     @Rpc(description = "Press the phone toggle on a entry under 'Paired Devices'")
pressPhoneToggleOnDevice(String deviceName)88     public void pressPhoneToggleOnDevice(String deviceName) {
89         mBluetoothSettingsHelper.get().pressPhoneToggleOnDevice(deviceName);
90     }
91 
92     @Rpc(description = "Press the bluetooth toggle on a entry under 'Paired Devices'")
pressBluetoothToggleOnDevice(String deviceName)93     public void pressBluetoothToggleOnDevice(String deviceName) {
94         mBluetoothSettingsHelper.get().pressBluetoothToggleOnDevice(deviceName);
95     }
96 
97     /**
98      * RPC to press the bluetooth toggle on a entry under 'Paired Devices'"
99      *
100      * @param deviceName - The listed device whose bluetooth toggle should be switched.
101      */
102     @Rpc(description = "Press the bluetooth toggle on a entry under 'Paired Devices'")
pressMediaToggleOnDevice(String deviceName)103     public void pressMediaToggleOnDevice(String deviceName) {
104         mBluetoothSettingsHelper.get().pressMediaToggleOnDevice(deviceName);
105     }
106 
107     @Rpc(description = "Press device on device list.")
pressDeviceInBluetoothSettings(String deviceName)108     public void pressDeviceInBluetoothSettings(String deviceName) {
109         mBluetoothSettingsHelper.get().pressDevice(deviceName);
110     }
111 
112     @Rpc(description = "Press an entry listed under 'Paired Devices'")
pressDeviceName(String deviceName)113     public void pressDeviceName(String deviceName) {
114         mBluetoothSettingsHelper.get().pressDevice(deviceName);
115     }
116 
117     @Rpc(
118             description =
119                     "Get the connection status of a device "
120                             + "whose level two connection screen is currently open.")
deviceIsConnected()121     public void deviceIsConnected() {
122         mBluetoothSettingsHelper.get().isConnected();
123     }
124 
125     @Rpc(description = "Press the Forget button on a currently open Level Two connection screen")
pressForget()126     public void pressForget() {
127         mBluetoothSettingsHelper.get().pressForget();
128     }
129 
130     @Rpc(description = "Get the device timezone")
getTimeZone()131     public String getTimeZone() {
132         return mDateTimeSettingsHelper.get().getTimeZone();
133     }
134 
135     @Rpc(description = "Open Date time Setting")
setTimeZone(String timezone)136     public void setTimeZone(String timezone) {
137         mSettingsHelper.get().openSetting(SettingsConstants.DATE_AND_TIME_SETTINGS);
138         mDateTimeSettingsHelper.get().setTimeZone(timezone);
139     }
140 
141     @Override
shutdown()142     public void shutdown() {}
143 }
144