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