Home | History | Annotate | Download | only in transaction
      1 /*
      2  * Copyright (C) 2007-2008 Esmertec AG.
      3  * Copyright (C) 2007-2008 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 com.google.android.mms.MmsException;
     21 import com.google.android.mms.pdu.PduComposer;
     22 import com.google.android.mms.pdu.PduPersister;
     23 import com.google.android.mms.pdu.ReadRecInd;
     24 import com.google.android.mms.pdu.EncodedStringValue;
     25 import com.android.mms.ui.MessageUtils;
     26 
     27 import android.content.Context;
     28 import android.net.Uri;
     29 import android.provider.Telephony.Mms.Sent;
     30 import android.util.Config;
     31 import android.util.Log;
     32 
     33 import java.io.IOException;
     34 
     35 /**
     36  * The ReadRecTransaction is responsible for sending read report
     37  * notifications (M-read-rec.ind) to clients that have requested them.
     38  * It:
     39  *
     40  * <ul>
     41  * <li>Loads the read report indication from storage (Outbox).
     42  * <li>Packs M-read-rec.ind and sends it.
     43  * <li>Notifies the TransactionService about succesful completion.
     44  * </ul>
     45  */
     46 public class ReadRecTransaction extends Transaction {
     47     private static final String TAG = "ReadRecTransaction";
     48     private static final boolean DEBUG = false;
     49     private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV;
     50 
     51     private final Uri mReadReportURI;
     52 
     53     public ReadRecTransaction(Context context,
     54             int transId,
     55             TransactionSettings connectionSettings,
     56             String uri) {
     57         super(context, transId, connectionSettings);
     58         mReadReportURI = Uri.parse(uri);
     59         mId = uri;
     60 
     61         // Attach the transaction to the instance of RetryScheduler.
     62         attach(RetryScheduler.getInstance(context));
     63     }
     64 
     65     /*
     66      * (non-Javadoc)
     67      * @see com.android.mms.Transaction#process()
     68      */
     69     @Override
     70     public void process() {
     71         PduPersister persister = PduPersister.getPduPersister(mContext);
     72 
     73         try {
     74             // Load M-read-rec.ind from outbox
     75             ReadRecInd readRecInd = (ReadRecInd) persister.load(mReadReportURI);
     76 
     77             // insert the 'from' address per spec
     78             String lineNumber = MessageUtils.getLocalNumber();
     79             readRecInd.setFrom(new EncodedStringValue(lineNumber));
     80 
     81             // Pack M-read-rec.ind and send it
     82             byte[] postingData = new PduComposer(mContext, readRecInd).make();
     83             sendPdu(postingData);
     84 
     85             Uri uri = persister.move(mReadReportURI, Sent.CONTENT_URI);
     86             mTransactionState.setState(TransactionState.SUCCESS);
     87             mTransactionState.setContentUri(uri);
     88         } catch (IOException e) {
     89             if (LOCAL_LOGV) {
     90                 Log.v(TAG, "Failed to send M-Read-Rec.Ind.", e);
     91             }
     92         } catch (MmsException e) {
     93             if (LOCAL_LOGV) {
     94                 Log.v(TAG, "Failed to load message from Outbox.", e);
     95             }
     96         } catch (RuntimeException e) {
     97             if (LOCAL_LOGV) {
     98                 Log.e(TAG, "Unexpected RuntimeException.", e);
     99             }
    100         } finally {
    101             if (mTransactionState.getState() != TransactionState.SUCCESS) {
    102                 mTransactionState.setState(TransactionState.FAILED);
    103                 mTransactionState.setContentUri(mReadReportURI);
    104             }
    105             notifyObservers();
    106         }
    107     }
    108 
    109     @Override
    110     public int getType() {
    111         return READREC_TRANSACTION;
    112     }
    113 }
    114