2014-11-08 19:35:58 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2014-12-12 09:03:24 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2015-01-20 04:24:10 +00:00
|
|
|
import org.thoughtcrime.securesms.database.TextSecureDirectory;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2014-12-12 09:03:24 +00:00
|
|
|
import org.thoughtcrime.securesms.mms.PartAuthority;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2015-03-03 19:44:49 +00:00
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
|
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentStream;
|
2015-02-28 00:57:32 +00:00
|
|
|
import org.whispersystems.textsecure.api.push.TextSecureAddress;
|
2014-11-12 19:15:05 +00:00
|
|
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
2014-11-08 19:35:58 +00:00
|
|
|
|
2014-12-12 09:03:24 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2014-11-08 19:35:58 +00:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import ws.com.google.android.mms.ContentType;
|
2014-12-12 09:03:24 +00:00
|
|
|
import ws.com.google.android.mms.pdu.PduPart;
|
2014-11-08 19:35:58 +00:00
|
|
|
import ws.com.google.android.mms.pdu.SendReq;
|
|
|
|
|
2015-01-12 04:27:34 +00:00
|
|
|
public abstract class PushSendJob extends SendJob {
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
private static final String TAG = PushSendJob.class.getSimpleName();
|
|
|
|
|
|
|
|
protected PushSendJob(Context context, JobParameters parameters) {
|
|
|
|
super(context, parameters);
|
|
|
|
}
|
|
|
|
|
2015-03-11 21:23:45 +00:00
|
|
|
protected static JobParameters constructParameters(Context context, String destination) {
|
2014-11-08 19:35:58 +00:00
|
|
|
JobParameters.Builder builder = JobParameters.newBuilder();
|
|
|
|
builder.withPersistence();
|
|
|
|
builder.withGroupId(destination);
|
|
|
|
builder.withRequirement(new MasterSecretRequirement(context));
|
2015-03-11 21:23:45 +00:00
|
|
|
builder.withRequirement(new NetworkRequirement(context));
|
|
|
|
builder.withRetryCount(5);
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
return builder.create();
|
|
|
|
}
|
|
|
|
|
2015-03-03 19:44:49 +00:00
|
|
|
protected TextSecureAddress getPushAddress(String number) throws InvalidNumberException {
|
|
|
|
String e164number = Util.canonicalizeNumber(context, number);
|
2014-11-10 04:35:08 +00:00
|
|
|
String relay = TextSecureDirectory.getInstance(context).getRelay(e164number);
|
2015-03-03 19:44:49 +00:00
|
|
|
return new TextSecureAddress(e164number, Optional.fromNullable(relay));
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
|
2014-12-12 09:03:24 +00:00
|
|
|
protected List<TextSecureAttachment> getAttachments(final MasterSecret masterSecret, final SendReq message) {
|
2014-11-08 19:35:58 +00:00
|
|
|
List<TextSecureAttachment> attachments = new LinkedList<>();
|
|
|
|
|
|
|
|
for (int i=0;i<message.getBody().getPartsNum();i++) {
|
2014-12-12 09:03:24 +00:00
|
|
|
PduPart part = message.getBody().getPart(i);
|
|
|
|
String contentType = Util.toIsoString(part.getContentType());
|
2014-11-08 19:35:58 +00:00
|
|
|
if (ContentType.isImageType(contentType) ||
|
|
|
|
ContentType.isAudioType(contentType) ||
|
|
|
|
ContentType.isVideoType(contentType))
|
|
|
|
{
|
2014-12-12 09:03:24 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
InputStream is = PartAuthority.getPartStream(context, masterSecret, part.getDataUri());
|
|
|
|
attachments.add(new TextSecureAttachmentStream(is, contentType, part.getDataSize()));
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
Log.w(TAG, "Couldn't open attachment", ioe);
|
|
|
|
}
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return attachments;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void notifyMediaMessageDeliveryFailed(Context context, long messageId) {
|
|
|
|
long threadId = DatabaseFactory.getMmsDatabase(context).getThreadIdForMessage(messageId);
|
|
|
|
Recipients recipients = DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId);
|
|
|
|
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
|
|
|
|
}
|
|
|
|
}
|