1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-16 22:45:04 -05:00
k-9/src/com/fsck/k9/service/CoreReceiver.java

101 lines
3.3 KiB
Java
Raw Normal View History

package com.fsck.k9.service;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.util.Log;
import com.fsck.k9.K9;
public class CoreReceiver extends BroadcastReceiver
{
public static String WAKE_LOCK_RELEASE = "com.fsck.k9.service.CoreReceiver.wakeLockRelease";
public static String WAKE_LOCK_ID = "com.fsck.k9.service.CoreReceiver.wakeLockId";
private static ConcurrentHashMap<Integer, WakeLock> wakeLocks = new ConcurrentHashMap<Integer, WakeLock>();
private static AtomicInteger wakeLockSeq = new AtomicInteger(0);
private static Integer getWakeLock(Context context)
{
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9");
wakeLock.setReferenceCounted(false);
wakeLock.acquire(K9.BOOT_RECEIVER_WAKE_LOCK_TIMEOUT);
Integer tmpWakeLockId = wakeLockSeq.getAndIncrement();
wakeLocks.put(tmpWakeLockId, wakeLock);
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "CoreReceiver Created wakeLock " + tmpWakeLockId);
return tmpWakeLockId;
}
private static void releaseWakeLock(Integer wakeLockId)
{
if (wakeLockId != null)
{
WakeLock wl = wakeLocks.remove(wakeLockId);
if (wl != null)
{
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "CoreReceiver Releasing wakeLock " + wakeLockId);
wl.release();
}
else
{
Log.w(K9.LOG_TAG, "BootReceiver WakeLock " + wakeLockId + " doesn't exist");
}
}
}
public void onReceive(Context context, Intent intent)
{
Integer tmpWakeLockId = CoreReceiver.getWakeLock(context);
try
{
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "CoreReceiver.onReceive" + intent);
if (CoreReceiver.WAKE_LOCK_RELEASE.equals(intent.getAction()))
{
Integer wakeLockId = intent.getIntExtra(WAKE_LOCK_ID, -1);
if (wakeLockId != -1)
{
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "CoreReceiver Release wakeLock " + wakeLockId);
CoreReceiver.releaseWakeLock(wakeLockId);
}
}
else
{
tmpWakeLockId = receive(context, intent, tmpWakeLockId);
}
}
finally
{
CoreReceiver.releaseWakeLock(tmpWakeLockId);
}
}
public Integer receive(Context context, Intent intent, Integer wakeLockId)
{
return wakeLockId;
}
public static void releaseWakeLock(Context context, int wakeLockId)
{
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "CoreReceiver Got request to release wakeLock " + wakeLockId);
Intent i = new Intent();
i.setClass(context, CoreReceiver.class);
i.setAction(WAKE_LOCK_RELEASE);
i.putExtra(WAKE_LOCK_ID, wakeLockId);
context.sendBroadcast(i);
}
}