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);
}
}
});
}
});
}
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);
}
}
}
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;
}
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;
}
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);
}
Aggregations