1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-14 05:25:07 -05:00
k-9/src/com/fsck/k9/service/PollService.java

175 lines
5.2 KiB
Java
Raw Normal View History

package com.fsck.k9.service;
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;
import com.fsck.k9.*;
import java.util.HashMap;
public class PollService extends CoreService
{
private static String START_SERVICE = "com.fsck.k9.service.PollService.startService";
private static String STOP_SERVICE = "com.fsck.k9.service.PollService.stopService";
private Listener mListener = new Listener();
public static void startService(Context context)
{
Intent i = new Intent();
i.setClass(context, PollService.class);
i.setAction(PollService.START_SERVICE);
addWakeLock(context, i);
context.startService(i);
}
public static void stopService(Context context)
{
Intent i = new Intent();
i.setClass(context, PollService.class);
i.setAction(PollService.STOP_SERVICE);
addWakeLock(context, i);
context.startService(i);
}
@Override
public void startService(Intent intent, int startId)
{
if (START_SERVICE.equals(intent.getAction()))
{
2010-01-02 20:50:32 -05:00
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "PollService started with startId = " + startId);
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");
mListener.setStartId(startId);
mListener.wakeLockAcquire();
controller.setCheckMailListener(mListener);
controller.checkMail(this, null, false, false, mListener);
}
else
{
2010-01-02 20:50:32 -05:00
if (K9.DEBUG)
Log.i(K9.LOG_TAG,"***** PollService *****: renewing WakeLock");
listener.setStartId(startId);
listener.wakeLockAcquire();
}
}
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");
stopSelf();
}
}
@Override
public IBinder onBind(Intent arg0)
{
return null;
}
class Listener extends MessagingListener
{
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()
{
WakeLock oldWakeLock = wakeLock;
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9");
wakeLock.setReferenceCounted(false);
wakeLock.acquire(K9.WAKE_LOCK_TIMEOUT);
if (oldWakeLock != null)
{
oldWakeLock.release();
}
}
public synchronized void wakeLockRelease()
{
if (wakeLock != null)
{
wakeLock.release();
wakeLock = null;
}
}
@Override
public void checkMailStarted(Context context, Account account)
{
accountsChecked.clear();
}
@Override
public void checkMailFailed(Context context, Account account, String reason)
{
release();
}
@Override
public void synchronizeMailboxFinished(
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);
}
}
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
MessagingController controller = MessagingController.getInstance(getApplication());
controller.setCheckMailListener(null);
2010-04-05 22:38:22 -04:00
MailService.saveLastCheckEnd(getApplication());
MailService.actionReschedulePoll(PollService.this, null);
wakeLockRelease();
2010-01-02 20:50:32 -05:00
if (K9.DEBUG)
Log.i(K9.LOG_TAG, "PollService stopping with startId = " + startId);
stopSelf(startId);
}
@Override
public void checkMailFinished(Context context, Account account)
{
2010-01-02 20:50:32 -05:00
if (K9.DEBUG)
Log.v(K9.LOG_TAG, "***** PollService *****: checkMailFinished");
release();
}
public int getStartId()
{
return startId;
}
public void setStartId(int startId)
{
this.startId = startId;
}
}
}