2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
import android.app.PendingIntent;
|
2014-11-03 15:16:04 -08:00
|
|
|
import android.content.Context;
|
2014-11-08 11:35:58 -08:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.net.Uri;
|
2016-02-05 16:10:33 -08:00
|
|
|
import android.os.Build;
|
2015-12-10 23:45:07 -06:00
|
|
|
import android.telephony.PhoneNumberUtils;
|
2014-11-08 11:35:58 -08:00
|
|
|
import android.telephony.SmsManager;
|
|
|
|
import android.util.Log;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
2015-03-13 11:29:44 -07:00
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2014-12-24 12:40:28 -08:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.NetworkOrServiceRequirement;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.ServiceRequirement;
|
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.service.SmsDeliveryListener;
|
|
|
|
import org.thoughtcrime.securesms.transport.UndeliverableMessageException;
|
|
|
|
import org.thoughtcrime.securesms.util.NumberUtil;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
import java.util.ArrayList;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-01-11 20:27:34 -08:00
|
|
|
public class SmsSendJob extends SendJob {
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
private static final String TAG = SmsSendJob.class.getSimpleName();
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
private final long messageId;
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
public SmsSendJob(Context context, long messageId, String name) {
|
2014-12-24 12:40:28 -08:00
|
|
|
super(context, constructParameters(context, name));
|
2014-11-03 15:16:04 -08:00
|
|
|
this.messageId = messageId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-12-21 09:49:01 -08:00
|
|
|
public void onAdded() {}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
@Override
|
2015-01-11 20:27:34 -08:00
|
|
|
public void onSend(MasterSecret masterSecret) throws NoSuchMessageException {
|
2014-11-11 19:57:53 -08:00
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
SmsMessageRecord record = database.getMessage(masterSecret, messageId);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
try {
|
|
|
|
Log.w(TAG, "Sending message: " + messageId);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
deliver(record);
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (UndeliverableMessageException ude) {
|
|
|
|
Log.w(TAG, ude);
|
|
|
|
DatabaseFactory.getSmsDatabase(context).markAsSentFailed(record.getId());
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, record.getRecipients(), record.getThreadId());
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
@Override
|
2014-11-11 21:11:57 -08:00
|
|
|
public boolean onShouldRetryThrowable(Exception throwable) {
|
2014-11-11 19:57:53 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
2014-11-08 11:35:58 -08:00
|
|
|
Log.w(TAG, "onCanceled()");
|
|
|
|
long threadId = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageId);
|
|
|
|
Recipients recipients = DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
DatabaseFactory.getSmsDatabase(context).markAsSentFailed(messageId);
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2015-03-11 14:23:45 -07:00
|
|
|
private void deliver(SmsMessageRecord message)
|
|
|
|
throws UndeliverableMessageException
|
2014-11-08 11:35:58 -08:00
|
|
|
{
|
2015-03-11 14:23:45 -07:00
|
|
|
if (message.isSecure() || message.isKeyExchange() || message.isEndSession()) {
|
|
|
|
throw new UndeliverableMessageException("Trying to send a secure SMS?");
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2015-12-10 23:45:07 -06:00
|
|
|
String recipient = message.getIndividualRecipient().getNumber();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2015-12-10 23:45:07 -06:00
|
|
|
// See issue #1516 for bug report, and discussion on commits related to #4833 for problems
|
|
|
|
// related to the original fix to #1516. This still may not be a correct fix if networks allow
|
|
|
|
// SMS/MMS sending to alphanumeric recipients other than email addresses, but should also
|
|
|
|
// help to fix issue #3099.
|
2015-12-10 18:49:58 -08:00
|
|
|
if (!NumberUtil.isValidEmail(recipient)) {
|
2015-12-10 23:45:07 -06:00
|
|
|
recipient = PhoneNumberUtils.stripSeparators(PhoneNumberUtils.convertKeypadLettersToDigits(recipient));
|
2015-12-10 18:49:58 -08:00
|
|
|
}
|
2015-06-21 19:54:19 +02:00
|
|
|
|
2015-12-10 23:45:07 -06:00
|
|
|
if (!NumberUtil.isValidSmsOrEmail(recipient)) {
|
|
|
|
throw new UndeliverableMessageException("Not a valid SMS destination! " + recipient);
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<String> messages = SmsManager.getDefault().divideMessage(message.getBody().getBody());
|
|
|
|
ArrayList<PendingIntent> sentIntents = constructSentIntents(message.getId(), message.getType(), messages, false);
|
|
|
|
ArrayList<PendingIntent> deliveredIntents = constructDeliveredIntents(message.getId(), message.getType(), messages);
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
// NOTE 11/04/14 -- There's apparently a bug where for some unknown recipients
|
|
|
|
// and messages, this will throw an NPE. We have no idea why, so we're just
|
|
|
|
// catching it and marking the message as a failure. That way at least it doesn't
|
|
|
|
// repeatedly crash every time you start the app.
|
|
|
|
try {
|
2016-02-05 16:10:33 -08:00
|
|
|
getSmsManagerFor(message.getSubscriptionId()).sendMultipartTextMessage(recipient, null, messages, sentIntents, deliveredIntents);
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (NullPointerException npe) {
|
|
|
|
Log.w(TAG, npe);
|
|
|
|
Log.w(TAG, "Recipient: " + recipient);
|
|
|
|
Log.w(TAG, "Message Parts: " + messages.size());
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (int i=0;i<messages.size();i++) {
|
2016-02-05 16:10:33 -08:00
|
|
|
getSmsManagerFor(message.getSubscriptionId()).sendTextMessage(recipient, null, messages.get(i),
|
|
|
|
sentIntents.get(i),
|
|
|
|
deliveredIntents == null ? null : deliveredIntents.get(i));
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
} catch (NullPointerException npe2) {
|
|
|
|
Log.w(TAG, npe);
|
|
|
|
throw new UndeliverableMessageException(npe2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private ArrayList<PendingIntent> constructSentIntents(long messageId, long type,
|
|
|
|
ArrayList<String> messages, boolean secure)
|
|
|
|
{
|
|
|
|
ArrayList<PendingIntent> sentIntents = new ArrayList<>(messages.size());
|
|
|
|
|
|
|
|
for (String ignored : messages) {
|
|
|
|
sentIntents.add(PendingIntent.getBroadcast(context, 0,
|
|
|
|
constructSentIntent(context, messageId, type, secure, false),
|
|
|
|
0));
|
|
|
|
}
|
|
|
|
|
|
|
|
return sentIntents;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ArrayList<PendingIntent> constructDeliveredIntents(long messageId, long type, ArrayList<String> messages) {
|
|
|
|
if (!TextSecurePreferences.isSmsDeliveryReportsEnabled(context)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArrayList<PendingIntent> deliveredIntents = new ArrayList<>(messages.size());
|
|
|
|
|
|
|
|
for (String ignored : messages) {
|
|
|
|
deliveredIntents.add(PendingIntent.getBroadcast(context, 0,
|
|
|
|
constructDeliveredIntent(context, messageId, type),
|
|
|
|
0));
|
|
|
|
}
|
|
|
|
|
|
|
|
return deliveredIntents;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Intent constructSentIntent(Context context, long messageId, long type,
|
|
|
|
boolean upgraded, boolean push)
|
|
|
|
{
|
|
|
|
Intent pending = new Intent(SmsDeliveryListener.SENT_SMS_ACTION,
|
|
|
|
Uri.parse("custom://" + messageId + System.currentTimeMillis()),
|
|
|
|
context, SmsDeliveryListener.class);
|
|
|
|
|
|
|
|
pending.putExtra("type", type);
|
|
|
|
pending.putExtra("message_id", messageId);
|
|
|
|
pending.putExtra("upgraded", upgraded);
|
|
|
|
pending.putExtra("push", push);
|
|
|
|
|
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
|
2014-12-24 12:40:28 -08:00
|
|
|
private Intent constructDeliveredIntent(Context context, long messageId, long type) {
|
2014-11-08 11:35:58 -08:00
|
|
|
Intent pending = new Intent(SmsDeliveryListener.DELIVERED_SMS_ACTION,
|
|
|
|
Uri.parse("custom://" + messageId + System.currentTimeMillis()),
|
|
|
|
context, SmsDeliveryListener.class);
|
|
|
|
pending.putExtra("type", type);
|
|
|
|
pending.putExtra("message_id", messageId);
|
|
|
|
|
|
|
|
return pending;
|
|
|
|
}
|
|
|
|
|
2016-02-05 16:10:33 -08:00
|
|
|
private SmsManager getSmsManagerFor(int subscriptionId) {
|
|
|
|
if (Build.VERSION.SDK_INT >= 22 && subscriptionId != -1) {
|
|
|
|
return SmsManager.getSmsManagerForSubscriptionId(subscriptionId);
|
|
|
|
} else {
|
|
|
|
return SmsManager.getDefault();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-24 12:40:28 -08:00
|
|
|
private static JobParameters constructParameters(Context context, String name) {
|
|
|
|
JobParameters.Builder builder = JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
2015-11-03 10:37:06 -08:00
|
|
|
.withRetryCount(15)
|
2014-12-24 12:40:28 -08:00
|
|
|
.withGroupId(name);
|
|
|
|
|
|
|
|
if (TextSecurePreferences.isWifiSmsEnabled(context)) {
|
|
|
|
builder.withRequirement(new NetworkOrServiceRequirement(context));
|
|
|
|
} else {
|
|
|
|
builder.withRequirement(new ServiceRequirement(context));
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.create();
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|