2015-06-22 21:49:04 +00:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.Nullable;
|
2015-08-06 17:20:13 +00:00
|
|
|
import android.util.Log;
|
2015-06-22 21:49:04 +00:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2016-12-20 17:55:52 +00:00
|
|
|
import org.thoughtcrime.securesms.dependencies.SignalCommunicationModule;
|
2015-06-22 21:49:04 +00:00
|
|
|
import org.thoughtcrime.securesms.jobs.requirements.MasterSecretRequirement;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
|
|
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
2016-03-23 17:34:41 +00: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;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
2015-06-22 21:49:04 +00:00
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
public class MultiDeviceGroupUpdateJob extends MasterSecretJob implements InjectableType {
|
|
|
|
|
2015-06-25 01:26:51 +00:00
|
|
|
private static final long serialVersionUID = 1L;
|
2015-08-06 17:20:13 +00:00
|
|
|
private static final String TAG = MultiDeviceGroupUpdateJob.class.getSimpleName();
|
2015-06-25 01:26:51 +00:00
|
|
|
|
2015-06-22 21:49:04 +00:00
|
|
|
@Inject
|
2016-12-20 17:55:52 +00:00
|
|
|
transient SignalCommunicationModule.SignalMessageSenderFactory messageSenderFactory;
|
2015-06-22 21:49:04 +00:00
|
|
|
|
|
|
|
public MultiDeviceGroupUpdateJob(Context context) {
|
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withRequirement(new NetworkRequirement(context))
|
|
|
|
.withRequirement(new MasterSecretRequirement(context))
|
|
|
|
.withGroupId(MultiDeviceGroupUpdateJob.class.getSimpleName())
|
|
|
|
.withPersistence()
|
|
|
|
.create());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onRun(MasterSecret masterSecret) throws Exception {
|
2016-03-23 17:34:41 +00:00
|
|
|
SignalServiceMessageSender messageSender = messageSenderFactory.create();
|
|
|
|
File contactDataFile = createTempFile("multidevice-contact-update");
|
|
|
|
GroupDatabase.Reader reader = null;
|
2015-06-22 21:49:04 +00:00
|
|
|
|
|
|
|
GroupDatabase.GroupRecord record;
|
|
|
|
|
|
|
|
try {
|
|
|
|
DeviceGroupsOutputStream out = new DeviceGroupsOutputStream(new FileOutputStream(contactDataFile));
|
|
|
|
|
|
|
|
reader = DatabaseFactory.getGroupDatabase(context).getGroups();
|
|
|
|
|
|
|
|
while ((record = reader.getNext()) != null) {
|
|
|
|
out.write(new DeviceGroup(record.getId(), Optional.fromNullable(record.getTitle()),
|
2016-02-16 19:47:47 +00:00
|
|
|
record.getMembers(), getAvatar(record.getAvatar()),
|
|
|
|
record.isActive()));
|
2015-06-22 21:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
2015-08-06 17:20:13 +00:00
|
|
|
if (contactDataFile.exists() && contactDataFile.length() > 0) {
|
|
|
|
sendUpdate(messageSender, contactDataFile);
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "No groups present for sync message...");
|
|
|
|
}
|
2015-06-22 21:49:04 +00:00
|
|
|
|
|
|
|
} finally {
|
|
|
|
if (contactDataFile != null) contactDataFile.delete();
|
|
|
|
if (reader != null) reader.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
|
|
|
if (exception instanceof PushNetworkException) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-23 17:34:41 +00:00
|
|
|
private void sendUpdate(SignalServiceMessageSender messageSender, File contactsFile)
|
2015-06-22 21:49:04 +00:00
|
|
|
throws IOException, UntrustedIdentityException
|
|
|
|
{
|
2016-03-23 17:34:41 +00:00
|
|
|
FileInputStream contactsFileStream = new FileInputStream(contactsFile);
|
|
|
|
SignalServiceAttachmentStream attachmentStream = SignalServiceAttachment.newStreamBuilder()
|
|
|
|
.withStream(contactsFileStream)
|
|
|
|
.withContentType("application/octet-stream")
|
|
|
|
.withLength(contactsFile.length())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
messageSender.sendMessage(SignalServiceSyncMessage.forGroups(attachmentStream));
|
2015-06-22 21:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-03-23 17:34:41 +00:00
|
|
|
private Optional<SignalServiceAttachmentStream> getAvatar(@Nullable byte[] avatar) {
|
2015-06-22 21:49:04 +00:00
|
|
|
if (avatar == null) return Optional.absent();
|
|
|
|
|
2016-03-23 17:34:41 +00:00
|
|
|
return Optional.of(SignalServiceAttachment.newStreamBuilder()
|
|
|
|
.withStream(new ByteArrayInputStream(avatar))
|
|
|
|
.withContentType("image/*")
|
|
|
|
.withLength(avatar.length)
|
|
|
|
.build());
|
2015-06-22 21:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private File createTempFile(String prefix) throws IOException {
|
|
|
|
File file = File.createTempFile(prefix, "tmp", context.getCacheDir());
|
|
|
|
file.deleteOnExit();
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|