/* * Copyright (C) 2008 Esmertec AG. * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.mms.transaction; import com.android.internal.telephony.PhoneFactory; import com.android.mms.ui.MessageUtils; import com.android.mms.ui.MessagingPreferenceActivity; import com.android.mms.util.SendingProgressTokenManager; import com.google.android.mms.InvalidHeaderValueException; import com.google.android.mms.MmsException; import com.google.android.mms.pdu.EncodedStringValue; import com.google.android.mms.pdu.GenericPdu; import com.google.android.mms.pdu.PduHeaders; import com.google.android.mms.pdu.PduPersister; import com.google.android.mms.pdu.ReadRecInd; import com.google.android.mms.pdu.SendReq; import android.content.ContentUris; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; import android.database.sqlite.SqliteWrapper; import android.net.Uri; import android.preference.PreferenceManager; import android.provider.Telephony.Mms; import android.telephony.TelephonyManager; import android.util.Log; public class MmsMessageSender implements MessageSender { private static final String TAG = "MmsMessageSender"; private final Context mContext; private final Uri mMessageUri; private final long mMessageSize; private int mPhoneId; // Default preference values private static final boolean DEFAULT_DELIVERY_REPORT_MODE = false; private static final boolean DEFAULT_READ_REPORT_MODE = false; private static final long DEFAULT_EXPIRY_TIME = 7 * 24 * 60 * 60; private static final int DEFAULT_PRIORITY = PduHeaders.PRIORITY_NORMAL; private static final String DEFAULT_MESSAGE_CLASS = PduHeaders.MESSAGE_CLASS_PERSONAL_STR; public MmsMessageSender(Context context, Uri location, long messageSize) { mContext = context; mMessageUri = location; mMessageSize = messageSize; mPhoneId = PhoneFactory.getDefaultPhoneId(); if (mMessageUri == null) { throw new IllegalArgumentException("Null message URI."); } } public MmsMessageSender(Context context, Uri location, long messageSize, int phoneId) { mContext = context; mMessageUri = location; mMessageSize = messageSize; mPhoneId = phoneId; if (mMessageUri == null) { throw new IllegalArgumentException("Null message URI."); } } public boolean sendMessage(long token) throws MmsException { // Load the MMS from the message uri PduPersister p = PduPersister.getPduPersister(mContext); GenericPdu pdu = p.load(mMessageUri); if (pdu.getMessageType() != PduHeaders.MESSAGE_TYPE_SEND_REQ) { throw new MmsException("Invalid message: " + pdu.getMessageType()); } SendReq sendReq = (SendReq) pdu; sendReq.setPhoneId(mPhoneId);//mPhoneId should be the same as phoneId in SendReq // Update headers. updatePreferencesHeaders(sendReq); // MessageClass. sendReq.setMessageClass(DEFAULT_MESSAGE_CLASS.getBytes()); // Update the 'date' field of the message before sending it. sendReq.setDate(System.currentTimeMillis() / 1000L); sendReq.setMessageSize(mMessageSize); p.updateHeaders(mMessageUri, sendReq); // Move the message into MMS Outbox p.move(mMessageUri, Mms.Outbox.CONTENT_URI); // Start MMS transaction service SendingProgressTokenManager.put(ContentUris.parseId(mMessageUri), token); //cienet edit yuanman 2011-6-15: if (TelephonyManager.getPhoneCount() > 1 || MessageUtils.isMSMS){ Log.i(TAG,"[MmsMessageSender] sendMessage TelephonyManager.getPhoneCount() > 1"); Log.i(TAG,"mPhoneId is"+mPhoneId); mContext.startService(new Intent(mContext, TransactionServiceHelper .getTransactionServiceClass(mPhoneId))); } else { Log.i(TAG,"[MmsMessageSender] sendMessage[ELSE]TelephonyManager.getPhoneCount() > 1"); mContext.startService(new Intent(mContext, TransactionService.class)); } //cienet end yuanman. return true; } // Update the headers which are stored in SharedPreferences. private void updatePreferencesHeaders(SendReq sendReq) throws MmsException { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); // Expiry. sendReq.setExpiry(prefs.getLong( MessagingPreferenceActivity.EXPIRY_TIME, DEFAULT_EXPIRY_TIME)); // Priority. sendReq.setPriority(prefs.getInt(MessagingPreferenceActivity.PRIORITY, DEFAULT_PRIORITY)); // Delivery report. boolean dr = prefs.getBoolean(MessagingPreferenceActivity.MMS_DELIVERY_REPORT_MODE, DEFAULT_DELIVERY_REPORT_MODE); sendReq.setDeliveryReport(dr?PduHeaders.VALUE_YES:PduHeaders.VALUE_NO); // Read report. boolean rr = prefs.getBoolean(MessagingPreferenceActivity.READ_REPORT_MODE, DEFAULT_READ_REPORT_MODE); sendReq.setReadReport(rr?PduHeaders.VALUE_YES:PduHeaders.VALUE_NO); } public static void sendReadRec(Context context, String to, String messageId, int status) { EncodedStringValue[] sender = new EncodedStringValue[1]; sender[0] = new EncodedStringValue(to); // TODO: phoneId is not set @zha // zhanglj add begin 2011-05-27 // fix for bug17086 start //String selection = Mms._ID + " = " + messageId; String selection = Mms.MESSAGE_ID + " = '" + messageId + "' "; //// fix for bug17086 end int phoneId = -1; Cursor c = null; try{ c = SqliteWrapper.query(context, context.getContentResolver(), Mms.Inbox.CONTENT_URI, new String[] {Mms._ID, Mms.PHONE_ID }, selection, null, null); //cienet add yuanman 2011-6-15:TODO:set the phoneId's value if (c.getCount() == 0 || c.moveToFirst()) { Log.e(TAG,"fail to find the phoneId of original message"); return; } phoneId = c.getInt(1);// get the phone phoneId id //cienet end yuanman. }finally{ c.close(); } // zhanglj add end try { final ReadRecInd readRec = new ReadRecInd( new EncodedStringValue(PduHeaders.FROM_INSERT_ADDRESS_TOKEN_STR.getBytes()), messageId.getBytes(), PduHeaders.CURRENT_MMS_VERSION, status, sender, phoneId); readRec.setDate(System.currentTimeMillis() / 1000); PduPersister.getPduPersister(context).persist(readRec, Mms.Outbox.CONTENT_URI, phoneId); //cienet edit yuanman 2011-6-15: if (TelephonyManager.getPhoneCount() > 1 || MessageUtils.isMSMS){ context.startService(new Intent(context, TransactionServiceHelper .getTransactionServiceClass(phoneId))); } else { context.startService(new Intent(context, TransactionService.class)); } //cienet end yuanman. } catch (InvalidHeaderValueException e) { Log.e(TAG, "Invalide header value", e); } catch (MmsException e) { Log.e(TAG, "Persist message failed", e); } } }