Search in sources :

Example 1 with SimpleMessagingListener

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

the class K9 method onCreate.

@Override
public void onCreate() {
    if (K9.DEVELOPER_MODE) {
        StrictMode.enableDefaults();
    }
    PRNGFixes.apply();
    super.onCreate();
    app = this;
    Globals.setContext(this);
    K9MailLib.setDebugStatus(new K9MailLib.DebugStatus() {

        @Override
        public boolean enabled() {
            return DEBUG;
        }

        @Override
        public boolean debugSensitive() {
            return DEBUG_SENSITIVE;
        }
    });
    checkCachedDatabaseVersion();
    Preferences prefs = Preferences.getPreferences(this);
    loadPrefs(prefs);
    /*
         * We have to give MimeMessage a temp directory because File.createTempFile(String, String)
         * doesn't work in Android and MimeMessage does not have access to a Context.
         */
    BinaryTempFileBody.setTempDirectory(getCacheDir());
    LocalKeyStore.setKeyStoreLocation(getDir("KeyStore", MODE_PRIVATE).toString());
    /*
         * Enable background sync of messages
         */
    setServicesEnabled(this);
    registerReceivers();
    MessagingController.getInstance(this).addListener(new SimpleMessagingListener() {

        private void broadcastIntent(String action, Account account, String folder, Message message) {
            Uri uri = Uri.parse("email://messages/" + account.getAccountNumber() + "/" + Uri.encode(folder) + "/" + Uri.encode(message.getUid()));
            Intent intent = new Intent(action, uri);
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_ACCOUNT, account.getDescription());
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_FOLDER, folder);
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_SENT_DATE, message.getSentDate());
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_FROM, Address.toString(message.getFrom()));
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_TO, Address.toString(message.getRecipients(Message.RecipientType.TO)));
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_CC, Address.toString(message.getRecipients(Message.RecipientType.CC)));
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_BCC, Address.toString(message.getRecipients(Message.RecipientType.BCC)));
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_SUBJECT, message.getSubject());
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_FROM_SELF, account.isAnIdentity(message.getFrom()));
            K9.this.sendBroadcast(intent);
            Timber.d("Broadcasted: action=%s account=%s folder=%s message uid=%s", action, account.getDescription(), folder, message.getUid());
        }

        private void updateUnreadWidget() {
            try {
                UnreadWidgetProvider.updateUnreadCount(K9.this);
            } catch (Exception e) {
                Timber.e(e, "Error while updating unread widget(s)");
            }
        }

        private void updateMailListWidget() {
            try {
                MessageListWidgetProvider.triggerMessageListWidgetUpdate(K9.this);
            } catch (RuntimeException e) {
                if (BuildConfig.DEBUG) {
                    throw e;
                } else {
                    Timber.e(e, "Error while updating message list widget");
                }
            }
        }

        @Override
        public void synchronizeMailboxRemovedMessage(Account account, String folder, Message message) {
            broadcastIntent(K9.Intents.EmailReceived.ACTION_EMAIL_DELETED, account, folder, message);
            updateUnreadWidget();
            updateMailListWidget();
        }

        @Override
        public void messageDeleted(Account account, String folder, Message message) {
            broadcastIntent(K9.Intents.EmailReceived.ACTION_EMAIL_DELETED, account, folder, message);
            updateUnreadWidget();
            updateMailListWidget();
        }

        @Override
        public void synchronizeMailboxNewMessage(Account account, String folder, Message message) {
            broadcastIntent(K9.Intents.EmailReceived.ACTION_EMAIL_RECEIVED, account, folder, message);
            updateUnreadWidget();
            updateMailListWidget();
        }

        @Override
        public void folderStatusChanged(Account account, String folderName, int unreadMessageCount) {
            updateUnreadWidget();
            updateMailListWidget();
            // let observers know a change occurred
            Intent intent = new Intent(K9.Intents.EmailReceived.ACTION_REFRESH_OBSERVER, null);
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_ACCOUNT, account.getDescription());
            intent.putExtra(K9.Intents.EmailReceived.EXTRA_FOLDER, folderName);
            K9.this.sendBroadcast(intent);
        }
    });
    notifyObservers();
}
Also used : Message(com.fsck.k9.mail.Message) Intent(android.content.Intent) Uri(android.net.Uri) K9MailLib(com.fsck.k9.mail.K9MailLib) SimpleMessagingListener(com.fsck.k9.controller.SimpleMessagingListener) SharedPreferences(android.content.SharedPreferences)

