clean up jobs
parent
dee7d78acb
commit
12a2061251
@ -1,169 +0,0 @@
|
|||||||
package org.thoughtcrime.securesms.jobmanager.migration;
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
import org.session.libsession.messaging.jobs.Data;
|
|
||||||
import org.session.libsignal.utilities.logging.Log;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.ObjectInputStream;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Takes a persisted data blob stored by WorkManager and converts it to our {@link Data} class.
|
|
||||||
*/
|
|
||||||
final class DataMigrator {
|
|
||||||
|
|
||||||
private static final String TAG = Log.tag(DataMigrator.class);
|
|
||||||
|
|
||||||
static final Data convert(@NonNull byte[] workManagerData) {
|
|
||||||
Map<String, Object> values = parseWorkManagerDataMap(workManagerData);
|
|
||||||
|
|
||||||
Data.Builder builder = new Data.Builder();
|
|
||||||
|
|
||||||
for (Map.Entry<String, Object> entry : values.entrySet()) {
|
|
||||||
Object value = entry.getValue();
|
|
||||||
|
|
||||||
if (value == null) {
|
|
||||||
builder.putString(entry.getKey(), null);
|
|
||||||
} else {
|
|
||||||
Class type = value.getClass();
|
|
||||||
|
|
||||||
if (type == String.class) {
|
|
||||||
builder.putString(entry.getKey(), (String) value);
|
|
||||||
} else if (type == String[].class) {
|
|
||||||
builder.putStringArray(entry.getKey(), (String[]) value);
|
|
||||||
} else if (type == Integer.class || type == int.class) {
|
|
||||||
builder.putInt(entry.getKey(), (int) value);
|
|
||||||
} else if (type == Integer[].class || type == int[].class) {
|
|
||||||
builder.putIntArray(entry.getKey(), convertToIntArray(value, type));
|
|
||||||
} else if (type == Long.class || type == long.class) {
|
|
||||||
builder.putLong(entry.getKey(), (long) value);
|
|
||||||
} else if (type == Long[].class || type == long[].class) {
|
|
||||||
builder.putLongArray(entry.getKey(), convertToLongArray(value, type));
|
|
||||||
} else if (type == Float.class || type == float.class) {
|
|
||||||
builder.putFloat(entry.getKey(), (float) value);
|
|
||||||
} else if (type == Float[].class || type == float[].class) {
|
|
||||||
builder.putFloatArray(entry.getKey(), convertToFloatArray(value, type));
|
|
||||||
} else if (type == Double.class || type == double.class) {
|
|
||||||
builder.putDouble(entry.getKey(), (double) value);
|
|
||||||
} else if (type == Double[].class || type == double[].class) {
|
|
||||||
builder.putDoubleArray(entry.getKey(), convertToDoubleArray(value, type));
|
|
||||||
} else if (type == Boolean.class || type == boolean.class) {
|
|
||||||
builder.putBoolean(entry.getKey(), (boolean) value);
|
|
||||||
} else if (type == Boolean[].class || type == boolean[].class) {
|
|
||||||
builder.putBooleanArray(entry.getKey(), convertToBooleanArray(value, type));
|
|
||||||
} else {
|
|
||||||
Log.w(TAG, "Encountered unexpected type '" + type + "'. Skipping.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return builder.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static @NonNull Map<String, Object> parseWorkManagerDataMap(@NonNull byte[] bytes) throws IllegalStateException {
|
|
||||||
Map<String, Object> map = new HashMap<>();
|
|
||||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
|
|
||||||
ObjectInputStream objectInputStream = null;
|
|
||||||
|
|
||||||
try {
|
|
||||||
objectInputStream = new ObjectInputStream(inputStream);
|
|
||||||
|
|
||||||
for (int i = objectInputStream.readInt(); i > 0; i--) {
|
|
||||||
map.put(objectInputStream.readUTF(), objectInputStream.readObject());
|
|
||||||
}
|
|
||||||
} catch (IOException | ClassNotFoundException e) {
|
|
||||||
Log.w(TAG, "Failed to read WorkManager data.", e);
|
|
||||||
} finally {
|
|
||||||
try {
|
|
||||||
inputStream.close();
|
|
||||||
|
|
||||||
if (objectInputStream != null) {
|
|
||||||
objectInputStream.close();
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
Log.e(TAG, "Failed to close streams after reading WorkManager data.", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int[] convertToIntArray(Object value, Class type) {
|
|
||||||
if (type == int[].class) {
|
|
||||||
return (int[]) value;
|
|
||||||
}
|
|
||||||
|
|
||||||
Integer[] casted = (Integer[]) value;
|
|
||||||
int[] output = new int[casted.length];
|
|
||||||
|
|
||||||
for (int i = 0; i < casted.length; i++) {
|
|
||||||
output[i] = casted[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static long[] convertToLongArray(Object value, Class type) {
|
|
||||||
if (type == long[].class) {
|
|
||||||
return (long[]) value;
|
|
||||||
}
|
|
||||||
|
|
||||||
Long[] casted = (Long[]) value;
|
|
||||||
long[] output = new long[casted.length];
|
|
||||||
|
|
||||||
for (int i = 0; i < casted.length; i++) {
|
|
||||||
output[i] = casted[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static float[] convertToFloatArray(Object value, Class type) {
|
|
||||||
if (type == float[].class) {
|
|
||||||
return (float[]) value;
|
|
||||||
}
|
|
||||||
|
|
||||||
Float[] casted = (Float[]) value;
|
|
||||||
float[] output = new float[casted.length];
|
|
||||||
|
|
||||||
for (int i = 0; i < casted.length; i++) {
|
|
||||||
output[i] = casted[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static double[] convertToDoubleArray(Object value, Class type) {
|
|
||||||
if (type == double[].class) {
|
|
||||||
return (double[]) value;
|
|
||||||
}
|
|
||||||
|
|
||||||
Double[] casted = (Double[]) value;
|
|
||||||
double[] output = new double[casted.length];
|
|
||||||
|
|
||||||
for (int i = 0; i < casted.length; i++) {
|
|
||||||
output[i] = casted[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static boolean[] convertToBooleanArray(Object value, Class type) {
|
|
||||||
if (type == boolean[].class) {
|
|
||||||
return (boolean[]) value;
|
|
||||||
}
|
|
||||||
|
|
||||||
Boolean[] casted = (Boolean[]) value;
|
|
||||||
boolean[] output = new boolean[casted.length];
|
|
||||||
|
|
||||||
for (int i = 0; i < casted.length; i++) {
|
|
||||||
output[i] = casted[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,152 +0,0 @@
|
|||||||
package org.thoughtcrime.securesms.jobs;
|
|
||||||
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
|
|
||||||
import org.session.libsession.messaging.jobs.Data;
|
|
||||||
import org.session.libsession.messaging.threads.Address;
|
|
||||||
import org.session.libsession.messaging.threads.GroupRecord;
|
|
||||||
import org.session.libsession.messaging.threads.recipients.Recipient;
|
|
||||||
import org.session.libsession.utilities.GroupUtil;
|
|
||||||
|
|
||||||
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
|
|
||||||
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
||||||
import org.thoughtcrime.securesms.database.GroupDatabase;
|
|
||||||
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
|
||||||
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
||||||
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
|
||||||
import org.session.libsignal.utilities.logging.Log;
|
|
||||||
import org.session.libsignal.libsignal.util.guava.Optional;
|
|
||||||
import org.session.libsignal.service.api.SignalServiceMessageSender;
|
|
||||||
import org.session.libsignal.service.api.crypto.UntrustedIdentityException;
|
|
||||||
import org.session.libsignal.service.api.messages.SignalServiceAttachment;
|
|
||||||
import org.session.libsignal.service.api.messages.SignalServiceAttachmentStream;
|
|
||||||
import org.session.libsignal.service.api.messages.SignalServiceDataMessage;
|
|
||||||
import org.session.libsignal.service.api.messages.SignalServiceGroup;
|
|
||||||
import org.session.libsignal.service.api.messages.SignalServiceGroup.Type;
|
|
||||||
import org.session.libsignal.service.api.push.SignalServiceAddress;
|
|
||||||
import org.session.libsignal.service.api.push.exceptions.PushNetworkException;
|
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import javax.inject.Inject;
|
|
||||||
|
|
||||||
public class PushGroupUpdateJob extends BaseJob implements InjectableType {
|
|
||||||
|
|
||||||
public static final String KEY = "PushGroupUpdateJob";
|
|
||||||
|
|
||||||
private static final String TAG = PushGroupUpdateJob.class.getSimpleName();
|
|
||||||
|
|
||||||
private static final String KEY_SOURCE = "source";
|
|
||||||
private static final String KEY_GROUP_ID = "group_id";
|
|
||||||
|
|
||||||
@Inject SignalServiceMessageSender messageSender;
|
|
||||||
|
|
||||||
private String source;
|
|
||||||
private byte[] groupId;
|
|
||||||
|
|
||||||
public PushGroupUpdateJob(String source, byte[] groupId) {
|
|
||||||
this(new Job.Parameters.Builder()
|
|
||||||
.addConstraint(NetworkConstraint.KEY)
|
|
||||||
.setLifespan(TimeUnit.DAYS.toMillis(1))
|
|
||||||
.setMaxAttempts(Parameters.UNLIMITED)
|
|
||||||
.build(),
|
|
||||||
source,
|
|
||||||
groupId);
|
|
||||||
}
|
|
||||||
|
|
||||||
private PushGroupUpdateJob(@NonNull Job.Parameters parameters, String source, byte[] groupId) {
|
|
||||||
super(parameters);
|
|
||||||
|
|
||||||
this.source = source;
|
|
||||||
this.groupId = groupId;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NonNull
|
|
||||||
Data serialize() {
|
|
||||||
return new Data.Builder().putString(KEY_SOURCE, source)
|
|
||||||
.putString(KEY_GROUP_ID, GroupUtil.getEncodedClosedGroupID(groupId))
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NonNull String getFactoryKey() {
|
|
||||||
return KEY;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onRun() throws IOException, UntrustedIdentityException {
|
|
||||||
GroupDatabase groupDatabase = DatabaseFactory.getGroupDatabase(context);
|
|
||||||
Optional<GroupRecord> record = groupDatabase.getGroup(GroupUtil.getEncodedClosedGroupID(groupId));
|
|
||||||
SignalServiceAttachment avatar = null;
|
|
||||||
|
|
||||||
if (record == null) {
|
|
||||||
Log.w(TAG, "No information for group record info request: " + new String(groupId));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (record.get().getAvatar() != null) {
|
|
||||||
avatar = SignalServiceAttachmentStream.newStreamBuilder()
|
|
||||||
.withContentType("image/jpeg")
|
|
||||||
.withStream(new ByteArrayInputStream(record.get().getAvatar()))
|
|
||||||
.withLength(record.get().getAvatar().length)
|
|
||||||
.build();
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> members = new LinkedList<>();
|
|
||||||
for (Address member : record.get().getMembers()) {
|
|
||||||
members.add(member.serialize());
|
|
||||||
}
|
|
||||||
|
|
||||||
List<String> admins = new LinkedList<>();
|
|
||||||
for (Address admin : record.get().getAdmins()) {
|
|
||||||
admins.add(admin.serialize());
|
|
||||||
}
|
|
||||||
|
|
||||||
SignalServiceGroup groupContext = SignalServiceGroup.newBuilder(Type.UPDATE)
|
|
||||||
.withAvatar(avatar)
|
|
||||||
.withId(groupId, SignalServiceGroup.GroupType.SIGNAL)
|
|
||||||
.withMembers(members)
|
|
||||||
.withAdmins(admins)
|
|
||||||
.withName(record.get().getTitle())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
Address groupAddress = Address.fromSerialized(GroupUtil.getEncodedClosedGroupID(groupId));
|
|
||||||
Recipient groupRecipient = Recipient.from(context, groupAddress, false);
|
|
||||||
|
|
||||||
SignalServiceDataMessage message = SignalServiceDataMessage.newBuilder()
|
|
||||||
.asGroupMessage(groupContext)
|
|
||||||
.withTimestamp(System.currentTimeMillis())
|
|
||||||
.withExpiration(groupRecipient.getExpireMessages())
|
|
||||||
.build();
|
|
||||||
|
|
||||||
messageSender.sendMessage(0, new SignalServiceAddress(source),
|
|
||||||
UnidentifiedAccessUtil.getAccessFor(context, Recipient.from(context, Address.fromSerialized(source), false)),
|
|
||||||
message);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onShouldRetry(@NonNull Exception e) {
|
|
||||||
Log.w(TAG, e);
|
|
||||||
return e instanceof PushNetworkException;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCanceled() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final class Factory implements Job.Factory<PushGroupUpdateJob> {
|
|
||||||
@Override
|
|
||||||
public @NonNull PushGroupUpdateJob create(@NonNull Parameters parameters, @NonNull Data data) {
|
|
||||||
return new PushGroupUpdateJob(parameters,
|
|
||||||
data.getString(KEY_SOURCE),
|
|
||||||
GroupUtil.getDecodedGroupIDAsData(data.getString(KEY_GROUP_ID)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue