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
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
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)
|
|
|
|
{
|
Enhanced header in Accounts, MessageList and Folder to show the unread
count, scoped for the Activity, the in-progress operation, the account
on which the operation is in progress, the folder for the operation,
when appropriate, and the progress of the operation, when it applies
to multiple items. For the MessageList, also use the determinate
progress bar to show progress for synchronization of the folder being
displayed.
Fixes Issue 924.
Also, a minor change that might help with Issue 913, by putting the
insertion of the pending command into a background thread.
2009-12-19 19:02:46 -05:00
|
|
|
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
|
|
|
|
{
|
Enhanced header in Accounts, MessageList and Folder to show the unread
count, scoped for the Activity, the in-progress operation, the account
on which the operation is in progress, the folder for the operation,
when appropriate, and the progress of the operation, when it applies
to multiple items. For the MessageList, also use the determinate
progress bar to show progress for synchronization of the folder being
displayed.
Fixes Issue 924.
Also, a minor change that might help with Issue 913, by putting the
insertion of the pending command into a background thread.
2009-12-19 19:02:46 -05:00
|
|
|
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()))
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
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 checkMailDone(Context context, Account doNotUseaccount)
|
|
|
|
{
|
|
|
|
if (accountsChecked.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-11-24 19:40:29 -05:00
|
|
|
for (Account thisAccount : Preferences.getPreferences(context).getAccounts())
|
|
|
|
{
|
2009-11-22 12:01:04 -05:00
|
|
|
Integer newMailCount = accountsChecked.get(thisAccount.getUuid());
|
|
|
|
if (newMailCount != null)
|
|
|
|
{
|
2009-11-29 11:55:35 -05:00
|
|
|
MessagingController.getInstance(getApplication()).notifyAccount(context, thisAccount, newMailCount);
|
2009-11-22 12:01:04 -05:00
|
|
|
}
|
|
|
|
}//for accounts
|
|
|
|
}//checkMailDone
|
2009-11-24 19:40:29 -05:00
|
|
|
|
|
|
|
|
2009-11-22 12:01:04 -05:00
|
|
|
private void release()
|
|
|
|
{
|
2009-11-24 19:40:29 -05:00
|
|
|
MessagingController controller = MessagingController.getInstance(getApplication());
|
|
|
|
controller.setCheckMailListener(null);
|
|
|
|
MailService.rescheduleCheck(PollService.this, null);
|
|
|
|
wakeLockRelease();
|
2009-12-14 21:50:53 -05:00
|
|
|
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
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.v(K9.LOG_TAG, "***** PollService *****: checkMailFinished");
|
2009-11-22 12:01:04 -05:00
|
|
|
try
|
|
|
|
{
|
|
|
|
checkMailDone(context, account);
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2009-11-24 19:40:29 -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
|
|
|
}
|