Move delete and edit number to overflow menu.

Bug: 5035685
Bug: 5036100
Change-Id: I503d0bcf978b34aa559d517ff8d693b597121703
diff --git a/res/menu/call_details_options.xml b/res/menu/call_details_options.xml
new file mode 100644
index 0000000..68e265c
--- /dev/null
+++ b/res/menu/call_details_options.xml
@@ -0,0 +1,29 @@
+<?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.
+-->
+<menu xmlns:android="http://schemas.android.com/apk/res/android">
+    <item
+        android:id="@+id/remove_from_call_log"
+        android:icon="@android:drawable/ic_menu_close_clear_cancel"
+        android:title="@string/recentCalls_removeFromRecentList"
+        android:showAsAction="withText"
+    />
+
+    <item
+        android:id="@+id/edit_number_before_call"
+        android:title="@string/recentCalls_editNumberBeforeCall"
+        android:showAsAction="withText"
+    />
+</menu>
diff --git a/src/com/android/contacts/CallDetailActivity.java b/src/com/android/contacts/CallDetailActivity.java
index 324ab20..68c9f61 100644
--- a/src/com/android/contacts/CallDetailActivity.java
+++ b/src/com/android/contacts/CallDetailActivity.java
@@ -45,6 +45,8 @@
 import android.util.Log;
 import android.view.KeyEvent;
 import android.view.LayoutInflater;
+import android.view.Menu;
+import android.view.MenuItem;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
@@ -99,6 +101,11 @@
     private TextView mStatusMessageText;
     private TextView mStatusMessageAction;
 
+    /** Whether we should show "remove from call log" in the options menu. */
+    private boolean mHasRemoveFromCallLog;
+    /** Whether we should show "edit number before call" in the options menu. */
+    private boolean mHasEditNumberBeforeCall;
+
     static final String[] CALL_LOG_PROJECTION = new String[] {
         CallLog.Calls.DATE,
         CallLog.Calls.DURATION,
@@ -356,37 +363,17 @@
 
         // This action deletes all elements in the group from the call log.
         // We don't have this action for voicemails, because you can just use the trash button.
-        if (!hasVoicemail()) {
-            actions.add(new ViewEntry(android.R.drawable.ic_menu_close_clear_cancel,
-                    getString(R.string.recentCalls_removeFromRecentList),
-                    new View.OnClickListener() {
-                        @Override
-                        public void onClick(View v) {
-                            StringBuilder callIds = new StringBuilder();
-                            for (Uri callUri : callUris) {
-                                if (callIds.length() != 0) {
-                                    callIds.append(",");
-                                }
-                                callIds.append(ContentUris.parseId(callUri));
-                            }
+        mHasRemoveFromCallLog = !hasVoicemail();
+        mHasEditNumberBeforeCall = canPlaceCallsTo && !isSipNumber && !isVoicemailNumber;
 
-                            getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
-                                    Calls._ID + " IN (" + callIds + ")", null);
-                            finish();
-                        }
-                    }));
+        if (actions.size() != 0) {
+            // Set the actions for this phone number.
+            setListAdapter(new ViewAdapter(this, actions));
+            getListView().setVisibility(View.VISIBLE);
+        } else {
+            getListView().setVisibility(View.GONE);
         }
 
-        if (canPlaceCallsTo && !isSipNumber && !isVoicemailNumber) {
-            // "Edit the number before calling" is only available for PSTN numbers.
-            actions.add(new ViewEntry(android.R.drawable.sym_action_call,
-                    getString(R.string.recentCalls_editNumberBeforeCall),
-                    new Intent(Intent.ACTION_DIAL, numberCallUri)));
-        }
-
-        // Set the actions for this phone number.
-        setListAdapter(new ViewAdapter(this, actions));
-
         ListView historyList = (ListView) findViewById(R.id.history);
         historyList.setAdapter(
                 new CallDetailHistoryAdapter(this, mInflater, mCallTypeHelper, details));
@@ -631,4 +618,48 @@
         }
         return messages.get(0);
     }
