Clean up some interactions in entering/exiting searchUI

Entering/exiting the search UI should now behave as a state machine
that is described as follows:

* Tapping on the search box or the dialpad brings up the search UI
* If back is pressed when in the search UI, one of three things happens
1) If the dialpad or IME is up and a query exists in the searchbox or the
dialpad, the IME/dialpad is hidden to allow the user to scroll search
results.
2) If the dialpad or IME is up and the query is empty, hide the dialpad and
IME, and exit the search UI.
3) If the dialpad or IME is not up and the query is not empty, clear the
search query and exit the search UI.

Change-Id: I9ba21227e1f1fcba4cde1101b3516009ee55da12
diff --git a/src/com/android/dialer/DialtactsActivity.java b/src/com/android/dialer/DialtactsActivity.java
index d898600..c164294 100644
--- a/src/com/android/dialer/DialtactsActivity.java
+++ b/src/com/android/dialer/DialtactsActivity.java
@@ -48,6 +48,7 @@
 import android.view.MotionEvent;
 import android.view.View;
 import android.view.View.OnDragListener;
+import android.view.View.OnTouchListener;
 import android.view.animation.AccelerateInterpolator;
 import android.view.animation.Animation;
 import android.view.animation.Animation.AnimationListener;
@@ -305,7 +306,7 @@
         public boolean onKey(View v, int keyCode, KeyEvent event) {
             if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
                     TextUtils.isEmpty(mSearchView.getText().toString())) {
-                onBackPressed();
+                maybeExitSearchUi();
             }
             return false;
         }
@@ -367,6 +368,8 @@
         parentLayout.getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
         parentLayout.setOnDragListener(new LayoutOnDragListener());
 
+        setupActivityOverlay();
+
         mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
         ViewUtil.setupFloatingActionButton(mFloatingActionButtonContainer, getResources());
 
@@ -379,6 +382,19 @@
         SmartDialPrefix.initializeNanpSettings(this);
     }
 
+    private void setupActivityOverlay() {
+        final View activityOverlay = findViewById(R.id.activity_overlay);
+        activityOverlay.setOnTouchListener(new OnTouchListener() {
+            @Override
+            public boolean onTouch(View v, MotionEvent event) {
+                if (!mIsDialpadShown) {
+                    maybeExitSearchUi();
+                }
+                return false;
+            }
+        });
+    }
+
     @Override
     protected void onResume() {
         super.onResume();
@@ -865,6 +881,18 @@
         }
     }
 
+    /**
+     * @return True if the search UI was exited, false otherwise
+     */
+    private boolean maybeExitSearchUi() {
+        if (isInSearchUi() && TextUtils.isEmpty(mSearchQuery)) {
+            exitSearchUi();
+            hideInputMethod(parentLayout);
+            return true;
+        }
+        return false;
+    }
+
     @Override
     public void onDialpadQueryChanged(String query) {
         if (mSmartDialSearchFragment != null) {