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.ui.MessageUtils;
28import com.google.android.mms.MmsException;
29import com.google.android.mms.pdu.EncodedStringValue;
30import com.google.android.mms.pdu.PduComposer;
31import com.google.android.mms.pdu.PduPersister;
32import com.google.android.mms.pdu.ReadRecInd;
33
34/**
35 * The ReadRecTransaction is responsible for sending read report
36 * notifications (M-read-rec.ind) to clients that have requested them.
37 * It:
38 *
39 * <ul>
40 * <li>Loads the read report indication from storage (Outbox).
41 * <li>Packs M-read-rec.ind and sends it.
42 * <li>Notifies the TransactionService about succesful completion.
43 * </ul>
44 */
45public class ReadRecTransaction extends Transaction implements Runnable{
46    private static final String TAG = "ReadRecTransaction";
47    private static final boolean DEBUG = false;
48    private static final boolean LOCAL_LOGV = false;
49
50    private Thread mThread;
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        mThread = new Thread(this, "ReadRecTransaction");
72        mThread.start();
73    }
74
75    public void run() {
76        PduPersister persister = PduPersister.getPduPersister(mContext);
77
78        try {
79            // Load M-read-rec.ind from outbox
80            ReadRecInd readRecInd = (ReadRecInd) persister.load(mReadReportURI);
81
82            // insert the 'from' address per spec
83            String lineNumber = MessageUtils.getLocalNumber();
84            readRecInd.setFrom(new EncodedStringValue(lineNumber));
85
86            // Pack M-read-rec.ind and send it
87            byte[] postingData = new PduComposer(mContext, readRecInd).make();
88            sendPdu(postingData);
89
90            Uri uri = persister.move(mReadReportURI, Sent.CONTENT_URI);
91            mTransactionState.setState(TransactionState.SUCCESS);
92            mTransactionState.setContentUri(uri);
93        } catch (IOException e) {
94            if (LOCAL_LOGV) {
95                Log.v(TAG, "Failed to send M-Read-Rec.Ind.", e);
96            }
97        } catch (MmsException e) {
98            if (LOCAL_LOGV) {
99                Log.v(TAG, "Failed to load message from Outbox.", e);
100            }
101        } catch (RuntimeException e) {
102            if (LOCAL_LOGV) {
103                Log.e(TAG, "Unexpected RuntimeException.", e);
104            }
105        } finally {
106            if (mTransactionState.getState() != TransactionState.SUCCESS) {
107                mTransactionState.setState(TransactionState.FAILED);
108                mTransactionState.setContentUri(mReadReportURI);
109            }
110            notifyObservers();
111        }
112    }
113
114    @Override
115    public int getType() {
116        return READREC_TRANSACTION;
117    }
118}
119