Implement QR code parser WifiQrCodetest.
The parser supports standard WI-Fi DPP QR code & ZXing reader library's Wi-Fi Network config format. Bug: 118797380 Test: atest WifiQrCodetest atest RunSettingsRoboTests Change-Id: Ia47b8f65a900099749a6aa24afa3abd21ede1582
This commit is contained in:
223
src/com/android/settings/wifi/dpp/WifiQrCode.java
Normal file
223
src/com/android/settings/wifi/dpp/WifiQrCode.java
Normal file
@@ -0,0 +1,223 @@
|
||||
/*
|
||||
* Copyright (C) 2018 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.dpp;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import androidx.annotation.Keep;
|
||||
import androidx.annotation.VisibleForTesting;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Supports to parse 2 types of QR code
|
||||
*
|
||||
* 1. Standard Wi-Fi DPP bootstrapping information or
|
||||
* 2. ZXing reader library's Wi-Fi Network config format described in
|
||||
* https://github.com/zxing/zxing/wiki/Barcode-Contents#wi-fi-network-config-android-ios-11
|
||||
*
|
||||
* ZXing reader library's Wi-Fi Network config format example:
|
||||
*
|
||||
* WIFI:T:WPA;S:mynetwork;P:mypass;;
|
||||
*
|
||||
* parameter Example Description
|
||||
* T WPA Authentication type; can be WEP or WPA, or 'nopass' for no password. Or,
|
||||
* omit for no password.
|
||||
* S mynetwork Network SSID. Required. Enclose in double quotes if it is an ASCII name,
|
||||
* but could be interpreted as hex (i.e. "ABCD")
|
||||
* P mypass Password, ignored if T is "nopass" (in which case it may be omitted).
|
||||
* Enclose in double quotes if it is an ASCII name, but could be interpreted as
|
||||
* hex (i.e. "ABCD")
|
||||
* H true Optional. True if the network SSID is hidden.
|
||||
*
|
||||
*/
|
||||
@Keep
|
||||
public class WifiQrCode {
|
||||
public static final String SCHEME_DPP = "DPP";
|
||||
public static final String SCHEME_ZXING_WIFI_NETWORK_CONFIG = "WIFI";
|
||||
public static final String PREFIX_DPP = "DPP:";
|
||||
public static final String PREFIX_ZXING_WIFI_NETWORK_CONFIG = "WIFI:";
|
||||
|
||||
public static final String PREFIX_DPP_PUBLIC_KEY = "K:";
|
||||
public static final String PREFIX_DPP_INFORMATION = "I:";
|
||||
|
||||
public static final String PREFIX_ZXING_SECURITY = "T:";
|
||||
public static final String PREFIX_ZXING_SSID = "S:";
|
||||
public static final String PREFIX_ZXING_PASSWORD = "P:";
|
||||
public static final String PREFIX_ZXING_HIDDEN_SSID = "H:";
|
||||
|
||||
public static final String SUFFIX_QR_CODE = ";";
|
||||
|
||||
private String mQrCode;
|
||||
|
||||
/**
|
||||
* SCHEME_DPP for standard Wi-Fi device provision protocol; SCHEME_ZXING_WIFI_NETWORK_CONFIG
|
||||
* for ZXing reader library' Wi-Fi Network config format
|
||||
*/
|
||||
private String mScheme;
|
||||
|
||||
// Data from parsed Wi-Fi DPP QR code
|
||||
private String mPublicKey;
|
||||
private String mInformation;
|
||||
|
||||
// Data from parsed ZXing reader library's Wi-Fi Network config format
|
||||
private WifiNetworkConfig mWifiNetworkConfig;
|
||||
|
||||
@Keep
|
||||
public WifiQrCode(String qrCode) throws IllegalArgumentException {
|
||||
if (TextUtils.isEmpty(qrCode)) {
|
||||
throw new IllegalArgumentException("Empty QR code");
|
||||
}
|
||||
|
||||
mQrCode = qrCode;
|
||||
|
||||
if (qrCode.startsWith(PREFIX_DPP)) {
|
||||
mScheme = SCHEME_DPP;
|
||||
parseWifiDppQrCode(qrCode);
|
||||
} else if (qrCode.startsWith(PREFIX_ZXING_WIFI_NETWORK_CONFIG)) {
|
||||
mScheme = SCHEME_ZXING_WIFI_NETWORK_CONFIG;
|
||||
parseZxingWifiQrCode(qrCode);
|
||||
} else {
|
||||
throw new IllegalArgumentException("Invalid scheme");
|
||||
}
|
||||
}
|
||||
|
||||
/** Parses Wi-Fi DPP QR code string */
|
||||
private void parseWifiDppQrCode(String qrCode) throws IllegalArgumentException {
|
||||
String publicKey = getSubStringOrNull(qrCode, PREFIX_DPP_PUBLIC_KEY, SUFFIX_QR_CODE);
|
||||
if (TextUtils.isEmpty(publicKey)) {
|
||||
throw new IllegalArgumentException("Invalid format");
|
||||
}
|
||||
mPublicKey = publicKey;
|
||||
|
||||
mInformation = getSubStringOrNull(qrCode, PREFIX_DPP_INFORMATION, SUFFIX_QR_CODE);
|
||||
}
|
||||
|
||||
/** Parses ZXing reader library's Wi-Fi Network config format */
|
||||
private void parseZxingWifiQrCode(String qrCode) throws IllegalArgumentException {
|
||||
String security = getSubStringOrNull(qrCode, PREFIX_ZXING_SECURITY, SUFFIX_QR_CODE);
|
||||
String ssid = getSubStringOrNull(qrCode, PREFIX_ZXING_SSID, SUFFIX_QR_CODE);
|
||||
String password = getSubStringOrNull(qrCode, PREFIX_ZXING_PASSWORD, SUFFIX_QR_CODE);
|
||||
String hiddenSsidString = getSubStringOrNull(qrCode, PREFIX_ZXING_HIDDEN_SSID,
|
||||
SUFFIX_QR_CODE);
|
||||
boolean hiddenSsid = "true".equalsIgnoreCase(hiddenSsidString);
|
||||
|
||||
//"\", ";", "," and ":" are escaped with a backslash "\", should remove at first
|
||||
security = removeBackSlash(security);
|
||||
ssid = removeBackSlash(ssid);
|
||||
password = removeBackSlash(password);
|
||||
|
||||
mWifiNetworkConfig = WifiNetworkConfig.getValidConfigOrNull(security, ssid, password,
|
||||
hiddenSsid);
|
||||
|
||||
if (mWifiNetworkConfig == null) {
|
||||
throw new IllegalArgumentException("Invalid format");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the substring between prefix & suffix from input.
|
||||
*
|
||||
* @param prefix the string before the returned substring
|
||||
* @param suffix the string after the returned substring
|
||||
* @return null if not exists, non-null otherwise
|
||||
*/
|
||||
private static String getSubStringOrNull(String input, String prefix, String suffix) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String regex = sb.append(prefix).append("(.*?)").append(suffix).toString();
|
||||
Pattern pattern = Pattern.compile(regex);
|
||||
Matcher matcher = pattern.matcher(input);
|
||||
|
||||
if (!matcher.find()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
String target = matcher.group(1);
|
||||
if (TextUtils.isEmpty(target)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return target;
|
||||
}
|
||||
|
||||
@Keep
|
||||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
|
||||
protected static String removeBackSlash(String input) {
|
||||
if (input == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
boolean backSlash = false;
|
||||
for (char ch : input.toCharArray()) {
|
||||
if (ch != '\\') {
|
||||
sb.append(ch);
|
||||
backSlash = false;
|
||||
} else {
|
||||
if (backSlash) {
|
||||
sb.append(ch);
|
||||
backSlash = false;
|
||||
continue;
|
||||
}
|
||||
|
||||
backSlash = true;
|
||||
}
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
@Keep
|
||||
public String getQrCode() {
|
||||
return mQrCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses to check type of QR code
|
||||
*
|
||||
* SCHEME_DPP for standard Wi-Fi device provision protocol; SCHEME_ZXING_WIFI_NETWORK_CONFIG
|
||||
* for ZXing reader library' Wi-Fi Network config format
|
||||
*/
|
||||
@Keep
|
||||
public String getScheme() {
|
||||
return mScheme;
|
||||
}
|
||||
|
||||
/** Available when {@code getScheme()} returns SCHEME_DPP */
|
||||
@Keep
|
||||
public String getPublicKey() {
|
||||
return mPublicKey;
|
||||
}
|
||||
|
||||
/** May be available when {@code getScheme()} returns SCHEME_DPP */
|
||||
@Keep
|
||||
public String getInformation() {
|
||||
return mInformation;
|
||||
}
|
||||
|
||||
/** Available when {@code getScheme()} returns SCHEME_ZXING_WIFI_NETWORK_CONFIG */
|
||||
@Keep
|
||||
public WifiNetworkConfig getWifiNetworkConfig() {
|
||||
if (mWifiNetworkConfig == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new WifiNetworkConfig(mWifiNetworkConfig);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user