@Override public BackupState transition(SmsSyncState newState, Exception exception) { return new BackupState(newState, currentSyncedItems, itemsToSync, backupType, dataType, exception); }
public boolean isFinished() { return !isInitialState() && !isRunning(); }
@Override public RestoreState transition(SmsSyncState newState, Exception exception) { return new RestoreState(newState, currentRestoredCount, itemsToRestore, actualRestoredCount, duplicateCount, dataType, exception); }
@Subscribe public void backupStateChanged(BackupState state) { if (this.state == state) return; this.state = state; if (this.state.isInitialState()) return; if (state.isError()) { handleErrorState(state); } if (state.isRunning()) { if (state.backupType == MANUAL) { notifyAboutBackup(state); } } else { appLogDebug(state.toString()); appLog(state.isCanceled() ? R.string.app_log_backup_canceled : R.string.app_log_backup_finished); scheduleNextBackup(state); stopForeground(true); stopSelf(); } }
@Test public void shouldGetNotificationLabelCalculating() throws Exception { assertThat(new BackupState(SmsSyncState.CALC, 0, 0, BackupType.REGULAR, DataType.SMS, null).getNotificationLabel(resources)).isEqualTo( resources.getString(R.string.status_calc_details) ); }
@Test public void shouldCheckError() throws Exception { assertThat(new BackupState(SmsSyncState.ERROR, 0, 0, BackupType.REGULAR, DataType.SMS, new IOException("foo") ).isError()).isTrue(); assertThat(new BackupState(SmsSyncState.FINISHED_BACKUP, 0, 0, BackupType.REGULAR, DataType.SMS, null ).isError()).isFalse(); }
@Test public void shouldGetErrorMessagePrefix() throws Exception { BackupState state = new BackupState(SmsSyncState.ERROR, 0, 0, BackupType.REGULAR, DataType.SMS, new MessagingException("Unable to get IMAP prefix")); assertThat(state.getErrorMessage(resources)).isEqualTo("Temporary Gmail IMAP error, try again later."); }
@Subscribe public void backupStateChanged(final BackupState newState) { if ((newState.backupType == MANUAL || newState.backupType == SKIP) && newState.isPermissionException()) { ActivityCompat.requestPermissions(this, newState.getMissingPermissions(), newState.backupType == SKIP ? REQUEST_PERMISSIONS_BACKUP_MANUAL_SKIP : REQUEST_PERMISSIONS_BACKUP_MANUAL ); } }
private void notifyAboutBackup(BackupState state) { NotificationCompat.Builder builder = createNotification(R.string.status_backup); notification = builder.setContentTitle(getString(R.string.status_backup)) .setContentText(state.getNotificationLabel(getResources())) .setContentIntent(getPendingIntent(null)) .build(); startForeground(BACKUP_ID, notification); }
private RestoreState transition(SmsSyncState smsSyncState, Exception exception) { return service.getState().transition(smsSyncState, exception); }
@Override public String getNotificationLabel(Resources resources) { String label = super.getNotificationLabel(resources); if (label != null) return label; if (state == BACKUP) { label = resources.getString(R.string.status_backup_details, currentSyncedItems, itemsToSync); if (dataType != null) { label += " ("+resources.getString(dataType.resId)+")"; } return label; } else { return ""; } } }
public BackupState transition(SmsSyncState newState, Exception e) { return state.transition(newState, e); } }
public String getNotificationLabel(Resources resources) { switch (state) { case LOGIN: return resources.getString(R.string.status_login_details); case CALC: return resources.getString(R.string.status_calc_details); case ERROR: return getErrorMessage(resources); default: return null; } }
public String[] getMissingPermissions() { if (isPermissionException()) { MissingPermissionException mpe = (MissingPermissionException)exception; return mpe.permissions.toArray(new String[mpe.permissions.size()]); } else { return new String[0]; } } }
public boolean isWorking() { return getState().isRunning(); }
private boolean shouldNotifyUser(BackupState state) { return state.backupType == MANUAL || (getPreferences().isNotificationEnabled() && !state.isConnectivityError()); }
@Subscribe public void restoreStateChanged(final RestoreState newState) { if (newState.isFinished() && isSmsBackupDefaultSmsApp(this)) { restoreDefaultSmsProvider(preferences.getSmsDefaultPackage()); } }
@Test public void shouldGetNotificationLabelError() throws Exception { assertThat(new BackupState(SmsSyncState.ERROR, 0, 0, BackupType.REGULAR, DataType.SMS, new IOException("foo")).getNotificationLabel(resources)) .isEqualTo("foo"); } }
private void postError(Exception exception) { App.post(state.transition(ERROR, exception)); }
@Override public String getNotificationLabel(Resources resources) { String label = super.getNotificationLabel(resources); if (label != null) return label; switch (state) { case RESTORE: label = resources.getString(R.string.status_restore_details, currentRestoredCount, itemsToRestore); if (dataType != null) { label += " ("+resources.getString(dataType.resId)+")"; } return label; case UPDATING_THREADS: return resources.getString(R.string.status_updating_threads); default: return ""; } } }