2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.notification;
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.net.Uri;
23 import android.preference.Preference;
24 import android.preference.TwoStatePreference;
25 import android.preference.Preference.OnPreferenceChangeListener;
26 import android.provider.Settings.Global;
27 import android.provider.Settings.System;
29 import com.android.settings.SettingsPreferenceFragment;
31 /** Helper to manage a two-state or dropdown preference bound to a global or system setting. */
32 public class SettingPref {
33 public static final int TYPE_GLOBAL = 1;
34 public static final int TYPE_SYSTEM = 2;
36 protected final int mType;
37 private final String mKey;
38 protected final String mSetting;
39 protected final int mDefault;
40 private final int[] mValues;
41 private final Uri mUri;
43 protected TwoStatePreference mTwoState;
44 protected DropDownPreference mDropDown;
46 public SettingPref(int type, String key, String setting, int def, int... values) {
52 mUri = getUriFor(mType, mSetting);
55 public boolean isApplicable(Context context) {
59 protected String getCaption(Resources res, int value) {
60 throw new UnsupportedOperationException();
63 public Preference init(SettingsPreferenceFragment settings) {
64 final Context context = settings.getActivity();
65 Preference p = settings.getPreferenceScreen().findPreference(mKey);
66 if (p != null && !isApplicable(context)) {
67 settings.getPreferenceScreen().removePreference(p);
70 if (p instanceof TwoStatePreference) {
71 mTwoState = (TwoStatePreference) p;
72 } else if (p instanceof DropDownPreference) {
73 mDropDown = (DropDownPreference) p;
74 for (int value : mValues) {
75 mDropDown.addItem(getCaption(context.getResources(), value), value);
79 if (mTwoState != null) {
80 p.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
82 public boolean onPreferenceChange(Preference preference, Object newValue) {
83 setSetting(context, (Boolean) newValue ? 1 : 0);
89 if (mDropDown != null) {
90 mDropDown.setCallback(new DropDownPreference.Callback() {
92 public boolean onItemSelected(int pos, Object value) {
93 return setSetting(context, (Integer) value);
101 protected boolean setSetting(Context context, int value) {
102 return putInt(mType, context.getContentResolver(), mSetting, value);
105 public Uri getUri() {
109 public String getKey() {
113 public void update(Context context) {
114 final int val = getInt(mType, context.getContentResolver(), mSetting, mDefault);
115 if (mTwoState != null) {
116 mTwoState.setChecked(val != 0);
117 } else if (mDropDown != null) {
118 mDropDown.setSelectedValue(val);
122 private static Uri getUriFor(int type, String setting) {
125 return Global.getUriFor(setting);
127 return System.getUriFor(setting);
129 throw new IllegalArgumentException();
132 protected static boolean putInt(int type, ContentResolver cr, String setting, int value) {
135 return Global.putInt(cr, setting, value);
137 return System.putInt(cr, setting, value);
139 throw new IllegalArgumentException();
142 protected static int getInt(int type, ContentResolver cr, String setting, int def) {
145 return Global.getInt(cr, setting, def);
147 return System.getInt(cr, setting, def);
149 throw new IllegalArgumentException();