1/*
2 * Copyright (C) 2007 Esmertec AG.
3 * Copyright (C) 2007 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mms.transaction;
19
20import android.content.BroadcastReceiver;
21import android.content.ContentUris;
22import android.content.ContentValues;
23import android.content.Context;
24import android.content.Intent;
25import android.database.Cursor;
26import android.net.Uri;
27import android.provider.Telephony.Sms;
28import android.telephony.SmsMessage;
29import android.util.Log;
30
31import android.database.sqlite.SqliteWrapper;
32import com.android.mms.LogTag;
33
34public class MessageStatusReceiver extends BroadcastReceiver {
35    public static final String MESSAGE_STATUS_RECEIVED_ACTION =
36            "com.android.mms.transaction.MessageStatusReceiver.MESSAGE_STATUS_RECEIVED";
37    private static final String[] ID_PROJECTION = new String[] { Sms._ID };
38    private static final String LOG_TAG = "MessageStatusReceiver";
39    private static final Uri STATUS_URI =
40            Uri.parse("content://sms/status");
41
42    @Override
43    public void onReceive(Context context, Intent intent) {
44        if (MESSAGE_STATUS_RECEIVED_ACTION.equals(intent.getAction())) {
45
46            Uri messageUri = intent.getData();
47            byte[] pdu = (byte[]) intent.getExtra("pdu");
48            String format = intent.getStringExtra("format");
49
50            SmsMessage message = updateMessageStatus(context, messageUri, pdu, format);
51
52            // Called on the UI thread so don't block.
53            if (message != null && message.getStatus() < Sms.STATUS_PENDING)
54                MessagingNotification.nonBlockingUpdateNewMessageIndicator(context,
55                        true, message.isStatusReportMessage());
56       }
57    }
58
59    private SmsMessage updateMessageStatus(Context context, Uri messageUri, byte[] pdu,
60            String format) {
61        SmsMessage message = SmsMessage.createFromPdu(pdu, format);
62        if (message == null) {
63            return null;
64        }
65        // Create a "status/#" URL and use it to update the
66        // message's status in the database.
67        Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
68                            messageUri, ID_PROJECTION, null, null, null);
69
70        try {
71            if (cursor.moveToFirst()) {
72                int messageId = cursor.getInt(0);
73
74                Uri updateUri = ContentUris.withAppendedId(STATUS_URI, messageId);
75                int status = message.getStatus();
76                boolean isStatusReport = message.isStatusReportMessage();
77                ContentValues contentValues = new ContentValues(1);
78
79                if (Log.isLoggable(LogTag.TAG, Log.DEBUG)) {
80                    log("updateMessageStatus: msgUrl=" + messageUri + ", status=" + status +
81                            ", isStatusReport=" + isStatusReport);
82                }
83
84                contentValues.put(Sms.STATUS, status);
85                SqliteWrapper.update(context, context.getContentResolver(),
86                                    updateUri, contentValues, null, null);
87            } else {
88                error("Can't find message for status update: " + messageUri);
89            }
90        } finally {
91            cursor.close();
92        }
93        return message;
94    }
95
96    private void error(String message) {
97        Log.e(LOG_TAG, "[MessageStatusReceiver] " + message);
98    }
99
100    private void log(String message) {
101        Log.d(LOG_TAG, "[MessageStatusReceiver] " + message);
102    }
103}
104