Add long press actions to call log

When a call log entry is long pressed, show a context menu that
provides the following actions:

1) Copy to clipboard
2) Edit number before call

Bug: 21453814

Change-Id: I796cffbec39847fd9c61891063217b15cb298479
diff --git a/res/values/ids.xml b/res/values/ids.xml
new file mode 100644
index 0000000..e2f0ff5
--- /dev/null
+++ b/res/values/ids.xml
@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2015 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.
+-->
+
+<resources>
+    <item type="id" name="context_menu_copy_to_clipboard" />
+    <item type="id" name="context_menu_edit_before_call" />
+</resources>
diff --git a/src/com/android/dialer/calllog/CallLogAdapter.java b/src/com/android/dialer/calllog/CallLogAdapter.java
index bb776d2..83e5594 100644
--- a/src/com/android/dialer/calllog/CallLogAdapter.java
+++ b/src/com/android/dialer/calllog/CallLogAdapter.java
@@ -33,18 +33,27 @@
 import android.telephony.PhoneNumberUtils;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
+import android.util.Log;
+import android.view.ContextMenu;
 import android.view.LayoutInflater;
+import android.view.MenuItem;
+import android.view.MenuItem.OnMenuItemClickListener;
 import android.view.View;
 import android.view.View.AccessibilityDelegate;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver;
+import android.view.ContextMenu.ContextMenuInfo;
 import android.view.accessibility.AccessibilityEvent;
 
+import com.android.contacts.common.CallUtil;
+import com.android.contacts.common.ClipboardUtils;
 import com.android.contacts.common.util.PermissionsUtil;
+import com.android.dialer.DialtactsActivity;
 import com.android.dialer.PhoneCallDetails;
 import com.android.dialer.R;
 import com.android.dialer.contactinfo.ContactInfoCache;
 import com.android.dialer.contactinfo.ContactInfoCache.OnContactInfoChangedListener;
+import com.android.dialer.util.DialerUtils;
 import com.android.dialer.util.PhoneNumberUtil;
 import com.android.dialer.voicemail.VoicemailPlaybackPresenter;
 
@@ -138,7 +147,6 @@
         @Override
         public void onClick(View v) {
             CallLogListItemViewHolder viewHolder = (CallLogListItemViewHolder) v.getTag();
-
             if (viewHolder == null) {
                 return;
             }
@@ -185,6 +193,68 @@
         }
     };
 
+    /**
+     * Listener that is triggered to populate the context menu with actions to perform on the call's
+     * number, when the call log entry is long pressed.
+     */
+    private final View.OnCreateContextMenuListener mOnCreateContextMenuListener =
+            new View.OnCreateContextMenuListener() {
+                @Override
+                public void onCreateContextMenu(ContextMenu menu, View v,
+                        ContextMenuInfo menuInfo) {
+                    final CallLogListItemViewHolder vh =
+                            (CallLogListItemViewHolder) v.getTag();
+                    if (TextUtils.isEmpty(vh.number)) {
+                        return;
+                    }
+
+                    menu.setHeaderTitle(vh.number);
+
+                    final MenuItem copyItem = menu.add(
+                            ContextMenu.NONE,
+                            R.id.context_menu_copy_to_clipboard,
+                            ContextMenu.NONE,
+                            R.string.copy_text);
+
+                    copyItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+                        @Override
+                        public boolean onMenuItemClick(MenuItem item) {
+                            ClipboardUtils.copyText(CallLogAdapter.this.mContext, null,
+                                    vh.number, true);
+                            return true;
+                        }
+                    });
+
+                    // The edit number before call does not show up if any of the conditions apply:
+                    // 1) Number cannot be called
+                    // 2) Number is the voicemail number
+                    // 3) Number is a SIP address
+
+                    if (!PhoneNumberUtil.canPlaceCallsTo(vh.number, vh.numberPresentation)
+                            || mTelecomCallLogCache.isVoicemailNumber(vh.accountHandle, vh.number)
+                            || PhoneNumberUtil.isSipNumber(vh.number)) {
+                        return;
+                    }
+
+                    final MenuItem editItem = menu.add(
+                            ContextMenu.NONE,
+                            R.id.context_menu_edit_before_call,
+                            ContextMenu.NONE,
+                            R.string.recentCalls_editNumberBeforeCall);
+
+                    editItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+                        @Override
+                        public boolean onMenuItemClick(MenuItem item) {
+                            final Intent intent = new Intent(Intent.ACTION_DIAL,
+                                    CallUtil.getCallUri(vh.number));
+                            intent.setClass(mContext, DialtactsActivity.class);
+                            DialerUtils.startActivityWithErrorToast(mContext, intent);
+                            return true;
+                        }
+                    });
+                }
+            };
+
     private void expandViewHolderActions(CallLogListItemViewHolder viewHolder) {
         // If another item is expanded, notify it that it has changed. Its actions will be
         // hidden when it is re-binded because we change mCurrentlyExpandedPosition below.
@@ -369,6 +439,7 @@
         viewHolder.callLogEntryView.setTag(viewHolder);
         viewHolder.callLogEntryView.setAccessibilityDelegate(mAccessibilityDelegate);
 
+        viewHolder.primaryActionView.setOnCreateContextMenuListener(mOnCreateContextMenuListener);
         viewHolder.primaryActionView.setTag(viewHolder);
 
         return viewHolder;