2009-12-14 21:50:53 -05:00
|
|
|
package com.fsck.k9;
|
2009-11-28 09:51:44 -05:00
|
|
|
|
|
|
|
import android.app.Application;
|
|
|
|
import android.content.Context;
|
|
|
|
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.mail.Folder;
|
|
|
|
import com.fsck.k9.mail.Folder.OpenMode;
|
|
|
|
import com.fsck.k9.mail.Message;
|
|
|
|
import com.fsck.k9.mail.PushReceiver;
|
|
|
|
import com.fsck.k9.mail.store.LocalStore;
|
|
|
|
import com.fsck.k9.mail.store.LocalStore.LocalFolder;
|
|
|
|
import com.fsck.k9.service.SleepService;
|
2009-11-28 09:51:44 -05:00
|
|
|
|
2009-12-09 22:16:42 -05:00
|
|
|
import java.util.List;
|
2010-04-26 23:02:17 -04:00
|
|
|
import java.util.concurrent.CountDownLatch;
|
2009-12-09 22:16:42 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
public class MessagingControllerPushReceiver implements PushReceiver
|
|
|
|
{
|
|
|
|
final Account account;
|
|
|
|
final MessagingController controller;
|
|
|
|
final Application mApplication;
|
2009-12-06 19:56:06 -05:00
|
|
|
|
2009-11-28 09:51:44 -05:00
|
|
|
public MessagingControllerPushReceiver(Application nApplication, Account nAccount, MessagingController nController)
|
|
|
|
{
|
|
|
|
account = nAccount;
|
|
|
|
controller = nController;
|
|
|
|
mApplication = nApplication;
|
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
ThreadLocal<WakeLock> threadWakeLock = new ThreadLocal<WakeLock>();
|
|
|
|
public void acquireWakeLock()
|
|
|
|
{
|
|
|
|
WakeLock wakeLock = threadWakeLock.get();
|
|
|
|
if (wakeLock == null)
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
2009-12-06 19:56:06 -05:00
|
|
|
PowerManager pm = (PowerManager) mApplication.getSystemService(Context.POWER_SERVICE);
|
2009-12-14 21:50:53 -05:00
|
|
|
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "K9");
|
2009-12-06 19:56:06 -05:00
|
|
|
wakeLock.setReferenceCounted(false);
|
|
|
|
threadWakeLock.set(wakeLock);
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
2009-12-14 21:50:53 -05:00
|
|
|
wakeLock.acquire(K9.PUSH_WAKE_LOCK_TIMEOUT);
|
2010-01-02 20:50:41 -05:00
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Acquired WakeLock for Pushing for thread " + Thread.currentThread().getName());
|
2009-12-06 19:56:06 -05:00
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
|
2009-12-06 19:56:06 -05:00
|
|
|
public void releaseWakeLock()
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Considering releasing WakeLock for Pushing");
|
2010-01-02 20:50:41 -05:00
|
|
|
|
2009-12-06 19:56:06 -05:00
|
|
|
WakeLock wakeLock = threadWakeLock.get();
|
|
|
|
if (wakeLock != null)
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
|
|
|
|
2009-12-14 21:50:53 -05:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.d(K9.LOG_TAG, "Releasing WakeLock for Pushing for thread " + Thread.currentThread().getName());
|
2010-01-02 20:50:41 -05:00
|
|
|
|
2009-12-06 19:56:06 -05:00
|
|
|
wakeLock.release();
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
else
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "No WakeLock waiting to be released for thread " + Thread.currentThread().getName());
|
2009-12-06 19:56:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void messagesFlagsChanged(Folder folder,
|
|
|
|
List<Message> messages)
|
|
|
|
{
|
|
|
|
controller.messagesArrived(account, folder, messages, true);
|
|
|
|
|
|
|
|
}
|
|
|
|
public void messagesArrived(Folder folder, List<Message> messages)
|
|
|
|
{
|
|
|
|
controller.messagesArrived(account, folder, messages, false);
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-04-26 23:02:17 -04:00
|
|
|
public void syncFolder(Folder folder)
|
|
|
|
{
|
2010-04-27 01:00:08 -04:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "syncFolder(" + folder.getName() + ")");
|
2010-04-26 23:02:17 -04:00
|
|
|
final CountDownLatch latch = new CountDownLatch(1);
|
|
|
|
controller.synchronizeMailbox(account, folder.getName(), new MessagingListener()
|
|
|
|
{
|
2010-05-03 09:46:55 -04:00
|
|
|
@Override
|
2010-04-26 23:02:17 -04:00
|
|
|
public void synchronizeMailboxFinished(Account account, String folder,
|
2010-04-29 00:59:14 -04:00
|
|
|
int totalMessagesInMailbox, int numNewMessages)
|
2010-04-26 23:02:17 -04:00
|
|
|
{
|
|
|
|
latch.countDown();
|
|
|
|
}
|
2010-04-29 00:59:14 -04:00
|
|
|
|
2010-05-03 09:46:55 -04:00
|
|
|
@Override
|
2010-04-26 23:02:17 -04:00
|
|
|
public void synchronizeMailboxFailed(Account account, String folder,
|
2010-04-29 00:59:14 -04:00
|
|
|
String message)
|
2010-04-26 23:02:17 -04:00
|
|
|
{
|
|
|
|
latch.countDown();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-04-27 01:00:08 -04:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "syncFolder(" + folder.getName() + ") about to await latch release");
|
2010-04-26 23:02:17 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
latch.await();
|
2010-04-27 01:00:08 -04:00
|
|
|
if (K9.DEBUG)
|
|
|
|
Log.v(K9.LOG_TAG, "syncFolder(" + folder.getName() + ") got latch release");
|
2010-04-26 23:02:17 -04:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Log.e(K9.LOG_TAG, "Interrupted while awaiting latch release", e);
|
|
|
|
}
|
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
|
|
|
|
public void sleep(long millis)
|
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
SleepService.sleep(mApplication, millis, threadWakeLock.get(), K9.PUSH_WAKE_LOCK_TIMEOUT);
|
2009-12-06 19:56:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void pushError(String errorMessage, Exception e)
|
|
|
|
{
|
|
|
|
String errMess = errorMessage;
|
2009-11-28 09:51:44 -05:00
|
|
|
|
2009-12-06 19:56:06 -05:00
|
|
|
if (errMess == null && e != null)
|
|
|
|
{
|
|
|
|
errMess = e.getMessage();
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
2010-04-14 23:17:25 -04:00
|
|
|
controller.addErrorMessage(account, errMess, e);
|
2009-12-06 19:56:06 -05:00
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
|
2009-12-06 19:56:06 -05:00
|
|
|
public String getPushState(String folderName)
|
|
|
|
{
|
|
|
|
LocalFolder localFolder = null;
|
|
|
|
try
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
2010-03-03 23:00:30 -05:00
|
|
|
LocalStore localStore = account.getLocalStore();
|
|
|
|
localFolder = localStore.getFolder(folderName);
|
2009-12-06 19:56:06 -05:00
|
|
|
localFolder.open(OpenMode.READ_WRITE);
|
|
|
|
return localFolder.getPushState();
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
catch (Exception e)
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
2009-12-14 21:50:53 -05:00
|
|
|
Log.e(K9.LOG_TAG, "Unable to get push state from account " + account.getDescription()
|
2009-12-06 19:56:06 -05:00
|
|
|
+ ", folder " + folderName, e);
|
|
|
|
return null;
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
finally
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
2009-12-06 19:56:06 -05:00
|
|
|
if (localFolder != null)
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
2010-01-16 11:22:20 -05:00
|
|
|
localFolder.close();
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
|
2009-12-06 19:56:06 -05:00
|
|
|
public void setPushActive(String folderName, boolean enabled)
|
|
|
|
{
|
|
|
|
for (MessagingListener l : controller.getListeners())
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
2009-12-06 19:56:06 -05:00
|
|
|
l.setPushActive(account, folderName, enabled);
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
}
|
2009-11-28 09:51:44 -05:00
|
|
|
|
|
|
|
}
|