2014-11-08 11:35:58 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
2018-08-09 10:15:43 -04:00
|
|
|
import android.support.annotation.NonNull;
|
2014-11-08 11:35:58 -08:00
|
|
|
import android.telephony.SmsManager;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2014-11-08 11:35:58 -08: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-24 19:17:44 -08:00
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
2018-06-18 12:27:04 -07:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.service.SmsDeliveryListener;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import androidx.work.Data;
|
2018-11-27 12:34:42 -08:00
|
|
|
import androidx.work.WorkerParameters;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2018-11-15 12:05:08 -08:00
|
|
|
public class SmsSentJob extends ContextJob {
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2018-06-19 19:22:39 -07:00
|
|
|
private static final long serialVersionUID = -2624694558755317560L;
|
|
|
|
private static final String TAG = SmsSentJob.class.getSimpleName();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2018-11-13 00:28:46 -08:00
|
|
|
private static final String KEY_MESSAGE_ID = "message_id";
|
|
|
|
private static final String KEY_ACTION = "action";
|
|
|
|
private static final String KEY_RESULT = "result";
|
|
|
|
private static final String KEY_RUN_ATTEMPT = "run_attempt";
|
2018-08-09 10:15:43 -04:00
|
|
|
|
|
|
|
private long messageId;
|
|
|
|
private String action;
|
|
|
|
private int result;
|
2018-11-13 00:28:46 -08:00
|
|
|
private int runAttempt;
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2018-11-27 12:34:42 -08:00
|
|
|
public SmsSentJob(@NonNull Context context, @NonNull WorkerParameters workerParameters) {
|
|
|
|
super(context, workerParameters);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2018-11-13 00:28:46 -08:00
|
|
|
public SmsSentJob(Context context, long messageId, String action, int result, int runAttempt) {
|
2014-11-08 11:35:58 -08:00
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.create());
|
|
|
|
|
2018-11-13 00:28:46 -08:00
|
|
|
this.messageId = messageId;
|
|
|
|
this.action = action;
|
|
|
|
this.result = result;
|
|
|
|
this.runAttempt = runAttempt;
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 10:15:43 -04:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
2018-11-13 00:28:46 -08:00
|
|
|
messageId = data.getLong(KEY_MESSAGE_ID);
|
|
|
|
action = data.getString(KEY_ACTION);
|
|
|
|
result = data.getInt(KEY_RESULT);
|
|
|
|
runAttempt = data.getInt(KEY_RUN_ATTEMPT);
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.putLong(KEY_MESSAGE_ID, messageId)
|
|
|
|
.putString(KEY_ACTION, action)
|
|
|
|
.putInt(KEY_RESULT, result)
|
2018-11-13 00:28:46 -08:00
|
|
|
.putInt(KEY_RUN_ATTEMPT, runAttempt)
|
2018-08-09 10:15:43 -04:00
|
|
|
.build();
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public void onRun() {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Got SMS callback: " + action + " , " + result);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case SmsDeliveryListener.SENT_SMS_ACTION:
|
2018-01-24 19:17:44 -08:00
|
|
|
handleSentResult(messageId, result);
|
2014-11-08 11:35:58 -08:00
|
|
|
break;
|
|
|
|
case SmsDeliveryListener.DELIVERED_SMS_ACTION:
|
|
|
|
handleDeliveredResult(messageId, result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public boolean onShouldRetry(Exception throwable) {
|
2014-11-08 11:35:58 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void handleDeliveredResult(long messageId, int result) {
|
2018-01-24 19:17:44 -08:00
|
|
|
DatabaseFactory.getSmsDatabase(context).markStatus(messageId, result);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
2018-01-24 19:17:44 -08:00
|
|
|
private void handleSentResult(long messageId, int result) {
|
2014-11-08 11:35:58 -08:00
|
|
|
try {
|
2018-01-24 19:17:44 -08:00
|
|
|
SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
|
|
|
|
SmsMessageRecord record = database.getMessage(messageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case Activity.RESULT_OK:
|
2016-12-21 09:49:01 -08:00
|
|
|
database.markAsSent(messageId, false);
|
2014-11-08 11:35:58 -08: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()
|
2018-11-13 00:28:46 -08:00
|
|
|
.add(new SmsSendJob(context, messageId, record.getIndividualRecipient().getAddress().serialize(), runAttempt + 1));
|
2014-11-08 11:35:58 -08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
database.markAsSentFailed(messageId);
|
2017-08-01 08:56:00 -07:00
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, record.getRecipient(), record.getThreadId());
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
} catch (NoSuchMessageException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|