Example 2 with SimpleMessagingListener

use of com.fsck.k9.controller.SimpleMessagingListener 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 3 with SimpleMessagingListener

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

the class AttachmentController method downloadAttachment.

private void downloadAttachment(LocalPart localPart, final Runnable attachmentDownloadedCallback) {
    String accountUuid = localPart.getAccountUuid();
    Account account = Preferences.getPreferences(context).getAccount(accountUuid);
    LocalMessage message = localPart.getMessage();
    messageViewFragment.showAttachmentLoadingDialog();
    controller.loadAttachment(account, message, attachment.part, new SimpleMessagingListener() {

        @Override
        public void loadAttachmentFinished(Account account, Message message, Part part) {
            messageViewFragment.hideAttachmentLoadingDialogOnMainThread();
            messageViewFragment.runOnMainThread(attachmentDownloadedCallback);
        }

        @Override
        public void loadAttachmentFailed(Account account, Message message, Part part, String reason) {
            messageViewFragment.hideAttachmentLoadingDialogOnMainThread();
        }
    });
}
Also used : Account(com.fsck.k9.Account) LocalMessage(com.fsck.k9.mailstore.LocalMessage) LocalMessage(com.fsck.k9.mailstore.LocalMessage) Message(com.fsck.k9.mail.Message) Part(com.fsck.k9.mail.Part) LocalPart(com.fsck.k9.mailstore.LocalPart) SimpleMessagingListener(com.fsck.k9.controller.SimpleMessagingListener)

Example 4 with SimpleMessagingListener

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

the class MessagingControllerPushReceiver method syncFolder.

public void syncFolder(Folder folder) {
    Timber.v("syncFolder(%s)", folder.getName());
    final CountDownLatch latch = new CountDownLatch(1);
    controller.synchronizeMailbox(account, folder.getName(), new SimpleMessagingListener() {

        @Override
        public void synchronizeMailboxFinished(Account account, String folder, int totalMessagesInMailbox, int numNewMessages) {
            latch.countDown();
        }

        @Override
        public void synchronizeMailboxFailed(Account account, String folder, String message) {
            latch.countDown();
        }
    }, folder);
    Timber.v("syncFolder(%s) about to await latch release", folder.getName());
    try {
        latch.await();
        Timber.v("syncFolder(%s) got latch release", folder.getName());
    } catch (Exception e) {
        Timber.e(e, "Interrupted while awaiting latch release");
    }
}
Also used : Account(com.fsck.k9.Account) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

Account (com.fsck.k9.Account)3 SimpleMessagingListener (com.fsck.k9.controller.SimpleMessagingListener)3 Message (com.fsck.k9.mail.Message)2 Intent (android.content.Intent)1 SharedPreferences (android.content.SharedPreferences)1 Uri (android.net.Uri)1 BaseAccount (com.fsck.k9.BaseAccount)1 MessagingListener (com.fsck.k9.controller.MessagingListener)1 K9MailLib (com.fsck.k9.mail.K9MailLib)1 Part (com.fsck.k9.mail.Part)1 TracingPowerManager (com.fsck.k9.mail.power.TracingPowerManager)1 TracingWakeLock (com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock)1 LocalMessage (com.fsck.k9.mailstore.LocalMessage)1 LocalPart (com.fsck.k9.mailstore.LocalPart)1 CountDownLatch (java.util.concurrent.CountDownLatch)1