2017-09-16 05:38:53 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2017-09-16 05:38:53 +00:00
|
|
|
|
2018-05-22 09:13:10 +00:00
|
|
|
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
|
2017-09-16 05:38:53 +00:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2019-03-28 15:56:35 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2018-05-22 09:13:10 +00:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2017-09-16 05:38:53 +00: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 14:15:43 +00:00
|
|
|
import java.util.ArrayList;
|
2017-09-16 05:38:53 +00:00
|
|
|
import java.util.List;
|
2019-03-28 15:56:35 +00:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-09-16 05:38:53 +00:00
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public class SendReadReceiptJob extends BaseJob implements InjectableType {
|
2018-08-09 14:15:43 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
public static final String KEY = "SendReadReceiptJob";
|
2017-09-16 05:38:53 +00:00
|
|
|
|
|
|
|
private static final String TAG = SendReadReceiptJob.class.getSimpleName();
|
|
|
|
|
2018-08-09 14:15:43 +00: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 15:56:35 +00:00
|
|
|
@Inject SignalServiceMessageSender messageSender;
|
2017-09-16 05:38:53 +00:00
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
private String address;
|
|
|
|
private List<Long> messageIds;
|
|
|
|
private long timestamp;
|
|
|
|
|
2019-03-28 15:56:35 +00: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 14:15:43 +00:00
|
|
|
}
|
2017-09-16 05:38:53 +00:00
|
|
|
|
2019-03-28 15:56:35 +00:00
|
|
|
private SendReadReceiptJob(@NonNull Job.Parameters parameters,
|
|
|
|
@NonNull Address address,
|
|
|
|
@NonNull List<Long> messageIds,
|
|
|
|
long timestamp)
|
|
|
|
{
|
|
|
|
super(parameters);
|
2017-09-16 05:38:53 +00:00
|
|
|
|
|
|
|
this.address = address.serialize();
|
|
|
|
this.messageIds = messageIds;
|
2019-03-28 15:56:35 +00:00
|
|
|
this.timestamp = timestamp;
|
2017-09-16 05:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 15:56:35 +00:00
|
|
|
public @NonNull Data serialize() {
|
2018-08-09 14:15:43 +00:00
|
|
|
long[] ids = new long[messageIds.size()];
|
|
|
|
for (int i = 0; i < ids.length; i++) {
|
|
|
|
ids[i] = messageIds.get(i);
|
|
|
|
}
|
|
|
|
|
2019-03-28 15:56:35 +00: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 14:15:43 +00:00
|
|
|
}
|
2017-09-16 05:38:53 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException, UntrustedIdentityException {
|
2019-04-15 19:17:53 +00:00
|
|
|
if (!TextSecurePreferences.isReadReceiptsEnabled(context) || messageIds.isEmpty()) return;
|
2017-09-16 05:38:53 +00:00
|
|
|
|
|
|
|
SignalServiceAddress remoteAddress = new SignalServiceAddress(address);
|
|
|
|
SignalServiceReceiptMessage receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.READ, messageIds, timestamp);
|
|
|
|
|
2018-05-22 09:13:10 +00:00
|
|
|
messageSender.sendReceipt(remoteAddress,
|
|
|
|
UnidentifiedAccessUtil.getAccessFor(context, Recipient.from(context, Address.fromSerialized(address), false)),
|
|
|
|
receiptMessage);
|
2017-09-16 05:38:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onShouldRetry(Exception e) {
|
|
|
|
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 15:56:35 +00: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 19:17:53 +00:00
|
|
|
long[] ids = data.hasLongArray(KEY_MESSAGE_IDS) ? data.getLongArray(KEY_MESSAGE_IDS) : new long[0];
|
2019-03-28 15:56:35 +00:00
|
|
|
List<Long> messageIds = new ArrayList<>(ids.length);
|
|
|
|
|
|
|
|
for (long id : ids) {
|
|
|
|
messageIds.add(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new SendReadReceiptJob(parameters, address, messageIds, timestamp);
|
|
|
|
}
|
|
|
|
}
|
2017-09-16 05:38:53 +00:00
|
|
|
}
|