2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9.service;
|
2009-11-22 12:01:04 -05:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.IBinder;
|
|
|
|
import android.os.PowerManager;
|
|
|
|
import android.os.PowerManager.WakeLock;
|
|
|
|
import android.util.Log;
|
2009-12-14 21:50:53 -05:00
|
|
|
import com.fsck.k9.*;
|
2009-12-09 22:16:42 -05:00
|
|
|
|
|
|
|
import java.util.HashMap;
|
2009-11-22 12:01:04 -05:00
|
|
|
|
|
|
|
public class PollService extends CoreService
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
private static String START_SERVICE = "com.fsck.k9.service.PollService.startService";
|
|
|
|
private static String STOP_SERVICE = "com.fsck.k9.service.PollService.stopService";
|
2009-11-22 12:01:04 -05:00
|
|
|
|
|
|
|
private Listener mListener = new Listener();
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
public static void startService(Context context)
|
|
|
|
{
|
2009-11-22 12:01:04 -05:00
|
|
|
Intent i = new Intent();
|
|
|
|
i.setClass(context, PollService.class);
|
|
|
|
i.setAction(PollService.START_SERVICE);
|
2009-12-20 20:48:15 -05:00
|
|
|
addWakeLock(context, i);
|
2009-11-22 12:01:04 -05:00
|
|
|
context.startService(i);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
public static void stopService(Context context)
|
|
|
|
{
|
2009-11-22 12:01:04 -05:00
|
|
|
Intent i = new Intent();
|
|
|
|
i.setClass(context, PollService.class);
|
|
|
|
i.setAction(PollService.STOP_SERVICE);
|
2009-12-20 20:48:15 -05:00
|
|
|
addWakeLock(context, i);
|
2009-11-22 12:01:04 -05:00
|
|
|
context.startService(i);
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-22 12:01:04 -05:00
|
|
|
@Override
|
|
|
|
public void startService(Intent intent, int startId)
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
if (START_SERVICE.equals(intent.getAction()))
|
2009-11-22 12:01:04 -05:00
|
|
|
{
|
2010-01-02 20:50:32 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "PollService started with startId = " + startId);
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-22 12:01:04 -05:00
|
|
|
MessagingController controller = MessagingController.getInstance(getApplication());
|
|
|
|
Listener listener = (Listener)controller.getCheckMailListener();
|
|
|
|
if (listener == null)
|
|
|
|
{
|
2010-01-02 20:50:32 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "***** PollService *****: starting new check");
|
2009-11-24 19:40:29 -05:00
|
|
|
mListener.setStartId(startId);
|
|
|
|
mListener.wakeLockAcquire();
|
|
|
|
controller.setCheckMailListener(mListener);
|
|
|
|
controller.checkMail(this, null, false, false, mListener);
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-01-02 20:50:32 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG,"***** PollService *****: renewing WakeLock");
|
2009-11-24 19:40:29 -05:00
|
|
|
listener.setStartId(startId);
|
|
|
|
listener.wakeLockAcquire();
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (STOP_SERVICE.equals(intent.getAction()))
|
|
|
|
{
|
2010-01-02 20:50:32 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "PollService stopping");
|
2009-11-22 12:01:04 -05:00
|
|
|
stopSelf();
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-22 12:01:04 -05:00
|
|
|
@Override
|
|
|
|
public IBinder onBind(Intent arg0)
|
|
|
|
{
|
|
|
|
return null;
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
class Listener extends MessagingListener
|
|
|
|
{
|
2009-11-22 12:01:04 -05:00
|
|
|
HashMap<String, Integer> accountsChecked = new HashMap<String, Integer>();
|
|
|
|
private WakeLock wakeLock = null;
|
|
|
|
private int startId = -1;
|
|
|
|
|
|
|
|
// wakelock strategy is to be very conservative. If there is any reason to release, then release
|
|
|
|
// don't want to take the chance of running wild
|
|
|
|
public synchronized void wakeLockAcquire()
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
WakeLock oldWakeLock = wakeLock;
|
2009-11-22 12:01:04 -05:00
|
|
|
|
|
|
|
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
2009-12-14 21:50:53 -05:00
|
|
|
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9");
|
2009-11-22 12:01:04 -05:00
|
|
|
wakeLock.setReferenceCounted(false);
|
2009-12-14 21:50:53 -05:00
|
|
|
wakeLock.acquire(K9.WAKE_LOCK_TIMEOUT);
|
2009-11-22 12:01:04 -05:00
|
|
|
|
|
|
|
if (oldWakeLock != null)
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
oldWakeLock.release();
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
public synchronized void wakeLockRelease()
|
|
|
|
{
|
|
|
|
if (wakeLock != null)
|
|
|
|
{
|
|
|
|
wakeLock.release();
|
|
|
|
wakeLock = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void checkMailStarted(Context context, Account account)
|
|
|
|
{
|
2009-11-22 12:01:04 -05:00
|
|
|
accountsChecked.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void checkMailFailed(Context context, Account account, String reason)
|
|
|
|
{
|
2009-11-22 12:01:04 -05:00
|
|
|
release();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void synchronizeMailboxFinished(
|
2009-11-24 19:40:29 -05:00
|
|
|
Account account,
|
|
|
|
String folder,
|
|
|
|
int totalMessagesInMailbox,
|
|
|
|
int numNewMessages)
|
|
|
|
{
|
|
|
|
if (account.isNotifyNewMail())
|
|
|
|
{
|
|
|
|
Integer existingNewMessages = accountsChecked.get(account.getUuid());
|
|
|
|
if (existingNewMessages == null)
|
|
|
|
{
|
|
|
|
existingNewMessages = 0;
|
|
|
|
}
|
|
|
|
accountsChecked.put(account.getUuid(), existingNewMessages + numNewMessages);
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void release()
|
|
|
|
{
|
Far more advanced poll scheduler. Now it remembers that finish time
of the last successful periodic mail and computes the next start time
as an offset from that successful finish. The ramifications of this
new method is that changing polling interval on an account does not
force delaying all accounts to poll next in the future by the new
interval. Instead, K-9 Mail now adjusts the next poll time based on
what the next poll time should be based on the last poll finish and
the new interval.
Example 1: In the old way, if the old polling interval was 1 hour, and
the next poll was 50 minutes away (10 minutes have passed), and you
changed the interval to 15 minutes, the poll would happen 15 minutes
from now. In the new way, the next poll will happen only 5 minutes
from now, which is 15 minutes since the last poll.
Example 2: In the old way, if the old polling interval was 1 hour, and
the next poll was 10 minutes away (50 minutes have passed), and you
changed the interval to 30 minutes, the poll would happen 30 minutes
from now. The next poll would then happen actually 80 minutes after
the previous poll completed. In the new way, it'll actually happen
immediately, because the time for the next poll, based on the new
schedule, has already passed.
Similar scenarios happen when a loss of network connectivity
occurs. In the old way, polling would resume using the restoration of
connectivity as the starting point. Each time network connectivity
was lost and restored, the next poll would be further delayed. *If
connectivity was lost and restored frequently, a poll might never
happen!* In the new way, the next poll is rescheduled based on the
time of the last successful poll, so will be rescheduled just like it
was before the loss of connectivity. If the time has already been
passed, the poll will happen immediately.
2010-02-10 01:18:35 -05:00
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
MessagingController controller = MessagingController.getInstance(getApplication());
|
|
|
|
controller.setCheckMailListener(null);
|
2010-04-05 22:38:22 -04:00
|
|
|
MailService.saveLastCheckEnd(getApplication());
|
|
|
|
|
2010-02-07 16:23:33 -05:00
|
|
|
MailService.actionReschedulePoll(PollService.this, null);
|
2009-11-24 19:40:29 -05:00
|
|
|
wakeLockRelease();
|
2010-01-02 20:50:32 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.i(K9.LOG_TAG, "PollService stopping with startId = " + startId);
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
stopSelf(startId);
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2009-11-24 19:40:29 -05:00
|
|
|
public void checkMailFinished(Context context, Account account)
|
|
|
|
{
|
2009-11-22 12:01:04 -05:00
|
|
|
|
2010-01-02 20:50:32 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "***** PollService *****: checkMailFinished");
|
2010-01-21 13:49:11 -05:00
|
|
|
release();
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|
|
|
|
public int getStartId()
|
|
|
|
{
|
|
|
|
return startId;
|
|
|
|
}
|
|
|
|
public void setStartId(int startId)
|
|
|
|
{
|
|
|
|
this.startId = startId;
|
|
|
|
}
|
|
|
|
}
|
2009-11-24 19:40:29 -05:00
|
|
|
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|