2016-02-20 01:07:41 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.SyncMessageId;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.TextSecureCommunicationModule;
|
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2016-03-23 17:34:41 +00:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
|
|
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.ReadMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
2016-02-20 01:07:41 +00:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.Serializable;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
public class MultiDeviceReadUpdateJob extends MasterSecretJob implements InjectableType {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private static final String TAG = MultiDeviceReadUpdateJob.class.getSimpleName();
|
|
|
|
|
|
|
|
private final List<SerializableSyncMessageId> messageIds;
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
transient TextSecureCommunicationModule.TextSecureMessageSenderFactory messageSenderFactory;
|
|
|
|
|
|
|
|
public MultiDeviceReadUpdateJob(Context context, List<SyncMessageId> messageIds) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.withPersistence()
|
|
|
|
.create());
|
|
|
|
|
|
|
|
this.messageIds = new LinkedList<>();
|
|
|
|
|
|
|
|
for (SyncMessageId messageId : messageIds) {
|
|
|
|
this.messageIds.add(new SerializableSyncMessageId(messageId.getAddress(), messageId.getTimetamp()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun(MasterSecret masterSecret) throws IOException, UntrustedIdentityException {
|
|
|
|
List<ReadMessage> readMessages = new LinkedList<>();
|
|
|
|
|
|
|
|
for (SerializableSyncMessageId messageId : messageIds) {
|
|
|
|
readMessages.add(new ReadMessage(messageId.sender, messageId.timestamp));
|
|
|
|
}
|
|
|
|
|
2016-03-23 17:34:41 +00:00
|
|
|
SignalServiceMessageSender messageSender = messageSenderFactory.create();
|
|
|
|
messageSender.sendMessage(SignalServiceSyncMessage.forRead(readMessages));
|
2016-02-20 01:07:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
|
|
|
return exception instanceof PushNetworkException;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class SerializableSyncMessageId implements Serializable {
|
|
|
|
|
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
|
|
|
private final String sender;
|
|
|
|
private final long timestamp;
|
|
|
|
|
|
|
|
private SerializableSyncMessageId(String sender, long timestamp) {
|
|
|
|
this.sender = sender;
|
|
|
|
this.timestamp = timestamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|