2014-11-08 19:35:58 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.telephony.SmsManager;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
2018-01-25 03:17:44 +00:00
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
2018-06-18 19:27:04 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2014-11-08 19:35:58 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.service.SmsDeliveryListener;
|
|
|
|
|
|
|
|
public class SmsSentJob extends MasterSecretJob {
|
|
|
|
|
2018-06-20 02:22:39 +00:00
|
|
|
private static final long serialVersionUID = -2624694558755317560L;
|
|
|
|
private static final String TAG = SmsSentJob.class.getSimpleName();
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
private final long messageId;
|
|
|
|
private final String action;
|
|
|
|
private final int result;
|
|
|
|
|
|
|
|
public SmsSentJob(Context context, long messageId, String action, int result) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.create());
|
|
|
|
|
|
|
|
this.messageId = messageId;
|
|
|
|
this.action = action;
|
|
|
|
this.result = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 03:57:53 +00:00
|
|
|
public void onRun(MasterSecret masterSecret) {
|
2014-11-08 19:35:58 +00:00
|
|
|
Log.w(TAG, "Got SMS callback: " + action + " , " + result);
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case SmsDeliveryListener.SENT_SMS_ACTION:
|
2018-01-25 03:17:44 +00:00
|
|
|
handleSentResult(messageId, result);
|
2014-11-08 19:35:58 +00:00
|
|
|
break;
|
|
|
|
case SmsDeliveryListener.DELIVERED_SMS_ACTION:
|
|
|
|
handleDeliveredResult(messageId, result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 05:11:57 +00:00
|
|
|
public boolean onShouldRetryThrowable(Exception throwable) {
|
2014-11-08 19:35:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleDeliveredResult(long messageId, int result) {
|
2018-01-25 03:17:44 +00:00
|
|
|
DatabaseFactory.getSmsDatabase(context).markStatus(messageId, result);
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
|
2018-01-25 03:17:44 +00:00
|
|
|
private void handleSentResult(long messageId, int result) {
|
2014-11-08 19:35:58 +00:00
|
|
|
try {
|
2018-01-25 03:17:44 +00:00
|
|
|
SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
|
|
|
|
SmsMessageRecord record = database.getMessage(messageId);
|
2014-11-08 19:35:58 +00:00
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case Activity.RESULT_OK:
|
2016-12-21 17:49:01 +00:00
|
|
|
database.markAsSent(messageId, false);
|
2014-11-08 19:35:58 +00:00
|
|
|
break;
|
|
|
|
case SmsManager.RESULT_ERROR_NO_SERVICE:
|
|
|
|
case SmsManager.RESULT_ERROR_RADIO_OFF:
|
|
|
|
Log.w(TAG, "Service connectivity problem, requeuing...");
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
2017-07-26 16:59:15 +00:00
|
|
|
.add(new SmsSendJob(context, messageId, record.getIndividualRecipient().getAddress().serialize()));
|
2014-11-08 19:35:58 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
database.markAsSentFailed(messageId);
|
2017-08-01 15:56:00 +00:00
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, record.getRecipient(), record.getThreadId());
|
2014-11-08 19:35:58 +00:00
|
|
|
}
|
|
|
|
} catch (NoSuchMessageException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|