From cdb86d24468b14abfd82a5055339d86d3fabba96 Mon Sep 17 00:00:00 2001 From: M66B Date: Fri, 16 Oct 2020 13:02:32 +0200 Subject: [PATCH] Refactoring --- .../java/eu/faircode/email/ApplicationEx.java | 4 ++-- .../eu/faircode/email/ServiceSynchronize.java | 22 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ApplicationEx.java b/app/src/main/java/eu/faircode/email/ApplicationEx.java index ffd9d35c1a..5c8736bb12 100644 --- a/app/src/main/java/eu/faircode/email/ApplicationEx.java +++ b/app/src/main/java/eu/faircode/email/ApplicationEx.java @@ -321,7 +321,7 @@ public class ApplicationEx extends Application implements SharedPreferences.OnSh } else if (version < 1124) { editor.remove("experiments"); } else if (version < 1181) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !BuildConfig.DEBUG) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) editor.remove("background_service"); } else if (version < 1195) editor.remove("auto_optimize"); @@ -351,7 +351,7 @@ public class ApplicationEx extends Application implements SharedPreferences.OnSh editor.putBoolean("beige", false); } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && !BuildConfig.DEBUG) editor.remove("background_service"); if (version < BuildConfig.VERSION_CODE) diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 3de88aae2f..b23ab91f10 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -142,9 +142,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences EntityLog.log(this, "Service create version=" + BuildConfig.VERSION_NAME); super.onCreate(); - final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - boolean background_service = prefs.getBoolean("background_service", false); - if (background_service) + if (isBackgroundService(this)) stopForeground(true); else startForeground(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(null, null).build()); @@ -165,6 +163,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) registerReceiver(idleModeChangedReceiver, new IntentFilter(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED)); + final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); + DB db = DB.getInstance(this); db.account().liveAccountState().observe(this, new Observer>() { @@ -705,9 +705,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences super.onStartCommand(intent, flags, startId); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); - boolean background_service = prefs.getBoolean("background_service", false); - if (background_service) + if (isBackgroundService(this)) stopForeground(true); else startForeground(Helper.NOTIFICATION_SYNCHRONIZE, getNotificationService(null, null).build()); @@ -2254,11 +2252,17 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } private static void start(Context context, Intent intent) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - boolean background_service = prefs.getBoolean("background_service", false); - if (background_service) + if (isBackgroundService(context)) context.startService(intent); else ContextCompat.startForegroundService(context, intent); } + + private static boolean isBackgroundService(Context context) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) + return false; + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + return prefs.getBoolean("background_service", false); + } }