mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-01 05:55:18 +00:00
98 lines
4.1 KiB
Java
98 lines
4.1 KiB
Java
|
package org.thoughtcrime.securesms.jobs;
|
||
|
|
||
|
|
||
|
import android.content.Context;
|
||
|
import android.util.Log;
|
||
|
|
||
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
||
|
import org.thoughtcrime.securesms.database.GroupDatabase;
|
||
|
import org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord;
|
||
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
||
|
import org.thoughtcrime.securesms.dependencies.TextSecureCommunicationModule.TextSecureMessageSenderFactory;
|
||
|
import org.whispersystems.jobqueue.JobParameters;
|
||
|
import org.whispersystems.jobqueue.requirements.NetworkRequirement;
|
||
|
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.SignalServiceDataMessage;
|
||
|
import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
|
||
|
import org.whispersystems.signalservice.api.messages.SignalServiceGroup.Type;
|
||
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
||
|
|
||
|
import java.io.ByteArrayInputStream;
|
||
|
import java.io.IOException;
|
||
|
|
||
|
import javax.inject.Inject;
|
||
|
|
||
|
public class PushGroupUpdateJob extends ContextJob implements InjectableType {
|
||
|
|
||
|
private static final String TAG = RequestGroupInfoJob.class.getSimpleName();
|
||
|
|
||
|
private static final long serialVersionUID = 0L;
|
||
|
|
||
|
@Inject transient TextSecureMessageSenderFactory messageSenderFactory;
|
||
|
|
||
|
private final String source;
|
||
|
private final byte[] groupId;
|
||
|
|
||
|
|
||
|
public PushGroupUpdateJob(Context context, String source, byte[] groupId) {
|
||
|
super(context, JobParameters.newBuilder()
|
||
|
.withPersistence()
|
||
|
.withRequirement(new NetworkRequirement(context))
|
||
|
.withRetryCount(50)
|
||
|
.create());
|
||
|
|
||
|
this.source = source;
|
||
|
this.groupId = groupId;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onAdded() {}
|
||
|
|
||
|
@Override
|
||
|
public void onRun() throws IOException, UntrustedIdentityException {
|
||
|
SignalServiceMessageSender messageSender = messageSenderFactory.create();
|
||
|
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
||
|
GroupRecord record = groupDatabase.getGroup(groupId);
|
||
|
|
||
|
if (record == null) {
|
||
|
Log.w(TAG, "No information for group record info request: " + new String(groupId));
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
SignalServiceAttachment avatar = SignalServiceAttachmentStream.newStreamBuilder()
|
||
|
.withContentType("image/jpeg")
|
||
|
.withStream(new ByteArrayInputStream(record.getAvatar()))
|
||
|
.withLength(record.getAvatar().length)
|
||
|
.build();
|
||
|
|
||
|
SignalServiceGroup groupContext = SignalServiceGroup.newBuilder(Type.UPDATE)
|
||
|
.withAvatar(avatar)
|
||
|
.withId(groupId)
|
||
|
.withMembers(record.getMembers())
|
||
|
.withName(record.getTitle())
|
||
|
.build();
|
||
|
|
||
|
SignalServiceDataMessage message = SignalServiceDataMessage.newBuilder()
|
||
|
.asGroupMessage(groupContext)
|
||
|
.withTimestamp(System.currentTimeMillis())
|
||
|
.build();
|
||
|
|
||
|
messageSender.sendMessage(new SignalServiceAddress(source), message);
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean onShouldRetry(Exception e) {
|
||
|
Log.w(TAG, e);
|
||
|
return e instanceof PushNetworkException;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void onCanceled() {
|
||
|
|
||
|
}
|
||
|
}
|