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 
17 package com.android.settings.notification;
18 
19 import android.content.ContentResolver;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.net.Uri;
23 import android.provider.Settings.Global;
24 import android.provider.Settings.System;
25 import android.support.v7.preference.DropDownPreference;
26 import android.support.v7.preference.Preference;
27 import android.support.v7.preference.Preference.OnPreferenceChangeListener;
28 import android.support.v7.preference.TwoStatePreference;
29 
30 import com.android.settings.SettingsPreferenceFragment;
31 
32 /** Helper to manage a two-state or dropdown preference bound to a global or system setting. */
33 public 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 
SettingPref(int type, String key, String setting, int def, int... values)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 
isApplicable(Context context)56     public boolean isApplicable(Context context) {
57         return true;
58     }
59 
getCaption(Resources res, int value)60     protected String getCaption(Resources res, int value) {
61         throw new UnsupportedOperationException();
62     }
63 
init(SettingsPreferenceFragment settings)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             CharSequence[] entries = new CharSequence[mValues.length];
76             CharSequence[] values = new CharSequence[mValues.length];
77             for (int i = 0; i < mValues.length; i++) {
78                 entries[i] = getCaption(context.getResources(), mValues[i]);
79                 values[i] = Integer.toString(mValues[i]);
80             }
81             mDropDown.setEntries(entries);
82             mDropDown.setEntryValues(values);
83         }
84         update(context);
85         if (mTwoState != null) {
86             p.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
87                 @Override
88                 public boolean onPreferenceChange(Preference preference, Object newValue) {
89                     setSetting(context, (Boolean) newValue ? 1 : 0);
90                     return true;
91                 }
92             });
93             return mTwoState;
94         }
95         if (mDropDown != null) {
96             p.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
97                 @Override
98                 public boolean onPreferenceChange(Preference preference, Object newValue) {
99                     return setSetting(context, Integer.parseInt((String) newValue));
100                 }
101             });
102             return mDropDown;
103         }
104         return null;
105     }
106 
setSetting(Context context, int value)107     protected boolean setSetting(Context context, int value) {
108         return putInt(mType, context.getContentResolver(), mSetting, value);
109     }
110 
getUri()111     public Uri getUri() {
112         return mUri;
113     }
114 
getKey()115     public String getKey() {
116         return mKey;
117     }
118 
update(Context context)119     public void update(Context context) {
120         final int val = getInt(mType, context.getContentResolver(), mSetting, mDefault);
121         if (mTwoState != null) {
122             mTwoState.setChecked(val != 0);
123         } else if (mDropDown != null) {
124             mDropDown.setValue(Integer.toString(val));
125         }
126     }
127 
getUriFor(int type, String setting)128     private static Uri getUriFor(int type, String setting) {
129         switch(type) {
130             case TYPE_GLOBAL:
131                 return Global.getUriFor(setting);
132             case TYPE_SYSTEM:
133                 return System.getUriFor(setting);
134         }
135         throw new IllegalArgumentException();
136     }
137 
putInt(int type, ContentResolver cr, String setting, int value)138     protected static boolean putInt(int type, ContentResolver cr, String setting, int value) {
139         switch(type) {
140             case TYPE_GLOBAL:
141                 return Global.putInt(cr, setting, value);
142             case TYPE_SYSTEM:
143                 return System.putInt(cr, setting, value);
144         }
145         throw new IllegalArgumentException();
146     }
147 
getInt(int type, ContentResolver cr, String setting, int def)148     protected static int getInt(int type, ContentResolver cr, String setting, int def) {
149         switch(type) {
150             case TYPE_GLOBAL:
151                 return Global.getInt(cr, setting, def);
152             case TYPE_SYSTEM:
153                 return System.getInt(cr, setting, def);
154         }
155         throw new IllegalArgumentException();
156     }
157 }