diff options
Diffstat (limited to 'src/com/android/settings/bluetooth/BluetoothEnabler.java')
-rw-r--r-- | src/com/android/settings/bluetooth/BluetoothEnabler.java | 134 |
1 files changed, 53 insertions, 81 deletions
diff --git a/src/com/android/settings/bluetooth/BluetoothEnabler.java b/src/com/android/settings/bluetooth/BluetoothEnabler.java index b872916..426a4d3 100644 --- a/src/com/android/settings/bluetooth/BluetoothEnabler.java +++ b/src/com/android/settings/bluetooth/BluetoothEnabler.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2008 The Android Open Source Project + * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,6 +17,7 @@ package com.android.settings.bluetooth; import com.android.settings.R; +import com.android.settings.WirelessSettings; import android.bluetooth.BluetoothAdapter; import android.content.BroadcastReceiver; @@ -25,8 +26,9 @@ import android.content.Intent; import android.content.IntentFilter; import android.preference.Preference; import android.preference.CheckBoxPreference; +import android.provider.Settings; import android.text.TextUtils; -import android.util.Config; +import android.widget.Toast; /** * BluetoothEnabler is a helper to manage the Bluetooth on/off checkbox @@ -34,16 +36,12 @@ import android.util.Config; * preference reflects the current state. */ public class BluetoothEnabler implements Preference.OnPreferenceChangeListener { - - private static final boolean LOCAL_LOGD = Config.LOGD || false; - private static final String TAG = "BluetoothEnabler"; - private final Context mContext; - private final CheckBoxPreference mCheckBoxPreference; + private final CheckBoxPreference mCheckBox; private final CharSequence mOriginalSummary; private final LocalBluetoothManager mLocalManager; - + private final IntentFilter mIntentFilter; private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { @@ -52,18 +50,18 @@ public class BluetoothEnabler implements Preference.OnPreferenceChangeListener { } }; - public BluetoothEnabler(Context context, CheckBoxPreference checkBoxPreference) { + public BluetoothEnabler(Context context, CheckBoxPreference checkBox) { mContext = context; - mCheckBoxPreference = checkBoxPreference; - - mOriginalSummary = checkBoxPreference.getSummary(); - checkBoxPreference.setPersistent(false); + mCheckBox = checkBox; + mOriginalSummary = checkBox.getSummary(); + checkBox.setPersistent(false); mLocalManager = LocalBluetoothManager.getInstance(context); if (mLocalManager == null) { - // Bluetooth not supported - checkBoxPreference.setEnabled(false); + // Bluetooth is not supported + checkBox.setEnabled(false); } + mIntentFilter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED); } public void resume() { @@ -71,16 +69,11 @@ public class BluetoothEnabler implements Preference.OnPreferenceChangeListener { return; } - int state = mLocalManager.getBluetoothState(); - // This is the widget enabled state, not the preference toggled state - mCheckBoxPreference.setEnabled(state == BluetoothAdapter.STATE_ON || - state == BluetoothAdapter.STATE_OFF); - // BT state is not a sticky broadcast, so set it manually - handleStateChanged(state); + // Bluetooth state is not sticky, so set it manually + handleStateChanged(mLocalManager.getBluetoothState()); - mContext.registerReceiver(mReceiver, - new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED)); - mCheckBoxPreference.setOnPreferenceChangeListener(this); + mContext.registerReceiver(mReceiver, mIntentFilter); + mCheckBox.setOnPreferenceChangeListener(this); } public void pause() { @@ -89,72 +82,51 @@ public class BluetoothEnabler implements Preference.OnPreferenceChangeListener { } mContext.unregisterReceiver(mReceiver); - mCheckBoxPreference.setOnPreferenceChangeListener(null); + mCheckBox.setOnPreferenceChangeListener(null); } public boolean onPreferenceChange(Preference preference, Object value) { - // Turn on/off BT - setEnabled((Boolean) value); + boolean enable = (Boolean) value; + + // Show toast message if Bluetooth is not allowed in airplane mode + if (enable && !WirelessSettings + .isRadioAllowed(mContext, Settings.System.RADIO_BLUETOOTH)) { + Toast.makeText(mContext, R.string.wifi_in_airplane_mode, + Toast.LENGTH_SHORT).show(); + return false; + } + + mLocalManager.setBluetoothEnabled(enable); + mCheckBox.setEnabled(false); // Don't update UI to opposite state until we're sure return false; } - private void setEnabled(final boolean enable) { - // Disable preference - mCheckBoxPreference.setEnabled(false); - - mLocalManager.setBluetoothEnabled(enable); - } - private void handleStateChanged(int state) { - - if (state == BluetoothAdapter.STATE_OFF || - state == BluetoothAdapter.STATE_ON) { - mCheckBoxPreference.setChecked(state == BluetoothAdapter.STATE_ON); - mCheckBoxPreference.setSummary(state == BluetoothAdapter.STATE_OFF ? - mOriginalSummary : - null); - - /* - * Don't ever disable the preference. Only enable here. Disablement - * is taken care of by the dependency code. If this is disabled - * here, it may not be re-enabled from the framework when dependency - * is met. http://b/issue?id=2053751 - */ - if (isEnabledByDependency()) { - mCheckBoxPreference.setEnabled(true); - } - - } else if (state == BluetoothAdapter.STATE_TURNING_ON || - state == BluetoothAdapter.STATE_TURNING_OFF) { - mCheckBoxPreference.setSummary(state == BluetoothAdapter.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 boolean isEnabledByDependency() { - Preference dep = getDependencyPreference(); - if (dep == null) { - return true; - } - - return !dep.shouldDisableDependents(); - } - - private Preference getDependencyPreference() { - String depKey = mCheckBoxPreference.getDependency(); - if (TextUtils.isEmpty(depKey)) { - return null; + switch (state) { + case BluetoothAdapter.STATE_TURNING_ON: + mCheckBox.setSummary(R.string.wifi_starting); + mCheckBox.setEnabled(false); + break; + case BluetoothAdapter.STATE_ON: + mCheckBox.setChecked(true); + mCheckBox.setSummary(null); + mCheckBox.setEnabled(true); + break; + case BluetoothAdapter.STATE_TURNING_OFF: + mCheckBox.setSummary(R.string.wifi_stopping); + mCheckBox.setEnabled(false); + break; + case BluetoothAdapter.STATE_OFF: + mCheckBox.setChecked(false); + mCheckBox.setSummary(mOriginalSummary); + mCheckBox.setEnabled(true); + break; + default: + mCheckBox.setChecked(false); + mCheckBox.setSummary(R.string.wifi_error); + mCheckBox.setEnabled(true); } - - return mCheckBoxPreference.getPreferenceManager().findPreference(depKey); } - } |