Ned Burns | f098dbf | 2019-09-13 19:17:53 -0400 | [diff] [blame] | 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.os.Handler; |
| 21 | import android.os.HandlerExecutor; |
Ned Burns | f098dbf | 2019-09-13 19:17:53 -0400 | [diff] [blame] | 22 | import android.provider.DeviceConfig; |
| 23 | import android.util.ArrayMap; |
| 24 | |
Dave Mankoff | f473681 | 2019-10-18 17:25:50 -0400 | [diff] [blame^] | 25 | import com.android.systemui.dagger.qualifiers.BgHandler; |
| 26 | |
Ned Burns | f098dbf | 2019-09-13 19:17:53 -0400 | [diff] [blame] | 27 | import java.util.Map; |
| 28 | |
| 29 | import javax.inject.Inject; |
Ned Burns | 9df879d | 2019-10-17 11:42:57 -0400 | [diff] [blame] | 30 | import javax.inject.Singleton; |
Ned Burns | f098dbf | 2019-09-13 19:17:53 -0400 | [diff] [blame] | 31 | |
| 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 Burns | 9df879d | 2019-10-17 11:42:57 -0400 | [diff] [blame] | 41 | * 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 Burns | f098dbf | 2019-09-13 19:17:53 -0400 | [diff] [blame] | 46 | */ |
Ned Burns | 9df879d | 2019-10-17 11:42:57 -0400 | [diff] [blame] | 47 | @Singleton |
Ned Burns | f098dbf | 2019-09-13 19:17:53 -0400 | [diff] [blame] | 48 | public class FeatureFlags { |
| 49 | private final Map<String, Boolean> mCachedDeviceConfigFlags = new ArrayMap<>(); |
| 50 | |
| 51 | @Inject |
Dave Mankoff | f473681 | 2019-10-18 17:25:50 -0400 | [diff] [blame^] | 52 | public FeatureFlags(@BgHandler Handler bgHandler) { |
Ned Burns | f098dbf | 2019-09-13 19:17:53 -0400 | [diff] [blame] | 53 | DeviceConfig.addOnPropertiesChangedListener( |
| 54 | "systemui", |
Ned Burns | 9df879d | 2019-10-17 11:42:57 -0400 | [diff] [blame] | 55 | new HandlerExecutor(bgHandler), |
Ned Burns | f098dbf | 2019-09-13 19:17:53 -0400 | [diff] [blame] | 56 | 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 | } |