Skip to content

Commit af06294

Browse files
committed
Schedule/cancel jobs on boot
1 parent 0d67e4b commit af06294

File tree

3 files changed

+30
-18
lines changed

3 files changed

+30
-18
lines changed

app/src/main/java/com/zegoggles/smssync/service/AlarmManagerDriver.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import android.app.PendingIntent;
2020
import android.content.Context;
2121
import android.content.Intent;
22+
import android.support.annotation.NonNull;
2223
import android.util.Log;
2324
import com.firebase.jobdispatcher.Driver;
2425
import com.firebase.jobdispatcher.Job;
@@ -51,7 +52,7 @@ class AlarmManagerDriver implements Driver, JobValidator {
5152
}
5253

5354
@Override
54-
public int schedule(Job job) {
55+
public int schedule(@NonNull Job job) {
5556
if (LOCAL_LOGV) {
5657
Log.v(TAG, "AlarmManagerDriver: schedule " +job);
5758
}
@@ -70,7 +71,7 @@ public int schedule(Job job) {
7071
}
7172

7273
@Override
73-
public int cancel(String tag) {
74+
public int cancel(@NonNull String tag) {
7475
if (LOCAL_LOGV) {
7576
Log.v(TAG, "AlarmManagerDriver: cancel " +tag);
7677
}
@@ -90,7 +91,7 @@ public int cancelAll() {
9091
}
9192

9293
@Override
93-
public JobValidator getValidator() {
94+
public @NonNull JobValidator getValidator() {
9495
return this;
9596
}
9697

app/src/main/java/com/zegoggles/smssync/service/BackupJobs.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,42 +52,46 @@ public class BackupJobs {
5252
private static final int BOOT_BACKUP_DELAY = 60;
5353
private static final String CONTENT_TRIGGER_TAG = "contentTrigger";
5454

55-
private final Preferences mPreferences;
55+
private final Preferences preferences;
5656
private FirebaseJobDispatcher firebaseJobDispatcher;
5757

5858
public BackupJobs(Context context) {
5959
this(context, new Preferences(context));
6060
}
6161

6262
BackupJobs(Context context, Preferences preferences) {
63-
mPreferences = preferences;
63+
this.preferences = preferences;
6464
firebaseJobDispatcher = new FirebaseJobDispatcher(
65-
mPreferences.isUseOldScheduler() ?
65+
this.preferences.isUseOldScheduler() ?
6666
new AlarmManagerDriver(context) :
6767
new GooglePlayDriver(context));
6868
}
6969

7070
public Job scheduleIncoming() {
71-
return schedule(mPreferences.getIncomingTimeoutSecs(), INCOMING, false);
71+
return schedule(preferences.getIncomingTimeoutSecs(), INCOMING, false);
7272
}
7373

7474
public Job scheduleRegular() {
75-
return schedule(mPreferences.getRegularTimeoutSecs(), REGULAR, false);
75+
return schedule(preferences.getRegularTimeoutSecs(), REGULAR, false);
7676
}
7777

7878
public Job scheduleContentTriggerJob() {
7979
return schedule(createContentUriTriggerJob());
8080
}
8181

8282
public Job scheduleBootup() {
83-
if (mPreferences.isUseOldScheduler()) {
83+
if (!preferences.isEnableAutoSync()) {
84+
Log.d(TAG, "auto backup no longer enabled, canceling all jobs");
85+
cancelAll();
86+
return null;
87+
} else if (preferences.isUseOldScheduler()) {
8488
return schedule(BOOT_BACKUP_DELAY, REGULAR, false);
8589
} else {
8690
if (LOCAL_LOGV) {
8791
Log.v(TAG, "not scheduling bootup backup (using new scheduler)");
8892
}
89-
// assume jobs are persistent
90-
return null;
93+
// assume regular jobs are persistent
94+
return scheduleContentTriggerJob();
9195
}
9296
}
9397

@@ -124,7 +128,7 @@ private void cancel(String tag) {
124128
Log.v(TAG, "scheduleBackup(" + inSeconds + ", " + backupType + ", " + force + ")");
125129
}
126130

127-
if (force || (mPreferences.isEnableAutoSync() && inSeconds > 0)) {
131+
if (force || (preferences.isEnableAutoSync() && inSeconds > 0)) {
128132
final Job job = createJob(inSeconds, backupType);
129133
if (schedule(job) != null) {
130134
if (LOCAL_LOGV) {
@@ -152,15 +156,15 @@ private Job schedule(Job job) {
152156
}
153157
}
154158

155-
@NonNull private Job createJob(int inSeconds, BackupType backupType) {
159+
private @NonNull Job createJob(int inSeconds, BackupType backupType) {
156160
return createBuilder(backupType)
157161
.setTrigger(inSeconds <= 0 ? NOW : Trigger.executionWindow(inSeconds, inSeconds))
158162
.setRecurring(backupType.isRecurring())
159163
.setLifetime(backupType.isRecurring() ? FOREVER : UNTIL_NEXT_BOOT)
160164
.build();
161165
}
162166

163-
@NonNull private Job createContentUriTriggerJob() {
167+
private @NonNull Job createContentUriTriggerJob() {
164168
final ObservedUri observedUri = new ObservedUri(SMS_PROVIDER, FLAG_NOTIFY_FOR_DESCENDANTS);
165169
final JobTrigger trigger = Trigger.contentUriTrigger(Collections.singletonList(observedUri));
166170
return createBuilder(INCOMING)
@@ -171,15 +175,15 @@ private Job schedule(Job job) {
171175
.build();
172176
}
173177

174-
private Job.Builder createBuilder(BackupType backupType) {
178+
private @NonNull Job.Builder createBuilder(BackupType backupType) {
175179
final Bundle extras = new Bundle();
176180
extras.putString(BackupType.EXTRA, backupType.name());
177181
return firebaseJobDispatcher.newJobBuilder()
178182
.setReplaceCurrent(true)
179183
.setService(SmsJobService.class)
180184
.setExtras(extras)
181185
.setTag(backupType.name())
182-
.setConstraints(mPreferences.isWifiOnly() ? ON_UNMETERED_NETWORK : ON_ANY_NETWORK)
186+
.setConstraints(preferences.isWifiOnly() ? ON_UNMETERED_NETWORK : ON_ANY_NETWORK)
183187
.setRetryStrategy(DEFAULT_EXPONENTIAL);
184188
}
185189
}

app/src/test/java/com/zegoggles/smssync/service/BackupJobsTest.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,24 @@ public class BackupJobsTest {
6060
assertThat(job.getTrigger()).isInstanceOf(JobTrigger.ContentUriTrigger.class);
6161
}
6262

63-
@Test public void shouldScheduleBootForOldScheduler() throws Exception {
63+
@Test public void shouldScheduleRegularJobAfterBootForOldScheduler() throws Exception {
6464
when(preferences.isEnableAutoSync()).thenReturn(true);
6565
when(preferences.isUseOldScheduler()).thenReturn(true);
6666
Job job = subject.scheduleBootup();
6767
verifyJobScheduled(job, 60, "REGULAR");
6868
}
6969

70-
@Test public void shouldNotScheduleBootForNewScheduler() throws Exception {
70+
@Test public void shouldScheduleContentTriggerJobAfterBootForNewScheduler() throws Exception {
7171
when(preferences.isEnableAutoSync()).thenReturn(true);
7272
when(preferences.isUseOldScheduler()).thenReturn(false);
7373
Job job = subject.scheduleBootup();
74+
assertThat(job).isNotNull();
75+
assertThat(job.getTrigger()).isInstanceOf(JobTrigger.ContentUriTrigger.class);
76+
}
77+
78+
@Test public void shouldCancelAllJobsAfterBootIfAutoBackupDisabled() throws Exception {
79+
when(preferences.isEnableAutoSync()).thenReturn(false);
80+
Job job = subject.scheduleBootup();
7481
assertThat(job).isNull();
7582
}
7683

0 commit comments

Comments
 (0)