Remove the long-press menu from the call log.

Make sure that the logic is preserved in the CallDetailsActivity where
the actions are available: we were currently not handling all the
special cases for SIP numbers and voicemail.

Change-Id: I97f6ffaeb6ee839fd1ddbd4c4c0a532457cc9d14
diff --git a/src/com/android/contacts/CallDetailActivity.java b/src/com/android/contacts/CallDetailActivity.java
index bf5d8aa..9e8d471 100644
--- a/src/com/android/contacts/CallDetailActivity.java
+++ b/src/com/android/contacts/CallDetailActivity.java
@@ -226,6 +226,12 @@
         mPhoneCallDetailsHelper.setPhoneCallDetails(mPhoneCallDetailsViews,
                 details[0], false);
 
+        // Cache the details about the phone number.
+        final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
+        final boolean canPlaceCallsTo = mPhoneNumberHelper.canPlaceCallsTo(mNumber);
+        final boolean isVoicemailNumber = mPhoneNumberHelper.isVoicemailNumber(mNumber);
+        final boolean isSipNumber = mPhoneNumberHelper.isSipNumber(mNumber);
+
         // Let user view contact details if they exist, otherwise add option to create new contact
         // from this number.
         final Intent mainActionIntent;
@@ -235,6 +241,21 @@
             Uri personUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, personId);
             mainActionIntent = new Intent(Intent.ACTION_VIEW, personUri);
             mainActionIcon = R.drawable.sym_action_view_contact;
+        } else if (isVoicemailNumber) {
+            mainActionIntent = null;
+            mainActionIcon = 0;
+        } else if (isSipNumber) {
+            // TODO: This item is currently disabled for SIP addresses, because
+            // the Insert.PHONE extra only works correctly for PSTN numbers.
+            //
+            // To fix this for SIP addresses, we need to:
+            // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
+            //   the current number is a SIP address
+            // - update the contacts UI code to handle Insert.SIP_ADDRESS by
+            //   updating the SipAddress field
+            // and then we can remove the "!isSipNumber" check above.
+            mainActionIntent = null;
+            mainActionIcon = 0;
         } else {
             mainActionIntent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
             mainActionIntent.setType(Contacts.CONTENT_ITEM_TYPE);
@@ -242,30 +263,31 @@
             mainActionIcon = R.drawable.sym_action_add;
         }
 
-        mMainActionView.setVisibility(View.VISIBLE);
-        mMainActionView.setImageResource(mainActionIcon);
-        mMainActionView.setOnClickListener(new View.OnClickListener() {
-            @Override
-            public void onClick(View v) {
-                startActivity(mainActionIntent);
-            }
-        });
+        if (mainActionIntent == null) {
+            mMainActionView.setVisibility(View.INVISIBLE);
+        } else {
+            mMainActionView.setVisibility(View.VISIBLE);
+            mMainActionView.setImageResource(mainActionIcon);
+            mMainActionView.setOnClickListener(new View.OnClickListener() {
+                @Override
+                public void onClick(View v) {
+                    startActivity(mainActionIntent);
+                }
+            });
+        }
 
         // Build list of various available actions.
         final List<ViewEntry> actions = new ArrayList<ViewEntry>();
 
-        final boolean isSipNumber = PhoneNumberUtils.isUriNumber(mNumber);
-        final Uri numberCallUri = mPhoneNumberHelper.getCallUri(mNumber);
-
         // This action allows to call the number that places the call.
