Search in sources :

Example 1 with TracingWakeLock

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

the class MessagingController method checkMail.

/**
     * Checks mail for one or multiple accounts. If account is null all accounts
     * are checked.
     */
public void checkMail(final Context context, final Account account, final boolean ignoreLastCheckedTime, final boolean useManualWakeLock, final MessagingListener listener) {
    TracingWakeLock twakeLock = null;
    if (useManualWakeLock) {
        TracingPowerManager pm = TracingPowerManager.getPowerManager(context);
        twakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9 MessagingController.checkMail");
        twakeLock.setReferenceCounted(false);
        twakeLock.acquire(K9.MANUAL_WAKE_LOCK_TIMEOUT);
    }
    final TracingWakeLock wakeLock = twakeLock;
    for (MessagingListener l : getListeners()) {
        l.checkMailStarted(context, account);
    }
    putBackground("checkMail", listener, new Runnable() {

        @Override
        public void run() {
            try {
                Timber.i("Starting mail check");
                Preferences prefs = Preferences.getPreferences(context);
                Collection<Account> accounts;
                if (account != null) {
                    accounts = new ArrayList<>(1);
                    accounts.add(account);
                } else {
                    accounts = prefs.getAvailableAccounts();
                }
                for (final Account account : accounts) {
                    checkMailForAccount(context, account, ignoreLastCheckedTime, listener);
                }
            } catch (Exception e) {
                Timber.e(e, "Unable to synchronize mail");
                addErrorMessage(account, null, e);
            }
            putBackground("finalize sync", null, new Runnable() {

                @Override
                public void run() {
                    Timber.i("Finished mail sync");
                    if (wakeLock != null) {
                        wakeLock.release();
                    }
                    for (MessagingListener l : getListeners()) {
                        l.checkMailFinished(context, account);
                    }
                }
            });
        }
    });
}
Also used : SearchAccount(com.fsck.k9.search.SearchAccount) Account(com.fsck.k9.Account) TracingPowerManager(com.fsck.k9.mail.power.TracingPowerManager) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Preferences(com.fsck.k9.Preferences) TracingWakeLock(com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock) 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 2 with TracingWakeLock

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

the class SleepService method reacquireWakeLock.

private static void reacquireWakeLock(SleepDatum sleepDatum) {
    TracingWakeLock wakeLock = sleepDatum.wakeLock;
    if (wakeLock != null) {
        synchronized (wakeLock) {
            long timeout = sleepDatum.timeout;
            Timber.d("SleepService Acquiring wakeLock for %d ms", timeout);
            wakeLock.acquire(timeout);
        }
    }
}
Also used : TracingWakeLock(com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock)

Example 3 with TracingWakeLock

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

the class CoreReceiver method getWakeLock.

private static Integer getWakeLock(Context context) {
    TracingPowerManager pm = TracingPowerManager.getPowerManager(context);
    TracingWakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CoreReceiver getWakeLock");
    wakeLock.setReferenceCounted(false);
    wakeLock.acquire(K9.BOOT_RECEIVER_WAKE_LOCK_TIMEOUT);
    Integer tmpWakeLockId = wakeLockSeq.getAndIncrement();
    wakeLocks.put(tmpWakeLockId, wakeLock);
    Timber.v("CoreReceiver Created wakeLock %d", tmpWakeLockId);
    return tmpWakeLockId;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TracingPowerManager(com.fsck.k9.mail.power.TracingPowerManager) TracingWakeLock(com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock)

Example 4 with TracingWakeLock

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

the class CoreService method onStartCommand.

@Override
public final int onStartCommand(Intent intent, int flags, int startId) {
    /*
         * When a process is killed due to low memory, it's later restarted and services that were
         * started with START_STICKY are started with the intent being null.
         *
         * For now we just ignore these restart events. This should be fine because all necessary
         * services are started from K9.onCreate() when the Application object is initialized.
         *
         * See issue 3750
         */
    if (intent == null) {
        stopSelf(startId);
        return START_NOT_STICKY;
    }
    // Acquire new wake lock
    TracingWakeLock wakeLock = acquireWakeLock(this, "CoreService onStart", K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT);
    Timber.i("CoreService: %s.onStart(%s, %d)", className, intent, startId);
    // If we were started by BootReceiver, release the wake lock acquired there.
    int wakeLockId = intent.getIntExtra(BootReceiver.WAKE_LOCK_ID, -1);
    if (wakeLockId != -1) {
        BootReceiver.releaseWakeLock(this, wakeLockId);
    }
    // If we were passed an ID from our own wake lock registry, retrieve that wake lock and
    // release it.
    int coreWakeLockId = intent.getIntExtra(WAKE_LOCK_ID, -1);
    if (coreWakeLockId != -1) {
        Timber.d("Got core wake lock id %d", coreWakeLockId);
        // Remove wake lock from the registry
        TracingWakeLock coreWakeLock = sWakeLocks.remove(coreWakeLockId);
        // Release wake lock
        if (coreWakeLock != null) {
            Timber.d("Found core wake lock with id %d, releasing", coreWakeLockId);
            coreWakeLock.release();
        }
    }
    // Run the actual start-code of the service
    mImmediateShutdown = true;
    int startFlag;
    try {
        startFlag = startService(intent, startId);
    } finally {
        try {
            // Release the wake lock acquired at the start of this method
            wakeLock.release();
        } catch (Exception e) {
        /* ignore */
        }
        try {
            // this service.
            if (mAutoShutdown && mImmediateShutdown && startId != -1) {
                stopSelf(startId);
                startFlag = START_NOT_STICKY;
            }
        } catch (Exception e) {
        /* ignore */
        }
    }
    return startFlag;
}
Also used : TracingWakeLock(com.fsck.k9.mail.power.TracingPowerManager.TracingWakeLock) RejectedExecutionException(java.util.concurrent.RejectedExecutionException)

Example 5 with TracingWakeLock

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

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