2017-09-16 05:38:53 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-08-09 14:15:43 +00:00
|
|
|
import android.support.annotation.NonNull;
|
2017-09-16 05:38:53 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2018-06-18 19:27:04 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.JobParameters;
|
2018-08-09 14:15:43 +00:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 15:09:24 +00:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
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;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
import androidx.work.Data;
|
|
|
|
|
2017-09-16 05:38:53 +00:00
|
|
|
public class SendReadReceiptJob extends ContextJob implements InjectableType {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
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";
|
|
|
|
|
2017-09-16 05:38:53 +00:00
|
|
|
@Inject transient SignalServiceMessageSender messageSender;
|
|
|
|
|
2018-08-09 14:15:43 +00:00
|
|
|
private String address;
|
|
|
|
private List<Long> messageIds;
|
|
|
|
private long timestamp;
|
|
|
|
|
|
|
|
public SendReadReceiptJob() {
|
|
|
|
super(null, null);
|
|
|
|
}
|
2017-09-16 05:38:53 +00:00
|
|
|
|
|
|
|
public SendReadReceiptJob(Context context, Address address, List<Long> messageIds) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
2018-08-09 14:15:43 +00:00
|
|
|
.withNetworkRequirement()
|
2017-09-16 05:38:53 +00:00
|
|
|
.create());
|
|
|
|
|
|
|
|
this.address = address.serialize();
|
|
|
|
this.messageIds = messageIds;
|
|
|
|
this.timestamp = System.currentTimeMillis();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-08-09 14:15:43 +00:00
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
address = data.getString(KEY_ADDRESS);
|
|
|
|
timestamp = data.getLong(KEY_TIMESTAMP);
|
|
|
|
|
|
|
|
long[] ids = data.getLongArray(KEY_MESSAGE_IDS);
|
|
|
|
messageIds = new ArrayList<>(ids.length);
|
|
|
|
for (long id : ids) {
|
|
|
|
messageIds.add(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
long[] ids = new long[messageIds.size()];
|
|
|
|
for (int i = 0; i < ids.length; i++) {
|
|
|
|
ids[i] = messageIds.get(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataBuilder.putString(KEY_ADDRESS, address)
|
|
|
|
.putLongArray(KEY_MESSAGE_IDS, ids)
|
|
|
|
.putLong(KEY_TIMESTAMP, timestamp)
|
|
|
|
.build();
|
|
|
|
}
|
2017-09-16 05:38:53 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun() throws IOException, UntrustedIdentityException {
|
|
|
|
if (!TextSecurePreferences.isReadReceiptsEnabled(context)) return;
|
|
|
|
|
|
|
|
SignalServiceAddress remoteAddress = new SignalServiceAddress(address);
|
|
|
|
SignalServiceReceiptMessage receiptMessage = new SignalServiceReceiptMessage(SignalServiceReceiptMessage.Type.READ, messageIds, timestamp);
|
|
|
|
|
|
|
|
messageSender.sendReceipt(remoteAddress, 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 read receipts to: " + address);
|
|
|
|
}
|
|
|
|
}
|