Simplify code complexity

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2023-12-06 09:56:07 +01:00
parent 39231af670
commit b2e0e76605
No known key found for this signature in database
GPG key ID: 4E577DC593B59BDF
2 changed files with 21 additions and 9 deletions

View file

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

View file

@ -21,6 +21,25 @@
package com.owncloud.android.datamodel 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 { 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
}
}
} }