session-android/src/org/thoughtcrime/securesms/jobs/SendDeliveryReceiptJob.java

112 lines
4.1 KiB
Java
Raw Normal View History

2018-05-22 02:13:10 -07:00
package org.thoughtcrime.securesms.jobs;
import android.support.annotation.NonNull;
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
import org.thoughtcrime.securesms.database.Address;
import org.thoughtcrime.securesms.dependencies.InjectableType;
2019-03-28 08:56:35 -07:00
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
import org.thoughtcrime.securesms.logging.Log;
2018-05-22 02:13:10 -07:00
import org.thoughtcrime.securesms.recipients.Recipient;
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
import java.io.IOException;
import java.util.Collections;
2019-03-28 08:56:35 -07:00
import java.util.concurrent.TimeUnit;
2018-05-22 02:13:10 -07:00
import javax.inject.Inject;
2019-03-28 08:56:35 -07:00
public class SendDeliveryReceiptJob extends BaseJob implements InjectableType {
2018-05-22 02:13:10 -07:00
2019-03-28 08:56:35 -07:00
public static final String KEY = "SendDeliveryReceiptJob";
2018-05-22 02:13:10 -07:00
private static final String KEY_ADDRESS = "address";
private static final String KEY_MESSAGE_ID = "message_id";
private static final String KEY_TIMESTAMP = "timestamp";
private static final String TAG = SendReadReceiptJob.class.getSimpleName();
@Inject
transient SignalServiceMessageSender messageSender;
private String address;
private long messageId;
private long timestamp;
2019-03-28 08:56:35 -07:00
public SendDeliveryReceiptJob(@NonNull Address address, long messageId) {
this(new Job.Parameters.Builder()
.addConstraint(NetworkConstraint.KEY)
.setLifespan(TimeUnit.DAYS.toMillis(1))
.setMaxAttempts(Parameters.UNLIMITED)
.build(),
address,
messageId,
System.currentTimeMillis());
2018-05-22 02:13:10 -07:00
}
2019-03-28 08:56:35 -07:00
private SendDeliveryReceiptJob(@NonNull Job.Parameters parameters,
@NonNull Address address,
long messageId,
long timestamp)
{
super(parameters);
2018-05-22 02:13:10 -07:00
this.address = address.serialize();
this.messageId = messageId;
2019-03-28 08:56:35 -07:00
this.timestamp = timestamp;
2018-05-22 02:13:10 -07:00
}
@Override
2019-03-28 08:56:35 -07:00
public @NonNull Data serialize() {
return new Data.Builder().putString(KEY_ADDRESS, address)
.putLong(KEY_MESSAGE_ID, messageId)
.putLong(KEY_TIMESTAMP, timestamp)
.build();
2018-05-22 02:13:10 -07:00
}
@Override
2019-03-28 08:56:35 -07:00
public @NonNull String getFactoryKey() {
return KEY;
2018-05-22 02:13:10 -07:00
}
@Override
public void onRun() throws IOException, UntrustedIdentityException {
SignalServiceAddress remoteAddress = new SignalServiceAddress(address);
SignalServiceReceiptMessage receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.DELIVERY,
Collections.singletonList(messageId),
timestamp);
messageSender.sendReceipt(remoteAddress,
UnidentifiedAccessUtil.getAccessFor(context, Recipient.from(context, Address.fromSerialized(address), false)),
receiptMessage);
}
@Override
public boolean onShouldRetry(Exception e) {
if (e instanceof PushNetworkException) return true;
return false;
}
@Override
public void onCanceled() {
Log.w(TAG, "Failed to send delivery receipt to: " + address);
}
2019-03-28 08:56:35 -07:00
public static final class Factory implements Job.Factory<SendDeliveryReceiptJob> {
@Override
public @NonNull SendDeliveryReceiptJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new SendDeliveryReceiptJob(parameters,
Address.fromSerialized(data.getString(KEY_ADDRESS)),
data.getLong(KEY_MESSAGE_ID),
data.getLong(KEY_TIMESTAMP));
}
}
2018-05-22 02:13:10 -07:00
}