-        if (mPhoneNumberHelper.canPlaceCallsTo(mNumber)) {
+        if (canPlaceCallsTo) {
             actions.add(new ViewEntry(android.R.drawable.sym_action_call,
                     getString(R.string.menu_callNumber, mNumber),
                     new Intent(Intent.ACTION_CALL_PRIVILEGED, numberCallUri)));
         }
 
-        if (!isSipNumber) {
-            // SMS is only available for PSTN numbers.
+        // This action allows to send an SMS to the number that placed the call.
+        if (mPhoneNumberHelper.canSendSmsTo(mNumber)) {
             Intent smsIntent = new Intent(Intent.ACTION_SENDTO,
                     Uri.fromParts("sms", mNumber, null));
             actions.add(new ViewEntry(R.drawable.sym_action_sms,
@@ -292,7 +314,7 @@
                     }
                 }));
 
-        if (!isSipNumber) {
+        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),
diff --git a/src/com/android/contacts/calllog/CallLogFragment.java b/src/com/android/contacts/calllog/CallLogFragment.java
index b5d665a..0c87ebb 100644
--- a/src/com/android/contacts/calllog/CallLogFragment.java
+++ b/src/com/android/contacts/calllog/CallLogFragment.java
@@ -52,14 +52,11 @@
 import android.provider.ContactsContract.CommonDataKinds.SipAddress;
 import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.Data;
-import android.provider.ContactsContract.Intents.Insert;
 import android.provider.ContactsContract.PhoneLookup;
 import android.telephony.PhoneNumberUtils;
 import android.telephony.TelephonyManager;
 import android.text.TextUtils;
 import android.util.Log;
-import android.view.ContextMenu;
-import android.view.ContextMenu.ContextMenuInfo;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MenuInflater;
@@ -67,7 +64,6 @@
 import android.view.View;
 import android.view.ViewGroup;
 import android.view.ViewTreeObserver;
-import android.widget.AdapterView;
 import android.widget.ListView;
 import android.widget.QuickContactBadge;
 
@@ -77,8 +73,7 @@
 /**
  * Displays a list of call log entries.
  */
-public class CallLogFragment extends ListFragment
-        implements View.OnCreateContextMenuListener, ViewPagerVisibilityListener {
+public class CallLogFragment extends ListFragment implements ViewPagerVisibilityListener {
     private static final String TAG = "CallLogFragment";
 
     /** The size of the cache of contact info. */
@@ -124,10 +119,6 @@
         public static final int LOOKUP_KEY = 7;
     }
 
-    private static final class MenuItems {
-        public static final int DELETE = 1;
-    }
-
     private static final class OptionsMenuItems {
         public static final int DELETE_ALL = 1;
     }
@@ -845,7 +836,6 @@
     @Override
     public void onViewCreated(View view, Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
-        getListView().setOnCreateContextMenuListener(this);
         mAdapter = new CallLogAdapter();
         setListAdapter(mAdapter);
     }
@@ -952,88 +942,6 @@
     }
 
     @Override
-    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfoIn) {
-        AdapterView.AdapterContextMenuInfo menuInfo;
-        try {
-             menuInfo = (AdapterView.AdapterContextMenuInfo) menuInfoIn;
-        } catch (ClassCastException e) {
-            Log.e(TAG, "bad menuInfoIn", e);
-            return;
-        }
-
-        Cursor cursor = (Cursor) mAdapter.getItem(menuInfo.position);
-
-        String number = cursor.getString(CallLogQuery.NUMBER);
-        Uri numberUri = null;
-        boolean isVoicemail = false;
-        boolean isSipNumber = false;
-        if (number.equals(CallerInfo.UNKNOWN_NUMBER)) {
-            number = getString(R.string.unknown);
-        } else if (number.equals(CallerInfo.PRIVATE_NUMBER)) {
-            number = getString(R.string.private_num);
-        } else if (number.equals(CallerInfo.PAYPHONE_NUMBER)) {
-            number = getString(R.string.payphone);
-        } else if (PhoneNumberUtils.extractNetworkPortion(number).equals(mVoiceMailNumber)) {
-            number = getString(R.string.voicemail);
-            numberUri = Uri.parse("voicemail:x");
-            isVoicemail = true;
-        } else if (PhoneNumberUtils.isUriNumber(number)) {
-            numberUri = Uri.fromParts("sip", number, null);
-            isSipNumber = true;
-        } else {
-            numberUri = Uri.fromParts("tel", number, null);
-        }
-
-        ContactInfo info = mAdapter.getContactInfo(number);
-        boolean contactInfoPresent = (info != null && info != ContactInfo.EMPTY);
-        if (contactInfoPresent) {
-            menu.setHeaderTitle(info.name);
-        } else {
-            menu.setHeaderTitle(number);
-        }
-
-        if (numberUri != null) {
-            Intent intent = new Intent(Intent.ACTION_CALL_PRIVILEGED, numberUri);
-            menu.add(0, 0, 0, getResources().getString(R.string.recentCalls_callNumber, number))
-                    .setIntent(intent);
-        }
-
-        if (contactInfoPresent) {
-            menu.add(0, 0, 0, R.string.menu_viewContact)
-                    .setIntent(new Intent(Intent.ACTION_VIEW,
-                            ContentUris.withAppendedId(Contacts.CONTENT_URI, info.personId)));
-        }
-
-        if (numberUri != null && !isVoicemail && !isSipNumber) {
-            menu.add(0, 0, 0, R.string.recentCalls_editNumberBeforeCall)
-                    .setIntent(new Intent(Intent.ACTION_DIAL, numberUri));
-            menu.add(0, 0, 0, R.string.menu_sendTextMessage)
-                    .setIntent(new Intent(Intent.ACTION_SENDTO,
-                            Uri.fromParts("sms", number, null)));
-        }
-
-        // "Add to contacts" item, if this entry isn't already associated with a contact
-        if (!contactInfoPresent && numberUri != null && !isVoicemail && !isSipNumber) {
-            // TODO: This item is currently disabled for SIP addresses, because
-            // the Insert.PHONE extra only works correctly for PSTN numbers.
-            //
-            // To fix this for SIP addresses, we need to:
-            // - define ContactsContract.Intents.Insert.SIP_ADDRESS, and use it here if
-            //   the current number is a SIP address
-            // - update the contacts UI code to handle Insert.SIP_ADDRESS by
-            //   updating the SipAddress field
-            // and then we can remove the "!isSipNumber" check above.
-
-            Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
-            intent.setType(Contacts.CONTENT_ITEM_TYPE);
-            intent.putExtra(Insert.PHONE, number);
-            menu.add(0, 0, 0, R.string.recentCalls_addToContact)
-                    .setIntent(intent);
-        }
-        menu.add(0, MenuItems.DELETE, 0, R.string.recentCalls_removeFromRecentList);
-    }
-
-    @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         switch (item.getItemId()) {
             case OptionsMenuItems.DELETE_ALL: {
@@ -1044,42 +952,6 @@
         return super.onOptionsItemSelected(item);
     }
 
-    @Override
-    public boolean onContextItemSelected(MenuItem item) {
-        // Convert the menu info to the proper type
-        AdapterView.AdapterContextMenuInfo menuInfo;
-        try {
-             menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
-        } catch (ClassCastException e) {
-            Log.e(TAG, "bad menuInfoIn", e);
-            return false;
-        }
-
-        switch (item.getItemId()) {
-            case MenuItems.DELETE: {
-                Cursor cursor = (Cursor)mAdapter.getItem(menuInfo.position);
-                int groupSize = 1;
-                if (mAdapter.isGroupHeader(menuInfo.position)) {
-                    groupSize = mAdapter.getGroupSize(menuInfo.position);
-                }
-
-                StringBuilder sb = new StringBuilder();
-                for (int i = 0; i < groupSize; i++) {
-                    if (i != 0) {
-                        sb.append(",");
-                        cursor.moveToNext();
-                    }
-                    long id = cursor.getLong(CallLogQuery.ID);
-                    sb.append(id);
-                }
-
-                getActivity().getContentResolver().delete(Calls.CONTENT_URI_WITH_VOICEMAIL,
-                        Calls._ID + " IN (" + sb + ")", null);
-            }
-        }
-        return super.onContextItemSelected(item);
-    }
-
     /*
      * Get the number from the Contacts, if available, since sometimes
      * the number provided by caller id may not be formatted properly
diff --git a/src/com/android/contacts/calllog/PhoneNumberHelper.java b/src/com/android/contacts/calllog/PhoneNumberHelper.java
index b30fed5..4d96f4f 100644
--- a/src/com/android/contacts/calllog/PhoneNumberHelper.java
+++ b/src/com/android/contacts/calllog/PhoneNumberHelper.java
@@ -44,6 +44,11 @@
                 || number.equals(CallerInfo.PAYPHONE_NUMBER));
     }
 
+    /** Returns true if it is possible to send an SMS to the given number. */
+    public boolean canSendSmsTo(CharSequence number) {
+        return canPlaceCallsTo(number) && !isVoicemailNumber(number) && !isSipNumber(number);
+    }
+
     /**
      * Returns the string to display for the given phone number.
      *
@@ -75,10 +80,22 @@
 
     /** Returns a URI that can be used to place a call to this number. */
     public Uri getCallUri(String number) {
-        if (PhoneNumberUtils.isUriNumber(number)) {
+        if (isVoicemailNumber(number)) {
+            return Uri.parse("voicemail:x");
+        }
+        if (isSipNumber(number)) {
              return Uri.fromParts("sip", number, null);
-         } else {
-             return Uri.fromParts("tel", number, null);
-         }
+        }
+         return Uri.fromParts("tel", number, null);
+     }
+
+    /** Returns true if the given number is the number of the configured voicemail. */
+    public boolean isVoicemailNumber(CharSequence number) {
+        return PhoneNumberUtils.extractNetworkPortion(number.toString()).equals(mVoicemailNumber);
+    }
+
+    /** Returns true if the given number is a SIP address. */
+    public boolean isSipNumber(CharSequence number) {
+        return PhoneNumberUtils.isUriNumber(number.toString());
     }
 }