Merge "Fix issues in Wi-Fi NFC tag code"
diff --git a/res/layout/write_wifi_config_to_nfc.xml b/res/layout/write_wifi_config_to_nfc.xml
index 416c1ae..f132bdb 100644
--- a/res/layout/write_wifi_config_to_nfc.xml
+++ b/res/layout/write_wifi_config_to_nfc.xml
@@ -1,4 +1,19 @@
 <?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"
             style="@style/wifi_section">
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 350798d..535e502 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1417,6 +1417,8 @@
     <string name="wifi_menu_forget">Forget network</string>
     <!-- Menu option to modify a Wi-Fi network configuration -->
     <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]-->
     <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]-->
@@ -1522,8 +1524,8 @@
     <!-- 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 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] -->
-    <string name="wifi_wps_nfc_enter_password">Enter your network password.</string>
+    <!-- Message in WriteWifiConfigToNfcDialog when prompted to enter network password [CHAR_LIMIT=40] -->
+    <string name="wifi_wps_nfc_enter_password">Enter your network password</string>
 
     <!-- Do not translate.  Concise terminology for wifi with WEP security -->
     <string name="wifi_security_short_wep">WEP</string>
@@ -5138,10 +5140,10 @@
          settings button -->
     <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 -->
-    <string name="setup_wifi_nfc_tag">Set up WiFi NFC Tag</string>
+    <!-- Write NFC tag for Wi-Fi pairing/setup title [CHAR_LIMIT=30]-->
+    <string name="setup_wifi_nfc_tag">Set up Wi-Fi NFC Tag</string>
     <!-- Text for button to confirm writing tag -->
     <string name="write_tag">Write</string>
     <!-- Text to inform the user to tap a tag to complete the setup process -->
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index 295dc07..00a5b90 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -567,7 +567,7 @@
 
                     if (mSelectedAccessPoint.security != AccessPoint.SECURITY_NONE) {
                         // 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);
                     }
                 }
             }
@@ -702,9 +702,11 @@
                             })
                             .create();
             case WRITE_NFC_DIALOG_ID:
-                mWifiToNfcDialog =new WriteWifiConfigToNfcDialog(
-                        getActivity(), mSelectedAccessPoint, mWifiManager);
-                return mWifiToNfcDialog;
+                if (mSelectedAccessPoint != null) {
+                    mWifiToNfcDialog = new WriteWifiConfigToNfcDialog(
+                            getActivity(), mSelectedAccessPoint, mWifiManager);
+                    return mWifiToNfcDialog;
+                }
 
         }
         return super.onCreateDialog(dialogId);
diff --git a/src/com/android/settings/wifi/WriteWifiConfigToNfcDialog.java b/src/com/android/settings/wifi/WriteWifiConfigToNfcDialog.java
index 7515f5c..e59c61c 100644
--- a/src/com/android/settings/wifi/WriteWifiConfigToNfcDialog.java
+++ b/src/com/android/settings/wifi/WriteWifiConfigToNfcDialog.java
@@ -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;
 
 import android.app.Activity;
@@ -39,6 +55,8 @@
 
     private static final String TAG = WriteWifiConfigToNfcDialog.class.getName().toString();
     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;
 
@@ -58,12 +76,13 @@
     WriteWifiConfigToNfcDialog(Context context, AccessPoint accessPoint,
             WifiManager wifiManager) {
         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");
-        this.mAccessPoint = accessPoint;
-        this.mOnTextChangedHandler = new Handler();
-        this.mWifiManager = wifiManager;
+        mAccessPoint = accessPoint;
+        mOnTextChangedHandler = new Handler();
+        mWifiManager = wifiManager;
     }
 
     @Override
@@ -105,9 +124,9 @@
                 = mWifiManager.getWpsNfcConfigurationToken(mAccessPoint.networkId);
         String passwordHex = byteArrayToHexString(password.getBytes());
 
-        String passwordLength = password.length() >= 16
-                ? "" + Character.forDigit(password.length(), 16)
-                : "0" + Character.forDigit(password.length(), 16);
+        String passwordLength = password.length() >= HEX_RADIX
+                ? "" + Character.forDigit(password.length(), HEX_RADIX)
+                : "0" + Character.forDigit(password.length(), HEX_RADIX);
 
         passwordHex = String.format(PASSWORD_FORMAT, passwordLength, passwordHex).toUpperCase();
 
@@ -166,11 +185,11 @@
                     setViewText(mCancelButton, com.android.internal.R.string.done_label);
                 } catch (IOException e) {
                     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;
                 } catch (FormatException e) {
                     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;
                 }
             } else {
@@ -239,14 +258,13 @@
         byte[] data = new byte[len / 2];
 
         for (int i = 0; i < len; i += 2) {
-            data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
-                    + Character.digit(s.charAt(i + 1), 16));
+            data[i / 2] = (byte) ((Character.digit(s.charAt(i), HEX_RADIX) << 4)
+                    + Character.digit(s.charAt(i + 1), HEX_RADIX));
         }
 
         return data;
     }
 
-    final protected static char[] hexArray = "0123456789ABCDEF".toCharArray();
     private static String byteArrayToHexString(byte[] bytes) {
         char[] hexChars = new char[bytes.length * 2];
         for ( int j = 0; j < bytes.length; j++ ) {
@@ -259,6 +277,7 @@
 
     @Override
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
+
     @Override
     public void afterTextChanged(Editable s) {}
 }