Search in sources :

Example 16 with MessagingListener

use of com.fsck.k9.controller.MessagingListener in project k-9 by k9mail.

the class MessagingController method listFoldersSynchronous.

/**
     * Lists folders that are available locally and remotely. This method calls
     * listFoldersCallback for local folders before it returns, and then for
     * remote folders at some later point. If there are no local folders
     * includeRemote is forced by this method. This method is called in the
     * foreground.
     * TODO this needs to cache the remote folder list
     */
public void listFoldersSynchronous(final Account account, final boolean refreshRemote, final MessagingListener listener) {
    for (MessagingListener l : getListeners(listener)) {
        l.listFoldersStarted(account);
    }
    List<LocalFolder> localFolders = null;
    if (!account.isAvailable(context)) {
        Timber.i("not listing folders of unavailable account");
    } else {
        try {
            LocalStore localStore = account.getLocalStore();
            localFolders = localStore.getPersonalNamespaces(false);
            if (refreshRemote || localFolders.isEmpty()) {
                doRefreshRemote(account, listener);
                return;
            }
            for (MessagingListener l : getListeners(listener)) {
                l.listFolders(account, localFolders);
            }
        } catch (Exception e) {
            for (MessagingListener l : getListeners(listener)) {
                l.listFoldersFailed(account, e.getMessage());
            }
            addErrorMessage(account, null, e);
            return;
        } finally {
            if (localFolders != null) {
                for (Folder localFolder : localFolders) {
                    closeFolder(localFolder);
                }
            }
        }
    }
    for (MessagingListener l : getListeners(listener)) {
        l.listFoldersFinished(account);
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) LocalStore(com.fsck.k9.mailstore.LocalStore) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException)

Example 17 with MessagingListener

use of com.fsck.k9.controller.MessagingListener in project k-9 by k9mail.

the class MessagingController method refreshRemoteSynchronous.

@VisibleForTesting
void refreshRemoteSynchronous(final Account account, final MessagingListener listener) {
    List<LocalFolder> localFolders = null;
    try {
        Store store = account.getRemoteStore();
        List<? extends Folder> remoteFolders = store.getPersonalNamespaces(false);
        LocalStore localStore = account.getLocalStore();
        Set<String> remoteFolderNames = new HashSet<>();
        List<LocalFolder> foldersToCreate = new LinkedList<>();
        localFolders = localStore.getPersonalNamespaces(false);
        Set<String> localFolderNames = new HashSet<>();
        for (Folder localFolder : localFolders) {
            localFolderNames.add(localFolder.getName());
        }
        for (Folder remoteFolder : remoteFolders) {
            if (!localFolderNames.contains(remoteFolder.getName())) {
                LocalFolder localFolder = localStore.getFolder(remoteFolder.getName());
                foldersToCreate.add(localFolder);
            }
            remoteFolderNames.add(remoteFolder.getName());
        }
        localStore.createFolders(foldersToCreate, account.getDisplayCount());
        localFolders = localStore.getPersonalNamespaces(false);
        /*
             * Clear out any folders that are no longer on the remote store.
             */
        for (Folder localFolder : localFolders) {
            String localFolderName = localFolder.getName();
            //        special placeholder folder "-NONE-".
            if (K9.FOLDER_NONE.equals(localFolderName)) {
                localFolder.delete(false);
            }
            if (!account.isSpecialFolder(localFolderName) && !remoteFolderNames.contains(localFolderName)) {
                localFolder.delete(false);
            }
        }
        localFolders = localStore.getPersonalNamespaces(false);
        for (MessagingListener l : getListeners(listener)) {
            l.listFolders(account, localFolders);
        }
        for (MessagingListener l : getListeners(listener)) {
            l.listFoldersFinished(account);
        }
    } catch (Exception e) {
        for (MessagingListener l : getListeners(listener)) {
            l.listFoldersFailed(account, "");
        }
        addErrorMessage(account, null, e);
    } finally {
        if (localFolders != null) {
            for (Folder localFolder : localFolders) {
                closeFolder(localFolder);
            }
        }
    }
}
Also used : LocalFolder(com.fsck.k9.mailstore.LocalFolder) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) LocalStore(com.fsck.k9.mailstore.LocalStore) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) LinkedList(java.util.LinkedList) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException) HashSet(java.util.HashSet) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 18 with MessagingListener

use of com.fsck.k9.controller.MessagingListener in project k-9 by k9mail.

the class FolderList method checkMail.

/**
    * This class is responsible for reloading the list of local messages for a
    * given folder, notifying the adapter that the message have been loaded and
    * queueing up a remote update of the folder.
     */
private void checkMail(FolderInfoHolder folder) {
    TracingPowerManager pm = TracingPowerManager.getPowerManager(this);
    final TracingWakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "FolderList checkMail");
    wakeLock.setReferenceCounted(false);
    wakeLock.acquire(K9.WAKE_LOCK_TIMEOUT);
    MessagingListener listener = new SimpleMessagingListener() {

        @Override
        public void synchronizeMailboxFinished(Account account, String folder, int totalMessagesInMailbox, int numNewMessages) {
            if (!account.equals(mAccount)) {
                return;
            }
            wakeLock.release();
        }

        @Override
        public void synchronizeMailboxFailed(Account account, String folder, String message) {
            if (!account.equals(mAccount)) {
                return;
            }
            wakeLock.release();
        }
    };
    MessagingController.getInstance(getApplication()).synchronizeMailbox(mAccount, folder.name, listener, null);
    sendMail(mAccount);
}
Also used : Account(com.fsck.k9.Account) BaseAccount(com.fsck.k9.BaseAccount) TracingPowerManager(com.fsck.k9.mail.power.TracingPowerManager) MessagingListener(com.fsck.k9.controller.MessagingListener) SimpleMessagingListener(com.fsck.k9.controller.SimpleMessagingListener) SimpleMessagingListener(com.fsck.k9.controller.SimpleMessagingListener) TracingWakeLock(com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock)

