Reorganize Intent utilities, add send SMS intent.

+ Rename CallIntentUtils.java to IntentUtil.java.
+ Consolidate various intent creation methods to new file, and update
referenes throughout the application.

Bug: 20433758
Change-Id: Iee9e37985217c38c816124d0e74dff40a2871680
diff --git a/src/com/android/dialer/CallDetailActivity.java b/src/com/android/dialer/CallDetailActivity.java
index 0a295c4..735d6eb 100644
--- a/src/com/android/dialer/CallDetailActivity.java
+++ b/src/com/android/dialer/CallDetailActivity.java
@@ -64,7 +64,7 @@
 import com.android.dialer.calllog.PhoneNumberUtilsWrapper;
 import com.android.dialer.util.AsyncTaskExecutor;
 import com.android.dialer.util.AsyncTaskExecutors;
-import com.android.dialer.util.CallIntentUtil;
+import com.android.dialer.util.IntentUtil;
 import com.android.dialer.util.DialerUtils;
 import com.android.dialer.util.TelecomUtil;
 import com.android.dialer.voicemail.VoicemailPlaybackFragment;
@@ -323,8 +323,9 @@
                         getSystemService(Context.TELEPHONY_SERVICE);
                 if (tm.getCallState() == TelephonyManager.CALL_STATE_IDLE) {
                     DialerUtils.startActivityWithErrorToast(this,
-                            CallIntentUtil.getCallIntent(Uri.fromParts(PhoneAccount.SCHEME_TEL,
-                                    mNumber, null)), R.string.call_not_available);
+                            IntentUtil.getCallIntent(
+                                    Uri.fromParts(PhoneAccount.SCHEME_TEL, mNumber, null)),
+                                    R.string.call_not_available);
                     return true;
                 }
             }
diff --git a/src/com/android/dialer/DialtactsActivity.java b/src/com/android/dialer/DialtactsActivity.java
index cccc06e..25828e5 100644
--- a/src/com/android/dialer/DialtactsActivity.java
+++ b/src/com/android/dialer/DialtactsActivity.java
@@ -29,7 +29,6 @@
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Trace;
-import android.provider.ContactsContract.Contacts;
 import android.provider.ContactsContract.Intents;
 import android.speech.RecognizerIntent;
 import android.support.v4.view.ViewPager;
@@ -82,7 +81,7 @@
 import com.android.dialer.list.SmartDialSearchFragment;
 import com.android.dialer.list.SpeedDialFragment;
 import com.android.dialer.settings.DialerSettingsActivity;
-import com.android.dialer.util.CallIntentUtil;
+import com.android.dialer.util.IntentUtil;
 import com.android.dialer.util.DialerUtils;
 import com.android.dialer.widget.ActionBarController;
 import com.android.dialer.widget.SearchEditTextLayout;
