SecureSetting.java revision af8d6c44f06d2f8baac2c5774a9efdae3fc36797
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.Disposable;
25
26/** Helper for managing a secure setting. **/
27public abstract class SecureSetting extends ContentObserver implements Disposable {
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        mContext.getContentResolver().registerContentObserver(
42                Secure.getUriFor(mSettingName), false, this);
43    }
44
45    public int getValue() {
46        return Secure.getInt(mContext.getContentResolver(), mSettingName, 0);
47    }
48
49    public void setValue(int value) {
50        Secure.putInt(mContext.getContentResolver(), mSettingName, value);
51    }
52
53    @Override
54    public void dispose() {
55        mContext.getContentResolver().unregisterContentObserver(this);
56    }
57
58    @Override
59    public void onChange(boolean selfChange) {
60        handleValueChanged(getValue());
61    }
62}
63