2012-09-30 11:46:45 -07:00
|
|
|
/**
|
2013-07-16 19:52:02 -07:00
|
|
|
* Copyright (C) 2013 Open Whisper Systems
|
2012-09-30 11:46:45 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08: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 11:46:45 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08: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;
|
|
|
|
|
2014-02-15 18:31:25 -08:00
|
|
|
import android.app.AlarmManager;
|
|
|
|
import android.app.PendingIntent;
|
2012-09-30 11:46:45 -07:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.util.Log;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2014-02-15 18:31:25 -08:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2011-12-20 10:20:44 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
2013-07-16 19:52:02 -07:00
|
|
|
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
2013-11-19 10:13:24 -08:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsSendResult;
|
2013-02-09 15:17:55 -08:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2013-02-20 18:10:33 -08:00
|
|
|
import org.thoughtcrime.securesms.service.SendReceiveService.ToastHandler;
|
2014-02-16 15:23:49 -08:00
|
|
|
import org.thoughtcrime.securesms.sms.IncomingIdentityUpdateMessage;
|
2014-02-15 18:31:25 -08:00
|
|
|
import org.thoughtcrime.securesms.transport.RetryLaterException;
|
2013-07-16 19:52:02 -07:00
|
|
|
import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
|
2013-07-17 15:13:00 -07:00
|
|
|
import org.thoughtcrime.securesms.transport.UniversalTransport;
|
2014-02-16 15:23:49 -08:00
|
|
|
import org.thoughtcrime.securesms.transport.UntrustedIdentityException;
|
2013-11-18 13:16:18 -08:00
|
|
|
import org.whispersystems.textsecure.crypto.MasterSecret;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
|
|
|
import ws.com.google.android.mms.MmsException;
|
|
|
|
import ws.com.google.android.mms.pdu.SendReq;
|
2012-09-30 11:46:45 -07:00
|
|
|
|
2014-02-20 16:14:58 -08:00
|
|
|
import static org.thoughtcrime.securesms.database.SmsDatabase.Status;
|
|
|
|
|
2013-07-16 19:52:02 -07:00
|
|
|
public class MmsSender {
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2014-02-15 18:31:25 -08:00
|
|
|
private final Context context;
|
|
|
|
private final SystemStateListener systemStateListener;
|
|
|
|
private final ToastHandler toastHandler;
|
2012-09-30 11:46:45 -07:00
|
|
|
|
2014-02-15 18:31:25 -08:00
|
|
|
public MmsSender(Context context, SystemStateListener systemStateListener, ToastHandler toastHandler) {
|
|
|
|
this.context = context;
|
|
|
|
this.systemStateListener = systemStateListener;
|
|
|
|
this.toastHandler = toastHandler;
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void process(MasterSecret masterSecret, Intent intent) {
|
2013-07-16 19:52:02 -07:00
|
|
|
Log.w("MmsSender", "Got intent action: " + intent.getAction());
|
2013-11-18 13:16:18 -08:00
|
|
|
if (SendReceiveService.SEND_MMS_ACTION.equals(intent.getAction())) {
|
2013-07-16 19:52:02 -07:00
|
|
|
handleSendMms(masterSecret, intent);
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
|
|
|
}
|
2012-09-30 11:46:45 -07:00
|
|
|
|
2013-07-16 19:52:02 -07:00
|
|
|
private void handleSendMms(MasterSecret masterSecret, Intent intent) {
|
2013-07-17 15:13:00 -07:00
|
|
|
long messageId = intent.getLongExtra("message_id", -1);
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
ThreadDatabase threads = DatabaseFactory.getThreadDatabase(context);
|
|
|
|
UniversalTransport transport = new UniversalTransport(context, masterSecret);
|
2012-09-30 11:46:45 -07:00
|
|
|
|
2013-02-20 18:10:33 -08:00
|
|
|
try {
|
2013-07-16 19:52:02 -07:00
|
|
|
SendReq[] messages = database.getOutgoingMessages(masterSecret, messageId);
|
|
|
|
|
|
|
|
for (SendReq message : messages) {
|
2013-11-18 13:16:18 -08:00
|
|
|
long threadId = database.getThreadIdForMessage(message.getDatabaseMessageId());
|
|
|
|
|
2013-07-16 19:52:02 -07:00
|
|
|
try {
|
|
|
|
Log.w("MmsSender", "Passing to MMS transport: " + message.getDatabaseMessageId());
|
|
|
|
database.markAsSending(message.getDatabaseMessageId());
|
2013-11-19 10:13:24 -08:00
|
|
|
MmsSendResult result = transport.deliver(message, threadId);
|
|
|
|
|
2014-02-20 16:14:58 -08:00
|
|
|
if (result.isUpgradedSecure()) database.markAsSecure(message.getDatabaseMessageId());
|
2014-02-20 23:00:38 -08:00
|
|
|
if (result.isPush()) database.markAsPush(message.getDatabaseMessageId());
|
2013-11-19 10:13:24 -08:00
|
|
|
|
|
|
|
database.markAsSent(message.getDatabaseMessageId(), result.getMessageId(),
|
|
|
|
result.getResponseStatus());
|
2014-02-15 18:31:25 -08:00
|
|
|
|
|
|
|
systemStateListener.unregisterForConnectivityChange();
|
2013-07-16 19:52:02 -07:00
|
|
|
} catch (UndeliverableMessageException e) {
|
|
|
|
Log.w("MmsSender", e);
|
|
|
|
database.markAsSentFailed(message.getDatabaseMessageId());
|
|
|
|
Recipients recipients = threads.getRecipientsForThreadId(threadId);
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
|
2014-02-16 15:23:49 -08:00
|
|
|
} catch (UntrustedIdentityException uie) {
|
2014-02-17 11:42:51 -08:00
|
|
|
IncomingIdentityUpdateMessage identityUpdateMessage = IncomingIdentityUpdateMessage.createFor(message.getTo()[0].getString(), uie.getIdentityKey());
|
2014-02-16 15:23:49 -08:00
|
|
|
DatabaseFactory.getEncryptingSmsDatabase(context).insertMessageInbox(masterSecret, identityUpdateMessage);
|
|
|
|
database.markAsSentFailed(messageId);
|
2014-02-15 18:31:25 -08:00
|
|
|
} catch (RetryLaterException e) {
|
|
|
|
Log.w("MmsSender", e);
|
|
|
|
database.markAsOutbox(message.getDatabaseMessageId());
|
|
|
|
|
|
|
|
if (systemStateListener.isConnected()) scheduleQuickRetryAlarm();
|
|
|
|
else systemStateListener.registerForConnectivityChange();
|
|
|
|
|
|
|
|
toastHandler
|
|
|
|
.obtainMessage(0, context.getString(R.string.SmsReceiver_currently_unable_to_send_your_sms_message))
|
|
|
|
.sendToTarget();
|
2013-07-16 19:52:02 -07:00
|
|
|
}
|
2013-02-20 18:10:33 -08:00
|
|
|
}
|
2013-07-16 19:52:02 -07:00
|
|
|
} catch (MmsException e) {
|
|
|
|
Log.w("MmsSender", e);
|
|
|
|
if (messageId != -1)
|
|
|
|
database.markAsSentFailed(messageId);
|
2013-02-20 18:10:33 -08:00
|
|
|
}
|
|
|
|
}
|
2014-02-15 18:31:25 -08:00
|
|
|
|
|
|
|
private void scheduleQuickRetryAlarm() {
|
|
|
|
((AlarmManager)context.getSystemService(Context.ALARM_SERVICE))
|
|
|
|
.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (30 * 1000),
|
|
|
|
PendingIntent.getService(context, 0,
|
|
|
|
new Intent(SendReceiveService.SEND_MMS_ACTION,
|
|
|
|
null, context, SendReceiveService.class),
|
|
|
|
PendingIntent.FLAG_UPDATE_CURRENT));
|
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|