1 /*
2  * Copyright (C) 2012 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 com.android.cts.tradefed.targetprep;
17 
18 import com.android.tradefed.device.DeviceNotAvailableException;
19 import com.android.tradefed.device.ITestDevice;
20 
21 /**
22  * {@link SettingsToggler} sets settings by using the "adb shell content" command.
23  */
24 public class SettingsToggler {
25     private static final String GROUP_SECURE = "secure";
26     private static final String GROUP_GLOBAL = "global";
27 
28     /** Sets a setting by deleting and then inserting the string value. */
setString(ITestDevice device, String group, String name, String value)29     public static void setString(ITestDevice device, String group, String name, String value)
30             throws DeviceNotAvailableException {
31         deleteSetting(device, group, name);
32         device.executeShellCommand(
33                 "content insert"
34                 + " --uri content://settings/" + group
35                 + " --bind name:s:" + name
36                 + " --bind value:s:" + value);
37     }
38 
39     /** Sets a secure setting by deleting and then inserting the string value. */
setSecureString(ITestDevice device, String name, String value)40     public static void setSecureString(ITestDevice device, String name, String value)
41             throws DeviceNotAvailableException {
42         setString(device, GROUP_SECURE, name, value);
43     }
44 
45     /** Sets a global setting by deleting and then inserting the string value. */
setGlobalString(ITestDevice device, String name, String value)46     public static void setGlobalString(ITestDevice device, String name, String value)
47             throws DeviceNotAvailableException {
48         setString(device, GROUP_GLOBAL, name, value);
49     }
50 
51     /** Sets a setting by deleting and then inserting the int value. */
setInt(ITestDevice device, String group, String name, int value)52     public static void setInt(ITestDevice device, String group, String name, int value)
53             throws DeviceNotAvailableException {
54         deleteSetting(device, group, name);
55         device.executeShellCommand(
56                 "content insert"
57                 + " --uri content://settings/" + group
58                 + " --bind name:s:" + name
59                 + " --bind value:i:" + value);
60     }
61 
62     /** Sets a secure setting by deleting and then inserting the int value. */
setSecureInt(ITestDevice device, String name, int value)63     public static void setSecureInt(ITestDevice device, String name, int value)
64             throws DeviceNotAvailableException {
65         setInt(device, GROUP_SECURE, name, value);
66     }
67 
68     /** Sets a global setting by deleting and then inserting the int value. */
setGlobalInt(ITestDevice device, String name, int value)69     public static void setGlobalInt(ITestDevice device, String name, int value)
70             throws DeviceNotAvailableException {
71         setInt(device, GROUP_GLOBAL, name, value);
72     }
73 
updateString(ITestDevice device, String group, String name, String value)74     public static void updateString(ITestDevice device, String group, String name, String value)
75             throws DeviceNotAvailableException {
76         device.executeShellCommand(
77                 "content update"
78                 + " --uri content://settings/" + group
79                 + " --bind value:s:" + value
80                 + " --where \"name='" + name + "'\"");
81     }
82 
updateSecureString(ITestDevice device, String name, String value)83     public static void updateSecureString(ITestDevice device, String name, String value)
84             throws DeviceNotAvailableException {
85         updateString(device, GROUP_SECURE, name, value);
86     }
87 
updateGlobalString(ITestDevice device, String name, String value)88     public static void updateGlobalString(ITestDevice device, String name, String value)
89             throws DeviceNotAvailableException {
90         updateString(device, GROUP_GLOBAL, name, value);
91     }
92 
updateInt(ITestDevice device, String group, String name, int value)93     public static void updateInt(ITestDevice device, String group, String name, int value)
94             throws DeviceNotAvailableException {
95         device.executeShellCommand(
96                 "content update"
97                 + " --uri content://settings/" + group
98                 + " --bind value:i:" + value
99                 + " --where \"name='" + name + "'\"");
100     }
101 
updateSecureInt(ITestDevice device, String name, int value)102     public static void updateSecureInt(ITestDevice device, String name, int value)
103             throws DeviceNotAvailableException {
104         updateInt(device, GROUP_SECURE, name, value);
105     }
106 
updateGlobalInt(ITestDevice device, String name, int value)107     public static void updateGlobalInt(ITestDevice device, String name, int value)
108             throws DeviceNotAvailableException {
109         updateInt(device, GROUP_GLOBAL, name, value);
110     }
111 
deleteSetting(ITestDevice device, String group, String name)112     private static void deleteSetting(ITestDevice device, String group, String name)
113             throws DeviceNotAvailableException {
114         device.executeShellCommand(
115                 "content delete"
116                 + " --uri content://settings/" + group
117                 + " --where \"name='" + name + "'\"");
118     }
119 }
120