Remove the getSwitch() in SwitchWidgetController

IMHO we don't need to expose switch in SwitchWidgetController.
This controller already has enough API to control the switch.

Also rename mSwitchWidget to mSwitchController because it is not a
widget.

Bug: 69973752
Test: test still pass
Change-Id: I0ac247e34468a44109ab26019f1303c814e381f2
This commit is contained in:
jackqdyulei
2017-11-30 13:55:09 -08:00
parent d5fff3645a
commit 9891b74533
5 changed files with 30 additions and 58 deletions

View File

@@ -23,7 +23,6 @@ import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.os.UserManager; import android.os.UserManager;
import android.provider.Settings; import android.provider.Settings;
import android.widget.Switch;
import android.widget.Toast; import android.widget.Toast;
import com.android.internal.annotations.VisibleForTesting; import com.android.internal.annotations.VisibleForTesting;
@@ -41,8 +40,7 @@ import com.android.settingslib.bluetooth.LocalBluetoothManager;
* preference reflects the current state. * preference reflects the current state.
*/ */
public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchChangeListener { public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchChangeListener {
private final Switch mSwitch; private final SwitchWidgetController mSwitchController;
private final SwitchWidgetController mSwitchWidget;
private final MetricsFeatureProvider mMetricsFeatureProvider; private final MetricsFeatureProvider mMetricsFeatureProvider;
private Context mContext; private Context mContext;
private boolean mValidListener; private boolean mValidListener;
@@ -64,28 +62,27 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
} }
}; };
public BluetoothEnabler(Context context, SwitchWidgetController switchWidget, public BluetoothEnabler(Context context, SwitchWidgetController switchController,
MetricsFeatureProvider metricsFeatureProvider, LocalBluetoothManager manager, MetricsFeatureProvider metricsFeatureProvider, LocalBluetoothManager manager,
int metricsEvent) { int metricsEvent) {
this(context, switchWidget, metricsFeatureProvider, manager, metricsEvent, this(context, switchController, metricsFeatureProvider, manager, metricsEvent,
new RestrictionUtils()); new RestrictionUtils());
} }
public BluetoothEnabler(Context context, SwitchWidgetController switchWidget, public BluetoothEnabler(Context context, SwitchWidgetController switchController,
MetricsFeatureProvider metricsFeatureProvider, LocalBluetoothManager manager, MetricsFeatureProvider metricsFeatureProvider, LocalBluetoothManager manager,
int metricsEvent, RestrictionUtils restrictionUtils) { int metricsEvent, RestrictionUtils restrictionUtils) {
mContext = context; mContext = context;
mMetricsFeatureProvider = metricsFeatureProvider; mMetricsFeatureProvider = metricsFeatureProvider;
mSwitchWidget = switchWidget; mSwitchController = switchController;
mSwitch = mSwitchWidget.getSwitch(); mSwitchController.setListener(this);
mSwitchWidget.setListener(this);
mValidListener = false; mValidListener = false;
mMetricsEvent = metricsEvent; mMetricsEvent = metricsEvent;
if (manager == null) { if (manager == null) {
// Bluetooth is not supported // Bluetooth is not supported
mLocalAdapter = null; mLocalAdapter = null;
mSwitchWidget.setEnabled(false); mSwitchController.setEnabled(false);
} else { } else {
mLocalAdapter = manager.getBluetoothAdapter(); mLocalAdapter = manager.getBluetoothAdapter();
} }
@@ -94,11 +91,11 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
} }
public void setupSwitchController() { public void setupSwitchController() {
mSwitchWidget.setupView(); mSwitchController.setupView();
} }
public void teardownSwitchController() { public void teardownSwitchController() {
mSwitchWidget.teardownView(); mSwitchController.teardownView();
} }
public void resume(Context context) { public void resume(Context context) {
@@ -109,7 +106,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
final boolean restricted = maybeEnforceRestrictions(); final boolean restricted = maybeEnforceRestrictions();
if (mLocalAdapter == null) { if (mLocalAdapter == null) {
mSwitchWidget.setEnabled(false); mSwitchController.setEnabled(false);
return; return;
} }
@@ -118,7 +115,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
handleStateChanged(mLocalAdapter.getBluetoothState()); handleStateChanged(mLocalAdapter.getBluetoothState());
} }
mSwitchWidget.startListening(); mSwitchController.startListening();
mContext.registerReceiver(mReceiver, mIntentFilter); mContext.registerReceiver(mReceiver, mIntentFilter);
mValidListener = true; mValidListener = true;
} }
@@ -128,7 +125,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
return; return;
} }
if (mValidListener) { if (mValidListener) {
mSwitchWidget.stopListening(); mSwitchController.stopListening();
mContext.unregisterReceiver(mReceiver); mContext.unregisterReceiver(mReceiver);
mValidListener = false; mValidListener = false;
} }
@@ -137,37 +134,35 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
void handleStateChanged(int state) { void handleStateChanged(int state) {
switch (state) { switch (state) {
case BluetoothAdapter.STATE_TURNING_ON: case BluetoothAdapter.STATE_TURNING_ON:
mSwitchWidget.setEnabled(false); mSwitchController.setEnabled(false);
break; break;
case BluetoothAdapter.STATE_ON: case BluetoothAdapter.STATE_ON:
setChecked(true); setChecked(true);
mSwitchWidget.setEnabled(true); mSwitchController.setEnabled(true);
break; break;
case BluetoothAdapter.STATE_TURNING_OFF: case BluetoothAdapter.STATE_TURNING_OFF:
mSwitchWidget.setEnabled(false); mSwitchController.setEnabled(false);
break; break;
case BluetoothAdapter.STATE_OFF: case BluetoothAdapter.STATE_OFF:
setChecked(false); setChecked(false);
mSwitchWidget.setEnabled(true); mSwitchController.setEnabled(true);
break; break;
default: default:
setChecked(false); setChecked(false);
mSwitchWidget.setEnabled(true); mSwitchController.setEnabled(true);
} }
} }
private void setChecked(boolean isChecked) { private void setChecked(boolean isChecked) {
final boolean currentState = if (isChecked != mSwitchController.isChecked()) {
(mSwitchWidget.getSwitch() != null) && mSwitchWidget.getSwitch().isChecked();
if (isChecked != currentState) {
// set listener to null, so onCheckedChanged won't be called // set listener to null, so onCheckedChanged won't be called
// if the checked status on Switch isn't changed by user click // if the checked status on Switch isn't changed by user click
if (mValidListener) { if (mValidListener) {
mSwitchWidget.stopListening(); mSwitchController.stopListening();
} }
mSwitchWidget.setChecked(isChecked); mSwitchController.setChecked(isChecked);
if (mValidListener) { if (mValidListener) {
mSwitchWidget.startListening(); mSwitchController.startListening();
} }
} }
} }
@@ -183,7 +178,7 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
!WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) { !WirelessUtils.isRadioAllowed(mContext, Settings.Global.RADIO_BLUETOOTH)) {
Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show(); Toast.makeText(mContext, R.string.wifi_in_airplane_mode, Toast.LENGTH_SHORT).show();
// Reset switch to off // Reset switch to off
mSwitch.setChecked(false); mSwitchController.setChecked(false);
return false; return false;
} }
@@ -195,13 +190,13 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
// a) The switch should be OFF but it should still be togglable (enabled = True) // a) The switch should be OFF but it should still be togglable (enabled = True)
// b) The switch bar should have OFF text. // b) The switch bar should have OFF text.
if (isChecked && !status) { if (isChecked && !status) {
mSwitch.setChecked(false); mSwitchController.setChecked(false);
mSwitch.setEnabled(true); mSwitchController.setEnabled(true);
mSwitchWidget.updateTitle(false); mSwitchController.updateTitle(false);
return false; return false;
} }
} }
mSwitchWidget.setEnabled(false); mSwitchController.setEnabled(false);
return true; return true;
} }
@@ -213,13 +208,10 @@ public final class BluetoothEnabler implements SwitchWidgetController.OnSwitchCh
@VisibleForTesting @VisibleForTesting
boolean maybeEnforceRestrictions() { boolean maybeEnforceRestrictions() {
EnforcedAdmin admin = getEnforcedAdmin(mRestrictionUtils, mContext); EnforcedAdmin admin = getEnforcedAdmin(mRestrictionUtils, mContext);
mSwitchWidget.setDisabledByAdmin(admin); mSwitchController.setDisabledByAdmin(admin);
if (admin != null) { if (admin != null) {
mSwitchWidget.setChecked(false); mSwitchController.setChecked(false);
if (mSwitch != null) { mSwitchController.setEnabled(false);
mSwitch.setEnabled(false);
mSwitch.setChecked(false);
}
} }
return admin != null; return admin != null;
} }

