Signal-Android/app/src/main/java/org/thoughtcrime/securesms/jobs/AttachmentUploadJob.java

251 wiersze
11 KiB
Java
Czysty Zwykły widok Historia

2018-12-08 02:31:39 +00:00
package org.thoughtcrime.securesms.jobs;
import android.graphics.Bitmap;
import android.media.MediaDataSource;
import android.media.MediaMetadataRetriever;
import android.os.Build;
2019-06-05 19:47:14 +00:00
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
2018-12-08 02:31:39 +00:00
import org.greenrobot.eventbus.EventBus;
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;
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;
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;
import org.thoughtcrime.securesms.service.GenericForegroundService;
import org.thoughtcrime.securesms.service.NotificationController;
import org.thoughtcrime.securesms.util.FeatureFlags;
import org.thoughtcrime.securesms.util.MediaMetadataRetrieverUtil;
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;
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
private static final long UPLOAD_REUSE_THRESHOLD = TimeUnit.DAYS.toMillis(3);
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
/**
* 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 {
final ResumableUploadSpec resumableUploadSpec;
if (FeatureFlags.attachmentsV3()) {
Data inputData = requireInputData();
if (!inputData.hasString(ResumableUploadSpecJob.KEY_RESUME_SPEC)) {
throw new ResumeLocationInvalidException("V3 Attachment upload requires a ResumableUploadSpec");
}
resumableUploadSpec = ResumableUploadSpec.deserialize(inputData.getString(ResumableUploadSpecJob.KEY_RESUME_SPEC));
} else {
resumableUploadSpec = null;
}
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) {
throw new InvalidAttachmentException("Cannot find the specified attachment.");
2018-12-08 02:31:39 +00:00
}
long timeSinceUpload = System.currentTimeMillis() - databaseAttachment.getUploadTimestamp();
if (timeSinceUpload < UPLOAD_REUSE_THRESHOLD) {
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");
}
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)) {
SignalServiceAttachment localAttachment = getAttachmentFor(databaseAttachment, notification, resumableUploadSpec);
2019-09-23 20:24:55 +00:00
SignalServiceAttachmentPointer remoteAttachment = messageSender.uploadAttachment(localAttachment.asStream());
Attachment attachment = PointerAttachment.forPointer(Optional.of(remoteAttachment), null, databaseAttachment.getFastPreflightId()).get();
database.updateAttachmentAfterUpload(databaseAttachment.getAttachmentId(), attachment, remoteAttachment.getUploadTimestamp());
}
}
2018-12-08 02:31:39 +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
public void onFailure() {
if (isCanceled()) {
DatabaseFactory.getAttachmentDatabase(context).deleteAttachment(attachmentId);
}
}
2018-12-08 02:31:39 +00:00
@Override
protected boolean onShouldRetry(@NonNull Exception exception) {
if (exception instanceof ResumeLocationInvalidException) return false;
return exception instanceof IOException;
2018-12-08 02:31:39 +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());
SignalServiceAttachment.Builder builder = SignalServiceAttachment.newStreamBuilder()
.withStream(is)
.withContentType(attachment.getContentType())
.withLength(attachment.getSize())
.withFileName(attachment.getFileName())
.withVoiceNote(attachment.isVoiceNote())
.withWidth(attachment.getWidth())
.withHeight(attachment.getHeight())
.withUploadTimestamp(System.currentTimeMillis())
.withCaption(attachment.getCaption())
.withCancelationSignal(this::isCanceled)
.withResumableUploadSpec(resumableUploadSpec)
.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) {
throw new InvalidAttachmentException(ioe);
2018-12-08 02:31:39 +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();
MediaMetadataRetrieverUtil.setDataSource(retriever, dataSource);
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;
}
}
}
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
}