Use SmsManager for reject call with SMS

Instead of starting a service to send SMS when a call is
rejected, we now use the SmsManager#sendTextMessage API.

Bug: 62048610
Test: Reject call and send SMS
Change-Id: I8d648ab5c3e35539d1387ece4d11acc1c30c8ea2
(cherry picked from commit 90660b5f9a058c452aa41478feac23f656bb206c)
diff --git a/src/com/android/server/telecom/RespondViaSmsManager.java b/src/com/android/server/telecom/RespondViaSmsManager.java
index f107956..88d4919 100644
--- a/src/com/android/server/telecom/RespondViaSmsManager.java
+++ b/src/com/android/server/telecom/RespondViaSmsManager.java
@@ -18,15 +18,12 @@
 
 // TODO: Needed for move to system service: import com.android.internal.R;
 import com.android.internal.os.SomeArgs;
-import com.android.internal.telephony.PhoneConstants;
 import com.android.internal.telephony.SmsApplication;
 
 import android.content.ComponentName;
 import android.content.Context;
-import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.res.Resources;
-import android.net.Uri;
 import android.os.Handler;
 import android.os.Looper;
 import android.os.Message;
@@ -34,10 +31,11 @@
 import android.telecom.Log;
 import android.telecom.Response;
 import android.telephony.PhoneNumberUtils;
+import android.telephony.SmsManager;
 import android.telephony.SubscriptionManager;
-import android.telephony.TelephonyManager;
 import android.text.Spannable;
 import android.text.SpannableString;
+import android.text.TextUtils;
 import android.widget.Toast;
 
 import java.util.ArrayList;
@@ -177,29 +175,28 @@
      */
     private void rejectCallWithMessage(Context context, String phoneNumber, String textMessage,
             int subId) {
-        if (textMessage != null) {
-            final ComponentName component =
-                    SmsApplication.getDefaultRespondViaMessageApplication(context,
-                            true /*updateIfNeeded*/);
-            if (component != null) {
-                // Build and send the intent
-                final Uri uri = Uri.fromParts(Constants.SCHEME_SMSTO, phoneNumber, null);
-                final Intent intent = new Intent(TelephonyManager.ACTION_RESPOND_VIA_MESSAGE, uri);
-                intent.putExtra(Intent.EXTRA_TEXT, textMessage);
-                if (SubscriptionManager.isValidSubscriptionId(subId)) {
-                    intent.putExtra(PhoneConstants.SUBSCRIPTION_KEY, subId);
-                }
-                // Wakeup apps for the broadcast.
-                // TODO: Use SmsManager instead of an intent.
-                intent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
+        if (TextUtils.isEmpty(textMessage)) {
+            Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: empty text message. ");
+            return;
+        }
+        if (!SubscriptionManager.isValidSubscriptionId(subId)) {
+            Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: Invalid SubId: " +
+                    subId);
+            return;
+        }
 
-                SomeArgs args = SomeArgs.obtain();
-                args.arg1 = phoneNumber;
-                args.arg2 = context;
-                mHandler.obtainMessage(MSG_SHOW_SENT_TOAST, args).sendToTarget();
-                intent.setComponent(component);
-                context.startService(intent);
-            }
+        SmsManager smsManager = SmsManager.getSmsManagerForSubscriptionId(subId);
+        try {
+            smsManager.sendTextMessage(phoneNumber, null, textMessage, null /*sentIntent*/,
+                    null /*deliveryIntent*/);
+
+            SomeArgs args = SomeArgs.obtain();
+            args.arg1 = phoneNumber;
+            args.arg2 = context;
+            mHandler.obtainMessage(MSG_SHOW_SENT_TOAST, args).sendToTarget();
+        } catch (IllegalArgumentException e) {
+            Log.w(RespondViaSmsManager.this, "Couldn't send SMS message: " +
+                    e.getMessage());
         }
     }
 }