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
18package com.android.mms.transaction;
19
20import java.io.IOException;
21
22import android.content.Context;
23import android.net.Uri;
24import android.provider.Telephony.Mms.Sent;
25import android.util.Log;
26
27import com.android.mms.LogTag;
28import com.android.mms.ui.MessageUtils;
29import com.google.android.mms.MmsException;
30import com.google.android.mms.pdu.EncodedStringValue;
31import com.google.android.mms.pdu.PduComposer;
32import com.google.android.mms.pdu.PduPersister;
33import com.google.android.mms.pdu.ReadRecInd;
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 */
46public class ReadRecTransaction extends Transaction implements Runnable{
47    private static final String TAG = LogTag.TAG;
48    private static final boolean DEBUG = false;
49    private static final boolean LOCAL_LOGV = false;
50
51    private Thread mThread;
52    private final Uri mReadReportURI;
53
54    public ReadRecTransaction(Context context,
55            int transId,
56            TransactionSettings connectionSettings,
57            String uri) {
58        super(context, transId, connectionSettings);
59        mReadReportURI = Uri.parse(uri);
60        mId = uri;
61
62        // Attach the transaction to the instance of RetryScheduler.
63        attach(RetryScheduler.getInstance(context));
64    }
65
66    /*
67     * (non-Javadoc)
68     * @see com.android.mms.Transaction#process()
69     */
70    @Override
71    public void process() {
72        mThread = new Thread(this, "ReadRecTransaction");
73        mThread.start();
74    }
75
76    public void run() {
77        PduPersister persister = PduPersister.getPduPersister(mContext);
78
79        try {
80            // Load M-read-rec.ind from outbox
81            ReadRecInd readRecInd = (ReadRecInd) persister.load(mReadReportURI);
82
83            // insert the 'from' address per spec
84            String lineNumber = MessageUtils.getLocalNumber();
85            readRecInd.setFrom(new EncodedStringValue(lineNumber));
86
87            // Pack M-read-rec.ind and send it
88            byte[] postingData = new PduComposer(mContext, readRecInd).make();
89            sendPdu(postingData);
90
91            Uri uri = persister.move(mReadReportURI, Sent.CONTENT_URI);
92            mTransactionState.setState(TransactionState.SUCCESS);
93            mTransactionState.setContentUri(uri);
94        } catch (IOException e) {
95            if (LOCAL_LOGV) {
96                Log.v(TAG, "Failed to send M-Read-Rec.Ind.", e);
97            }
98        } catch (MmsException e) {
99            if (LOCAL_LOGV) {
100                Log.v(TAG, "Failed to load message from Outbox.", e);
101            }
102        } catch (RuntimeException e) {
103            if (LOCAL_LOGV) {
104                Log.e(TAG, "Unexpected RuntimeException.", e);
105            }
106        } finally {
107            if (mTransactionState.getState() != TransactionState.SUCCESS) {
108                mTransactionState.setState(TransactionState.FAILED);
109                mTransactionState.setContentUri(mReadReportURI);
110            }
111            notifyObservers();
112        }
113    }
114
115    @Override
116    public int getType() {
117        return READREC_TRANSACTION;
118    }
119}
120