NoConfirmationSendService.java revision 6d38b19aa9aef46afa855187f23e44d4c06f8878
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.Service;
20import android.content.Intent;
21import android.net.Uri;
22import android.os.Bundle;
23import android.os.IBinder;
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 Service {
34    public static final String SEND_NO_CONFIRM_INTENT_ACTION =
35        "com.android.mms.intent.action.SENDTO_NO_CONFIRMATION";
36    private static final String TAG = "Mms/NoConfirmationSendService";
37
38    @Override
39    public int onStartCommand(Intent intent, int flags, int startId) {
40        super.onStartCommand(intent, flags, startId);
41
42        ComposeMessageActivity.log("NoConfirmationSendService onStartCommand");
43
44        String action = intent.getAction();
45        if (!SEND_NO_CONFIRM_INTENT_ACTION.equals(action)) {
46            ComposeMessageActivity.log("NoConfirmationSendService onStartCommand wrong action: " +
47                    action);
48            stopSelf();
49            return START_NOT_STICKY;
50        }
51        Bundle extras = intent.getExtras();
52        if (extras == null) {
53            ComposeMessageActivity.log("Called to send SMS but no extras");
54            stopSelf();
55            return START_NOT_STICKY;
56        }
57
58        String message = extras.getString(Intent.EXTRA_TEXT);
59
60        Uri intentUri = intent.getData();
61        String recipients = Conversation.getRecipients(intentUri);
62
63        if (TextUtils.isEmpty(recipients) || TextUtils.isEmpty(message)) {
64            ComposeMessageActivity.log("Recipient(s) and/or message cannot be empty");
65            stopSelf();
66            return START_NOT_STICKY;
67        }
68        String[] dests = TextUtils.split(recipients, ";");
69
70        // Using invalid threadId 0 here. When the message is inserted into the db, the
71        // provider looks up the threadId based on the recipient(s).
72        long threadId = 0;
73        SmsMessageSender smsMessageSender = new SmsMessageSender(this, dests,
74                message, threadId);
75        try {
76            // This call simply puts the message on a queue and sends a broadcast to start
77            // a service to send the message. In queing up the message, however, it does
78            // insert the message into the DB.
79            smsMessageSender.sendMessage(threadId);
80        } catch (Exception e) {
81            Log.e(TAG, "Failed to send SMS message, threadId=" + threadId, e);
82        }
83
84        stopSelf();
85        return START_NOT_STICKY;
86    }
87
88    @Override
89    public IBinder onBind(Intent intent) {
90        return null;
91    }
92}
93