Restart Wi-Fi tethering automatically if configuration change

- When the Wi-Fi Hotspot is already started, if the user changes the configuration, the Wi-Fi Hotspot will be restarted automatically.

- When the Wi-Fi hotspot restarts, display a circle on the screen to indicate that it is processing.

Bug: 245258763
Test: manual test
atest -c WifiTetherSettingsTest \
         WifiHotspotSpeedSettingsTest
atest -c WifiHotspotRepositoryTest \
         WifiHotspotSecuritySettingsTest \
         WifiHotspotSecurityViewModelTest \
         WifiHotspotSpeedViewModelTest \
         WifiTetherViewModelTest

Change-Id: I6fdd5892916703095f28d0589ebc3b7dd59fcd61
This commit is contained in:
Weng Su
2023-04-20 21:40:30 +08:00
parent 0a506448b8
commit bf0e8c1dc7
15 changed files with 552 additions and 178 deletions

View File

@@ -19,6 +19,7 @@ package com.android.settings.wifi.tether;
import android.app.settings.SettingsEnums;
import android.os.Bundle;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.LiveData;
import com.android.settings.R;
@@ -72,6 +73,7 @@ public class WifiHotspotSecuritySettings extends DashboardFragment implements
SelectorWithWidgetPreference preference = findPreference(viewItem.mKey);
preference.setOnClickListener(this);
}
mWifiHotspotSecurityViewModel.getRestarting().observe(this, this::onRestartingChanged);
}
protected void onViewItemListDataChanged(
@@ -96,6 +98,12 @@ public class WifiHotspotSecuritySettings extends DashboardFragment implements
}
}
@VisibleForTesting
void onRestartingChanged(Boolean restarting) {
log("onRestartingChanged(), restarting:" + restarting);
setLoading(restarting, false);
}
@Override
public void onRadioButtonClicked(SelectorWithWidgetPreference emiter) {
String key = emiter.getKey();

View File

@@ -129,6 +129,13 @@ public class WifiHotspotSecurityViewModel extends AndroidViewModel {
mViewInfoListData.setValue(mViewItemMap.values().stream().toList());
}
/**
* Gets Restarting LiveData
*/
public LiveData<Boolean> getRestarting() {
return mWifiHotspotRepository.getRestarting();
}
/**
* Wi-Fi Hotspot View Item
*/

View File

@@ -24,6 +24,8 @@ import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_6
import android.app.settings.SettingsEnums;
import android.os.Bundle;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.overlay.FeatureFactory;
@@ -80,6 +82,7 @@ public class WifiHotspotSpeedSettings extends DashboardFragment implements
onSpeedInfoMapDataChanged(mWifiHotspotSpeedViewModel.getSpeedInfoMapData().getValue());
mWifiHotspotSpeedViewModel.getSpeedInfoMapData()
.observe(this, this::onSpeedInfoMapDataChanged);
mWifiHotspotSpeedViewModel.getRestarting().observe(this, this::onRestartingChanged);
}
protected void loadPreferences() {
@@ -117,6 +120,12 @@ public class WifiHotspotSpeedSettings extends DashboardFragment implements
}
}
@VisibleForTesting
void onRestartingChanged(Boolean restarting) {
log("onRestartingChanged(), restarting:" + restarting);
setLoading(restarting, false);
}
@Override
public void onRadioButtonClicked(SelectorWithWidgetPreference emiter) {
String key = emiter.getKey();

View File

@@ -130,6 +130,13 @@ public class WifiHotspotSpeedViewModel extends AndroidViewModel {
}
}
/**
* Gets Restarting LiveData
*/
public LiveData<Boolean> getRestarting() {
return mWifiHotspotRepository.getRestarting();
}
/**
* Wi-Fi Hotspot Speed Information
*/

View File

