1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-10-31 15:45:08 -04:00

Add wakelocks for calling between MailService and Push/PollServices.

I don't know if these are truly necessary, but they should not be
harmful and might be a big reliability boost.
This commit is contained in:
Daniel Applebaum 2009-12-21 01:48:15 +00:00
parent 41d7ca51a3
commit 4859ff7e8f
3 changed files with 41 additions and 0 deletions

View File

@ -1,5 +1,8 @@
package com.fsck.k9.service;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
@ -12,6 +15,10 @@ import com.fsck.k9.K9;
public abstract class CoreService extends Service
{
public static String WAKE_LOCK_ID = "com.fsck.k9.service.CoreService.wakeLockId";
private static ConcurrentHashMap<Integer, WakeLock> wakeLocks = new ConcurrentHashMap<Integer, WakeLock>();
private static AtomicInteger wakeLockSeq = new AtomicInteger(0);
protected static void addWakeLockId(Intent i, Integer wakeLockId)
{
if (wakeLockId != null)
@ -20,6 +27,19 @@ public abstract class CoreService extends Service
}
}
protected static void addWakeLock(Context context, Intent i)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9");
wakeLock.setReferenceCounted(false);
wakeLock.acquire(K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT);
Integer tmpWakeLockId = wakeLockSeq.getAndIncrement();
wakeLocks.put(tmpWakeLockId, wakeLock);
i.putExtra(WAKE_LOCK_ID, tmpWakeLockId);
}
@Override
public void onStart(Intent intent, int startId)
{
@ -36,6 +56,23 @@ public abstract class CoreService extends Service
{
BootReceiver.releaseWakeLock(this, wakeLockId);
}
Integer coreWakeLockId = intent.getIntExtra(WAKE_LOCK_ID, -1);
if (coreWakeLockId != null && coreWakeLockId != -1)
{
if (K9.DEBUG)
{
Log.d(K9.LOG_TAG, "Got core wake lock id " + coreWakeLockId);
}
WakeLock coreWakeLock = wakeLocks.remove(coreWakeLockId);
if (coreWakeLock != null)
{
if (K9.DEBUG)
{
Log.d(K9.LOG_TAG, "Found core wake lock with id " + coreWakeLockId + ", releasing");
}
coreWakeLock.release();
}
}
try
{

View File

@ -22,6 +22,7 @@ public class PollService extends CoreService
Intent i = new Intent();
i.setClass(context, PollService.class);
i.setAction(PollService.START_SERVICE);
addWakeLock(context, i);
context.startService(i);
}
@ -30,6 +31,7 @@ public class PollService extends CoreService
Intent i = new Intent();
i.setClass(context, PollService.class);
i.setAction(PollService.STOP_SERVICE);
addWakeLock(context, i);
context.startService(i);
}

View File

@ -16,6 +16,7 @@ public class PushService extends CoreService
Intent i = new Intent();
i.setClass(context, PushService.class);
i.setAction(PushService.START_SERVICE);
addWakeLock(context, i);
context.startService(i);
}
@ -24,6 +25,7 @@ public class PushService extends CoreService
Intent i = new Intent();
i.setClass(context, PushService.class);
i.setAction(PushService.STOP_SERVICE);
addWakeLock(context, i);
context.startService(i);
}