Example 19 with MessagingListener

use of com.fsck.k9.controller.MessagingListener in project k-9 by k9mail.

the class MessagingController method putCommand.

private void putCommand(BlockingQueue<Command> queue, String description, MessagingListener listener, Runnable runnable, boolean isForeground) {
    int retries = 10;
    Exception e = null;
    while (retries-- > 0) {
        try {
            Command command = new Command();
            command.listener = listener;
            command.runnable = runnable;
            command.description = description;
            command.isForegroundPriority = isForeground;
            queue.put(command);
            return;
        } catch (InterruptedException ie) {
            SystemClock.sleep(200);
            e = ie;
        }
    }
    throw new Error(e);
}
Also used : PendingCommand(com.fsck.k9.controller.MessagingControllerCommands.PendingCommand) SuppressLint(android.annotation.SuppressLint) CertificateValidationException(com.fsck.k9.mail.CertificateValidationException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) IOException(java.io.IOException) MessagingException(com.fsck.k9.mail.MessagingException) AuthenticationFailedException(com.fsck.k9.mail.AuthenticationFailedException)

Example 20 with MessagingListener

use of com.fsck.k9.controller.MessagingListener in project k-9 by k9mail.

the class MessagingController method checkMailForAccount.

private void checkMailForAccount(final Context context, final Account account, final boolean ignoreLastCheckedTime, final MessagingListener listener) {
    if (!account.isAvailable(context)) {
        Timber.i("Skipping synchronizing unavailable account %s", account.getDescription());
        return;
    }
    final long accountInterval = account.getAutomaticCheckIntervalMinutes() * 60 * 1000;
    if (!ignoreLastCheckedTime && accountInterval <= 0) {
        Timber.i("Skipping synchronizing account %s", account.getDescription());
        return;
    }
    Timber.i("Synchronizing account %s", account.getDescription());
    account.setRingNotified(false);
    sendPendingMessages(account, listener);
    try {
        Account.FolderMode aDisplayMode = account.getFolderDisplayMode();
        Account.FolderMode aSyncMode = account.getFolderSyncMode();
        Store localStore = account.getLocalStore();
        for (final Folder folder : localStore.getPersonalNamespaces(false)) {
            folder.open(Folder.OPEN_MODE_RW);
            Folder.FolderClass fDisplayClass = folder.getDisplayClass();
            Folder.FolderClass fSyncClass = folder.getSyncClass();
            if (modeMismatch(aDisplayMode, fDisplayClass)) {
                continue;
            }
            if (modeMismatch(aSyncMode, fSyncClass)) {
                continue;
            }
            synchronizeFolder(account, folder, ignoreLastCheckedTime, accountInterval, listener);
        }
    } catch (MessagingException e) {
        Timber.e(e, "Unable to synchronize account %s", account.getName());
        addErrorMessage(account, null, e);
    } finally {
        putBackground("clear notification flag for " + account.getDescription(), null, new Runnable() {

            @Override
            public void run() {
                Timber.v("Clearing notification flag for %s", account.getDescription());
                account.setRingNotified(false);
                try {
                    AccountStats stats = account.getStats(context);
                    if (stats == null || stats.unreadMessageCount == 0) {
                        notificationController.clearNewMailNotifications(account);
                    }
                } catch (MessagingException e) {
                    Timber.e(e, "Unable to getUnreadMessageCount for account: %s", account);
                }
            }
        });
    }
}
Also used : SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) MessagingException(com.fsck.k9.mail.MessagingException) LocalStore(com.fsck.k9.mailstore.LocalStore) Store(com.fsck.k9.mail.Store) Pop3Store(com.fsck.k9.mail.store.pop3.Pop3Store) Folder(com.fsck.k9.mail.Folder) LocalFolder(com.fsck.k9.mailstore.LocalFolder) AccountStats(com.fsck.k9.AccountStats)

Aggregations

MessagingException (com.fsck.k9.mail.MessagingException)25 LocalFolder (com.fsck.k9.mailstore.LocalFolder)20 LocalStore (com.fsck.k9.mailstore.LocalStore)20 LocalMessage (com.fsck.k9.mailstore.LocalMessage)18 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)15 SuppressLint (android.annotation.SuppressLint)14 Folder (com.fsck.k9.mail.Folder)14 Message (com.fsck.k9.mail.Message)14 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)14 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)13 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)13 IOException (java.io.IOException)13 Store (com.fsck.k9.mail.Store)12 Pop3Store (com.fsck.k9.mail.store.pop3.Pop3Store)12 ArrayList (java.util.ArrayList)9 FetchProfile (com.fsck.k9.mail.FetchProfile)7 VisibleForTesting (android.support.annotation.VisibleForTesting)6 Account (com.fsck.k9.Account)6 Date (java.util.Date)6 SearchAccount (com.fsck.k9.search.SearchAccount)5