@@ -587,7 +586,10 @@
         switch (view.getId()) {
             case R.id.floating_action_button:
                 if (mListsFragment.getTabPosition() == ListsFragment.TAB_INDEX_ALL_CONTACTS) {
-                    sendAddNewContactIntent();
+                    DialerUtils.startActivityWithErrorToast(
+                            this,
+                            IntentUtil.getNewContactIntent(),
+                            R.string.add_contact_not_available);
                 } else if (!mIsDialpadShown) {
                     mInCallDialpadUp = false;
                     showDialpadFragment(true);
@@ -622,7 +624,10 @@
                 startActivity(intent);
                 break;
             case R.id.menu_add_contact:
-                sendAddNewContactIntent();
+                DialerUtils.startActivityWithErrorToast(
+                        this,
+                        IntentUtil.getNewContactIntent(),
+                        R.string.add_contact_not_available);
                 break;
             case R.id.menu_import_export:
                 // We hard-code the "contactsAreAvailable" argument because doing it properly would
@@ -1082,38 +1087,6 @@
         return getTelecomManager().isInCall();
     }
 
-    public static Intent getAddNumberToContactIntent(CharSequence text) {
-        return getAddToContactIntent(null /* name */, text /* phoneNumber */,
-                -1 /* phoneNumberType */);
-    }
-
-    public static Intent getAddToContactIntent(CharSequence name, CharSequence phoneNumber,
-            int phoneNumberType) {
-        Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
-        intent.putExtra(Intents.Insert.PHONE, phoneNumber);
-        // Only include the name and phone type extras if they are specified (the method
-        // getAddNumberToContactIntent does not use them).
-        if (name != null) {
-            intent.putExtra(Intents.Insert.NAME, name);
-        }
-        if (phoneNumberType != -1) {
-            intent.putExtra(Intents.Insert.PHONE_TYPE, phoneNumberType);
-        }
-        intent.setType(Contacts.CONTENT_ITEM_TYPE);
-        return intent;
-    }
-
-    private void sendAddNewContactIntent() {
-        try {
-            startActivity(new Intent(Intent.ACTION_INSERT, Contacts.CONTENT_URI));
-        } catch (ActivityNotFoundException e) {
-            Toast toast = Toast.makeText(this,
-                    R.string.add_contact_not_available,
-                    Toast.LENGTH_SHORT);
-            toast.show();
-        }
-    }
-
     private boolean canIntentBeHandled(Intent intent) {
         final PackageManager packageManager = getPackageManager();
         final List<ResolveInfo> resolveInfo = packageManager.queryIntentActivities(intent,
@@ -1171,8 +1144,8 @@
     @Override
     public void onCallNumberDirectly(String phoneNumber, boolean isVideoCall) {
         Intent intent = isVideoCall ?
-                CallIntentUtil.getVideoCallIntent(phoneNumber, getCallOrigin()) :
-                CallIntentUtil.getCallIntent(phoneNumber, getCallOrigin());
+                IntentUtil.getVideoCallIntent(phoneNumber, getCallOrigin()) :
+                IntentUtil.getCallIntent(phoneNumber, getCallOrigin());
         DialerUtils.startActivityWithErrorToast(this, intent);
         mClearSearchOnPause = true;
     }
diff --git a/src/com/android/dialer/calllog/CallLogListItemViewHolder.java b/src/com/android/dialer/calllog/CallLogListItemViewHolder.java
index 4fa6561..72cbdba 100644
--- a/src/com/android/dialer/calllog/CallLogListItemViewHolder.java
+++ b/src/com/android/dialer/calllog/CallLogListItemViewHolder.java
@@ -72,6 +72,7 @@
     public View voicemailButtonView;
     public View createNewContactButtonView;
     public View addToExistingContactButtonView;
+    public View sendMessageView;
     public View detailsButtonView;
     public View reportButtonView;
 
@@ -218,35 +219,26 @@
         ViewStub stub = (ViewStub) rootView.findViewById(R.id.call_log_entry_actions_stub);
         if (stub != null) {
             actionsView = (ViewGroup) stub.inflate();
-        }
 
-        if (videoCallButtonView == null) {
             videoCallButtonView = actionsView.findViewById(R.id.video_call_action);
             videoCallButtonView.setOnClickListener(mActionListener);
-        }
 
-        if (voicemailButtonView == null) {
             voicemailButtonView = actionsView.findViewById(R.id.voicemail_action);
             voicemailButtonView.setOnClickListener(mActionListener);
-        }
 
-        if (createNewContactButtonView == null) {
             createNewContactButtonView = actionsView.findViewById(R.id.create_new_contact_action);
             createNewContactButtonView.setOnClickListener(mActionListener);
-        }
 
-        if (addToExistingContactButtonView == null) {
             addToExistingContactButtonView =
                     actionsView.findViewById(R.id.add_to_existing_contact_action);
             addToExistingContactButtonView.setOnClickListener(mActionListener);
-        }
 
-        if (detailsButtonView == null) {
+            sendMessageView = actionsView.findViewById(R.id.send_message_action);
+            sendMessageView.setOnClickListener(mActionListener);
+
             detailsButtonView = actionsView.findViewById(R.id.details_action);
             detailsButtonView.setOnClickListener(mActionListener);
-        }
 
-        if (reportButtonView == null) {
             reportButtonView = actionsView.findViewById(R.id.report_action);
             reportButtonView.setOnClickListener(new View.OnClickListener() {
                 @Override
@@ -344,6 +336,8 @@
             addToExistingContactButtonView.setVisibility(View.GONE);
         }
 
+        sendMessageView.setTag(IntentProvider.getSendSmsIntentProvider(number));
+
         mCallLogListItemHelper.setActionContentDescriptions(this);
     }
 
diff --git a/src/com/android/dialer/calllog/IntentProvider.java b/src/com/android/dialer/calllog/IntentProvider.java
index 9f5150a..d7a35e8 100644
--- a/src/com/android/dialer/calllog/IntentProvider.java
+++ b/src/com/android/dialer/calllog/IntentProvider.java
@@ -30,7 +30,7 @@
 import com.android.dialer.CallDetailActivity;
 import com.android.dialer.DialtactsActivity;
 import com.android.dialer.PhoneCallDetails;
-import com.android.dialer.util.CallIntentUtil;
+import com.android.dialer.util.IntentUtil;
 import com.android.dialer.util.TelecomUtil;
 
 import java.util.ArrayList;
@@ -55,7 +55,7 @@
         return new IntentProvider() {
             @Override
             public Intent getIntent(Context context) {
-                return CallIntentUtil.getCallIntent(number, accountHandle);
+                return IntentUtil.getCallIntent(number, accountHandle);
             }
         };
     }
@@ -69,7 +69,7 @@
         return new IntentProvider() {
             @Override
             public Intent getIntent(Context context) {
-                return CallIntentUtil.getVideoCallIntent(number, accountHandle);
+                return IntentUtil.getVideoCallIntent(number, accountHandle);
             }
         };
     }
@@ -78,7 +78,7 @@
         return new IntentProvider() {
             @Override
             public Intent getIntent(Context context) {
-                return CallIntentUtil.getVoicemailIntent();
+                return IntentUtil.getVoicemailIntent();
             }
         };
     }
@@ -101,6 +101,15 @@
         };
     }
 
