2015-01-11 20:27:34 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2018-12-06 12:14:20 -08:00
|
|
|
import android.support.annotation.Nullable;
|
2015-01-11 20:27:34 -08:00
|
|
|
|
2019-07-24 12:30:23 +10:00
|
|
|
import network.loki.messenger.BuildConfig;
|
2015-01-11 20:27:34 -08:00
|
|
|
import org.thoughtcrime.securesms.TextSecureExpiredException;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
2017-05-08 15:32:59 -07:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2019-03-28 08:56:35 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobLogger;
|
2018-08-02 09:50:36 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2015-01-02 15:43:28 -08:00
|
|
|
import org.thoughtcrime.securesms.mms.MediaConstraints;
|
2015-11-23 13:18:54 +09:00
|
|
|
import org.thoughtcrime.securesms.mms.MediaStream;
|
2017-05-08 15:32:59 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsException;
|
2015-01-02 15:43:28 -08:00
|
|
|
import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
|
2018-03-19 11:22:39 -07:00
|
|
|
import org.thoughtcrime.securesms.util.MediaUtil;
|
2015-01-11 20:27:34 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
|
2015-01-02 15:43:28 -08:00
|
|
|
import java.io.IOException;
|
2015-10-12 18:25:05 -07:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2015-01-02 15:43:28 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public abstract class SendJob extends BaseJob {
|
2015-10-12 18:25:05 -07:00
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
@SuppressWarnings("unused")
|
2015-01-02 15:43:28 -08:00
|
|
|
private final static String TAG = SendJob.class.getSimpleName();
|
2015-01-11 20:27:34 -08:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public SendJob(Job.Parameters parameters) {
|
|
|
|
super(parameters);
|
2015-01-11 20:27:34 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public final void onRun() throws Exception {
|
2015-10-28 15:46:47 +01:00
|
|
|
if (Util.getDaysTillBuildExpiry() <= 0) {
|
2015-01-11 20:27:34 -08:00
|
|
|
throw new TextSecureExpiredException(String.format("TextSecure expired (build %d, now %d)",
|
|
|
|
BuildConfig.BUILD_TIMESTAMP,
|
|
|
|
System.currentTimeMillis()));
|
|
|
|
}
|
|
|
|
|
2018-08-02 09:50:36 -04:00
|
|
|
Log.i(TAG, "Starting message send attempt");
|
2018-11-15 12:05:08 -08:00
|
|
|
onSend();
|
2018-08-02 09:50:36 -04:00
|
|
|
Log.i(TAG, "Message send completed");
|
2015-01-11 20:27:34 -08:00
|
|
|
}
|
|
|
|
|
2018-11-15 12:05:08 -08:00
|
|
|
protected abstract void onSend() throws Exception;
|
2015-01-02 15:43:28 -08:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
protected void markAttachmentsUploaded(long messageId, @NonNull List<Attachment> attachments) {
|
|
|
|
AttachmentDatabase database = DatabaseFactory.getAttachmentDatabase(context);
|
|
|
|
|
|
|
|
for (Attachment attachment : attachments) {
|
|
|
|
database.markAttachmentUploaded(messageId, attachment);
|
2015-01-02 15:43:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-19 11:22:39 -07:00
|
|
|
protected List<Attachment> scaleAndStripExifFromAttachments(@NonNull MediaConstraints constraints,
|
|
|
|
@NonNull List<Attachment> attachments)
|
2015-10-12 18:25:05 -07:00
|
|
|
throws UndeliverableMessageException
|
2015-01-02 15:43:28 -08:00
|
|
|
{
|
2015-10-12 18:25:05 -07:00
|
|
|
AttachmentDatabase attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context);
|
|
|
|
List<Attachment> results = new LinkedList<>();
|
|
|
|
|
|
|
|
for (Attachment attachment : attachments) {
|
|
|
|
try {
|
2018-01-24 19:17:44 -08:00
|
|
|
if (constraints.isSatisfied(context, attachment)) {
|
2018-03-19 11:22:39 -07:00
|
|
|
if (MediaUtil.isJpeg(attachment)) {
|
|
|
|
MediaStream stripped = constraints.getResizedMedia(context, attachment);
|
|
|
|
results.add(attachmentDatabase.updateAttachmentData(attachment, stripped));
|
|
|
|
} else {
|
|
|
|
results.add(attachment);
|
|
|
|
}
|
2015-10-12 18:25:05 -07:00
|
|
|
} else if (constraints.canResize(attachment)) {
|
2018-01-24 19:17:44 -08:00
|
|
|
MediaStream resized = constraints.getResizedMedia(context, attachment);
|
|
|
|
results.add(attachmentDatabase.updateAttachmentData(attachment, resized));
|
2015-10-12 18:25:05 -07:00
|
|
|
} else {
|
|
|
|
throw new UndeliverableMessageException("Size constraints could not be met!");
|
|
|
|
}
|
|
|
|
} catch (IOException | MmsException e) {
|
|
|
|
throw new UndeliverableMessageException(e);
|
2015-01-02 15:43:28 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
return results;
|
2015-01-02 15:43:28 -08:00
|
|
|
}
|
2018-12-06 12:14:20 -08:00
|
|
|
|
|
|
|
protected void log(@NonNull String tag, @NonNull String message) {
|
2019-03-28 08:56:35 -07:00
|
|
|
Log.i(tag, JobLogger.format(this, message));
|
2018-12-06 12:14:20 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected void warn(@NonNull String tag, @NonNull String message) {
|
|
|
|
warn(tag, message, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void warn(@NonNull String tag, @Nullable Throwable t) {
|
|
|
|
warn(tag, "", t);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void warn(@NonNull String tag, @NonNull String message, @Nullable Throwable t) {
|
2019-03-28 08:56:35 -07:00
|
|
|
Log.w(tag, JobLogger.format(this, message), t);
|
2018-12-06 12:14:20 -08:00
|
|
|
}
|
2015-01-11 20:27:34 -08:00
|
|
|
}
|