session-android/src/org/thoughtcrime/securesms/jobs/TypingSendJob.java

126 lines
4.6 KiB
Java
Raw Normal View History

2018-10-29 22:14:31 +00:00
package org.thoughtcrime.securesms.jobs;
import android.support.annotation.NonNull;
import com.annimon.stream.Stream;
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
import org.thoughtcrime.securesms.database.DatabaseFactory;
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;
2018-10-29 22:14:31 +00:00
import org.thoughtcrime.securesms.logging.Log;
2019-10-29 22:59:11 +00:00
import org.thoughtcrime.securesms.loki.MultiDeviceUtilities;
2018-10-29 22:14:31 +00:00
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair;
import org.whispersystems.signalservice.api.messages.SignalServiceTypingMessage;
import org.whispersystems.signalservice.api.messages.SignalServiceTypingMessage.Action;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.loki.utilities.PromiseUtil;
2018-10-29 22:14:31 +00:00
import java.util.Collections;
import java.util.List;
2019-03-28 15:56:35 +00:00
import java.util.concurrent.TimeUnit;
2018-10-29 22:14:31 +00:00
import javax.inject.Inject;
2019-03-28 15:56:35 +00:00
public class TypingSendJob extends BaseJob implements InjectableType {
2018-10-29 22:14:31 +00:00
2019-03-28 15:56:35 +00:00
public static final String KEY = "TypingSendJob";
2018-10-29 22:14:31 +00:00
private static final String TAG = TypingSendJob.class.getSimpleName();
private static final String KEY_THREAD_ID = "thread_id";
private static final String KEY_TYPING = "typing";
private long threadId;
private boolean typing;
@Inject SignalServiceMessageSender messageSender;
2019-03-28 15:56:35 +00:00
public TypingSendJob(long threadId, boolean typing) {
this(new Job.Parameters.Builder()
.setQueue("TYPING_" + threadId)
.setMaxAttempts(1)
.setLifespan(TimeUnit.SECONDS.toMillis(5))
.build(),
threadId,
typing);
2018-10-29 22:14:31 +00:00
}
2019-03-28 15:56:35 +00:00
private TypingSendJob(@NonNull Job.Parameters parameters, long threadId, boolean typing) {
super(parameters);
2018-10-29 22:14:31 +00:00
this.threadId = threadId;
this.typing = typing;
}
2019-03-28 15:56:35 +00:00
2018-10-29 22:14:31 +00:00
@Override
2019-03-28 15:56:35 +00:00
public @NonNull Data serialize() {
return new Data.Builder().putLong(KEY_THREAD_ID, threadId)
.putBoolean(KEY_TYPING, typing)
.build();
2018-10-29 22:14:31 +00:00
}
@Override
2019-03-28 15:56:35 +00:00
public @NonNull String getFactoryKey() {
return KEY;
2018-10-29 22:14:31 +00:00
}
@Override
public void onRun() throws Exception {
if (!TextSecurePreferences.isTypingIndicatorsEnabled(context)) {
return;
}
Log.d(TAG, "Sending typing " + (typing ? "started" : "stopped") + " for thread " + threadId);
Recipient recipient = DatabaseFactory.getThreadDatabase(context).getRecipientForThreadId(threadId);
if (recipient == null) {
throw new IllegalStateException("Tried to send a typing indicator to a non-existent thread.");
}
List<Recipient> recipients = Collections.singletonList(recipient);
Optional<byte[]> groupId = Optional.absent();
if (recipient.isGroupRecipient()) {
recipients = DatabaseFactory.getGroupDatabase(context).getGroupMembers(recipient.getAddress().toGroupString(), false);
groupId = Optional.of(GroupUtil.getDecodedId(recipient.getAddress().toGroupString()));
}
List<SignalServiceAddress> addresses = Stream.of(recipients).map(r -> new SignalServiceAddress(r.getAddress().serialize())).toList();
List<Optional<UnidentifiedAccessPair>> unidentifiedAccess = Stream.of(recipients).map(r -> UnidentifiedAccessUtil.getAccessFor(context, r)).toList();
SignalServiceTypingMessage typingMessage = new SignalServiceTypingMessage(typing ? Action.STARTED : Action.STOPPED, System.currentTimeMillis(), groupId);
// Loki - Don't send typing indicators in group chats or to ourselves
if (recipient.isGroupRecipient()) { return; }
boolean isOurDevice = PromiseUtil.get(MultiDeviceUtilities.isOneOfOurDevices(context, recipient.getAddress()), false);
if (!isOurDevice) {
messageSender.sendTyping(0, addresses, unidentifiedAccess, typingMessage);
}
2018-10-29 22:14:31 +00:00
}
@Override
2019-03-28 15:56:35 +00:00
public void onCanceled() {
2018-10-29 22:14:31 +00:00
}
@Override
protected boolean onShouldRetry(@NonNull Exception exception) {
2018-10-29 22:14:31 +00:00
return false;
}
2019-03-28 15:56:35 +00:00
public static final class Factory implements Job.Factory<TypingSendJob> {
@Override
public @NonNull TypingSendJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new TypingSendJob(parameters, data.getLong(KEY_THREAD_ID), data.getBoolean(KEY_TYPING));
}
}
2018-10-29 22:14:31 +00:00
}