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.systemui.util;
18 
19 import android.annotation.CallbackExecutor;
20 import android.annotation.NonNull;
21 import android.annotation.Nullable;
22 import android.content.Context;
23 import android.provider.DeviceConfig;
24 import android.provider.Settings;
25 
26 import com.android.systemui.dagger.SysUISingleton;
27 
28 import java.util.concurrent.Executor;
29 
30 import javax.inject.Inject;
31 
32 /**
33  * Wrapper around DeviceConfig useful for testing.
34  */
35 @SysUISingleton
36 public class DeviceConfigProxy {
37 
38     @Inject
DeviceConfigProxy()39     public DeviceConfigProxy() {
40     }
41 
42     /**
43      * Wrapped version of {@link DeviceConfig#addOnPropertiesChangedListener}.
44      */
addOnPropertiesChangedListener( @onNull String namespace, @NonNull @CallbackExecutor Executor executor, @NonNull DeviceConfig.OnPropertiesChangedListener onPropertiesChangedListener)45     public void addOnPropertiesChangedListener(
46             @NonNull String namespace,
47             @NonNull @CallbackExecutor Executor executor,
48             @NonNull DeviceConfig.OnPropertiesChangedListener onPropertiesChangedListener) {
49         DeviceConfig.addOnPropertiesChangedListener(
50                 namespace, executor, onPropertiesChangedListener);
51     }
52 
53     /**
54      * Wrapped version of {@link DeviceConfig#enforceReadPermission}.
55      */
enforceReadPermission(Context context, String namespace)56     public void enforceReadPermission(Context context, String namespace) {
57         DeviceConfig.enforceReadPermission(context, namespace);
58     }
59 
60     /**
61      * Wrapped version of {@link DeviceConfig#getBoolean}.
62      */
getBoolean( @onNull String namespace, @NonNull String name, boolean defaultValue)63     public boolean getBoolean(
64             @NonNull String namespace, @NonNull String name, boolean defaultValue) {
65         return DeviceConfig.getBoolean(namespace, name, defaultValue);
66     }
67 
68     /**
69      * Wrapped version of {@link DeviceConfig#getFloat}.
70      */
getFloat( @onNull String namespace, @NonNull String name, float defaultValue)71     public float getFloat(
72             @NonNull String namespace, @NonNull String name, float defaultValue) {
73         return DeviceConfig.getFloat(namespace, name, defaultValue);
74     }
75 
76     /**
77      * Wrapped version of {@link DeviceConfig#getInt}.
78      */
getInt(@onNull String namespace, @NonNull String name, int defaultValue)79     public int getInt(@NonNull String namespace, @NonNull String name, int defaultValue) {
80         return DeviceConfig.getInt(namespace, name, defaultValue);
81     }
82 
83     /**
84      * Wrapped version of {@link DeviceConfig#getLong}.
85      */
getLong(@onNull String namespace, @NonNull String name, long defaultValue)86     public long getLong(@NonNull String namespace, @NonNull String name, long defaultValue) {
87         return DeviceConfig.getLong(namespace, name, defaultValue);
88 
89     }
90 
91     /**
92      * Wrapped version of {@link DeviceConfig#getProperty}.
93      */
getProperty(@onNull String namespace, @NonNull String name)94     public String getProperty(@NonNull String namespace, @NonNull String name) {
95         return DeviceConfig.getProperty(namespace, name);
96     }
97 
98     /**
99      * Wrapped version of {@link DeviceConfig#getString}.
100      */
getString( @onNull String namespace, @NonNull String name, @Nullable String defaultValue)101     public String getString(
102             @NonNull String namespace, @NonNull String name, @Nullable String defaultValue) {
103         return DeviceConfig.getString(namespace, name, defaultValue);
104     }
105 
106     /**
107      * Wrapped version of {@link DeviceConfig#removeOnPropertiesChangedListener}.
108      *
109      * Like {@link #addOnPropertiesChangedListener}, this operates on a callback type that
110      * wraps the original callback type provided by {@link DeviceConfig}.
111      */
removeOnPropertiesChangedListener( @onNull DeviceConfig.OnPropertiesChangedListener onPropertiesChangedListener)112     public void removeOnPropertiesChangedListener(
113             @NonNull DeviceConfig.OnPropertiesChangedListener onPropertiesChangedListener) {
114         DeviceConfig.removeOnPropertiesChangedListener(onPropertiesChangedListener);
115     }
116 
117     /**
118      * Wrapped version of {@link DeviceConfig#resetToDefaults}.
119      */
resetToDefaults(@ettings.ResetMode int resetMode, @Nullable String namespace)120     public void resetToDefaults(@Settings.ResetMode int resetMode,
121             @Nullable String namespace) {
122         DeviceConfig.resetToDefaults(resetMode, namespace);
123     }
124 
125     /**
126      * Wrapped version of {@link DeviceConfig#setProperty}.
127      */
setProperty( @onNull String namespace, @NonNull String name, @Nullable String value, boolean makeDefault)128     public boolean setProperty(
129             @NonNull String namespace,
130             @NonNull String name,
131             @Nullable String value,
132             boolean makeDefault) {
133         return DeviceConfig.setProperty(namespace, name, value, makeDefault);
134     }
135 }
136