Browse Source

Only start foreground service when app in foreground

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 11 tháng trước cách đây
mục cha
commit
ea2665951e

+ 3 - 1
app/src/main/java/com/nextcloud/client/jobs/transfer/FileTransferService.kt

@@ -23,8 +23,10 @@ import com.nextcloud.client.logger.Logger
 import com.nextcloud.client.network.ClientFactory
 import com.nextcloud.client.network.ConnectivityService
 import com.nextcloud.client.notifications.AppNotificationManager
+import com.nextcloud.model.AppLifecycle
 import com.nextcloud.utils.ForegroundServiceHelper
 import com.nextcloud.utils.extensions.getParcelableArgument
+import com.owncloud.android.MainApp
 import com.owncloud.android.datamodel.FileDataStorageManager
 import com.owncloud.android.datamodel.ForegroundServiceType
 import com.owncloud.android.datamodel.UploadsStorageManager
@@ -101,7 +103,7 @@ class FileTransferService : Service() {
             return START_NOT_STICKY
         }
 
-        if (!isRunning) {
+        if (!isRunning && MainApp.lifecycle == AppLifecycle.Foreground) {
             ForegroundServiceHelper.startService(
                 this,
                 AppNotificationManager.TRANSFER_NOTIFICATION_ID,

+ 12 - 0
app/src/main/java/com/nextcloud/model/AppLifecycle.kt

@@ -0,0 +1,12 @@
+/*
+ * Nextcloud - Android Client
+ *
+ * SPDX-FileCopyrightText: 2024 Your Name <your@email.com>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+package com.nextcloud.model
+
+enum class AppLifecycle {
+    Foreground, Background;
+}

+ 17 - 8
app/src/main/java/com/nextcloud/utils/ForegroundServiceHelper.kt

@@ -7,27 +7,36 @@
  */
 package com.nextcloud.utils
 
+import android.app.ForegroundServiceStartNotAllowedException
 import android.app.Notification
 import android.app.Service
 import android.os.Build
+import android.util.Log
 import androidx.core.app.ServiceCompat
 import androidx.work.ForegroundInfo
 import com.owncloud.android.datamodel.ForegroundServiceType
 
 object ForegroundServiceHelper {
+    private const val TAG = "ForegroundServiceHelper"
+    private val isAPILevel29OrAbove = Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q
+
     fun startService(
         service: Service,
         id: Int,
         notification: Notification,
         foregroundServiceType: ForegroundServiceType
     ) {
-        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
-            ServiceCompat.startForeground(
-                service,
-                id,
-                notification,
-                foregroundServiceType.getId()
-            )
+        if (isAPILevel29OrAbove) {
+            try {
+                ServiceCompat.startForeground(
+                    service,
+                    id,
+                    notification,
+                    foregroundServiceType.getId()
+                )
+            } catch (e: ForegroundServiceStartNotAllowedException) {
+                Log.d(TAG, "Exception caught at ForegroundServiceHelper.startService: $e")
+            }
         } else {
             service.startForeground(id, notification)
         }
@@ -38,7 +47,7 @@ object ForegroundServiceHelper {
         notification: Notification,
         foregroundServiceType: ForegroundServiceType
     ): ForegroundInfo {
-        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
+        return if (isAPILevel29OrAbove) {
             ForegroundInfo(id, notification, foregroundServiceType.getId())
         } else {
             ForegroundInfo(id, notification)

+ 5 - 0
app/src/main/java/com/owncloud/android/MainApp.java

@@ -58,6 +58,7 @@ import com.nextcloud.client.onboarding.OnboardingService;
 import com.nextcloud.client.preferences.AppPreferences;
 import com.nextcloud.client.preferences.AppPreferencesImpl;
 import com.nextcloud.client.preferences.DarkMode;
+import com.nextcloud.model.AppLifecycle;
 import com.nextcloud.utils.extensions.ContextExtensionsKt;
 import com.nmc.android.ui.LauncherActivity;
 import com.owncloud.android.authentication.AuthenticatorActivity;
@@ -375,10 +376,14 @@ public class MainApp extends MultiDexApplication implements HasAndroidInjector {
         registerGlobalPassCodeProtection();
     }
 
+    public static AppLifecycle lifecycle = AppLifecycle.Foreground;
+
     private final LifecycleEventObserver lifecycleEventObserver = ((lifecycleOwner, event) -> {
         if (event == Lifecycle.Event.ON_START) {
+            lifecycle = AppLifecycle.Foreground;
             Log_OC.d(TAG, "APP IN FOREGROUND");
         } else if (event == Lifecycle.Event.ON_STOP) {
+            lifecycle = AppLifecycle.Background;
             passCodeManager.setCanAskPin(true);
             Log_OC.d(TAG, "APP IN BACKGROUND");
         } else if (event == Lifecycle.Event.ON_RESUME) {