2014-11-08 11:35:58 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
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;
|
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.Recipient;
|
|
|
|
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;
|
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
|
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentStream;
|
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;
|
|
|
|
|
|
|
|
import ws.com.google.android.mms.ContentType;
|
2014-12-12 01:03:24 -08:00
|
|
|
import ws.com.google.android.mms.pdu.PduPart;
|
2014-11-08 11:35:58 -08:00
|
|
|
import ws.com.google.android.mms.pdu.SendReq;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:10:11 -08:00
|
|
|
protected static JobParameters constructParameters(Context context, String destination, boolean media) {
|
2014-11-08 11:35:58 -08:00
|
|
|
JobParameters.Builder builder = JobParameters.newBuilder();
|
|
|
|
builder.withPersistence();
|
|
|
|
builder.withGroupId(destination);
|
|
|
|
builder.withRequirement(new MasterSecretRequirement(context));
|
|
|
|
|
2014-12-11 16:10:11 -08:00
|
|
|
if (!isSmsFallbackSupported(context, destination, media)) {
|
2014-11-08 11:35:58 -08:00
|
|
|
builder.withRequirement(new NetworkRequirement(context));
|
|
|
|
builder.withRetryCount(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.create();
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:10:11 -08:00
|
|
|
protected static boolean isSmsFallbackSupported(Context context, String destination, boolean media) {
|
2014-12-03 14:15:54 -08:00
|
|
|
try {
|
|
|
|
String e164number = Util.canonicalizeNumber(context, destination);
|
|
|
|
|
|
|
|
if (GroupUtil.isEncodedGroup(e164number)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2014-12-03 14:15:54 -08:00
|
|
|
if (!TextSecurePreferences.isFallbackSmsAllowed(context)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-11 16:10:11 -08:00
|
|
|
if (media && !TextSecurePreferences.isFallbackMmsEnabled(context)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-03 14:15:54 -08:00
|
|
|
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
|
|
|
|
return directory.isSmsFallbackSupported(e164number);
|
|
|
|
} catch (InvalidNumberException e) {
|
|
|
|
Log.w(TAG, e);
|
2014-11-08 11:35:58 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 16:57:32 -08:00
|
|
|
protected TextSecureAddress getPushAddress(Recipient recipient) throws InvalidNumberException {
|
2014-11-08 11:35:58 -08:00
|
|
|
String e164number = Util.canonicalizeNumber(context, recipient.getNumber());
|
2014-11-09 20:35:08 -08:00
|
|
|
String relay = TextSecureDirectory.getInstance(context).getRelay(e164number);
|
2015-02-27 16:57:32 -08:00
|
|
|
return new TextSecureAddress(recipient.getRecipientId(), e164number, relay);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2014-12-11 16:10:11 -08:00
|
|
|
protected boolean isSmsFallbackApprovalRequired(String destination, boolean media) {
|
|
|
|
return (isSmsFallbackSupported(context, destination, media) && TextSecurePreferences.isFallbackSmsAskRequired(context));
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2014-12-12 01:03:24 -08:00
|
|
|
protected List<TextSecureAttachment> getAttachments(final MasterSecret masterSecret, final SendReq message) {
|
2014-11-08 11:35:58 -08:00
|
|
|
List<TextSecureAttachment> attachments = new LinkedList<>();
|
|
|
|
|
|
|
|
for (int i=0;i<message.getBody().getPartsNum();i++) {
|
2014-12-12 01:03:24 -08:00
|
|
|
PduPart part = message.getBody().getPart(i);
|
|
|
|
String contentType = Util.toIsoString(part.getContentType());
|
2014-11-08 11:35:58 -08:00
|
|
|
if (ContentType.isImageType(contentType) ||
|
|
|
|
ContentType.isAudioType(contentType) ||
|
|
|
|
ContentType.isVideoType(contentType))
|
|
|
|
{
|
2014-12-12 01:03:24 -08: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 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);
|
|
|
|
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
|
|
|
|
}
|
|
|
|
}
|