Home | History | Annotate | Download | only in settings
      1 package com.android.settings;
      2 
      3 import android.content.Context;
      4 import android.support.v7.preference.PreferenceViewHolder;
      5 import android.util.AttributeSet;
      6 import android.util.Log;
      7 import android.widget.Checkable;
      8 
      9 /**
     10  * A hybrid of AppListPreference and SwitchPreference, representing a preference which can be on or
     11  * off but must have a selected value when turned on.
     12  *
     13  * It is invalid to show this preference when zero valid apps are present.
     14  */
     15 public class AppListSwitchPreference extends AppListPreference {
     16     private static final String TAG = "AppListSwitchPref";
     17 
     18     private Checkable mSwitch;
     19 
     20     public AppListSwitchPreference(Context context, AttributeSet attrs) {
     21         super(context, attrs, 0, R.style.AppListSwitchPreference);
     22     }
     23 
     24     @Override
     25     public void onBindViewHolder(PreferenceViewHolder view) {
     26         super.onBindViewHolder(view);
     27         mSwitch = (Checkable) view.findViewById(com.android.internal.R.id.switch_widget);
     28         mSwitch.setChecked(getValue() != null);
     29     }
     30 
     31     @Override
     32     protected void onClick() {
     33         if (getValue() != null) {
     34             // Turning off the current value.
     35             if (callChangeListener(null)) {
     36                 setValue(null);
     37             }
     38         } else if (getEntryValues() == null || getEntryValues().length == 0) {
     39             Log.e(TAG, "Attempting to show dialog with zero entries: " + getKey());
     40         } else if (getEntryValues().length == 1) {
     41             // Suppress the dialog and just toggle the preference with the only choice.
     42             String value = getEntryValues()[0].toString();
     43             if (callChangeListener(value)) {
     44                 setValue(value);
     45             }
     46         } else {
     47             super.onClick();
     48         }
     49     }
     50 
     51     @Override
     52     public void setValue(String value) {
     53         super.setValue(value);
     54         if (mSwitch != null) {
     55             mSwitch.setChecked(value != null);
     56         }
     57     }
     58 }
     59