1 /* 2 * Copyright (C) 2023 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.statusbar.policy.Listenable; 23 import com.android.systemui.util.settings.SecureSettings; 24 import com.android.systemui.util.settings.SystemSettings; 25 import com.android.systemui.util.settings.UserSettingsProxy; 26 27 /** 28 * Helper for managing secure and system settings through use of {@link UserSettingsProxy}, 29 * which is the common superclass of {@link SecureSettings} and {@link SystemSettings}. 30 */ 31 public abstract class UserSettingObserver extends ContentObserver implements Listenable { 32 private final UserSettingsProxy mSettingsProxy; 33 private final String mSettingName; 34 private final int mDefaultValue; 35 36 private boolean mListening; 37 private int mUserId; 38 private int mObservedValue; 39 handleValueChanged(int value, boolean observedChange)40 protected abstract void handleValueChanged(int value, boolean observedChange); 41 UserSettingObserver(UserSettingsProxy settingsProxy, Handler handler, String settingName, int userId)42 public UserSettingObserver(UserSettingsProxy settingsProxy, Handler handler, String settingName, 43 int userId) { 44 this(settingsProxy, handler, settingName, userId, 0); 45 } 46 UserSettingObserver(UserSettingsProxy settingsProxy, Handler handler, String settingName, int userId, int defaultValue)47 public UserSettingObserver(UserSettingsProxy settingsProxy, Handler handler, String settingName, 48 int userId, int defaultValue) { 49 super(handler); 50 mSettingsProxy = settingsProxy; 51 mSettingName = settingName; 52 mObservedValue = mDefaultValue = defaultValue; 53 mUserId = userId; 54 } 55 getValue()56 public int getValue() { 57 return mListening ? mObservedValue : getValueFromProvider(); 58 } 59 60 /** 61 * Set the value of the observed setting. 62 * 63 * @param value The new value for the setting. 64 */ setValue(int value)65 public void setValue(int value) { 66 mSettingsProxy.putIntForUser(mSettingName, value, mUserId); 67 } 68 getValueFromProvider()69 private int getValueFromProvider() { 70 return mSettingsProxy.getIntForUser(mSettingName, mDefaultValue, mUserId); 71 } 72 73 @Override setListening(boolean listening)74 public void setListening(boolean listening) { 75 if (listening == mListening) return; 76 mListening = listening; 77 if (listening) { 78 mObservedValue = getValueFromProvider(); 79 mSettingsProxy.registerContentObserverForUserSync( 80 mSettingsProxy.getUriFor(mSettingName), false, this, mUserId); 81 } else { 82 mSettingsProxy.unregisterContentObserverSync(this); 83 mObservedValue = mDefaultValue; 84 } 85 } 86 87 @Override onChange(boolean selfChange)88 public void onChange(boolean selfChange) { 89 final int value = getValueFromProvider(); 90 final boolean changed = value != mObservedValue; 91 mObservedValue = value; 92 handleValueChanged(value, changed); 93 } 94 95 /** 96 * Set user handle for which to observe the setting. 97 */ setUserId(int userId)98 public void setUserId(int userId) { 99 mUserId = userId; 100 if (mListening) { 101 setListening(false); 102 setListening(true); 103 } 104 } 105 getCurrentUser()106 public int getCurrentUser() { 107 return mUserId; 108 } 109 getKey()110 public String getKey() { 111 return mSettingName; 112 } 113 isListening()114 public boolean isListening() { 115 return mListening; 116 } 117 } 118