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;
|
2018-06-11 10:38:11 -07:00
|
|
|
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
2017-07-21 15:59:27 -07:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
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;
|
2017-07-21 15:59:27 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsException;
|
2015-07-31 16:46:17 -07:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2017-07-21 15:59:27 -07:00
|
|
|
import org.thoughtcrime.securesms.util.AttachmentUtil;
|
2018-01-24 19:17:44 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
2017-02-26 10:06:27 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Hex;
|
2017-11-01 15:56:31 -07:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
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.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;
|
|
|
|
|
|
|
|
public class AttachmentDownloadJob extends MasterSecretJob implements InjectableType {
|
2017-07-21 15:59:27 -07:00
|
|
|
private static final long serialVersionUID = 2L;
|
2017-03-09 15:04:16 -08:00
|
|
|
private static final int MAX_ATTACHMENT_SIZE = 150 * 1024 * 1024;
|
|
|
|
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
|
|
|
|
2017-07-21 15:59:27 -07:00
|
|
|
private final long messageId;
|
|
|
|
private final long partRowId;
|
|
|
|
private final long partUniqueId;
|
|
|
|
private final boolean manual;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2017-07-21 15:59:27 -07:00
|
|
|
public AttachmentDownloadJob(Context context, long messageId, AttachmentId attachmentId, boolean manual) {
|
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))
|
|
|
|
.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();
|
2017-07-21 15:59:27 -07:00
|
|
|
this.manual = manual;
|
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 {
|
2017-07-21 15:59:27 -07:00
|
|
|
final AttachmentDatabase database = DatabaseFactory.getAttachmentDatabase(context);
|
|
|
|
final AttachmentId attachmentId = new AttachmentId(partRowId, partUniqueId);
|
2018-06-11 10:38:11 -07:00
|
|
|
final DatabaseAttachment attachment = database.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
|
|
|
|
2017-07-21 15:59:27 -07:00
|
|
|
if (!manual && !AttachmentUtil.isAutoDownloadPermitted(context, attachment)) {
|
|
|
|
Log.w(TAG, "Attachment can't be auto downloaded...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
Log.w(TAG, "Downloading push part " + attachmentId);
|
2017-07-21 15:59:27 -07:00
|
|
|
database.setTransferState(messageId, attachmentId, AttachmentDatabase.TRANSFER_PROGRESS_STARTED);
|
2015-07-31 16:46:17 -07:00
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
retrieveAttachment(messageId, attachmentId, attachment);
|
|
|
|
MessageNotifier.updateNotification(context);
|
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
|
|
|
}
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
private void retrieveAttachment(long messageId,
|
2015-10-12 18:25:05 -07:00
|
|
|
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
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
SignalServiceAttachmentPointer pointer = createAttachmentPointer(attachment);
|
|
|
|
InputStream stream = messageReceiver.retrieveAttachment(pointer, attachmentFile, MAX_ATTACHMENT_SIZE, (total, progress) -> EventBus.getDefault().postSticky(new PartProgressEvent(attachment, total, progress)));
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
database.insertAttachmentsForPlaceholder(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 {
|
2018-01-24 19:17:44 -08:00
|
|
|
if (attachmentFile != null) {
|
|
|
|
//noinspection ResultOfMethodCallIgnored
|
2014-11-03 15:16:04 -08:00
|
|
|
attachmentFile.delete();
|
2018-01-24 19:17:44 -08:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 12:28:29 -07:00
|
|
|
@VisibleForTesting
|
2018-01-24 19:17:44 -08:00
|
|
|
SignalServiceAttachmentPointer createAttachmentPointer(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 {
|
2018-01-24 19:17:44 -08:00
|
|
|
long id = Long.parseLong(attachment.getLocation());
|
|
|
|
byte[] key = Base64.decode(attachment.getKey());
|
|
|
|
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...");
|
|
|
|
}
|
|
|
|
|
2017-11-01 15:56:31 -07:00
|
|
|
return new SignalServiceAttachmentPointer(id, null, key, relay,
|
|
|
|
Optional.of(Util.toIntExact(attachment.getSize())),
|
|
|
|
Optional.absent(),
|
2018-01-18 10:01:41 -08:00
|
|
|
0, 0,
|
2017-11-01 15:56:31 -07:00
|
|
|
Optional.fromNullable(attachment.getDigest()),
|
|
|
|
Optional.fromNullable(attachment.getFileName()),
|
|
|
|
attachment.isVoiceNote());
|
2018-01-24 19:17:44 -08:00
|
|
|
} catch (IOException | ArithmeticException e) {
|
2014-11-03 15:16:04 -08:00
|
|
|
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 {
|
2018-01-24 19:17:44 -08:00
|
|
|
InvalidPartException(String s) {super(s);}
|
|
|
|
InvalidPartException(Exception e) {super(e);}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
2015-06-26 20:14:51 -07:00
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|