Show filter menu on the right of SearchView when no menu button

TESTED:
- filter option menu shows up on the right side of SearchView
  instead of being shown as an overflow menu when the device
  doesn't have a hard menu button
- filter option menu keeps shown as one of menus when
  the device has a hard menu button

Bug: 5131136
Change-Id: Ice630533cfd4fb13383958489549236f9229053d
diff --git a/res/layout/dialtacts_custom_action_bar.xml b/res/layout/dialtacts_custom_action_bar.xml
new file mode 100644
index 0000000..2be66f7
--- /dev/null
+++ b/res/layout/dialtacts_custom_action_bar.xml
@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+
+<!-- Dimensions are set at runtime in ActionBarAdapter -->
+<LinearLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="0dip"
+    android:layout_height="0dip"
+    android:orientation="horizontal">
+
+    <SearchView
+        android:id="@+id/search_view"
+        android:layout_width="0px"
+        android:layout_height="match_parent"
+        android:layout_weight="1"
+        android:iconifiedByDefault="false" />
+
+    <ImageButton
+        android:id="@+id/search_option"
+        android:layout_width="wrap_content"
+        android:paddingRight="8dip"
+        android:layout_height="match_parent"
+        android:layout_alignParentRight="true"
+        android:src="@drawable/ic_menu_overflow"
+        android:background="@android:color/transparent"
+        android:visibility="gone" />
+
+</LinearLayout>
diff --git a/res/menu/dialtacts_search_options.xml b/res/menu/dialtacts_search_options.xml
new file mode 100644
index 0000000..f2e0c67
--- /dev/null
+++ b/res/menu/dialtacts_search_options.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2011 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<!-- Used with DialtactsActivity's search mode. -->
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+    <item
+        android:id="@+id/filter_option"
+        android:title="@string/menu_contacts_filter"
+        android:showAsAction="withText" />
+</menu>
diff --git a/src/com/android/contacts/activities/DialtactsActivity.java b/src/com/android/contacts/activities/DialtactsActivity.java
index 3fd1558..7f4e6b4 100644
--- a/src/com/android/contacts/activities/DialtactsActivity.java
+++ b/src/com/android/contacts/activities/DialtactsActivity.java
@@ -57,9 +57,10 @@
 import android.view.MenuItem;
 import android.view.MenuItem.OnMenuItemClickListener;
 import android.view.View;
-import android.view.View.OnAttachStateChangeListener;
+import android.view.View.OnClickListener;
 import android.view.ViewConfiguration;
 import android.view.inputmethod.InputMethodManager;
+import android.widget.PopupMenu;
 import android.widget.SearchView;
 import android.widget.SearchView.OnCloseListener;
 import android.widget.SearchView.OnQueryTextListener;
@@ -245,6 +246,23 @@
     private SearchView mSearchView;
 
     /**
+     * Available only when the device doesn't have hard menu key. Used to show "filter option"
+     * menu on the right of {@link SearchView}.
+     */
+    private View mFilterOptionView;
+    private final OnClickListener mFilterOptionClickListener = new OnClickListener() {
+        @Override
+        public void onClick(View view) {
+            final PopupMenu popupMenu = new PopupMenu(DialtactsActivity.this, view);
+            final Menu menu = popupMenu.getMenu();
+            popupMenu.inflate(R.menu.dialtacts_search_options);
+            final MenuItem filterOptionMenuItem = menu.findItem(R.id.filter_option);
+            filterOptionMenuItem.setOnMenuItemClickListener(mFilterOptionsMenuItemClickListener);
+            popupMenu.show();
+        }
+    };
+
+    /**
      * The index of the Fragment (or, the tab) that has last been manually selected.
      * This value does not keep track of programmatically set Tabs (e.g. Call Log after a Call)
      */
@@ -393,7 +411,7 @@
 
     private void prepareSearchView() {
         final View searchViewLayout =
-                getLayoutInflater().inflate(R.layout.custom_action_bar, null);
+                getLayoutInflater().inflate(R.layout.dialtacts_custom_action_bar, null);
         mSearchView = (SearchView) searchViewLayout.findViewById(R.id.search_view);
         mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener);
         mSearchView.setOnCloseListener(mPhoneSearchCloseListener);
@@ -406,6 +424,14 @@
         mSearchView.setIconifiedByDefault(true);
         mSearchView.setQueryHint(getString(R.string.hint_findContacts));
         mSearchView.setIconified(false);
+
+        if (!ViewConfiguration.get(this).hasPermanentMenuKey()) {
+            // Filter option menu should be shown on the right side of SearchView.
+            mFilterOptionView = searchViewLayout.findViewById(R.id.search_option);
+            mFilterOptionView.setVisibility(View.VISIBLE);
+            mFilterOptionView.setOnClickListener(mFilterOptionClickListener);
+        }
+
         getActionBar().setCustomView(searchViewLayout,
                 new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
     }
@@ -675,9 +701,14 @@
         Tab tab = getActionBar().getSelectedTab();
         if (mInSearchUi) {
             searchMenuItem.setVisible(false);
-            filterOptionMenuItem.setVisible(true);
-            filterOptionMenuItem.setOnMenuItemClickListener(
-                    mFilterOptionsMenuItemClickListener);
+            if (ViewConfiguration.get(this).hasPermanentMenuKey()) {
+                filterOptionMenuItem.setVisible(true);
+                filterOptionMenuItem.setOnMenuItemClickListener(
+                        mFilterOptionsMenuItemClickListener);
+            } else {
+                // Filter option menu should be not be shown as a overflow menu.
+                filterOptionMenuItem.setVisible(false);
+            }
             callSettingsMenuItem.setVisible(false);
         } else {
             final boolean showCallSettingsMenu;