Fix IME soft keyboard for Bluetooth rename device dialog.

The soft keyboard should automatically pop up when the user selects
the Bluetooth rename device menu item. Fixed by calling
setSoftInputMode(SOFT_INPUT_STATE_ALWAYS_VISIBLE) on the Window
containing the AlertDialog before showing.

The device name field should also be a single line field, with the
Done button causing the device to be renamed. Set the "singleLine"
attribute in the layout XML to true, and added a
TextView.OnEditorActionListener to set the device name and dismiss
the dialog for EditorInfo.IME_ACTION_DONE.

Bug: 5342542
Bug: 5343354
Change-Id: I550d8e9a59395ad66f8a9c11d29c0f2ef278c196
diff --git a/res/layout/dialog_edittext.xml b/res/layout/dialog_edittext.xml
index 6b849ac..80911da 100644
--- a/res/layout/dialog_edittext.xml
+++ b/res/layout/dialog_edittext.xml
@@ -25,6 +25,7 @@
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:maxLength="50"
+        android:singleLine="true"
     />
         
 </LinearLayout>
diff --git a/src/com/android/settings/bluetooth/BluetoothNameDialogFragment.java b/src/com/android/settings/bluetooth/BluetoothNameDialogFragment.java
index 4996858..b80e42a 100644
--- a/src/com/android/settings/bluetooth/BluetoothNameDialogFragment.java
+++ b/src/com/android/settings/bluetooth/BluetoothNameDialogFragment.java
@@ -30,10 +30,14 @@
 import android.text.InputFilter;
 import android.text.TextWatcher;
 import android.util.Log;
+import android.view.KeyEvent;
 import android.view.LayoutInflater;
 import android.view.View;
+import android.view.WindowManager;
+import android.view.inputmethod.EditorInfo;
 import android.widget.Button;
 import android.widget.EditText;
+import android.widget.TextView;
 
 import com.android.settings.R;
 
@@ -94,19 +98,23 @@
                 .setPositiveButton(R.string.bluetooth_rename_button,
                         new DialogInterface.OnClickListener() {
                             public void onClick(DialogInterface dialog, int which) {
-                                if (mLocalAdapter != null) {
-                                    String deviceName = mDeviceNameView.getText().toString();
-                                    Log.d(TAG, "Setting device name to " + deviceName);
-                                    mLocalAdapter.setName(deviceName);
-                                }
+                                String deviceName = mDeviceNameView.getText().toString();
+                                setDeviceName(deviceName);
                             }
                         })
                 .setNegativeButton(android.R.string.cancel, null)
                 .create();
+        mAlertDialog.getWindow().setSoftInputMode(
+                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
 
         return mAlertDialog;
     }
 
+    private void setDeviceName(String deviceName) {
+        Log.d(TAG, "Setting device name to " + deviceName);
+        mLocalAdapter.setName(deviceName);
+    }
+
     @Override
     public void onSaveInstanceState(Bundle outState) {
         outState.putString(KEY_NAME, mDeviceNameView.getText().toString());
@@ -123,6 +131,18 @@
         });
         mDeviceNameView.setText(deviceName);    // set initial value before adding listener
         mDeviceNameView.addTextChangedListener(this);
+        mDeviceNameView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
+            @Override
+            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
+                if (actionId == EditorInfo.IME_ACTION_DONE) {
+                    setDeviceName(v.getText().toString());
+                    mAlertDialog.dismiss();
+                    return true;    // action handled
+                } else {
+                    return false;   // not handled
+                }
+            }
+        });
         return view;
     }