mirror of
https://github.com/moparisthebest/k-9
synced 2024-12-24 08:38:51 -05:00
. More reliable background sync by using wake lock
--> Background sync now work even when the device is asleep (Thanks danapple0)
This commit is contained in:
parent
d55e335f48
commit
1e67bbee6b
@ -8,6 +8,7 @@
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
|
||||
<uses-permission android:name="android.permission.INTERNET"/>
|
||||
<uses-permission android:name="android.permission.VIBRATE"/>
|
||||
<uses-permission android:name="android.permission.WAKE_LOCK" />
|
||||
<permission android:name="com.android.email.permission.READ_ATTACHMENT"
|
||||
android:permissionGroup="android.permission-group.MESSAGES"
|
||||
android:protectionLevel="dangerous"
|
||||
|
@ -99,6 +99,11 @@ public class Email extends Application {
|
||||
* 6.8MB downloaded but only 5MB saved.
|
||||
*/
|
||||
public static final int MAX_ATTACHMENT_DOWNLOAD_SIZE = (5 * 1024 * 1024);
|
||||
|
||||
/**
|
||||
* Max time (in millis) the wake lock will be held for when background sync is happening
|
||||
*/
|
||||
public static final int WAKE_LOCK_TIMEOUT = 30000;
|
||||
|
||||
/**
|
||||
* Called throughout the application when the number of accounts has changed. This method
|
||||
|
@ -11,6 +11,8 @@ import android.app.Service;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.IBinder;
|
||||
import android.os.PowerManager;
|
||||
import android.os.PowerManager.WakeLock;
|
||||
import android.os.SystemClock;
|
||||
import android.util.Config;
|
||||
import android.util.Log;
|
||||
@ -61,6 +63,7 @@ public class MailService extends Service {
|
||||
if (Config.LOGV) {
|
||||
Log.v(Email.LOG_TAG, "***** MailService *****: checking mail");
|
||||
}
|
||||
mListener.wakeLockAcquire();
|
||||
MessagingController.getInstance(getApplication()).checkMail(this, null, mListener);
|
||||
}
|
||||
else if (ACTION_CANCEL.equals(intent.getAction())) {
|
||||
@ -124,6 +127,25 @@ public class MailService extends Service {
|
||||
|
||||
class Listener extends MessagingListener {
|
||||
HashMap<Account, Integer> accountsWithNewMail = new HashMap<Account, Integer>();
|
||||
private WakeLock wakeLock = null;
|
||||
|
||||
// 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() {
|
||||
if (wakeLock == null) {
|
||||
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Email");
|
||||
wakeLock.setReferenceCounted(false);
|
||||
wakeLock.acquire(Email.WAKE_LOCK_TIMEOUT);
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized void wakeLockRelease() {
|
||||
if (wakeLock != null) {
|
||||
wakeLock.release();
|
||||
wakeLock = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void checkMailStarted(Context context, Account account) {
|
||||
@ -133,6 +155,7 @@ public class MailService extends Service {
|
||||
@Override
|
||||
public void checkMailFailed(Context context, Account account, String reason) {
|
||||
reschedule();
|
||||
wakeLockRelease();
|
||||
stopSelf(mStartId);
|
||||
}
|
||||
|
||||
@ -187,6 +210,7 @@ public class MailService extends Service {
|
||||
}
|
||||
|
||||
reschedule();
|
||||
wakeLockRelease();
|
||||
stopSelf(mStartId);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user