[Wi-Fi] Control Wi-Fi Hotspot Settings using onClick instead of onSwitchToggled

In the current Settings framework onSwitchToggled has been implemented
to monitor the change in Settings done by user but its getting called
even when Settings is changed using setChecked where under certain
condition like rotation during config changed,
Tether Settings is disabled and then enabled and later before Rotation
Settings remained false due to stop Tethering with Tether Enabled
After Rotation when settings is initialized it is set as disabled
and even after receiving the Tether Enabled it remains Off.
Hence used onClick to monitor user ON/OFF Settings

Bug: 142625203
Test: make RunSettingsRoboTests ROBOTEST_FILTER=WifiTetherSwitchBarControllerTest.java
Test: Manual
1. Go to Wifi Hotspot Settings
2. Turn ON
3. Change SSID and save
4. Rotate screen soon after saving
Result: Hotspot does not turn ON, remains in OFF state.
Signed-off-by:JeiFeng Lee <linger.lee@mediatek.com>

Change-Id: Ibddacdf35eb3196f40cdb2c240efa902f9123646
This commit is contained in:
Arc Wang
2020-03-05 18:04:30 +08:00
parent 4a76598ba5
commit ac5e1acfba
3 changed files with 28 additions and 31 deletions

View File

@@ -40,7 +40,6 @@ import com.android.settings.core.FeatureFlags;
import com.android.settings.dashboard.RestrictedDashboardFragment; import com.android.settings.dashboard.RestrictedDashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.SwitchBar; import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchBarController;
import com.android.settingslib.TetherUtil; import com.android.settingslib.TetherUtil;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexable; import com.android.settingslib.search.SearchIndexable;
@@ -130,8 +129,7 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
// SettingsActivity as base for all preference fragments. // SettingsActivity as base for all preference fragments.
final SettingsActivity activity = (SettingsActivity) getActivity(); final SettingsActivity activity = (SettingsActivity) getActivity();
final SwitchBar switchBar = activity.getSwitchBar(); final SwitchBar switchBar = activity.getSwitchBar();
mSwitchBarController = new WifiTetherSwitchBarController(activity, mSwitchBarController = new WifiTetherSwitchBarController(activity, switchBar);
new SwitchBarController(switchBar));
getSettingsLifecycle().addObserver(mSwitchBarController); getSettingsLifecycle().addObserver(mSwitchBarController);
switchBar.show(); switchBar.show();
} }

View File

