瀏覽代碼

Simplify code complexity

Signed-off-by: alperozturk <alper_ozturk@proton.me>
alperozturk 1 年之前
父節點
當前提交
235d7418cd

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

@@ -23,7 +23,6 @@ package com.nextcloud.utils
 
 import android.app.Notification
 import android.app.Service
-import android.content.pm.ServiceInfo
 import android.os.Build
 import androidx.core.app.ServiceCompat
 import com.owncloud.android.datamodel.ForegroundServiceType
@@ -36,17 +35,11 @@ object ForegroundServiceHelper {
         foregroundServiceType: ForegroundServiceType
     ) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
-            val foregroundServiceTypeId: Int = if (foregroundServiceType == ForegroundServiceType.DataSync) {
-                ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
-            } else {
-                ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
-            }
-
             ServiceCompat.startForeground(
                 service,
                 id,
                 notification,
-                foregroundServiceTypeId
+                foregroundServiceType.getId()
             )
         } else {
             service.startForeground(id, notification)

+ 20 - 1
app/src/main/java/com/owncloud/android/datamodel/ForegroundServiceType.kt

@@ -21,6 +21,25 @@
 
 package com.owncloud.android.datamodel
 
+import android.content.pm.ServiceInfo
+import android.os.Build
+import androidx.annotation.RequiresApi
+
+/**
+ * Enum to specify the type of foreground service.
+ * Use this enum when starting a foreground service to indicate its purpose.
+ * Note: Foreground service type is not available for older Android versions.
+ * This wrapper is designed for compatibility on those versions.
+ */
 enum class ForegroundServiceType {
-    DataSync, MediaPlayback
+    DataSync, MediaPlayback;
+
+    @RequiresApi(Build.VERSION_CODES.Q)
+    fun getId(): Int {
+        return if (this == DataSync) {
+            ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC
+        } else {
+            ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
+        }
+    }
 }