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
17package com.android.mms.ui;
18
19import android.app.IntentService;
20import android.content.Intent;
21import android.net.Uri;
22import android.os.Bundle;
23import android.telephony.TelephonyManager;
24import android.text.TextUtils;
25import android.util.Log;
26
27import com.android.mms.data.Conversation;
28import com.android.mms.transaction.SmsMessageSender;
29
30/**
31 * Respond to a special intent and send an SMS message without the user's intervention.
32 */
33public class NoConfirmationSendService extends IntentService {
34    public NoConfirmationSendService() {
35        // Class name will be the thread name.
36        super(NoConfirmationSendService.class.getName());
37
38        // Intent should be redelivered if the process gets killed before completing the job.
39        setIntentRedelivery(true);
40    }
41
42    private static final String TAG = "Mms/NoConfirmationSendService";
43
44    @Override
45    protected void onHandleIntent(Intent intent) {
46        ComposeMessageActivity.log("NoConfirmationSendService onHandleIntent");
47
48        String action = intent.getAction();
49        if (!TelephonyManager.ACTION_RESPOND_VIA_MESSAGE.equals(action)) {
50            ComposeMessageActivity.log("NoConfirmationSendService onHandleIntent wrong action: " +
51                    action);
52            return;
53        }
54        Bundle extras = intent.getExtras();
55        if (extras == null) {
56            ComposeMessageActivity.log("Called to send SMS but no extras");
57            return;
58        }
59
60        String message = extras.getString(Intent.EXTRA_TEXT);
61
62        Uri intentUri = intent.getData();
63        String recipients = Conversation.getRecipients(intentUri);
64
65        if (TextUtils.isEmpty(recipients)) {
66            ComposeMessageActivity.log("Recipient(s) cannot be empty");
67            return;
68        }
69        if (extras.getBoolean("showUI", false)) {
70            intent.setClassName(this, "com.android.mms.ui.ComposeMessageActivityNoLockScreen");
71            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
72            startActivity(intent);
73        } else {
74            if (TextUtils.isEmpty(message)) {
75                ComposeMessageActivity.log("Message cannot be empty");
76                return;
77            }
78            String[] dests = TextUtils.split(recipients, ";");
79
80            // Using invalid threadId 0 here. When the message is inserted into the db, the
81            // provider looks up the threadId based on the recipient(s).
82            long threadId = 0;
83            SmsMessageSender smsMessageSender = new SmsMessageSender(this, dests,
84                    message, threadId);
85            try {
86                // This call simply puts the message on a queue and sends a broadcast to start
87                // a service to send the message. In queing up the message, however, it does
88                // insert the message into the DB.
89                smsMessageSender.sendMessage(threadId);
90            } catch (Exception e) {
91                Log.e(TAG, "Failed to send SMS message, threadId=" + threadId, e);
92            }
93        }
94    }
95}
96