diff --git a/src/com/fsck/k9/service/CoreService.java b/src/com/fsck/k9/service/CoreService.java index 4bcc28ae7..ed2cb29be 100644 --- a/src/com/fsck/k9/service/CoreService.java +++ b/src/com/fsck/k9/service/CoreService.java @@ -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 wakeLocks = new ConcurrentHashMap(); + 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 { diff --git a/src/com/fsck/k9/service/PollService.java b/src/com/fsck/k9/service/PollService.java index b94a6cf81..5376891e8 100644 --- a/src/com/fsck/k9/service/PollService.java +++ b/src/com/fsck/k9/service/PollService.java @@ -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); } diff --git a/src/com/fsck/k9/service/PushService.java b/src/com/fsck/k9/service/PushService.java index 3f0220285..b90890ed0 100644 --- a/src/com/fsck/k9/service/PushService.java +++ b/src/com/fsck/k9/service/PushService.java @@ -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); }