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