2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import android.support.annotation.NonNull;
|
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;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
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;
|
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;
|
2019-04-17 10:21:30 -04:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
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;
|
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;
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public class AttachmentDownloadJob extends BaseJob implements InjectableType {
|
|
|
|
|
|
|
|
public static final String KEY = "AttachmentDownloadJob";
|
2018-08-09 10:15:43 -04:00
|
|
|
|
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
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_MESSAGE_ID = "message_id";
|
|
|
|
private static final String KEY_PART_ROW_ID = "part_row_id";
|
|
|
|
private static final String KEY_PAR_UNIQUE_ID = "part_unique_id";
|
|
|
|
private static final String KEY_MANUAL = "part_manual";
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
@Inject SignalServiceMessageReceiver messageReceiver;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private long messageId;
|
|
|
|
private long partRowId;
|
|
|
|
private long partUniqueId;
|
|
|
|
private boolean manual;
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public AttachmentDownloadJob(long messageId, AttachmentId attachmentId, boolean manual) {
|
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.setQueue("AttachmentDownloadJob" + attachmentId.getRowId() + "-" + attachmentId.getUniqueId())
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setMaxAttempts(25)
|
|
|
|
.build(),
|
|
|
|
messageId,
|
|
|
|
attachmentId,
|
|
|
|
manual);
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
private AttachmentDownloadJob(@NonNull Job.Parameters parameters, long messageId, AttachmentId attachmentId, boolean manual) {
|
|
|
|
super(parameters);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return new Data.Builder().putLong(KEY_MESSAGE_ID, messageId)
|
|
|
|
.putLong(KEY_PART_ROW_ID, partRowId)
|
|
|
|
.putLong(KEY_PAR_UNIQUE_ID, partUniqueId)
|
|
|
|
.putBoolean(KEY_MANUAL, manual)
|
|
|
|
.build();
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
@Override
|
2015-08-24 15:24:31 -07:00
|
|
|
public void onAdded() {
|
2018-08-02 09:50:36 -04:00
|
|
|
Log.i(TAG, "onAdded() messageId: " + messageId + " partRowId: " + partRowId + " partUniqueId: " + partUniqueId + " manual: " + manual);
|
2018-11-08 23:33:37 -08:00
|
|
|
|
|
|
|
final AttachmentDatabase database = DatabaseFactory.getAttachmentDatabase(context);
|
|
|
|
final AttachmentId attachmentId = new AttachmentId(partRowId, partUniqueId);
|
|
|
|
final DatabaseAttachment attachment = database.getAttachment(attachmentId);
|
|
|
|
final boolean pending = attachment != null && attachment.getTransferState() != AttachmentDatabase.TRANSFER_PROGRESS_DONE;
|
|
|
|
|
|
|
|
if (pending && (manual || AttachmentUtil.isAutoDownloadPermitted(context, attachment))) {
|
|
|
|
Log.i(TAG, "onAdded() Marking attachment progress as 'started'");
|
|
|
|
database.setTransferState(messageId, attachmentId, AttachmentDatabase.TRANSFER_PROGRESS_STARTED);
|
|
|
|
}
|
2015-08-24 15:24:31 -07:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public void onRun() throws IOException {
|
2019-04-17 10:21:30 -04:00
|
|
|
doWork();
|
|
|
|
MessageNotifier.updateNotification(context, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void doWork() throws IOException {
|
2018-08-02 09:50:36 -04:00
|
|
|
Log.i(TAG, "onRun() messageId: " + messageId + " partRowId: " + partRowId + " partUniqueId: " + partUniqueId + " manual: " + manual);
|
|
|
|
|
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...");
|
2018-10-07 14:21:33 -07:00
|
|
|
database.setTransferState(messageId, attachmentId, AttachmentDatabase.TRANSFER_PROGRESS_PENDING);
|
2017-07-21 15:59:27 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(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);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
2018-08-02 09:50:36 -04:00
|
|
|
Log.w(TAG, "onCanceled() messageId: " + messageId + " partRowId: " + partRowId + " partUniqueId: " + partUniqueId + " manual: " + manual);
|
|
|
|
|
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
|
2019-05-22 13:51:56 -03:00
|
|
|
protected boolean onShouldRetry(@NonNull 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) {
|
2018-08-02 09:50:36 -04:00
|
|
|
Log.w(TAG, "Experienced exception while trying to download an attachment.", 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
|
|
|
|
{
|
2019-10-21 10:52:53 +11:00
|
|
|
boolean isPublicAttachment = TextUtils.isEmpty(attachment.getKey()) && attachment.getDigest() == null;
|
|
|
|
|
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
|
|
|
|
2019-10-21 10:52:53 +11:00
|
|
|
if (TextUtils.isEmpty(attachment.getKey()) && !isPublicAttachment) {
|
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());
|
2019-10-21 10:52:53 +11:00
|
|
|
if (isPublicAttachment) {
|
|
|
|
return new SignalServiceAttachmentPointer(id, null, new byte[0],
|
|
|
|
Optional.of(Util.toIntExact(attachment.getSize())),
|
|
|
|
Optional.absent(),
|
2020-05-11 16:54:31 +10:00
|
|
|
0,
|
|
|
|
0,
|
2019-10-21 10:52:53 +11:00
|
|
|
Optional.fromNullable(attachment.getDigest()),
|
|
|
|
Optional.fromNullable(attachment.getFileName()),
|
|
|
|
attachment.isVoiceNote(),
|
|
|
|
Optional.absent(), attachment.getUrl());
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
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) {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Downloading attachment with digest: " + Hex.toString(attachment.getDigest()));
|
2017-02-26 10:06:27 -08:00
|
|
|
} else {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Downloading attachment with no digest...");
|
2017-02-26 10:06:27 -08:00
|
|
|
}
|
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
return new SignalServiceAttachmentPointer(id, null, key,
|
2017-11-01 15:56:31 -07:00
|
|
|
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()),
|
2018-11-09 12:17:14 -08:00
|
|
|
attachment.isVoiceNote(),
|
2019-10-17 10:39:59 +11:00
|
|
|
Optional.absent(), attachment.getUrl());
|
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
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public static final class Factory implements Job.Factory<AttachmentDownloadJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull AttachmentDownloadJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
|
|
return new AttachmentDownloadJob(parameters,
|
|
|
|
data.getLong(KEY_MESSAGE_ID),
|
|
|
|
new AttachmentId(data.getLong(KEY_PART_ROW_ID), data.getLong(KEY_PAR_UNIQUE_ID)),
|
|
|
|
data.getBoolean(KEY_MANUAL));
|
|
|
|
}
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|