FileDocCategorySizeDatePackage
BluetoothEnabler.javaAPI DocAndroid 1.5 API5420Wed May 06 22:42:48 BST 2009com.android.settings.bluetooth

BluetoothEnabler

public class BluetoothEnabler extends Object implements Preference.OnPreferenceChangeListener
BluetoothEnabler is a helper to manage the Bluetooth on/off checkbox preference. It is turns on/off Bluetooth and ensures the summary of the preference reflects the current state.

Fields Summary
private static final boolean
LOCAL_LOGD
private static final String
TAG
private final android.content.Context
mContext
private final android.preference.CheckBoxPreference
mCheckBoxPreference
private final CharSequence
mOriginalSummary
private final LocalBluetoothManager
mLocalManager
private final android.content.BroadcastReceiver
mReceiver
Constructors Summary
public BluetoothEnabler(android.content.Context context, android.preference.CheckBoxPreference checkBoxPreference)


         
        mContext = context;
        mCheckBoxPreference = checkBoxPreference;
        
        mOriginalSummary = checkBoxPreference.getSummary();
        checkBoxPreference.setPersistent(false);
        
        mLocalManager = LocalBluetoothManager.getInstance(context);
        if (mLocalManager == null) {
            // Bluetooth not supported
            checkBoxPreference.setEnabled(false);
        }
    
Methods Summary
private android.preference.PreferencegetDependencyPreference()

        String depKey = mCheckBoxPreference.getDependency();
        if (TextUtils.isEmpty(depKey)) {
            return null;
        }
        
        return mCheckBoxPreference.getPreferenceManager().findPreference(depKey);
    
private voidhandleStateChanged(int state)


        if (state == BluetoothDevice.BLUETOOTH_STATE_OFF ||
                state == BluetoothDevice.BLUETOOTH_STATE_ON) {
            mCheckBoxPreference.setChecked(state == BluetoothDevice.BLUETOOTH_STATE_ON);
            mCheckBoxPreference.setSummary(state == BluetoothDevice.BLUETOOTH_STATE_OFF ?
                                           mOriginalSummary :
                                           null);
            
            mCheckBoxPreference.setEnabled(isEnabledByDependency());
            
        } else if (state == BluetoothDevice.BLUETOOTH_STATE_TURNING_ON ||
                state == BluetoothDevice.BLUETOOTH_STATE_TURNING_OFF) {
            mCheckBoxPreference.setSummary(state == BluetoothDevice.BLUETOOTH_STATE_TURNING_ON
                    ? R.string.wifi_starting
                    : R.string.wifi_stopping);
            
        } else {
            mCheckBoxPreference.setChecked(false);
            mCheckBoxPreference.setSummary(R.string.wifi_error);
            mCheckBoxPreference.setEnabled(true);
        }
    
private booleanisEnabledByDependency()

        Preference dep = getDependencyPreference();
        if (dep == null) {
            return true;
        }
        
        return !dep.shouldDisableDependents();
    
public booleanonPreferenceChange(android.preference.Preference preference, java.lang.Object value)

        // Turn on/off BT
        setEnabled((Boolean) value);
        
        // Don't update UI to opposite state until we're sure
        return false;
    
public voidpause()

        if (mLocalManager == null) {
            return;
        }
        
        mContext.unregisterReceiver(mReceiver);
        mCheckBoxPreference.setOnPreferenceChangeListener(null);
    
public voidresume()

        if (mLocalManager == null) {
            return;
        }
        
        int state = mLocalManager.getBluetoothState();
        // This is the widget enabled state, not the preference toggled state
        mCheckBoxPreference.setEnabled(state == BluetoothDevice.BLUETOOTH_STATE_ON ||
                state == BluetoothDevice.BLUETOOTH_STATE_OFF);
        // BT state is not a sticky broadcast, so set it manually
        handleStateChanged(state);
        
        mContext.registerReceiver(mReceiver, 
                new IntentFilter(BluetoothIntent.BLUETOOTH_STATE_CHANGED_ACTION));
        mCheckBoxPreference.setOnPreferenceChangeListener(this);
    
private voidsetEnabled(boolean enable)

        // Disable preference
        mCheckBoxPreference.setEnabled(false);
        
        mLocalManager.setBluetoothEnabled(enable);