1/*
2 * Copyright (C) 2015 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.messaging.receiver;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.net.Uri;
23import android.telephony.SmsMessage;
24
25import com.android.messaging.datamodel.action.ProcessDeliveryReportAction;
26import com.android.messaging.datamodel.action.ProcessDownloadedMmsAction;
27import com.android.messaging.datamodel.action.ProcessSentMessageAction;
28import com.android.messaging.datamodel.data.ParticipantData;
29import com.android.messaging.sms.MmsUtils;
30import com.android.messaging.sms.SmsSender;
31import com.android.messaging.util.LogUtil;
32
33/**
34 * The SMS sent and delivery intent receiver.
35 *
36 * This class just simply forwards the intents to proper recipients for actual handling.
37 */
38public class SendStatusReceiver extends BroadcastReceiver {
39    public static final String MESSAGE_SENT_ACTION =
40            "com.android.messaging.receiver.SendStatusReceiver.MESSAGE_SENT";
41    public static final String MESSAGE_DELIVERED_ACTION =
42            "com.android.messaging.receiver.SendStatusReceiver.MESSAGE_DELIVERED";
43    public static final String MMS_SENT_ACTION =
44            "com.android.messaging.receiver.SendStatusReceiver.MMS_SENT";
45    public static final String MMS_DOWNLOADED_ACTION =
46            "com.android.messaging.receiver.SendStatusReceiver.MMS_DOWNLOADED";
47
48    // Defined by platform, but no constant provided. See docs for SmsManager.sendTextMessage.
49    public static final String EXTRA_ERROR_CODE = "errorCode";
50
51    public static final String EXTRA_PART_ID = "partId";
52    public static final String EXTRA_SUB_ID = "subId";
53
54    public static final int NO_ERROR_CODE = 0;
55    public static final int NO_PART_ID = -1;
56
57    @Override
58    public void onReceive(final Context context, final Intent intent) {
59        // This will be called on the main thread (so it should exit quickly)
60        final String action = intent.getAction();
61        final int resultCode = getResultCode();
62        if (MESSAGE_SENT_ACTION.equals(action)) {
63            final Uri requestId = intent.getData();
64            SmsSender.setResult(
65                    requestId,
66                    resultCode,
67                    intent.getIntExtra(EXTRA_ERROR_CODE, NO_ERROR_CODE),
68                    intent.getIntExtra(EXTRA_PART_ID, NO_PART_ID),
69                    intent.getIntExtra(EXTRA_SUB_ID, ParticipantData.DEFAULT_SELF_SUB_ID));
70        } else if (MMS_SENT_ACTION.equals(action)) {
71            final Uri messageUri = intent.getData();
72            ProcessSentMessageAction.processMmsSent(resultCode, messageUri,
73                    intent.getExtras());
74        } else if (MMS_DOWNLOADED_ACTION.equals(action)) {
75            ProcessDownloadedMmsAction.processMessageDownloaded(resultCode,
76                    intent.getExtras());
77        } else if (MESSAGE_DELIVERED_ACTION.equals(action)) {
78            final SmsMessage smsMessage = MmsUtils.getSmsMessageFromDeliveryReport(intent);
79            final Uri smsMessageUri = intent.getData();
80            if (smsMessage == null) {
81                LogUtil.e(LogUtil.BUGLE_TAG, "SendStatusReceiver: empty report message");
82                return;
83            }
84            int status = 0;
85            try {
86                status = smsMessage.getStatus();
87            } catch (final NullPointerException e) {
88                // Sometimes, SmsMessage.mWrappedSmsMessage is null causing NPE when we access
89                // the methods on it although the SmsMessage itself is not null.
90                LogUtil.e(LogUtil.BUGLE_TAG, "SendStatusReceiver: NPE inside SmsMessage");
91                return;
92            }
93            ProcessDeliveryReportAction.deliveryReportReceived(smsMessageUri, status);
94        }
95    }
96}
97