2014-11-03 23:16:04 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2012-09-30 18:46:45 +00:00
|
|
|
import android.content.Context;
|
2014-07-29 22:31:20 +00:00
|
|
|
import android.net.Uri;
|
2014-12-29 22:01:02 +00:00
|
|
|
import android.os.Build.VERSION;
|
|
|
|
import android.os.Build.VERSION_CODES;
|
2012-09-30 18:46:45 +00:00
|
|
|
import android.util.Log;
|
2013-04-26 18:23:43 +00:00
|
|
|
import android.util.Pair;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2013-09-16 07:55:01 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.ApnUnavailableException;
|
2014-12-29 22:01:02 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.IncomingLollipopMmsConnection;
|
2013-07-19 00:42:45 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.IncomingMediaMessage;
|
2014-12-29 22:01:02 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.IncomingLegacyMmsConnection;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.IncomingMmsConnection;
|
2013-07-17 02:52:02 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsRadioException;
|
2013-04-26 18:23:43 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2011-12-20 18:20:44 +00:00
|
|
|
import org.thoughtcrime.securesms.protocol.WirePrefix;
|
2014-11-03 23:16:04 +00:00
|
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
|
|
|
import org.whispersystems.libaxolotl.DuplicateMessageException;
|
|
|
|
import org.whispersystems.libaxolotl.InvalidMessageException;
|
|
|
|
import org.whispersystems.libaxolotl.LegacyMessageException;
|
|
|
|
import org.whispersystems.libaxolotl.NoSessionException;
|
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
2011-12-20 18:20:44 +00:00
|
|
|
|
2013-09-16 07:55:01 +00:00
|
|
|
import java.io.IOException;
|
2015-03-18 21:25:27 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2013-09-16 07:55:01 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
import ws.com.google.android.mms.MmsException;
|
2014-11-03 23:16:04 +00:00
|
|
|
import ws.com.google.android.mms.pdu.NotificationInd;
|
2011-12-20 18:20:44 +00:00
|
|
|
import ws.com.google.android.mms.pdu.RetrieveConf;
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
public class MmsDownloadJob extends MasterSecretJob {
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
private static final String TAG = MmsDownloadJob.class.getSimpleName();
|
|
|
|
|
|
|
|
private final long messageId;
|
|
|
|
private final long threadId;
|
|
|
|
private final boolean automatic;
|
|
|
|
|
|
|
|
public MmsDownloadJob(Context context, long messageId, long threadId, boolean automatic) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
2014-11-08 19:35:58 +00:00
|
|
|
.withGroupId("mms-operation")
|
2015-03-18 21:25:27 +00:00
|
|
|
.withWakeLock(true, 30, TimeUnit.SECONDS)
|
2014-11-03 23:16:04 +00:00
|
|
|
.create());
|
|
|
|
|
|
|
|
this.messageId = messageId;
|
|
|
|
this.threadId = threadId;
|
|
|
|
this.automatic = automatic;
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
|
|
|
if (automatic && KeyCachingService.getMasterSecret(context) == null) {
|
|
|
|
DatabaseFactory.getMmsDatabase(context).markIncomingNotificationReceived(threadId);
|
|
|
|
MessageNotifier.updateNotification(context, null);
|
2013-02-21 02:10:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
@Override
|
2014-12-29 22:01:02 +00:00
|
|
|
public void onRun(MasterSecret masterSecret) {
|
|
|
|
Log.w(TAG, "onRun()");
|
2014-11-03 23:16:04 +00:00
|
|
|
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
Optional<NotificationInd> notification = database.getNotification(messageId);
|
2013-09-16 07:55:01 +00:00
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
if (!notification.isPresent()) {
|
|
|
|
Log.w(TAG, "No notification for ID: " + messageId);
|
|
|
|
return;
|
2013-09-16 07:55:01 +00:00
|
|
|
}
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
database.markDownloadState(messageId, MmsDatabase.Status.DOWNLOAD_CONNECTING);
|
2013-09-16 07:55:01 +00:00
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
String contentLocation = new String(notification.get().getContentLocation());
|
|
|
|
byte[] transactionId = notification.get().getTransactionId();
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
Log.w(TAG, "Downloading mms at " + Uri.parse(contentLocation).getHost());
|
2014-07-29 22:31:20 +00:00
|
|
|
|
2011-12-20 18:20:44 +00:00
|
|
|
try {
|
2014-12-29 22:01:02 +00:00
|
|
|
RetrieveConf retrieveConf = getMmsConnection(context).retrieve(contentLocation, transactionId);
|
2015-03-30 17:46:14 +00:00
|
|
|
if (retrieveConf == null) {
|
|
|
|
throw new MmsException("RetrieveConf was null");
|
|
|
|
}
|
2014-12-29 22:01:02 +00:00
|
|
|
storeRetrievedMms(masterSecret, contentLocation, messageId, threadId, retrieveConf);
|
2013-09-16 07:55:01 +00:00
|
|
|
} catch (ApnUnavailableException e) {
|
2014-11-03 23:16:04 +00:00
|
|
|
Log.w(TAG, e);
|
2013-07-17 02:52:02 +00:00
|
|
|
handleDownloadError(masterSecret, messageId, threadId, MmsDatabase.Status.DOWNLOAD_APN_UNAVAILABLE,
|
2014-12-29 22:01:02 +00:00
|
|
|
automatic);
|
2011-12-20 18:20:44 +00:00
|
|
|
} catch (MmsException e) {
|
2014-11-03 23:16:04 +00:00
|
|
|
Log.w(TAG, e);
|
2013-07-17 02:52:02 +00:00
|
|
|
handleDownloadError(masterSecret, messageId, threadId,
|
|
|
|
MmsDatabase.Status.DOWNLOAD_HARD_FAILURE,
|
|
|
|
automatic);
|
2014-12-29 22:01:02 +00:00
|
|
|
} catch (MmsRadioException | IOException e) {
|
2014-11-03 23:16:04 +00:00
|
|
|
Log.w(TAG, e);
|
2013-07-17 02:52:02 +00:00
|
|
|
handleDownloadError(masterSecret, messageId, threadId,
|
|
|
|
MmsDatabase.Status.DOWNLOAD_SOFT_FAILURE,
|
|
|
|
automatic);
|
2014-11-03 23:16:04 +00:00
|
|
|
} catch (DuplicateMessageException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsDecryptDuplicate(messageId, threadId);
|
|
|
|
} catch (LegacyMessageException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsLegacyVersion(messageId, threadId);
|
|
|
|
} catch (NoSessionException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsNoSession(messageId, threadId);
|
|
|
|
} catch (InvalidMessageException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsDecryptFailed(messageId, threadId);
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2014-12-29 22:01:02 +00:00
|
|
|
private IncomingMmsConnection getMmsConnection(Context context)
|
|
|
|
throws ApnUnavailableException
|
|
|
|
{
|
|
|
|
if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
|
|
|
|
return new IncomingLollipopMmsConnection(context);
|
|
|
|
} else {
|
|
|
|
return new IncomingLegacyMmsConnection(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 23:16:04 +00:00
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
2014-11-08 19:35:58 +00:00
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
database.markDownloadState(messageId, MmsDatabase.Status.DOWNLOAD_SOFT_FAILURE);
|
|
|
|
|
|
|
|
if (automatic) {
|
|
|
|
database.markIncomingNotificationReceived(threadId);
|
|
|
|
MessageNotifier.updateNotification(context, null, threadId);
|
|
|
|
}
|
2014-11-03 23:16:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 05:11:57 +00:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
2014-11-03 23:16:04 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
private void storeRetrievedMms(MasterSecret masterSecret, String contentLocation,
|
|
|
|
long messageId, long threadId, RetrieveConf retrieved)
|
2014-11-03 23:16:04 +00:00
|
|
|
throws MmsException, NoSessionException, DuplicateMessageException, InvalidMessageException,
|
|
|
|
LegacyMessageException
|
2013-02-21 02:10:33 +00:00
|
|
|
{
|
2013-07-19 00:42:45 +00:00
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
IncomingMediaMessage message = new IncomingMediaMessage(retrieved);
|
|
|
|
|
2013-04-26 18:23:43 +00:00
|
|
|
Pair<Long, Long> messageAndThreadId;
|
|
|
|
|
2013-02-21 02:10:33 +00:00
|
|
|
if (retrieved.getSubject() != null && WirePrefix.isEncryptedMmsSubject(retrieved.getSubject().getString())) {
|
2015-03-11 21:23:45 +00:00
|
|
|
database.markAsLegacyVersion(messageId, threadId);
|
|
|
|
messageAndThreadId = new Pair<>(messageId, threadId);
|
2013-02-21 02:10:33 +00:00
|
|
|
} else {
|
2013-07-19 00:42:45 +00:00
|
|
|
messageAndThreadId = database.insertMessageInbox(masterSecret, message,
|
2013-07-17 02:52:02 +00:00
|
|
|
contentLocation, threadId);
|
2015-03-11 21:23:45 +00:00
|
|
|
database.delete(messageId);
|
2013-02-21 02:10:33 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret, messageAndThreadId.second);
|
2013-02-21 02:10:33 +00:00
|
|
|
}
|
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
private void handleDownloadError(MasterSecret masterSecret, long messageId, long threadId,
|
2014-12-29 22:01:02 +00:00
|
|
|
int downloadStatus, boolean automatic)
|
2013-07-17 02:52:02 +00:00
|
|
|
{
|
2013-04-26 18:23:43 +00:00
|
|
|
MmsDatabase db = DatabaseFactory.getMmsDatabase(context);
|
2012-09-30 18:46:45 +00:00
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
db.markDownloadState(messageId, downloadStatus);
|
2013-02-21 02:10:33 +00:00
|
|
|
|
2013-07-17 02:52:02 +00:00
|
|
|
if (automatic) {
|
|
|
|
db.markIncomingNotificationReceived(threadId);
|
|
|
|
MessageNotifier.updateNotification(context, masterSecret, threadId);
|
2013-02-21 02:10:33 +00:00
|
|
|
}
|
2011-12-20 18:20:44 +00:00
|
|
|
}
|
|
|
|
}
|