Refactoring

This commit is contained in:
M66B
2020-10-16 13:02:32 +02:00
parent 676fb4ff5e
commit cdb86d2446
2 changed files with 15 additions and 11 deletions

View File

@@ -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<List<TupleAccountState>>() {
@@ -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);
}
}