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

109 lines
3.8 KiB
Java
Raw Normal View History

package org.thoughtcrime.securesms.jobs;
import androidx.annotation.NonNull;
2018-05-22 02:13:10 -07:00
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.RecipientDatabase;
import org.thoughtcrime.securesms.database.RecipientDatabase.RecipientReader;
import org.thoughtcrime.securesms.dependencies.InjectableType;
2019-03-28 08:56:35 -07:00
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.recipients.Recipient;
import org.thoughtcrime.securesms.util.GroupUtil;
2018-10-11 16:45:22 -07: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.multidevice.BlockedListMessage;
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
2019-03-28 08:56:35 -07:00
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
2019-03-28 08:56:35 -07:00
public class MultiDeviceBlockedUpdateJob extends BaseJob implements InjectableType {
2019-03-28 08:56:35 -07:00
public static final String KEY = "MultiDeviceBlockedUpdateJob";
2018-05-22 02:13:10 -07:00
@SuppressWarnings("unused")
private static final String TAG = MultiDeviceBlockedUpdateJob.class.getSimpleName();
2019-03-28 08:56:35 -07:00
@Inject SignalServiceMessageSender messageSender;
2019-03-28 08:56:35 -07:00
public MultiDeviceBlockedUpdateJob() {
this(new Job.Parameters.Builder()
.addConstraint(NetworkConstraint.KEY)
.setQueue("MultiDeviceBlockedUpdateJob")
.setLifespan(TimeUnit.DAYS.toMillis(1))
.setMaxAttempts(Parameters.UNLIMITED)
.build());
}
2019-03-28 08:56:35 -07:00
private MultiDeviceBlockedUpdateJob(@NonNull Job.Parameters parameters) {
super(parameters);
}
@Override
2019-03-28 08:56:35 -07:00
public @NonNull Data serialize() {
return Data.EMPTY;
}
@Override
2019-03-28 08:56:35 -07:00
public @NonNull String getFactoryKey() {
return KEY;
}
@Override
public void onRun()
throws IOException, UntrustedIdentityException
{
2018-10-11 16:45:22 -07:00
if (!TextSecurePreferences.isMultiDevice(context)) {
Log.i(TAG, "Not multi device, aborting...");
return;
}
2017-09-15 22:38:53 -07:00
RecipientDatabase database = DatabaseFactory.getRecipientDatabase(context);
try (RecipientReader reader = database.readerForBlocked(database.getBlocked())) {
List<String> blockedIndividuals = new LinkedList<>();
List<byte[]> blockedGroups = new LinkedList<>();
Recipient recipient;
while ((recipient = reader.getNext()) != null) {
if (recipient.isGroupRecipient()) {
blockedGroups.add(GroupUtil.getDecodedId(recipient.getAddress().toGroupString()));
} else {
blockedIndividuals.add(recipient.getAddress().serialize());
}
}
2020-05-11 16:54:31 +10:00
messageSender.sendMessage(SignalServiceSyncMessage.forBlocked(new BlockedListMessage(blockedIndividuals, blockedGroups)),
2018-05-22 02:13:10 -07:00
UnidentifiedAccessUtil.getAccessForSync(context));
}
}
@Override
public boolean onShouldRetry(@NonNull Exception exception) {
if (exception instanceof PushNetworkException) return true;
return false;
}
@Override
public void onCanceled() {
2019-03-28 08:56:35 -07:00
}
2019-03-28 08:56:35 -07:00
public static final class Factory implements Job.Factory<MultiDeviceBlockedUpdateJob> {
@Override
public @NonNull MultiDeviceBlockedUpdateJob create(@NonNull Parameters parameters, @NonNull Data data) {
return new MultiDeviceBlockedUpdateJob(parameters);
}
}
}