Make user explicitly set security type for tether network

This was made implicit which is confusing for the user so this CL
adds a preference to make the user manually choose their security
type.

Test: robotests
Bug: 79435112
Change-Id: Ie78806e8952b52e1b7cd21f0b87c9d064acaff64
This commit is contained in:
Salvador Martinez
2018-05-15 17:44:42 -07:00
parent 7c0c7a0626
commit f3d532f43d
9 changed files with 242 additions and 64 deletions

View File

@@ -50,7 +50,7 @@ public class WifiUtils {
public static boolean isHotspotPasswordValid(String password) {
if (TextUtils.isEmpty(password)) {
return true;
return false;
}
final int length = password.length();

View File

@@ -31,7 +31,6 @@ import java.util.UUID;
public class WifiTetherPasswordPreferenceController extends WifiTetherBasePreferenceController
implements ValidatedEditTextPreference.Validator {
private static final String TAG = "WifiTetherPswdPref";
private static final String PREF_KEY = "wifi_tether_network_password";
private String mPassword;
@@ -49,10 +48,11 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
@Override
public void updateDisplay() {
final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
if (config != null) {
mPassword = config.preSharedKey;
} else {
if (config == null || (config.getAuthType() == WifiConfiguration.KeyMgmt.WPA2_PSK
&& TextUtils.isEmpty(config.preSharedKey))) {
mPassword = generateRandomPassword();
} else {
mPassword = config.preSharedKey;
}
((ValidatedEditTextPreference) mPreference).setValidator(this);
((ValidatedEditTextPreference) mPreference).setIsPassword(true);
@@ -68,17 +68,27 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
return true;
}
public String getPassword() {
/**
* This method returns the current password if it is valid for the indicated security type. If
* the password currently set is invalid it will forcefully set a random password that is valid.
*
* @param securityType The security type for the password.
* @return The current password if it is valid for the indicated security type. A new randomly
* generated password if it is not.
*/
public String getPasswordValidated(int securityType) {
// don't actually overwrite unless we get a new config in case it was accidentally toggled.
if (securityType == WifiConfiguration.KeyMgmt.NONE) {
return "";
} else if (!isTextValid(mPassword)) {
mPassword = generateRandomPassword();
updatePasswordDisplay((EditTextPreference) mPreference);
}
return mPassword;
}
public int getSecuritySettingForPassword() {
// We should return NONE when no password is set
if (TextUtils.isEmpty(mPassword)) {
return WifiConfiguration.KeyMgmt.NONE;
}
// Only other currently supported type is WPA2 so we'll try that
return WifiConfiguration.KeyMgmt.WPA2_PSK;
public void updateVisibility(int securityType) {
mPreference.setVisible(securityType != WifiConfiguration.KeyMgmt.NONE);
}
@Override
@@ -98,9 +108,11 @@ public class WifiTetherPasswordPreferenceController extends WifiTetherBasePrefer
if (!TextUtils.isEmpty(mPassword)) {
pref.setIsSummaryPassword(true);
pref.setSummary(mPassword);
pref.setVisible(true);
} else {
pref.setIsSummaryPassword(false);
pref.setSummary(R.string.wifi_hotspot_no_password_subtext);
pref.setVisible(false);
}
}
}

View File

@@ -0,0 +1,63 @@
package com.android.settings.wifi.tether;
import android.content.Context;
import android.content.res.Resources;
import android.net.wifi.WifiConfiguration;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import com.android.settings.R;
public class WifiTetherSecurityPreferenceController extends WifiTetherBasePreferenceController {
private static final String PREF_KEY = "wifi_tether_security";
private final String[] mSecurityEntries;
private int mSecurityValue;
public WifiTetherSecurityPreferenceController(Context context,
OnTetherConfigUpdateListener listener) {
super(context, listener);
mSecurityEntries = mContext.getResources().getStringArray(R.array.wifi_tether_security);
}
@Override
public String getPreferenceKey() {
return PREF_KEY;
}
@Override
public void updateDisplay() {
final WifiConfiguration config = mWifiManager.getWifiApConfiguration();
if (config != null && config.getAuthType() == WifiConfiguration.KeyMgmt.NONE) {
mSecurityValue = WifiConfiguration.KeyMgmt.NONE;
} else {
mSecurityValue = WifiConfiguration.KeyMgmt.WPA2_PSK;
}
final ListPreference preference = (ListPreference) mPreference;
preference.setSummary(getSummaryForSecurityType(mSecurityValue));
preference.setValue(String.valueOf(mSecurityValue));
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
mSecurityValue = Integer.parseInt((String) newValue);
preference.setSummary(getSummaryForSecurityType(mSecurityValue));
mListener.onTetherConfigUpdated();
return true;
}
public int getSecurityType() {
return mSecurityValue;
}
private String getSummaryForSecurityType(int securityType) {
if (securityType == WifiConfiguration.KeyMgmt.NONE) {
return mSecurityEntries[1];
}
// WPA2 PSK
return mSecurityEntries[0];
}
}

View File

@@ -46,7 +46,6 @@ import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
@SearchIndexable
public class WifiTetherSettings extends RestrictedDashboardFragment
@@ -68,6 +67,7 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
private WifiTetherSSIDPreferenceController mSSIDPreferenceController;
private WifiTetherPasswordPreferenceController mPasswordPreferenceController;
private WifiTetherApBandPreferenceController mApBandPreferenceController;
private WifiTetherSecurityPreferenceController mSecurityPreferenceController;
private WifiManager mWifiManager;
private boolean mRestartWifiApAfterConfigChange;
@@ -142,10 +142,12 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
mSSIDPreferenceController = new WifiTetherSSIDPreferenceController(context, this);
mSecurityPreferenceController = new WifiTetherSecurityPreferenceController(context, this);
mPasswordPreferenceController = new WifiTetherPasswordPreferenceController(context, this);
mApBandPreferenceController = new WifiTetherApBandPreferenceController(context, this);
controllers.add(mSSIDPreferenceController);
controllers.add(mSecurityPreferenceController);
controllers.add(mPasswordPreferenceController);
controllers.add(mApBandPreferenceController);
controllers.add(
@@ -156,6 +158,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
@Override
public void onTetherConfigUpdated() {
final WifiConfiguration config = buildNewConfig();
mPasswordPreferenceController.updateVisibility(config.getAuthType());
/**
* if soft AP is stopped, bring up
* else restart with new config
@@ -172,11 +176,11 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
private WifiConfiguration buildNewConfig() {
final WifiConfiguration config = new WifiConfiguration();
final int securityType = mSecurityPreferenceController.getSecurityType();
config.SSID = mSSIDPreferenceController.getSSID();
config.preSharedKey = mPasswordPreferenceController.getPassword();
config.allowedKeyManagement.set(
mPasswordPreferenceController.getSecuritySettingForPassword());
config.allowedKeyManagement.set(securityType);
config.preSharedKey = mPasswordPreferenceController.getPasswordValidated(securityType);
config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
config.apBand = mApBandPreferenceController.getBandIndex();
return config;
@@ -190,6 +194,8 @@ public class WifiTetherSettings extends RestrictedDashboardFragment
private void updateDisplayWithNewConfig() {
use(WifiTetherSSIDPreferenceController.class)
.updateDisplay();
use(WifiTetherSecurityPreferenceController.class)
.updateDisplay();
use(WifiTetherPasswordPreferenceController.class)
.updateDisplay();
use(WifiTetherApBandPreferenceController.class)