Merge "Add toggle for Wi-Fi hotspot"

This commit is contained in:
TreeHugger Robot
2023-01-13 06:25:47 +00:00
committed by Android (Google) Code Review
7 changed files with 276 additions and 71 deletions

View File

@@ -96,11 +96,12 @@ public class TetherSettings extends RestrictedSettingsFragment
private static final String TAG = "TetheringSettings";
private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
private RestrictedSwitchPreference mUsbTether;
private SwitchPreference mBluetoothTether;
private SwitchPreference mEthernetTether;
@VisibleForTesting
RestrictedSwitchPreference mUsbTether;
@VisibleForTesting
SwitchPreference mBluetoothTether;
@VisibleForTesting
SwitchPreference mEthernetTether;
private BroadcastReceiver mTetherChangeReceiver;
private BroadcastReceiver mBluetoothStateReceiver;
@@ -115,7 +116,8 @@ public class TetherSettings extends RestrictedSettingsFragment
private EthernetListener mEthernetListener;
private final HashSet<String> mAvailableInterfaces = new HashSet<>();
private WifiTetherPreferenceController mWifiTetherPreferenceController;
@VisibleForTesting
WifiTetherPreferenceController mWifiTetherPreferenceController;
private boolean mUsbConnected;
private boolean mMassStorageActive;
@@ -125,7 +127,8 @@ public class TetherSettings extends RestrictedSettingsFragment
private DataSaverBackend mDataSaverBackend;
private boolean mDataSaverEnabled;
private Preference mDataSaverFooter;
@VisibleForTesting
Preference mDataSaverFooter;
@VisibleForTesting
String[] mUsbRegexs;
@@ -146,10 +149,10 @@ public class TetherSettings extends RestrictedSettingsFragment
@Override
public void onAttach(Context context) {
super.onAttach(context);
mWifiTetherPreferenceController =
new WifiTetherPreferenceController(context, getSettingsLifecycle());
TetheringManagerModel model = new ViewModelProvider(this).get(TetheringManagerModel.class);
mTm = model.mTetheringManager;
mWifiTetherPreferenceController =
new WifiTetherPreferenceController(context, getSettingsLifecycle(), model);
mTm = model.getTetheringManager();
model.getTetheredInterfaces().observe(this, this::onTetheredInterfacesChanged);
}
@@ -248,6 +251,7 @@ public class TetherSettings extends RestrictedSettingsFragment
@Override
public void onDataSaverChanged(boolean isDataSaving) {
mDataSaverEnabled = isDataSaving;
mWifiTetherPreferenceController.setDataSaverEnabled(mDataSaverEnabled);
mUsbTether.setEnabled(!mDataSaverEnabled);
mBluetoothTether.setEnabled(!mDataSaverEnabled);
mEthernetTether.setEnabled(!mDataSaverEnabled);

View File

@@ -34,6 +34,7 @@ public class TetheringManagerModel extends AndroidViewModel {
protected TetheringManager mTetheringManager;
protected EventCallback mEventCallback = new EventCallback();
protected MutableLiveData<List<String>> mTetheredInterfaces = new MutableLiveData<>();
protected StartTetheringCallback mStartTetheringCallback = new StartTetheringCallback();
public TetheringManagerModel(@NonNull Application application) {
super(application);
@@ -62,6 +63,27 @@ public class TetheringManagerModel extends AndroidViewModel {
return Transformations.distinctUntilChanged(mTetheredInterfaces);
}
/**
* Starts tethering and runs tether provisioning for the given type if needed. If provisioning
* fails, stopTethering will be called automatically.
*
* @param type The tethering type, on of the {@code TetheringManager#TETHERING_*} constants.
*/
public void startTethering(int type) {
mTetheringManager.startTethering(type, getApplication().getMainExecutor(),
mStartTetheringCallback);
}
/**
* Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
* applicable.
*
* @param type The tethering type, on of the {@code TetheringManager#TETHERING_*} constants.
*/
public void stopTethering(int type) {
mTetheringManager.stopTethering(type);
}
/**
* Callback for use with {@link TetheringManager#registerTetheringEventCallback} to find out
* tethering upstream status.
@@ -72,4 +94,16 @@ public class TetheringManagerModel extends AndroidViewModel {
mTetheredInterfaces.setValue(interfaces);
}
}
private class StartTetheringCallback implements TetheringManager.StartTetheringCallback {
@Override
public void onTetheringStarted() {
// Do nothing
}
@Override
public void onTetheringFailed(int error) {
// Do nothing
}
}
}

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;
}
}