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