Add connect() method in WifiNetworkConfig to connect Wi-fi.
This method uses data of ZXing Wi-Fi QR code to connect. Bug: 118794978 Test: manual test Change-Id: I249f2f638c8d2fe231c50ba50eae8dfc0dc40a5c
This commit is contained in:
@@ -16,8 +16,18 @@
|
|||||||
|
|
||||||
package com.android.settings.wifi.dpp;
|
package com.android.settings.wifi.dpp;
|
||||||
|
|
||||||
|
import static com.android.settings.wifi.dpp.WifiQrCode.SECURITY_NO_PASSWORD;
|
||||||
|
import static com.android.settings.wifi.dpp.WifiQrCode.SECURITY_WEP;
|
||||||
|
import static com.android.settings.wifi.dpp.WifiQrCode.SECURITY_WPA;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.net.wifi.WifiConfiguration;
|
||||||
|
import android.net.wifi.WifiConfiguration.AuthAlgorithm;
|
||||||
|
import android.net.wifi.WifiConfiguration.KeyMgmt;
|
||||||
|
import android.net.wifi.WifiManager;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import androidx.annotation.Keep;
|
import androidx.annotation.Keep;
|
||||||
|
|
||||||
@@ -30,8 +40,7 @@ import androidx.annotation.Keep;
|
|||||||
* EXTRA_QR_CODE
|
* EXTRA_QR_CODE
|
||||||
*/
|
*/
|
||||||
public class WifiNetworkConfig {
|
public class WifiNetworkConfig {
|
||||||
// Ignores password if security is NO_PASSWORD or absent
|
private static final String TAG = "WifiNetworkConfig";
|
||||||
public static final String NO_PASSWORD = "nopass";
|
|
||||||
|
|
||||||
private String mSecurity;
|
private String mSecurity;
|
||||||
private String mSsid;
|
private String mSsid;
|
||||||
@@ -47,18 +56,9 @@ public class WifiNetworkConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public WifiNetworkConfig(WifiNetworkConfig config) {
|
public WifiNetworkConfig(WifiNetworkConfig config) {
|
||||||
if (config.mSecurity != null) {
|
mSecurity = config.mSecurity;
|
||||||
mSecurity = new String(config.mSecurity);
|
mSsid = config.mSsid;
|
||||||
}
|
mPreSharedKey = config.mPreSharedKey;
|
||||||
|
|
||||||
if (config.mSsid != null) {
|
|
||||||
mSsid = new String(config.mSsid);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (config.mPreSharedKey != null) {
|
|
||||||
mPreSharedKey = new String(config.mPreSharedKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
mHiddenSsid = config.mHiddenSsid;
|
mHiddenSsid = config.mHiddenSsid;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,7 +106,7 @@ public class WifiNetworkConfig {
|
|||||||
|
|
||||||
public static boolean isValidConfig(String security, String ssid, String preSharedKey,
|
public static boolean isValidConfig(String security, String ssid, String preSharedKey,
|
||||||
boolean hiddenSsid) {
|
boolean hiddenSsid) {
|
||||||
if (!TextUtils.isEmpty(security) && !NO_PASSWORD.equals(security)) {
|
if (!TextUtils.isEmpty(security) && !SECURITY_NO_PASSWORD.equals(security)) {
|
||||||
if (TextUtils.isEmpty(preSharedKey)) {
|
if (TextUtils.isEmpty(preSharedKey)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -183,4 +183,77 @@ public class WifiNetworkConfig {
|
|||||||
public boolean getHiddenSsid() {
|
public boolean getHiddenSsid() {
|
||||||
return mHiddenSsid;
|
return mHiddenSsid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void connect(Context context, WifiManager.ActionListener listener) {
|
||||||
|
WifiConfiguration wifiConfiguration = getWifiConfigurationOrNull();
|
||||||
|
if (wifiConfiguration == null) {
|
||||||
|
if (listener != null) {
|
||||||
|
listener.onFailure(WifiManager.ERROR);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
|
||||||
|
wifiManager.connect(wifiConfiguration, listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is a simplified method from {@code WifiConfigController.getConfig()}
|
||||||
|
*/
|
||||||
|
private WifiConfiguration getWifiConfigurationOrNull() {
|
||||||
|
if (!isValidConfig(this)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
final WifiConfiguration wifiConfiguration = new WifiConfiguration();
|
||||||
|
wifiConfiguration.SSID = addQuotationIfNeeded(mSsid);
|
||||||
|
wifiConfiguration.hiddenSSID = mHiddenSsid;
|
||||||
|
|
||||||
|
if (TextUtils.isEmpty(mSecurity) || SECURITY_NO_PASSWORD.equals(mSecurity)) {
|
||||||
|
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
|
||||||
|
return wifiConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (mSecurity.startsWith(SECURITY_WEP)) {
|
||||||
|
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
|
||||||
|
wifiConfiguration.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
|
||||||
|
wifiConfiguration.allowedAuthAlgorithms.set(AuthAlgorithm.SHARED);
|
||||||
|
|
||||||
|
// WEP-40, WEP-104, and 256-bit WEP (WEP-232?)
|
||||||
|
final int length = mPreSharedKey.length();
|
||||||
|
if ((length == 10 || length == 26 || length == 58)
|
||||||
|
&& mPreSharedKey.matches("[0-9A-Fa-f]*")) {
|
||||||
|
wifiConfiguration.wepKeys[0] = mPreSharedKey;
|
||||||
|
} else {
|
||||||
|
wifiConfiguration.wepKeys[0] = addQuotationIfNeeded(mPreSharedKey);
|
||||||
|
}
|
||||||
|
} else if (mSecurity.startsWith(SECURITY_WPA)) {
|
||||||
|
wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
|
||||||
|
|
||||||
|
if (mPreSharedKey.matches("[0-9A-Fa-f]{64}")) {
|
||||||
|
wifiConfiguration.preSharedKey = mPreSharedKey;
|
||||||
|
} else {
|
||||||
|
wifiConfiguration.preSharedKey = addQuotationIfNeeded(mPreSharedKey);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Log.w(TAG, "Unsupported security");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return wifiConfiguration;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String addQuotationIfNeeded(String input) {
|
||||||
|
if (TextUtils.isEmpty(input)) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (input.length() >= 2 && input.startsWith("\"") && input.endsWith("\"")) {
|
||||||
|
return input;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("\"").append(input).append("\"");
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -65,6 +65,11 @@ public class WifiQrCode {
|
|||||||
|
|
||||||
public static final String DELIMITER_QR_CODE = ";";
|
public static final String DELIMITER_QR_CODE = ";";
|
||||||
|
|
||||||
|
// Ignores password if security is SECURITY_NO_PASSWORD or absent
|
||||||
|
public static final String SECURITY_NO_PASSWORD = "nopass";
|
||||||
|
public static final String SECURITY_WEP = "WEP";
|
||||||
|
public static final String SECURITY_WPA = "WPA";
|
||||||
|
|
||||||
private String mQrCode;
|
private String mQrCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user