@@ -17,6 +17,8 @@
package com.android.settings.wifi.tether;
import static android.net.wifi.WifiManager.WIFI_AP_STATE_CHANGED_ACTION;
import static android.view.View.INVISIBLE;
import static android.view.View.VISIBLE;
import static com.android.settings.wifi.WifiUtils.canShowWifiHotspot;
@@ -26,7 +28,6 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.UserManager;
import android.util.FeatureFlagUtils;
@@ -76,6 +77,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
@VisibleForTesting
static final String KEY_WIFI_HOTSPOT_SPEED = "wifi_hotspot_speed";
@VisibleForTesting
SettingsMainSwitchBar mMainSwitchBar;
private WifiTetherSwitchBarController mSwitchBarController;
private WifiTetherSSIDPreferenceController mSSIDPreferenceController;
private WifiTetherPasswordPreferenceController mPasswordPreferenceController;
@@ -83,8 +86,6 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
private WifiTetherMaximizeCompatibilityPreferenceController mMaxCompatibilityPrefController;
private WifiTetherAutoOffPreferenceController mWifiTetherAutoOffPreferenceController;
private WifiManager mWifiManager;
private boolean mRestartWifiApAfterConfigChange;
private boolean mUnavailable;
private WifiRestriction mWifiRestriction;
@VisibleForTesting
@@ -138,6 +139,7 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
.getWifiTetherViewModel(this);
if (mWifiTetherViewModel != null) {
setupSpeedFeature(mWifiTetherViewModel.isSpeedFeatureAvailable());
mWifiTetherViewModel.getRestarting().observe(this, this::onRestartingChanged);
}
}
@@ -159,7 +161,6 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
@Override
public void onAttach(Context context) {
super.onAttach(context);
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
mTetherChangeReceiver = new TetherChangeReceiver();
mSSIDPreferenceController = use(WifiTetherSSIDPreferenceController.class);
@@ -179,11 +180,11 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
// Assume we are in a SettingsActivity. This is only safe because we currently use
// SettingsActivity as base for all preference fragments.
final SettingsActivity activity = (SettingsActivity) getActivity();
final SettingsMainSwitchBar switchBar = activity.getSwitchBar();
switchBar.setTitle(getContext().getString(R.string.use_wifi_hotsopt_main_switch_title));
mSwitchBarController = new WifiTetherSwitchBarController(activity, switchBar);
mMainSwitchBar = activity.getSwitchBar();
mMainSwitchBar.setTitle(getString(R.string.use_wifi_hotsopt_main_switch_title));
mSwitchBarController = new WifiTetherSwitchBarController(activity, mMainSwitchBar);
getSettingsLifecycle().addObserver(mSwitchBarController);
switchBar.show();
mMainSwitchBar.show();
}
@Override
@@ -259,40 +260,34 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
final SoftApConfiguration config = buildNewConfig();
mPasswordPreferenceController.setSecurityType(config.getSecurityType());
/**
* if soft AP is stopped, bring up
* else restart with new config
* TODO: update config on a running access point when framework support is added
*/
if (mWifiManager.getWifiApState() == WifiManager.WIFI_AP_STATE_ENABLED) {
Log.d("TetheringSettings",
"Wifi AP config changed while enabled, stop and restart");
mRestartWifiApAfterConfigChange = true;
mSwitchBarController.stopTether();
}
mWifiTetherViewModel.setSoftApConfiguration(config);
}
@VisibleForTesting
void onRestartingChanged(Boolean restarting) {
mMainSwitchBar.setVisibility((restarting) ? INVISIBLE : VISIBLE);
setLoading(restarting, false);
}
private SoftApConfiguration buildNewConfig() {
final SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder();
final int securityType = mSecurityPreferenceController.getSecurityType();
SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder();
int securityType = (mWifiTetherViewModel.isSpeedFeatureAvailable())
? mWifiTetherViewModel.getSoftApConfiguration().getSecurityType()
: mSecurityPreferenceController.getSecurityType();
configBuilder.setSsid(mSSIDPreferenceController.getSSID());
if (securityType != SoftApConfiguration.SECURITY_TYPE_OPEN) {
configBuilder.setPassphrase(
mPasswordPreferenceController.getPasswordValidated(securityType),
securityType);
}
mMaxCompatibilityPrefController.setupMaximizeCompatibility(configBuilder);
if (!mWifiTetherViewModel.isSpeedFeatureAvailable()) {
mMaxCompatibilityPrefController.setupMaximizeCompatibility(configBuilder);
}
configBuilder.setAutoShutdownEnabled(
mWifiTetherAutoOffPreferenceController.isEnabled());
return configBuilder.build();
}
private void startTether() {
mRestartWifiApAfterConfigChange = false;
mSwitchBarController.startTether();
}
private void updateDisplayWithNewConfig() {
use(WifiTetherSSIDPreferenceController.class).updateDisplay();
use(WifiTetherSecurityPreferenceController.class).updateDisplay();
@@ -369,13 +364,6 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
String action = intent.getAction();
Log.d(TAG, "updating display config due to receiving broadcast action " + action);
updateDisplayWithNewConfig();
if (action.equals(WIFI_AP_STATE_CHANGED_ACTION)) {
int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_AP_STATE, 0);
if (state == WifiManager.WIFI_AP_STATE_DISABLED
&& mRestartWifiApAfterConfigChange) {
startTether();
}
}
}
}
}

View File

@@ -100,6 +100,15 @@ public class WifiTetherViewModel extends AndroidViewModel {
return mWifiHotspotRepository.isSpeedFeatureAvailable();
}
/**
* Gets the Wi-Fi tethered AP Configuration.
*
* @return AP details in {@link SoftApConfiguration}
*/
public SoftApConfiguration getSoftApConfiguration() {
return mWifiHotspotRepository.getSoftApConfiguration();
}
/**
* Sets the tethered Wi-Fi AP Configuration.
*
@@ -153,4 +162,11 @@ public class WifiTetherViewModel extends AndroidViewModel {
}
mSpeedSummary.setValue(resId);
}
/**
* Gets Restarting LiveData
*/
public LiveData<Boolean> getRestarting() {
return mWifiHotspotRepository.getRestarting();
}
}