SecureSetting.java revision ccb6b9a90f228cc4e31a9442ed28756ff474c080
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
17package com.android.systemui.qs;
18
19import android.content.Context;
20import android.database.ContentObserver;
21import android.os.Handler;
22import android.provider.Settings.Secure;
23
24import com.android.systemui.statusbar.policy.Listenable;
25
26/** Helper for managing a secure setting. **/
27public abstract class SecureSetting extends ContentObserver implements Listenable {
28    private final Context mContext;
29    private final String mSettingName;
30
31    protected abstract void handleValueChanged(int value);
32
33    public SecureSetting(Context context, Handler handler, String settingName) {
34        super(handler);
35        mContext = context;
36        mSettingName = settingName;
37        rebindForCurrentUser();
38    }
39
40    public void rebindForCurrentUser() {
41        setListening(true);
42    }
43
44    public int getValue() {
45        return Secure.getInt(mContext.getContentResolver(), mSettingName, 0);
46    }
47
48    public void setValue(int value) {
49        Secure.putInt(mContext.getContentResolver(), mSettingName, value);
50    }
51
52    @Override
53    public void setListening(boolean listening) {
54        if (listening) {
55            mContext.getContentResolver().registerContentObserver(
56                    Secure.getUriFor(mSettingName), false, this);
57        } else {
58            mContext.getContentResolver().unregisterContentObserver(this);
59        }
60    }
61
62    @Override
63    public void onChange(boolean selfChange) {
64        handleValueChanged(getValue());
65    }
66}
67