Move actionbar menu into menu button in search edit text.

No longer use the built-in options menu functionality. Instead add
our own menu button and handling its clicks as menu item clicks
instead of options menu clicks.

Bug: 14900155
Change-Id: I1ed15df2fc2463f963c1825f6f52437c6122f539
diff --git a/res/layout/search_edittext.xml b/res/layout/search_edittext.xml
index f44cc0b..a20935f 100644
--- a/res/layout/search_edittext.xml
+++ b/res/layout/search_edittext.xml
@@ -45,4 +45,12 @@
         android:clickable="true"
         android:contentDescription="@string/description_start_voice_search"
         android:background="?android:attr/selectableItemBackground" />
+    <ImageButton
+        android:id="@+id/dialtacts_options_menu_button"
+        android:layout_width="@dimen/search_box_icon_size"
+        android:layout_height="@dimen/search_box_icon_size"
+        android:padding="@dimen/search_box_icon_padding"
+        android:background="?android:attr/selectableItemBackground"
+        android:src="@drawable/ic_overflow_menu"
+        android:tint="@color/searchbox_options_menu_color" />
 </view>
\ No newline at end of file
diff --git a/res/values/colors.xml b/res/values/colors.xml
index 4874e1f..d337269 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -59,6 +59,7 @@
     <color name="searchbox_text_color">#000000</color>
     <!-- Text color of the search box hint text  -->
     <color name="searchbox_hint_text_color">#d3d3d3</color>
+    <color name="searchbox_options_menu_color">#d5d5d5</color>
 
     <!-- Color of the contact name in favorite tiles -->
     <color name="contact_tile_name_color">#ffffff</color>
diff --git a/src/com/android/dialer/DialtactsActivity.java b/src/com/android/dialer/DialtactsActivity.java
index 8fa4ae6..d888de6 100644
--- a/src/com/android/dialer/DialtactsActivity.java
+++ b/src/com/android/dialer/DialtactsActivity.java
@@ -44,7 +44,6 @@
 import android.view.DragEvent;
 import android.view.KeyEvent;
 import android.view.Menu;
-import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.view.MotionEvent;
 import android.view.View;
@@ -103,6 +102,7 @@
         SpeedDialFragment.HostInterface,
         OnDragDropListener,
         OnPhoneNumberPickerActionListener,
+        PopupMenu.OnMenuItemClickListener,
         ViewPager.OnPageChangeListener {
     private static final String TAG = "DialtactsActivity";
 
@@ -193,6 +193,7 @@
     private EditText mSearchView;
     private View mSearchViewCloseButton;
     private View mVoiceSearchButton;
+
     /**
      * View that contains the "Remove" dialog that shows up when the user long presses a contact.
      * If the user releases a contact when hovering on top of this, the contact is unfavorited and
@@ -207,8 +208,8 @@
     private DialerDatabaseHelper mDialerDatabaseHelper;
     private DragDropController mDragDropController;
 
-    private class OverflowPopupMenu extends PopupMenu {
-        public OverflowPopupMenu(Context context, View anchor) {
+    private class OptionsPopupMenu extends PopupMenu {
+        public OptionsPopupMenu(Context context, View anchor) {
             super(context, anchor);
         }
 
@@ -216,8 +217,9 @@
         public void show() {
             final Menu menu = getMenu();
             final MenuItem clearFrequents = menu.findItem(R.id.menu_clear_frequents);
-            // TODO: Check mSpeedDialFragment.hasFrequents()
-            clearFrequents.setVisible(true);
+            clearFrequents.setVisible(mListsFragment != null &&
+                    mListsFragment.getSpeedDialFragment() != null &&
+                    mListsFragment.getSpeedDialFragment().hasFrequents());
             super.show();
         }
     }
@@ -335,10 +337,15 @@
         mSearchView.addTextChangedListener(mPhoneSearchQueryTextListener);
         mSearchView.setOnTouchListener(mSearchViewOnTouchListener);
 
-
         mSearchViewCloseButton = actionBarView.findViewById(R.id.search_close_button);
         mSearchViewCloseButton.setOnClickListener(this);
 
+        ImageButton optionsMenuButton =
+                (ImageButton) actionBarView.findViewById(R.id.dialtacts_options_menu_button);
+        optionsMenuButton.setOnClickListener(this);
+        final OptionsPopupMenu optionsMenu = buildOptionsMenu(optionsMenuButton);
+        optionsMenuButton.setOnTouchListener(optionsMenu.getDragToOpenListener());
+
         final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
                 new int[] { android.R.attr.actionBarSize });
         mActionBarHeight = (int) styledAttributes.getDimension(0, 0);
@@ -462,6 +469,9 @@
                             Toast.LENGTH_SHORT).show();
                 }
                 break;
+            case R.id.dialtacts_options_menu_button:
+                buildOptionsMenu(view).show();
+                break;
             default: {
                 Log.wtf(TAG, "Unexpected onClick event from " + view);
                 break;
@@ -470,7 +480,7 @@
     }
 
     @Override
-    public boolean onOptionsItemSelected(MenuItem item) {
+    public boolean onMenuItemClick(MenuItem item) {
         switch (item.getItemId()) {
             case R.id.menu_history:
                 showCallHistory();
@@ -494,8 +504,6 @@
                         DialtactsActivity.class);
                 return true;
             case R.id.menu_clear_frequents:
-                // TODO: This should be enabled/disabled based on
-                // SpeedDialFragment.hasFrequents
                 ClearFrequentsDialog.show(getFragmentManager());
                 return true;
             case R.id.menu_call_settings:
@@ -653,19 +661,20 @@
         }
     }
 
+    private OptionsPopupMenu buildOptionsMenu(View invoker) {
+        final OptionsPopupMenu popupMenu = new OptionsPopupMenu(this, invoker);
+        popupMenu.inflate(R.menu.dialtacts_options);
+        popupMenu.setOnMenuItemClickListener(this);
+        return popupMenu;
+    }
+
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
-        if (DEBUG) {
-            Log.d(TAG, "onCreateOptionsMenu");
-        }
-        MenuInflater inflater = getMenuInflater();
-        inflater.inflate(R.menu.dialtacts_options, menu);
-
         if (mPendingSearchViewQuery != null) {
             mSearchView.setText(mPendingSearchViewQuery);
             mPendingSearchViewQuery = null;
         }
-        return super.onCreateOptionsMenu(menu);
+        return false;
     }
 
     /**
diff --git a/src/com/android/dialer/list/ListsFragment.java b/src/com/android/dialer/list/ListsFragment.java
index 2507065..b4497e4 100644
--- a/src/com/android/dialer/list/ListsFragment.java
+++ b/src/com/android/dialer/list/ListsFragment.java
@@ -364,4 +364,8 @@
         // height changes.
         transition.enableTransitionType(LayoutTransition.CHANGING);
     }
+
+    public SpeedDialFragment getSpeedDialFragment() {
+        return mSpeedDialFragment;
+    }
 }