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()
|
|
|
|
{
|
2010-04-29 00:59:14 -04: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-04-29 00:59:14 -04:00
|
|
|
|
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
|
|
|
}
|