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 {
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 final Uri mReadReportURI;
51
52    public ReadRecTransaction(Context context,
53            int transId,
54            TransactionSettings connectionSettings,
55            String uri) {
56        super(context, transId, connectionSettings);
57        mReadReportURI = Uri.parse(uri);
58        mId = uri;
59
60        // Attach the transaction to the instance of RetryScheduler.
61        attach(RetryScheduler.getInstance(context));
62    }
63
64    /*
65     * (non-Javadoc)
66     * @see com.android.mms.Transaction#process()
67     */
68    @Override
69    public void process() {
70        PduPersister persister = PduPersister.getPduPersister(mContext);
71
72        try {
73            // Load M-read-rec.ind from outbox
74            ReadRecInd readRecInd = (ReadRecInd) persister.load(mReadReportURI);
75
76            // insert the 'from' address per spec
77            String lineNumber = MessageUtils.getLocalNumber();
78            readRecInd.setFrom(new EncodedStringValue(lineNumber));
79
80            // Pack M-read-rec.ind and send it
81            byte[] postingData = new PduComposer(mContext, readRecInd).make();
82            sendPdu(postingData);
83
84            Uri uri = persister.move(mReadReportURI, Sent.CONTENT_URI);
85            mTransactionState.setState(TransactionState.SUCCESS);
86            mTransactionState.setContentUri(uri);
87        } catch (IOException e) {
88            if (LOCAL_LOGV) {
89                Log.v(TAG, "Failed to send M-Read-Rec.Ind.", e);
90            }
91        } catch (MmsException e) {
92            if (LOCAL_LOGV) {
93                Log.v(TAG, "Failed to load message from Outbox.", e);
94            }
95        } catch (RuntimeException e) {
96            if (LOCAL_LOGV) {
97                Log.e(TAG, "Unexpected RuntimeException.", e);
98            }
99        } finally {
100            if (mTransactionState.getState() != TransactionState.SUCCESS) {
101                mTransactionState.setState(TransactionState.FAILED);
102                mTransactionState.setContentUri(mReadReportURI);
103            }
104            notifyObservers();
105        }
106    }
107
108    @Override
109    public int getType() {
110        return READREC_TRANSACTION;
111    }
112}
113