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.wm.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#addOnPropertiesChangedListener
58      */
addOnPropertiesChangedListener(@onNull String namespace, @NonNull Executor executor, @NonNull DeviceConfig.OnPropertiesChangedListener listener)59     void addOnPropertiesChangedListener(@NonNull String namespace, @NonNull Executor executor,
60             @NonNull DeviceConfig.OnPropertiesChangedListener listener);
61 
62     /**
63      * @see DeviceConfig#removeOnPropertiesChangedListener
64      */
removeOnPropertiesChangedListener( @onNull DeviceConfig.OnPropertiesChangedListener listener)65     void removeOnPropertiesChangedListener(
66             @NonNull DeviceConfig.OnPropertiesChangedListener listener);
67 
68     /**
69      * Calls through to the real {@link DeviceConfig}.
70      */
71     DeviceConfigInterface REAL = new DeviceConfigInterface() {
72         @Override
73         public String getProperty(String namespace, String name) {
74             return DeviceConfig.getProperty(namespace, name);
75         }
76 
77         @Override
78         public String getString(String namespace, String name, String defaultValue) {
79             return DeviceConfig.getString(namespace, name, defaultValue);
80         }
81 
82         @Override
83         public int getInt(String namespace, String name, int defaultValue) {
84             return DeviceConfig.getInt(namespace, name, defaultValue);
85         }
86 
87         @Override
88         public long getLong(String namespace, String name, long defaultValue) {
89             return DeviceConfig.getLong(namespace, name, defaultValue);
90         }
91 
92         @Override
93         public boolean getBoolean(@NonNull String namespace, @NonNull String name,
94                 boolean defaultValue) {
95             return DeviceConfig.getBoolean(namespace, name, defaultValue);
96         }
97 
98         @Override
99         public void addOnPropertiesChangedListener(String namespace, Executor executor,
100                 DeviceConfig.OnPropertiesChangedListener listener) {
101             DeviceConfig.addOnPropertiesChangedListener(namespace, executor, listener);
102         }
103 
104         @Override
105         public void removeOnPropertiesChangedListener(
106                 DeviceConfig.OnPropertiesChangedListener listener) {
107             DeviceConfig.removeOnPropertiesChangedListener(listener);
108         }
109     };
110 }
111