2018-12-08 02:31:39 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2019-10-17 12:26:08 +00:00
|
|
|
import android.graphics.Bitmap;
|
|
|
|
import android.media.MediaDataSource;
|
|
|
|
import android.media.MediaMetadataRetriever;
|
|
|
|
import android.os.Build;
|
2020-04-30 18:04:53 +00:00
|
|
|
import android.text.TextUtils;
|
2019-10-17 12:26:08 +00:00
|
|
|
|
2019-06-05 19:47:14 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2019-06-27 16:18:52 +00:00
|
|
|
import androidx.annotation.Nullable;
|
2020-04-16 20:06:18 +00:00
|
|
|
import androidx.annotation.WorkerThread;
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
import org.greenrobot.eventbus.EventBus;
|
2019-06-27 16:18:52 +00:00
|
|
|
import org.thoughtcrime.securesms.R;
|
2018-12-08 02:31:39 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
|
|
|
import org.thoughtcrime.securesms.attachments.AttachmentId;
|
|
|
|
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
|
|
|
import org.thoughtcrime.securesms.attachments.PointerAttachment;
|
2019-10-17 12:26:08 +00:00
|
|
|
import org.thoughtcrime.securesms.blurhash.BlurHashEncoder;
|
2018-12-08 02:31:39 +00:00
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2019-07-15 15:12:26 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
2018-12-08 02:31:39 +00:00
|
|
|
import org.thoughtcrime.securesms.events.PartProgressEvent;
|
2019-03-28 15:56:35 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
2018-12-08 02:31:39 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
2019-06-27 16:18:52 +00:00
|
|
|
import org.thoughtcrime.securesms.service.GenericForegroundService;
|
|
|
|
import org.thoughtcrime.securesms.service.NotificationController;
|
2020-04-16 20:06:18 +00:00
|
|
|
import org.thoughtcrime.securesms.util.FeatureFlags;
|
2019-10-19 16:23:11 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MediaMetadataRetrieverUtil;
|
2019-10-17 12:26:08 +00:00
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
2018-12-08 02:31:39 +00:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
2020-04-16 20:06:18 +00:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.ResumeLocationInvalidException;
|
|
|
|
import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
/**
|
|
|
|
* Uploads an attachment without alteration.
|
|
|
|
* <p>
|
|
|
|
* Queue {@link AttachmentCompressionJob} before to compress.
|
|
|
|
*/
|
|
|
|
public final class AttachmentUploadJob extends BaseJob {
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
public static final String KEY = "AttachmentUploadJobV2";
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
@SuppressWarnings("unused")
|
|
|
|
private static final String TAG = Log.tag(AttachmentUploadJob.class);
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2020-03-28 00:36:06 +00:00
|
|
|
private static final long UPLOAD_REUSE_THRESHOLD = TimeUnit.DAYS.toMillis(3);
|
|
|
|
|
2020-04-16 20:06:18 +00:00
|
|
|
private static final String KEY_ROW_ID = "row_id";
|
|
|
|
private static final String KEY_UNIQUE_ID = "unique_id";
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2019-06-27 16:18:52 +00:00
|
|
|
/**
|
|
|
|
* Foreground notification shows while uploading attachments above this.
|
|
|
|
*/
|
|
|
|
private static final int FOREGROUND_LIMIT = 10 * 1024 * 1024;
|
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
private final AttachmentId attachmentId;
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
public AttachmentUploadJob(AttachmentId attachmentId) {
|
2019-03-28 15:56:35 +00:00
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setLifespan(TimeUnit.DAYS.toMillis(1))
|
|
|
|
.setMaxAttempts(Parameters.UNLIMITED)
|
|
|
|
.build(),
|
|
|
|
attachmentId);
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
private AttachmentUploadJob(@NonNull Job.Parameters parameters, @NonNull AttachmentId attachmentId) {
|
|
|
|
super(parameters);
|
2018-12-08 02:31:39 +00:00
|
|
|
this.attachmentId = attachmentId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return new Data.Builder().putLong(KEY_ROW_ID, attachmentId.getRowId())
|
|
|
|
.putLong(KEY_UNIQUE_ID, attachmentId.getUniqueId())
|
|
|
|
.build();
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws Exception {
|
2020-07-21 19:02:57 +00:00
|
|
|
Data inputData = getInputData();
|
2020-04-16 20:06:18 +00:00
|
|
|
|
2020-07-21 19:02:57 +00:00
|
|
|
ResumableUploadSpec resumableUploadSpec;
|
|
|
|
|
|
|
|
if (inputData != null && inputData.hasString(ResumableUploadSpecJob.KEY_RESUME_SPEC)) {
|
|
|
|
Log.d(TAG, "Using attachments V3");
|
2020-04-16 20:06:18 +00:00
|
|
|
resumableUploadSpec = ResumableUploadSpec.deserialize(inputData.getString(ResumableUploadSpecJob.KEY_RESUME_SPEC));
|
|
|
|
} else {
|
2020-07-21 19:02:57 +00:00
|
|
|
Log.d(TAG, "Using attachments V2");
|
2020-04-16 20:06:18 +00:00
|
|
|
resumableUploadSpec = null;
|
|
|
|
}
|
|
|
|
|
2019-07-15 15:12:26 +00:00
|
|
|
SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
|
|
|
|
AttachmentDatabase database = DatabaseFactory.getAttachmentDatabase(context);
|
|
|
|
DatabaseAttachment databaseAttachment = database.getAttachment(attachmentId);
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
if (databaseAttachment == null) {
|
2019-08-02 20:31:10 +00:00
|
|
|
throw new InvalidAttachmentException("Cannot find the specified attachment.");
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 00:36:06 +00:00
|
|
|
long timeSinceUpload = System.currentTimeMillis() - databaseAttachment.getUploadTimestamp();
|
2020-04-30 18:04:53 +00:00
|
|
|
if (timeSinceUpload < UPLOAD_REUSE_THRESHOLD && !TextUtils.isEmpty(databaseAttachment.getLocation())) {
|
2020-03-28 00:36:06 +00:00
|
|
|
Log.i(TAG, "We can re-use an already-uploaded file. It was uploaded " + timeSinceUpload + " ms ago. Skipping.");
|
|
|
|
return;
|
|
|
|
} else if (databaseAttachment.getUploadTimestamp() > 0) {
|
|
|
|
Log.i(TAG, "This file was previously-uploaded, but too long ago to be re-used. Age: " + timeSinceUpload + " ms");
|
|
|
|
}
|
|
|
|
|
2019-10-22 14:52:55 +00:00
|
|
|
Log.i(TAG, "Uploading attachment for message " + databaseAttachment.getMmsId() + " with ID " + databaseAttachment.getAttachmentId());
|
|
|
|
|
2019-08-06 20:52:15 +00:00
|
|
|
try (NotificationController notification = getNotificationForAttachment(databaseAttachment)) {
|
2020-04-16 20:06:18 +00:00
|
|
|
SignalServiceAttachment localAttachment = getAttachmentFor(databaseAttachment, notification, resumableUploadSpec);
|
2019-09-23 20:24:55 +00:00
|
|
|
SignalServiceAttachmentPointer remoteAttachment = messageSender.uploadAttachment(localAttachment.asStream());
|
2019-06-27 16:18:52 +00:00
|
|
|
Attachment attachment = PointerAttachment.forPointer(Optional.of(remoteAttachment), null, databaseAttachment.getFastPreflightId()).get();
|
|
|
|
|
2020-03-28 00:36:06 +00:00
|
|
|
database.updateAttachmentAfterUpload(databaseAttachment.getAttachmentId(), attachment, remoteAttachment.getUploadTimestamp());
|
2019-06-27 16:18:52 +00:00
|
|
|
}
|
|
|
|
}
|
2018-12-08 02:31:39 +00:00
|
|
|
|
2019-06-27 16:18:52 +00:00
|
|
|
private @Nullable NotificationController getNotificationForAttachment(@NonNull Attachment attachment) {
|
|
|
|
if (attachment.getSize() >= FOREGROUND_LIMIT) {
|
|
|
|
return GenericForegroundService.startForegroundTask(context, context.getString(R.string.AttachmentUploadJob_uploading_media));
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-08 20:56:51 +00:00
|
|
|
public void onFailure() {
|
|
|
|
if (isCanceled()) {
|
|
|
|
DatabaseFactory.getAttachmentDatabase(context).deleteAttachment(attachmentId);
|
|
|
|
}
|
|
|
|
}
|
2018-12-08 02:31:39 +00:00
|
|
|
|
|
|
|
@Override
|
2019-05-22 16:51:56 +00:00
|
|
|
protected boolean onShouldRetry(@NonNull Exception exception) {
|
2020-04-16 20:06:18 +00:00
|
|
|
if (exception instanceof ResumeLocationInvalidException) return false;
|
|
|
|
|
2019-04-17 14:21:30 +00:00
|
|
|
return exception instanceof IOException;
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 20:06:18 +00:00
|
|
|
private @NonNull SignalServiceAttachment getAttachmentFor(Attachment attachment, @Nullable NotificationController notification, @Nullable ResumableUploadSpec resumableUploadSpec) throws InvalidAttachmentException {
|
2018-12-08 02:31:39 +00:00
|
|
|
try {
|
|
|
|
if (attachment.getDataUri() == null || attachment.getSize() == 0) throw new IOException("Assertion failed, outgoing attachment has no data!");
|
|
|
|
InputStream is = PartAuthority.getAttachmentStream(context, attachment.getDataUri());
|
2019-10-17 12:26:08 +00:00
|
|
|
SignalServiceAttachment.Builder builder = SignalServiceAttachment.newStreamBuilder()
|
|
|
|
.withStream(is)
|
|
|
|
.withContentType(attachment.getContentType())
|
|
|
|
.withLength(attachment.getSize())
|
|
|
|
.withFileName(attachment.getFileName())
|
|
|
|
.withVoiceNote(attachment.isVoiceNote())
|
2020-07-06 21:13:08 +00:00
|
|
|
.withBorderless(attachment.isBorderless())
|
2019-10-17 12:26:08 +00:00
|
|
|
.withWidth(attachment.getWidth())
|
|
|
|
.withHeight(attachment.getHeight())
|
2020-03-28 00:36:06 +00:00
|
|
|
.withUploadTimestamp(System.currentTimeMillis())
|
2019-10-17 12:26:08 +00:00
|
|
|
.withCaption(attachment.getCaption())
|
2020-01-08 20:56:51 +00:00
|
|
|
.withCancelationSignal(this::isCanceled)
|
2020-04-16 20:06:18 +00:00
|
|
|
.withResumableUploadSpec(resumableUploadSpec)
|
2019-10-17 12:26:08 +00:00
|
|
|
.withListener((total, progress) -> {
|
|
|
|
EventBus.getDefault().postSticky(new PartProgressEvent(attachment, PartProgressEvent.Type.NETWORK, total, progress));
|
|
|
|
if (notification != null) {
|
|
|
|
notification.setProgress(total, progress);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (MediaUtil.isImageType(attachment.getContentType())) {
|
|
|
|
return builder.withBlurHash(getImageBlurHash(attachment)).build();
|
|
|
|
} else if (MediaUtil.isVideoType(attachment.getContentType())) {
|
|
|
|
return builder.withBlurHash(getVideoBlurHash(attachment)).build();
|
|
|
|
} else {
|
|
|
|
return builder.build();
|
|
|
|
}
|
|
|
|
|
2018-12-08 02:31:39 +00:00
|
|
|
} catch (IOException ioe) {
|
2019-08-02 20:31:10 +00:00
|
|
|
throw new InvalidAttachmentException(ioe);
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-17 12:26:08 +00:00
|
|
|
private @Nullable String getImageBlurHash(@NonNull Attachment attachment) throws IOException {
|
|
|
|
if (attachment.getBlurHash() != null) return attachment.getBlurHash().getHash();
|
|
|
|
if (attachment.getDataUri() == null) return null;
|
|
|
|
|
|
|
|
return BlurHashEncoder.encode(PartAuthority.getAttachmentStream(context, attachment.getDataUri()));
|
|
|
|
}
|
|
|
|
|
|
|
|
private @Nullable String getVideoBlurHash(@NonNull Attachment attachment) throws IOException {
|
|
|
|
if (attachment.getThumbnailUri() != null) {
|
|
|
|
return BlurHashEncoder.encode(PartAuthority.getAttachmentStream(context, attachment.getThumbnailUri()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (attachment.getBlurHash() != null) return attachment.getBlurHash().getHash();
|
|
|
|
|
|
|
|
if (Build.VERSION.SDK_INT < 23) {
|
|
|
|
Log.w(TAG, "Video thumbnails not supported...");
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try (MediaDataSource dataSource = DatabaseFactory.getAttachmentDatabase(context).mediaDataSourceFor(attachmentId)) {
|
|
|
|
if (dataSource == null) return null;
|
|
|
|
|
|
|
|
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
|
2019-10-19 16:23:11 +00:00
|
|
|
MediaMetadataRetrieverUtil.setDataSource(retriever, dataSource);
|
2019-10-17 12:26:08 +00:00
|
|
|
|
|
|
|
Bitmap bitmap = retriever.getFrameAtTime(1000);
|
|
|
|
|
|
|
|
if (bitmap != null) {
|
|
|
|
Bitmap thumb = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
|
|
|
|
bitmap.recycle();
|
|
|
|
|
|
|
|
Log.i(TAG, "Generated video thumbnail...");
|
|
|
|
String hash = BlurHashEncoder.encode(thumb);
|
|
|
|
thumb.recycle();
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 20:31:10 +00:00
|
|
|
private class InvalidAttachmentException extends Exception {
|
|
|
|
InvalidAttachmentException(String message) {
|
|
|
|
super(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
InvalidAttachmentException(Exception e) {
|
|
|
|
super(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public static final class Factory implements Job.Factory<AttachmentUploadJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull AttachmentUploadJob create(@NonNull Parameters parameters, @NonNull org.thoughtcrime.securesms.jobmanager.Data data) {
|
|
|
|
return new AttachmentUploadJob(parameters, new AttachmentId(data.getLong(KEY_ROW_ID), data.getLong(KEY_UNIQUE_ID)));
|
|
|
|
}
|
|
|
|
}
|
2018-12-08 02:31:39 +00:00
|
|
|
}
|