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.app.ActivityManager;
20 import android.content.Context;
21 import android.database.ContentObserver;
22 import android.os.Handler;
23 import android.provider.Settings.Secure;
24 
25 import com.android.systemui.statusbar.policy.Listenable;
26 
27 /** Helper for managing a secure setting. **/
28 public abstract class SecureSetting extends ContentObserver implements Listenable {
29     private static final int DEFAULT = 0;
30 
31     private final Context mContext;
32     private final String mSettingName;
33 
34     private boolean mListening;
35     private int mUserId;
36     private int mObservedValue = DEFAULT;
37 
38     protected abstract void handleValueChanged(int value, boolean observedChange);
39 
40     protected SecureSetting(Context context, Handler handler, String settingName) {
41         this(context, handler, settingName, ActivityManager.getCurrentUser());
42     }
43 
44     public SecureSetting(Context context, Handler handler, String settingName, int userId) {
45         super(handler);
46         mContext = context;
47         mSettingName = settingName;
48         mUserId = userId;
49     }
50 
51     public int getValue() {
52         return Secure.getIntForUser(mContext.getContentResolver(), mSettingName, DEFAULT, mUserId);
53     }
54 
55     public void setValue(int value) {
56         Secure.putIntForUser(mContext.getContentResolver(), mSettingName, value, mUserId);
57     }
58 
59     @Override
60     public void setListening(boolean listening) {
61         if (listening == mListening) return;
62         mListening = listening;
63         if (listening) {
64             mObservedValue = getValue();
65             mContext.getContentResolver().registerContentObserver(
66                     Secure.getUriFor(mSettingName), false, this, mUserId);
67         } else {
68             mContext.getContentResolver().unregisterContentObserver(this);
69             mObservedValue = DEFAULT;
70         }
71     }
72 
73     @Override
74     public void onChange(boolean selfChange) {
75         final int value = getValue();
76         handleValueChanged(value, value != mObservedValue);
77         mObservedValue = value;
78     }
79 
80     public void setUserId(int userId) {
81         mUserId = userId;
82         if (mListening) {
83             setListening(false);
84             setListening(true);
85         }
86     }
87 
88     public int getCurrentUser() {
89         return mUserId;
90     }
91 
92     public String getKey() {
93         return mSettingName;
94     }
95 
96     public boolean isListening() {
97         return mListening;
98     }
99 }
100