2012-09-30 18:46:45 +00:00
|
|
|
/**
|
2011-12-20 18:20:44 +00:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-09-30 18:46:45 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-09-30 18:46:45 +00:00
|
|
|
*
|
2011-12-20 18:20:44 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.service;
|
|
|
|
|
2012-09-30 18:46:45 +00:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2013-02-09 23:17:55 +00:00
|
|
|
import android.os.Handler;
|
2012-09-30 18:46:45 +00:00
|
|
|
import android.telephony.TelephonyManager;
|
|
|
|
import android.util.Log;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-02-09 23:17:55 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.SessionCipher;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.mms.MmsSendHelper;
|
|
|
|
import org.thoughtcrime.securesms.mms.TextTransport;
|
2013-02-09 23:17:55 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.protocol.WirePrefix;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2013-02-09 23:17:55 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Hex;
|
|
|
|
|
|
|
|
import ws.com.google.android.mms.ContentType;
|
|
|
|
import ws.com.google.android.mms.MmsException;
|
|
|
|
import ws.com.google.android.mms.pdu.EncodedStringValue;
|
|
|
|
import ws.com.google.android.mms.pdu.PduBody;
|
|
|
|
import ws.com.google.android.mms.pdu.PduComposer;
|
|
|
|
import ws.com.google.android.mms.pdu.PduHeaders;
|
|
|
|
import ws.com.google.android.mms.pdu.PduParser;
|
|
|
|
import ws.com.google.android.mms.pdu.PduPart;
|
|
|
|
import ws.com.google.android.mms.pdu.SendConf;
|
|
|
|
import ws.com.google.android.mms.pdu.SendReq;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.LinkedList;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
|
|
|
public class MmsSender extends MmscProcessor {
|
|
|
|
|
|
|
|
private final LinkedList<SendReq[]> pendingMessages = new LinkedList<SendReq[]>();
|
2013-02-09 23:17:55 +00:00
|
|
|
private final Handler toastHandler;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-09 23:17:55 +00:00
|
|
|
public MmsSender(Context context, Handler toastHandler) {
|
2011-12-20 18:20:44 +00:00
|
|
|
super(context);
|
2013-02-09 23:17:55 +00:00
|
|
|
this.toastHandler = toastHandler;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void process(MasterSecret masterSecret, Intent intent) {
|
|
|
|
if (intent.getAction().equals(SendReceiveService.SEND_MMS_ACTION)) {
|
2012-09-30 18:46:45 +00:00
|
|
|
long messageId = intent.getLongExtra("message_id", -1);
|
2011-12-20 18:20:44 +00:00
|
|
|
MmsDatabase database = DatabaseFactory.getEncryptingMmsDatabase(context, masterSecret);
|
|
|
|
|
|
|
|
try {
|
2012-09-30 18:46:45 +00:00
|
|
|
SendReq[] sendRequests;
|
|
|
|
|
|
|
|
if (messageId == -1) {
|
|
|
|
sendRequests = database.getOutgoingMessages();
|
|
|
|
} else {
|
|
|
|
sendRequests = new SendReq[1];
|
|
|
|
sendRequests[0] = database.getSendRequest(messageId);
|
|
|
|
}
|
|
|
|
|
2013-02-09 23:17:55 +00:00
|
|
|
if (sendRequests != null && sendRequests.length > 0)
|
|
|
|
handleSendMms(sendRequests, messageId != -1);
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
} catch (MmsException me) {
|
2012-09-30 18:46:45 +00:00
|
|
|
Log.w("MmsSender", me);
|
|
|
|
if (messageId != -1)
|
|
|
|
database.markAsSentFailed(messageId);
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
} else if (intent.getAction().equals(SendReceiveService.SEND_MMS_CONNECTIVITY_ACTION)) {
|
|
|
|
handleConnectivityChange(masterSecret);
|
|
|
|
}
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
protected void handleConnectivityChange(MasterSecret masterSecret) {
|
|
|
|
if (!isConnected())
|
|
|
|
return;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (!pendingMessages.isEmpty()) handleSendMmsContinued(masterSecret, pendingMessages.remove());
|
|
|
|
else finishConnectivity();
|
|
|
|
}
|
|
|
|
|
2013-02-09 23:17:55 +00:00
|
|
|
private void handleSendMms(SendReq[] sendRequests, boolean targeted) {
|
2011-12-20 18:20:44 +00:00
|
|
|
if (!isConnectivityPossible()) {
|
2013-02-09 23:17:55 +00:00
|
|
|
if (targeted) {
|
|
|
|
toastHandler
|
|
|
|
.obtainMessage(0, context.getString(R.string.MmsSender_currently_unable_to_send_your_mms_message))
|
|
|
|
.sendToTarget();
|
|
|
|
}
|
|
|
|
// for (int i=0;i<sendRequests.length;i++)
|
|
|
|
// DatabaseFactory.getMmsDatabase(context).markAsSentFailed(sendRequests[i].getDatabaseMessageId());
|
2011-12-20 18:20:44 +00:00
|
|
|
} else {
|
|
|
|
pendingMessages.add(sendRequests);
|
|
|
|
issueConnectivityRequest();
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
}
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private boolean isInconsistentResponse(SendReq send, SendConf response) {
|
|
|
|
Log.w("MmsSenderService", "Comparing: " + Hex.toString(send.getTransactionId()));
|
|
|
|
Log.w("MmsSenderService", "With: " + Hex.toString(response.getTransactionId()));
|
|
|
|
return !Arrays.equals(send.getTransactionId(), response.getTransactionId());
|
|
|
|
}
|
|
|
|
|
|
|
|
private byte[] getEncryptedPdu(MasterSecret masterSecret, String recipient, byte[] pduBytes) {
|
|
|
|
synchronized (SessionCipher.CIPHER_LOCK) {
|
2012-12-24 16:40:37 +00:00
|
|
|
SessionCipher cipher = new SessionCipher(context, masterSecret, new Recipient(null, recipient, null, null), new TextTransport());
|
2011-12-20 18:20:44 +00:00
|
|
|
return cipher.encryptMessage(pduBytes);
|
|
|
|
}
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private SendReq getEncryptedMms(MasterSecret masterSecret, SendReq pdu, long messageId) {
|
|
|
|
Log.w("MmsSender", "Sending Secure MMS.");
|
|
|
|
EncodedStringValue[] encodedRecipient = pdu.getTo();
|
|
|
|
String recipient = encodedRecipient[0].getString();
|
|
|
|
byte[] pduBytes = new PduComposer(context, pdu).make();
|
|
|
|
byte[] encryptedPdu = getEncryptedPdu(masterSecret, recipient, pduBytes);
|
|
|
|
Log.w("MmsSendeR", "Got encrypted bytes: " + encryptedPdu.length);
|
|
|
|
PduBody body = new PduBody();
|
|
|
|
PduPart part = new PduPart();
|
|
|
|
|
|
|
|
part.setContentId((System.currentTimeMillis()+"").getBytes());
|
|
|
|
part.setContentType(ContentType.TEXT_PLAIN.getBytes());
|
|
|
|
part.setName((System.currentTimeMillis()+"").getBytes());
|
|
|
|
part.setData(encryptedPdu);
|
|
|
|
body.addPart(part);
|
|
|
|
pdu.setSubject(new EncodedStringValue(WirePrefix.calculateEncryptedMmsSubject()));
|
|
|
|
pdu.setBody(body);
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
return pdu;
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void sendMms(MmsDatabase db, SendReq pdu, String number, long messageId, boolean secure) {
|
|
|
|
try {
|
|
|
|
if (number != null && number.trim().length() != 0)
|
2012-09-30 18:46:45 +00:00
|
|
|
pdu.setFrom(new EncodedStringValue(number));
|
|
|
|
|
|
|
|
byte[] response = MmsSendHelper.sendMms(context, new PduComposer(context, pdu).make(), getApnInformation());
|
2011-12-20 18:20:44 +00:00
|
|
|
SendConf conf = (SendConf) new PduParser(response).parse();
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
for (int i=0;i<pdu.getBody().getPartsNum();i++) {
|
2012-09-30 18:46:45 +00:00
|
|
|
Log.w("MmsSender", "Sent MMS part of content-type: " + new String(pdu.getBody().getPart(i).getContentType()));
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-02-09 23:17:55 +00:00
|
|
|
long threadId = DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(messageId);
|
|
|
|
Recipients recipients = DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(context, threadId);
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (conf == null) {
|
2012-09-30 18:46:45 +00:00
|
|
|
db.markAsSentFailed(messageId);
|
2013-02-09 23:17:55 +00:00
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
|
2012-09-30 18:46:45 +00:00
|
|
|
Log.w("MmsSender", "No M-Send.conf received in response to send.");
|
|
|
|
return;
|
2011-12-20 18:20:44 +00:00
|
|
|
} else if (conf.getResponseStatus() != PduHeaders.RESPONSE_STATUS_OK) {
|
2012-09-30 18:46:45 +00:00
|
|
|
Log.w("MmsSender", "Got bad response: " + conf.getResponseStatus());
|
|
|
|
db.updateResponseStatus(messageId, conf.getResponseStatus());
|
|
|
|
db.markAsSentFailed(messageId);
|
2013-02-09 23:17:55 +00:00
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
|
2012-09-30 18:46:45 +00:00
|
|
|
return;
|
2011-12-20 18:20:44 +00:00
|
|
|
} else if (isInconsistentResponse(pdu, conf)) {
|
2012-09-30 18:46:45 +00:00
|
|
|
db.markAsSentFailed(messageId);
|
2013-02-09 23:17:55 +00:00
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
|
2012-09-30 18:46:45 +00:00
|
|
|
Log.w("MmsSender", "Got a response for the wrong transaction?");
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
Log.w("MmsSender", "Successful send! " + messageId);
|
|
|
|
if (!secure)
|
|
|
|
db.markAsSent(messageId, conf.getMessageId(), conf.getResponseStatus());
|
|
|
|
else
|
|
|
|
db.markAsSecureSent(messageId, conf.getMessageId(), conf.getResponseStatus());
|
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w("MmsSender", ioe);
|
|
|
|
db.markAsSentFailed(messageId);
|
2012-09-30 18:46:45 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
private void handleSendMmsContinued(MasterSecret masterSecret, SendReq[] requests) {
|
|
|
|
Log.w("MmsSenderService", "Handling MMS send continuation...");
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
MmsDatabase db = DatabaseFactory.getEncryptingMmsDatabase(context, masterSecret);
|
|
|
|
String number = ((TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
|
|
|
|
|
|
|
|
for (int i=0;i<requests.length;i++) {
|
|
|
|
SendReq request = requests[i];
|
|
|
|
long messageId = request.getDatabaseMessageId();
|
|
|
|
long messageBox = request.getDatabaseMessageBox();
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (MmsDatabase.Types.isSecureMmsBox(messageBox))
|
2012-09-30 18:46:45 +00:00
|
|
|
request = getEncryptedMms(masterSecret, request, messageId);
|
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
sendMms(db, request, number, messageId, MmsDatabase.Types.isSecureMmsBox(messageBox));
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
if (this.pendingMessages.isEmpty())
|
|
|
|
finishConnectivity();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2012-09-30 18:46:45 +00:00
|
|
|
protected String getConnectivityAction() {
|
2011-12-20 18:20:44 +00:00
|
|
|
return SendReceiveService.SEND_MMS_CONNECTIVITY_ACTION;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|