Show QR Code picture for WiFi sharing
Bug: 118794858 Test: make RunSettingsRoboTests Change-Id: Ic5a489840ba59262eb444f02d77bf066889a4220
This commit is contained in:
@@ -25,9 +25,10 @@
|
|||||||
<include layout="@layout/wifi_dpp_fragment_header"/>
|
<include layout="@layout/wifi_dpp_fragment_header"/>
|
||||||
|
|
||||||
<ImageView
|
<ImageView
|
||||||
android:id="@+id/barcode_view"
|
android:id="@+id/qrcode_view"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="@dimen/qrcode_size"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="@dimen/qrcode_size"
|
||||||
|
android:src="@android:color/transparent"
|
||||||
android:layout_gravity="center"/>
|
android:layout_gravity="center"/>
|
||||||
|
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
@@ -366,4 +366,7 @@
|
|||||||
<dimen name="homepage_condition_header_icon_width_height">24dp</dimen>
|
<dimen name="homepage_condition_header_icon_width_height">24dp</dimen>
|
||||||
<dimen name="homepage_condition_header_icon_margin_end">24dp</dimen>
|
<dimen name="homepage_condition_header_icon_margin_end">24dp</dimen>
|
||||||
|
|
||||||
|
<!-- QR code picture size -->
|
||||||
|
<dimen name="qrcode_size">264dp</dimen>
|
||||||
|
|
||||||
</resources>
|
</resources>
|
||||||
|
@@ -549,8 +549,8 @@ public class WifiDetailPreferenceController extends AbstractPreferenceController
|
|||||||
* Show QR code to share the network represented by this preference.
|
* Show QR code to share the network represented by this preference.
|
||||||
*/
|
*/
|
||||||
public void launchQRCodeGenerator() {
|
public void launchQRCodeGenerator() {
|
||||||
Intent intent = WifiDppUtils.getConfiguratorQrCodeGeneratorIntent(mAccessPoint.getSsidStr(),
|
Intent intent = WifiDppUtils.getConfiguratorQrCodeGeneratorIntent(mContext, mWifiManager,
|
||||||
mAccessPoint.getSecurityString(/* concise */ false));
|
mAccessPoint);
|
||||||
mContext.startActivity(intent);
|
mContext.startActivity(intent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -18,19 +18,31 @@ package com.android.settings.wifi.dpp;
|
|||||||
|
|
||||||
import android.app.ActionBar;
|
import android.app.ActionBar;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.graphics.Bitmap;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuInflater;
|
import android.view.MenuInflater;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
|
||||||
import com.android.internal.logging.nano.MetricsProto;
|
import com.android.internal.logging.nano.MetricsProto;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
|
import com.android.settings.wifi.qrcode.QrCodeGenerator;
|
||||||
|
|
||||||
|
import com.google.zxing.WriterException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* After sharing a saved Wi-Fi network, {@code WifiDppConfiguratorActivity} start with this fragment
|
* After sharing a saved Wi-Fi network, {@code WifiDppConfiguratorActivity} start with this fragment
|
||||||
* to generate a Wi-Fi DPP QR code for other device to initiate as an enrollee.
|
* to generate a Wi-Fi DPP QR code for other device to initiate as an enrollee.
|
||||||
*/
|
*/
|
||||||
public class WifiDppQrCodeGeneratorFragment extends WifiDppQrCodeBaseFragment {
|
public class WifiDppQrCodeGeneratorFragment extends WifiDppQrCodeBaseFragment {
|
||||||
|
private static final String TAG = "WifiDppQrCodeGeneratorFragment";
|
||||||
|
|
||||||
|
private ImageView mQrCodeView;
|
||||||
|
private String mQrCode;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected int getLayout() {
|
protected int getLayout() {
|
||||||
return R.layout.wifi_dpp_qrcode_generator_fragment;
|
return R.layout.wifi_dpp_qrcode_generator_fragment;
|
||||||
@@ -67,6 +79,9 @@ public class WifiDppQrCodeGeneratorFragment extends WifiDppQrCodeBaseFragment {
|
|||||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
actionBar.setDisplayHomeAsUpEnabled(true);
|
||||||
actionBar.show();
|
actionBar.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mQrCode = wifiNetworkConfig.getQrCode();
|
||||||
|
setQrCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -102,4 +117,21 @@ public class WifiDppQrCodeGeneratorFragment extends WifiDppQrCodeBaseFragment {
|
|||||||
return super.onOptionsItemSelected(menuItem);
|
return super.onOptionsItemSelected(menuItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onViewCreated(View view, Bundle savedInstanceState) {
|
||||||
|
super.onViewCreated(view, savedInstanceState);
|
||||||
|
mQrCodeView = view.findViewById(R.id.qrcode_view);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setQrCode() {
|
||||||
|
try {
|
||||||
|
final int qrcodeSize = getContext().getResources().getDimensionPixelSize(
|
||||||
|
R.dimen.qrcode_size);
|
||||||
|
final Bitmap bmp = QrCodeGenerator.encodeQrCode(mQrCode, qrcodeSize);
|
||||||
|
mQrCodeView.setImageBitmap(bmp);
|
||||||
|
} catch (WriterException e) {
|
||||||
|
Log.e(TAG, "Error generatting QR code bitmap " + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,9 +18,15 @@ package com.android.settings.wifi.dpp;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
import android.net.wifi.WifiConfiguration;
|
||||||
|
import android.net.wifi.WifiManager;
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.util.FeatureFlagUtils;
|
import android.util.FeatureFlagUtils;
|
||||||
|
|
||||||
|
import com.android.settingslib.wifi.AccessPoint;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Here are the items shared by both WifiDppConfiguratorActivity & WifiDppEnrolleeActivity
|
* Here are the items shared by both WifiDppConfiguratorActivity & WifiDppEnrolleeActivity
|
||||||
*/
|
*/
|
||||||
@@ -84,23 +90,81 @@ public class WifiDppUtils {
|
|||||||
return intent;
|
return intent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String getPresharedKey(WifiManager wifiManager, WifiConfiguration config) {
|
||||||
|
String preSharedKey = config.preSharedKey;
|
||||||
|
|
||||||
|
final List<WifiConfiguration> wifiConfigs = wifiManager.getPrivilegedConfiguredNetworks();
|
||||||
|
for (WifiConfiguration wifiConfig : wifiConfigs) {
|
||||||
|
if (wifiConfig.networkId == config.networkId) {
|
||||||
|
preSharedKey = wifiConfig.preSharedKey;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return preSharedKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String removeFirstAndLastDoubleQuotes(String str) {
|
||||||
|
if (TextUtils.isEmpty(str)) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
int begin = 0;
|
||||||
|
int end = str.length() - 1;
|
||||||
|
if (str.charAt(begin) == '\"') {
|
||||||
|
begin++;
|
||||||
|
}
|
||||||
|
if (str.charAt(end) == '\"') {
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
return str.substring(begin, end+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getSecurityString(AccessPoint accessPoint) {
|
||||||
|
switch(accessPoint.getSecurity()) {
|
||||||
|
case AccessPoint.SECURITY_WEP:
|
||||||
|
return "WEP";
|
||||||
|
case AccessPoint.SECURITY_PSK:
|
||||||
|
return "WPA";
|
||||||
|
default:
|
||||||
|
return "nopass";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an intent to launch QR code generator.
|
* Returns an intent to launch QR code generator.
|
||||||
*
|
*
|
||||||
* @param ssid The data corresponding to {@code WifiConfiguration} SSID
|
* @param context The context to use for the content resolver
|
||||||
* @param Security The data is from {@code AccessPoint.securityToString}
|
* @param wifiManager An instance of {@link WifiManager}
|
||||||
|
* @param accessPoint An instance of {@link AccessPoint}
|
||||||
* @return Intent for launching QR code generator
|
* @return Intent for launching QR code generator
|
||||||
*/
|
*/
|
||||||
public static Intent getConfiguratorQrCodeGeneratorIntent(String ssid, String Security) {
|
public static Intent getConfiguratorQrCodeGeneratorIntent(Context context,
|
||||||
//TODO: b/118794858#comment6 should put password & hideSsid in intent extra
|
WifiManager wifiManager, AccessPoint accessPoint) {
|
||||||
final Intent intent = new Intent(
|
final Intent intent = new Intent(context, WifiDppConfiguratorActivity.class);
|
||||||
WifiDppConfiguratorActivity.ACTION_CONFIGURATOR_QR_CODE_GENERATOR);
|
intent.setAction(WifiDppConfiguratorActivity.ACTION_CONFIGURATOR_QR_CODE_GENERATOR);
|
||||||
|
|
||||||
|
final WifiConfiguration wifiConfig = accessPoint.getConfig();
|
||||||
|
final String ssid = removeFirstAndLastDoubleQuotes(wifiConfig.SSID);
|
||||||
|
final String security = getSecurityString(accessPoint);
|
||||||
|
String preSharedKey = wifiConfig.preSharedKey;
|
||||||
|
|
||||||
|
if (preSharedKey != null) {
|
||||||
|
// When the value of this key is read, the actual key is not returned, just a "*".
|
||||||
|
// Call privileged system API to obtain actual key.
|
||||||
|
preSharedKey = removeFirstAndLastDoubleQuotes(getPresharedKey(wifiManager, wifiConfig));
|
||||||
|
}
|
||||||
|
|
||||||
if (!TextUtils.isEmpty(ssid)) {
|
if (!TextUtils.isEmpty(ssid)) {
|
||||||
intent.putExtra(EXTRA_WIFI_SSID, ssid);
|
intent.putExtra(EXTRA_WIFI_SSID, ssid);
|
||||||
}
|
}
|
||||||
if (!TextUtils.isEmpty(Security)) {
|
if (!TextUtils.isEmpty(security)) {
|
||||||
intent.putExtra(EXTRA_WIFI_SECURITY, Security);
|
intent.putExtra(EXTRA_WIFI_SECURITY, security);
|
||||||
}
|
}
|
||||||
|
if (!TextUtils.isEmpty(preSharedKey)) {
|
||||||
|
intent.putExtra(EXTRA_WIFI_PRE_SHARED_KEY, preSharedKey);
|
||||||
|
}
|
||||||
|
|
||||||
return intent;
|
return intent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -119,6 +119,51 @@ public class WifiNetworkConfig {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Escaped special characters "\", ";", ":", "," with a backslash
|
||||||
|
* See https://github.com/zxing/zxing/wiki/Barcode-Contents
|
||||||
|
*/
|
||||||
|
private String escapeSpecialCharacters(String str) {
|
||||||
|
if (TextUtils.isEmpty(str)) {
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder buf = new StringBuilder();
|
||||||
|
for (int i = 0; i < str.length(); i++) {
|
||||||
|
char ch = str.charAt(i);
|
||||||
|
if (ch =='\\' || ch == ',' || ch == ';' || ch == ':') {
|
||||||
|
buf.append('\\');
|
||||||
|
}
|
||||||
|
buf.append(ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a barcode string for WiFi network login.
|
||||||
|
* See https://en.wikipedia.org/wiki/QR_code#WiFi_network_login
|
||||||
|
*/
|
||||||
|
public String getQrCode() {
|
||||||
|
final String empty = "";
|
||||||
|
String barcode = new StringBuilder("WIFI:")
|
||||||
|
.append("S:")
|
||||||
|
.append(escapeSpecialCharacters(mSsid))
|
||||||
|
.append(";")
|
||||||
|
.append("T:")
|
||||||
|
.append(TextUtils.isEmpty(mSecurity) ? empty : mSecurity)
|
||||||
|
.append(";")
|
||||||
|
.append("P:")
|
||||||
|
.append(TextUtils.isEmpty(mPreSharedKey) ? empty
|
||||||
|
: escapeSpecialCharacters(mPreSharedKey))
|
||||||
|
.append(";")
|
||||||
|
.append("H:")
|
||||||
|
.append(mHiddenSsid)
|
||||||
|
.append(";;")
|
||||||
|
.toString();
|
||||||
|
return barcode;
|
||||||
|
}
|
||||||
|
|
||||||
@Keep
|
@Keep
|
||||||
public String getSecurity() {
|
public String getSecurity() {
|
||||||
return mSecurity;
|
return mSecurity;
|
||||||
|
@@ -0,0 +1,46 @@
|
|||||||
|
/*
|
||||||
|
* 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 static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
|
import android.content.Intent;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.robolectric.RobolectricTestRunner;
|
||||||
|
|
||||||
|
@RunWith(RobolectricTestRunner.class)
|
||||||
|
public class WifiNetworkConfigTest {
|
||||||
|
private WifiNetworkConfig mWifiConfig;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
Intent intent = new Intent();
|
||||||
|
intent.putExtra(WifiDppUtils.EXTRA_WIFI_SSID, "Pixel:_ABCD;");
|
||||||
|
intent.putExtra(WifiDppUtils.EXTRA_WIFI_SECURITY, "WPA");
|
||||||
|
intent.putExtra(WifiDppUtils.EXTRA_WIFI_PRE_SHARED_KEY, "\\012345678,");
|
||||||
|
mWifiConfig = WifiNetworkConfig.getValidConfigOrNull(intent);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInitConfig_IntentReceived_QRCodeValue() {
|
||||||
|
String qrcode = mWifiConfig.getQrCode();
|
||||||
|
assertThat(qrcode).isEqualTo("WIFI:S:Pixel\\:_ABCD\\;;T:WPA;P:\\\\012345678\\,;H:false;;");
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user