kopia lustrzana https://github.com/ryukoposting/Signal-Android
Add username sync job to be run after new registrations.
rodzic
877a62b809
commit
e222f96310
|
@ -497,6 +497,34 @@ public class JobManager implements ConstraintObserver.Notifier {
|
|||
enqueue();
|
||||
}
|
||||
|
||||
public Optional<JobTracker.JobState> enqueueAndBlockUntilCompletion(long timeout) {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
AtomicReference<JobTracker.JobState> resultState = new AtomicReference<>();
|
||||
JobTracker.JobListener listener = new JobTracker.JobListener() {
|
||||
@Override
|
||||
public void onStateChanged(@NonNull Job job, @NonNull JobTracker.JobState jobState) {
|
||||
if (jobState.isComplete()) {
|
||||
jobManager.removeListener(this);
|
||||
resultState.set(jobState);
|
||||
latch.countDown();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
enqueue(listener);
|
||||
|
||||
try {
|
||||
if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
|
||||
return Optional.empty();
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Log.w(TAG, "Interrupted during enqueueSynchronously()", e);
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.ofNullable(resultState.get());
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
public List<List<Job>> getJobListChain() {
|
||||
return jobs;
|
||||
|
|
|
@ -141,6 +141,7 @@ public final class JobManagerFactories {
|
|||
put(MultiDeviceVerifiedUpdateJob.KEY, new MultiDeviceVerifiedUpdateJob.Factory());
|
||||
put(MultiDeviceViewOnceOpenJob.KEY, new MultiDeviceViewOnceOpenJob.Factory());
|
||||
put(MultiDeviceViewedUpdateJob.KEY, new MultiDeviceViewedUpdateJob.Factory());
|
||||
put(NewRegistrationUsernameSyncJob.KEY, new NewRegistrationUsernameSyncJob.Factory());
|
||||
put(NullMessageSendJob.KEY, new NullMessageSendJob.Factory());
|
||||
put(OptimizeMessageSearchIndexJob.KEY, new OptimizeMessageSearchIndexJob.Factory());
|
||||
put(PaymentLedgerUpdateJob.KEY, new PaymentLedgerUpdateJob.Factory());
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
package org.thoughtcrime.securesms.jobs
|
||||
|
||||
import org.signal.core.util.logging.Log
|
||||
import org.thoughtcrime.securesms.database.SignalDatabase
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
|
||||
import org.thoughtcrime.securesms.jobmanager.Data
|
||||
import org.thoughtcrime.securesms.jobmanager.Job
|
||||
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint
|
||||
import org.thoughtcrime.securesms.recipients.Recipient
|
||||
import java.io.IOException
|
||||
|
||||
/**
|
||||
* If a user registers and the storage sync service doesn't contain a username,
|
||||
* then we should delete our username from the server.
|
||||
*/
|
||||
class NewRegistrationUsernameSyncJob private constructor(parameters: Parameters) : BaseJob(parameters) {
|
||||
|
||||
companion object {
|
||||
private val TAG = Log.tag(NewRegistrationUsernameSyncJob::class.java)
|
||||
|
||||
const val KEY = "NewRegistrationUsernameSyncJob"
|
||||
}
|
||||
|
||||
constructor() : this(
|
||||
Parameters.Builder()
|
||||
.setQueue(StorageSyncJob.QUEUE_KEY)
|
||||
.setMaxInstancesForFactory(1)
|
||||
.addConstraint(NetworkConstraint.KEY)
|
||||
.build()
|
||||
)
|
||||
|
||||
override fun serialize(): Data = Data.EMPTY
|
||||
|
||||
override fun getFactoryKey(): String = KEY
|
||||
|
||||
override fun onFailure() = Unit
|
||||
|
||||
override fun onRun() {
|
||||
if (SignalDatabase.recipients.getUsername(Recipient.self().id).isNullOrEmpty()) {
|
||||
Log.i(TAG, "Clearing username from server.")
|
||||
ApplicationDependencies.getSignalServiceAccountManager().deleteUsername()
|
||||
} else {
|
||||
Log.i(TAG, "Local user has a username, attempting username synchronization.")
|
||||
RefreshOwnProfileJob.checkUsernameIsInSync()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onShouldRetry(e: Exception): Boolean {
|
||||
return e is IOException
|
||||
}
|
||||
|
||||
class Factory : Job.Factory<NewRegistrationUsernameSyncJob> {
|
||||
override fun create(parameters: Parameters, data: Data): NewRegistrationUsernameSyncJob {
|
||||
return NewRegistrationUsernameSyncJob(parameters)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -253,7 +253,6 @@ public class RefreshOwnProfileJob extends BaseJob {
|
|||
}
|
||||
}
|
||||
|
||||
@VisibleForTesting
|
||||
static void checkUsernameIsInSync() {
|
||||
try {
|
||||
String localUsername = SignalDatabase.recipients().getUsername(Recipient.self().getId());
|
||||
|
|
|
@ -5,6 +5,7 @@ import androidx.annotation.NonNull;
|
|||
import org.signal.core.util.concurrent.SignalExecutors;
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.thoughtcrime.securesms.jobs.NewRegistrationUsernameSyncJob;
|
||||
import org.thoughtcrime.securesms.jobs.StorageAccountRestoreJob;
|
||||
import org.thoughtcrime.securesms.jobs.StorageSyncJob;
|
||||
import org.signal.core.util.Stopwatch;
|
||||
|
@ -34,7 +35,11 @@ public class PinRestoreRepository {
|
|||
ApplicationDependencies.getJobManager().runSynchronously(new StorageAccountRestoreJob(), StorageAccountRestoreJob.LIFESPAN);
|
||||
stopwatch.split("AccountRestore");
|
||||
|
||||
ApplicationDependencies.getJobManager().runSynchronously(new StorageSyncJob(), TimeUnit.SECONDS.toMillis(10));
|
||||
ApplicationDependencies
|
||||
.getJobManager()
|
||||
.startChain(new StorageSyncJob())
|
||||
.then(new NewRegistrationUsernameSyncJob())
|
||||
.enqueueAndBlockUntilCompletion(TimeUnit.SECONDS.toMillis(10));
|
||||
stopwatch.split("ContactRestore");
|
||||
|
||||
stopwatch.stop(TAG);
|
||||
|
|
|
@ -8,6 +8,7 @@ import org.signal.core.util.concurrent.SimpleTask;
|
|||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.thoughtcrime.securesms.jobs.NewRegistrationUsernameSyncJob;
|
||||
import org.thoughtcrime.securesms.jobs.StorageAccountRestoreJob;
|
||||
import org.thoughtcrime.securesms.jobs.StorageSyncJob;
|
||||
import org.thoughtcrime.securesms.keyvalue.SignalStore;
|
||||
|
@ -52,7 +53,11 @@ public final class RegistrationLockFragment extends BaseRegistrationLockFragment
|
|||
ApplicationDependencies.getJobManager().runSynchronously(new StorageAccountRestoreJob(), StorageAccountRestoreJob.LIFESPAN);
|
||||
stopwatch.split("AccountRestore");
|
||||
|
||||
ApplicationDependencies.getJobManager().runSynchronously(new StorageSyncJob(), TimeUnit.SECONDS.toMillis(10));
|
||||
ApplicationDependencies
|
||||
.getJobManager()
|
||||
.startChain(new StorageSyncJob())
|
||||
.then(new NewRegistrationUsernameSyncJob())
|
||||
.enqueueAndBlockUntilCompletion(TimeUnit.SECONDS.toMillis(10));
|
||||
stopwatch.split("ContactRestore");
|
||||
|
||||
try {
|
||||
|
|
|
@ -12,6 +12,7 @@ import androidx.savedstate.SavedStateRegistryOwner;
|
|||
import org.signal.core.util.Stopwatch;
|
||||
import org.signal.core.util.logging.Log;
|
||||
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
|
||||
import org.thoughtcrime.securesms.jobs.NewRegistrationUsernameSyncJob;
|
||||
import org.thoughtcrime.securesms.jobs.StorageAccountRestoreJob;
|
||||
import org.thoughtcrime.securesms.jobs.StorageSyncJob;
|
||||
import org.thoughtcrime.securesms.keyvalue.SignalStore;
|
||||
|
@ -383,7 +384,11 @@ public final class RegistrationViewModel extends BaseRegistrationViewModel {
|
|||
ApplicationDependencies.getJobManager().runSynchronously(new StorageAccountRestoreJob(), StorageAccountRestoreJob.LIFESPAN);
|
||||
stopwatch.split("AccountRestore");
|
||||
|
||||
ApplicationDependencies.getJobManager().runSynchronously(new StorageSyncJob(), TimeUnit.SECONDS.toMillis(10));
|
||||
ApplicationDependencies
|
||||
.getJobManager()
|
||||
.startChain(new StorageSyncJob())
|
||||
.then(new NewRegistrationUsernameSyncJob())
|
||||
.enqueueAndBlockUntilCompletion(TimeUnit.SECONDS.toMillis(10));
|
||||
stopwatch.split("ContactRestore");
|
||||
|
||||
try {
|
||||
|
|
Ładowanie…
Reference in New Issue