• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 package com.android.mms.transaction;
19 
20 import android.content.BroadcastReceiver;
21 import android.content.ContentUris;
22 import android.content.ContentValues;
23 import android.content.Context;
24 import android.content.Intent;
25 import android.database.Cursor;
26 import android.net.Uri;
27 import android.provider.Telephony.Sms;
28 import android.telephony.SmsMessage;
29 import android.util.Log;
30 
31 import android.database.sqlite.SqliteWrapper;
32 import com.android.mms.LogTag;
33 
34 public 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     private Context mContext;
42 
43     @Override
onReceive(Context context, Intent intent)44     public void onReceive(Context context, Intent intent) {
45         mContext = context;
46         if (MESSAGE_STATUS_RECEIVED_ACTION.equals(intent.getAction())) {
47 
48             Uri messageUri = intent.getData();
49             byte[] pdu = (byte[]) intent.getExtra("pdu");
50             String format = intent.getStringExtra("format");
51 
52             SmsMessage message = updateMessageStatus(context, messageUri, pdu, format);
53 
54             // Called on the UI thread so don't block.
55             if (message.getStatus() < Sms.STATUS_PENDING)
56                 MessagingNotification.nonBlockingUpdateNewMessageIndicator(context,
57                         true, message.isStatusReportMessage());
58        }
59     }
60 
updateMessageStatus(Context context, Uri messageUri, byte[] pdu, String format)61     private SmsMessage updateMessageStatus(Context context, Uri messageUri, byte[] pdu,
62             String format) {
63         // Create a "status/#" URL and use it to update the
64         // message's status in the database.
65         Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
66                             messageUri, ID_PROJECTION, null, null, null);
67         SmsMessage message = SmsMessage.createFromPdu(pdu, format);
68 
69         try {
70             if (cursor.moveToFirst()) {
71                 int messageId = cursor.getInt(0);
72 
73                 Uri updateUri = ContentUris.withAppendedId(STATUS_URI, messageId);
74                 int status = message.getStatus();
75                 boolean isStatusReport = message.isStatusReportMessage();
76                 ContentValues contentValues = new ContentValues(1);
77 
78                 if (Log.isLoggable(LogTag.TAG, Log.DEBUG)) {
79                     log("updateMessageStatus: msgUrl=" + messageUri + ", status=" + status +
80                             ", isStatusReport=" + isStatusReport);
81                 }
82 
83                 contentValues.put(Sms.STATUS, status);
84                 SqliteWrapper.update(context, context.getContentResolver(),
85                                     updateUri, contentValues, null, null);
86             } else {
87                 error("Can't find message for status update: " + messageUri);
88             }
89         } finally {
90             cursor.close();
91         }
92         return message;
93     }
94 
error(String message)95     private void error(String message) {
96         Log.e(LOG_TAG, "[MessageStatusReceiver] " + message);
97     }
98 
log(String message)99     private void log(String message) {
100         Log.d(LOG_TAG, "[MessageStatusReceiver] " + message);
101     }
102 }
103