diff options
| author | Roman Birg <roman@cyngn.com> | 2015-01-05 13:32:20 -0800 |
|---|---|---|
| committer | Danesh M <daneshm90@gmail.com> | 2015-12-15 18:31:05 -0800 |
| commit | c5cd017e7e168c5b69072dacda74aa2f298c7ab0 (patch) | |
| tree | c061f473d41602c0ce7058c7507d62d89ee796e6 /src/com/android/settings/bluetooth | |
| parent | e1f1ba9036f23f439dbeab6b48a16bcf97db3b93 (diff) | |
| download | packages_apps_Settings-c5cd017e7e168c5b69072dacda74aa2f298c7ab0.tar.gz packages_apps_Settings-c5cd017e7e168c5b69072dacda74aa2f298c7ab0.tar.bz2 packages_apps_Settings-c5cd017e7e168c5b69072dacda74aa2f298c7ab0.zip | |
Settings: add switches for dashboard items
Change-Id: Ibff81510270745807a4b133457d60d47dd629df6
Signed-off-by: Roman Birg <roman@cyngn.com>
Diffstat (limited to 'src/com/android/settings/bluetooth')
| -rw-r--r-- | src/com/android/settings/bluetooth/BluetoothEnabler.java | 81 |
1 files changed, 38 insertions, 43 deletions
diff --git a/src/com/android/settings/bluetooth/BluetoothEnabler.java b/src/com/android/settings/bluetooth/BluetoothEnabler.java index 4f442ceb5..7d6bebecc 100644 --- a/src/com/android/settings/bluetooth/BluetoothEnabler.java +++ b/src/com/android/settings/bluetooth/BluetoothEnabler.java @@ -24,11 +24,13 @@ import android.content.IntentFilter; import android.os.Handler; import android.os.Message; import android.provider.Settings; +import android.widget.CompoundButton; import android.widget.Switch; import android.widget.Toast; import com.android.internal.logging.MetricsLogger; import com.android.settings.R; +import com.android.settings.dashboard.GenericSwitchToggle; import com.android.settings.search.Index; import com.android.settings.widget.SwitchBar; import com.android.settingslib.WirelessUtils; @@ -40,13 +42,9 @@ import com.android.settingslib.bluetooth.LocalBluetoothManager; * preference. It turns on/off Bluetooth and ensures the summary of the * preference reflects the current state. */ -public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener { - private Context mContext; - private Switch mSwitch; - private SwitchBar mSwitchBar; - private boolean mValidListener; - private final LocalBluetoothAdapter mLocalAdapter; - private final IntentFilter mIntentFilter; +public final class BluetoothEnabler extends GenericSwitchToggle { + private LocalBluetoothAdapter mLocalAdapter; + private IntentFilter mIntentFilter; private static final String EVENT_DATA_IS_BT_ON = "is_bluetooth_on"; private static final int EVENT_UPDATE_INDEX = 0; @@ -75,16 +73,23 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener }; public BluetoothEnabler(Context context, SwitchBar switchBar) { - mContext = context; - mSwitchBar = switchBar; - mSwitch = switchBar.getSwitch(); - mValidListener = false; + super(context, switchBar); - LocalBluetoothManager manager = Utils.getLocalBtManager(context); + init(); + } + + public BluetoothEnabler(Context context, Switch switch_) { + super(context, switch_); + + init(); + } + + private void init() { + LocalBluetoothManager manager = Utils.getLocalBtManager(mContext); if (manager == null) { // Bluetooth is not supported mLocalAdapter = null; - mSwitch.setEnabled(false); + setEnabled(false); } else { mLocalAdapter = manager.getBluetoothAdapter(); } @@ -99,74 +104,56 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener mSwitchBar.hide(); } + @Override public void resume(Context context) { + super.resume(context); if (mLocalAdapter == null) { - mSwitch.setEnabled(false); + setEnabled(false); return; } - if (mContext != context) { - mContext = context; - } - // Bluetooth state is not sticky, so set it manually handleStateChanged(mLocalAdapter.getBluetoothState()); - mSwitchBar.addOnSwitchChangeListener(this); mContext.registerReceiver(mReceiver, mIntentFilter); - mValidListener = true; } + @Override public void pause() { + super.pause(); if (mLocalAdapter == null) { return; } - mSwitchBar.removeOnSwitchChangeListener(this); mContext.unregisterReceiver(mReceiver); - mValidListener = false; } void handleStateChanged(int state) { switch (state) { case BluetoothAdapter.STATE_TURNING_ON: - mSwitch.setEnabled(false); + setEnabled(false); break; case BluetoothAdapter.STATE_ON: setChecked(true); - mSwitch.setEnabled(true); + setEnabled(true); updateSearchIndex(true); mLocalAdapter.setScanMode(BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE); break; case BluetoothAdapter.STATE_TURNING_OFF: - mSwitch.setEnabled(false); + setEnabled(false); break; case BluetoothAdapter.STATE_OFF: setChecked(false); - mSwitch.setEnabled(true); + setEnabled(true); updateSearchIndex(false); break; default: setChecked(false); - mSwitch.setEnabled(true); + setEnabled(true); updateSearchIndex(false); } } - private void setChecked(boolean isChecked) { - if (isChecked != mSwitch.isChecked()) { - // set listener to null, so onCheckedChanged won't be called - // if the checked status on Switch isn't changed by user click - if (mValidListener) { - mSwitchBar.removeOnSwitchChangeListener(this); - } - mSwitch.setChecked(isChecked); - if (mValidListener) { - mSwitchBar.addOnSwitchChangeListener(this); - } - } - } - private void updateSearchIndex(boolean isBluetoothOn) { mHandler.removeMessages(EVENT_UPDATE_INDEX); @@ -178,12 +165,15 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener @Override public void onSwitchChanged(Switch switchView, boolean isChecked) { + if (mStateMachineEvent) { + return; + } // Show toast message if Bluetooth is not allowed in airplane mode if (isChecked && !WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) { Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show(); // Reset switch to off - switchView.setChecked(false); + setChecked(false); } MetricsLogger.action(mContext, MetricsLogger.ACTION_BLUETOOTH_TOGGLE, isChecked); @@ -191,6 +181,11 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener if (mLocalAdapter != null) { mLocalAdapter.setBluetoothEnabled(isChecked); } - mSwitch.setEnabled(false); + setEnabled(false); + } + + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + super.onCheckedChanged(buttonView, isChecked); } } |
