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

Move execute into a re-usable location

This commit is contained in:
Daniel Applebaum 2010-01-12 14:09:30 +00:00
parent 11b7157772
commit 64525028de
2 changed files with 40 additions and 38 deletions

View File

@ -1,6 +1,8 @@
package com.fsck.k9.service;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import android.app.Service;
@ -18,6 +20,7 @@ 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);
private final ExecutorService threadPool = Executors.newFixedThreadPool(1); // Must be single threaded
protected static void addWakeLockId(Intent i, Integer wakeLockId)
{
@ -86,6 +89,41 @@ public abstract class CoreService extends Service
}
public void execute(Context context, final Runnable runner, int wakeLockTime, final Integer startId)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
final WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9");
wakeLock.setReferenceCounted(false);
wakeLock.acquire(wakeLockTime);
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "CoreService queueing Runnable " + runner.hashCode() + " with startId " + startId);
Runnable myRunner = new Runnable()
{
public void run()
{
try
{
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "CoreService running Runnable " + runner.hashCode() + " with startId " + startId);
runner.run();
}
finally
{
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "CoreService completed Runnable " + runner.hashCode() + " with startId " + startId);
wakeLock.release();
if (startId != null)
{
stopSelf(startId);
}
}
}
};
threadPool.execute(myRunner);
}
public abstract void startService(Intent intent, int startId);

View File

@ -37,7 +37,6 @@ public class MailService extends CoreService
private static final String HAS_CONNECTIVITY = "com.fsck.k9.intent.action.MAIL_SERVICE_HAS_CONNECTIVITY";
private final ExecutorService threadPool = Executors.newFixedThreadPool(1); // Must be single threaded
public static void actionReschedule(Context context, Integer wakeLockId)
@ -428,41 +427,6 @@ public class MailService extends CoreService
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
}
public void execute(Context context, final Runnable runner, int wakeLockTime, final Integer startId)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
final WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9");
wakeLock.setReferenceCounted(false);
wakeLock.acquire(wakeLockTime);
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "MailService queueing Runnable " + runner.hashCode() + " with startId " + startId);
Runnable myRunner = new Runnable()
{
public void run()
{
try
{
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "MailService running Runnable " + runner.hashCode() + " with startId " + startId);
runner.run();
}
finally
{
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "MailService completed Runnable " + runner.hashCode() + " with startId " + startId);
wakeLock.release();
if (startId != null)
{
stopSelf(startId);
}
}
}
};
threadPool.execute(myRunner);
}
public IBinder onBind(Intent intent)
{