Hide keyboard on touching type spinner and show it after selection changed

Bug: 25322155
Change-Id: I33eecbcaad15828a417921b893d6eb495395a15c
diff --git a/src/com/android/contacts/editor/LabeledEditorView.java b/src/com/android/contacts/editor/LabeledEditorView.java
index d09fb89..97926f4 100644
--- a/src/com/android/contacts/editor/LabeledEditorView.java
+++ b/src/com/android/contacts/editor/LabeledEditorView.java
@@ -29,10 +29,12 @@
 import android.util.AttributeSet;
 import android.util.TypedValue;
 import android.view.LayoutInflater;
+import android.view.MotionEvent;
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.WindowManager;
 import android.view.inputmethod.EditorInfo;
+import android.view.inputmethod.InputMethodManager;
 import android.widget.AdapterView;
 import android.widget.AdapterView.OnItemSelectedListener;
 import android.widget.ArrayAdapter;
@@ -137,6 +139,18 @@
         // Turn off the Spinner's own state management. We do this ourselves on rotation
         mLabel.setId(View.NO_ID);
         mLabel.setOnItemSelectedListener(mSpinnerListener);
+        mLabel.setOnTouchListener(new OnTouchListener() {
+            @Override
+            public boolean onTouch(View v, MotionEvent event) {
+                if (v == mLabel) {
+                    final InputMethodManager inputMethodManager = (InputMethodManager)
+                            getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+                    inputMethodManager.hideSoftInputFromWindow(
+                            mLabel.getWindowToken(), /* flags */ 0);
+                }
+                return false;
+            }
+        });
 
         mDelete = (ImageView) findViewById(R.id.delete_button);
         mDeleteContainer = findViewById(R.id.delete_button_container);
diff --git a/src/com/android/contacts/editor/TextFieldsEditorView.java b/src/com/android/contacts/editor/TextFieldsEditorView.java
index d82b019..c0c2332 100644
--- a/src/com/android/contacts/editor/TextFieldsEditorView.java
+++ b/src/com/android/contacts/editor/TextFieldsEditorView.java
@@ -123,13 +123,7 @@
         final View editor = mFields.getChildAt(0);
 
         // Show the soft-keyboard.
-        InputMethodManager imm =
-                (InputMethodManager)getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
-        if (imm != null) {
-            if (!imm.showSoftInput(editor, InputMethodManager.SHOW_IMPLICIT)) {
-                Log.w(TAG, "Failed to show soft input method.");
-            }
-        }
+        showSoftKeyboard(editor);
     }
 
     @Override
@@ -178,15 +172,29 @@
                 }
                 if (editText.hasFocus()) {
                     anyFieldHasFocus = true;
+                    showSoftKeyboard(editText);
                     break;
                 }
             }
             if (!anyFieldHasFocus && firstField != null) {
                 firstField.requestFocus();
+                showSoftKeyboard(firstField);
             }
         }
     }
 
+    /**
+     * Show soft keyboard for the currently focused view.
+     */
+    private void showSoftKeyboard(View v) {
+        final InputMethodManager inputMethodManager = (InputMethodManager)
+                getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
+        if (inputMethodManager != null &&
+                !inputMethodManager.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT)) {
+            Log.w(TAG, "Failed to show soft input method.");
+        }
+    }
+
     public void setValue(int field, String value) {
         mFieldEditTexts[field].setText(value);
     }