blob: 4f812bc1059ca1dcac7e15ff07aad7c5eeb3950c [file] [log] [blame]
John Spurlockaf8d6c42014-05-07 17:49:08 -04001/*
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
17package com.android.systemui.qs;
18
Adrian Roos32d88e82014-09-24 17:08:22 +020019import android.app.ActivityManager;
John Spurlockaf8d6c42014-05-07 17:49:08 -040020import android.content.Context;
21import android.database.ContentObserver;
22import android.os.Handler;
23import android.provider.Settings.Secure;
24
John Spurlockccb6b9a2014-05-17 15:54:40 -040025import com.android.systemui.statusbar.policy.Listenable;
John Spurlockaf8d6c42014-05-07 17:49:08 -040026
27/** Helper for managing a secure setting. **/
John Spurlockccb6b9a2014-05-17 15:54:40 -040028public abstract class SecureSetting extends ContentObserver implements Listenable {
John Spurlock18764bf2014-11-19 20:33:40 -050029 private static final int DEFAULT = 0;
30
John Spurlockaf8d6c42014-05-07 17:49:08 -040031 private final Context mContext;
32 private final String mSettingName;
33
Adrian Roos32d88e82014-09-24 17:08:22 +020034 private boolean mListening;
35 private int mUserId;
John Spurlock18764bf2014-11-19 20:33:40 -050036 private int mObservedValue = DEFAULT;
Adrian Roos32d88e82014-09-24 17:08:22 +020037
John Spurlock18764bf2014-11-19 20:33:40 -050038 protected abstract void handleValueChanged(int value, boolean observedChange);
John Spurlockaf8d6c42014-05-07 17:49:08 -040039
40 public SecureSetting(Context context, Handler handler, String settingName) {
41 super(handler);
42 mContext = context;
43 mSettingName = settingName;
Adrian Roos32d88e82014-09-24 17:08:22 +020044 mUserId = ActivityManager.getCurrentUser();
John Spurlockaf8d6c42014-05-07 17:49:08 -040045 }
46
47 public int getValue() {
John Spurlock18764bf2014-11-19 20:33:40 -050048 return Secure.getIntForUser(mContext.getContentResolver(), mSettingName, DEFAULT, mUserId);
John Spurlockaf8d6c42014-05-07 17:49:08 -040049 }
50
51 public void setValue(int value) {
Adrian Roos32d88e82014-09-24 17:08:22 +020052 Secure.putIntForUser(mContext.getContentResolver(), mSettingName, value, mUserId);
John Spurlockaf8d6c42014-05-07 17:49:08 -040053 }
54
55 @Override
John Spurlockccb6b9a2014-05-17 15:54:40 -040056 public void setListening(boolean listening) {
John Spurlock18764bf2014-11-19 20:33:40 -050057 if (listening == mListening) return;
Adrian Roos32d88e82014-09-24 17:08:22 +020058 mListening = listening;
John Spurlockccb6b9a2014-05-17 15:54:40 -040059 if (listening) {
John Spurlock18764bf2014-11-19 20:33:40 -050060 mObservedValue = getValue();
John Spurlockccb6b9a2014-05-17 15:54:40 -040061 mContext.getContentResolver().registerContentObserver(
Adrian Roos32d88e82014-09-24 17:08:22 +020062 Secure.getUriFor(mSettingName), false, this, mUserId);
John Spurlockccb6b9a2014-05-17 15:54:40 -040063 } else {
64 mContext.getContentResolver().unregisterContentObserver(this);
John Spurlock18764bf2014-11-19 20:33:40 -050065 mObservedValue = DEFAULT;
John Spurlockccb6b9a2014-05-17 15:54:40 -040066 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040067 }
68
69 @Override
70 public void onChange(boolean selfChange) {
John Spurlock18764bf2014-11-19 20:33:40 -050071 final int value = getValue();
72 handleValueChanged(value, value != mObservedValue);
73 mObservedValue = value;
John Spurlockaf8d6c42014-05-07 17:49:08 -040074 }
Adrian Roos32d88e82014-09-24 17:08:22 +020075
76 public void setUserId(int userId) {
77 mUserId = userId;
78 if (mListening) {
79 setListening(false);
80 setListening(true);
81 }
82 }
John Spurlockaf8d6c42014-05-07 17:49:08 -040083}