+
+    @Override
+    public boolean onCreateOptionsMenu(Menu menu) {
+        getMenuInflater().inflate(R.menu.call_details_options, menu);
+        return true;
+    }
+
+    @Override
+    public boolean onPrepareOptionsMenu(Menu menu) {
+        // This action deletes all elements in the group from the call log.
+        // We don't have this action for voicemails, because you can just use the trash button.
+        menu.findItem(R.id.remove_from_call_log).setVisible(mHasRemoveFromCallLog);
+        menu.findItem(R.id.edit_number_before_call).setVisible(mHasEditNumberBeforeCall);
+        return mHasRemoveFromCallLog || mHasEditNumberBeforeCall;
+    }
+
+    @Override
+    public boolean onMenuItemSelected(int featureId, MenuItem item) {
+        switch (item.getItemId()) {
+            case R.id.remove_from_call_log: {
+                StringBuilder callIds = new StringBuilder();
+                for (Uri callUri : getCallLogEntryUris()) {
+                    if (callIds.length() != 0) {
+                        callIds.append(",");
+                    }
+                    callIds.append(ContentUris.parseId(callUri));
+                }
+
+                getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
+                        Calls._ID + " IN (" + callIds + ")", null);
+                // Also close the activity.
+                finish();
+                return true;
+            }
+
+            case R.id.edit_number_before_call:
+                startActivity(
+                        new Intent(Intent.ACTION_DIAL, mPhoneNumberHelper.getCallUri(mNumber)));
+                return true;
+
+            default:
+                throw new IllegalArgumentException();
+        }
+    }
 }
diff --git a/tests/src/com/android/contacts/CallDetailActivityTest.java b/tests/src/com/android/contacts/CallDetailActivityTest.java
index 665bc92..c279860 100644
--- a/tests/src/com/android/contacts/CallDetailActivityTest.java
+++ b/tests/src/com/android/contacts/CallDetailActivityTest.java
@@ -18,6 +18,7 @@
 
 import com.android.contacts.util.IntegrationTestUtils;
 import com.android.contacts.util.LocaleTestUtils;
+import com.android.internal.view.menu.ContextMenuBuilder;
 import com.google.common.base.Preconditions;
 
 import android.app.Activity;
@@ -28,6 +29,7 @@
 import android.net.Uri;
 import android.provider.CallLog;
 import android.test.ActivityInstrumentationTestCase2;
+import android.view.Menu;
 
 import java.util.Locale;
 
@@ -93,19 +95,21 @@
      */
     public void testVoicemailDoesNotHaveRemoveFromCallLog() throws Throwable {
         setActivityIntentForTestVoicemailEntry();
-        getActivity();
-        assertEquals(0, countTextViewsContaining("Remove from call log"));
+        CallDetailActivity activity = getActivity();
+        Menu menu = new ContextMenuBuilder(activity);
+        activity.onCreateOptionsMenu(menu);
+        activity.onPrepareOptionsMenu(menu);
+        assertFalse(menu.findItem(R.id.remove_from_call_log).isVisible());
     }
 
     /** Test to check that I haven't broken the remove-from-call-log entry from regular calls. */
     public void testRegularCallDoesHaveRemoveFromCallLog() throws Throwable {
         setActivityIntentForTestCallEntry();
-        getActivity();
-        assertEquals(1, countTextViewsContaining("Remove from call log"));
-    }
-
-    private int countTextViewsContaining(String text) throws Throwable {
-        return mTestUtils.getTextViewsWithString(getActivity(), text).size();
+        CallDetailActivity activity = getActivity();
+        Menu menu = new ContextMenuBuilder(activity);
+        activity.onCreateOptionsMenu(menu);
+        activity.onPrepareOptionsMenu(menu);
+        assertTrue(menu.findItem(R.id.remove_from_call_log).isVisible());
     }
 
     private void setActivityIntentForTestCallEntry() {