2014-07-25 15:14:29 -07:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2014-08-04 16:15:13 -07:00
|
|
|
import android.content.Context;
|
2014-07-25 15:14:29 -07:00
|
|
|
import android.util.Log;
|
|
|
|
|
2014-11-09 20:35:08 -08:00
|
|
|
import org.thoughtcrime.securesms.Release;
|
|
|
|
import org.thoughtcrime.securesms.push.TextSecurePushTrustStore;
|
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2014-08-04 16:15:13 -07:00
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2014-11-09 20:35:08 -08:00
|
|
|
import org.whispersystems.libaxolotl.util.guava.Optional;
|
|
|
|
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
|
|
|
import org.whispersystems.textsecure.push.PushAddress;
|
2014-07-25 15:14:29 -07:00
|
|
|
import org.whispersystems.textsecure.push.exceptions.NonSuccessfulResponseCodeException;
|
|
|
|
import org.whispersystems.textsecure.push.exceptions.PushNetworkException;
|
|
|
|
|
2014-11-09 20:35:08 -08:00
|
|
|
import java.io.IOException;
|
|
|
|
|
2014-07-25 15:14:29 -07:00
|
|
|
public class DeliveryReceiptJob extends ContextJob {
|
|
|
|
|
|
|
|
private static final String TAG = DeliveryReceiptJob.class.getSimpleName();
|
|
|
|
|
|
|
|
private final String destination;
|
|
|
|
private final long timestamp;
|
|
|
|
private final String relay;
|
|
|
|
|
2014-08-04 16:15:13 -07:00
|
|
|
public DeliveryReceiptJob(Context context, String destination, long timestamp, String relay) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withPersistence()
|
|
|
|
.withRetryCount(50)
|
|
|
|
.create());
|
2014-07-25 15:14:29 -07:00
|
|
|
|
|
|
|
this.destination = destination;
|
|
|
|
this.timestamp = timestamp;
|
|
|
|
this.relay = relay;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {}
|
|
|
|
|
|
|
|
@Override
|
2014-11-09 20:35:08 -08:00
|
|
|
public void onRun() throws IOException {
|
2014-07-25 15:14:29 -07:00
|
|
|
Log.w("DeliveryReceiptJob", "Sending delivery receipt...");
|
2014-11-09 20:35:08 -08:00
|
|
|
TextSecureMessageSender messageSender =
|
|
|
|
new TextSecureMessageSender(Release.PUSH_URL,
|
|
|
|
new TextSecurePushTrustStore(context),
|
|
|
|
TextSecurePreferences.getLocalNumber(context),
|
|
|
|
TextSecurePreferences.getPushServerPassword(context),
|
|
|
|
null, Optional.<TextSecureMessageSender.EventListener>absent());
|
|
|
|
|
|
|
|
PushAddress pushAddress = new PushAddress(-1, destination, 1, relay);
|
|
|
|
messageSender.sendDeliveryReceipt(pushAddress, timestamp);
|
2014-07-25 15:14:29 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-08-04 16:15:13 -07:00
|
|
|
public void onCanceled() {
|
2014-07-25 15:14:29 -07:00
|
|
|
Log.w(TAG, "Failed to send receipt after retry exhausted!");
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-08-04 16:15:13 -07:00
|
|
|
public boolean onShouldRetry(Throwable throwable) {
|
2014-07-25 15:14:29 -07:00
|
|
|
Log.w(TAG, throwable);
|
|
|
|
if (throwable instanceof NonSuccessfulResponseCodeException) return false;
|
2014-08-04 16:15:13 -07:00
|
|
|
if (throwable instanceof PushNetworkException) return true;
|
2014-07-25 15:14:29 -07:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|