Home | History | Annotate | Download | only in transaction
      1 /*
      2  * Copyright (C) 2008 Esmertec AG.
      3  * Copyright (C) 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.android.mms.ui.MessagingPreferenceActivity;
     21 import com.android.mms.util.SendingProgressTokenManager;
     22 import com.google.android.mms.InvalidHeaderValueException;
     23 import com.google.android.mms.MmsException;
     24 import com.google.android.mms.pdu.EncodedStringValue;
     25 import com.google.android.mms.pdu.GenericPdu;
     26 import com.google.android.mms.pdu.PduHeaders;
     27 import com.google.android.mms.pdu.PduPersister;
     28 import com.google.android.mms.pdu.ReadRecInd;
     29 import com.google.android.mms.pdu.SendReq;
     30 
     31 import android.content.ContentUris;
     32 import android.content.Context;
     33 import android.content.Intent;
     34 import android.content.SharedPreferences;
     35 import android.net.Uri;
     36 import android.preference.PreferenceManager;
     37 import android.provider.Telephony.Mms;
     38 import android.util.Log;
     39 
     40 public class MmsMessageSender implements MessageSender {
     41     private static final String TAG = "MmsMessageSender";
     42 
     43     private final Context mContext;
     44     private final Uri mMessageUri;
     45     private final long mMessageSize;
     46 
     47     // Default preference values
     48     private static final boolean DEFAULT_DELIVERY_REPORT_MODE  = false;
     49     private static final boolean DEFAULT_READ_REPORT_MODE      = false;
     50     private static final long    DEFAULT_EXPIRY_TIME     = 7 * 24 * 60 * 60;
     51     private static final int     DEFAULT_PRIORITY        = PduHeaders.PRIORITY_NORMAL;
     52     private static final String  DEFAULT_MESSAGE_CLASS   = PduHeaders.MESSAGE_CLASS_PERSONAL_STR;
     53 
     54     public MmsMessageSender(Context context, Uri location, long messageSize) {
     55         mContext = context;
     56         mMessageUri = location;
     57         mMessageSize = messageSize;
     58 
     59         if (mMessageUri == null) {
     60             throw new IllegalArgumentException("Null message URI.");
     61         }
     62     }
     63 
     64     public boolean sendMessage(long token) throws MmsException {
     65         // Load the MMS from the message uri
     66         PduPersister p = PduPersister.getPduPersister(mContext);
     67         GenericPdu pdu = p.load(mMessageUri);
     68 
     69         if (pdu.getMessageType() != PduHeaders.MESSAGE_TYPE_SEND_REQ) {
     70             throw new MmsException("Invalid message: " + pdu.getMessageType());
     71         }
     72 
     73         SendReq sendReq = (SendReq) pdu;
     74 
     75         // Update headers.
     76         updatePreferencesHeaders(sendReq);
     77 
     78         // MessageClass.
     79         sendReq.setMessageClass(DEFAULT_MESSAGE_CLASS.getBytes());
     80 
     81         // Update the 'date' field of the message before sending it.
     82         sendReq.setDate(System.currentTimeMillis() / 1000L);
     83 
     84         sendReq.setMessageSize(mMessageSize);
     85 
     86         p.updateHeaders(mMessageUri, sendReq);
     87 
     88         // Move the message into MMS Outbox
     89         p.move(mMessageUri, Mms.Outbox.CONTENT_URI);
     90 
     91         // Start MMS transaction service
     92         SendingProgressTokenManager.put(ContentUris.parseId(mMessageUri), token);
     93         mContext.startService(new Intent(mContext, TransactionService.class));
     94 
     95         return true;
     96     }
     97 
     98     // Update the headers which are stored in SharedPreferences.
     99     private void updatePreferencesHeaders(SendReq sendReq) throws MmsException {
    100         SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
    101 
    102         // Expiry.
    103         sendReq.setExpiry(prefs.getLong(
    104                 MessagingPreferenceActivity.EXPIRY_TIME, DEFAULT_EXPIRY_TIME));
    105 
    106         // Priority.
    107         sendReq.setPriority(prefs.getInt(MessagingPreferenceActivity.PRIORITY, DEFAULT_PRIORITY));
    108 
    109         // Delivery report.
    110         boolean dr = prefs.getBoolean(MessagingPreferenceActivity.MMS_DELIVERY_REPORT_MODE,
    111                 DEFAULT_DELIVERY_REPORT_MODE);
    112         sendReq.setDeliveryReport(dr?PduHeaders.VALUE_YES:PduHeaders.VALUE_NO);
    113 
    114         // Read report.
    115         boolean rr = prefs.getBoolean(MessagingPreferenceActivity.READ_REPORT_MODE,
    116                 DEFAULT_READ_REPORT_MODE);
    117         sendReq.setReadReport(rr?PduHeaders.VALUE_YES:PduHeaders.VALUE_NO);
    118     }
    119 
    120     public static void sendReadRec(Context context, String to, String messageId, int status) {
    121         EncodedStringValue[] sender = new EncodedStringValue[1];
    122         sender[0] = new EncodedStringValue(to);
    123 
    124         try {
    125             final ReadRecInd readRec = new ReadRecInd(
    126                     new EncodedStringValue(PduHeaders.FROM_INSERT_ADDRESS_TOKEN_STR.getBytes()),
    127                     messageId.getBytes(),
    128                     PduHeaders.CURRENT_MMS_VERSION,
    129                     status,
    130                     sender);
    131 
    132             readRec.setDate(System.currentTimeMillis() / 1000);
    133 
    134             PduPersister.getPduPersister(context).persist(readRec, Mms.Outbox.CONTENT_URI);
    135             context.startService(new Intent(context, TransactionService.class));
    136         } catch (InvalidHeaderValueException e) {
    137             Log.e(TAG, "Invalide header value", e);
    138         } catch (MmsException e) {
    139             Log.e(TAG, "Persist message failed", e);
    140         }
    141     }
    142 }
    143