John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 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 | |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 19 | import android.app.ActivityManager; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 20 | import android.content.Context; |
| 21 | import android.database.ContentObserver; |
| 22 | import android.os.Handler; |
| 23 | import android.provider.Settings.Secure; |
| 24 | |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 25 | import com.android.systemui.statusbar.policy.Listenable; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 26 | |
| 27 | /** Helper for managing a secure setting. **/ |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 28 | public abstract class SecureSetting extends ContentObserver implements Listenable { |
John Spurlock | 18764bf | 2014-11-19 20:33:40 -0500 | [diff] [blame] | 29 | private static final int DEFAULT = 0; |
| 30 | |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 31 | private final Context mContext; |
| 32 | private final String mSettingName; |
| 33 | |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 34 | private boolean mListening; |
| 35 | private int mUserId; |
John Spurlock | 18764bf | 2014-11-19 20:33:40 -0500 | [diff] [blame] | 36 | private int mObservedValue = DEFAULT; |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 37 | |
John Spurlock | 18764bf | 2014-11-19 20:33:40 -0500 | [diff] [blame] | 38 | protected abstract void handleValueChanged(int value, boolean observedChange); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 39 | |
| 40 | public SecureSetting(Context context, Handler handler, String settingName) { |
| 41 | super(handler); |
| 42 | mContext = context; |
| 43 | mSettingName = settingName; |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 44 | mUserId = ActivityManager.getCurrentUser(); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | public int getValue() { |
John Spurlock | 18764bf | 2014-11-19 20:33:40 -0500 | [diff] [blame] | 48 | return Secure.getIntForUser(mContext.getContentResolver(), mSettingName, DEFAULT, mUserId); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public void setValue(int value) { |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 52 | Secure.putIntForUser(mContext.getContentResolver(), mSettingName, value, mUserId); |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 56 | public void setListening(boolean listening) { |
John Spurlock | 18764bf | 2014-11-19 20:33:40 -0500 | [diff] [blame] | 57 | if (listening == mListening) return; |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 58 | mListening = listening; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 59 | if (listening) { |
John Spurlock | 18764bf | 2014-11-19 20:33:40 -0500 | [diff] [blame] | 60 | mObservedValue = getValue(); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 61 | mContext.getContentResolver().registerContentObserver( |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 62 | Secure.getUriFor(mSettingName), false, this, mUserId); |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 63 | } else { |
| 64 | mContext.getContentResolver().unregisterContentObserver(this); |
John Spurlock | 18764bf | 2014-11-19 20:33:40 -0500 | [diff] [blame] | 65 | mObservedValue = DEFAULT; |
John Spurlock | ccb6b9a | 2014-05-17 15:54:40 -0400 | [diff] [blame] | 66 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public void onChange(boolean selfChange) { |
John Spurlock | 18764bf | 2014-11-19 20:33:40 -0500 | [diff] [blame] | 71 | final int value = getValue(); |
| 72 | handleValueChanged(value, value != mObservedValue); |
| 73 | mObservedValue = value; |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 74 | } |
Adrian Roos | 32d88e8 | 2014-09-24 17:08:22 +0200 | [diff] [blame] | 75 | |
| 76 | public void setUserId(int userId) { |
| 77 | mUserId = userId; |
| 78 | if (mListening) { |
| 79 | setListening(false); |
| 80 | setListening(true); |
| 81 | } |
| 82 | } |
John Spurlock | af8d6c4 | 2014-05-07 17:49:08 -0400 | [diff] [blame] | 83 | } |