mirror of
https://github.com/moparisthebest/k-9
synced 2024-12-26 01:28:50 -05:00
Preparatory for more BroadcastReceivers
This commit is contained in:
parent
33495135ac
commit
c86a46deb1
@ -214,14 +214,18 @@
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="android.net.conn.BACKGROUND_DATA_SETTING_CHANGED" />
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="com.fsck.k9.service.BroadcastReceiver.wakeLockRelease"/>
|
||||
</intent-filter>
|
||||
<intent-filter>
|
||||
<action android:name="com.fsck.k9.service.BroadcastReceiver.scheduleIntent"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver android:name="com.fsck.k9.service.CoreReceiver"
|
||||
android:enabled="true"
|
||||
>
|
||||
<intent-filter>
|
||||
<action android:name="com.fsck.k9.service.CoreReceiver.wakeLockRelease"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<service
|
||||
android:name="com.fsck.k9.service.MailService"
|
||||
android:enabled="true"
|
||||
|
@ -1,69 +1,29 @@
|
||||
|
||||
package com.fsck.k9.service;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.Uri;
|
||||
import android.os.PowerManager;
|
||||
import android.os.PowerManager.WakeLock;
|
||||
import android.util.Log;
|
||||
|
||||
import com.fsck.k9.K9;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
public class BootReceiver extends BroadcastReceiver
|
||||
public class BootReceiver extends CoreReceiver
|
||||
{
|
||||
|
||||
public static String WAKE_LOCK_RELEASE = "com.fsck.k9.service.BroadcastReceiver.wakeLockRelease";
|
||||
public static String FIRE_INTENT = "com.fsck.k9.service.BroadcastReceiver.fireIntent";
|
||||
public static String SCHEDULE_INTENT = "com.fsck.k9.service.BroadcastReceiver.scheduleIntent";
|
||||
public static String CANCEL_INTENT = "com.fsck.k9.service.BroadcastReceiver.cancelIntent";
|
||||
|
||||
public static String WAKE_LOCK_ID = "com.fsck.k9.service.BroadcastReceiver.wakeLockId";
|
||||
public static String ALARMED_INTENT = "com.fsck.k9.service.BroadcastReceiver.pendingIntent";
|
||||
public static String AT_TIME = "com.fsck.k9.service.BroadcastReceiver.atTime";
|
||||
|
||||
private static ConcurrentHashMap<Integer, WakeLock> wakeLocks = new ConcurrentHashMap<Integer, WakeLock>();
|
||||
private static AtomicInteger wakeLockSeq = new AtomicInteger(0);
|
||||
|
||||
private 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);
|
||||
return tmpWakeLockId;
|
||||
}
|
||||
|
||||
private void releaseWakeLock(Integer wakeLockId)
|
||||
{
|
||||
if (wakeLockId != null)
|
||||
{
|
||||
WakeLock wl = wakeLocks.remove(wakeLockId);
|
||||
if (wl != null)
|
||||
{
|
||||
wl.release();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (K9.DEBUG)
|
||||
Log.w(K9.LOG_TAG, "BootReceiver WakeLock " + wakeLockId + " doesn't exist");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onReceive(Context context, Intent intent)
|
||||
{
|
||||
Integer tmpWakeLockId = getWakeLock(context);
|
||||
try
|
||||
public Integer receive(Context context, Intent intent, Integer tmpWakeLockId)
|
||||
{
|
||||
if (K9.DEBUG)
|
||||
Log.i(K9.LOG_TAG, "BootReceiver.onReceive" + intent);
|
||||
@ -98,7 +58,6 @@ public class BootReceiver extends BroadcastReceiver
|
||||
{
|
||||
Intent alarmedIntent = intent.getParcelableExtra(ALARMED_INTENT);
|
||||
String alarmedAction = alarmedIntent.getAction();
|
||||
|
||||
if (K9.DEBUG)
|
||||
Log.i(K9.LOG_TAG, "BootReceiver Got alarm to fire alarmedIntent " + alarmedAction);
|
||||
alarmedIntent.putExtra(WAKE_LOCK_ID, tmpWakeLockId);
|
||||
@ -131,21 +90,9 @@ public class BootReceiver extends BroadcastReceiver
|
||||
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
|
||||
alarmMgr.cancel(pi);
|
||||
}
|
||||
else if (BootReceiver.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, "BootReceiver Release wakeLock " + wakeLockId);
|
||||
releaseWakeLock(wakeLockId);
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
releaseWakeLock(tmpWakeLockId);
|
||||
}
|
||||
|
||||
|
||||
return tmpWakeLockId;
|
||||
}
|
||||
|
||||
private PendingIntent buildPendingIntent(Context context, Intent intent)
|
||||
@ -185,14 +132,4 @@ public class BootReceiver extends BroadcastReceiver
|
||||
context.sendBroadcast(i);
|
||||
}
|
||||
|
||||
public static void releaseWakeLock(Context context, int wakeLockId)
|
||||
{
|
||||
if (K9.DEBUG)
|
||||
Log.i(K9.LOG_TAG, "BootReceiver Got request to release wakeLock " + wakeLockId);
|
||||
Intent i = new Intent();
|
||||
i.setClass(context, BootReceiver.class);
|
||||
i.setAction(WAKE_LOCK_RELEASE);
|
||||
i.putExtra(WAKE_LOCK_ID, wakeLockId);
|
||||
context.sendBroadcast(i);
|
||||
}
|
||||
}
|
||||
|
100
src/com/fsck/k9/service/CoreReceiver.java
Normal file
100
src/com/fsck/k9/service/CoreReceiver.java
Normal file
@ -0,0 +1,100 @@
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
@ -46,6 +46,10 @@ public class MailService extends CoreService
|
||||
i.setClass(context, MailService.class);
|
||||
i.setAction(MailService.ACTION_RESCHEDULE);
|
||||
addWakeLockId(i, wakeLockId);
|
||||
if (wakeLockId == null)
|
||||
{
|
||||
addWakeLock(context, i);
|
||||
}
|
||||
context.startService(i);
|
||||
}
|
||||
|
||||
@ -142,29 +146,25 @@ public class MailService extends CoreService
|
||||
}
|
||||
else if (ACTION_CANCEL.equals(intent.getAction()))
|
||||
{
|
||||
if (Config.LOGV)
|
||||
{
|
||||
if (K9.DEBUG)
|
||||
Log.v(K9.LOG_TAG, "***** MailService *****: cancel");
|
||||
}
|
||||
|
||||
cancel();
|
||||
}
|
||||
else if (ACTION_RESCHEDULE.equals(intent.getAction()))
|
||||
{
|
||||
if (Config.LOGV)
|
||||
{
|
||||
if (K9.DEBUG)
|
||||
Log.v(K9.LOG_TAG, "***** MailService *****: reschedule");
|
||||
}
|
||||
|
||||
rescheduleAll(hasConnectivity, doBackground, startIdObj);
|
||||
startIdObj = null;
|
||||
|
||||
}
|
||||
else if (ACTION_RESCHEDULE_CHECK.equals(intent.getAction()))
|
||||
{
|
||||
if (Config.LOGV)
|
||||
{
|
||||
if (K9.DEBUG)
|
||||
Log.v(K9.LOG_TAG, "***** MailService *****: reschedule check");
|
||||
}
|
||||
|
||||
reschedule(startIdObj);
|
||||
startIdObj = null;
|
||||
|
||||
@ -292,19 +292,16 @@ public class MailService extends CoreService
|
||||
long delay = (shortestInterval * (60 * 1000));
|
||||
|
||||
long nextTime = System.currentTimeMillis() + delay;
|
||||
if (K9.DEBUG)
|
||||
{
|
||||
try
|
||||
{
|
||||
Log.i(K9.LOG_TAG,
|
||||
"Next check for package " + getApplication().getPackageName() + " scheduled for " + new Date(nextTime));
|
||||
if (K9.DEBUG)
|
||||
Log.i(K9.LOG_TAG, "Next check for package " + getApplication().getPackageName() + " scheduled for " + new Date(nextTime));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
// I once got a NullPointerException deep in new Date();
|
||||
Log.e(K9.LOG_TAG, "Exception while logging", e);
|
||||
}
|
||||
}
|
||||
|
||||
Intent i = new Intent();
|
||||
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService");
|
||||
@ -413,15 +410,14 @@ public class MailService extends CoreService
|
||||
}
|
||||
}
|
||||
if (K9.DEBUG)
|
||||
{
|
||||
Log.v(K9.LOG_TAG, "Pusher refresh interval = " + minInterval);
|
||||
|
||||
}
|
||||
if (minInterval != -1)
|
||||
{
|
||||
long nextTime = System.currentTimeMillis() + minInterval;
|
||||
|
||||
if (K9.DEBUG)
|
||||
Log.d(K9.LOG_TAG, "Next pusher refresh scheduled for " + new Date(nextTime));
|
||||
|
||||
Intent i = new Intent();
|
||||
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService");
|
||||
i.setAction(ACTION_REFRESH_PUSHERS);
|
||||
@ -440,13 +436,13 @@ public class MailService extends CoreService
|
||||
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();
|
||||
@ -456,7 +452,6 @@ public class MailService extends CoreService
|
||||
if (K9.DEBUG)
|
||||
Log.i(K9.LOG_TAG, "MailService completed Runnable " + runner.hashCode() + " with startId " + startId);
|
||||
wakeLock.release();
|
||||
|
||||
if (startId != null)
|
||||
{
|
||||
stopSelf(startId);
|
||||
|
Loading…
Reference in New Issue
Block a user