2015-06-22 23:49:04 +02:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
2019-06-05 21:47:14 +02:00
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.annotation.Nullable;
|
2015-06-22 23:49:04 +02:00
|
|
|
|
2018-05-22 11:13:10 +02:00
|
|
|
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
|
2015-06-22 23:49:04 +02:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
2019-07-15 17:12:26 +02:00
|
|
|
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
2019-03-28 16:56:35 +01:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.Data;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
|
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
2018-08-01 17:09:24 +02:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2020-03-26 20:38:27 +01:00
|
|
|
import org.thoughtcrime.securesms.profiles.AvatarHelper;
|
2018-01-18 19:01:41 +01:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2019-08-07 20:22:51 +02:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientId;
|
2019-09-07 05:40:06 +02:00
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientUtil;
|
2018-10-12 01:45:22 +02:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2016-03-23 18:34:41 +01:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
|
|
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
|
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroup;
|
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.DeviceGroupsOutputStream;
|
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
|
2019-09-07 05:40:06 +02:00
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
2016-03-23 18:34:41 +01:00
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
2015-06-22 23:49:04 +02:00
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
2017-07-26 18:59:15 +02:00
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2019-12-18 06:44:21 +01:00
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2019-03-28 16:56:35 +01:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2015-06-22 23:49:04 +02:00
|
|
|
|
2019-07-15 17:12:26 +02:00
|
|
|
public class MultiDeviceGroupUpdateJob extends BaseJob {
|
2018-08-09 16:15:43 +02:00
|
|
|
|
2019-03-28 16:56:35 +01:00
|
|
|
public static final String KEY = "MultiDeviceGroupUpdateJob";
|
2015-06-22 23:49:04 +02:00
|
|
|
|
2015-08-06 19:20:13 +02:00
|
|
|
private static final String TAG = MultiDeviceGroupUpdateJob.class.getSimpleName();
|
2015-06-25 03:26:51 +02:00
|
|
|
|
2019-03-28 16:56:35 +01:00
|
|
|
public MultiDeviceGroupUpdateJob() {
|
|
|
|
this(new Job.Parameters.Builder()
|
|
|
|
.addConstraint(NetworkConstraint.KEY)
|
|
|
|
.setQueue("MultiDeviceGroupUpdateJob")
|
|
|
|
.setLifespan(TimeUnit.DAYS.toMillis(1))
|
|
|
|
.setMaxAttempts(Parameters.UNLIMITED)
|
|
|
|
.build());
|
2018-08-09 16:15:43 +02:00
|
|
|
}
|
|
|
|
|
2019-03-28 16:56:35 +01:00
|
|
|
private MultiDeviceGroupUpdateJob(@NonNull Job.Parameters parameters) {
|
|
|
|
super(parameters);
|
2015-06-22 23:49:04 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 16:15:43 +02:00
|
|
|
@Override
|
2019-03-28 16:56:35 +01:00
|
|
|
public @NonNull String getFactoryKey() {
|
|
|
|
return KEY;
|
2018-08-09 16:15:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-03-28 16:56:35 +01:00
|
|
|
public @NonNull Data serialize() {
|
|
|
|
return Data.EMPTY;
|
2018-08-09 16:15:43 +02:00
|
|
|
}
|
|
|
|
|
2015-06-22 23:49:04 +02:00
|
|
|
@Override
|
2018-11-15 21:05:08 +01:00
|
|
|
public void onRun() throws Exception {
|
2018-10-12 01:45:22 +02:00
|
|
|
if (!TextSecurePreferences.isMultiDevice(context)) {
|
|
|
|
Log.i(TAG, "Not multi device, aborting...");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-16 07:38:53 +02:00
|
|
|
File contactDataFile = createTempFile("multidevice-contact-update");
|
|
|
|
GroupDatabase.Reader reader = null;
|
2015-06-22 23:49:04 +02:00
|
|
|
|
|
|
|
GroupDatabase.GroupRecord record;
|
|
|
|
|
|
|
|
try {
|
|
|
|
DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new FileOutputStream(contactDataFile));
|
|
|
|
|
|
|
|
reader = DatabaseFactory.getGroupDatabase(context).getGroups();
|
|
|
|
|
|
|
|
while ((record = reader.getNext()) != null) {
|
2017-09-07 20:12:57 +02:00
|
|
|
if (!record.isMms()) {
|
2019-09-07 05:40:06 +02:00
|
|
|
List<SignalServiceAddress> members = new LinkedList<>();
|
2017-07-26 18:59:15 +02:00
|
|
|
|
2019-08-07 20:22:51 +02:00
|
|
|
for (RecipientId member : record.getMembers()) {
|
2019-09-07 05:40:06 +02:00
|
|
|
members.add(RecipientUtil.toSignalServiceAddress(context, Recipient.resolved(member)));
|
2017-09-07 20:12:57 +02:00
|
|
|
}
|
2017-07-26 18:59:15 +02:00
|
|
|
|
2020-03-26 15:00:17 +01:00
|
|
|
RecipientId recipientId = DatabaseFactory.getRecipientDatabase(context).getOrInsertFromGroupId(record.getId());
|
2019-12-18 06:44:21 +01:00
|
|
|
Recipient recipient = Recipient.resolved(recipientId);
|
|
|
|
Optional<Integer> expirationTimer = recipient.getExpireMessages() > 0 ? Optional.of(recipient.getExpireMessages()) : Optional.absent();
|
|
|
|
Map<RecipientId, Integer> inboxPositions = DatabaseFactory.getThreadDatabase(context).getInboxPositions();
|
|
|
|
Set<RecipientId> archived = DatabaseFactory.getThreadDatabase(context).getArchivedRecipients();
|
|
|
|
|
2020-03-26 15:00:17 +01:00
|
|
|
out.write(new DeviceGroup(record.getId().getDecodedId(),
|
2019-12-18 06:44:21 +01:00
|
|
|
Optional.fromNullable(record.getTitle()),
|
|
|
|
members,
|
2020-03-26 20:38:27 +01:00
|
|
|
getAvatar(record.getRecipientId()),
|
2019-12-18 06:44:21 +01:00
|
|
|
record.isActive(),
|
|
|
|
expirationTimer,
|
2018-09-10 17:40:00 +02:00
|
|
|
Optional.of(recipient.getColor().serialize()),
|
2019-12-18 06:44:21 +01:00
|
|
|
recipient.isBlocked(),
|
|
|
|
Optional.fromNullable(inboxPositions.get(recipientId)),
|
|
|
|
archived.contains(recipientId)));
|
2017-09-07 20:12:57 +02:00
|
|
|
}
|
2015-06-22 23:49:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
2015-08-06 19:20:13 +02:00
|
|
|
if (contactDataFile.exists() && contactDataFile.length() > 0) {
|
2019-07-15 17:12:26 +02:00
|
|
|
sendUpdate(ApplicationDependencies.getSignalServiceMessageSender(), contactDataFile);
|
2015-08-06 19:20:13 +02:00
|
|
|
} else {
|
|
|
|
Log.w(TAG, "No groups present for sync message...");
|
|
|
|
}
|
2015-06-22 23:49:04 +02:00
|
|
|
|
|
|
|
} finally {
|
|
|
|
if (contactDataFile != null) contactDataFile.delete();
|
|
|
|
if (reader != null) reader.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-22 18:51:56 +02:00
|
|
|
public boolean onShouldRetry(@NonNull Exception exception) {
|
2015-06-22 23:49:04 +02:00
|
|
|
if (exception instanceof PushNetworkException) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-01-03 20:10:16 +01:00
|
|
|
public void onFailure() {
|
2015-06-22 23:49:04 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-23 18:34:41 +01:00
|
|
|
private void sendUpdate(SignalServiceMessageSender messageSender, File contactsFile)
|
2015-06-22 23:49:04 +02:00
|
|
|
throws IOException, UntrustedIdentityException
|
|
|
|
{
|
2016-03-23 18:34:41 +01:00
|
|
|
FileInputStream contactsFileStream = new FileInputStream(contactsFile);
|
|
|
|
SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
|
|
|
|
.withStream(contactsFileStream)
|
|
|
|
.withContentType("application/octet-stream")
|
|
|
|
.withLength(contactsFile.length())
|
|
|
|
.build();
|
|
|
|
|
2018-05-22 11:13:10 +02:00
|
|
|
messageSender.sendMessage(SignalServiceSyncMessage.forGroups(attachmentStream),
|
|
|
|
UnidentifiedAccessUtil.getAccessForSync(context));
|
2015-06-22 23:49:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-26 20:38:27 +01:00
|
|
|
private Optional<SignalServiceAttachmentStream> getAvatar(@NonNull RecipientId recipientId) throws IOException {
|
|
|
|
if (!AvatarHelper.hasAvatar(context, recipientId)) return Optional.absent();
|
2015-06-22 23:49:04 +02:00
|
|
|
|
2016-03-23 18:34:41 +01:00
|
|
|
return Optional.of(SignalServiceAttachment.newStreamBuilder()
|
2020-03-26 20:38:27 +01:00
|
|
|
.withStream(AvatarHelper.getAvatar(context, recipientId))
|
2016-03-23 18:34:41 +01:00
|
|
|
.withContentType("image/*")
|
2020-03-26 20:38:27 +01:00
|
|
|
.withLength(AvatarHelper.getAvatarLength(context, recipientId))
|
2016-03-23 18:34:41 +01:00
|
|
|
.build());
|
2015-06-22 23:49:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private File createTempFile(String prefix) throws IOException {
|
|
|
|
File file = File.createTempFile(prefix, "tmp", context.getCacheDir());
|
|
|
|
file.deleteOnExit();
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2019-03-28 16:56:35 +01:00
|
|
|
public static final class Factory implements Job.Factory<MultiDeviceGroupUpdateJob> {
|
|
|
|
@Override
|
|
|
|
public @NonNull MultiDeviceGroupUpdateJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
|
|
return new MultiDeviceGroupUpdateJob(parameters);
|
|
|
|
}
|
|
|
|
}
|
2015-06-22 23:49:04 +02:00
|
|
|
}
|