mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-23 12:38:33 +00:00
Convert MultiDeviceKeysUpdateJob to kotlin.
This commit is contained in:
@@ -1,94 +0,0 @@
|
|||||||
package org.thoughtcrime.securesms.jobs;
|
|
||||||
|
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
|
|
||||||
import org.signal.core.util.logging.Log;
|
|
||||||
import org.thoughtcrime.securesms.dependencies.AppDependencies;
|
|
||||||
import org.thoughtcrime.securesms.jobmanager.Job;
|
|
||||||
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint;
|
|
||||||
import org.thoughtcrime.securesms.keyvalue.SignalStore;
|
|
||||||
import org.thoughtcrime.securesms.net.NotPushRegisteredException;
|
|
||||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
||||||
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
|
||||||
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
|
||||||
import org.whispersystems.signalservice.api.messages.multidevice.KeysMessage;
|
|
||||||
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
|
|
||||||
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException;
|
|
||||||
import org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException;
|
|
||||||
import org.whispersystems.signalservice.api.storage.StorageKey;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
public class MultiDeviceKeysUpdateJob extends BaseJob {
|
|
||||||
|
|
||||||
public static final String KEY = "MultiDeviceKeysUpdateJob";
|
|
||||||
|
|
||||||
private static final String TAG = Log.tag(MultiDeviceKeysUpdateJob.class);
|
|
||||||
|
|
||||||
public MultiDeviceKeysUpdateJob() {
|
|
||||||
this(new Parameters.Builder()
|
|
||||||
.setQueue("MultiDeviceKeysUpdateJob")
|
|
||||||
.setMaxInstancesForFactory(2)
|
|
||||||
.addConstraint(NetworkConstraint.KEY)
|
|
||||||
.setMaxAttempts(10)
|
|
||||||
.build());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
private MultiDeviceKeysUpdateJob(@NonNull Parameters parameters) {
|
|
||||||
super(parameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @Nullable byte[] serialize() {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public @NonNull String getFactoryKey() {
|
|
||||||
return KEY;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onRun() throws IOException, UntrustedIdentityException {
|
|
||||||
if (!Recipient.self().isRegistered()) {
|
|
||||||
throw new NotPushRegisteredException();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!SignalStore.account().hasLinkedDevices()) {
|
|
||||||
Log.i(TAG, "Not multi device, aborting...");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (SignalStore.account().isLinkedDevice()) {
|
|
||||||
Log.i(TAG, "Not primary device, aborting...");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
SignalServiceMessageSender messageSender = AppDependencies.getSignalServiceMessageSender();
|
|
||||||
StorageKey storageServiceKey = SignalStore.storageService().getOrCreateStorageKey();
|
|
||||||
|
|
||||||
messageSender.sendSyncMessage(SignalServiceSyncMessage.forKeys(new KeysMessage(Optional.ofNullable(storageServiceKey), Optional.of(SignalStore.svr().getMasterKey())))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onShouldRetry(@NonNull Exception e) {
|
|
||||||
if (e instanceof ServerRejectedException) return false;
|
|
||||||
return e instanceof PushNetworkException;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onFailure() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static final class Factory implements Job.Factory<MultiDeviceKeysUpdateJob> {
|
|
||||||
@Override
|
|
||||||
public @NonNull MultiDeviceKeysUpdateJob create(@NonNull Parameters parameters, @Nullable byte[] serializedData) {
|
|
||||||
return new MultiDeviceKeysUpdateJob(parameters);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
package org.thoughtcrime.securesms.jobs
|
||||||
|
|
||||||
|
import org.signal.core.util.logging.Log
|
||||||
|
import org.thoughtcrime.securesms.dependencies.AppDependencies
|
||||||
|
import org.thoughtcrime.securesms.jobmanager.Job
|
||||||
|
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint
|
||||||
|
import org.thoughtcrime.securesms.keyvalue.SignalStore
|
||||||
|
import org.thoughtcrime.securesms.net.NotPushRegisteredException
|
||||||
|
import org.thoughtcrime.securesms.recipients.Recipient
|
||||||
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException
|
||||||
|
import org.whispersystems.signalservice.api.messages.multidevice.KeysMessage
|
||||||
|
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage
|
||||||
|
import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException
|
||||||
|
import org.whispersystems.signalservice.api.push.exceptions.ServerRejectedException
|
||||||
|
import java.io.IOException
|
||||||
|
import java.util.Optional
|
||||||
|
|
||||||
|
class MultiDeviceKeysUpdateJob private constructor(parameters: Parameters) : BaseJob(parameters) {
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
const val KEY: String = "MultiDeviceKeysUpdateJob"
|
||||||
|
|
||||||
|
private val TAG = Log.tag(MultiDeviceKeysUpdateJob::class.java)
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor() : this(
|
||||||
|
Parameters.Builder()
|
||||||
|
.setQueue("MultiDeviceKeysUpdateJob")
|
||||||
|
.setMaxInstancesForFactory(2)
|
||||||
|
.addConstraint(NetworkConstraint.KEY)
|
||||||
|
.setMaxAttempts(10)
|
||||||
|
.build()
|
||||||
|
)
|
||||||
|
|
||||||
|
override fun serialize(): ByteArray? = null
|
||||||
|
|
||||||
|
override fun getFactoryKey(): String = KEY
|
||||||
|
|
||||||
|
@Throws(IOException::class, UntrustedIdentityException::class)
|
||||||
|
public override fun onRun() {
|
||||||
|
if (!Recipient.self().isRegistered) {
|
||||||
|
throw NotPushRegisteredException()
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!SignalStore.account.hasLinkedDevices) {
|
||||||
|
Log.i(TAG, "Not multi device, aborting...")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (SignalStore.account.isLinkedDevice) {
|
||||||
|
Log.i(TAG, "Not primary device, aborting...")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val syncMessage = SignalServiceSyncMessage.forKeys(
|
||||||
|
KeysMessage(
|
||||||
|
Optional.of(SignalStore.storageService.getOrCreateStorageKey()),
|
||||||
|
Optional.of(SignalStore.svr.masterKey)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
AppDependencies.signalServiceMessageSender.sendSyncMessage(syncMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
public override fun onShouldRetry(e: Exception): Boolean {
|
||||||
|
if (e is ServerRejectedException) return false
|
||||||
|
return e is PushNetworkException
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onFailure() {
|
||||||
|
}
|
||||||
|
|
||||||
|
class Factory : Job.Factory<MultiDeviceKeysUpdateJob?> {
|
||||||
|
override fun create(parameters: Parameters, serializedData: ByteArray?): MultiDeviceKeysUpdateJob {
|
||||||
|
return MultiDeviceKeysUpdateJob(parameters)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user