private void appLog(int id, Object... args) { service.appLog(id, args); }
@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(); } }
@Override protected void handleIntent(final Intent intent) { if (intent == null) return; // NB: should not happen with START_NOT_STICKY final BackupType backupType = BackupType.fromIntent(intent); if (LOCAL_LOGV) { Log.v(TAG, "handleIntent(" + intent + ", " + (intent.getExtras() == null ? "null" : intent.getExtras().keySet()) + ", " + intent.getAction() + ", type="+backupType+")"); } appLog(R.string.app_log_backup_requested, getString(backupType.resId)); // Only start a backup if there's no other operation going on at this time. if (!isWorking() && SmsRestoreService.isServiceIdle()) { backup(backupType); } else { appLog(R.string.app_log_skip_backup_already_running); } }
private void scheduleNextBackup(BackupState state) { if (state.backupType == REGULAR && getPreferences().isUseOldScheduler()) { final Job nextSync = getBackupJobs().scheduleRegular(); if (nextSync != null) { JobTrigger.ExecutionWindowTrigger trigger = (JobTrigger.ExecutionWindowTrigger) nextSync.getTrigger(); Date date = new Date(System.currentTimeMillis() + (trigger.getWindowStart() * 1000)); appLog(R.string.app_log_scheduled_next_sync, DateFormat.format("kk:mm", date)); } else { appLog(R.string.app_log_no_next_sync); } } // else job already persisted }
private void handleErrorState(BackupState state) { if (state.isAuthException()) { appLog(R.string.app_log_backup_failed_authentication, state.getDetailedErrorMessage(getResources())); if (shouldNotifyUser(state)) { notifyUser(NOTIFICATION_ID_WARNING, notificationBuilder(stat_sys_warning, getString(R.string.notification_auth_failure), getString(getAuthPreferences().useXOAuth() ? R.string.status_auth_failure_details_xoauth : R.string.status_auth_failure_details_plain))); } } else if (state.isConnectivityError()) { appLog(R.string.app_log_backup_failed_connectivity, state.getDetailedErrorMessage(getResources())); } else if (state.isPermissionException()) { if (state.backupType != MANUAL) { Bundle extras = new Bundle(); extras.putStringArray(MainActivity.EXTRA_PERMISSIONS, state.getMissingPermissions()); notifyUser(NOTIFICATION_ID_WARNING, notificationBuilder(R.drawable.ic_notification, getString(R.string.notification_missing_permission), formatMissingPermissionDetails(getResources(), state.getMissingPermissions())) .setContentIntent(getPendingIntent(extras))); } } else { appLog(R.string.app_log_backup_failed_general_error, state.getDetailedErrorMessage(getResources())); if (shouldNotifyUser(state)) { notifyUser(NOTIFICATION_ID_WARNING, notificationBuilder(stat_sys_warning, getString(R.string.notification_general_error), state.getErrorMessage(getResources()))); } } }
private void backup(BackupType backupType) { getNotifier().cancel(NOTIFICATION_ID_WARNING); try { // set initial state state = new BackupState(INITIAL, 0, 0, backupType, null, null); EnumSet<DataType> enabledTypes = getEnabledBackupTypes(); checkPermissions(enabledTypes); if (backupType != SKIP) { checkCredentials(); if (getPreferences().isUseOldScheduler()) { legacyCheckConnectivity(); } } appLog(R.string.app_log_start_backup, backupType); getBackupTask().execute(getBackupConfig(backupType, enabledTypes, getBackupImapStore())); } catch (MessagingException e) { Log.w(TAG, e); moveToState(state.transition(ERROR, e)); } catch (ConnectivityException e) { moveToState(state.transition(ERROR, e)); } catch (RequiresLoginException e) { appLog(R.string.app_log_missing_credentials); moveToState(state.transition(ERROR, e)); } catch (BackupDisabledException e) { moveToState(state.transition(FINISHED_BACKUP, e)); } catch (MissingPermissionException e) { moveToState(state.transition(ERROR, e)); } }