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.settings.notification;
18
19import android.content.ContentResolver;
20import android.content.Context;
21import android.content.res.Resources;
22import android.net.Uri;
23import android.preference.Preference;
24import android.preference.TwoStatePreference;
25import android.preference.Preference.OnPreferenceChangeListener;
26import android.provider.Settings.Global;
27import android.provider.Settings.System;
28
29import com.android.settings.DropDownPreference;
30import com.android.settings.SettingsPreferenceFragment;
31
32/** Helper to manage a two-state or dropdown preference bound to a global or system setting. */
33public class SettingPref {
34    public static final int TYPE_GLOBAL = 1;
35    public static final int TYPE_SYSTEM = 2;
36
37    protected final int mType;
38    private final String mKey;
39    protected final String mSetting;
40    protected final int mDefault;
41    private final int[] mValues;
42    private final Uri mUri;
43
44    protected TwoStatePreference mTwoState;
45    protected DropDownPreference mDropDown;
46
47    public SettingPref(int type, String key, String setting, int def, int... values) {
48        mType = type;
49        mKey = key;
50        mSetting = setting;
51        mDefault = def;
52        mValues = values;
53        mUri = getUriFor(mType, mSetting);
54    }
55
56    public boolean isApplicable(Context context) {
57        return true;
58    }
59
60    protected String getCaption(Resources res, int value) {
61        throw new UnsupportedOperationException();
62    }
63
64    public Preference init(SettingsPreferenceFragment settings) {
65        final Context context = settings.getActivity();
66        Preference p = settings.getPreferenceScreen().findPreference(mKey);
67        if (p != null && !isApplicable(context)) {
68            settings.getPreferenceScreen().removePreference(p);
69            p = null;
70        }
71        if (p instanceof TwoStatePreference) {
72            mTwoState = (TwoStatePreference) p;
73        } else if (p instanceof DropDownPreference) {
74            mDropDown = (DropDownPreference) p;
75            for (int value : mValues) {
76                mDropDown.addItem(getCaption(context.getResources(), value), value);
77            }
78        }
79        update(context);
80        if (mTwoState != null) {
81            p.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
82                @Override
83                public boolean onPreferenceChange(Preference preference, Object newValue) {
84                    setSetting(context, (Boolean) newValue ? 1 : 0);
85                    return true;
86                }
87            });
88            return mTwoState;
89        }
90        if (mDropDown != null) {
91            mDropDown.setCallback(new DropDownPreference.Callback() {
92                @Override
93                public boolean onItemSelected(int pos, Object value) {
94                    return setSetting(context, (Integer) value);
95                }
96            });
97            return mDropDown;
98        }
99        return null;
100    }
101
102    protected boolean setSetting(Context context, int value) {
103        return putInt(mType, context.getContentResolver(), mSetting, value);
104    }
105
106    public Uri getUri() {
107        return mUri;
108    }
109
110    public String getKey() {
111        return mKey;
112    }
113
114    public void update(Context context) {
115        final int val = getInt(mType, context.getContentResolver(), mSetting, mDefault);
116        if (mTwoState != null) {
117            mTwoState.setChecked(val != 0);
118        } else if (mDropDown != null) {
119            mDropDown.setSelectedValue(val);
120        }
121    }
122
123    private static Uri getUriFor(int type, String setting) {
124        switch(type) {
125            case TYPE_GLOBAL:
126                return Global.getUriFor(setting);
127            case TYPE_SYSTEM:
128                return System.getUriFor(setting);
129        }
130        throw new IllegalArgumentException();
131    }
132
133    protected static boolean putInt(int type, ContentResolver cr, String setting, int value) {
134        switch(type) {
135            case TYPE_GLOBAL:
136                return Global.putInt(cr, setting, value);
137            case TYPE_SYSTEM:
138                return System.putInt(cr, setting, value);
139        }
140        throw new IllegalArgumentException();
141    }
142
143    protected static int getInt(int type, ContentResolver cr, String setting, int def) {
144        switch(type) {
145            case TYPE_GLOBAL:
146                return Global.getInt(cr, setting, def);
147            case TYPE_SYSTEM:
148                return System.getInt(cr, setting, def);
149        }
150        throw new IllegalArgumentException();
151    }
152}