2010-01-03 19:47:11 -05:00
|
|
|
|
|
|
|
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.util.Log;
|
|
|
|
|
|
|
|
import com.fsck.k9.K9;
|
2010-05-16 20:30:32 -04:00
|
|
|
import com.fsck.k9.helper.power.TracingPowerManager;
|
|
|
|
import com.fsck.k9.helper.power.TracingPowerManager.TracingWakeLock;
|
2010-01-03 19:47:11 -05:00
|
|
|
|
|
|
|
public class CoreReceiver extends BroadcastReceiver
|
|
|
|
{
|
|
|
|
|
|
|
|
public static String WAKE_LOCK_RELEASE = "com.fsck.k9.service.CoreReceiver.wakeLockRelease";
|
2010-01-13 20:07:28 -05:00
|
|
|
|
2010-01-03 19:47:11 -05:00
|
|
|
public static String WAKE_LOCK_ID = "com.fsck.k9.service.CoreReceiver.wakeLockId";
|
2010-01-13 20:07:28 -05:00
|
|
|
|
2010-05-16 20:30:32 -04:00
|
|
|
private static ConcurrentHashMap<Integer, TracingWakeLock> wakeLocks = new ConcurrentHashMap<Integer, TracingWakeLock>();
|
2010-01-03 19:47:11 -05:00
|
|
|
private static AtomicInteger wakeLockSeq = new AtomicInteger(0);
|
|
|
|
|
|
|
|
private static Integer getWakeLock(Context context)
|
|
|
|
{
|
2010-05-16 20:30:32 -04:00
|
|
|
TracingPowerManager pm = TracingPowerManager.getPowerManager(context);
|
|
|
|
TracingWakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "CoreReceiver getWakeLock");
|
2010-01-03 19:47:11 -05:00
|
|
|
wakeLock.setReferenceCounted(false);
|
|
|
|
wakeLock.acquire(K9.BOOT_RECEIVER_WAKE_LOCK_TIMEOUT);
|
|
|
|
Integer tmpWakeLockId = wakeLockSeq.getAndIncrement();
|
|
|
|
wakeLocks.put(tmpWakeLockId, wakeLock);
|
|
|
|
if (K9.DEBUG)
|
2010-01-22 00:55:58 -05:00
|
|
|
Log.v(K9.LOG_TAG, "CoreReceiver Created wakeLock " + tmpWakeLockId);
|
2010-01-03 19:47:11 -05:00
|
|
|
return tmpWakeLockId;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void releaseWakeLock(Integer wakeLockId)
|
|
|
|
{
|
|
|
|
if (wakeLockId != null)
|
|
|
|
{
|
2010-05-16 20:30:32 -04:00
|
|
|
TracingWakeLock wl = wakeLocks.remove(wakeLockId);
|
2010-01-03 19:47:11 -05:00
|
|
|
if (wl != null)
|
|
|
|
{
|
|
|
|
if (K9.DEBUG)
|
2010-01-22 00:55:58 -05:00
|
|
|
Log.v(K9.LOG_TAG, "CoreReceiver Releasing wakeLock " + wakeLockId);
|
2010-01-03 19:47:11 -05:00
|
|
|
wl.release();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Log.w(K9.LOG_TAG, "BootReceiver WakeLock " + wakeLockId + " doesn't exist");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-16 08:20:10 -04:00
|
|
|
@Override
|
2010-01-03 19:47:11 -05:00
|
|
|
public void onReceive(Context context, Intent intent)
|
|
|
|
{
|
|
|
|
Integer tmpWakeLockId = CoreReceiver.getWakeLock(context);
|
|
|
|
try
|
2010-01-13 20:07:28 -05:00
|
|
|
{
|
2010-01-03 19:47:11 -05:00
|
|
|
if (K9.DEBUG)
|
2010-01-13 20:07:28 -05:00
|
|
|
Log.i(K9.LOG_TAG, "CoreReceiver.onReceive" + intent);
|
2010-01-03 19:47:11 -05:00
|
|
|
if (CoreReceiver.WAKE_LOCK_RELEASE.equals(intent.getAction()))
|
|
|
|
{
|
|
|
|
Integer wakeLockId = intent.getIntExtra(WAKE_LOCK_ID, -1);
|
|
|
|
if (wakeLockId != -1)
|
|
|
|
{
|
|
|
|
if (K9.DEBUG)
|
2010-01-22 00:55:58 -05:00
|
|
|
Log.v(K9.LOG_TAG, "CoreReceiver Release wakeLock " + wakeLockId);
|
2010-01-03 19:47:11 -05:00
|
|
|
CoreReceiver.releaseWakeLock(wakeLockId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tmpWakeLockId = receive(context, intent, tmpWakeLockId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
CoreReceiver.releaseWakeLock(tmpWakeLockId);
|
|
|
|
}
|
|
|
|
}
|
2010-01-13 20:07:28 -05:00
|
|
|
|
2010-01-03 19:47:11 -05:00
|
|
|
public Integer receive(Context context, Intent intent, Integer wakeLockId)
|
|
|
|
{
|
|
|
|
return wakeLockId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void releaseWakeLock(Context context, int wakeLockId)
|
|
|
|
{
|
|
|
|
if (K9.DEBUG)
|
2010-01-22 00:55:58 -05:00
|
|
|
Log.v(K9.LOG_TAG, "CoreReceiver Got request to release wakeLock " + wakeLockId);
|
2010-01-03 19:47:11 -05:00
|
|
|
Intent i = new Intent();
|
|
|
|
i.setClass(context, CoreReceiver.class);
|
|
|
|
i.setAction(WAKE_LOCK_RELEASE);
|
|
|
|
i.putExtra(WAKE_LOCK_ID, wakeLockId);
|
|
|
|
context.sendBroadcast(i);
|
|
|
|
}
|
|
|
|
}
|