Improve IME opening logic

Currently, the IME opens when the search box is focused and it closes
when the search box loses focus. This prevents the user from navigating
to the Clear button because focus moves to the IME when it opens. This
change makes the IME open when the search box is clicked. It also moves
focus to the search box and opens the IME when the Clear button is
pressed. The logic to close the IME remains unchanged.

Test: Search for a contact using touch and rotary
Bug: 174768762
Change-Id: I09f134e3754adeb3b5bff5a12d5e0aac3661bf20
diff --git a/car-ui-lib/car-ui-lib/src/main/java/com/android/car/ui/toolbar/SearchView.java b/car-ui-lib/car-ui-lib/src/main/java/com/android/car/ui/toolbar/SearchView.java
index 62511f0..3ee0f2f 100644
--- a/car-ui-lib/car-ui-lib/src/main/java/com/android/car/ui/toolbar/SearchView.java
+++ b/car-ui-lib/car-ui-lib/src/main/java/com/android/car/ui/toolbar/SearchView.java
@@ -132,7 +132,13 @@
         mIcon = requireViewByRefId(this, R.id.car_ui_toolbar_search_icon);
         mCloseIcon = requireViewByRefId(this, R.id.car_ui_toolbar_search_close);
 
-        mCloseIcon.setOnClickListener(view -> mSearchText.getText().clear());
+        mCloseIcon.setOnClickListener(view -> {
+            if (view.isFocused()) {
+                mSearchText.requestFocus();
+                mInputMethodManager.showSoftInput(mSearchText, 0);
+            }
+            mSearchText.getText().clear();
+        });
         mCloseIcon.setVisibility(View.GONE);
 
         mStartPaddingWithoutIcon = mSearchText.getPaddingStart();
@@ -144,11 +150,11 @@
         mSearchText.setSaveEnabled(false);
         mSearchText.setPaddingRelative(mStartPadding, 0, mEndPadding, 0);
 
+        mSearchText.setOnClickListener((view) -> mInputMethodManager.showSoftInput(view, 0));
+
         mSearchText.setOnFocusChangeListener(
                 (view, hasFocus) -> {
-                    if (hasFocus) {
-                        mInputMethodManager.showSoftInput(view, 0);
-                    } else {
+                    if (!hasFocus) {
                         mInputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
                     }
                 });
@@ -240,6 +246,7 @@
             boolean hasQuery = mSearchText.getText().length() > 0;
             mCloseIcon.setVisibility(hasQuery ? View.VISIBLE : View.GONE);
             mSearchText.requestFocus();
+            mInputMethodManager.showSoftInput(mSearchText, 0);
         }
         mWasShown = isShown;
     }