Search in sources :

Example 16 with MessagingController

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

the class CoreService method execute.

/**
     * Execute a task in the background thread.
     *
     * @param context
     *         A {@link Context} instance. Never {@code null}.
     * @param runner
     *         The code to be executed in the background thread.
     * @param wakeLockTime
     *         The timeout for the wake lock that will be acquired by this method.
     * @param startId
     *         The {@code startId} value received in {@link #onStart(Intent, int)} or {@code null}
     *         if you don't want the service to be shut down after {@code runner} has been executed
     *         (e.g. because you need to run another background task).<br>
     *         If this parameter is {@code null} you need to call {@code setAutoShutdown(false)}
     *         otherwise the auto shutdown code will stop the service.
     */
public void execute(Context context, final Runnable runner, int wakeLockTime, final Integer startId) {
    boolean serviceShutdownScheduled = false;
    final boolean autoShutdown = mAutoShutdown;
    // Acquire a new wakelock
    final TracingWakeLock wakeLock = acquireWakeLock(context, "CoreService execute", wakeLockTime);
    // Wrap the supplied runner with code to release the wake lock and stop the service if
    // appropriate.
    Runnable myRunner = new Runnable() {

        public void run() {
            try {
                // Get the sync status
                boolean oldIsSyncDisabled = MailService.isSyncDisabled();
                Timber.d("CoreService (%s) running Runnable %d with startId %d", className, runner.hashCode(), startId);
                // Run the supplied code
                runner.run();
                // MessagingController
                if (MailService.isSyncDisabled() != oldIsSyncDisabled) {
                    MessagingController.getInstance(getApplication()).systemStatusChanged();
                }
            } finally {
                // Making absolutely sure stopSelf() will be called
                try {
                    Timber.d("CoreService (%s) completed Runnable %d with startId %d", className, runner.hashCode(), startId);
                    wakeLock.release();
                } finally {
                    if (autoShutdown && startId != null) {
                        stopSelf(startId);
                    }
                }
            }
        }
    };
    // TODO: remove this. we never set mThreadPool to null
    if (mThreadPool == null) {
        Timber.e("CoreService.execute (%s) called with no thread pool available; " + "running Runnable %d in calling thread", className, runner.hashCode());
        synchronized (this) {
            myRunner.run();
            serviceShutdownScheduled = startId != null;
        }
    } else {
        Timber.d("CoreService (%s) queueing Runnable %d with startId %d", className, runner.hashCode(), startId);
        try {
            mThreadPool.execute(myRunner);
            serviceShutdownScheduled = startId != null;
        } catch (RejectedExecutionException e) {
            // onDestroy(). Still, this should not happen!
            if (!mShutdown) {
                throw e;
            }
            Timber.i("CoreService: %s is shutting down, ignoring rejected execution exception: %s", className, e.getMessage());
        }
    }
    mImmediateShutdown = !serviceShutdownScheduled;
}
Also used : TracingWakeLock(com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 17 with MessagingController

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

the class MessageProvider method delete.

@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
    Timber.v("MessageProvider/delete: %s", uri);
    // Note: can only delete a message
    List<String> segments = uri.getPathSegments();
    int accountId = Integer.parseInt(segments.get(1));
    long folderId = Long.parseLong(segments.get(2));
    String msgUid = segments.get(3);
    // get account
    Account myAccount = null;
    for (Account account : Preferences.getPreferences(getContext()).getAccounts()) {
        if (account.getAccountNumber() == accountId) {
            myAccount = account;
        }
    }
    if (myAccount == null) {
        Timber.e("Could not find account with id %d", accountId);
    }
    if (myAccount != null) {
        MessageReference messageReference = new MessageReference(myAccount.getUuid(), folderId, msgUid);
        MessagingController controller = MessagingController.getInstance(getContext());
        controller.deleteMessage(messageReference);
    }
    // FIXME return the actual number of deleted messages
    return 0;
}
Also used : SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) MessagingController(com.fsck.k9.controller.MessagingController) MessageReference(com.fsck.k9.controller.MessageReference)

Example 18 with MessagingController

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

the class MessageProvider method init.

public static void init() {
    Timber.v("Registering content resolver notifier");
    final Context context = DI.get(Context.class);
    MessagingController messagingController = DI.get(MessagingController.class);
    messagingController.addListener(new SimpleMessagingListener() {

        @Override
        public void folderStatusChanged(Account account, long folderId) {
            context.getContentResolver().notifyChange(CONTENT_URI, null);
        }
    });
}
Also used : Context(android.content.Context) SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) MessagingController(com.fsck.k9.controller.MessagingController) SimpleMessagingListener(com.fsck.k9.controller.SimpleMessagingListener)

Aggregations

MessagingController (com.fsck.k9.controller.MessagingController)10 Account (com.fsck.k9.Account)6 MessageReference (com.fsck.k9.activity.MessageReference)5 SearchAccount (com.fsck.k9.search.SearchAccount)5 SimpleMessagingListener (com.fsck.k9.controller.SimpleMessagingListener)3 Context (android.content.Context)2 BaseAccount (com.fsck.k9.BaseAccount)2 PendingIntent (android.app.PendingIntent)1 ProgressDialog (android.app.ProgressDialog)1 Intent (android.content.Intent)1 Bundle (android.os.Bundle)1 RemoteViews (android.widget.RemoteViews)1 AccountStats (com.fsck.k9.AccountStats)1 Preferences (com.fsck.k9.Preferences)1 MessageReferenceHelper.toMessageReferenceList (com.fsck.k9.activity.MessageReferenceHelper.toMessageReferenceList)1 MessageReferenceHelper.toMessageReferenceStringList (com.fsck.k9.activity.MessageReferenceHelper.toMessageReferenceStringList)1 UnreadWidgetConfiguration (com.fsck.k9.activity.UnreadWidgetConfiguration)1 SaveMessageTask (com.fsck.k9.activity.compose.SaveMessageTask)1 MessageReference (com.fsck.k9.controller.MessageReference)1 Contacts (com.fsck.k9.helper.Contacts)1