Move add contact intent creation to IntentProvider.

This is the more established pattern for creating intents to be used
by call log list item actions.

Bug: 19372817
Change-Id: I62cf5170cf51c4fbec8d1d9910b3fa3380a3dc3e
diff --git a/src/com/android/dialer/calllog/CallLogAdapter.java b/src/com/android/dialer/calllog/CallLogAdapter.java
index c4f6e89..2911d80 100644
--- a/src/com/android/dialer/calllog/CallLogAdapter.java
+++ b/src/com/android/dialer/calllog/CallLogAdapter.java
@@ -26,7 +26,6 @@
 import android.os.Handler;
 import android.os.Message;
 import android.provider.CallLog.Calls;
-import android.provider.ContactsContract;
 import android.provider.ContactsContract.CommonDataKinds.Phone;
 import android.provider.ContactsContract.PhoneLookup;
 import android.telecom.PhoneAccountHandle;
@@ -48,10 +47,7 @@
 import com.android.contacts.common.CallUtil;
 import com.android.contacts.common.ContactPhotoManager;
 import com.android.contacts.common.ContactPhotoManager.DefaultImageRequest;
-import com.android.contacts.common.model.Contact;
-import com.android.contacts.common.model.ContactLoader;
 import com.android.contacts.common.util.UriUtils;
-import com.android.dialer.DialtactsActivity;
 import com.android.dialer.PhoneCallDetails;
 import com.android.dialer.PhoneCallDetailsHelper;
 import com.android.dialer.R;
@@ -61,7 +57,6 @@
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Objects;
 
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.LinkedList;
 
@@ -1379,53 +1374,4 @@
             }
         }
     }
-
-    /**
-     * Invokes the "add contact" activity given the expanded contact information stored in a
-     * lookup URI.  This can include, for example, address and website information.
-     *
-     * @param lookupUri The lookup URI.
-     */
-    private void addContactFromLookupUri(Uri lookupUri) {
-        Contact contactToSave = ContactLoader.parseEncodedContactEntity(lookupUri);
-        if (contactToSave == null) {
-            return;
-        }
-
-        // Note: This code mirrors code in Contacts/QuickContactsActivity.
-        final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
-        intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
-
-        ArrayList<ContentValues> values = contactToSave.getContentValues();
-        // Only pre-fill the name field if the provided display name is an nickname
-        // or better (e.g. structured name, nickname)
-        if (contactToSave.getDisplayNameSource()
-                >= ContactsContract.DisplayNameSources.NICKNAME) {
-            intent.putExtra(ContactsContract.Intents.Insert.NAME,
-                    contactToSave.getDisplayName());
-        } else if (contactToSave.getDisplayNameSource()
-                == ContactsContract.DisplayNameSources.ORGANIZATION) {
-            // This is probably an organization. Instead of copying the organization
-            // name into a name entry, copy it into the organization entry. This
-            // way we will still consider the contact an organization.
-            final ContentValues organization = new ContentValues();
-            organization.put(ContactsContract.CommonDataKinds.Organization.COMPANY,
-                    contactToSave.getDisplayName());
-            organization.put(ContactsContract.Data.MIMETYPE,
-                    ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
-            values.add(organization);
-        }
-
-        // Last time used and times used are aggregated values from the usage stat
-        // table. They need to be removed from data values so the SQL table can insert
-        // properly
-        for (ContentValues value : values) {
-            value.remove(ContactsContract.Data.LAST_TIME_USED);
-            value.remove(ContactsContract.Data.TIMES_USED);
-        }
-        intent.putExtra(ContactsContract.Intents.Insert.DATA, values);
-
-        DialerUtils.startActivityWithErrorToast(mContext, intent,
-                R.string.add_contact_not_available);
-    }
 }
diff --git a/src/com/android/dialer/calllog/IntentProvider.java b/src/com/android/dialer/calllog/IntentProvider.java
index 8e96da7..2bd3f2e 100644
--- a/src/com/android/dialer/calllog/IntentProvider.java
+++ b/src/com/android/dialer/calllog/IntentProvider.java
@@ -16,16 +16,24 @@
 
 package com.android.dialer.calllog;
 
+import android.content.ContentValues;
 import android.content.ContentUris;
 import android.content.Context;
 import android.content.Intent;
 import android.net.Uri;
 import android.provider.CallLog.Calls;
+import android.provider.ContactsContract;
 import android.telecom.PhoneAccountHandle;
 
+import com.android.contacts.common.model.Contact;
+import com.android.contacts.common.model.ContactLoader;
 import com.android.dialer.CallDetailActivity;
+import com.android.dialer.DialtactsActivity;
+import com.android.dialer.PhoneCallDetails;
 import com.android.dialer.util.PrivilegedCallUtil;
 
+import java.util.ArrayList;
+
 /**
  * Used to create an intent to attach to an action in the call log.
  * <p>
@@ -124,4 +132,68 @@
             }
         };
     }
+
+    /**
+     * Retrieves an add contact intent for the given contact and phone call details.
+     *
+     * @param info The contact info.
+     * @param details The phone call details.
+     */
+    public static IntentProvider getAddContactIntentProvider(
+            final ContactInfo info, final PhoneCallDetails details) {
+        return new IntentProvider() {
+            @Override
+            public Intent getIntent(Context context) {
+                Contact contactToSave = null;
+
+                if (info.lookupUri != null) {
+                    contactToSave = ContactLoader.parseEncodedContactEntity(info.lookupUri);
+                }
+
+                if (contactToSave != null) {
+                    // Populate the intent with contact information stored in the lookup URI.
+                    // Note: This code mirrors code in Contacts/QuickContactsActivity.
+                    final Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
+                    intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
+
+                    ArrayList<ContentValues> values = contactToSave.getContentValues();
+                    // Only pre-fill the name field if the provided display name is an nickname
+                    // or better (e.g. structured name, nickname)
+                    if (contactToSave.getDisplayNameSource()
+                            >= ContactsContract.DisplayNameSources.NICKNAME) {
+                        intent.putExtra(ContactsContract.Intents.Insert.NAME,
+                                contactToSave.getDisplayName());
+                    } else if (contactToSave.getDisplayNameSource()
+                            == ContactsContract.DisplayNameSources.ORGANIZATION) {
+                        // This is probably an organization. Instead of copying the organization
+                        // name into a name entry, copy it into the organization entry. This
+                        // way we will still consider the contact an organization.
+                        final ContentValues organization = new ContentValues();
+                        organization.put(ContactsContract.CommonDataKinds.Organization.COMPANY,
+                                contactToSave.getDisplayName());
+                        organization.put(ContactsContract.Data.MIMETYPE,
+                                ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE);
+                        values.add(organization);
+                    }
+
+                    // Last time used and times used are aggregated values from the usage stat
+                    // table. They need to be removed from data values so the SQL table can insert
+                    // properly
+                    for (ContentValues value : values) {
+                        value.remove(ContactsContract.Data.LAST_TIME_USED);
+                        value.remove(ContactsContract.Data.TIMES_USED);
+                    }
+
+                    intent.putExtra(ContactsContract.Intents.Insert.DATA, values);
+
+                    return intent;
+                } else {
+                    // If no lookup uri is provided, rely on the available phone number and name.
+                    return DialtactsActivity.getAddToContactIntent(details.name,
+                            details.number,
+                            details.numberType);
+                }
+            }
+        };
+    }
 }