Add Wi-Fi hotspot > Security Settings page

- Restrict low security type when 6 GHz band is selected
  - Disable "WPA2/WPA3-Personal" security type
  - Disable "WPA2-Personal" security type
  - Disable "None" security type

- Automatically updated security type to WPA3 when 6 GHz band is selected
  - Regenerate password when security type is changed from None

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

Change-Id: I31b08795419baed10dc40b876aeec175f6f41e69
This commit is contained in:
Weng Su
2023-04-10 23:31:50 +08:00
parent 211b544ea8
commit 9f80cd2f77
14 changed files with 1019 additions and 28 deletions

View File

@@ -26,6 +26,7 @@ import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStoreOwner;
import com.android.settings.wifi.repository.WifiHotspotRepository;
import com.android.settings.wifi.tether.WifiHotspotSecurityViewModel;
import com.android.settings.wifi.tether.WifiHotspotSpeedViewModel;
import com.android.settings.wifi.tether.WifiTetherViewModel;
@@ -84,6 +85,17 @@ public class WifiFeatureProvider {
return new ViewModelProvider(owner).get(WifiTetherViewModel.class);
}
/**
* Get WifiHotspotSecurityViewModel
*/
public WifiHotspotSecurityViewModel getWifiHotspotSecurityViewModel(
@NotNull ViewModelStoreOwner owner) {
WifiHotspotSecurityViewModel viewModel =
new ViewModelProvider(owner).get(WifiHotspotSecurityViewModel.class);
verboseLog(TAG, "getWifiHotspotSecurityViewModel():" + viewModel);
return viewModel;
}
/**
* Get WifiHotspotSpeedViewModel
*/

View File

