2014-11-08 20:35:58 +01:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
|
|
|
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;
|
2014-11-10 05:35:08 +01:00
|
|
|
import org.thoughtcrime.securesms.database.TextSecureDirectory;
|
2014-11-12 20:35:54 +01:00
|
|
|
import org.whispersystems.textsecure.api.push.PushAddress;
|
2014-11-12 20:15:05 +01:00
|
|
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
2014-11-08 20:35:58 +01:00
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import ws.com.google.android.mms.ContentType;
|
|
|
|
import ws.com.google.android.mms.pdu.SendReq;
|
|
|
|
|
|
|
|
public abstract class PushSendJob extends MasterSecretJob {
|
|
|
|
|
|
|
|
private static final String TAG = PushSendJob.class.getSimpleName();
|
|
|
|
|
|
|
|
protected PushSendJob(Context context, JobParameters parameters) {
|
|
|
|
super(context, parameters);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static JobParameters constructParameters(Context context, String destination) {
|
|
|
|
JobParameters.Builder builder = JobParameters.newBuilder();
|
|
|
|
builder.withPersistence();
|
|
|
|
builder.withGroupId(destination);
|
|
|
|
builder.withRequirement(new MasterSecretRequirement(context));
|
|
|
|
|
|
|
|
if (!isSmsFallbackSupported(context, destination)) {
|
|
|
|
builder.withRequirement(new NetworkRequirement(context));
|
|
|
|
builder.withRetryCount(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.create();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected static boolean isSmsFallbackSupported(Context context, String destination) {
|
|
|
|
if (GroupUtil.isEncodedGroup(destination)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!TextSecurePreferences.isFallbackSmsAllowed(context)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-10 05:35:08 +01:00
|
|
|
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
|
2014-11-08 20:35:58 +01:00
|
|
|
return directory.isSmsFallbackSupported(destination);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected PushAddress getPushAddress(Recipient recipient) throws InvalidNumberException {
|
|
|
|
String e164number = Util.canonicalizeNumber(context, recipient.getNumber());
|
2014-11-10 05:35:08 +01:00
|
|
|
String relay = TextSecureDirectory.getInstance(context).getRelay(e164number);
|
2014-11-08 20:35:58 +01:00
|
|
|
return new PushAddress(recipient.getRecipientId(), e164number, 1, relay);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected boolean isSmsFallbackApprovalRequired(String destination) {
|
|
|
|
return (isSmsFallbackSupported(context, destination) && TextSecurePreferences.isFallbackSmsAskRequired(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected List<TextSecureAttachment> getAttachments(SendReq message) {
|
|
|
|
List<TextSecureAttachment> attachments = new LinkedList<>();
|
|
|
|
|
|
|
|
for (int i=0;i<message.getBody().getPartsNum();i++) {
|
|
|
|
String contentType = Util.toIsoString(message.getBody().getPart(i).getContentType());
|
|
|
|
if (ContentType.isImageType(contentType) ||
|
|
|
|
ContentType.isAudioType(contentType) ||
|
|
|
|
ContentType.isVideoType(contentType))
|
|
|
|
{
|
|
|
|
byte[] data = message.getBody().getPart(i).getData();
|
|
|
|
Log.w(TAG, "Adding attachment...");
|
|
|
|
attachments.add(new TextSecureAttachmentStream(new ByteArrayInputStream(data), contentType, data.length));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|