Update People app to direct SMS SEND_TO only to the default SMS app.

In KLP we have added the concept of a default SMS app to the platform. Only
this application has write permission to the SMS database. To help ensure
a better user experience we will always direct "send to sms" in the people
app to the app that the user has selected as their default SMS app.

Bug: 10870624 System should direct "SENDTO" intent with "sms" and "mms" schemes to the default SMS app
Change-Id: Ic35282b52d0f12baa11b57dc1b3ae8556ecb9a5a
diff --git a/src/com/android/contacts/detail/ContactDetailFragment.java b/src/com/android/contacts/detail/ContactDetailFragment.java
index e48d248..34583dc 100644
--- a/src/com/android/contacts/detail/ContactDetailFragment.java
+++ b/src/com/android/contacts/detail/ContactDetailFragment.java
@@ -19,6 +19,7 @@
 import android.app.Activity;
 import android.app.Fragment;
 import android.app.SearchManager;
+import android.content.ComponentName;
 import android.content.ContentUris;
 import android.content.ContentValues;
 import android.content.Context;
@@ -169,21 +170,6 @@
     };
 
     /**
-     * Device capability: Set during buildEntries and used in the long-press context menu
-     */
-    private boolean mHasPhone;
-
-    /**
-     * Device capability: Set during buildEntries and used in the long-press context menu
-     */
-    private boolean mHasSms;
-
-    /**
-     * Device capability: Set during buildEntries and used in the long-press context menu
-     */
-    private boolean mHasSip;
-
-    /**
      * The view shown if the detail list is empty.
      * We set this to the list view when first bind the adapter, so that it won't be shown while
      * we're loading data.
@@ -533,9 +519,10 @@
      * Build up the entries to display on the screen.
      */
     private final void buildEntries() {
-        mHasPhone = PhoneCapabilityTester.isPhone(mContext);
-        mHasSms = PhoneCapabilityTester.isSmsIntentRegistered(mContext);
-        mHasSip = PhoneCapabilityTester.isSipPhone(mContext);
+        final boolean hasPhone = PhoneCapabilityTester.isPhone(mContext);
+        final ComponentName smsComponent = PhoneCapabilityTester.getSmsComponent(getContext());
+        final boolean hasSms = (smsComponent != null);
+        final boolean hasSip = PhoneCapabilityTester.isSipPhone(mContext);
 
         // Clear out the old entries
         mAllEntries.clear();
@@ -583,21 +570,25 @@
                     PhoneDataItem phone = (PhoneDataItem) dataItem;
                     // Build phone entries
                     entry.data = phone.getFormattedPhoneNumber();
-                    final Intent phoneIntent = mHasPhone ?
+                    final Intent phoneIntent = hasPhone ?
                             CallUtil.getCallIntent(entry.data) : null;
-                    final Intent smsIntent = mHasSms ? new Intent(Intent.ACTION_SENDTO,
-                            Uri.fromParts(CallUtil.SCHEME_SMSTO, entry.data, null)) : null;
+                    Intent smsIntent = null;
+                    if (hasSms) {
+                        smsIntent = new Intent(Intent.ACTION_SENDTO,
+                                Uri.fromParts(CallUtil.SCHEME_SMSTO, entry.data, null));
+                        smsIntent.setComponent(smsComponent);
+                    }
 
                     // Configure Icons and Intents.
-                    if (mHasPhone && mHasSms) {
+                    if (hasPhone && hasSms) {
                         entry.intent = phoneIntent;
                         entry.secondaryIntent = smsIntent;
                         entry.secondaryActionIcon = kind.iconAltRes;
                         entry.secondaryActionDescription =
                             ContactDisplayUtils.getSmsLabelResourceId(entry.type);
-                    } else if (mHasPhone) {
+                    } else if (hasPhone) {
                         entry.intent = phoneIntent;
-                    } else if (mHasSms) {
+                    } else if (hasSms) {
                         entry.intent = smsIntent;
                     } else {
                         entry.intent = null;
@@ -694,7 +685,7 @@
                 } else if (dataItem instanceof SipAddressDataItem && hasData) {
                     // Build SipAddress entries
                     entry.uri = null;
-                    if (mHasSip) {
+                    if (hasSip) {
                         entry.intent = CallUtil.getCallIntent(
                                 Uri.fromParts(CallUtil.SCHEME_SIP, entry.data, null));
                     } else {
diff --git a/src/com/android/contacts/quickcontact/DataAction.java b/src/com/android/contacts/quickcontact/DataAction.java
index 0750a1d..0e3495c 100644
--- a/src/com/android/contacts/quickcontact/DataAction.java
+++ b/src/com/android/contacts/quickcontact/DataAction.java
@@ -16,6 +16,7 @@
 
 package com.android.contacts.quickcontact;
 
+import android.content.ComponentName;
 import android.content.ContentUris;
 import android.content.Context;
 import android.content.Intent;
@@ -102,7 +103,8 @@
         mDataUri = ContentUris.withAppendedId(Data.CONTENT_URI, mDataId);
 
         final boolean hasPhone = PhoneCapabilityTester.isPhone(mContext);
-        final boolean hasSms = PhoneCapabilityTester.isSmsIntentRegistered(mContext);
+        final ComponentName smsComponent = PhoneCapabilityTester.getSmsComponent(mContext);
+        final boolean hasSms = (smsComponent != null);
 
         // Handle well-known MIME-types with special care
         if (item instanceof PhoneDataItem) {
@@ -113,8 +115,12 @@
 
                     final Intent phoneIntent = hasPhone ? CallUtil.getCallIntent(number)
                             : null;
-                    final Intent smsIntent = hasSms ? new Intent(Intent.ACTION_SENDTO,
-                            Uri.fromParts(CallUtil.SCHEME_SMSTO, number, null)) : null;
+                    Intent smsIntent = null;
+                    if (hasSms) {
+                        smsIntent = new Intent(Intent.ACTION_SENDTO,
+                                Uri.fromParts(CallUtil.SCHEME_SMSTO, number, null));
+                        smsIntent.setComponent(smsComponent);
+                    }
 
                     // Configure Icons and Intents. Notice actionIcon is already set to the phone
                     if (hasPhone && hasSms) {
diff --git a/src/com/android/contacts/util/PhoneCapabilityTester.java b/src/com/android/contacts/util/PhoneCapabilityTester.java
index 5f73418..e20a076 100644
--- a/src/com/android/contacts/util/PhoneCapabilityTester.java
+++ b/src/com/android/contacts/util/PhoneCapabilityTester.java
@@ -16,6 +16,7 @@
 
 package com.android.contacts.util;
 
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.pm.PackageManager;
@@ -23,6 +24,7 @@
 import android.net.Uri;
 import android.net.sip.SipManager;
 import android.provider.MediaStore;
+import android.provider.Telephony;
 import android.telephony.TelephonyManager;
 
 import com.android.contacts.common.CallUtil;
@@ -75,13 +77,22 @@
     }
 
     /**
-     * Returns true if the device has an SMS application installed.
+     * Returns the component name to use for sending to sms or null.
      */
-    public static boolean isSmsIntentRegistered(Context context) {
-        // Don't cache the result as the user might install third party apps to send SMS
-        final Intent intent = new Intent(Intent.ACTION_SENDTO,
-                Uri.fromParts(CallUtil.SCHEME_SMSTO, "", null));
-        return isIntentRegistered(context, intent);
+    public static ComponentName getSmsComponent(Context context) {
+        String smsPackage = Telephony.Sms.getDefaultSmsPackage(context);
+        if (smsPackage != null) {
+            final PackageManager packageManager = context.getPackageManager();
+            final Intent intent = new Intent(Intent.ACTION_SENDTO,
+                    Uri.fromParts(CallUtil.SCHEME_SMSTO, "", null));
+            final List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
+            for (ResolveInfo resolveInfo : resolveInfos) {
+                if (smsPackage.equals(resolveInfo.activityInfo.packageName)) {
+                    return new ComponentName(smsPackage, resolveInfo.activityInfo.name);
+                }
+            }
+        }
+        return null;
     }
 
     /**