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