Home | History | Annotate | Download | only in notification
      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.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;
     28 
     29 import com.android.settings.SettingsPreferenceFragment;
     30 
     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;
     35 
     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;
     42 
     43     protected TwoStatePreference mTwoState;
     44     protected DropDownPreference mDropDown;
     45 
     46     public SettingPref(int type, String key, String setting, int def, int... values) {
     47         mType = type;
     48         mKey = key;
     49         mSetting = setting;
     50         mDefault = def;
     51         mValues = values;
     52         mUri = getUriFor(mType, mSetting);
     53     }
     54 
     55     public boolean isApplicable(Context context) {
     56         return true;
     57     }
     58 
     59     protected String getCaption(Resources res, int value) {
     60         throw new UnsupportedOperationException();
     61     }
     62 
     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);
     68             p = null;
     69         }
     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);
     76             }
     77         }
     78         update(context);
     79         if (mTwoState != null) {
     80             p.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
     81                 @Override
     82                 public boolean onPreferenceChange(Preference preference, Object newValue) {
     83                     setSetting(context, (Boolean) newValue ? 1 : 0);
     84                     return true;
     85                 }
     86             });
     87             return mTwoState;
     88         }
     89         if (mDropDown != null) {
     90             mDropDown.setCallback(new DropDownPreference.Callback() {
     91                 @Override
     92                 public boolean onItemSelected(int pos, Object value) {
     93                     return setSetting(context, (Integer) value);
     94                 }
     95             });
     96             return mDropDown;
     97         }
     98         return null;
     99     }
    100 
    101     protected boolean setSetting(Context context, int value) {
    102         return putInt(mType, context.getContentResolver(), mSetting, value);
    103     }
    104 
    105     public Uri getUri() {
    106         return mUri;
    107     }
    108 
    109     public String getKey() {
    110         return mKey;
    111     }
    112 
    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);
    119         }
    120     }
    121 
    122     private static Uri getUriFor(int type, String setting) {
    123         switch(type) {
    124             case TYPE_GLOBAL:
    125                 return Global.getUriFor(setting);
    126             case TYPE_SYSTEM:
    127                 return System.getUriFor(setting);
    128         }
    129         throw new IllegalArgumentException();
    130     }
    131 
    132     protected static boolean putInt(int type, ContentResolver cr, String setting, int value) {
    133         switch(type) {
    134             case TYPE_GLOBAL:
    135                 return Global.putInt(cr, setting, value);
    136             case TYPE_SYSTEM:
    137                 return System.putInt(cr, setting, value);
    138         }
    139         throw new IllegalArgumentException();
    140     }
    141 
    142     protected static int getInt(int type, ContentResolver cr, String setting, int def) {
    143         switch(type) {
    144             case TYPE_GLOBAL:
    145                 return Global.getInt(cr, setting, def);
    146             case TYPE_SYSTEM:
    147                 return System.getInt(cr, setting, def);
    148         }
    149         throw new IllegalArgumentException();
    150     }
    151 }