Add toggle for Wi-Fi hotspot

- Add Wi-Fi hotspot toggle with vertical divider

- Disable preference and toggle when Data Saver is enabled

Bug: 245569117
Test: manual test
make RunSettingsRoboTests ROBOTEST_FILTER=TetherSettingsTest
make RunSettingsRoboTests ROBOTEST_FILTER=WifiTetherPreferenceControllerTest
atest -c TetheringManagerModelTest

Change-Id: Ic2baf7d3d0a7bf9527da38d24ecc511b7c91265a
This commit is contained in:
Weng Su
2023-01-13 07:52:23 +08:00
parent a65999cc60
commit f779e6c9d3
7 changed files with 276 additions and 71 deletions

View File

@@ -16,6 +16,9 @@
package com.android.settings.wifi.tether;
import static android.net.TetheringManager.TETHERING_WIFI;
import static android.net.wifi.WifiManager.SAP_START_FAILURE_GENERAL;
import static com.android.settings.wifi.WifiUtils.canShowWifiHotspot;
import android.annotation.NonNull;
@@ -26,12 +29,15 @@ import android.net.wifi.WifiManager;
import android.text.BidiFormatter;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceScreen;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.network.tether.TetheringManagerModel;
import com.android.settings.widget.GenericSwitchController;
import com.android.settings.widget.SwitchWidgetController;
import com.android.settingslib.PrimarySwitchPreference;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver;
@@ -43,7 +49,8 @@ import com.android.settingslib.wifi.WifiUtils;
import java.util.List;
public class WifiTetherPreferenceController extends AbstractPreferenceController
implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop {
implements PreferenceControllerMixin, LifecycleObserver, OnStart, OnStop,
SwitchWidgetController.OnSwitchChangeListener {
private static final String WIFI_TETHER_SETTINGS = "wifi_tether";
@@ -51,16 +58,24 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
private boolean mIsWifiTetheringAllow;
private int mSoftApState;
@VisibleForTesting
Preference mPreference;
PrimarySwitchPreference mPreference;
@VisibleForTesting
WifiTetherSoftApManager mWifiTetherSoftApManager;
@VisibleForTesting
TetheringManagerModel mTetheringManagerModel;
@VisibleForTesting
boolean mIsDataSaverEnabled;
@VisibleForTesting
SwitchWidgetController mSwitch;
public WifiTetherPreferenceController(Context context, Lifecycle lifecycle) {
public WifiTetherPreferenceController(Context context, Lifecycle lifecycle,
TetheringManagerModel tetheringManagerModel) {
// TODO(b/246537032):Use fragment context to WifiManager service will caused memory leak
this(context, lifecycle,
context.getApplicationContext().getSystemService(WifiManager.class),
true /* initSoftApManager */,
WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(context));
WifiEnterpriseRestrictionUtils.isWifiTetheringAllowed(context),
tetheringManagerModel);
}
@VisibleForTesting
@@ -69,11 +84,13 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
Lifecycle lifecycle,
WifiManager wifiManager,
boolean initSoftApManager,
boolean isWifiTetheringAllow) {
boolean isWifiTetheringAllow,
TetheringManagerModel tetheringManagerModel) {
super(context);
mIsWifiTetheringAllow = isWifiTetheringAllow;
if (!isWifiTetheringAllow) return;
mTetheringManagerModel = tetheringManagerModel;
mWifiManager = wifiManager;
if (lifecycle != null) {
@@ -97,8 +114,13 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
// unavailable
return;
}
if (!mIsWifiTetheringAllow && mPreference.isEnabled()) {
mPreference.setEnabled(false);
if (mSwitch == null) {
mSwitch = new GenericSwitchController(mPreference);
mSwitch.setListener(this);
updateSwitch();
}
mPreference.setEnabled(canEnabled());
if (!mIsWifiTetheringAllow) {
mPreference.setSummary(R.string.not_allowed_by_ent);
}
}
@@ -114,6 +136,9 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
if (mWifiTetherSoftApManager != null) {
mWifiTetherSoftApManager.registerSoftApCallback();
}
if (mSwitch != null) {
mSwitch.startListening();
}
}
}
@@ -123,6 +148,9 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
if (mWifiTetherSoftApManager != null) {
mWifiTetherSoftApManager.unRegisterSoftApCallback();
}
if (mSwitch != null) {
mSwitch.stopListening();
}
}
}
@@ -158,6 +186,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
mPreference.setSummary(R.string.wifi_tether_starting);
break;
case WifiManager.WIFI_AP_STATE_ENABLED:
mSwitch.setChecked(true);
final SoftApConfiguration softApConfig = mWifiManager.getSoftApConfiguration();
updateConfigSummary(softApConfig);
break;
@@ -165,6 +194,7 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
mPreference.setSummary(R.string.wifi_tether_stopping);
break;
case WifiManager.WIFI_AP_STATE_DISABLED:
mSwitch.setChecked(false);
mPreference.setSummary(R.string.wifi_hotspot_off_subtext);
break;
default:
@@ -184,4 +214,40 @@ public class WifiTetherPreferenceController extends AbstractPreferenceController
mPreference.setSummary(mContext.getString(R.string.wifi_tether_enabled_subtext,
BidiFormatter.getInstance().unicodeWrap(softApConfig.getSsid())));
}
/**
* Sets the Data Saver state for preference update.
*/
public void setDataSaverEnabled(boolean enabled) {
mIsDataSaverEnabled = enabled;
if (mPreference != null) {
mPreference.setEnabled(canEnabled());
}
if (mSwitch != null) {
mSwitch.setEnabled(canEnabled());
}
}
private boolean canEnabled() {
return mIsWifiTetheringAllow && !mIsDataSaverEnabled;
}
@VisibleForTesting
protected void updateSwitch() {
if (mWifiManager == null) return;
int wifiApState = mWifiManager.getWifiApState();
mSwitch.setEnabled(canEnabled());
mSwitch.setChecked(wifiApState == WifiManager.WIFI_AP_STATE_ENABLED);
handleWifiApStateChanged(wifiApState, SAP_START_FAILURE_GENERAL);
}
@Override
public boolean onSwitchToggled(boolean isChecked) {
if (isChecked) {
mTetheringManagerModel.startTethering(TETHERING_WIFI);
} else {
mTetheringManagerModel.stopTethering(TETHERING_WIFI);
}
return true;
}
}