Search in sources :

Example 71 with MessagingException

use of com.fsck.k9.mail.MessagingException in project sms-backup-plus by jberkel.

the class MmsSupport method getMMSBodyParts.

public List<BodyPart> getMMSBodyParts(final Uri uriPart) throws MessagingException {
    final List<BodyPart> parts = new ArrayList<BodyPart>();
    Cursor curPart = resolver.query(uriPart, null, null, null, null);
    // _id, mid, seq, ct, name, chset, cd, fn, cid, cl, ctt_s, ctt_t, _data, text
    while (curPart != null && curPart.moveToNext()) {
        final String id = curPart.getString(curPart.getColumnIndex("_id"));
        final String contentType = curPart.getString(curPart.getColumnIndex("ct"));
        final String fileName = curPart.getString(curPart.getColumnIndex("cl"));
        final String text = curPart.getString(curPart.getColumnIndex("text"));
        if (LOCAL_LOGV) {
            Log.v(TAG, String.format(Locale.ENGLISH, "processing part %s, name=%s (%s)", id, fileName, contentType));
        }
        if (!TextUtils.isEmpty(contentType) && contentType.startsWith("text/") && !TextUtils.isEmpty(text)) {
            // text
            parts.add(new MimeBodyPart(new TextBody(text), contentType));
        } else // noinspection StatementWithEmptyBody
        if ("application/smil".equalsIgnoreCase(contentType)) {
        // silently ignore SMIL stuff
        } else {
            // attach everything else
            final Uri partUri = Uri.withAppendedPath(Consts.MMS_PROVIDER, MMS_PART + "/" + id);
            parts.add(Attachment.createPartFromUri(resolver, partUri, fileName, contentType));
        }
    }
    if (curPart != null)
        curPart.close();
    return parts;
}
Also used : TextBody(com.fsck.k9.mail.internet.TextBody) BodyPart(com.fsck.k9.mail.BodyPart) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) ArrayList(java.util.ArrayList) Cursor(android.database.Cursor) MimeBodyPart(com.fsck.k9.mail.internet.MimeBodyPart) Uri(android.net.Uri)

Example 72 with MessagingException

use of com.fsck.k9.mail.MessagingException 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 73 with MessagingException

use of com.fsck.k9.mail.MessagingException in project sms-backup-plus by jberkel.

the class StateTest method shouldGetErrorMessagePrefix.

@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.");
}
Also used : MessagingException(com.fsck.k9.mail.MessagingException) Test(org.junit.Test)

Example 74 with MessagingException

use of com.fsck.k9.mail.MessagingException 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 75 with MessagingException

use of com.fsck.k9.mail.MessagingException in project sms-backup-plus by jberkel.

the class SmsBackupService method backup.

private void backup(BackupType backupType) {
    getNotifier().cancel(NOTIFICATION_ID_WARNING);
    try {
        // set initial state
        mState = 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(mState.transition(ERROR, e));
    } catch (ConnectivityException e) {
        moveToState(mState.transition(ERROR, e));
    } catch (RequiresLoginException e) {
        appLog(R.string.app_log_missing_credentials);
        moveToState(mState.transition(ERROR, e));
    } catch (BackupDisabledException e) {
        moveToState(mState.transition(FINISHED_BACKUP, e));
    } catch (MissingPermissionException e) {
        moveToState(mState.transition(ERROR, e));
    }
}
Also used : BackupState(com.zegoggles.smssync.service.state.BackupState) MissingPermissionException(com.zegoggles.smssync.service.exception.MissingPermissionException) MessagingException(com.fsck.k9.mail.MessagingException) RequiresLoginException(com.zegoggles.smssync.service.exception.RequiresLoginException) BackupDisabledException(com.zegoggles.smssync.service.exception.BackupDisabledException) DataType(com.zegoggles.smssync.mail.DataType) ConnectivityException(com.zegoggles.smssync.service.exception.ConnectivityException)

Aggregations

MessagingException (com.fsck.k9.mail.MessagingException)159 Test (org.junit.Test)73 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)52 LocalFolder (com.fsck.k9.mailstore.LocalFolder)49 LocalStore (com.fsck.k9.mailstore.LocalStore)49 ArrayList (java.util.ArrayList)49 Message (com.fsck.k9.mail.Message)44 LocalMessage (com.fsck.k9.mailstore.LocalMessage)42 IOException (java.io.IOException)42 FetchProfile (com.fsck.k9.mail.FetchProfile)30 MimeBodyPart (com.fsck.k9.mail.internet.MimeBodyPart)28 ByteArrayOutputStream (java.io.ByteArrayOutputStream)27 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)26 BodyPart (com.fsck.k9.mail.BodyPart)23 Part (com.fsck.k9.mail.Part)22 Account (com.fsck.k9.Account)21 Body (com.fsck.k9.mail.Body)21 TextBody (com.fsck.k9.mail.internet.TextBody)21 Date (java.util.Date)20 MimeMultipart (com.fsck.k9.mail.internet.MimeMultipart)18