+    public static IntentProvider getSendSmsIntentProvider(final String number) {
+        return new IntentProvider() {
+            @Override
+            public Intent getIntent(Context context) {
+                return IntentUtil.getSendSmsIntent(number);
+            }
+        };
+    }
+
     /**
      * Retrieves the call details intent provider for an entry in the call log.
      *
@@ -157,11 +166,9 @@
                     // Note: This code mirrors code in Contacts/QuickContactsActivity.
                     final Intent intent;
                     if (isNewContact) {
-                        intent = new Intent(
-                                Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
+                        intent = IntentUtil.getNewContactIntent();
                     } else {
-                        intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
-                        intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
+                        intent = IntentUtil.getAddToExistingContactIntent();
                     }
 
                     ArrayList<ContentValues> values = contactToSave.getContentValues();
@@ -198,14 +205,9 @@
                 } else {
                     // If no lookup uri is provided, rely on the available phone number and name.
                     if (isNewContact) {
-                        return DialtactsActivity.getAddToContactIntent(name, number, numberType);
+                        return IntentUtil.getNewContactIntent(name, number, numberType);
                     } else {
-                        Intent intent = new Intent(
-                                Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
-                        intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
-                        intent.putExtra(ContactsContract.Intents.Insert.PHONE, number);
-                        intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, numberType);
-                        return intent;
+                        return IntentUtil.getAddToExistingContactIntent(name, number, numberType);
                     }
                 }
             }
diff --git a/src/com/android/dialer/dialpad/DialpadFragment.java b/src/com/android/dialer/dialpad/DialpadFragment.java
index 6a66eb3..229af38 100644
--- a/src/com/android/dialer/dialpad/DialpadFragment.java
+++ b/src/com/android/dialer/dialpad/DialpadFragment.java
@@ -78,7 +78,7 @@
 import com.android.dialer.R;
 import com.android.dialer.SpecialCharSequenceMgr;
 import com.android.dialer.calllog.PhoneAccountUtils;
-import com.android.dialer.util.CallIntentUtil;
+import com.android.dialer.util.IntentUtil;
 import com.android.dialer.util.DialerUtils;
 import com.android.phone.common.CallLogAsync;
 import com.android.phone.common.HapticFeedback;
@@ -1001,8 +1001,7 @@
     }
 
     public void callVoicemail() {
-        DialerUtils.startActivityWithErrorToast(getActivity(), CallIntentUtil
-                .getVoicemailIntent());
+        DialerUtils.startActivityWithErrorToast(getActivity(), IntentUtil.getVoicemailIntent());
         hideAndClearDialpad(false);
     }
 
@@ -1098,7 +1097,7 @@
                 // Clear the digits just in case.
                 clearDialpad();
             } else {
-                final Intent intent = CallIntentUtil.getCallIntent(number,
+                final Intent intent = IntentUtil.getCallIntent(number,
                         (getActivity() instanceof DialtactsActivity ?
                                 ((DialtactsActivity) getActivity()).getCallOrigin() : null));
                 DialerUtils.startActivityWithErrorToast(getActivity(), intent);
@@ -1616,7 +1615,7 @@
     }
 
     private Intent newFlashIntent() {
-        final Intent intent = CallIntentUtil.getCallIntent(EMPTY_NUMBER);
+        final Intent intent = IntentUtil.getCallIntent(EMPTY_NUMBER);
         intent.putExtra(EXTRA_SEND_EMPTY_FLASH, true);
         return intent;
     }
diff --git a/src/com/android/dialer/interactions/PhoneNumberInteraction.java b/src/com/android/dialer/interactions/PhoneNumberInteraction.java
index 8d6de71..8455f24 100644
--- a/src/com/android/dialer/interactions/PhoneNumberInteraction.java
+++ b/src/com/android/dialer/interactions/PhoneNumberInteraction.java
@@ -52,7 +52,7 @@
 import com.android.contacts.common.util.ContactDisplayUtils;
 import com.android.dialer.R;
 import com.android.dialer.contact.ContactUpdateService;
-import com.android.dialer.util.CallIntentUtil;
+import com.android.dialer.util.IntentUtil;
 import com.android.dialer.util.DialerUtils;
 
 import com.google.common.annotations.VisibleForTesting;
@@ -322,7 +322,7 @@
                         Intent.ACTION_SENDTO, Uri.fromParts("sms", phoneNumber, null));
                 break;
             default:
-                intent = CallIntentUtil.getCallIntent(phoneNumber, callOrigin);
+                intent = IntentUtil.getCallIntent(phoneNumber, callOrigin);
                 break;
         }
         DialerUtils.startActivityWithErrorToast(context, intent);
diff --git a/src/com/android/dialer/list/SearchFragment.java b/src/com/android/dialer/list/SearchFragment.java
index 73127a2..c314478 100644
--- a/src/com/android/dialer/list/SearchFragment.java
+++ b/src/com/android/dialer/list/SearchFragment.java
@@ -40,14 +40,13 @@
 import com.android.contacts.common.util.ViewUtil;
 import com.android.contacts.commonbind.analytics.AnalyticsUtil;
 import com.android.dialer.dialpad.DialpadFragment.ErrorDialogFragment;
-import com.android.dialer.DialtactsActivity;
 import com.android.dialer.R;
 import com.android.dialer.util.DialerUtils;
+import com.android.dialer.util.IntentUtil;
 import com.android.phone.common.animation.AnimUtils;
 
 public class SearchFragment extends PhoneNumberPickerFragment {
     private static final String TAG  = SearchFragment.class.getSimpleName();
-    private static final String SMS_URI_PREFIX = "sms:";
 
     private OnListFragmentScrolledListener mActivityScrollListener;
 
@@ -216,7 +215,7 @@
                 super.onItemClick(position, id);
                 break;
             case DialerPhoneNumberListAdapter.SHORTCUT_DIRECT_CALL:
-                number = adapter.getQueryString(); 
+                number = adapter.getQueryString();
                 listener = getOnPhoneNumberPickerListener();
                 if (listener != null && !checkForProhibitedPhoneNumber(number)) {
                     listener.onCallNumberDirectly(number);
@@ -225,26 +224,23 @@
             case DialerPhoneNumberListAdapter.SHORTCUT_CREATE_NEW_CONTACT:
                 number = TextUtils.isEmpty(mAddToContactNumber) ?
                         adapter.getFormattedQueryString() : mAddToContactNumber;
-                intent = new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
-                intent.putExtra(ContactsContract.Intents.Insert.PHONE, number);
-                intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE,
-                        ContactsContract.CommonDataKinds.Phone.TYPE_MAIN);
+                intent = IntentUtil.getNewContactIntent(number);
                 DialerUtils.startActivityWithErrorToast(getActivity(), intent);
                 break;
             case DialerPhoneNumberListAdapter.SHORTCUT_ADD_TO_EXISTING_CONTACT:
                 number = TextUtils.isEmpty(mAddToContactNumber) ?
                         adapter.getFormattedQueryString() : mAddToContactNumber;
-                intent = DialtactsActivity.getAddNumberToContactIntent(number);
+                intent = IntentUtil.getAddToExistingContactIntent(number);
                 DialerUtils.startActivityWithErrorToast(getActivity(), intent,
                         R.string.add_contact_not_available);
                 break;
             case DialerPhoneNumberListAdapter.SHORTCUT_SEND_SMS_MESSAGE:
-                intent = new Intent(
-                        Intent.ACTION_VIEW, Uri.parse(SMS_URI_PREFIX + getQueryString()));
+                number = adapter.getFormattedQueryString();
+                intent = IntentUtil.getSendSmsIntent(number);
                 DialerUtils.startActivityWithErrorToast(getActivity(), intent);
                 break;
             case DialerPhoneNumberListAdapter.SHORTCUT_MAKE_VIDEO_CALL:
-                number = adapter.getQueryString(); 
+                number = adapter.getQueryString();
                 listener = getOnPhoneNumberPickerListener();
                 if (listener != null && !checkForProhibitedPhoneNumber(number)) {
                     listener.onCallNumberDirectly(number, true /* isVideoCall */);
