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.launcher3.uioverrides; 18 19 import android.annotation.TargetApi; 20 import android.content.Context; 21 import android.os.Build; 22 import android.provider.DeviceConfig; 23 24 import com.android.launcher3.config.FeatureFlags.DebugFlag; 25 26 import java.util.ArrayList; 27 28 @TargetApi(Build.VERSION_CODES.P) 29 public class DeviceFlag extends DebugFlag { 30 31 public static final String NAMESPACE_LAUNCHER = "launcher"; 32 33 private final boolean mDefaultValueInCode; 34 ArrayList<Runnable> mListeners; 35 DeviceFlag(String key, boolean defaultValue, String description)36 public DeviceFlag(String key, boolean defaultValue, String description) { 37 super(key, getDeviceValue(key, defaultValue), description); 38 mDefaultValueInCode = defaultValue; 39 } 40 41 @Override appendProps(StringBuilder src)42 protected StringBuilder appendProps(StringBuilder src) { 43 return super.appendProps(src).append(", mDefaultValueInCode=").append(mDefaultValueInCode); 44 } 45 46 @Override initialize(Context context)47 public void initialize(Context context) { 48 super.initialize(context); 49 if (mListeners == null) { 50 mListeners = new ArrayList<>(); 51 registerDeviceConfigChangedListener(context); 52 } 53 } 54 55 @Override addChangeListener(Context context, Runnable r)56 public void addChangeListener(Context context, Runnable r) { 57 if (mListeners == null) { 58 initialize(context); 59 } 60 mListeners.add(r); 61 } 62 registerDeviceConfigChangedListener(Context context)63 private void registerDeviceConfigChangedListener(Context context) { 64 DeviceConfig.addOnPropertiesChangedListener( 65 NAMESPACE_LAUNCHER, 66 context.getMainExecutor(), 67 properties -> { 68 if (!NAMESPACE_LAUNCHER.equals(properties.getNamespace()) 69 || !properties.getKeyset().contains(key)) { 70 return; 71 } 72 defaultValue = getDeviceValue(key, mDefaultValueInCode); 73 initialize(context); 74 for (Runnable r: mListeners) { 75 r.run(); 76 } 77 }); 78 } 79 getDeviceValue(String key, boolean defaultValue)80 protected static boolean getDeviceValue(String key, boolean defaultValue) { 81 return DeviceConfig.getBoolean(NAMESPACE_LAUNCHER, key, defaultValue); 82 } 83 } 84