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 
17 package android.provider;
18 
19 import static android.provider.Settings.ResetMode;
20 
21 import android.annotation.NonNull;
22 import android.annotation.Nullable;
23 import android.provider.DeviceConfig.BadConfigException;
24 import android.provider.DeviceConfig.Properties;
25 
26 import java.util.concurrent.Executor;
27 
28 /**
29  * Abstraction around {@link DeviceConfig} to allow faking device configuration in tests.
30  *
31  * @hide
32  */
33 public interface DeviceConfigInterface {
34 
35     /**
36      * @hide
37      * @see DeviceConfig#getProperty
38      */
39     @Nullable
getProperty(@onNull String namespace, @NonNull String name)40     String getProperty(@NonNull String namespace, @NonNull String name);
41 
42     /**
43      * @hide
44      * @see DeviceConfig#getProperties
45      */
46     @NonNull
getProperties(@onNull String namespace, @NonNull String... names)47     Properties getProperties(@NonNull String namespace, @NonNull String... names);
48 
49     /**
50      * @hide
51      * @see DeviceConfig#setProperty
52      */
setProperty(@onNull String namespace, @NonNull String name, @Nullable String value, boolean makeDefault)53     boolean setProperty(@NonNull String namespace, @NonNull String name, @Nullable String value,
54             boolean makeDefault);
55 
56     /**
57      * @hide
58      * @see DeviceConfig#setProperties
59      */
setProperties(@onNull Properties properties)60     boolean setProperties(@NonNull Properties properties) throws BadConfigException;
61 
62     /**
63      * @hide
64      * @see DeviceConfig#deleteProperty
65      */
deleteProperty(@onNull String namespace, @NonNull String name)66     boolean deleteProperty(@NonNull String namespace, @NonNull String name);
67 
68     /**
69      * @hide
70      * @see DeviceConfig#resetToDefaults
71      */
resetToDefaults(@esetMode int resetMode, @Nullable String namespace)72     void resetToDefaults(@ResetMode int resetMode, @Nullable String namespace);
73 
74     /**
75      * @hide
76      * @see DeviceConfig#getString
77      */
78     @NonNull
getString(@onNull String namespace, @NonNull String name, @NonNull String defaultValue)79     String getString(@NonNull String namespace, @NonNull String name, @NonNull String defaultValue);
80 
81     /**
82      * @hide
83      * @see DeviceConfig#getInt
84      */
getInt(@onNull String namespace, @NonNull String name, int defaultValue)85     int getInt(@NonNull String namespace, @NonNull String name, int defaultValue);
86 
87     /**
88      * @hide
89      * @see DeviceConfig#getLong
90      */
getLong(@onNull String namespace, @NonNull String name, long defaultValue)91     long getLong(@NonNull String namespace, @NonNull String name, long defaultValue);
92 
93     /**
94      * @hide
95      * @see DeviceConfig#getBoolean
96      */
getBoolean(@onNull String namespace, @NonNull String name, boolean defaultValue)97     boolean getBoolean(@NonNull String namespace, @NonNull String name, boolean defaultValue);
98 
99     /**
100      * @hide
101      * @see DeviceConfig#getFloat
102      */
getFloat(@onNull String namespace, @NonNull String name, float defaultValue)103     float getFloat(@NonNull String namespace, @NonNull String name, float defaultValue);
104 
105     /**
106      * @hide
107      * @see DeviceConfig#addOnPropertiesChangedListener
108      */
addOnPropertiesChangedListener(@onNull String namespace, @NonNull Executor executor, @NonNull DeviceConfig.OnPropertiesChangedListener listener)109     void addOnPropertiesChangedListener(@NonNull String namespace, @NonNull Executor executor,
110             @NonNull DeviceConfig.OnPropertiesChangedListener listener);
111 
112     /**
113      * @hide
114      * @see DeviceConfig#removeOnPropertiesChangedListener
115      */
removeOnPropertiesChangedListener( @onNull DeviceConfig.OnPropertiesChangedListener listener)116     void removeOnPropertiesChangedListener(
117             @NonNull DeviceConfig.OnPropertiesChangedListener listener);
118 
119     /**
120      * Calls through to the real {@link DeviceConfig}.
121      *
122      * @hide
123      */
124     @NonNull
125     DeviceConfigInterface REAL = new DeviceConfigInterface() {
126         @Override
127         public String getProperty(String namespace, String name) {
128             return DeviceConfig.getProperty(namespace, name);
129         }
130 
131         @Override
132         public DeviceConfig.Properties getProperties(@NonNull String namespace,
133                 @NonNull String... names) {
134             return DeviceConfig.getProperties(namespace, names);
135         }
136 
137         @Override
138         public boolean setProperty(@NonNull String namespace,
139                 @NonNull String name,
140                 @Nullable String value, boolean makeDefault) {
141             return DeviceConfig.setProperty(namespace, name, value, makeDefault);
142         }
143 
144         @Override
145         public boolean setProperties(@NonNull Properties properties)
146                 throws BadConfigException {
147             return DeviceConfig.setProperties(properties);
148         }
149 
150         @Override
151         public boolean deleteProperty(@NonNull String namespace,
152                 @NonNull String name) {
153             return DeviceConfig.deleteProperty(namespace, name);
154         }
155 
156         @Override
157         public void resetToDefaults(int resetMode, @Nullable String namespace) {
158             DeviceConfig.resetToDefaults(resetMode, namespace);
159         }
160 
161         @Override
162         public String getString(String namespace, String name, String defaultValue) {
163             return DeviceConfig.getString(namespace, name, defaultValue);
164         }
165 
166         @Override
167         public int getInt(String namespace, String name, int defaultValue) {
168             return DeviceConfig.getInt(namespace, name, defaultValue);
169         }
170 
171         @Override
172         public long getLong(String namespace, String name, long defaultValue) {
173             return DeviceConfig.getLong(namespace, name, defaultValue);
174         }
175 
176         @Override
177         public boolean getBoolean(@NonNull String namespace, @NonNull String name,
178                 boolean defaultValue) {
179             return DeviceConfig.getBoolean(namespace, name, defaultValue);
180         }
181 
182         @Override
183         public float getFloat(@NonNull String namespace, @NonNull String name,
184                 float defaultValue) {
185             return DeviceConfig.getFloat(namespace, name, defaultValue);
186         }
187 
188         @Override
189         public void addOnPropertiesChangedListener(String namespace, Executor executor,
190                 DeviceConfig.OnPropertiesChangedListener listener) {
191             DeviceConfig.addOnPropertiesChangedListener(namespace, executor, listener);
192         }
193 
194         @Override
195         public void removeOnPropertiesChangedListener(
196                 DeviceConfig.OnPropertiesChangedListener listener) {
197             DeviceConfig.removeOnPropertiesChangedListener(listener);
198         }
199     };
200 }
201