2016-02-20 01:07:41 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2016-10-10 18:13:37 +00:00
|
|
|
import android.util.Log;
|
2016-02-20 01:07:41 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.SyncMessageId;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.SignalCommunicationModule;
|
2016-02-20 01:07:41 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
2016-10-10 18:13:37 +00:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2016-02-20 01:07:41 +00:00
|
|
|
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
|
2016-12-20 17:55:52 +00:00
|
|
|
transient SignalCommunicationModule.SignalMessageSenderFactory messageSenderFactory;
|
2016-02-20 01:07:41 +00:00
|
|
|
|
|
|
|
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 {
|
2016-10-10 18:13:37 +00:00
|
|
|
if (!TextSecurePreferences.isMultiDevice(context)) {
|
|
|
|
Log.w(TAG, "Not multi device...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-20 01:07:41 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|