From bba4e62b8cb89b5d981cccc8ba775fac1fe9e2b9 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 11 Jan 2023 09:19:42 +0100 Subject: [PATCH] On foreground while in car or in call --- .../eu/faircode/email/ServiceSynchronize.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index ce83b08d10..dcb15cd61e 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -122,7 +122,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences private boolean isInCar = false; private boolean isOptimizing = false; - private boolean foreground = false; + private MutableLiveData foreground = new MutableLiveData<>(); private final Map coreStates = new Hashtable<>(); private final MutableLiveData liveNetworkState = new MutableLiveData<>(); private final MutableLiveData> liveAccountState = new MutableLiveData<>(); @@ -815,6 +815,18 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } }); + foreground.observe(this, new Observer() { + @Override + public void onChanged(Boolean foreground) { + Log.i("Observed foreground=" + foreground); + boolean fg = Boolean.TRUE.equals(foreground); + if (!fg && (isInCall || isInCar)) + mowner.stop(); + else + mowner.start(); + } + }); + MediaPlayerHelper.liveInCall(this, this, new MediaPlayerHelper.IInCall() { @Override public void onChanged(boolean inCall) { @@ -822,7 +834,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Debug, "In call=" + inCall + " suppress=" + suppress); isInCall = (inCall && suppress); - if (isInCall || isInCar) + boolean fg = Boolean.TRUE.equals(foreground.getValue()); + if (!fg && (isInCall || isInCar)) mowner.stop(); else mowner.start(); @@ -838,7 +851,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences EntityLog.log(ServiceSynchronize.this, EntityLog.Type.Debug, "Projection=" + projection + " state=" + connectionState + " suppress=" + suppress); isInCar = (projection && suppress); - if (isInCall || isInCar) + boolean fg = Boolean.TRUE.equals(foreground.getValue()); + if (!fg && (isInCall || isInCar)) mowner.stop(); else mowner.start(); @@ -852,7 +866,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences @Override public void delegate() { try { - Core.notifyMessages(ServiceSynchronize.this, messages, notificationData, foreground); + boolean fg = Boolean.TRUE.equals(foreground.getValue()); + Core.notifyMessages(ServiceSynchronize.this, messages, notificationData, fg); } catch (SecurityException ex) { Log.w(ex); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ServiceSynchronize.this); @@ -1329,9 +1344,10 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } private void onState(Intent intent) { - foreground = intent.getBooleanExtra("foreground", false); + boolean fg = intent.getBooleanExtra("foreground", false); + foreground.setValue(fg); for (Core.State state : coreStates.values()) - state.setForeground(foreground); + state.setForeground(fg); } private void onPoll(Intent intent) {