diff --git a/src/com/android/dialer/util/DialerUtils.java b/src/com/android/dialer/util/DialerUtils.java
index 3899b37..a04719a 100644
--- a/src/com/android/dialer/util/DialerUtils.java
+++ b/src/com/android/dialer/util/DialerUtils.java
@@ -71,7 +71,7 @@
      */
     public static void startActivityWithErrorToast(Context context, Intent intent, int msgId) {
         try {
-            if ((CallIntentUtil.CALL_ACTION.equals(intent.getAction())
+            if ((IntentUtil.CALL_ACTION.equals(intent.getAction())
                             && context instanceof Activity)) {
                 // All dialer-initiated calls should pass the touch point to the InCallUI
                 Point touchPoint = TouchPointManager.getInstance().getPoint();
diff --git a/src/com/android/dialer/util/CallIntentUtil.java b/src/com/android/dialer/util/IntentUtil.java
similarity index 69%
rename from src/com/android/dialer/util/CallIntentUtil.java
rename to src/com/android/dialer/util/IntentUtil.java
index fac26f7..263d3cd 100644
--- a/src/com/android/dialer/util/CallIntentUtil.java
+++ b/src/com/android/dialer/util/IntentUtil.java
@@ -18,6 +18,7 @@
 
 import android.content.Intent;
 import android.net.Uri;
+import android.provider.ContactsContract;
 import android.telecom.PhoneAccount;
 import android.telecom.PhoneAccountHandle;
 import android.telecom.TelecomManager;
@@ -27,11 +28,12 @@
 import com.android.phone.common.PhoneConstants;
 
 /**
- * Utilities for creation of {@link Intent#ACTION_CALL} intents.
+ * Utilities for creation of intents in Dialer, such as {@link Intent#ACTION_CALL}.
  */
-public class CallIntentUtil {
+public class IntentUtil {
 
     public static final String CALL_ACTION = Intent.ACTION_CALL;
+    private static final String SMS_URI_PREFIX = "sms:";
 
     /**
      * Return an Intent for making a phone call. Scheme (e.g. tel, sip) will be determined
@@ -141,4 +143,59 @@
 
         return intent;
     }
+
+    public static Intent getSendSmsIntent(CharSequence phoneNumber) {
+        return new Intent(Intent.ACTION_VIEW, Uri.parse(SMS_URI_PREFIX + phoneNumber));
+    }
+
+    public static Intent getNewContactIntent() {
+        return new Intent(Intent.ACTION_INSERT, ContactsContract.Contacts.CONTENT_URI);
+    }
+
+    public static Intent getNewContactIntent(CharSequence phoneNumber) {
+        return getNewContactIntent(
+                null /* name */,
+                phoneNumber /* phoneNumber */,
+                -1 /* phoneNumberType */);
+    }
+
+    public static Intent getNewContactIntent(
+            CharSequence name, CharSequence phoneNumber, int phoneNumberType) {
+        Intent intent = getNewContactIntent();
+        populateContactIntent(intent, name, phoneNumber, phoneNumberType);
+        return intent;
+    }
+
+    public static Intent getAddToExistingContactIntent() {
+        Intent intent = new Intent(Intent.ACTION_INSERT_OR_EDIT);
+        intent.setType(ContactsContract.Contacts.CONTENT_ITEM_TYPE);
+        return intent;
+    }
+
+    public static Intent getAddToExistingContactIntent(CharSequence phoneNumber) {
+        return getAddToExistingContactIntent(
+                null /* name */,
+                phoneNumber /* phoneNumber */,
+                -1 /* phoneNumberType */);
+    }
+
+    public static Intent getAddToExistingContactIntent(
+            CharSequence name, CharSequence phoneNumber, int phoneNumberType) {
+        Intent intent = getAddToExistingContactIntent();
+        populateContactIntent(intent, name, phoneNumber, phoneNumberType);
+        return intent;
+    }
+
+    private static void populateContactIntent(
+            Intent intent, CharSequence name, CharSequence phoneNumber, int phoneNumberType) {
+        if (phoneNumber != null) {
+            intent.putExtra(ContactsContract.Intents.Insert.PHONE, phoneNumber);
+        }
+        if (name != null) {
+            intent.putExtra(ContactsContract.Intents.Insert.NAME, name);
+        }
+        if (phoneNumberType != -1) {
+            intent.putExtra(ContactsContract.Intents.Insert.PHONE_TYPE, phoneNumberType);
+        }
+    }
 }