2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
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
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2014-11-03 15:16:04 -08: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;
|
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;
|
2018-06-18 12:27:04 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
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;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import androidx.work.Data;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
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
|
|
|
|
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";
|
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
@Inject transient 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;
|
|
|
|
|
|
|
|
public AttachmentDownloadJob() {
|
|
|
|
super(null, null);
|
|
|
|
}
|
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())
|
2018-08-09 10:15:43 -04:00
|
|
|
.withMasterSecretRequirement()
|
|
|
|
.withNetworkRequirement()
|
2014-11-03 15:16:04 -08:00
|
|
|
.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
|
|
|
}
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
@Override
|
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
messageId = data.getLong(KEY_MESSAGE_ID);
|
|
|
|
partRowId = data.getLong(KEY_PART_ROW_ID);
|
|
|
|
partUniqueId = data.getLong(KEY_PAR_UNIQUE_ID);
|
2018-10-02 12:31:12 -07:00
|
|
|
manual = data.getBoolean(KEY_MANUAL);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.putLong(KEY_MESSAGE_ID, messageId)
|
|
|
|
.putLong(KEY_PART_ROW_ID, partRowId)
|
|
|
|
.putLong(KEY_PAR_UNIQUE_ID, partUniqueId)
|
|
|
|
.putBoolean(KEY_MANUAL, manual)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
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);
|
2015-08-24 15:24:31 -07:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
@Override
|
2014-11-11 19:57:53 -08:00
|
|
|
public void onRun(MasterSecret masterSecret) 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);
|
|
|
|
MessageNotifier.updateNotification(context);
|
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
|
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) {
|
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
|
|
|
|
{
|
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) {
|
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()),
|
|
|
|
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
|
|
|
}
|