@@ -19,6 +19,8 @@ package com.android.settings.wifi.repository;
import static android.net.wifi.SoftApConfiguration.BAND_2GHZ;
import static android.net.wifi.SoftApConfiguration.BAND_5GHZ;
import static android.net.wifi.SoftApConfiguration.BAND_6GHZ;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_OPEN;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA3_SAE;
import static android.net.wifi.WifiAvailableChannel.OP_MODE_SAP;
import android.content.Context;
@@ -81,6 +83,7 @@ public class WifiHotspotRepository {
protected String mLastPassword;
protected LastPasswordListener mLastPasswordListener = new LastPasswordListener();
protected MutableLiveData<Integer> mSecurityType;
protected MutableLiveData<Integer> mSpeedType;
protected Boolean mIsDualBand;
@@ -144,6 +147,7 @@ public class WifiHotspotRepository {
* Refresh data from the SoftApConfiguration.
*/
public void refresh() {
updateSecurityType();
update6gAvailable();
update5gAvailable();
updateSpeedType();
@@ -162,11 +166,71 @@ public class WifiHotspotRepository {
}
}
/**
* Gets SecurityType LiveData
*/
public LiveData<Integer> getSecurityType() {
if (mSecurityType == null) {
startAutoRefresh();
mSecurityType = new MutableLiveData<>();
updateSecurityType();
log("getSecurityType():" + mSecurityType.getValue());
}
return mSecurityType;
}
protected void updateSecurityType() {
if (mSecurityType == null) {
return;
}
SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
int securityType = (config != null) ? config.getSecurityType() : SECURITY_TYPE_OPEN;
log("updateSecurityType(), securityType:" + securityType);
mSecurityType.setValue(securityType);
}
/**
* Sets SecurityType
*
* @param securityType the Wi-Fi hotspot security type.
*/
public void setSecurityType(int securityType) {
log("setSecurityType():" + securityType);
if (mSecurityType == null) {
getSecurityType();
}
if (securityType == mSecurityType.getValue()) {
Log.w(TAG, "setSecurityType() is no changed! mSecurityType:"
+ mSecurityType.getValue());
return;
}
SoftApConfiguration config = mWifiManager.getSoftApConfiguration();
if (config == null) {
mSecurityType.setValue(SECURITY_TYPE_OPEN);
Log.e(TAG, "setSecurityType(), WifiManager#getSoftApConfiguration() return null!");
return;
}
SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder(config);
String passphrase = null;
if (securityType != SECURITY_TYPE_OPEN) {
passphrase = config.getPassphrase();
if (TextUtils.isEmpty(passphrase)) {
passphrase = generatePassword();
}
}
configBuilder.setPassphrase(passphrase, securityType);
setSoftApConfiguration(configBuilder.build());
mWifiManager.queryLastConfiguredTetheredApPassphraseSinceBoot(
mAppContext.getMainExecutor(), mLastPasswordListener);
}
/**
* Gets SpeedType LiveData
*/
public LiveData<Integer> getSpeedType() {
if (mSpeedType == null) {
startAutoRefresh();
mSpeedType = new MutableLiveData<>();
updateSpeedType();
log("getSpeedType():" + mSpeedType.getValue());
@@ -230,6 +294,10 @@ public class WifiHotspotRepository {
if (speedType == SPEED_6GHZ) {
log("setSpeedType(), setBand(BAND_2GHZ_5GHZ_6GHZ)");
configBuilder.setBand(BAND_2GHZ_5GHZ_6GHZ);
if (config.getSecurityType() != SECURITY_TYPE_WPA3_SAE) {
log("setSpeedType(), setPassphrase(SECURITY_TYPE_WPA3_SAE)");
configBuilder.setPassphrase(generatePassword(), SECURITY_TYPE_WPA3_SAE);
}
} else if (speedType == SPEED_5GHZ) {
log("setSpeedType(), setBand(BAND_2GHZ_5GHZ)");
configBuilder.setBand(BAND_2GHZ_5GHZ);

View File

@@ -0,0 +1,112 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.wifi.tether;
import android.app.settings.SettingsEnums;
import android.os.Bundle;
import androidx.lifecycle.LiveData;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.overlay.FeatureFactory;
import com.android.settingslib.widget.SelectorWithWidgetPreference;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Wi-Fi Hotspot Security Settings
*/
public class WifiHotspotSecuritySettings extends DashboardFragment implements
SelectorWithWidgetPreference.OnClickListener {
private static final String TAG = "WifiHotspotSecuritySettings";
protected WifiHotspotSecurityViewModel mWifiHotspotSecurityViewModel;
protected Map<Integer, SelectorWithWidgetPreference> mPreferenceMap = new HashMap<>();
@Override
public int getMetricsCategory() {
return SettingsEnums.WIFI_TETHER_SETTINGS;
}
@Override
protected int getPreferenceScreenResId() {
return R.xml.wifi_hotspot_security;
}
@Override
protected String getLogTag() {
return TAG;
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
loadViewModel();
}
protected void loadViewModel() {
mWifiHotspotSecurityViewModel = FeatureFactory.getFactory(getContext())
.getWifiFeatureProvider().getWifiHotspotSecurityViewModel(this);
LiveData<List<WifiHotspotSecurityViewModel.ViewItem>> viewItemListData =
mWifiHotspotSecurityViewModel.getViewItemListData();
viewItemListData.observe(this, this::onViewItemListDataChanged);
// set the onRadioButtonClicked callback to related preference
for (WifiHotspotSecurityViewModel.ViewItem viewItem : viewItemListData.getValue()) {
SelectorWithWidgetPreference preference = findPreference(viewItem.mKey);
preference.setOnClickListener(this);
}
}
protected void onViewItemListDataChanged(
List<WifiHotspotSecurityViewModel.ViewItem> viewItems) {
log("onViewItemListDataChanged(), viewItems:" + viewItems);
for (WifiHotspotSecurityViewModel.ViewItem viewItem : viewItems) {
SelectorWithWidgetPreference preference = findPreference(viewItem.mKey);
if (preference == null) {
continue;
}
if (preference.isChecked() != viewItem.mIsChecked) {
preference.setChecked(viewItem.mIsChecked);
}
if (preference.isEnabled() != viewItem.mIsEnabled) {
preference.setEnabled(viewItem.mIsEnabled);
if (viewItem.mIsEnabled) {
preference.setSummary(null);
} else {
preference.setSummary(R.string.wifi_hotspot_security_summary_unavailable);
}
}
}
}
@Override
public void onRadioButtonClicked(SelectorWithWidgetPreference emiter) {
String key = emiter.getKey();
log("onRadioButtonClicked(), key:" + key);
if (key.isEmpty()) {
return;
}
mWifiHotspotSecurityViewModel.handleRadioButtonClicked(key);
}
private void log(String msg) {
FeatureFactory.getFactory(getContext()).getWifiFeatureProvider().verboseLog(TAG, msg);
}
}

View File

@@ -0,0 +1,157 @@
/*
* Copyright (C) 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.wifi.tether;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_OPEN;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA2_PSK;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA3_SAE;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION;
import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_6GHZ;
import android.app.Application;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import com.android.settings.overlay.FeatureFactory;
import com.android.settings.wifi.repository.WifiHotspotRepository;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Wi-Fi Hotspot Security View Model for {@link WifiHotspotSecuritySettings}
*/
public class WifiHotspotSecurityViewModel extends AndroidViewModel {
private static final String TAG = "WifiHotspotSecurityViewModel";
public static final String KEY_SECURITY_WPA3 = "wifi_hotspot_security_wpa3";
public static final String KEY_SECURITY_WPA2_WPA3 = "wifi_hotspot_security_wpa2_wpa3";
public static final String KEY_SECURITY_WPA2 = "wifi_hotspot_security_wpa2";
public static final String KEY_SECURITY_NONE = "wifi_hotspot_security_none";
protected Map<Integer, ViewItem> mViewItemMap = new HashMap<>();
protected MutableLiveData<List<ViewItem>> mViewInfoListData;
protected final WifiHotspotRepository mWifiHotspotRepository;
protected final Observer<Integer> mSecurityTypeObserver = st -> onSecurityTypeChanged(st);
protected final Observer<Integer> mSpeedTypeObserver = st -> onSpeedTypeChanged(st);
public WifiHotspotSecurityViewModel(
@NotNull Application application) {
super(application);
mViewItemMap.put(SECURITY_TYPE_WPA3_SAE, new ViewItem(KEY_SECURITY_WPA3));
mViewItemMap.put(SECURITY_TYPE_WPA3_SAE_TRANSITION, new ViewItem(KEY_SECURITY_WPA2_WPA3));
mViewItemMap.put(SECURITY_TYPE_WPA2_PSK, new ViewItem(KEY_SECURITY_WPA2));
mViewItemMap.put(SECURITY_TYPE_OPEN, new ViewItem(KEY_SECURITY_NONE));
mWifiHotspotRepository = FeatureFactory.getFactory(application).getWifiFeatureProvider()
.getWifiHotspotRepository();
mWifiHotspotRepository.getSecurityType().observeForever(mSecurityTypeObserver);
mWifiHotspotRepository.getSpeedType().observeForever(mSpeedTypeObserver);
}
@Override
protected void onCleared() {
mWifiHotspotRepository.getSecurityType().removeObserver(mSecurityTypeObserver);
mWifiHotspotRepository.getSpeedType().removeObserver(mSpeedTypeObserver);
}
protected void onSecurityTypeChanged(int securityType) {
log("onSecurityTypeChanged(), securityType:" + securityType);
for (Map.Entry<Integer, ViewItem> entry : mViewItemMap.entrySet()) {
entry.getValue().mIsChecked = entry.getKey().equals(securityType);
}
updateViewItemListData();
}
protected void onSpeedTypeChanged(Integer speedType) {
log("onSpeedTypeChanged(), speedType:" + speedType);
boolean isWpa3Only = (speedType == SPEED_6GHZ);
for (Map.Entry<Integer, ViewItem> entry : mViewItemMap.entrySet()) {
if (entry.getKey() != SECURITY_TYPE_WPA3_SAE) {
entry.getValue().mIsEnabled = !isWpa3Only;
}
}
updateViewItemListData();
}
/**
* Handle RadioButton Clicked
*/
public void handleRadioButtonClicked(String key) {
log("handleRadioButtonClicked(), key:" + key);
for (Map.Entry<Integer, ViewItem> entry : mViewItemMap.entrySet()) {
ViewItem viewItem = entry.getValue();
if (viewItem.mKey.equals(key)) {
mWifiHotspotRepository.setSecurityType(entry.getKey());
return;
}
}
}
/**
* Gets ViewItemList LiveData
*/
public LiveData<List<ViewItem>> getViewItemListData() {
if (mViewInfoListData == null) {
mViewInfoListData = new MutableLiveData<>();
updateViewItemListData();
log("getViewItemListData(), mViewInfoListData:" + mViewInfoListData.getValue());
}
return mViewInfoListData;
}
protected void updateViewItemListData() {
if (mViewInfoListData == null) {
return;
}
mViewInfoListData.setValue(mViewItemMap.values().stream().toList());
}
/**
* Wi-Fi Hotspot View Item
*/
public static final class ViewItem {
String mKey;
boolean mIsChecked;
boolean mIsEnabled = true;
public ViewItem(String key) {
mKey = key;
}
@Override
public String toString() {
return new StringBuilder("ViewItem:{")
.append("Key:").append(mKey)
.append(",IsChecked:").append(mIsChecked)
.append(",IsEnabled:").append(mIsEnabled)
.append('}').toString();
}
}
private void log(String msg) {
FeatureFactory.getFactory(getApplication()).getWifiFeatureProvider().verboseLog(TAG, msg);
}
}

View File

@@ -72,6 +72,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
static final String KEY_WIFI_TETHER_MAXIMIZE_COMPATIBILITY =
WifiTetherMaximizeCompatibilityPreferenceController.PREF_KEY;
@VisibleForTesting
static final String KEY_WIFI_HOTSPOT_SECURITY = "wifi_hotspot_security";
@VisibleForTesting
static final String KEY_WIFI_HOTSPOT_SPEED = "wifi_hotspot_speed";
private WifiTetherSwitchBarController mSwitchBarController;
@@ -91,6 +93,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
@VisibleForTesting
WifiTetherViewModel mWifiTetherViewModel;
@VisibleForTesting
Preference mWifiHotspotSecurity;
@VisibleForTesting
Preference mWifiHotspotSpeed;
static {
@@ -132,6 +136,10 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
mWifiTetherViewModel = FeatureFactory.getFactory(getContext()).getWifiFeatureProvider()
.getWifiTetherViewModel(this);
mWifiHotspotSecurity = findPreference(KEY_WIFI_HOTSPOT_SECURITY);
if (mWifiHotspotSecurity != null && mWifiHotspotSecurity.isVisible()) {
mWifiTetherViewModel.getSecuritySummary().observe(this, this::onSecuritySummaryChanged);
}
mWifiHotspotSpeed = findPreference(KEY_WIFI_HOTSPOT_SPEED);
if (mWifiHotspotSpeed != null && mWifiHotspotSpeed.isVisible()) {
mWifiTetherViewModel.getSpeedSummary().observe(this, this::onSpeedSummaryChanged);
@@ -206,6 +214,10 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
}
}
protected void onSecuritySummaryChanged(Integer securityResId) {
mWifiHotspotSecurity.setSummary(securityResId);
}
protected void onSpeedSummaryChanged(Integer summaryResId) {
mWifiHotspotSpeed.setSummary(summaryResId);
}

View File

@@ -16,11 +16,15 @@
package com.android.settings.wifi.tether;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_OPEN;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA2_PSK;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA3_SAE;
import static android.net.wifi.SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION;
import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_2GHZ;
import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_2GHZ_5GHZ;
import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_5GHZ;
import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_6GHZ;
import static com.android.settings.wifi.repository.WifiHotspotRepository.SPEED_UNKNOWN;
import android.app.Application;
import android.net.wifi.SoftApConfiguration;
@@ -28,7 +32,7 @@ import android.net.wifi.SoftApConfiguration;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Transformations;
import androidx.lifecycle.Observer;
import com.android.settings.R;
import com.android.settings.overlay.FeatureFactory;
@@ -45,9 +49,19 @@ import java.util.Map;
public class WifiTetherViewModel extends AndroidViewModel {
private static final String TAG = "WifiTetherViewModel";
protected static Map<Integer, Integer> sSpeedSummaryResMap = new HashMap<>();
static Map<Integer, Integer> sSecuritySummaryResMap = new HashMap<>();
static {
sSecuritySummaryResMap.put(SECURITY_TYPE_WPA3_SAE, R.string.wifi_security_sae);
sSecuritySummaryResMap.put(SECURITY_TYPE_WPA3_SAE_TRANSITION,
R.string.wifi_security_psk_sae);
sSecuritySummaryResMap.put(SECURITY_TYPE_WPA2_PSK, R.string.wifi_security_wpa2);
sSecuritySummaryResMap.put(SECURITY_TYPE_OPEN, R.string.wifi_security_none);
}
static Map<Integer, Integer> sSpeedSummaryResMap = new HashMap<>();
static {
sSpeedSummaryResMap.put(SPEED_UNKNOWN, R.string.summary_placeholder);
sSpeedSummaryResMap.put(SPEED_2GHZ, R.string.wifi_hotspot_speed_summary_2g);
sSpeedSummaryResMap.put(SPEED_5GHZ, R.string.wifi_hotspot_speed_summary_5g);
sSpeedSummaryResMap.put(SPEED_6GHZ, R.string.wifi_hotspot_speed_summary_6g);
@@ -55,18 +69,22 @@ public class WifiTetherViewModel extends AndroidViewModel {
}
protected final WifiHotspotRepository mWifiHotspotRepository;
protected MutableLiveData<Integer> mSecuritySummary;
protected MutableLiveData<Integer> mSpeedSummary;
protected final Observer<Integer> mSecurityTypeObserver = st -> onSecurityTypeChanged(st);
protected final Observer<Integer> mSpeedTypeObserver = st -> onSpeedTypeChanged(st);
public WifiTetherViewModel(@NotNull Application application) {
super(application);
mWifiHotspotRepository = FeatureFactory.getFactory(application).getWifiFeatureProvider()
.getWifiHotspotRepository();
mWifiHotspotRepository.setAutoRefresh(true);
}
@Override
protected void onCleared() {
mWifiHotspotRepository.setAutoRefresh(false);
mWifiHotspotRepository.getSecurityType().removeObserver(mSecurityTypeObserver);
mWifiHotspotRepository.getSpeedType().removeObserver(mSpeedTypeObserver);
}
/**
@@ -85,18 +103,41 @@ public class WifiTetherViewModel extends AndroidViewModel {
mWifiHotspotRepository.refresh();
}
/**
* Gets SecuritySummary LiveData
*/
public LiveData<Integer> getSecuritySummary() {
if (mSecuritySummary == null) {
mSecuritySummary = new MutableLiveData<>();
mWifiHotspotRepository.getSecurityType().observeForever(mSecurityTypeObserver);
}
return mSecuritySummary;
}
protected void onSecurityTypeChanged(int securityType) {
int resId = R.string.summary_placeholder;
if (sSecuritySummaryResMap.containsKey(securityType)) {
resId = sSecuritySummaryResMap.get(securityType);
}
mSecuritySummary.setValue(resId);
}
/**
* Gets SpeedSummary LiveData
*/
public LiveData<Integer> getSpeedSummary() {
if (mSpeedSummary == null) {
mSpeedSummary = new MutableLiveData<>();
mWifiHotspotRepository.getSpeedType().observeForever(this::onSpeedTypeChanged);
mWifiHotspotRepository.getSpeedType().observeForever(mSpeedTypeObserver);
}
return Transformations.distinctUntilChanged(mSpeedSummary);
return mSpeedSummary;
}
protected void onSpeedTypeChanged(Integer speedType) {
mSpeedSummary.setValue(sSpeedSummaryResMap.get(speedType));
int resId = R.string.summary_placeholder;
if (sSpeedSummaryResMap.containsKey(speedType)) {
resId = sSpeedSummaryResMap.get(speedType);
}
mSpeedSummary.setValue(resId);
}
}