Search in sources :

Example 1 with XOAuth2AuthenticationFailedException

use of com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException in project sms-backup-plus by jberkel.

the class BackupTask method fetchAndBackupItems.

private BackupState fetchAndBackupItems(BackupConfig config) {
    BackupCursors cursors = null;
    try {
        final ContactGroupIds groupIds = contactAccessor.getGroupContactIds(service.getContentResolver(), config.groupToBackup);
        cursors = new BulkFetcher(fetcher).fetch(config.typesToBackup, groupIds, config.maxItemsPerSync);
        final int itemsToSync = cursors.count();
        if (itemsToSync > 0) {
            appLog(R.string.app_log_backup_messages, cursors.count(SMS), cursors.count(MMS), cursors.count(CALLLOG));
            if (config.debug) {
                appLog(R.string.app_log_backup_messages_with_config, config);
            }
            return backupCursors(cursors, config.imapStore, config.backupType, itemsToSync);
        } else {
            appLog(R.string.app_log_skip_backup_no_items);
            if (preferences.isFirstBackup()) {
                // If this is the first backup we need to write something to MAX_SYNCED_DATE
                // such that we know that we've performed a backup before.
                preferences.getDataTypePreferences().setMaxSyncedDate(SMS, MAX_SYNCED_DATE);
                preferences.getDataTypePreferences().setMaxSyncedDate(MMS, MAX_SYNCED_DATE);
            }
            Log.i(TAG, "Nothing to do.");
            return transition(FINISHED_BACKUP, null);
        }
    } catch (XOAuth2AuthenticationFailedException e) {
        return handleAuthError(config, e);
    } catch (AuthenticationFailedException e) {
        return transition(ERROR, e);
    } catch (MessagingException e) {
        return transition(ERROR, e);
    } catch (SecurityException e) {
        return transition(ERROR, e);
    } finally {
        if (cursors != null) {
            cursors.close();
        }
    }
}
Also used : ContactGroupIds(com.zegoggles.smssync.contacts.ContactGroupIds) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) XOAuth2AuthenticationFailedException(com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException) MessagingException(com.fsck.k9.mail.MessagingException) XOAuth2AuthenticationFailedException(com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException)

Example 2 with XOAuth2AuthenticationFailedException

use of com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException 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)

Example 3 with XOAuth2AuthenticationFailedException

use of com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException in project sms-backup-plus by jberkel.

the class BackupTaskTest method shouldHandleAuthErrorAndTokenCouldBeRefreshed.

@Test
public void shouldHandleAuthErrorAndTokenCouldBeRefreshed() throws Exception {
    mockFetch(SMS, 1);
    when(converter.convertMessages(any(Cursor.class), notNull(DataType.class))).thenReturn(result(SMS, 1));
    XOAuth2AuthenticationFailedException exception = mock(XOAuth2AuthenticationFailedException.class);
    when(exception.getStatus()).thenReturn(400);
    when(store.getFolder(notNull(DataType.class), same(dataTypePreferences))).thenThrow(exception);
    when(service.getBackupImapStore()).thenReturn(store);
    task.doInBackground(config);
    verify(tokenRefresher).refreshOAuth2Token();
    verify(service, times(2)).transition(SmsSyncState.LOGIN, null);
    verify(service, times(2)).transition(SmsSyncState.CALC, null);
    verify(service).transition(SmsSyncState.ERROR, exception);
    // make sure locks only get acquired+released once
    verify(service).acquireLocks();
    verify(service).releaseLocks();
}
Also used : XOAuth2AuthenticationFailedException(com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException) DataType(com.zegoggles.smssync.mail.DataType) BackupItemsFetcher.emptyCursor(com.zegoggles.smssync.service.BackupItemsFetcher.emptyCursor) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) Test(org.junit.Test)

Example 4 with XOAuth2AuthenticationFailedException

use of com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException in project sms-backup-plus by jberkel.

the class BackupTaskTest method shouldHandleAuthErrorAndTokenCannotBeRefreshed.

@Test
public void shouldHandleAuthErrorAndTokenCannotBeRefreshed() throws Exception {
    mockFetch(SMS, 1);
    when(converter.convertMessages(any(Cursor.class), notNull(DataType.class))).thenReturn(result(SMS, 1));
    XOAuth2AuthenticationFailedException exception = mock(XOAuth2AuthenticationFailedException.class);
    when(exception.getStatus()).thenReturn(400);
    when(store.getFolder(notNull(DataType.class), same(dataTypePreferences))).thenThrow(exception);
    doThrow(new TokenRefreshException("failed")).when(tokenRefresher).refreshOAuth2Token();
    task.doInBackground(config);
    verify(tokenRefresher, times(1)).refreshOAuth2Token();
    verify(service).transition(SmsSyncState.ERROR, exception);
    // make sure locks only get acquired+released once
    verify(service).acquireLocks();
    verify(service).releaseLocks();
}
Also used : TokenRefreshException(com.zegoggles.smssync.auth.TokenRefreshException) XOAuth2AuthenticationFailedException(com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException) DataType(com.zegoggles.smssync.mail.DataType) BackupItemsFetcher.emptyCursor(com.zegoggles.smssync.service.BackupItemsFetcher.emptyCursor) Cursor(android.database.Cursor) MatrixCursor(android.database.MatrixCursor) Test(org.junit.Test)

Aggregations

XOAuth2AuthenticationFailedException (com.fsck.k9.mail.store.imap.XOAuth2AuthenticationFailedException)4 DataType (com.zegoggles.smssync.mail.DataType)3 Cursor (android.database.Cursor)2 MatrixCursor (android.database.MatrixCursor)2 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)2 MessagingException (com.fsck.k9.mail.MessagingException)2 BackupItemsFetcher.emptyCursor (com.zegoggles.smssync.service.BackupItemsFetcher.emptyCursor)2 Test (org.junit.Test)2 Message (com.fsck.k9.mail.Message)1 TokenRefreshException (com.zegoggles.smssync.auth.TokenRefreshException)1 ContactGroupIds (com.zegoggles.smssync.contacts.ContactGroupIds)1 BackupImapStore (com.zegoggles.smssync.mail.BackupImapStore)1 RestoreState (com.zegoggles.smssync.service.state.RestoreState)1 ArrayList (java.util.ArrayList)1