Settings refactoring

Wifi and bluetooth pane refactored, main toggles moved to left pane.

Change-Id: I42ea4cf3bdf24158f3c67e0dea311258206cd78a
This commit is contained in:
Gilles Debunne
2011-06-20 15:00:07 -07:00
parent 222a619668
commit e78c187905
33 changed files with 1216 additions and 604 deletions

View File

@@ -16,28 +16,27 @@
package com.android.settings.bluetooth;
import com.android.settings.R;
import com.android.settings.WirelessSettings;
import android.bluetooth.BluetoothAdapter;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.preference.Preference;
import android.preference.CheckBoxPreference;
import android.provider.Settings;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.Toast;
import com.android.settings.R;
import com.android.settings.WirelessSettings;
/**
* BluetoothEnabler is a helper to manage the Bluetooth on/off checkbox
* preference. It turns on/off Bluetooth and ensures the summary of the
* preference reflects the current state.
*/
public final class BluetoothEnabler implements Preference.OnPreferenceChangeListener {
public final class BluetoothEnabler implements CompoundButton.OnCheckedChangeListener {
private final Context mContext;
private final CheckBoxPreference mCheckBox;
private final CharSequence mOriginalSummary;
private Switch mSwitch;
private final LocalBluetoothAdapter mLocalAdapter;
private final IntentFilter mIntentFilter;
@@ -50,17 +49,15 @@ public final class BluetoothEnabler implements Preference.OnPreferenceChangeList
}
};
public BluetoothEnabler(Context context, CheckBoxPreference checkBox) {
public BluetoothEnabler(Context context, Switch switch_) {
mContext = context;
mCheckBox = checkBox;
mOriginalSummary = checkBox.getSummary();
checkBox.setPersistent(false);
mSwitch = switch_;
LocalBluetoothManager manager = LocalBluetoothManager.getInstance(context);
if (manager == null) {
// Bluetooth is not supported
mLocalAdapter = null;
checkBox.setEnabled(false);
mSwitch.setEnabled(false);
} else {
mLocalAdapter = manager.getBluetoothAdapter();
}
@@ -76,7 +73,7 @@ public final class BluetoothEnabler implements Preference.OnPreferenceChangeList
handleStateChanged(mLocalAdapter.getBluetoothState());
mContext.registerReceiver(mReceiver, mIntentFilter);
mCheckBox.setOnPreferenceChangeListener(this);
mSwitch.setOnCheckedChangeListener(this);
}
public void pause() {
@@ -85,51 +82,54 @@ public final class BluetoothEnabler implements Preference.OnPreferenceChangeList
}
mContext.unregisterReceiver(mReceiver);
mCheckBox.setOnPreferenceChangeListener(null);
mSwitch.setOnCheckedChangeListener(null);
}
public boolean onPreferenceChange(Preference preference, Object value) {
boolean enable = (Boolean) value;
public void setSwitch(Switch switch_) {
if (mSwitch == switch_) return;
mSwitch.setOnCheckedChangeListener(null);
mSwitch = switch_;
mSwitch.setOnCheckedChangeListener(this);
final int bluetoothState = mLocalAdapter.getBluetoothState();
boolean isEnabled = bluetoothState == BluetoothAdapter.STATE_ON;
boolean isDisabled = bluetoothState == BluetoothAdapter.STATE_OFF;
mSwitch.setChecked(isEnabled);
mSwitch.setEnabled(isEnabled || isDisabled);
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// 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;
if (isChecked &&
!WirelessSettings.isRadioAllowed(mContext, Settings.System.RADIO_BLUETOOTH)) {
Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
// Reset switch to off
buttonView.setChecked(false);
}
mLocalAdapter.setBluetoothEnabled(enable);
mCheckBox.setEnabled(false);
// Don't update UI to opposite state until we're sure
return false;
mLocalAdapter.setBluetoothEnabled(isChecked);
mSwitch.setEnabled(false);
}
void handleStateChanged(int state) {
switch (state) {
case BluetoothAdapter.STATE_TURNING_ON:
mCheckBox.setSummary(R.string.wifi_starting);
mCheckBox.setEnabled(false);
mSwitch.setEnabled(false);
break;
case BluetoothAdapter.STATE_ON:
mCheckBox.setChecked(true);
mCheckBox.setSummary(null);
mCheckBox.setEnabled(true);
mSwitch.setChecked(true);
mSwitch.setEnabled(true);
break;
case BluetoothAdapter.STATE_TURNING_OFF:
mCheckBox.setSummary(R.string.wifi_stopping);
mCheckBox.setEnabled(false);
mSwitch.setEnabled(false);
break;
case BluetoothAdapter.STATE_OFF:
mCheckBox.setChecked(false);
mCheckBox.setSummary(mOriginalSummary);
mCheckBox.setEnabled(true);
mSwitch.setChecked(false);
mSwitch.setEnabled(true);
break;
default:
mCheckBox.setChecked(false);
mCheckBox.setSummary(R.string.wifi_error);
mCheckBox.setEnabled(true);
mSwitch.setChecked(false);
mSwitch.setEnabled(true);
}
}
}