From 9891b74533398e951e07ab24cbfdb58bdb12894f Mon Sep 17 00:00:00 2001 From: jackqdyulei Date: Thu, 30 Nov 2017 13:55:09 -0800 Subject: [PATCH] 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 --- .../settings/bluetooth/BluetoothEnabler.java | 66 ++++++++----------- .../widget/MasterSwitchController.java | 5 -- .../widget/MasterSwitchPreference.java | 2 +- .../settings/widget/SwitchBarController.java | 6 -- .../widget/SwitchWidgetController.java | 9 --- 5 files changed, 30 insertions(+), 58 deletions(-) diff --git a/src/com/android/settings/bluetooth/BluetoothEnabler.java b/src/com/android/settings/bluetooth/BluetoothEnabler.java index f95145d0bbb..87fa43d2db2 100644 --- a/src/com/android/settings/bluetooth/BluetoothEnabler.java +++ b/src/com/android/settings/bluetooth/BluetoothEnabler.java @@ -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; } diff --git a/src/com/android/settings/widget/MasterSwitchController.java b/src/com/android/settings/widget/MasterSwitchController.java index f7253fd5e7a..a22640c29cd 100644 --- a/src/com/android/settings/widget/MasterSwitchController.java +++ b/src/com/android/settings/widget/MasterSwitchController.java @@ -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(); - } } diff --git a/src/com/android/settings/widget/MasterSwitchPreference.java b/src/com/android/settings/widget/MasterSwitchPreference.java index 29bc17bce5a..d47de88b1c1 100644 --- a/src/com/android/settings/widget/MasterSwitchPreference.java +++ b/src/com/android/settings/widget/MasterSwitchPreference.java @@ -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) { diff --git a/src/com/android/settings/widget/SwitchBarController.java b/src/com/android/settings/widget/SwitchBarController.java index 624db2aa164..cc4c8dcaa55 100644 --- a/src/com/android/settings/widget/SwitchBarController.java +++ b/src/com/android/settings/widget/SwitchBarController.java @@ -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(); - } - } diff --git a/src/com/android/settings/widget/SwitchWidgetController.java b/src/com/android/settings/widget/SwitchWidgetController.java index 325a093767f..6c4f40ac78a 100644 --- a/src/com/android/settings/widget/SwitchWidgetController.java +++ b/src/com/android/settings/widget/SwitchWidgetController.java @@ -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(); - } \ No newline at end of file