Fix issues in Wi-Fi NFC tag code

An NPE + multiple comments

Change-Id: I04ffe10874cc2e9aa9f68bc551b8da98eb6f1640
This commit is contained in:
Andres Morales
2014-05-15 10:33:04 -07:00
parent 667848b8cb
commit ae04155bbe
4 changed files with 60 additions and 22 deletions

View File

@@ -1,4 +1,19 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<!--
Copyright (C) 2014 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.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/wifi_section"> style="@style/wifi_section">

View File

@@ -1417,6 +1417,8 @@
<string name="wifi_menu_forget">Forget network</string> <string name="wifi_menu_forget">Forget network</string>
<!-- Menu option to modify a Wi-Fi network configuration --> <!-- Menu option to modify a Wi-Fi network configuration -->
<string name="wifi_menu_modify">Modify network</string> <string name="wifi_menu_modify">Modify network</string>
<!-- Menu option to write a Wi-Fi configuration token to an NFC tag [CHAR_LIMIT=30]-->
<string name="wifi_menu_write_to_nfc">Write to NFC tag</string>
<!-- Wi-Fi settings. text displayed when Wi-Fi is off and network list is empty [CHAR LIMIT=50]--> <!-- Wi-Fi settings. text displayed when Wi-Fi is off and network list is empty [CHAR LIMIT=50]-->
<string name="wifi_empty_list_wifi_off">To see available networks, turn Wi\u2011Fi on.</string> <string name="wifi_empty_list_wifi_off">To see available networks, turn Wi\u2011Fi on.</string>
<!-- Wi-Fi settings. text displayed when Wi-Fi is on and network list is empty [CHAR LIMIT=50]--> <!-- Wi-Fi settings. text displayed when Wi-Fi is on and network list is empty [CHAR LIMIT=50]-->
@@ -1522,8 +1524,8 @@
<!-- Substring of wifi status for wifi with authentication. This version is for when the <!-- Substring of wifi status for wifi with authentication. This version is for when the
string is not first in the list (lowercase in english) --> string is not first in the list (lowercase in english) -->
<string name="wifi_secured_second_item">, secured with <xliff:g id="wifi_security_short">%1$s</xliff:g></string> <string name="wifi_secured_second_item">, secured with <xliff:g id="wifi_security_short">%1$s</xliff:g></string>
<!-- Message in WriteWifiConfigToNfcDialog when prompted to enter network password [CHAR LIMIT=150] --> <!-- Message in WriteWifiConfigToNfcDialog when prompted to enter network password [CHAR_LIMIT=40] -->
<string name="wifi_wps_nfc_enter_password">Enter your network password.</string> <string name="wifi_wps_nfc_enter_password">Enter your network password</string>
<!-- Do not translate. Concise terminology for wifi with WEP security --> <!-- Do not translate. Concise terminology for wifi with WEP security -->
<string name="wifi_security_short_wep">WEP</string> <string name="wifi_security_short_wep">WEP</string>
@@ -5138,10 +5140,10 @@
settings button --> settings button -->
<string name="notification_app_settings_button">Notification settings</string> <string name="notification_app_settings_button">Notification settings</string>
<!-- NFC WiFi pairing/setup strings--> <!-- NFC Wi-Fi pairing/setup strings-->
<!-- Write NFC tag for WiFi pairing/setup title --> <!-- Write NFC tag for Wi-Fi pairing/setup title [CHAR_LIMIT=30]-->
<string name="setup_wifi_nfc_tag">Set up WiFi NFC Tag</string> <string name="setup_wifi_nfc_tag">Set up Wi-Fi NFC Tag</string>
<!-- Text for button to confirm writing tag --> <!-- Text for button to confirm writing tag -->
<string name="write_tag">Write</string> <string name="write_tag">Write</string>
<!-- Text to inform the user to tap a tag to complete the setup process --> <!-- Text to inform the user to tap a tag to complete the setup process -->

View File

@@ -570,7 +570,7 @@ public class WifiSettings extends RestrictedSettingsFragment
if (mSelectedAccessPoint.security != AccessPoint.SECURITY_NONE) { if (mSelectedAccessPoint.security != AccessPoint.SECURITY_NONE) {
// Only allow writing of NFC tags for password-protected networks. // Only allow writing of NFC tags for password-protected networks.
menu.add(Menu.NONE, MENU_ID_WRITE_NFC, 0, "Write to NFC Tag"); menu.add(Menu.NONE, MENU_ID_WRITE_NFC, 0, R.string.wifi_menu_write_to_nfc);
} }
} }
} }
@@ -705,9 +705,11 @@ public class WifiSettings extends RestrictedSettingsFragment
}) })
.create(); .create();
case WRITE_NFC_DIALOG_ID: case WRITE_NFC_DIALOG_ID:
if (mSelectedAccessPoint != null) {
mWifiToNfcDialog = new WriteWifiConfigToNfcDialog( mWifiToNfcDialog = new WriteWifiConfigToNfcDialog(
getActivity(), mSelectedAccessPoint, mWifiManager); getActivity(), mSelectedAccessPoint, mWifiManager);
return mWifiToNfcDialog; return mWifiToNfcDialog;
}
} }
return super.onCreateDialog(dialogId); return super.onCreateDialog(dialogId);

