Search in sources :

Example 6 with TracingWakeLock

use of com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock in project k-9 by k9mail.

the class CoreReceiver method releaseWakeLock.

private static void releaseWakeLock(Integer wakeLockId) {
    if (wakeLockId != null) {
        TracingWakeLock wl = wakeLocks.remove(wakeLockId);
        if (wl != null) {
            Timber.v("CoreReceiver Releasing wakeLock %d", wakeLockId);
            wl.release();
        } else {
            Timber.w("BootReceiver WakeLock %d doesn't exist", wakeLockId);
        }
    }
}
Also used : TracingWakeLock(com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock)

Example 7 with TracingWakeLock

use of com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock in project k-9 by k9mail.

the class CoreService method addWakeLock.

/**
     * Adds a new wake lock to the specified intent.
     *
     * <p>
     * This will add the wake lock to the central wake lock registry managed by this class.
     * </p>
     *
     * @param context
     *         A {@link Context} instance. Never {@code null}.
     * @param intent
     *         The {@link Intent} to add the wake lock registry ID as extra to. Never {@code null}.
     */
protected static void addWakeLock(Context context, Intent intent) {
    TracingWakeLock wakeLock = acquireWakeLock(context, "CoreService addWakeLock", K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT);
    Integer tmpWakeLockId = registerWakeLock(wakeLock);
    intent.putExtra(WAKE_LOCK_ID, tmpWakeLockId);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TracingWakeLock(com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock)

Example 8 with TracingWakeLock

use of com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock in project k-9 by k9mail.

the class CoreService method acquireWakeLock.

/**
     * Acquires a wake lock.
     *
     * @param context
     *         A {@link Context} instance. Never {@code null}.
     * @param tag
     *         The tag to supply to {@link TracingPowerManager}.
     * @param timeout
     *         The wake lock timeout.
     *
     * @return A new {@link TracingWakeLock} instance.
     */
protected static TracingWakeLock acquireWakeLock(Context context, String tag, long timeout) {
    TracingPowerManager pm = TracingPowerManager.getPowerManager(context);
    TracingWakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, tag);
    wakeLock.setReferenceCounted(false);
    wakeLock.acquire(timeout);
    return wakeLock;
}
Also used : TracingPowerManager(com.fsck.k9.mail.power.TracingPowerManager) TracingWakeLock(com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock)

Example 9 with TracingWakeLock

use of com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock 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)

Aggregations

TracingWakeLock (com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock)9 TracingPowerManager (com.fsck.k9.mail.power.TracingPowerManager)4 Account (com.fsck.k9.Account)2 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 BaseAccount (com.fsck.k9.BaseAccount)1 Preferences (com.fsck.k9.Preferences)1 MessagingListener (com.fsck.k9.controller.MessagingListener)1 SimpleMessagingListener (com.fsck.k9.controller.SimpleMessagingListener)1 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)1 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)1 MessagingException (com.fsck.k9.mail.MessagingException)1 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)1 SearchAccount (com.fsck.k9.search.SearchAccount)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1