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;
|
|
|
|
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;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
String body = null;
|
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
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
body = errMess;
|
|
|
|
if (e != null)
|
2009-11-28 09:51:44 -05:00
|
|
|
{
|
2009-12-06 19:56:06 -05:00
|
|
|
body = e.toString();
|
2009-11-28 09:51:44 -05:00
|
|
|
}
|
2009-12-06 19:56:06 -05:00
|
|
|
controller.addErrorMessage(account, errMess, body);
|
|
|
|
}
|
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
|
|
|
{
|
2009-12-06 19:56:06 -05:00
|
|
|
LocalStore localStore = (LocalStore) Store.getInstance(account.getLocalStoreUri(), mApplication);
|
|
|
|
localFolder= (LocalFolder) localStore.getFolder(folderName);
|
|
|
|
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
|
|
|
|
|
|
|
}
|