blob: 341c49a8715680c0ac6d43d4afc06f3f1272fca5 [file] [log] [blame]
Ned Burnsf098dbf2019-09-13 19:17:53 -04001/*
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
17package com.android.systemui.statusbar;
18
19import android.annotation.NonNull;
20import android.os.Handler;
21import android.os.HandlerExecutor;
Ned Burnsf098dbf2019-09-13 19:17:53 -040022import android.provider.DeviceConfig;
23import android.util.ArrayMap;
24
Dave Mankofff4736812019-10-18 17:25:50 -040025import com.android.systemui.dagger.qualifiers.BgHandler;
26
Ned Burnsf098dbf2019-09-13 19:17:53 -040027import java.util.Map;
28
29import javax.inject.Inject;
Ned Burns9df879d2019-10-17 11:42:57 -040030import javax.inject.Singleton;
Ned Burnsf098dbf2019-09-13 19:17:53 -040031
32/**
33 * Class to manage simple DeviceConfig-based feature flags.
34 *
35 * To enable or disable a flag, run:
36 *
37 * {@code
38 * $ adb shell device_config put systemui <key> <true|false>
39* }
40 *
Ned Burns9df879d2019-10-17 11:42:57 -040041 * You will probably need to restart systemui for the changes to be picked up:
42 *
43 * {@code
44 * $ adb shell am crash com.android.systemui
45 * }
Ned Burnsf098dbf2019-09-13 19:17:53 -040046 */
Ned Burns9df879d2019-10-17 11:42:57 -040047@Singleton
Ned Burnsf098dbf2019-09-13 19:17:53 -040048public class FeatureFlags {
49 private final Map<String, Boolean> mCachedDeviceConfigFlags = new ArrayMap<>();
50
51 @Inject
Dave Mankofff4736812019-10-18 17:25:50 -040052 public FeatureFlags(@BgHandler Handler bgHandler) {
Ned Burnsf098dbf2019-09-13 19:17:53 -040053 DeviceConfig.addOnPropertiesChangedListener(
54 "systemui",
Ned Burns9df879d2019-10-17 11:42:57 -040055 new HandlerExecutor(bgHandler),
Ned Burnsf098dbf2019-09-13 19:17:53 -040056 this::onPropertiesChanged);
57 }
58
59 public boolean isNewNotifPipelineEnabled() {
60 return getDeviceConfigFlag("notification.newpipeline.enabled", false);
61 }
62
63 private void onPropertiesChanged(@NonNull DeviceConfig.Properties properties) {
64 synchronized (mCachedDeviceConfigFlags) {
65 for (String key : properties.getKeyset()) {
66 mCachedDeviceConfigFlags.remove(key);
67 }
68 }
69 }
70
71 private boolean getDeviceConfigFlag(String key, boolean defaultValue) {
72 synchronized (mCachedDeviceConfigFlags) {
73 Boolean flag = mCachedDeviceConfigFlags.get(key);
74 if (flag == null) {
75 flag = DeviceConfig.getBoolean("systemui", key, defaultValue);
76 mCachedDeviceConfigFlags.put(key, flag);
77 }
78 return flag;
79 }
80 }
81}