mirror of
https://github.com/M66B/FairEmail.git
synced 2025-02-27 08:23:24 +00:00
Separate backoff/keepalive
This commit is contained in:
parent
65ab16870b
commit
71dfca6265
2 changed files with 63 additions and 23 deletions
39
app/src/main/java/eu/faircode/email/PendingIntentCompat.java
Normal file
39
app/src/main/java/eu/faircode/email/PendingIntentCompat.java
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
package eu.faircode.email;
|
||||||
|
|
||||||
|
/*
|
||||||
|
This file is part of FairEmail.
|
||||||
|
|
||||||
|
FairEmail is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
FairEmail is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with FairEmail. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
Copyright 2018-2019 by Marcel Bokhorst (M66B)
|
||||||
|
*/
|
||||||
|
|
||||||
|
import android.app.PendingIntent;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.content.Intent;
|
||||||
|
import android.os.Build;
|
||||||
|
|
||||||
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
public class PendingIntentCompat {
|
||||||
|
private PendingIntentCompat() {
|
||||||
|
}
|
||||||
|
|
||||||
|
static PendingIntent getForegroundService(Context context, int requestCode, @NonNull Intent intent, int flags) {
|
||||||
|
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
||||||
|
return PendingIntent.getService(context, requestCode, intent, flags);
|
||||||
|
else
|
||||||
|
return PendingIntent.getForegroundService(context, requestCode, intent, flags);
|
||||||
|
}
|
||||||
|
}
|
|
@ -119,7 +119,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
));
|
));
|
||||||
|
|
||||||
static final int PI_ALARM = 1;
|
static final int PI_ALARM = 1;
|
||||||
static final int PI_WAKEUP = 2;
|
static final int PI_BACKOFF = 2;
|
||||||
|
static final int PI_KEEPALIVE = 2;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
|
@ -562,7 +563,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
onReload(intent);
|
onReload(intent);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case "wakeup":
|
case "backoff":
|
||||||
|
case "keepalive":
|
||||||
onWakeup(intent);
|
onWakeup(intent);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -708,14 +710,6 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
try {
|
try {
|
||||||
wlAccount.acquire();
|
wlAccount.acquire();
|
||||||
|
|
||||||
Intent wakeup = new Intent(ServiceSynchronize.this, ServiceSynchronize.class);
|
|
||||||
wakeup.setAction("wakeup:" + account.id);
|
|
||||||
PendingIntent piWakeup;
|
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
|
||||||
piWakeup = PendingIntent.getService(this, PI_WAKEUP, wakeup, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
||||||
else
|
|
||||||
piWakeup = PendingIntent.getForegroundService(this, PI_WAKEUP, wakeup, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||||
if (account.notify)
|
if (account.notify)
|
||||||
account.createNotificationChannel(ServiceSynchronize.this);
|
account.createNotificationChannel(ServiceSynchronize.this);
|
||||||
|
@ -1177,13 +1171,18 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
nm.cancel("alert:" + account.id, 1);
|
nm.cancel("alert:" + account.id, 1);
|
||||||
|
|
||||||
// Schedule keep alive alarm
|
// Schedule keep alive alarm
|
||||||
|
Intent intent = new Intent(ServiceSynchronize.this, ServiceSynchronize.class);
|
||||||
|
intent.setAction("keepalive:" + account.id);
|
||||||
|
PendingIntent pi = PendingIntentCompat.getForegroundService(
|
||||||
|
this, PI_KEEPALIVE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||||
try {
|
try {
|
||||||
long duration = account.poll_interval * 60 * 1000L;
|
long duration = account.poll_interval * 60 * 1000L;
|
||||||
long trigger = System.currentTimeMillis() + duration;
|
long trigger = System.currentTimeMillis() + duration;
|
||||||
EntityLog.log(this, "### " + account.name + " keep alive" +
|
EntityLog.log(this, "### " + account.name + " keep alive" +
|
||||||
" wait=" + account.poll_interval + " until=" + new Date(trigger));
|
" wait=" + account.poll_interval + " until=" + new Date(trigger));
|
||||||
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, piWakeup);
|
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, pi);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
wlAccount.release();
|
wlAccount.release();
|
||||||
|
@ -1195,7 +1194,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
wlAccount.acquire();
|
wlAccount.acquire();
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
am.cancel(piWakeup);
|
am.cancel(pi);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1264,13 +1263,18 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Long back-off period, let device sleep
|
// Long back-off period, let device sleep
|
||||||
|
Intent intent = new Intent(ServiceSynchronize.this, ServiceSynchronize.class);
|
||||||
|
intent.setAction("backoff:" + account.id);
|
||||||
|
PendingIntent pi = PendingIntentCompat.getForegroundService(
|
||||||
|
this, PI_BACKOFF, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
|
|
||||||
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
|
||||||
try {
|
try {
|
||||||
long duration = CONNECT_BACKOFF_AlARM * 60 * 1000L;
|
long duration = CONNECT_BACKOFF_AlARM * 60 * 1000L;
|
||||||
long trigger = System.currentTimeMillis() + duration;
|
long trigger = System.currentTimeMillis() + duration;
|
||||||
EntityLog.log(this, "### " + account.name + " backoff" +
|
EntityLog.log(this, "### " + account.name + " backoff" +
|
||||||
" alarm=" + CONNECT_BACKOFF_AlARM + " until=" + new Date(trigger));
|
" alarm=" + CONNECT_BACKOFF_AlARM + " until=" + new Date(trigger));
|
||||||
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, piWakeup);
|
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, pi);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
wlAccount.release();
|
wlAccount.release();
|
||||||
|
@ -1282,7 +1286,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
wlAccount.acquire();
|
wlAccount.acquire();
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
am.cancel(piWakeup);
|
am.cancel(pi);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1463,16 +1467,13 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void schedule(Context context) {
|
private static void schedule(Context context) {
|
||||||
Intent alarm = new Intent(context, ServiceSynchronize.class);
|
Intent intent = new Intent(context, ServiceSynchronize.class);
|
||||||
alarm.setAction("alarm");
|
intent.setAction("alarm");
|
||||||
PendingIntent piAlarm;
|
PendingIntent pi = PendingIntentCompat.getForegroundService(
|
||||||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
|
context, PI_ALARM, intent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||||
piAlarm = PendingIntent.getService(context, PI_ALARM, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
||||||
else
|
|
||||||
piAlarm = PendingIntent.getForegroundService(context, PI_ALARM, alarm, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
||||||
|
|
||||||
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||||
am.cancel(piAlarm);
|
am.cancel(pi);
|
||||||
|
|
||||||
long[] schedule = getSchedule(context);
|
long[] schedule = getSchedule(context);
|
||||||
if (schedule == null)
|
if (schedule == null)
|
||||||
|
@ -1488,7 +1489,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
|
||||||
Log.i("Schedule next=" + new Date(next));
|
Log.i("Schedule next=" + new Date(next));
|
||||||
Log.i("Schedule enabled=" + enabled);
|
Log.i("Schedule enabled=" + enabled);
|
||||||
|
|
||||||
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, piAlarm);
|
AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, next, pi);
|
||||||
|
|
||||||
WorkerPoll.init(context);
|
WorkerPoll.init(context);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue