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.Global;
23
24import com.android.systemui.statusbar.policy.Listenable;
25
26/** Helper for managing a global setting. **/
27public abstract class GlobalSetting 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 GlobalSetting(Context context, Handler handler, String settingName) {
34        super(handler);
35        mContext = context;
36        mSettingName = settingName;
37    }
38
39    public int getValue() {
40        return Global.getInt(mContext.getContentResolver(), mSettingName, 0);
41    }
42
43    public void setValue(int value) {
44        Global.putInt(mContext.getContentResolver(), mSettingName, value);
45    }
46
47    @Override
48    public void setListening(boolean listening) {
49        if (listening) {
50            mContext.getContentResolver().registerContentObserver(
51                    Global.getUriFor(mSettingName), false, this);
52        } else {
53            mContext.getContentResolver().unregisterContentObserver(this);
54        }
55    }
56
57    @Override
58    public void onChange(boolean selfChange) {
59        handleValueChanged(getValue());
60    }
61}
62