Add new preference class MasterSwitchPreference.

- Add a new preference type that has Title and optional summary on the
  left, and a toggle switch on the right. Clicking the left part of the
  preference will open a settings screen.
- Update Connected devices->Bluetooth to use this new preference.
- Refactor BluetoothSettings and BluetoothEnabler to share code between
  the new bluetooth preference controller and the bluetooth setting.

Bug: 34280769
Test: make RunSettingsRoboTests
Change-Id: I109ecdba640ecdd4748a6e5b2b4f4c47cbf653fd
This commit is contained in:
Doris Ling
2017-01-18 13:59:36 -08:00
parent 8f969665e3
commit 1432cb8529
16 changed files with 1107 additions and 233 deletions

View File

@@ -24,14 +24,13 @@ import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.provider.Settings;
import android.widget.Switch;
import android.widget.Toast;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R;
import com.android.settings.core.instrumentation.MetricsFeatureProvider;
import com.android.settings.search.Index;
import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchWidgetController;
import com.android.settingslib.WirelessUtils;
import com.android.settingslib.bluetooth.LocalBluetoothAdapter;
import com.android.settingslib.bluetooth.LocalBluetoothManager;
@@ -41,9 +40,8 @@ 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 final Switch mSwitch;
private final SwitchBar mSwitchBar;
public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchChangeListener {
private final SwitchWidgetController mSwitchWidget;
private final MetricsFeatureProvider mMetricsFeatureProvider;
private Context mContext;
private boolean mValidListener;
@@ -76,19 +74,18 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
}
};
public BluetoothEnabler(Context context, SwitchBar switchBar,
MetricsFeatureProvider metricsFeatureProvider) {
public BluetoothEnabler(Context context, SwitchWidgetController switchWidget,
MetricsFeatureProvider metricsFeatureProvider, LocalBluetoothManager manager) {
mContext = context;
mMetricsFeatureProvider = metricsFeatureProvider;
mSwitchBar = switchBar;
mSwitch = switchBar.getSwitch();
mSwitchWidget = switchWidget;
mSwitchWidget.setListener(this);
mValidListener = false;
LocalBluetoothManager manager = Utils.getLocalBtManager(context);
if (manager == null) {
// Bluetooth is not supported
mLocalAdapter = null;
mSwitch.setEnabled(false);
mSwitchWidget.setEnabled(false);
} else {
mLocalAdapter = manager.getBluetoothAdapter();
}
@@ -96,16 +93,16 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
}
public void setupSwitchBar() {
mSwitchBar.show();
mSwitchWidget.setupView();
}
public void teardownSwitchBar() {
mSwitchBar.hide();
mSwitchWidget.teardownView();
}
public void resume(Context context) {
if (mLocalAdapter == null) {
mSwitch.setEnabled(false);
mSwitchWidget.setEnabled(false);
return;
}
@@ -116,7 +113,7 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
// Bluetooth state is not sticky, so set it manually
handleStateChanged(mLocalAdapter.getBluetoothState());
mSwitchBar.addOnSwitchChangeListener(this);
mSwitchWidget.startListening();
mContext.registerReceiver(mReceiver, mIntentFilter);
mValidListener = true;
}
@@ -125,47 +122,48 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
if (mLocalAdapter == null) {
return;
}
mSwitchBar.removeOnSwitchChangeListener(this);
mContext.unregisterReceiver(mReceiver);
mValidListener = false;
if (mValidListener) {
mSwitchWidget.stopListening();
mContext.unregisterReceiver(mReceiver);
mValidListener = false;
}
}
void handleStateChanged(int state) {
switch (state) {
case BluetoothAdapter.STATE_TURNING_ON:
mSwitch.setEnabled(false);
mSwitchWidget.setEnabled(false);
break;
case BluetoothAdapter.STATE_ON:
setChecked(true);
mSwitch.setEnabled(true);
mSwitchWidget.setEnabled(true);
updateSearchIndex(true);
break;
case BluetoothAdapter.STATE_TURNING_OFF:
mSwitch.setEnabled(false);
mSwitchWidget.setEnabled(false);
break;
case BluetoothAdapter.STATE_OFF:
setChecked(false);
mSwitch.setEnabled(true);
mSwitchWidget.setEnabled(true);
updateSearchIndex(false);
break;
default:
setChecked(false);
mSwitch.setEnabled(true);
mSwitchWidget.setEnabled(true);
updateSearchIndex(false);
}
}
private void setChecked(boolean isChecked) {
if (isChecked != mSwitch.isChecked()) {
if (isChecked != mSwitchWidget.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);
mSwitchWidget.stopListening();
}
mSwitch.setChecked(isChecked);
mSwitchWidget.setChecked(isChecked);
if (mValidListener) {
mSwitchBar.addOnSwitchChangeListener(this);
mSwitchWidget.startListening();
}
}
}
@@ -180,13 +178,14 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
}
@Override
public void onSwitchChanged(Switch switchView, boolean isChecked) {
public boolean onSwitchToggled(boolean isChecked) {
// 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);
mSwitchWidget.setChecked(false);
return false;
}
mMetricsFeatureProvider.action(mContext, MetricsEvent.ACTION_BLUETOOTH_TOGGLE, isChecked);
@@ -197,12 +196,13 @@ public final class BluetoothEnabler implements SwitchBar.OnSwitchChangeListener
// a) The switch should be OFF but it should still be togglable (enabled = True)
// b) The switch bar should have OFF text.
if (isChecked && !status) {
switchView.setChecked(false);
mSwitch.setEnabled(true);
mSwitchBar.setTextViewLabel(false);
return;
mSwitchWidget.setChecked(false);
mSwitchWidget.setEnabled(true);
mSwitchWidget.updateTitle(false);
return false;
}
}
mSwitch.setEnabled(false);
mSwitchWidget.setEnabled(false);
return true;
}
}