session-android/src/org/thoughtcrime/securesms/jobs/SendJob.java

111 lines
4.0 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.jobs;
import android.content.Context;
import android.support.annotation.NonNull;
2018-12-06 20:14:20 +00:00
import android.support.annotation.Nullable;
import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.R;
import org.thoughtcrime.securesms.TextSecureExpiredException;
import org.thoughtcrime.securesms.attachments.Attachment;
import org.thoughtcrime.securesms.crypto.MasterSecret;
import org.thoughtcrime.securesms.database.AttachmentDatabase;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.jobmanager.JobParameters;
import org.thoughtcrime.securesms.logging.Log;
2015-01-02 23:43:28 +00:00
import org.thoughtcrime.securesms.mms.MediaConstraints;
import org.thoughtcrime.securesms.mms.MediaStream;
import org.thoughtcrime.securesms.mms.MmsException;
2015-01-02 23:43:28 +00:00
import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
import org.thoughtcrime.securesms.util.MediaUtil;
import org.thoughtcrime.securesms.util.Util;
2015-01-02 23:43:28 +00:00
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
2015-01-02 23:43:28 +00:00
2018-11-27 20:34:42 +00:00
import androidx.work.WorkerParameters;
public abstract class SendJob extends ContextJob {
@SuppressWarnings("unused")
2015-01-02 23:43:28 +00:00
private final static String TAG = SendJob.class.getSimpleName();
public SendJob(@NonNull Context context, @NonNull WorkerParameters workerParameters) {
2018-11-27 20:34:42 +00:00
super(context, workerParameters);
}
public SendJob(Context context, JobParameters parameters) {
super(context, parameters);
}
@Override
public final void onRun() throws Exception {
if (Util.getDaysTillBuildExpiry() <= 0) {
throw new TextSecureExpiredException(String.format("TextSecure expired (build %d, now %d)",
BuildConfig.BUILD_TIMESTAMP,
System.currentTimeMillis()));
}
Log.i(TAG, "Starting message send attempt");
onSend();
Log.i(TAG, "Message send completed");
}
protected abstract void onSend() throws Exception;
2015-01-02 23:43:28 +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
}
}
protected List<Attachment> scaleAndStripExifFromAttachments(@NonNull MediaConstraints constraints,
@NonNull List<Attachment> attachments)
throws UndeliverableMessageException
2015-01-02 23:43:28 +00:00
{
AttachmentDatabase attachmentDatabase = DatabaseFactory.getAttachmentDatabase(context);
List<Attachment> results = new LinkedList<>();
for (Attachment attachment : attachments) {
try {
if (constraints.isSatisfied(context, attachment)) {
if (MediaUtil.isJpeg(attachment)) {
MediaStream stripped = constraints.getResizedMedia(context, attachment);
results.add(attachmentDatabase.updateAttachmentData(attachment, stripped));
} else {
results.add(attachment);
}
} else if (constraints.canResize(attachment)) {
MediaStream resized = constraints.getResizedMedia(context, attachment);
results.add(attachmentDatabase.updateAttachmentData(attachment, resized));
} 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
}
}
return results;
2015-01-02 23:43:28 +00:00
}
2018-12-06 20:14:20 +00:00
protected void log(@NonNull String tag, @NonNull String message) {
Log.i(tag, "[" + getId().toString() + "] " + message + logSuffix());
}
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) {
Log.w(tag, "[" + getId().toString() + "] " + message + logSuffix(), t);
}
}