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 handleValueChanged(int value, boolean observedChange)38 protected abstract void handleValueChanged(int value, boolean observedChange); 39 SecureSetting(Context context, Handler handler, String settingName)40 public SecureSetting(Context context, Handler handler, String settingName) { 41 super(handler); 42 mContext = context; 43 mSettingName = settingName; 44 mUserId = ActivityManager.getCurrentUser(); 45 } 46 getValue()47 public int getValue() { 48 return Secure.getIntForUser(mContext.getContentResolver(), mSettingName, DEFAULT, mUserId); 49 } 50 setValue(int value)51 public void setValue(int value) { 52 Secure.putIntForUser(mContext.getContentResolver(), mSettingName, value, mUserId); 53 } 54 55 @Override setListening(boolean listening)56 public void setListening(boolean listening) { 57 if (listening == mListening) return; 58 mListening = listening; 59 if (listening) { 60 mObservedValue = getValue(); 61 mContext.getContentResolver().registerContentObserver( 62 Secure.getUriFor(mSettingName), false, this, mUserId); 63 } else { 64 mContext.getContentResolver().unregisterContentObserver(this); 65 mObservedValue = DEFAULT; 66 } 67 } 68 69 @Override onChange(boolean selfChange)70 public void onChange(boolean selfChange) { 71 final int value = getValue(); 72 handleValueChanged(value, value != mObservedValue); 73 mObservedValue = value; 74 } 75 setUserId(int userId)76 public void setUserId(int userId) { 77 mUserId = userId; 78 if (mListening) { 79 setListening(false); 80 setListening(true); 81 } 82 } 83 } 84