Merge "Remove the getSwitch() in SwitchWidgetController"

This commit is contained in:
Lei Yu
2017-12-01 19:13:56 +00:00
committed by Android (Google) Code Review
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;
}

View File

@@ -74,9 +74,4 @@ public class MasterSwitchController extends SwitchWidgetController implements
public void setDisabledByAdmin(EnforcedAdmin 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() {
return mSwitch != null && mSwitch.isEnabled() && mChecked;
return mSwitch != null && mChecked;
}
public void setChecked(boolean checked) {

View File

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

View File

@@ -16,7 +16,6 @@
package com.android.settings.widget;
import android.widget.Switch;
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.
*/
public abstract void setDisabledByAdmin(EnforcedAdmin admin);
/**
* Get the underlying switch widget.
*
* @return the switch widget.
*/
public abstract Switch getSwitch();
}