Home | History | Annotate | Download | only in transaction
      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
     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 
     51             SmsMessage message = updateMessageStatus(context, messageUri, pdu);
     52 
     53             // Called on the UI thread so don't block.
     54             if (message.getStatus() < Sms.STATUS_PENDING)
     55                 MessagingNotification.nonBlockingUpdateNewMessageIndicator(context,
     56                         true, message.isStatusReportMessage());
     57        }
     58     }
     59 
     60     private SmsMessage updateMessageStatus(Context context, Uri messageUri, byte[] pdu) {
     61         // Create a "status/#" URL and use it to update the
     62         // message's status in the database.
     63         Cursor cursor = SqliteWrapper.query(context, context.getContentResolver(),
     64                             messageUri, ID_PROJECTION, null, null, null);
     65         SmsMessage message = SmsMessage.createFromPdu(pdu);
     66 
     67         try {
     68             if (cursor.moveToFirst()) {
     69                 int messageId = cursor.getInt(0);
     70 
     71                 Uri updateUri = ContentUris.withAppendedId(STATUS_URI, messageId);
     72                 int status = message.getStatus();
     73                 boolean isStatusReport = message.isStatusReportMessage();
     74                 ContentValues contentValues = new ContentValues(1);
     75 
     76                 if (Log.isLoggable(LogTag.TAG, Log.DEBUG)) {
     77                     log("updateMessageStatus: msgUrl=" + messageUri + ", status=" + status +
     78                             ", isStatusReport=" + isStatusReport);
     79                 }
     80 
     81                 contentValues.put(Sms.STATUS, status);
     82                 SqliteWrapper.update(context, context.getContentResolver(),
     83                                     updateUri, contentValues, null, null);
     84             } else {
     85                 error("Can't find message for status update: " + messageUri);
     86             }
     87         } finally {
     88             cursor.close();
     89         }
     90         return message;
     91     }
     92 
     93     private void error(String message) {
     94         Log.e(LOG_TAG, "[MessageStatusReceiver] " + message);
     95     }
     96 
     97     private void log(String message) {
     98         Log.d(LOG_TAG, "[MessageStatusReceiver] " + message);
     99     }
    100 }
    101