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