diff --git a/src/com/android/settings/wifi/tether/WifiTetherSettings.java b/src/com/android/settings/wifi/tether/WifiTetherSettings.java index 66d017ae5ba..316060996a1 100644 --- a/src/com/android/settings/wifi/tether/WifiTetherSettings.java +++ b/src/com/android/settings/wifi/tether/WifiTetherSettings.java @@ -40,7 +40,6 @@ import com.android.settings.core.FeatureFlags; import com.android.settings.dashboard.RestrictedDashboardFragment; import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.widget.SwitchBar; -import com.android.settings.widget.SwitchBarController; import com.android.settingslib.TetherUtil; import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.search.SearchIndexable; @@ -130,8 +129,7 @@ public class WifiTetherSettings extends RestrictedDashboardFragment // SettingsActivity as base for all preference fragments. final SettingsActivity activity = (SettingsActivity) getActivity(); final SwitchBar switchBar = activity.getSwitchBar(); - mSwitchBarController = new WifiTetherSwitchBarController(activity, - new SwitchBarController(switchBar)); + mSwitchBarController = new WifiTetherSwitchBarController(activity, switchBar); getSettingsLifecycle().addObserver(mSwitchBarController); switchBar.show(); } diff --git a/src/com/android/settings/wifi/tether/WifiTetherSwitchBarController.java b/src/com/android/settings/wifi/tether/WifiTetherSwitchBarController.java index 0f31d19535c..a7b6772c21f 100644 --- a/src/com/android/settings/wifi/tether/WifiTetherSwitchBarController.java +++ b/src/com/android/settings/wifi/tether/WifiTetherSwitchBarController.java @@ -26,23 +26,27 @@ import android.net.ConnectivityManager; import android.net.wifi.WifiManager; import android.os.Handler; import android.os.Looper; -import android.provider.Settings; +import android.view.View; +import android.widget.Switch; import androidx.annotation.VisibleForTesting; import com.android.settings.datausage.DataSaverBackend; -import com.android.settings.widget.SwitchWidgetController; +import com.android.settings.widget.SwitchBar; import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStop; -public class WifiTetherSwitchBarController implements SwitchWidgetController.OnSwitchChangeListener, - LifecycleObserver, OnStart, OnStop, DataSaverBackend.Listener { - +/** + * Controller for logic pertaining to switch Wi-Fi tethering. + */ +public class WifiTetherSwitchBarController implements + LifecycleObserver, OnStart, OnStop, DataSaverBackend.Listener, View.OnClickListener { private static final IntentFilter WIFI_INTENT_FILTER; private final Context mContext; - private final SwitchWidgetController mSwitchBar; + private final SwitchBar mSwitchBar; + private final Switch mSwitch; private final ConnectivityManager mConnectivityManager; private final WifiManager mWifiManager; @@ -63,40 +67,38 @@ public class WifiTetherSwitchBarController implements SwitchWidgetController.OnS WIFI_INTENT_FILTER = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); } - WifiTetherSwitchBarController(Context context, SwitchWidgetController switchBar) { + WifiTetherSwitchBarController(Context context, SwitchBar switchBar) { mContext = context; mSwitchBar = switchBar; + mSwitch = mSwitchBar.getSwitch(); mDataSaverBackend = new DataSaverBackend(context); mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); mSwitchBar.setChecked(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED); - mSwitchBar.setListener(this); updateWifiSwitch(); } @Override public void onStart() { mDataSaverBackend.addListener(this); - mSwitchBar.startListening(); + mSwitch.setOnClickListener(this); mContext.registerReceiver(mReceiver, WIFI_INTENT_FILTER); } @Override public void onStop() { mDataSaverBackend.remListener(this); - mSwitchBar.stopListening(); mContext.unregisterReceiver(mReceiver); } @Override - public boolean onSwitchToggled(boolean isChecked) { - if (!isChecked) { - stopTether(); - } else if (!mWifiManager.isWifiApEnabled()) { + public void onClick(View v) { + if (mSwitch.isChecked()) { startTether(); + } else { + stopTether(); } - return true; } void stopTether() { @@ -128,23 +130,23 @@ public class WifiTetherSwitchBarController implements SwitchWidgetController.OnS mSwitchBar.setEnabled(false); break; case WifiManager.WIFI_AP_STATE_ENABLED: - if (!mSwitchBar.isChecked()) { - mSwitchBar.setChecked(true); + if (!mSwitch.isChecked()) { + mSwitch.setChecked(true); } updateWifiSwitch(); break; case WifiManager.WIFI_AP_STATE_DISABLING: - if (mSwitchBar.isChecked()) { - mSwitchBar.setChecked(false); + if (mSwitch.isChecked()) { + mSwitch.setChecked(false); } mSwitchBar.setEnabled(false); break; case WifiManager.WIFI_AP_STATE_DISABLED: - mSwitchBar.setChecked(false); + mSwitch.setChecked(false); updateWifiSwitch(); break; default: - mSwitchBar.setChecked(false); + mSwitch.setChecked(false); updateWifiSwitch(); break; } diff --git a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSwitchBarControllerTest.java b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSwitchBarControllerTest.java index 30358da69e4..e05541c970a 100644 --- a/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSwitchBarControllerTest.java +++ b/tests/robotests/src/com/android/settings/wifi/tether/WifiTetherSwitchBarControllerTest.java @@ -32,10 +32,8 @@ import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkPolicyManager; import android.net.wifi.WifiManager; -import android.provider.Settings; import com.android.settings.widget.SwitchBar; -import com.android.settings.widget.SwitchBarController; import org.junit.Before; import org.junit.Test; @@ -70,10 +68,9 @@ public class WifiTetherSwitchBarControllerTest { when(mContext.getSystemService(Context.NETWORK_POLICY_SERVICE)).thenReturn( mNetworkPolicyManager); - mController = new WifiTetherSwitchBarController(mContext, - new SwitchBarController(mSwitchBar)); + mController = new WifiTetherSwitchBarController(mContext, mSwitchBar); } - + @Test public void startTether_fail_resetSwitchBar() { when(mNetworkPolicyManager.getRestrictBackground()).thenReturn(false); @@ -103,12 +100,12 @@ public class WifiTetherSwitchBarControllerTest { @Test public void onSwitchToggled_onlyStartsTetherWhenNeeded() { when(mWifiManager.isWifiApEnabled()).thenReturn(true); - mController.onSwitchToggled(true); + mController.onClick(mSwitchBar.getSwitch()); verify(mConnectivityManager, never()).startTethering(anyInt(), anyBoolean(), any(), any()); doReturn(false).when(mWifiManager).isWifiApEnabled(); - mController.onSwitchToggled(true); + mController.onClick(mSwitchBar.getSwitch()); verify(mConnectivityManager, times(1)) .startTethering(anyInt(), anyBoolean(), any(), any());