2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2016-11-03 17:36:45 +01:00
|
|
|
import android.support.annotation.VisibleForTesting;
|
2015-10-12 18:25:05 -07:00
|
|
|
import android.text.TextUtils;
|
2014-11-03 15:16:04 -08:00
|
|
|
import android.util.Log;
|
|
|
|
|
2017-02-17 20:27:11 -08:00
|
|
|
import org.greenrobot.eventbus.EventBus;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
|
|
|
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
2015-07-06 17:36:49 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.AsymmetricMasterSecret;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-07-06 17:36:49 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MediaKey;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2015-11-02 14:32:02 -08:00
|
|
|
import org.thoughtcrime.securesms.events.PartProgressEvent;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2015-08-24 15:24:31 -07:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MediaNetworkRequirement;
|
2015-07-31 16:46:17 -07:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2017-02-26 10:06:27 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Hex;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.InvalidMessageException;
|
2017-02-26 10:06:27 -08:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment.ProgressListener;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
import ws.com.google.android.mms.MmsException;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
public class AttachmentDownloadJob extends MasterSecretJob implements InjectableType {
|
2015-08-24 15:24:31 -07:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private static final String TAG = AttachmentDownloadJob.class.getSimpleName();
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
@Inject transient SignalServiceMessageReceiver messageReceiver;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
private final long messageId;
|
2015-08-24 15:24:31 -07:00
|
|
|
private final long partRowId;
|
|
|
|
private final long partUniqueId;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
public AttachmentDownloadJob(Context context, long messageId, AttachmentId attachmentId) {
|
2014-11-03 15:16:04 -08:00
|
|
|
super(context, JobParameters.newBuilder()
|
2015-08-24 15:24:31 -07:00
|
|
|
.withGroupId(AttachmentDownloadJob.class.getCanonicalName())
|
2014-11-03 15:16:04 -08:00
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
2015-10-12 18:25:05 -07:00
|
|
|
.withRequirement(new MediaNetworkRequirement(context, messageId, attachmentId))
|
2014-11-03 15:16:04 -08:00
|
|
|
.withPersistence()
|
|
|
|
.create());
|
|
|
|
|
2015-08-24 15:24:31 -07:00
|
|
|
this.messageId = messageId;
|
2015-10-12 18:25:05 -07:00
|
|
|
this.partRowId = attachmentId.getRowId();
|
|
|
|
this.partUniqueId = attachmentId.getUniqueId();
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-08-24 15:24:31 -07:00
|
|
|
public void onAdded() {
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
@Override
|
2014-11-11 19:57:53 -08:00
|
|
|
public void onRun(MasterSecret masterSecret) throws IOException {
|
2015-10-12 18:25:05 -07:00
|
|
|
final AttachmentId attachmentId = new AttachmentId(partRowId, partUniqueId);
|
|
|
|
final Attachment attachment = DatabaseFactory.getAttachmentDatabase(context).getAttachment(attachmentId);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
if (attachment == null) {
|
|
|
|
Log.w(TAG, "attachment no longer exists.");
|
2015-09-07 11:32:11 -10:00
|
|
|
return;
|
|
|
|
}
|
2015-10-12 18:25:05 -07:00
|
|
|
|
|
|
|
if (!attachment.isInProgress()) {
|
|
|
|
Log.w(TAG, "Attachment was already downloaded.");
|
2015-09-10 16:48:10 -10:00
|
|
|
return;
|
|
|
|
}
|
2015-09-07 11:32:11 -10:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
Log.w(TAG, "Downloading push part " + attachmentId);
|
2015-07-31 16:46:17 -07:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
retrieveAttachment(masterSecret, messageId, attachmentId, attachment);
|
2015-07-31 16:46:17 -07:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
2015-10-12 18:25:05 -07:00
|
|
|
final AttachmentId attachmentId = new AttachmentId(partRowId, partUniqueId);
|
|
|
|
markFailed(messageId, attachmentId);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-11 21:11:57 -08:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
2014-12-12 01:03:24 -08:00
|
|
|
return (exception instanceof PushNetworkException);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
private void retrieveAttachment(MasterSecret masterSecret,
|
|
|
|
long messageId,
|
|
|
|
final AttachmentId attachmentId,
|
|
|
|
final Attachment attachment)
|
2014-11-03 15:16:04 -08:00
|
|
|
throws IOException
|
|
|
|
{
|
2015-09-04 10:56:59 -07:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
AttachmentDatabase database = DatabaseFactory.getAttachmentDatabase(context);
|
|
|
|
File attachmentFile = null;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
try {
|
|
|
|
attachmentFile = createTempFile();
|
2014-11-09 20:35:08 -08:00
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
SignalServiceAttachmentPointer pointer = createAttachmentPointer(masterSecret, attachment);
|
|
|
|
InputStream stream = messageReceiver.retrieveAttachment(pointer, attachmentFile, new ProgressListener() {
|
2015-10-12 18:25:05 -07:00
|
|
|
@Override
|
|
|
|
public void onAttachmentProgress(long total, long progress) {
|
|
|
|
EventBus.getDefault().postSticky(new PartProgressEvent(attachment, total, progress));
|
2015-06-26 20:14:51 -07:00
|
|
|
}
|
|
|
|
});
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
database.insertAttachmentsForPlaceholder(masterSecret, messageId, attachmentId, stream);
|
2014-11-03 15:16:04 -08:00
|
|
|
} catch (InvalidPartException | NonSuccessfulResponseCodeException | InvalidMessageException | MmsException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-10-12 18:25:05 -07:00
|
|
|
markFailed(messageId, attachmentId);
|
2014-11-03 15:16:04 -08:00
|
|
|
} finally {
|
|
|
|
if (attachmentFile != null)
|
|
|
|
attachmentFile.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 12:28:29 -07:00
|
|
|
@VisibleForTesting
|
2016-03-23 10:34:41 -07:00
|
|
|
SignalServiceAttachmentPointer createAttachmentPointer(MasterSecret masterSecret, Attachment attachment)
|
2014-11-03 15:16:04 -08:00
|
|
|
throws InvalidPartException
|
|
|
|
{
|
2015-10-12 18:25:05 -07:00
|
|
|
if (TextUtils.isEmpty(attachment.getLocation())) {
|
2015-10-01 12:28:29 -07:00
|
|
|
throw new InvalidPartException("empty content id");
|
|
|
|
}
|
2015-10-12 18:25:05 -07:00
|
|
|
|
|
|
|
if (TextUtils.isEmpty(attachment.getKey())) {
|
2015-10-01 12:28:29 -07:00
|
|
|
throw new InvalidPartException("empty encrypted key");
|
|
|
|
}
|
2015-09-04 10:56:59 -07:00
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
try {
|
2015-07-06 17:36:49 -07:00
|
|
|
AsymmetricMasterSecret asymmetricMasterSecret = MasterSecretUtil.getAsymmetricMasterSecret(context, masterSecret);
|
2015-10-12 18:25:05 -07:00
|
|
|
long id = Long.parseLong(attachment.getLocation());
|
|
|
|
byte[] key = MediaKey.getDecrypted(masterSecret, asymmetricMasterSecret, attachment.getKey());
|
2015-07-06 17:36:49 -07:00
|
|
|
String relay = null;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
if (TextUtils.isEmpty(attachment.getRelay())) {
|
|
|
|
relay = attachment.getRelay();
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2017-02-26 10:06:27 -08:00
|
|
|
if (attachment.getDigest() != null) {
|
|
|
|
Log.w(TAG, "Downloading attachment with digest: " + Hex.toString(attachment.getDigest()));
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Downloading attachment with no digest...");
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SignalServiceAttachmentPointer(id, null, key, relay, Optional.fromNullable(attachment.getDigest()));
|
2014-11-03 15:16:04 -08:00
|
|
|
} catch (InvalidMessageException | IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
throw new InvalidPartException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private File createTempFile() throws InvalidPartException {
|
|
|
|
try {
|
2015-01-05 14:39:05 -08:00
|
|
|
File file = File.createTempFile("push-attachment", "tmp", context.getCacheDir());
|
2014-11-03 15:16:04 -08:00
|
|
|
file.deleteOnExit();
|
|
|
|
|
|
|
|
return file;
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new InvalidPartException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
private void markFailed(long messageId, AttachmentId attachmentId) {
|
2014-11-03 15:16:04 -08:00
|
|
|
try {
|
2015-10-12 18:25:05 -07:00
|
|
|
AttachmentDatabase database = DatabaseFactory.getAttachmentDatabase(context);
|
|
|
|
database.setTransferProgressFailed(attachmentId, messageId);
|
2014-11-03 15:16:04 -08:00
|
|
|
} catch (MmsException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 12:28:29 -07:00
|
|
|
@VisibleForTesting static class InvalidPartException extends Exception {
|
2015-09-04 10:56:59 -07:00
|
|
|
public InvalidPartException(String s) {super(s);}
|
2014-11-03 15:16:04 -08:00
|
|
|
public InvalidPartException(Exception e) {super(e);}
|
|
|
|
}
|
2015-06-26 20:14:51 -07:00
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|