172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project/*
272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * Copyright (C) 2007-2008 Esmertec AG.
372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * Copyright (C) 2007-2008 The Android Open Source Project
472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project *
572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * you may not use this file except in compliance with the License.
772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * You may obtain a copy of the License at
872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project *
972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
1072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project *
1172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * Unless required by applicable law or agreed to in writing, software
1272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
1372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * See the License for the specific language governing permissions and
1572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * limitations under the License.
1672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project */
1772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
1872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectpackage com.android.mms.transaction;
1972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
20f7e8281a223af6228e6399055a6197a1edd9bc3aTom Taylorimport com.google.android.mms.MmsException;
21f7e8281a223af6228e6399055a6197a1edd9bc3aTom Taylorimport com.google.android.mms.pdu.PduComposer;
22f7e8281a223af6228e6399055a6197a1edd9bc3aTom Taylorimport com.google.android.mms.pdu.PduPersister;
23f7e8281a223af6228e6399055a6197a1edd9bc3aTom Taylorimport com.google.android.mms.pdu.ReadRecInd;
24f7e8281a223af6228e6399055a6197a1edd9bc3aTom Taylorimport com.google.android.mms.pdu.EncodedStringValue;
2513dbe96fc54f9b7190fd415d737f9a56dc409d10Wei Huangimport com.android.mms.ui.MessageUtils;
2672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
2772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport android.content.Context;
2872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport android.net.Uri;
29f7e8281a223af6228e6399055a6197a1edd9bc3aTom Taylorimport android.provider.Telephony.Mms.Sent;
3072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport android.util.Log;
3172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
3272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectimport java.io.IOException;
3372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
3472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project/**
3572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * The ReadRecTransaction is responsible for sending read report
3672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * notifications (M-read-rec.ind) to clients that have requested them.
3772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * It:
3872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project *
3972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * <ul>
4072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * <li>Loads the read report indication from storage (Outbox).
4172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * <li>Packs M-read-rec.ind and sends it.
4272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * <li>Notifies the TransactionService about succesful completion.
4372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project * </ul>
4472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project */
4572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Projectpublic class ReadRecTransaction extends Transaction {
4672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final String TAG = "ReadRecTransaction";
4772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private static final boolean DEBUG = false;
48150c4179995cc0a75f934ef194372f9295957ca2Joe Onorato    private static final boolean LOCAL_LOGV = false;
4972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
5072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    private final Uri mReadReportURI;
5172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
5272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    public ReadRecTransaction(Context context,
5372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            int transId,
5472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            TransactionSettings connectionSettings,
5572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            String uri) {
5672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        super(context, transId, connectionSettings);
5772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        mReadReportURI = Uri.parse(uri);
5872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        mId = uri;
5972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
6072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        // Attach the transaction to the instance of RetryScheduler.
6172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        attach(RetryScheduler.getInstance(context));
6272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
6372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
6472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    /*
6572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * (non-Javadoc)
6672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     * @see com.android.mms.Transaction#process()
6772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project     */
6872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    @Override
6972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    public void process() {
7072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        PduPersister persister = PduPersister.getPduPersister(mContext);
7172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
7272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        try {
7372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            // Load M-read-rec.ind from outbox
7472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            ReadRecInd readRecInd = (ReadRecInd) persister.load(mReadReportURI);
7572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
7613dbe96fc54f9b7190fd415d737f9a56dc409d10Wei Huang            // insert the 'from' address per spec
7713dbe96fc54f9b7190fd415d737f9a56dc409d10Wei Huang            String lineNumber = MessageUtils.getLocalNumber();
7813dbe96fc54f9b7190fd415d737f9a56dc409d10Wei Huang            readRecInd.setFrom(new EncodedStringValue(lineNumber));
7913dbe96fc54f9b7190fd415d737f9a56dc409d10Wei Huang
8072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            // Pack M-read-rec.ind and send it
8172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            byte[] postingData = new PduComposer(mContext, readRecInd).make();
8272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            sendPdu(postingData);
8372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
8472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            Uri uri = persister.move(mReadReportURI, Sent.CONTENT_URI);
8572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            mTransactionState.setState(TransactionState.SUCCESS);
8672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            mTransactionState.setContentUri(uri);
8772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (IOException e) {
8872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (LOCAL_LOGV) {
8972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                Log.v(TAG, "Failed to send M-Read-Rec.Ind.", e);
9072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
9172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (MmsException e) {
9272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (LOCAL_LOGV) {
9372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                Log.v(TAG, "Failed to load message from Outbox.", e);
9472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
9572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } catch (RuntimeException e) {
9672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (LOCAL_LOGV) {
9772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                Log.e(TAG, "Unexpected RuntimeException.", e);
9872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
9972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        } finally {
10072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            if (mTransactionState.getState() != TransactionState.SUCCESS) {
10172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                mTransactionState.setState(TransactionState.FAILED);
10272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project                mTransactionState.setContentUri(mReadReportURI);
10372735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            }
10472735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project            notifyObservers();
10572735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        }
10672735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
10772735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project
10872735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    @Override
10972735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    public int getType() {
11072735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project        return READREC_TRANSACTION;
11172735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project    }
11272735c62aba8fd2a9420a0f9f83d22543e3c164fThe Android Open Source Project}
113