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