Search in sources :

Example 1 with RestoreState

use of com.zegoggles.smssync.service.state.RestoreState in project sms-backup-plus by jberkel.

the class RestoreTask method restore.

private RestoreState restore(RestoreConfig config) {
    final BackupImapStore imapStore = config.imapStore;
    int currentRestoredItem = config.currentRestoredItem;
    try {
        publishProgress(LOGIN);
        imapStore.checkSettings();
        publishProgress(CALC);
        final List<Message> msgs = new ArrayList<Message>();
        if (config.restoreSms) {
            msgs.addAll(imapStore.getFolder(SMS, preferences.getDataTypePreferences()).getMessages(config.maxRestore, config.restoreOnlyStarred, null));
        }
        if (config.restoreCallLog) {
            msgs.addAll(imapStore.getFolder(CALLLOG, preferences.getDataTypePreferences()).getMessages(config.maxRestore, config.restoreOnlyStarred, null));
        }
        final int itemsToRestoreCount = config.maxRestore <= 0 ? msgs.size() : Math.min(msgs.size(), config.maxRestore);
        if (itemsToRestoreCount > 0) {
            for (; currentRestoredItem < itemsToRestoreCount && !isCancelled(); currentRestoredItem++) {
                DataType dataType = importMessage(msgs.get(currentRestoredItem));
                // help gc
                msgs.set(currentRestoredItem, null);
                publishProgress(new RestoreState(RESTORE, currentRestoredItem, itemsToRestoreCount, 0, 0, dataType, null));
                if (currentRestoredItem % 50 == 0) {
                    // clear cache periodically otherwise SD card fills up
                    service.clearCache();
                }
            }
            updateAllThreadsIfAnySmsRestored();
        } else {
            Log.d(TAG, "nothing to restore");
        }
        final int restoredCount = smsIds.size() + callLogIds.size();
        return new RestoreState(isCancelled() ? CANCELED_RESTORE : FINISHED_RESTORE, currentRestoredItem, itemsToRestoreCount, restoredCount, Math.max(0, uids.size() - restoredCount), null, null);
    } catch (XOAuth2AuthenticationFailedException e) {
        return handleAuthError(config, currentRestoredItem, e);
    } catch (AuthenticationFailedException e) {
        return transition(SmsSyncState.ERROR, e);
    } catch (MessagingException e) {
        Log.e(TAG, ERROR, e);
        updateAllThreadsIfAnySmsRestored();
        return transition(SmsSyncState.ERROR, e);
    } catch (IllegalStateException e) {
        // usually memory problems (Couldn't init cursor window)
        return transition(SmsSyncState.ERROR, e);
    } finally {
        imapStore.closeFolders();
    }
}
Also used : Message(com.fsck.k9.mail.Message) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) XOAuth2AuthenticationFailedException(com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException) MessagingException(com.fsck.k9.mail.MessagingException) BackupImapStore(com.zegoggles.smssync.mail.BackupImapStore) ArrayList(java.util.ArrayList) RestoreState(com.zegoggles.smssync.service.state.RestoreState) XOAuth2AuthenticationFailedException(com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException) DataType(com.zegoggles.smssync.mail.DataType)

Aggregations

AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)1 Message (com.fsck.k9.mail.Message)1 MessagingException (com.fsck.k9.mail.MessagingException)1 XOAuth2AuthenticationFailedException (com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException)1 BackupImapStore (com.zegoggles.smssync.mail.BackupImapStore)1 DataType (com.zegoggles.smssync.mail.DataType)1 RestoreState (com.zegoggles.smssync.service.state.RestoreState)1 ArrayList (java.util.ArrayList)1