diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index a840ca8387..d1af305b46 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -35,6 +35,7 @@ import android.util.Printer; import android.webkit.CookieManager; import androidx.preference.PreferenceManager; +import androidx.work.WorkManager; import org.jetbrains.annotations.NotNull; @@ -177,7 +178,9 @@ public class ApplicationEx extends Application ServiceSynchronize.watchdog(this); ServiceSend.watchdog(this); - WorkerWatchdog.init(this); + ServiceSynchronize.scheduleWatchdog(this); + WorkManager.getInstance(this).cancelUniqueWork("WorkerWatchdog"); + WorkerCleanup.init(this); registerReceiver(onScreenOff, new IntentFilter(Intent.ACTION_SCREEN_OFF)); @@ -192,7 +195,7 @@ public class ApplicationEx extends Application case "enabled": ServiceSynchronize.reschedule(this); WorkerCleanup.init(this); - WorkerWatchdog.init(this); + ServiceSynchronize.scheduleWatchdog(this); WidgetSync.update(this); break; case "poll_interval": @@ -209,7 +212,7 @@ public class ApplicationEx extends Application ServiceSynchronize.reschedule(this); break; case "watchdog": - WorkerWatchdog.init(this); + ServiceSynchronize.scheduleWatchdog(this); break; case "secure": // privacy case "shortcuts": // misc diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 606b6b599b..5c7bb52de4 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -129,6 +129,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences private static final int ACCOUNT_ERROR_AFTER_POLL = 4; // times private static final int FAST_FAIL_THRESHOLD = 75; // percent private static final int FETCH_YIELD_DURATION = 50; // milliseconds + private static final long WATCHDOG_INTERVAL = 60 * 60 * 1000L; // milliseconds private static final String ACTION_NEW_MESSAGE_COUNT = BuildConfig.APPLICATION_ID + ".NEW_MESSAGE_COUNT"; @@ -154,6 +155,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences static final int PI_KEEPALIVE = 3; static final int PI_ENABLE = 4; static final int PI_POLL = 5; + static final int PI_WATCHDOG = 6; @Override public void onCreate() { @@ -937,6 +939,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences if (lastNetworkState == null || !lastNetworkState.isSuitable()) updateNetworkState(null, "watchdog"); + + ServiceSend.boot(this); + + scheduleWatchdog(this); } private NotificationCompat.Builder getNotificationService(Integer accounts, Integer operations) { @@ -2472,6 +2478,28 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences return new long[]{start, end}; } + static void scheduleWatchdog(Context context) { + Intent intent = new Intent(context, ServiceSynchronize.class) + .setAction("watchdog"); + PendingIntent pi; + if (isBackgroundService(context)) + pi = PendingIntentCompat.getService(context, PI_WATCHDOG, intent, PendingIntent.FLAG_UPDATE_CURRENT); + else + pi = PendingIntentCompat.getForegroundService(context, PI_WATCHDOG, intent, PendingIntent.FLAG_UPDATE_CURRENT); + + AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + boolean watchdog = prefs.getBoolean("watchdog", true); + boolean enabled = prefs.getBoolean("enabled", true); + if (watchdog && enabled) { + long now = new Date().getTime(); + long trigger = (now / WATCHDOG_INTERVAL) * WATCHDOG_INTERVAL + WATCHDOG_INTERVAL; + Log.i("Sync watchdog at " + new Date(trigger)); + AlarmManagerCompat.setAndAllowWhileIdle(am, AlarmManager.RTC_WAKEUP, trigger, pi); // exact + } else + am.cancel(pi); + } + static void eval(Context context, String reason) { start(context, new Intent(context, ServiceSynchronize.class) diff --git a/app/src/main/java/eu/faircode/email/WorkerWatchdog.java b/app/src/main/java/eu/faircode/email/WorkerWatchdog.java deleted file mode 100644 index fd56b1fe7e..0000000000 --- a/app/src/main/java/eu/faircode/email/WorkerWatchdog.java +++ /dev/null @@ -1,81 +0,0 @@ -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 . - - Copyright 2018-2021 by Marcel Bokhorst (M66B) -*/ - -import android.content.Context; -import android.content.SharedPreferences; - -import androidx.annotation.NonNull; -import androidx.preference.PreferenceManager; -import androidx.work.ExistingPeriodicWorkPolicy; -import androidx.work.PeriodicWorkRequest; -import androidx.work.WorkManager; -import androidx.work.Worker; -import androidx.work.WorkerParameters; - -import java.util.concurrent.TimeUnit; - -public class WorkerWatchdog extends Worker { - private static final int WATCHDOG_INTERVAL = 60; // minutes - - public WorkerWatchdog(@NonNull Context context, @NonNull WorkerParameters workerParams) { - super(context, workerParams); - Log.i("Instance " + getName()); - } - - @NonNull - @Override - public Result doWork() { - Log.i("Running " + getName()); - ServiceSynchronize.watchdog(getApplicationContext()); - ServiceSend.watchdog(getApplicationContext()); - return Result.success(); - } - - static void init(Context context) { - try { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - boolean watchdog = prefs.getBoolean("watchdog", true); - boolean enabled = prefs.getBoolean("enabled", true); - if (watchdog && enabled) { - Log.i("Queuing " + getName() + " every " + WATCHDOG_INTERVAL + " minutes"); - - PeriodicWorkRequest workRequest = - new PeriodicWorkRequest.Builder(WorkerWatchdog.class, WATCHDOG_INTERVAL, TimeUnit.MINUTES) - .build(); - WorkManager.getInstance(context) - .enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.REPLACE, workRequest); - - Log.i("Queued " + getName()); - } else { - Log.i("Cancelling " + getName()); - WorkManager.getInstance(context).cancelUniqueWork(getName()); - Log.i("Cancelled " + getName()); - } - } catch (IllegalStateException ex) { - // https://issuetracker.google.com/issues/138465476 - Log.w(ex); - } - } - - private static String getName() { - return WorkerWatchdog.class.getSimpleName(); - } -}