2015-01-12 04:27:34 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-10-13 01:25:05 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2015-01-12 04:27:34 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.BuildConfig;
|
|
|
|
import org.thoughtcrime.securesms.TextSecureExpiredException;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2015-01-12 04:27:34 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-10-13 01:25:05 +00:00
|
|
|
import org.thoughtcrime.securesms.database.AttachmentDatabase;
|
2017-05-08 22:32:59 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2015-01-02 23:43:28 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MediaConstraints;
|
2015-11-23 04:18:54 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MediaStream;
|
2017-05-08 22:32:59 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsException;
|
2015-01-02 23:43:28 +00:00
|
|
|
import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
|
2015-01-12 04:27:34 +00:00
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
|
2015-01-02 23:43:28 +00:00
|
|
|
import java.io.IOException;
|
2015-10-13 01:25:05 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2015-01-02 23:43:28 +00:00
|
|
|
|
2015-01-12 04:27:34 +00:00
|
|
|
public abstract class SendJob extends MasterSecretJob {
|
2015-10-13 01:25:05 +00:00
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
@SuppressWarnings("unused")
|
2015-01-02 23:43:28 +00:00
|
|
|
private final static String TAG = SendJob.class.getSimpleName();
|
2015-01-12 04:27:34 +00:00
|
|
|
|
|
|
|
public SendJob(Context context, JobParameters parameters) {
|
|
|
|
super(context, parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public final void onRun(MasterSecret masterSecret) throws Exception {
|
2015-10-28 14:46:47 +00:00
|
|
|
if (Util.getDaysTillBuildExpiry() <= 0) {
|
2015-01-12 04:27:34 +00:00
|
|
|
throw new TextSecureExpiredException(String.format("TextSecure expired (build %d, now %d)",
|
|
|
|
BuildConfig.BUILD_TIMESTAMP,
|
|
|
|
System.currentTimeMillis()));
|
|
|
|
}
|
|
|
|
|
|
|
|
onSend(masterSecret);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract void onSend(MasterSecret masterSecret) throws Exception;
|
2015-01-02 23:43:28 +00:00
|
|
|
|
2015-10-13 01:25:05 +00: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 23:43:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
protected List<Attachment> scaleAttachments(@NonNull MediaConstraints constraints,
|
2015-10-13 01:25:05 +00:00
|
|
|
@NonNull List<Attachment> attachments)
|
|
|
|
throws UndeliverableMessageException
|
2015-01-02 23:43:28 +00:00
|
|
|
{
|
2015-10-13 01:25:05 +00:00
|
|
|
AttachmentDatabase attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context);
|
|
|
|
List<Attachment> results = new LinkedList<>();
|
|
|
|
|
|
|
|
for (Attachment attachment : attachments) {
|
|
|
|
try {
|
2018-01-25 03:17:44 +00:00
|
|
|
if (constraints.isSatisfied(context, attachment)) {
|
2015-10-13 01:25:05 +00:00
|
|
|
results.add(attachment);
|
|
|
|
} else if (constraints.canResize(attachment)) {
|
2018-01-25 03:17:44 +00:00
|
|
|
MediaStream resized = constraints.getResizedMedia(context, attachment);
|
|
|
|
results.add(attachmentDatabase.updateAttachmentData(attachment, resized));
|
2015-10-13 01:25:05 +00:00
|
|
|
} else {
|
|
|
|
throw new UndeliverableMessageException("Size constraints could not be met!");
|
|
|
|
}
|
|
|
|
} catch (IOException | MmsException e) {
|
|
|
|
throw new UndeliverableMessageException(e);
|
2015-01-02 23:43:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-13 01:25:05 +00:00
|
|
|
return results;
|
2015-01-02 23:43:28 +00:00
|
|
|
}
|
2015-01-12 04:27:34 +00:00
|
|
|
}
|