1 /* 2 * Copyright (C) 2014 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.qs; 18 19 import android.database.ContentObserver; 20 import android.os.Handler; 21 22 import com.android.systemui.Flags; 23 import com.android.systemui.statusbar.policy.Listenable; 24 import com.android.systemui.util.settings.SecureSettings; 25 import com.android.systemui.util.settings.SettingsProxy; 26 import com.android.systemui.util.settings.SystemSettings; 27 28 /** 29 * Helper for managing global settings through use of {@link SettingsProxy}. This should 30 * <em>not</em> be used for {@link SecureSettings} or {@link SystemSettings} since those must be 31 * user-aware (instead, use {@link UserSettingObserver}). 32 */ 33 public abstract class SettingObserver extends ContentObserver implements Listenable { 34 private final SettingsProxy mSettingsProxy; 35 private final String mSettingName; 36 private final int mDefaultValue; 37 38 private boolean mListening; 39 private int mObservedValue; 40 handleValueChanged(int value, boolean observedChange)41 protected abstract void handleValueChanged(int value, boolean observedChange); 42 SettingObserver(SettingsProxy settingsProxy, Handler handler, String settingName)43 public SettingObserver(SettingsProxy settingsProxy, Handler handler, String settingName) { 44 this(settingsProxy, handler, settingName, 0); 45 } 46 SettingObserver(SettingsProxy settingsProxy, Handler handler, String settingName, int defaultValue)47 public SettingObserver(SettingsProxy settingsProxy, Handler handler, String settingName, 48 int defaultValue) { 49 super(handler); 50 mSettingsProxy = settingsProxy; 51 mSettingName = settingName; 52 mObservedValue = mDefaultValue = defaultValue; 53 } 54 getValue()55 public int getValue() { 56 return mListening ? mObservedValue : getValueFromProvider(); 57 } 58 59 /** 60 * Set the value of the observed setting. 61 * 62 * @param value The new value for the setting. 63 */ setValue(int value)64 public void setValue(int value) { 65 mSettingsProxy.putInt(mSettingName, value); 66 } 67 getValueFromProvider()68 private int getValueFromProvider() { 69 return mSettingsProxy.getInt(mSettingName, mDefaultValue); 70 } 71 72 @Override setListening(boolean listening)73 public void setListening(boolean listening) { 74 if (listening == mListening) return; 75 mListening = listening; 76 if (listening) { 77 mObservedValue = getValueFromProvider(); 78 if (Flags.qsRegisterSettingObserverOnBgThread()) { 79 mSettingsProxy.registerContentObserverAsync( 80 mSettingsProxy.getUriFor(mSettingName), false, this, 81 () -> mObservedValue = getValueFromProvider()); 82 } else { 83 mSettingsProxy.registerContentObserverSync( 84 mSettingsProxy.getUriFor(mSettingName), false, this); 85 } 86 } else { 87 if (Flags.qsRegisterSettingObserverOnBgThread()) { 88 mSettingsProxy.unregisterContentObserverAsync(this); 89 } else { 90 mSettingsProxy.unregisterContentObserverSync(this); 91 } 92 mObservedValue = mDefaultValue; 93 } 94 } 95 96 @Override onChange(boolean selfChange)97 public void onChange(boolean selfChange) { 98 final int value = getValueFromProvider(); 99 final boolean changed = value != mObservedValue; 100 mObservedValue = value; 101 handleValueChanged(value, changed); 102 } 103 getKey()104 public String getKey() { 105 return mSettingName; 106 } 107 isListening()108 public boolean isListening() { 109 return mListening; 110 } 111 } 112