private static PendingIntent createPendingIntent(Context ctx, BackupType backupType) { final Intent intent = (new Intent(ctx, SmsBackupService.class)) .setAction(backupType.name()); return PendingIntent.getService(ctx, 0, intent, FLAG_UPDATE_CURRENT); }
public static BackupType fromName(String name) { for (BackupType type : values()) { if (type.name().equals(name)) { return type; } } return UNKNOWN; }
@Override public int cancelAll() { if (LOCAL_LOGV) { Log.v(TAG, "AlarmManagerDriver: cancelAll"); } cancel(BackupType.REGULAR.name()); return CANCEL_RESULT_SUCCESS; }
public void cancelRegular() { cancel(REGULAR.name()); }
private void startBackup(BackupType backupType) { startService(new Intent(this, SmsBackupService.class).setAction(backupType.name())); }
@Subscribe public void backupStateChanged(BackupState state) { if (!state.isFinished()) { return; } final JobParameters jobParameters = jobs.remove(state.backupType.name()); if (jobParameters != null) { final boolean needsReschedule = state.isError() && !state.isPermissionException(); if (LOCAL_LOGV) { Log.v(TAG, "jobFinished(" + jobParameters + ", isError=" + state.isError() + ", needsReschedule="+needsReschedule+")"); } jobFinished(jobParameters, needsReschedule); } else { Log.w(TAG, "unknown job for state "+state); } }
@Test public void shouldTriggerBackupWithManualIntent() throws Exception { Intent intent = new Intent(MANUAL.name()); service.handleIntent(intent); verify(backupTask).execute(any(BackupConfig.class)); }
@Test public void shouldPassInCorrectBackupConfig() throws Exception { Intent intent = new Intent(MANUAL.name()); ArgumentCaptor<BackupConfig> config = ArgumentCaptor.forClass(BackupConfig.class); service.handleIntent(intent); verify(backupTask).execute(config.capture()); BackupConfig backupConfig = config.getValue(); assertThat(backupConfig.backupType).isEqualTo(MANUAL); assertThat(backupConfig.currentTry).isEqualTo(0); }
@Test public void shouldCheckForValidStore() throws Exception { when(authPreferences.getStoreUri()).thenReturn("invalid"); Intent intent = new Intent(MANUAL.name()); service.handleIntent(intent); verifyZeroInteractions(backupTask); assertThat(service.getState().exception).isInstanceOf(MessagingException.class); }
private @NonNull Job.Builder createBuilder(BackupType backupType) { return firebaseJobDispatcher.newJobBuilder() .setReplaceCurrent(true) .setService(SmsJobService.class) .setTag(backupType.name()) .setRetryStrategy(defaultRetryStrategy()) .setConstraints(jobConstraints(backupType)); }
@Test public void shouldNotCheckForConnectivityBeforeBackingUpWithNewScheduler() throws Exception { when(preferences.isUseOldScheduler()).thenReturn(false); Intent intent = new Intent(REGULAR.name()); shadowConnectivityManager.setActiveNetworkInfo(null); shadowConnectivityManager.setBackgroundDataSetting(true); service.handleIntent(intent); verify(backupTask).execute(any(BackupConfig.class)); }
@Test public void shouldCheckForConnectivityBeforeBackingUp() throws Exception { Intent intent = new Intent(MANUAL.name()); shadowConnectivityManager.setActiveNetworkInfo(null); service.handleIntent(intent); verifyZeroInteractions(backupTask); assertThat(service.getState().exception).isInstanceOf(NoConnectionException.class); }
@Test public void shouldNotifyUserAboutErrorInManualMode() throws Exception { when(authPreferences.getStoreUri()).thenReturn("invalid"); Intent intent = new Intent(MANUAL.name()); service.handleIntent(intent); verifyZeroInteractions(backupTask); assertNotificationShown("SMSBackup+ error", "No valid IMAP URI: invalid"); assertThat(shadowOf(service).isStoppedBySelf()).isTrue(); assertThat(shadowOf(service).isForegroundStopped()).isTrue(); }
@Test public void shouldScheduleNextRegularBackupAfterFinished() throws Exception { shadowConnectivityManager.setBackgroundDataSetting(true); Intent intent = new Intent(REGULAR.name()); service.handleIntent(intent); verify(backupTask).execute(any(BackupConfig.class)); service.backupStateChanged(service.transition(SmsSyncState.FINISHED_BACKUP, null)); verify(backupJobs).scheduleRegular(); assertThat(shadowOf(service).isStoppedBySelf()).isTrue(); assertThat(shadowOf(service).isForegroundStopped()).isTrue(); }