Skip to content

Commit

Permalink
Persistent jobs, retry failed, skip connectivity check
Browse files Browse the repository at this point in the history
  • Loading branch information
jberkel committed Nov 12, 2017
1 parent 50e4697 commit b07a6d9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 10 deletions.
17 changes: 13 additions & 4 deletions app/src/main/java/com/zegoggles/smssync/service/BackupJobs.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@
import com.firebase.jobdispatcher.GooglePlayDriver;
import com.firebase.jobdispatcher.Job;
import com.firebase.jobdispatcher.JobTrigger;
import com.firebase.jobdispatcher.Lifetime;
import com.firebase.jobdispatcher.RetryStrategy;
import com.firebase.jobdispatcher.Trigger;
import com.zegoggles.smssync.compat.GooglePlayServices;
import com.zegoggles.smssync.preferences.Preferences;

import static com.firebase.jobdispatcher.FirebaseJobDispatcher.CANCEL_RESULT_SUCCESS;
import static com.firebase.jobdispatcher.FirebaseJobDispatcher.SCHEDULE_RESULT_SUCCESS;
import static com.firebase.jobdispatcher.Lifetime.FOREVER;
import static com.firebase.jobdispatcher.Lifetime.UNTIL_NEXT_BOOT;
import static com.zegoggles.smssync.App.LOCAL_LOGV;
import static com.zegoggles.smssync.App.TAG;
import static com.zegoggles.smssync.service.BackupType.BROADCAST_INTENT;
Expand Down Expand Up @@ -71,7 +73,12 @@ public Job scheduleRegular() {
}

public Job scheduleBootup() {
return schedule(BOOT_BACKUP_DELAY, REGULAR, false);
if (mPreferences.isUseOldScheduler()) {
return schedule(BOOT_BACKUP_DELAY, REGULAR, false);
} else {
// assume jobs are persistent
return null;
}
}

public Job scheduleImmediate() {
Expand Down Expand Up @@ -119,10 +126,12 @@ public void cancel() {
final Bundle extras = new Bundle();
extras.putString(BackupType.EXTRA, backupType.name());
return builder
.setReplaceCurrent(false)
.setReplaceCurrent(true)
.setTrigger(trigger)
.setRecurring(backupType == REGULAR)
.setConstraints(constraint)
.setLifetime(Lifetime.UNTIL_NEXT_BOOT)
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
.setLifetime(backupType == REGULAR ? FOREVER : UNTIL_NEXT_BOOT)
.setTag(backupType.name())
.setExtras(extras)
.setService(SmsJobService.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private void backup(BackupType backupType, boolean skip) {
if (!skip) {
checkCredentials();
checkBackgroundDataSettings(backupType);
checkConnectivity();
checkConnectivity(backupType);
}

appLog(R.string.app_log_start_backup, backupType);
Expand Down Expand Up @@ -172,7 +172,12 @@ private void checkBackgroundDataSettings(BackupType backupType) throws RequiresB
}
}

private void checkConnectivity() throws ConnectivityException {
private void checkConnectivity(BackupType backupType) throws ConnectivityException {
if (backupType != MANUAL && !getPreferences().isUseOldScheduler()) {
Log.d(TAG, "skipping connectivity check, running with new scheduler");
return;
}

NetworkInfo active = getConnectivityManager().getActiveNetworkInfo();
if (active == null || !active.isConnectedOrConnecting()) {
throw new NoConnectionException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public void backupStateChanged(BackupState state) {
final JobParameters jobParameters = jobs.remove(state.backupType.name());
if (jobParameters != null) {
Log.v(TAG, "jobFinished("+jobParameters+")");
jobFinished(jobParameters, false);
jobFinished(jobParameters, state.isError());
} else {
Log.w(TAG, "unknown job for state "+state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,20 @@ public class BackupJobsTest {
verifyJobScheduled(job, 2000, "REGULAR");
}

@Test public void shouldScheduleBoot() throws Exception {
@Test public void shouldScheduleBootForOldScheduler() throws Exception {
when(preferences.isEnableAutoSync()).thenReturn(true);
when(preferences.isUseOldScheduler()).thenReturn(true);
Job job = subject.scheduleBootup();
verifyJobScheduled(job, 60, "REGULAR");
}

@Test public void shouldNotScheduleBootForNewScheduler() throws Exception {
when(preferences.isEnableAutoSync()).thenReturn(true);
when(preferences.isUseOldScheduler()).thenReturn(false);
Job job = subject.scheduleBootup();
assertThat(job).isNull();
}

@Test public void shouldScheduleIncoming() throws Exception {
when(preferences.isEnableAutoSync()).thenReturn(true);
when(preferences.getIncomingTimeoutSecs()).thenReturn(2000);
Expand All @@ -78,8 +86,8 @@ public class BackupJobsTest {
assertThat(subject.scheduleIncoming()).isEqualTo(null);
}

private void verifyJobScheduled(Job job, int scheduled, String expectedType)
{
private void verifyJobScheduled(Job job, int scheduled, String expectedType) {
assertThat(job).isNotNull();
if (scheduled <= 0) {
assertThat(job.getTrigger() instanceof JobTrigger.ImmediateTrigger);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private UserNotification(String title, String text) {
when(authPreferences.getStoreUri()).thenReturn("imap+ssl+://xoauth:[email protected]:993");
when(authPreferences.isLoginInformationSet()).thenReturn(true);
when(preferences.getBackupContactGroup()).thenReturn(ContactGroup.EVERYBODY);
when(preferences.isUseOldScheduler()).thenReturn(true);
}

@After public void after() {
Expand Down Expand Up @@ -120,6 +121,17 @@ private UserNotification(String title, String text) {
assertThat(service.getState().exception).isExactlyInstanceOf(NoConnectionException.class);
}

@Test public void shouldNotCheckForConnectivityBeforeBackingUpWithNewScheduler() throws Exception {
when(preferences.isUseOldScheduler()).thenReturn(false);

Intent intent = new Intent();
intent.putExtra(BackupType.EXTRA, BackupType.REGULAR.name());
shadowConnectivityManager.setActiveNetworkInfo(null);
shadowConnectivityManager.setBackgroundDataSetting(true);
service.handleIntent(intent);
verify(backupTask).execute(any(BackupConfig.class));
}

@Test public void shouldCheckForWifiConnectivity() throws Exception {
Intent intent = new Intent();
when(preferences.isWifiOnly()).thenReturn(true);
Expand Down

0 comments on commit b07a6d9

Please sign in to comment.