1 /*
2  * Copyright (C) 2019 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.android.server.utils;
18 
19 import android.annotation.NonNull;
20 import android.annotation.Nullable;
21 import android.provider.DeviceConfig;
22 
23 import java.util.concurrent.Executor;
24 
25 /**
26  * Abstraction around {@link DeviceConfig} to allow faking device configuration in tests.
27  */
28 public interface DeviceConfigInterface {
29     /**
30      * @see DeviceConfig#getProperty
31      */
32     @Nullable
getProperty(@onNull String namespace, @NonNull String name)33     String getProperty(@NonNull String namespace, @NonNull String name);
34 
35     /**
36      * @see DeviceConfig#getString
37      */
38     @NonNull
getString(@onNull String namespace, @NonNull String name, @NonNull String defaultValue)39     String getString(@NonNull String namespace, @NonNull String name, @NonNull String defaultValue);
40 
41     /**
42      * @see DeviceConfig#getInt
43      */
getInt(@onNull String namespace, @NonNull String name, int defaultValue)44     int getInt(@NonNull String namespace, @NonNull String name, int defaultValue);
45 
46     /**
47      * @see DeviceConfig#getLong
48      */
getLong(@onNull String namespace, @NonNull String name, long defaultValue)49     long getLong(@NonNull String namespace, @NonNull String name, long defaultValue);
50 
51     /**
52      * @see DeviceConfig#getBoolean
53      */
getBoolean(@onNull String namespace, @NonNull String name, boolean defaultValue)54     boolean getBoolean(@NonNull String namespace, @NonNull String name, boolean defaultValue);
55 
56     /**
57      * @see DeviceConfig#getFloat
58      */
getFloat(@onNull String namespace, @NonNull String name, float defaultValue)59     float getFloat(@NonNull String namespace, @NonNull String name, float defaultValue);
60 
61     /**
62      * @see DeviceConfig#addOnPropertiesChangedListener
63      */
addOnPropertiesChangedListener(@onNull String namespace, @NonNull Executor executor, @NonNull DeviceConfig.OnPropertiesChangedListener listener)64     void addOnPropertiesChangedListener(@NonNull String namespace, @NonNull Executor executor,
65             @NonNull DeviceConfig.OnPropertiesChangedListener listener);
66 
67     /**
68      * @see DeviceConfig#removeOnPropertiesChangedListener
69      */
removeOnPropertiesChangedListener( @onNull DeviceConfig.OnPropertiesChangedListener listener)70     void removeOnPropertiesChangedListener(
71             @NonNull DeviceConfig.OnPropertiesChangedListener listener);
72 
73     /**
74      * Calls through to the real {@link DeviceConfig}.
75      */
76     DeviceConfigInterface REAL = new DeviceConfigInterface() {
77         @Override
78         public String getProperty(String namespace, String name) {
79             return DeviceConfig.getProperty(namespace, name);
80         }
81 
82         @Override
83         public String getString(String namespace, String name, String defaultValue) {
84             return DeviceConfig.getString(namespace, name, defaultValue);
85         }
86 
87         @Override
88         public int getInt(String namespace, String name, int defaultValue) {
89             return DeviceConfig.getInt(namespace, name, defaultValue);
90         }
91 
92         @Override
93         public long getLong(String namespace, String name, long defaultValue) {
94             return DeviceConfig.getLong(namespace, name, defaultValue);
95         }
96 
97         @Override
98         public boolean getBoolean(@NonNull String namespace, @NonNull String name,
99                 boolean defaultValue) {
100             return DeviceConfig.getBoolean(namespace, name, defaultValue);
101         }
102 
103         @Override
104         public float getFloat(@NonNull String namespace, @NonNull String name,
105                 float defaultValue) {
106             return DeviceConfig.getFloat(namespace, name, defaultValue);
107         }
108 
109         @Override
110         public void addOnPropertiesChangedListener(String namespace, Executor executor,
111                 DeviceConfig.OnPropertiesChangedListener listener) {
112             DeviceConfig.addOnPropertiesChangedListener(namespace, executor, listener);
113         }
114 
115         @Override
116         public void removeOnPropertiesChangedListener(
117                 DeviceConfig.OnPropertiesChangedListener listener) {
118             DeviceConfig.removeOnPropertiesChangedListener(listener);
119         }
120     };
121 }
122