From a887657618e0ef1b2d098b1b577d0deb41be9755 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 5 Feb 2022 21:21:33 +0100 Subject: [PATCH] Duty/cycle end --- app/src/main/java/eu/faircode/email/Core.java | 4 ++-- .../java/eu/faircode/email/DutyCycle.java | 20 ++++++++++++++++--- .../eu/faircode/email/ServiceSynchronize.java | 2 +- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 3d711321ee..49e7624116 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -3559,7 +3559,7 @@ class Core { } finally { // Free memory isub[j] = null; - dc.stop(state.getForeground()); + dc.stop(state.getForeground(), from == 0 && j == 0); } } } @@ -3647,7 +3647,7 @@ class Core { } finally { // Free memory isub[j] = null; - dc.stop(state.getForeground()); + dc.stop(state.getForeground(), from == 0 && j == 0); } } } diff --git a/app/src/main/java/eu/faircode/email/DutyCycle.java b/app/src/main/java/eu/faircode/email/DutyCycle.java index 151c5edeab..ce7ae87fee 100644 --- a/app/src/main/java/eu/faircode/email/DutyCycle.java +++ b/app/src/main/java/eu/faircode/email/DutyCycle.java @@ -20,6 +20,8 @@ package eu.faircode.email; */ import java.util.Date; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.ThreadPoolExecutor; public class DutyCycle { private final String name; @@ -48,7 +50,19 @@ public class DutyCycle { start = new Date().getTime(); } - public void stop(boolean foreground) { + public void stop(boolean foreground, ExecutorService executor) { + boolean done = false; + try { + done = (executor instanceof ThreadPoolExecutor && + ((ThreadPoolExecutor) executor).getQueue().size() == 0); + } catch (Throwable ex) { + Log.e(ex); + } finally { + stop(foreground, done); + } + } + + public void stop(boolean foreground, boolean done) { long end = new Date().getTime(); if (last != null) @@ -60,8 +74,8 @@ public class DutyCycle { if (busy + idle > interval) { long wait = (duration - idle); Log.i(name + " busy=" + busy + " idle=" + idle + - " wait=" + wait + " foreground=" + foreground); - if (wait > 0 && foreground) { + " wait=" + wait + " foreground=" + foreground + " done=" + done); + if (wait > 0 && foreground && !done) { try { Thread.sleep(wait); } catch (InterruptedException ex) { diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index b1d0886e99..54c18ed8cf 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -1994,7 +1994,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences iservice, ifolder, state, serial); } finally { - dc.stop(state.getForeground()); + dc.stop(state.getForeground(), executor); } } catch (Throwable ex) {