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.statusbar;
18 
19 import android.annotation.NonNull;
20 import android.provider.DeviceConfig;
21 import android.util.ArrayMap;
22 
23 import com.android.systemui.dagger.qualifiers.Background;
24 
25 import java.util.Map;
26 import java.util.concurrent.Executor;
27 
28 import javax.inject.Inject;
29 import javax.inject.Singleton;
30 
31 /**
32  * Class to manage simple DeviceConfig-based feature flags.
33  *
34  * To enable or disable a flag, run:
35  *
36  * {@code
37  *  $ adb shell device_config put systemui <key> <true|false>
38 *  }
39  *
40  * You will probably need to restart systemui for the changes to be picked up:
41  *
42  * {@code
43  *  $ adb shell am restart com.android.systemui
44  * }
45  */
46 @Singleton
47 public class FeatureFlags {
48     private final Map<String, Boolean> mCachedDeviceConfigFlags = new ArrayMap<>();
49 
50     @Inject
FeatureFlags(@ackground Executor executor)51     public FeatureFlags(@Background Executor executor) {
52         DeviceConfig.addOnPropertiesChangedListener(
53                 "systemui",
54                 executor,
55                 this::onPropertiesChanged);
56     }
57 
isNewNotifPipelineEnabled()58     public boolean isNewNotifPipelineEnabled() {
59         return getDeviceConfigFlag("notification.newpipeline.enabled", true);
60     }
61 
isNewNotifPipelineRenderingEnabled()62     public boolean isNewNotifPipelineRenderingEnabled() {
63         return isNewNotifPipelineEnabled()
64                 && getDeviceConfigFlag("notification.newpipeline.rendering", false);
65     }
66 
onPropertiesChanged(@onNull DeviceConfig.Properties properties)67     private void onPropertiesChanged(@NonNull DeviceConfig.Properties properties) {
68         synchronized (mCachedDeviceConfigFlags) {
69             for (String key : properties.getKeyset()) {
70                 mCachedDeviceConfigFlags.remove(key);
71             }
72         }
73     }
74 
getDeviceConfigFlag(String key, boolean defaultValue)75     private boolean getDeviceConfigFlag(String key, boolean defaultValue) {
76         synchronized (mCachedDeviceConfigFlags) {
77             Boolean flag = mCachedDeviceConfigFlags.get(key);
78             if (flag == null) {
79                 flag = DeviceConfig.getBoolean("systemui", key, defaultValue);
80                 mCachedDeviceConfigFlags.put(key, flag);
81             }
82             return flag;
83         }
84     }
85 }
86