@@ -26,23 +26,27 @@ import android.net.ConnectivityManager;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Handler; import android.os.Handler;
import android.os.Looper; import android.os.Looper;
import android.provider.Settings; import android.view.View;
import android.widget.Switch;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import com.android.settings.datausage.DataSaverBackend; 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.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnStart; import com.android.settingslib.core.lifecycle.events.OnStart;
import com.android.settingslib.core.lifecycle.events.OnStop; 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 static final IntentFilter WIFI_INTENT_FILTER;
private final Context mContext; private final Context mContext;
private final SwitchWidgetController mSwitchBar; private final SwitchBar mSwitchBar;
private final Switch mSwitch;
private final ConnectivityManager mConnectivityManager; private final ConnectivityManager mConnectivityManager;
private final WifiManager mWifiManager; 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); WIFI_INTENT_FILTER = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
} }
WifiTetherSwitchBarController(Context context, SwitchWidgetController switchBar) { WifiTetherSwitchBarController(Context context, SwitchBar switchBar) {
mContext = context; mContext = context;
mSwitchBar = switchBar; mSwitchBar = switchBar;
mSwitch = mSwitchBar.getSwitch();
mDataSaverBackend = new DataSaverBackend(context); mDataSaverBackend = new DataSaverBackend(context);
mConnectivityManager = mConnectivityManager =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mSwitchBar.setChecked(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED); mSwitchBar.setChecked(mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED);
mSwitchBar.setListener(this);
updateWifiSwitch(); updateWifiSwitch();
} }
@Override @Override
public void onStart() { public void onStart() {
mDataSaverBackend.addListener(this); mDataSaverBackend.addListener(this);
mSwitchBar.startListening(); mSwitch.setOnClickListener(this);
mContext.registerReceiver(mReceiver, WIFI_INTENT_FILTER); mContext.registerReceiver(mReceiver, WIFI_INTENT_FILTER);
} }
@Override @Override
public void onStop() { public void onStop() {
mDataSaverBackend.remListener(this); mDataSaverBackend.remListener(this);
mSwitchBar.stopListening();
mContext.unregisterReceiver(mReceiver); mContext.unregisterReceiver(mReceiver);
} }
@Override @Override
public boolean onSwitchToggled(boolean isChecked) { public void onClick(View v) {
if (!isChecked) { if (mSwitch.isChecked()) {
stopTether();
} else if (!mWifiManager.isWifiApEnabled()) {
startTether(); startTether();
} else {
stopTether();
} }
return true;
} }
void stopTether() { void stopTether() {
@@ -128,23 +130,23 @@ public class WifiTetherSwitchBarController implements SwitchWidgetController.OnS
mSwitchBar.setEnabled(false); mSwitchBar.setEnabled(false);
break; break;
case WifiManager.WIFI_AP_STATE_ENABLED: case WifiManager.WIFI_AP_STATE_ENABLED:
if (!mSwitchBar.isChecked()) { if (!mSwitch.isChecked()) {
mSwitchBar.setChecked(true); mSwitch.setChecked(true);
} }
updateWifiSwitch(); updateWifiSwitch();
break; break;
case WifiManager.WIFI_AP_STATE_DISABLING: case WifiManager.WIFI_AP_STATE_DISABLING:
if (mSwitchBar.isChecked()) { if (mSwitch.isChecked()) {
mSwitchBar.setChecked(false); mSwitch.setChecked(false);
} }
mSwitchBar.setEnabled(false); mSwitchBar.setEnabled(false);
break; break;
case WifiManager.WIFI_AP_STATE_DISABLED: case WifiManager.WIFI_AP_STATE_DISABLED:
mSwitchBar.setChecked(false); mSwitch.setChecked(false);
updateWifiSwitch(); updateWifiSwitch();
break; break;
default: default:
mSwitchBar.setChecked(false); mSwitch.setChecked(false);
updateWifiSwitch(); updateWifiSwitch();
break; break;
} }

View File

@@ -32,10 +32,8 @@ import android.content.Context;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkPolicyManager; import android.net.NetworkPolicyManager;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.provider.Settings;
import com.android.settings.widget.SwitchBar; import com.android.settings.widget.SwitchBar;
import com.android.settings.widget.SwitchBarController;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@@ -70,8 +68,7 @@ public class WifiTetherSwitchBarControllerTest {
when(mContext.getSystemService(Context.NETWORK_POLICY_SERVICE)).thenReturn( when(mContext.getSystemService(Context.NETWORK_POLICY_SERVICE)).thenReturn(
mNetworkPolicyManager); mNetworkPolicyManager);
mController = new WifiTetherSwitchBarController(mContext, mController = new WifiTetherSwitchBarController(mContext, mSwitchBar);
new SwitchBarController(mSwitchBar));
} }
@Test @Test
@@ -103,12 +100,12 @@ public class WifiTetherSwitchBarControllerTest {
@Test @Test
public void onSwitchToggled_onlyStartsTetherWhenNeeded() { public void onSwitchToggled_onlyStartsTetherWhenNeeded() {
when(mWifiManager.isWifiApEnabled()).thenReturn(true); when(mWifiManager.isWifiApEnabled()).thenReturn(true);
mController.onSwitchToggled(true); mController.onClick(mSwitchBar.getSwitch());
verify(mConnectivityManager, never()).startTethering(anyInt(), anyBoolean(), any(), any()); verify(mConnectivityManager, never()).startTethering(anyInt(), anyBoolean(), any(), any());
doReturn(false).when(mWifiManager).isWifiApEnabled(); doReturn(false).when(mWifiManager).isWifiApEnabled();
mController.onSwitchToggled(true); mController.onClick(mSwitchBar.getSwitch());
verify(mConnectivityManager, times(1)) verify(mConnectivityManager, times(1))
.startTethering(anyInt(), anyBoolean(), any(), any()); .startTethering(anyInt(), anyBoolean(), any(), any());