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