Skip to content

Commit

Permalink
Fix can create backups when timed backup is waiting for charging cons…
Browse files Browse the repository at this point in the history
…traint.
  • Loading branch information
alan-signal committed Oct 2, 2020
1 parent 19c74c8 commit 8de4290
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static void showEnableBackupDialog(@NonNull Context context, @NonNull Swi
CheckBox confirmationCheckBox = dialog.findViewById(R.id.confirmation_check);
if (confirmationCheckBox.isChecked()) {
BackupPassphrase.set(context, Util.join(password, " "));
TextSecurePreferences.setNextBackupTime(context, 0);
TextSecurePreferences.setBackupEnabled(context, true);
LocalBackupListener.schedule(context);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.thoughtcrime.securesms.jobmanager;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
Expand All @@ -23,7 +24,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
Expand Down Expand Up @@ -158,9 +158,8 @@ synchronized void cancelJob(@NonNull String id) {

@WorkerThread
synchronized void cancelAllInQueue(@NonNull String queue) {
Stream.of(runningJobs.values())
.filter(j -> Objects.equals(j.getParameters().getQueue(), queue))
.map(Job::getId)
Stream.of(jobStorage.getJobsInQueue(queue))
.map(JobSpec::getId)
.forEach(this::cancelJob);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import org.thoughtcrime.securesms.crypto.AttachmentSecretProvider;
import org.thoughtcrime.securesms.database.DatabaseFactory;
import org.thoughtcrime.securesms.database.NoExternalStorageException;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.jobmanager.Data;
import org.thoughtcrime.securesms.jobmanager.Job;
import org.thoughtcrime.securesms.jobmanager.JobManager;
import org.thoughtcrime.securesms.jobmanager.impl.ChargingConstraint;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.notifications.NotificationChannels;
Expand All @@ -34,24 +36,24 @@ public final class LocalBackupJob extends BaseJob {

private static final String TAG = Log.tag(LocalBackupJob.class);

private static final String QUEUE = "__LOCAL_BACKUP__";

public static final String TEMP_BACKUP_FILE_PREFIX = ".backup";
public static final String TEMP_BACKUP_FILE_SUFFIX = ".tmp";

public LocalBackupJob(boolean forceNow) {
this(buildParameters(forceNow));
}

private static @NonNull Job.Parameters buildParameters(boolean forceNow) {
Job.Parameters.Builder builder = new Job.Parameters.Builder()
.setQueue("__LOCAL_BACKUP__")
.setMaxInstances(1)
.setMaxAttempts(3);

if (!forceNow) {
builder.addConstraint(ChargingConstraint.KEY);
public static void enqueue(boolean force) {
JobManager jobManager = ApplicationDependencies.getJobManager();
Parameters.Builder parameters = new Parameters.Builder()
.setQueue(QUEUE)
.setMaxInstances(1)
.setMaxAttempts(3);
if (force) {
jobManager.cancelAllInQueue(QUEUE);
} else {
parameters.addConstraint(ChargingConstraint.KEY);
}

return builder.build();
jobManager.add(new LocalBackupJob(parameters.build()));
}

private LocalBackupJob(@NonNull Job.Parameters parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.thoughtcrime.securesms.backup.BackupDialog;
import org.thoughtcrime.securesms.backup.FullBackupBase.BackupEvent;
import org.thoughtcrime.securesms.components.SwitchPreferenceCompat;
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.jobs.LocalBackupJob;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.permissions.Permissions;
Expand Down Expand Up @@ -152,8 +151,8 @@ public boolean onPreferenceClick(Preference preference) {
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.ifNecessary()
.onAllGranted(() -> {
Log.i(TAG, "Queing backup...");
ApplicationDependencies.getJobManager().add(new LocalBackupJob(true));
Log.i(TAG, "Starting backup from user");
LocalBackupJob.enqueue(true);
})
.withPermanentDenialDialog(getString(R.string.ChatsPreferenceFragment_signal_requires_external_storage_permission_in_order_to_create_backups))
.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import androidx.annotation.NonNull;

import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.jobs.LocalBackupJob;
import org.thoughtcrime.securesms.util.TextSecurePreferences;

Expand All @@ -24,7 +23,7 @@ protected long getNextScheduledExecutionTime(Context context) {
@Override
protected long onAlarm(Context context, long scheduledTime) {
if (TextSecurePreferences.isBackupEnabled(context)) {
ApplicationDependencies.getJobManager().add(new LocalBackupJob(false));
LocalBackupJob.enqueue(false);
}

return setNextBackupTimeToIntervalFromNow(context);
Expand Down

0 comments on commit 8de4290

Please sign in to comment.