Suppress IME show-up on search mode.

- call SearchView#clearFocus()
- prepare the search view on onCreate()

Though SearchView itself tries to suppress IME on its clearFocus(),
ondemand SearchView creation forces IME show-up for some reason
(attaching it to ActionBar might be the cause of the harm).

Just creating the view every time on onCreate() avoids the problem.
It doesn't hurt performance or any other app capability much.

This change also removes some stale code for showing up IME, which
is needless anyway.

Bug: 5104943
Change-Id: I4c542b3d0cdc5287c9285493e8e5ed0ce25cb204
diff --git a/src/com/android/contacts/activities/DialtactsActivity.java b/src/com/android/contacts/activities/DialtactsActivity.java
index 6200a69..a51b267 100644
--- a/src/com/android/contacts/activities/DialtactsActivity.java
+++ b/src/com/android/contacts/activities/DialtactsActivity.java
@@ -358,6 +358,8 @@
         mViewPager.setAdapter(new ViewPagerAdapter(getFragmentManager()));
         mViewPager.setOnPageChangeListener(mPageChangeListener);
 
+        prepareSearchView();
+
         // Setup the ActionBar tabs (the order matches the tab-index contants TAB_INDEX_*)
         setupDialer();
         setupCallLog();
@@ -383,6 +385,25 @@
         }
     }
 
+    private void prepareSearchView() {
+        final View searchViewLayout =
+                getLayoutInflater().inflate(R.layout.custom_action_bar, null);
+        mSearchView = (SearchView) searchViewLayout.findViewById(R.id.search_view);
+        mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener);
+        mSearchView.setOnCloseListener(mPhoneSearchCloseListener);
+        // Since we're using a custom layout for showing SearchView instead of letting the
+        // search menu icon do that job, we need to manually configure the View so it looks
+        // "shown via search menu".
+        // - it should be iconified by default
+        // - it should not be iconified at this time
+        // See also comments for onActionViewExpanded()/onActionViewCollapsed()
+        mSearchView.setIconifiedByDefault(true);
+        mSearchView.setQueryHint(getString(R.string.hint_findContacts));
+        mSearchView.setIconified(false);
+        getActionBar().setCustomView(searchViewLayout,
+                new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
+    }
+
     @Override
     public void onAttachFragment(Fragment fragment) {
         // This method can be called before onCreate(), at which point we cannot rely on ViewPager.
@@ -690,46 +711,7 @@
             mLastManuallySelectedFragment = tab.getPosition();
         }
 
-        // Instantiate or reset SearchView in ActionBar.
-        if (mSearchView == null) {
-            final View searchViewLayout =
-                    getLayoutInflater().inflate(R.layout.custom_action_bar, null);
-            mSearchView = (SearchView) searchViewLayout.findViewById(R.id.search_view);
-            mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener);
-            mSearchView.setOnCloseListener(mPhoneSearchCloseListener);
-            // Since we're using a custom layout for showing SearchView instead of letting the
-            // search menu icon do that job, we need to manually configure the View so it looks
-            // "shown via search menu".
-            // - it should be iconified by default
-            // - it should not be iconified at this time
-            // See also comments for onActionViewExpanded()/onActionViewCollapsed()
-            mSearchView.setIconifiedByDefault(true);
-            mSearchView.setQueryHint(getString(R.string.hint_findContacts));
-            mSearchView.setIconified(false);
-            mSearchView.requestFocus();
-            // Show soft keyboard when SearchView has a focus. Need to delay the request in order
-            // to let InputMethodManager handle it correctly.
-            mSearchView.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
-                @Override
-                public void onViewDetachedFromWindow(View v) {
-                }
-
-                @Override
-                public void onViewAttachedToWindow(View v) {
-                    if (mSearchView.hasFocus()) {
-                        mSearchView.postDelayed(new Runnable() {
-                            public void run() {
-                                showInputMethod(mSearchView.findFocus());
-                            }
-                        }, 0);
-                    }
-                }
-            });
-            actionBar.setCustomView(searchViewLayout,
-                    new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
-        } else {
-            mSearchView.setQuery(null, true);
-        }
+        mSearchView.setQuery(null, true);
 
         actionBar.setDisplayShowCustomEnabled(true);
         actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
@@ -748,6 +730,9 @@
         // layout instead of asking the search menu item to take care of SearchView.
         mSearchView.onActionViewExpanded();
         mInSearchUi = true;
+
+        // Clear focus and suppress keyboard show-up.
+        mSearchView.clearFocus();
     }
 
     private void showInputMethod(View view) {