[Wi-Fi] Fix crash after editing invalid hotspot password

From Android 11, Wi-Fi framework only supports ASCII encoding
for hotspot password, APP should check if password character is
valid.

This change checks if SoftApConfiguration accepts the password
and disable the OK button if the password is invalid.

Bug: 163353576
Test: make RunSettingsRoboTests ROBOTEST_FILTER=WifiUtilsTest
Change-Id: Icf3b5c85856906e4cbe2f0ad79583c1b7182b8c7
Merged-In: Ief3c6c1f08f8fcdf128768cde3ab8eef91f19fbe
This commit is contained in:
Arc Wang
2020-08-12 13:59:27 +08:00
parent 88558e8940
commit b432cf293c
3 changed files with 21 additions and 18 deletions

View File

@@ -23,6 +23,7 @@ import android.content.Context;
import android.content.pm.PackageManager;
import android.net.NetworkCapabilities;
import android.net.wifi.ScanResult;
import android.net.wifi.SoftApConfiguration;
import android.net.wifi.WifiConfiguration;
import android.os.UserHandle;
import android.os.UserManager;
@@ -38,8 +39,6 @@ public class WifiUtils {
private static final int SSID_ASCII_MIN_LENGTH = 1;
private static final int SSID_ASCII_MAX_LENGTH = 32;
private static final int PASSWORD_MIN_LENGTH = 8;
private static final int PASSWORD_MAX_LENGTH = 63;
public static boolean isSSIDTooLong(String ssid) {
@@ -56,13 +55,17 @@ public class WifiUtils {
return ssid.length() < SSID_ASCII_MIN_LENGTH;
}
public static boolean isHotspotPasswordValid(String password) {
if (TextUtils.isEmpty(password)) {
/**
* Check if the WPA2-PSK hotspot password is valid.
*/
public static boolean isHotspotWpa2PasswordValid(String password) {
final SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder();
try {
configBuilder.setPassphrase(password, SoftApConfiguration.SECURITY_TYPE_WPA2_PSK);
} catch (IllegalArgumentException e) {
return false;
}
final int length = password.length();
return length >= PASSWORD_MIN_LENGTH && length <= PASSWORD_MAX_LENGTH;
return true;
}
/**