View File

@@ -74,9 +74,4 @@ public class MasterSwitchController extends SwitchWidgetController implements
public void setDisabledByAdmin(EnforcedAdmin admin) { public void setDisabledByAdmin(EnforcedAdmin admin) {
mPreference.setDisabledByAdmin(admin); mPreference.setDisabledByAdmin(admin);
} }
@Override
public Switch getSwitch() {
return mPreference.getSwitch();
}
} }

View File

@@ -90,7 +90,7 @@ public class MasterSwitchPreference extends TwoTargetPreference {
} }
public boolean isChecked() { public boolean isChecked() {
return mSwitch != null && mSwitch.isEnabled() && mChecked; return mSwitch != null && mChecked;
} }
public void setChecked(boolean checked) { public void setChecked(boolean checked) {

View File

@@ -82,10 +82,4 @@ public class SwitchBarController extends SwitchWidgetController implements
public void setDisabledByAdmin(EnforcedAdmin admin) { public void setDisabledByAdmin(EnforcedAdmin admin) {
mSwitchBar.setDisabledByAdmin(admin); mSwitchBar.setDisabledByAdmin(admin);
} }
@Override
public Switch getSwitch() {
return mSwitchBar.getSwitch();
}
} }

View File

@@ -16,7 +16,6 @@
package com.android.settings.widget; package com.android.settings.widget;
import android.widget.Switch;
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
/* /*
@@ -108,12 +107,4 @@ public abstract class SwitchWidgetController {
* is {@code null}, then this preference will be enabled. Otherwise, it will be disabled. * is {@code null}, then this preference will be enabled. Otherwise, it will be disabled.
*/ */
public abstract void setDisabledByAdmin(EnforcedAdmin admin); public abstract void setDisabledByAdmin(EnforcedAdmin admin);
/**
* Get the underlying switch widget.
*
* @return the switch widget.
*/
public abstract Switch getSwitch();
} }