mirror of
https://github.com/moparisthebest/k-9
synced 2024-11-24 02:12:15 -05:00
EOLs got twisted by something. fixing
This commit is contained in:
parent
0e273893bd
commit
eb54a99aab
@ -1,299 +1,299 @@
|
|||||||
|
|
||||||
package com.fsck.k9.service;
|
package com.fsck.k9.service;
|
||||||
|
|
||||||
import android.app.Notification;
|
import android.app.Notification;
|
||||||
import android.app.NotificationManager;
|
import android.app.NotificationManager;
|
||||||
import android.app.PendingIntent;
|
import android.app.PendingIntent;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.ConnectivityManager;
|
import android.net.ConnectivityManager;
|
||||||
import android.net.NetworkInfo;
|
import android.net.NetworkInfo;
|
||||||
import android.net.NetworkInfo.State;
|
import android.net.NetworkInfo.State;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.os.PowerManager;
|
import android.os.PowerManager;
|
||||||
import android.os.PowerManager.WakeLock;
|
import android.os.PowerManager.WakeLock;
|
||||||
import android.util.Config;
|
import android.util.Config;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import com.fsck.k9.*;
|
import com.fsck.k9.*;
|
||||||
import com.fsck.k9.mail.Pusher;
|
import com.fsck.k9.mail.Pusher;
|
||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*/
|
*/
|
||||||
public class MailService extends CoreService
|
public class MailService extends CoreService
|
||||||
{
|
{
|
||||||
private static final String ACTION_CHECK_MAIL = "com.fsck.k9.intent.action.MAIL_SERVICE_WAKEUP";
|
private static final String ACTION_CHECK_MAIL = "com.fsck.k9.intent.action.MAIL_SERVICE_WAKEUP";
|
||||||
private static final String ACTION_RESCHEDULE = "com.fsck.k9.intent.action.MAIL_SERVICE_RESCHEDULE";
|
private static final String ACTION_RESCHEDULE = "com.fsck.k9.intent.action.MAIL_SERVICE_RESCHEDULE";
|
||||||
private static final String ACTION_RESCHEDULE_CHECK = "com.fsck.k9.intent.action.MAIL_SERVICE_RESCHEDULE_CHECK";
|
private static final String ACTION_RESCHEDULE_CHECK = "com.fsck.k9.intent.action.MAIL_SERVICE_RESCHEDULE_CHECK";
|
||||||
private static final String ACTION_CANCEL = "com.fsck.k9.intent.action.MAIL_SERVICE_CANCEL";
|
private static final String ACTION_CANCEL = "com.fsck.k9.intent.action.MAIL_SERVICE_CANCEL";
|
||||||
private static final String ACTION_REFRESH_PUSHERS = "com.fsck.k9.intent.action.MAIL_SERVICE_REFRESH_PUSHERS";
|
private static final String ACTION_REFRESH_PUSHERS = "com.fsck.k9.intent.action.MAIL_SERVICE_REFRESH_PUSHERS";
|
||||||
private static final String CONNECTIVITY_CHANGE = "com.fsck.k9.intent.action.MAIL_SERVICE_CONNECTIVITY_CHANGE";
|
private static final String CONNECTIVITY_CHANGE = "com.fsck.k9.intent.action.MAIL_SERVICE_CONNECTIVITY_CHANGE";
|
||||||
private static final String BACKGROUND_DATA_CHANGED = "com.fsck.k9.intent.action.MAIL_SERVICE_BACKGROUND_DATA_CHANGED";
|
private static final String BACKGROUND_DATA_CHANGED = "com.fsck.k9.intent.action.MAIL_SERVICE_BACKGROUND_DATA_CHANGED";
|
||||||
private static final String CANCEL_CONNECTIVITY_NOTICE = "com.fsck.k9.intent.action.MAIL_SERVICE_CANCEL_CONNECTIVITY_NOTICE";
|
private static final String CANCEL_CONNECTIVITY_NOTICE = "com.fsck.k9.intent.action.MAIL_SERVICE_CANCEL_CONNECTIVITY_NOTICE";
|
||||||
|
|
||||||
private static final String HAS_CONNECTIVITY = "com.fsck.k9.intent.action.MAIL_SERVICE_HAS_CONNECTIVITY";
|
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
|
private final ExecutorService threadPool = Executors.newFixedThreadPool(1); // Must be single threaded
|
||||||
|
|
||||||
|
|
||||||
public static void actionReschedule(Context context, Integer wakeLockId)
|
public static void actionReschedule(Context context, Integer wakeLockId)
|
||||||
{
|
{
|
||||||
Intent i = new Intent();
|
Intent i = new Intent();
|
||||||
i.setClass(context, MailService.class);
|
i.setClass(context, MailService.class);
|
||||||
i.setAction(MailService.ACTION_RESCHEDULE);
|
i.setAction(MailService.ACTION_RESCHEDULE);
|
||||||
addWakeLockId(i, wakeLockId);
|
addWakeLockId(i, wakeLockId);
|
||||||
context.startService(i);
|
context.startService(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void rescheduleCheck(Context context, Integer wakeLockId)
|
public static void rescheduleCheck(Context context, Integer wakeLockId)
|
||||||
{
|
{
|
||||||
Intent i = new Intent();
|
Intent i = new Intent();
|
||||||
i.setClass(context, MailService.class);
|
i.setClass(context, MailService.class);
|
||||||
i.setAction(MailService.ACTION_RESCHEDULE_CHECK);
|
i.setAction(MailService.ACTION_RESCHEDULE_CHECK);
|
||||||
addWakeLockId(i, wakeLockId);
|
addWakeLockId(i, wakeLockId);
|
||||||
context.startService(i);
|
context.startService(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void actionCancel(Context context, Integer wakeLockId)
|
public static void actionCancel(Context context, Integer wakeLockId)
|
||||||
{
|
{
|
||||||
Intent i = new Intent();
|
Intent i = new Intent();
|
||||||
i.setClass(context, MailService.class);
|
i.setClass(context, MailService.class);
|
||||||
i.setAction(MailService.ACTION_CANCEL);
|
i.setAction(MailService.ACTION_CANCEL);
|
||||||
addWakeLockId(i, wakeLockId);
|
addWakeLockId(i, wakeLockId);
|
||||||
context.startService(i);
|
context.startService(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void connectivityChange(Context context, boolean hasConnectivity, Integer wakeLockId)
|
public static void connectivityChange(Context context, boolean hasConnectivity, Integer wakeLockId)
|
||||||
{
|
{
|
||||||
Intent i = new Intent();
|
Intent i = new Intent();
|
||||||
i.setClass(context, MailService.class);
|
i.setClass(context, MailService.class);
|
||||||
i.setAction(MailService.CONNECTIVITY_CHANGE);
|
i.setAction(MailService.CONNECTIVITY_CHANGE);
|
||||||
i.putExtra(HAS_CONNECTIVITY, hasConnectivity);
|
i.putExtra(HAS_CONNECTIVITY, hasConnectivity);
|
||||||
addWakeLockId(i, wakeLockId);
|
addWakeLockId(i, wakeLockId);
|
||||||
context.startService(i);
|
context.startService(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void backgroundDataChanged(Context context, Integer wakeLockId)
|
public static void backgroundDataChanged(Context context, Integer wakeLockId)
|
||||||
{
|
{
|
||||||
Intent i = new Intent();
|
Intent i = new Intent();
|
||||||
i.setClass(context, MailService.class);
|
i.setClass(context, MailService.class);
|
||||||
i.setAction(MailService.BACKGROUND_DATA_CHANGED);
|
i.setAction(MailService.BACKGROUND_DATA_CHANGED);
|
||||||
addWakeLockId(i, wakeLockId);
|
addWakeLockId(i, wakeLockId);
|
||||||
context.startService(i);
|
context.startService(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate()
|
public void onCreate()
|
||||||
{
|
{
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
Log.v(K9.LOG_TAG, "***** MailService *****: onCreate");
|
Log.v(K9.LOG_TAG, "***** MailService *****: onCreate");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startService(Intent intent, int startId)
|
public void startService(Intent intent, int startId)
|
||||||
{
|
{
|
||||||
Integer startIdObj = startId;
|
Integer startIdObj = startId;
|
||||||
long startTime = System.currentTimeMillis();
|
long startTime = System.currentTimeMillis();
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ConnectivityManager connectivityManager = (ConnectivityManager)getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
|
ConnectivityManager connectivityManager = (ConnectivityManager)getApplication().getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
boolean doBackground = true;
|
boolean doBackground = true;
|
||||||
boolean hasConnectivity = false;
|
boolean hasConnectivity = false;
|
||||||
|
|
||||||
if (connectivityManager != null)
|
if (connectivityManager != null)
|
||||||
{
|
{
|
||||||
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
|
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
|
||||||
if (netInfo != null)
|
if (netInfo != null)
|
||||||
{
|
{
|
||||||
State state = netInfo.getState();
|
State state = netInfo.getState();
|
||||||
hasConnectivity = state == State.CONNECTED;
|
hasConnectivity = state == State.CONNECTED;
|
||||||
}
|
}
|
||||||
boolean backgroundData = connectivityManager.getBackgroundDataSetting();
|
boolean backgroundData = connectivityManager.getBackgroundDataSetting();
|
||||||
|
|
||||||
K9.BACKGROUND_OPS bOps = K9.getBackgroundOps();
|
K9.BACKGROUND_OPS bOps = K9.getBackgroundOps();
|
||||||
doBackground = (backgroundData == true && bOps != K9.BACKGROUND_OPS.NEVER)
|
doBackground = (backgroundData == true && bOps != K9.BACKGROUND_OPS.NEVER)
|
||||||
| (backgroundData == false && bOps == K9.BACKGROUND_OPS.ALWAYS);
|
| (backgroundData == false && bOps == K9.BACKGROUND_OPS.ALWAYS);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setForeground(true); // if it gets killed once, it'll never restart
|
setForeground(true); // if it gets killed once, it'll never restart
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
Log.i(K9.LOG_TAG, "MailService.onStart(" + intent + ", " + startId
|
Log.i(K9.LOG_TAG, "MailService.onStart(" + intent + ", " + startId
|
||||||
+ "), hasConnectivity = " + hasConnectivity + ", doBackground = " + doBackground);
|
+ "), hasConnectivity = " + hasConnectivity + ", doBackground = " + doBackground);
|
||||||
|
|
||||||
// MessagingController.getInstance(getApplication()).addListener(mListener);
|
// MessagingController.getInstance(getApplication()).addListener(mListener);
|
||||||
if (ACTION_CHECK_MAIL.equals(intent.getAction()))
|
if (ACTION_CHECK_MAIL.equals(intent.getAction()))
|
||||||
{
|
{
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
Log.i(K9.LOG_TAG, "***** MailService *****: checking mail");
|
Log.i(K9.LOG_TAG, "***** MailService *****: checking mail");
|
||||||
|
|
||||||
if (hasConnectivity && doBackground)
|
if (hasConnectivity && doBackground)
|
||||||
{
|
{
|
||||||
PollService.startService(this);
|
PollService.startService(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
reschedule(startIdObj);
|
reschedule(startIdObj);
|
||||||
startIdObj = null;
|
startIdObj = null;
|
||||||
}
|
}
|
||||||
else if (ACTION_CANCEL.equals(intent.getAction()))
|
else if (ACTION_CANCEL.equals(intent.getAction()))
|
||||||
{
|
{
|
||||||
if (Config.LOGV)
|
if (Config.LOGV)
|
||||||
{
|
{
|
||||||
Log.v(K9.LOG_TAG, "***** MailService *****: cancel");
|
Log.v(K9.LOG_TAG, "***** MailService *****: cancel");
|
||||||
}
|
}
|
||||||
|
|
||||||
cancel();
|
cancel();
|
||||||
}
|
}
|
||||||
else if (ACTION_RESCHEDULE.equals(intent.getAction()))
|
else if (ACTION_RESCHEDULE.equals(intent.getAction()))
|
||||||
{
|
{
|
||||||
if (Config.LOGV)
|
if (Config.LOGV)
|
||||||
{
|
{
|
||||||
Log.v(K9.LOG_TAG, "***** MailService *****: reschedule");
|
Log.v(K9.LOG_TAG, "***** MailService *****: reschedule");
|
||||||
}
|
}
|
||||||
rescheduleAll(hasConnectivity, doBackground, startIdObj);
|
rescheduleAll(hasConnectivity, doBackground, startIdObj);
|
||||||
startIdObj = null;
|
startIdObj = null;
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (ACTION_RESCHEDULE_CHECK.equals(intent.getAction()))
|
else if (ACTION_RESCHEDULE_CHECK.equals(intent.getAction()))
|
||||||
{
|
{
|
||||||
if (Config.LOGV)
|
if (Config.LOGV)
|
||||||
{
|
{
|
||||||
Log.v(K9.LOG_TAG, "***** MailService *****: reschedule check");
|
Log.v(K9.LOG_TAG, "***** MailService *****: reschedule check");
|
||||||
}
|
}
|
||||||
reschedule(startIdObj);
|
reschedule(startIdObj);
|
||||||
startIdObj = null;
|
startIdObj = null;
|
||||||
|
|
||||||
}
|
}
|
||||||
else if (ACTION_REFRESH_PUSHERS.equals(intent.getAction()))
|
else if (ACTION_REFRESH_PUSHERS.equals(intent.getAction()))
|
||||||
{
|
{
|
||||||
if (hasConnectivity && doBackground)
|
if (hasConnectivity && doBackground)
|
||||||
{
|
{
|
||||||
schedulePushers(null);
|
schedulePushers(null);
|
||||||
refreshPushers(startIdObj);
|
refreshPushers(startIdObj);
|
||||||
startIdObj = null;
|
startIdObj = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (CONNECTIVITY_CHANGE.equals(intent.getAction()) ||
|
else if (CONNECTIVITY_CHANGE.equals(intent.getAction()) ||
|
||||||
BACKGROUND_DATA_CHANGED.equals(intent.getAction()))
|
BACKGROUND_DATA_CHANGED.equals(intent.getAction()))
|
||||||
{
|
{
|
||||||
notifyConnectionStatus(hasConnectivity);
|
notifyConnectionStatus(hasConnectivity);
|
||||||
rescheduleAll(hasConnectivity, doBackground, startIdObj);
|
rescheduleAll(hasConnectivity, doBackground, startIdObj);
|
||||||
startIdObj = null;
|
startIdObj = null;
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
Log.i(K9.LOG_TAG, "Got connectivity action with hasConnectivity = " + hasConnectivity + ", doBackground = " + doBackground);
|
Log.i(K9.LOG_TAG, "Got connectivity action with hasConnectivity = " + hasConnectivity + ", doBackground = " + doBackground);
|
||||||
}
|
}
|
||||||
else if (CANCEL_CONNECTIVITY_NOTICE.equals(intent.getAction()))
|
else if (CANCEL_CONNECTIVITY_NOTICE.equals(intent.getAction()))
|
||||||
{
|
{
|
||||||
notifyConnectionStatus(true);
|
notifyConnectionStatus(true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (startIdObj != null)
|
if (startIdObj != null)
|
||||||
{
|
{
|
||||||
stopSelf(startId);
|
stopSelf(startId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
Log.i(K9.LOG_TAG, "MailService.onStart took " + (System.currentTimeMillis() - startTime) + "ms");
|
Log.i(K9.LOG_TAG, "MailService.onStart took " + (System.currentTimeMillis() - startTime) + "ms");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void rescheduleAll(final boolean hasConnectivity, final boolean doBackground, final Integer startId)
|
private void rescheduleAll(final boolean hasConnectivity, final boolean doBackground, final Integer startId)
|
||||||
{
|
{
|
||||||
if (hasConnectivity && doBackground)
|
if (hasConnectivity && doBackground)
|
||||||
{
|
{
|
||||||
reschedule(null);
|
reschedule(null);
|
||||||
reschedulePushers(startId);
|
reschedulePushers(startId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
stopPushers(startId);
|
stopPushers(startId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void notifyConnectionStatus(boolean hasConnectivity)
|
private void notifyConnectionStatus(boolean hasConnectivity)
|
||||||
{
|
{
|
||||||
if (true) return;
|
if (true) return;
|
||||||
NotificationManager notifMgr =
|
NotificationManager notifMgr =
|
||||||
(NotificationManager)getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
|
(NotificationManager)getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
|
||||||
if (hasConnectivity == false)
|
if (hasConnectivity == false)
|
||||||
{
|
{
|
||||||
String notice = getApplication().getString(R.string.no_connection_alert);
|
String notice = getApplication().getString(R.string.no_connection_alert);
|
||||||
String header = getApplication().getString(R.string.alert_header);
|
String header = getApplication().getString(R.string.alert_header);
|
||||||
|
|
||||||
|
|
||||||
Notification notif = new Notification(R.drawable.stat_notify_email_generic,
|
Notification notif = new Notification(R.drawable.stat_notify_email_generic,
|
||||||
header, System.currentTimeMillis());
|
header, System.currentTimeMillis());
|
||||||
|
|
||||||
Intent i = new Intent();
|
Intent i = new Intent();
|
||||||
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService");
|
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService");
|
||||||
i.setAction(MailService.CANCEL_CONNECTIVITY_NOTICE);
|
i.setAction(MailService.CANCEL_CONNECTIVITY_NOTICE);
|
||||||
|
|
||||||
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
|
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
|
||||||
|
|
||||||
notif.setLatestEventInfo(getApplication(), header, notice, pi);
|
notif.setLatestEventInfo(getApplication(), header, notice, pi);
|
||||||
notif.flags = Notification.FLAG_ONGOING_EVENT;
|
notif.flags = Notification.FLAG_ONGOING_EVENT;
|
||||||
|
|
||||||
notifMgr.notify(K9.CONNECTIVITY_ID, notif);
|
notifMgr.notify(K9.CONNECTIVITY_ID, notif);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
notifMgr.cancel(K9.CONNECTIVITY_ID);
|
notifMgr.cancel(K9.CONNECTIVITY_ID);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDestroy()
|
public void onDestroy()
|
||||||
{
|
{
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
Log.v(K9.LOG_TAG, "***** MailService *****: onDestroy()");
|
Log.v(K9.LOG_TAG, "***** MailService *****: onDestroy()");
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
// MessagingController.getInstance(getApplication()).removeListener(mListener);
|
// MessagingController.getInstance(getApplication()).removeListener(mListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void cancel()
|
private void cancel()
|
||||||
{
|
{
|
||||||
Intent i = new Intent();
|
Intent i = new Intent();
|
||||||
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService");
|
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService");
|
||||||
i.setAction(ACTION_CHECK_MAIL);
|
i.setAction(ACTION_CHECK_MAIL);
|
||||||
BootReceiver.cancelIntent(this, i);
|
BootReceiver.cancelIntent(this, i);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reschedule(Integer startId)
|
private void reschedule(Integer startId)
|
||||||
{
|
{
|
||||||
execute(getApplication(), new Runnable()
|
execute(getApplication(), new Runnable()
|
||||||
{
|
{
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
int shortestInterval = -1;
|
int shortestInterval = -1;
|
||||||
|
|
||||||
for (Account account : Preferences.getPreferences(MailService.this).getAccounts())
|
for (Account account : Preferences.getPreferences(MailService.this).getAccounts())
|
||||||
{
|
{
|
||||||
if (account.getAutomaticCheckIntervalMinutes() != -1
|
if (account.getAutomaticCheckIntervalMinutes() != -1
|
||||||
&& (account.getAutomaticCheckIntervalMinutes() < shortestInterval || shortestInterval == -1))
|
&& (account.getAutomaticCheckIntervalMinutes() < shortestInterval || shortestInterval == -1))
|
||||||
{
|
{
|
||||||
shortestInterval = account.getAutomaticCheckIntervalMinutes();
|
shortestInterval = account.getAutomaticCheckIntervalMinutes();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shortestInterval == -1)
|
if (shortestInterval == -1)
|
||||||
{
|
{
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
Log.v(K9.LOG_TAG, "No next check scheduled for package " + getApplication().getPackageName());
|
Log.v(K9.LOG_TAG, "No next check scheduled for package " + getApplication().getPackageName());
|
||||||
cancel();
|
cancel();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
long delay = (shortestInterval * (60 * 1000));
|
long delay = (shortestInterval * (60 * 1000));
|
||||||
|
|
||||||
long nextTime = System.currentTimeMillis() + delay;
|
long nextTime = System.currentTimeMillis() + delay;
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Log.i(K9.LOG_TAG,
|
Log.i(K9.LOG_TAG,
|
||||||
@ -304,172 +304,173 @@ public class MailService extends CoreService
|
|||||||
// I once got a NullPointerException deep in new Date();
|
// I once got a NullPointerException deep in new Date();
|
||||||
Log.e(K9.LOG_TAG, "Exception while logging", e);
|
Log.e(K9.LOG_TAG, "Exception while logging", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Intent i = new Intent();
|
Intent i = new Intent();
|
||||||
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService");
|
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService");
|
||||||
i.setAction(ACTION_CHECK_MAIL);
|
i.setAction(ACTION_CHECK_MAIL);
|
||||||
BootReceiver.scheduleIntent(MailService.this, nextTime, i);
|
BootReceiver.scheduleIntent(MailService.this, nextTime, i);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stopPushers(final Integer startId)
|
private void stopPushers(final Integer startId)
|
||||||
{
|
{
|
||||||
execute(getApplication(), new Runnable()
|
execute(getApplication(), new Runnable()
|
||||||
{
|
{
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
MessagingController.getInstance(getApplication()).stopAllPushing();
|
MessagingController.getInstance(getApplication()).stopAllPushing();
|
||||||
PushService.stopService(MailService.this);
|
PushService.stopService(MailService.this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void reschedulePushers(final Integer startId)
|
private void reschedulePushers(final Integer startId)
|
||||||
{
|
{
|
||||||
execute(getApplication(), new Runnable()
|
execute(getApplication(), new Runnable()
|
||||||
{
|
{
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
|
|
||||||
Log.i(K9.LOG_TAG, "Rescheduling pushers");
|
Log.i(K9.LOG_TAG, "Rescheduling pushers");
|
||||||
stopPushers(null);
|
stopPushers(null);
|
||||||
setupPushers(null);
|
setupPushers(null);
|
||||||
schedulePushers(startId);
|
schedulePushers(startId);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, null);
|
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupPushers(final Integer startId)
|
private void setupPushers(final Integer startId)
|
||||||
{
|
{
|
||||||
execute(getApplication(), new Runnable()
|
execute(getApplication(), new Runnable()
|
||||||
{
|
{
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
boolean pushing = false;
|
boolean pushing = false;
|
||||||
for (Account account : Preferences.getPreferences(MailService.this).getAccounts())
|
for (Account account : Preferences.getPreferences(MailService.this).getAccounts())
|
||||||
{
|
{
|
||||||
Log.i(K9.LOG_TAG, "Setting up pushers for account " + account.getDescription());
|
Log.i(K9.LOG_TAG, "Setting up pushers for account " + account.getDescription());
|
||||||
pushing |= MessagingController.getInstance(getApplication()).setupPushing(account);
|
pushing |= MessagingController.getInstance(getApplication()).setupPushing(account);
|
||||||
}
|
}
|
||||||
if (pushing)
|
if (pushing)
|
||||||
{
|
{
|
||||||
PushService.startService(MailService.this);
|
PushService.startService(MailService.this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void refreshPushers(final Integer startId)
|
private void refreshPushers(final Integer startId)
|
||||||
{
|
{
|
||||||
execute(getApplication(), new Runnable()
|
execute(getApplication(), new Runnable()
|
||||||
{
|
{
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Log.i(K9.LOG_TAG, "Refreshing pushers");
|
Log.i(K9.LOG_TAG, "Refreshing pushers");
|
||||||
Collection<Pusher> pushers = MessagingController.getInstance(getApplication()).getPushers();
|
Collection<Pusher> pushers = MessagingController.getInstance(getApplication()).getPushers();
|
||||||
for (Pusher pusher : pushers)
|
for (Pusher pusher : pushers)
|
||||||
{
|
{
|
||||||
pusher.refresh();
|
pusher.refresh();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Log.e(K9.LOG_TAG, "Exception while refreshing pushers", e);
|
Log.e(K9.LOG_TAG, "Exception while refreshing pushers", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void schedulePushers(final Integer startId)
|
private void schedulePushers(final Integer startId)
|
||||||
{
|
{
|
||||||
execute(getApplication(), new Runnable()
|
execute(getApplication(), new Runnable()
|
||||||
{
|
{
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
int minInterval = -1;
|
int minInterval = -1;
|
||||||
|
|
||||||
Collection<Pusher> pushers = MessagingController.getInstance(getApplication()).getPushers();
|
Collection<Pusher> pushers = MessagingController.getInstance(getApplication()).getPushers();
|
||||||
for (Pusher pusher : pushers)
|
for (Pusher pusher : pushers)
|
||||||
{
|
{
|
||||||
int interval = pusher.getRefreshInterval();
|
int interval = pusher.getRefreshInterval();
|
||||||
if (interval != -1 && (interval < minInterval || minInterval == -1))
|
if (interval != -1 && (interval < minInterval || minInterval == -1))
|
||||||
{
|
{
|
||||||
minInterval = interval;
|
minInterval = interval;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
{
|
{
|
||||||
Log.v(K9.LOG_TAG, "Pusher refresh interval = " + minInterval);
|
Log.v(K9.LOG_TAG, "Pusher refresh interval = " + minInterval);
|
||||||
}
|
}
|
||||||
if (minInterval != -1)
|
if (minInterval != -1)
|
||||||
{
|
{
|
||||||
long nextTime = System.currentTimeMillis() + minInterval;
|
long nextTime = System.currentTimeMillis() + minInterval;
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
{
|
{
|
||||||
Log.d(K9.LOG_TAG, "Next pusher refresh scheduled for " + new Date(nextTime));
|
Log.d(K9.LOG_TAG, "Next pusher refresh scheduled for " + new Date(nextTime));
|
||||||
}
|
}
|
||||||
Intent i = new Intent();
|
Intent i = new Intent();
|
||||||
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService");
|
i.setClassName(getApplication().getPackageName(), "com.fsck.k9.service.MailService");
|
||||||
i.setAction(ACTION_REFRESH_PUSHERS);
|
i.setAction(ACTION_REFRESH_PUSHERS);
|
||||||
BootReceiver.scheduleIntent(MailService.this, nextTime, i);
|
BootReceiver.scheduleIntent(MailService.this, nextTime, i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
, K9.MAIL_SERVICE_WAKE_LOCK_TIMEOUT, startId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void execute(Context context, final Runnable runner, int wakeLockTime, final Integer startId)
|
public void execute(Context context, final Runnable runner, int wakeLockTime, final Integer startId)
|
||||||
{
|
{
|
||||||
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||||
final WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9");
|
final WakeLock wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9");
|
||||||
wakeLock.setReferenceCounted(false);
|
wakeLock.setReferenceCounted(false);
|
||||||
wakeLock.acquire(wakeLockTime);
|
wakeLock.acquire(wakeLockTime);
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
Log.i(K9.LOG_TAG, "MailService queueing Runnable " + runner.hashCode() + " with startId " + startId);
|
Log.i(K9.LOG_TAG, "MailService queueing Runnable " + runner.hashCode() + " with startId " + startId);
|
||||||
|
|
||||||
Runnable myRunner = new Runnable()
|
Runnable myRunner = new Runnable()
|
||||||
{
|
{
|
||||||
public void run()
|
public void run()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
Log.i(K9.LOG_TAG, "MailService running Runnable " + runner.hashCode() + " with startId " + startId);
|
Log.i(K9.LOG_TAG, "MailService running Runnable " + runner.hashCode() + " with startId " + startId);
|
||||||
runner.run();
|
runner.run();
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
{
|
{
|
||||||
if (K9.DEBUG)
|
if (K9.DEBUG)
|
||||||
Log.i(K9.LOG_TAG, "MailService completed Runnable " + runner.hashCode() + " with startId " + startId);
|
Log.i(K9.LOG_TAG, "MailService completed Runnable " + runner.hashCode() + " with startId " + startId);
|
||||||
wakeLock.release();
|
wakeLock.release();
|
||||||
if (startId != null)
|
|
||||||
{
|
if (startId != null)
|
||||||
stopSelf(startId);
|
{
|
||||||
}
|
stopSelf(startId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
|
||||||
|
};
|
||||||
threadPool.execute(myRunner);
|
|
||||||
}
|
threadPool.execute(myRunner);
|
||||||
|
}
|
||||||
public IBinder onBind(Intent intent)
|
|
||||||
{
|
public IBinder onBind(Intent intent)
|
||||||
return null;
|
{
|
||||||
}
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user