View File

@@ -1,3 +1,19 @@
/*
* Copyright (C) 2014 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; package com.android.settings.wifi;
import android.app.Activity; import android.app.Activity;
@@ -39,6 +55,8 @@ class WriteWifiConfigToNfcDialog extends AlertDialog
private static final String TAG = WriteWifiConfigToNfcDialog.class.getName().toString(); private static final String TAG = WriteWifiConfigToNfcDialog.class.getName().toString();
private static final String PASSWORD_FORMAT = "102700%s%s"; private static final String PASSWORD_FORMAT = "102700%s%s";
private static final int HEX_RADIX = 16;
private static final char[] hexArray = "0123456789ABCDEF".toCharArray();
private final PowerManager.WakeLock mWakeLock; private final PowerManager.WakeLock mWakeLock;
@@ -58,12 +76,13 @@ class WriteWifiConfigToNfcDialog extends AlertDialog
WriteWifiConfigToNfcDialog(Context context, AccessPoint accessPoint, WriteWifiConfigToNfcDialog(Context context, AccessPoint accessPoint,
WifiManager wifiManager) { WifiManager wifiManager) {
super(context); super(context);
this.mContext = context;
this.mWakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE)) mContext = context;
mWakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE))
.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WriteWifiConfigToNfcDialog:wakeLock"); .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WriteWifiConfigToNfcDialog:wakeLock");
this.mAccessPoint = accessPoint; mAccessPoint = accessPoint;
this.mOnTextChangedHandler = new Handler(); mOnTextChangedHandler = new Handler();
this.mWifiManager = wifiManager; mWifiManager = wifiManager;
} }
@Override @Override
@@ -105,9 +124,9 @@ class WriteWifiConfigToNfcDialog extends AlertDialog
= mWifiManager.getWpsNfcConfigurationToken(mAccessPoint.networkId); = mWifiManager.getWpsNfcConfigurationToken(mAccessPoint.networkId);
String passwordHex = byteArrayToHexString(password.getBytes()); String passwordHex = byteArrayToHexString(password.getBytes());
String passwordLength = password.length() >= 16 String passwordLength = password.length() >= HEX_RADIX
? "" + Character.forDigit(password.length(), 16) ? "" + Character.forDigit(password.length(), HEX_RADIX)
: "0" + Character.forDigit(password.length(), 16); : "0" + Character.forDigit(password.length(), HEX_RADIX);
passwordHex = String.format(PASSWORD_FORMAT, passwordLength, passwordHex).toUpperCase(); passwordHex = String.format(PASSWORD_FORMAT, passwordLength, passwordHex).toUpperCase();
@@ -166,11 +185,11 @@ class WriteWifiConfigToNfcDialog extends AlertDialog
setViewText(mCancelButton, com.android.internal.R.string.done_label); setViewText(mCancelButton, com.android.internal.R.string.done_label);
} catch (IOException e) { } catch (IOException e) {
setViewText(mLabelView, R.string.status_failed_to_write); setViewText(mLabelView, R.string.status_failed_to_write);
Log.e(TAG, "Unable to write WiFi config to NFC tag.", e); Log.e(TAG, "Unable to write Wi-Fi config to NFC tag.", e);
return; return;
} catch (FormatException e) { } catch (FormatException e) {
setViewText(mLabelView, R.string.status_failed_to_write); setViewText(mLabelView, R.string.status_failed_to_write);
Log.e(TAG, "Unable to write WiFi config to NFC tag.", e); Log.e(TAG, "Unable to write Wi-Fi config to NFC tag.", e);
return; return;
} }
} else { } else {
@@ -239,14 +258,13 @@ class WriteWifiConfigToNfcDialog extends AlertDialog
byte[] data = new byte[len / 2]; byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) { for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4) data[i / 2] = (byte) ((Character.digit(s.charAt(i), HEX_RADIX) << 4)
+ Character.digit(s.charAt(i + 1), 16)); + Character.digit(s.charAt(i + 1), HEX_RADIX));
} }
return data; return data;
} }
final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
private static String byteArrayToHexString(byte[] bytes) { private static String byteArrayToHexString(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2]; char[] hexChars = new char[bytes.length * 2];
for ( int j = 0; j < bytes.length; j++ ) { for ( int j = 0; j < bytes.length; j++ ) {
@@ -259,6 +277,7 @@ class WriteWifiConfigToNfcDialog extends AlertDialog
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override @Override
public void afterTextChanged(Editable s) {} public void afterTextChanged(Editable s) {}
} }