2017-09-15 22:38:53 -07:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import android.support.annotation.NonNull;
|
2017-09-15 22:38:53 -07:00
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
|
2017-09-15 22:38:53 -07:00
|
|
|
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;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2018-05-22 02:13:10 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2017-09-15 22:38:53 -07:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
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;
|
2018-08-09 10:15:43 -04:00
|
|
|
import java.util.ArrayList;
|
2017-09-15 22:38:53 -07:00
|
|
|
import java.util.List;
|
2019-03-28 08:56:35 -07:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-09-15 22:38:53 -07:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public class SendReadReceiptJob extends BaseJob implements InjectableType {
|
2018-08-09 10:15:43 -04:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public static final String KEY = "SendReadReceiptJob";
|
2017-09-15 22:38:53 -07:00
|
|
|
|
|
|
|
private static final String TAG = SendReadReceiptJob.class.getSimpleName();
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_ADDRESS = "address";
|
|
|
|
private static final String KEY_MESSAGE_IDS = "message_ids";
|
|
|
|
private static final String KEY_TIMESTAMP = "timestamp";
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
@Inject SignalServiceMessageSender messageSender;
|
2017-09-15 22:38:53 -07:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private String address;
|
|
|
|
private List<Long> messageIds;
|
|
|
|
private long timestamp;
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
public SendReadReceiptJob(Address address, List<Long> messageIds) {
|
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setLifespan(TimeUnit.DAYS.toMillis(1))
|
|
|
|
.setMaxAttempts(Parameters.UNLIMITED)
|
|
|
|
.build(),
|
|
|
|
address,
|
|
|
|
messageIds,
|
|
|
|
System.currentTimeMillis());
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2017-09-15 22:38:53 -07:00
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
private SendReadReceiptJob(@NonNull Job.Parameters parameters,
|
|
|
|
@NonNull Address address,
|
|
|
|
@NonNull List<Long> messageIds,
|
|
|
|
long timestamp)
|
|
|
|
{
|
|
|
|
super(parameters);
|
2017-09-15 22:38:53 -07:00
|
|
|
|
|
|
|
this.address = address.serialize();
|
|
|
|
this.messageIds = messageIds;
|
2019-03-28 08:56:35 -07:00
|
|
|
this.timestamp = timestamp;
|
2017-09-15 22:38:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 08:56:35 -07:00
|
|
|
public @NonNull Data serialize() {
|
2018-08-09 10:15:43 -04:00
|
|
|
long[] ids = new long[messageIds.size()];
|
|
|
|
for (int i = 0; i < ids.length; i++) {
|
|
|
|
ids[i] = messageIds.get(i);
|
|
|
|
}
|
|
|
|
|
2019-03-28 08:56:35 -07:00
|
|
|
return new Data.Builder().putString(KEY_ADDRESS, address)
|
|
|
|
.putLongArray(KEY_MESSAGE_IDS, ids)
|
|
|
|
.putLong(KEY_TIMESTAMP, timestamp)
|
|
|
|
.build();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-08-09 10:15:43 -04:00
|
|
|
}
|
2017-09-15 22:38:53 -07:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException, UntrustedIdentityException {
|
2019-04-15 15:17:53 -04:00
|
|
|
if (!TextSecurePreferences.isReadReceiptsEnabled(context) || messageIds.isEmpty()) return;
|
2017-09-15 22:38:53 -07:00
|
|
|
|
|
|
|
SignalServiceAddress remoteAddress = new SignalServiceAddress(address);
|
|
|
|
SignalServiceReceiptMessage receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.READ, messageIds, timestamp);
|
|
|
|
|
2020-05-12 11:26:44 +10:00
|
|
|
messageSender.sendReceipt(remoteAddress,
|
2018-05-22 02:13:10 -07:00
|
|
|
UnidentifiedAccessUtil.getAccessFor(context, Recipient.from(context, Address.fromSerialized(address), false)),
|
|
|
|
receiptMessage);
|
2017-09-15 22:38:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 13:51:56 -03:00
|
|
|
public boolean onShouldRetry(@NonNull Exception e) {
|
2017-09-15 22:38:53 -07:00
|
|
|
if (e instanceof PushNetworkException) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
Log.w(TAG, "Failed to send read receipts to: " + address);
|
|
|
|
}
|
2019-03-28 08:56:35 -07:00
|
|
|
|
|
|
|
public static final class Factory implements Job.Factory<SendReadReceiptJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull SendReadReceiptJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
|
|
Address address = Address.fromSerialized(data.getString(KEY_ADDRESS));
|
|
|
|
long timestamp = data.getLong(KEY_TIMESTAMP);
|
2019-04-15 15:17:53 -04:00
|
|
|
long[] ids = data.hasLongArray(KEY_MESSAGE_IDS) ? data.getLongArray(KEY_MESSAGE_IDS) : new long[0];
|
2019-03-28 08:56:35 -07:00
|
|
|
List<Long> messageIds = new ArrayList<>(ids.length);
|
|
|
|
|
|
|
|
for (long id : ids) {
|
|
|
|
messageIds.add(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SendReadReceiptJob(parameters, address, messageIds, timestamp);
|
|
|
|
}
|
|
|
|
}
|
2017-09-15 22:38:53 -07:00
|
|
|
}
|