2014-11-08 11:35:58 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.attachments.Attachment;
|
2014-12-12 01:03:24 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2015-01-19 20:24:10 -08:00
|
|
|
import org.thoughtcrime.securesms.database.TextSecureDirectory;
|
2015-11-02 14:32:02 -08:00
|
|
|
import org.thoughtcrime.securesms.events.PartProgressEvent;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2014-12-12 01:03:24 -08:00
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2015-03-03 11:44:49 -08:00
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
|
2015-06-26 20:14:51 -07:00
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureAttachment.ProgressListener;
|
2015-02-27 16:57:32 -08:00
|
|
|
import org.whispersystems.textsecure.api.push.TextSecureAddress;
|
2014-11-12 11:15:05 -08:00
|
|
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2014-12-12 01:03:24 -08:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2014-11-08 11:35:58 -08:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2015-06-26 20:14:51 -07:00
|
|
|
import de.greenrobot.event.EventBus;
|
2014-11-08 11:35:58 -08:00
|
|
|
import ws.com.google.android.mms.ContentType;
|
|
|
|
|
2015-01-11 20:27:34 -08:00
|
|
|
public abstract class PushSendJob extends SendJob {
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
private static final String TAG = PushSendJob.class.getSimpleName();
|
|
|
|
|
|
|
|
protected PushSendJob(Context context, JobParameters parameters) {
|
|
|
|
super(context, parameters);
|
|
|
|
}
|
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
protected static JobParameters constructParameters(Context context, String destination) {
|
2014-11-08 11:35:58 -08:00
|
|
|
JobParameters.Builder builder = JobParameters.newBuilder();
|
|
|
|
builder.withPersistence();
|
|
|
|
builder.withGroupId(destination);
|
|
|
|
builder.withRequirement(new MasterSecretRequirement(context));
|
2015-03-11 14:23:45 -07:00
|
|
|
builder.withRequirement(new NetworkRequirement(context));
|
|
|
|
builder.withRetryCount(5);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
return builder.create();
|
|
|
|
}
|
|
|
|
|
2015-03-03 11:44:49 -08:00
|
|
|
protected TextSecureAddress getPushAddress(String number) throws InvalidNumberException {
|
|
|
|
String e164number = Util.canonicalizeNumber(context, number);
|
2014-11-09 20:35:08 -08:00
|
|
|
String relay = TextSecureDirectory.getInstance(context).getRelay(e164number);
|
2015-03-03 11:44:49 -08:00
|
|
|
return new TextSecureAddress(e164number, Optional.fromNullable(relay));
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
protected List<TextSecureAttachment> getAttachmentsFor(MasterSecret masterSecret, List<Attachment> parts) {
|
2014-11-08 11:35:58 -08:00
|
|
|
List<TextSecureAttachment> attachments = new LinkedList<>();
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
for (final Attachment attachment : parts) {
|
|
|
|
if (ContentType.isImageType(attachment.getContentType()) ||
|
|
|
|
ContentType.isAudioType(attachment.getContentType()) ||
|
|
|
|
ContentType.isVideoType(attachment.getContentType()))
|
2014-11-08 11:35:58 -08:00
|
|
|
{
|
2014-12-12 01:03:24 -08:00
|
|
|
try {
|
2015-10-12 18:25:05 -07:00
|
|
|
if (attachment.getDataUri() == null) throw new IOException("Assertion failed, outgoing attachment has no data!");
|
|
|
|
InputStream is = PartAuthority.getAttachmentStream(context, masterSecret, attachment.getDataUri());
|
2015-07-01 15:39:18 -07:00
|
|
|
attachments.add(TextSecureAttachment.newStreamBuilder()
|
|
|
|
.withStream(is)
|
2015-10-12 18:25:05 -07:00
|
|
|
.withContentType(attachment.getContentType())
|
|
|
|
.withLength(attachment.getSize())
|
2015-07-01 15:39:18 -07:00
|
|
|
.withListener(new ProgressListener() {
|
2015-10-12 18:25:05 -07:00
|
|
|
@Override
|
|
|
|
public void onAttachmentProgress(long total, long progress) {
|
|
|
|
EventBus.getDefault().postSticky(new PartProgressEvent(attachment, total, progress));
|
2015-07-01 15:39:18 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.build());
|
2014-12-12 01:03:24 -08:00
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, "Couldn't open attachment", ioe);
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attachments;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void notifyMediaMessageDeliveryFailed(Context context, long messageId) {
|
|
|
|
long threadId = DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(messageId);
|
|
|
|
Recipients recipients = DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId);
|
|
|
|
|
2015-04-14 10:01:33 -07:00
|
|
|
if (threadId != -1 && recipients != null) {
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
}
|