1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.mms.ui;
18 
19 import android.app.IntentService;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.os.Bundle;
23 import android.telephony.TelephonyManager;
24 import android.text.TextUtils;
25 import android.util.Log;
26 
27 import com.android.mms.LogTag;
28 import com.android.mms.MmsConfig;
29 import com.android.mms.data.Conversation;
30 import com.android.mms.transaction.SmsMessageSender;
31 
32 /**
33  * Respond to a special intent and send an SMS message without the user's intervention.
34  */
35 public class NoConfirmationSendService extends IntentService {
NoConfirmationSendService()36     public NoConfirmationSendService() {
37         // Class name will be the thread name.
38         super(NoConfirmationSendService.class.getName());
39 
40         // Intent should be redelivered if the process gets killed before completing the job.
41         setIntentRedelivery(true);
42     }
43 
44     private static final String TAG = LogTag.TAG;
45 
46     @Override
onHandleIntent(Intent intent)47     protected void onHandleIntent(Intent intent) {
48         ComposeMessageActivity.log("NoConfirmationSendService onHandleIntent");
49 
50         if (!MmsConfig.isSmsEnabled(this)) {
51             ComposeMessageActivity.log("NoConfirmationSendService is not the default sms app");
52             return;
53         }
54 
55         String action = intent.getAction();
56         if (!TelephonyManager.ACTION_RESPOND_VIA_MESSAGE.equals(action)) {
57             ComposeMessageActivity.log("NoConfirmationSendService onHandleIntent wrong action: " +
58                     action);
59             return;
60         }
61         Bundle extras = intent.getExtras();
62         if (extras == null) {
63             ComposeMessageActivity.log("Called to send SMS but no extras");
64             return;
65         }
66 
67         String message = extras.getString(Intent.EXTRA_TEXT);
68 
69         Uri intentUri = intent.getData();
70         String recipients = Conversation.getRecipients(intentUri);
71 
72         if (TextUtils.isEmpty(recipients)) {
73             ComposeMessageActivity.log("Recipient(s) cannot be empty");
74             return;
75         }
76         if (extras.getBoolean("showUI", false)) {
77             intent.setClassName(this, "com.android.mms.ui.ComposeMessageActivityNoLockScreen");
78             intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
79             startActivity(intent);
80         } else {
81             if (TextUtils.isEmpty(message)) {
82                 ComposeMessageActivity.log("Message cannot be empty");
83                 return;
84             }
85             String[] dests = TextUtils.split(recipients, ";");
86 
87             // Using invalid threadId 0 here. When the message is inserted into the db, the
88             // provider looks up the threadId based on the recipient(s).
89             long threadId = 0;
90             SmsMessageSender smsMessageSender = new SmsMessageSender(this, dests,
91                     message, threadId);
92             try {
93                 // This call simply puts the message on a queue and sends a broadcast to start
94                 // a service to send the message. In queing up the message, however, it does
95                 // insert the message into the DB.
96                 smsMessageSender.sendMessage(threadId);
97             } catch (Exception e) {
98                 Log.e(TAG, "Failed to send SMS message, threadId=" + threadId, e);
99             }
100         }
101     }
102 }
103