diff --git a/vector/src/main/java/im/vector/app/core/extensions/Service.kt b/vector/src/main/java/im/vector/app/core/extensions/Service.kt deleted file mode 100644 index 2f56b13e1a8..00000000000 --- a/vector/src/main/java/im/vector/app/core/extensions/Service.kt +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 2022-2024 New Vector Ltd. - * - * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial - * Please see LICENSE files in the repository root for full details. - */ - -package im.vector.app.core.extensions - -import android.app.Notification -import android.app.Service -import android.content.pm.ServiceInfo -import android.os.Build - -fun Service.startForegroundCompat( - id: Int, - notification: Notification, - provideForegroundServiceType: (() -> Int)? = null -) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { - startForeground( - id, - notification, - provideForegroundServiceType?.invoke() ?: ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST - ) - } else { - startForeground(id, notification) - } -} diff --git a/vector/src/main/java/im/vector/app/core/services/CallAndroidService.kt b/vector/src/main/java/im/vector/app/core/services/CallAndroidService.kt index 9e0a718b156..1b8e781009a 100644 --- a/vector/src/main/java/im/vector/app/core/services/CallAndroidService.kt +++ b/vector/src/main/java/im/vector/app/core/services/CallAndroidService.kt @@ -12,17 +12,18 @@ import android.Manifest import android.content.Context import android.content.Intent import android.content.pm.PackageManager +import android.content.pm.ServiceInfo import android.os.Binder import android.support.v4.media.session.MediaSessionCompat import android.view.KeyEvent import androidx.core.app.ActivityCompat import androidx.core.app.NotificationManagerCompat +import androidx.core.app.ServiceCompat import androidx.core.content.ContextCompat import androidx.media.session.MediaButtonReceiver import com.airbnb.mvrx.Mavericks import dagger.hilt.android.AndroidEntryPoint import im.vector.app.core.extensions.singletonEntryPoint -import im.vector.app.core.extensions.startForegroundCompat import im.vector.app.features.call.CallArgs import im.vector.app.features.call.VectorCallActivity import im.vector.app.features.call.audio.MicrophoneAccessService @@ -178,7 +179,12 @@ class CallAndroidService : VectorAndroidService() { fromBg = fromBg ) if (knownCalls.isEmpty()) { - startForegroundCompat(callId.hashCode(), notification) + ServiceCompat.startForeground( + this, + callId.hashCode(), + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL, + ) } else { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { Timber.w("Not allowed to notify.") @@ -201,8 +207,12 @@ class CallAndroidService : VectorAndroidService() { return } val notification = notificationUtils.buildCallEndedNotification(false) - val notificationId = callId.hashCode() - startForegroundCompat(notificationId, notification) + ServiceCompat.startForeground( + this, + callId.hashCode(), + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL, + ) if (knownCalls.isEmpty()) { Timber.tag(loggerTag.value).v("No more call, stop the service") stopForegroundCompat() @@ -240,7 +250,12 @@ class CallAndroidService : VectorAndroidService() { title = callInformation.opponentMatrixItem?.getBestName() ?: callInformation.opponentUserId ) if (knownCalls.isEmpty()) { - startForegroundCompat(callId.hashCode(), notification) + ServiceCompat.startForeground( + this, + callId.hashCode(), + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL, + ) } else { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { Timber.w("Not allowed to notify.") @@ -268,7 +283,12 @@ class CallAndroidService : VectorAndroidService() { title = callInformation.opponentMatrixItem?.getBestName() ?: callInformation.opponentUserId ) if (knownCalls.isEmpty()) { - startForegroundCompat(callId.hashCode(), notification) + ServiceCompat.startForeground( + this, + callId.hashCode(), + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL, + ) } else { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) { Timber.w("Not allowed to notify.") @@ -285,9 +305,19 @@ class CallAndroidService : VectorAndroidService() { callRingPlayerOutgoing?.stop() val notification = notificationUtils.buildCallEndedNotification(false) if (callId != null) { - startForegroundCompat(callId.hashCode(), notification) + ServiceCompat.startForeground( + this, + callId.hashCode(), + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL, + ) } else { - startForegroundCompat(DEFAULT_NOTIFICATION_ID, notification) + ServiceCompat.startForeground( + this, + DEFAULT_NOTIFICATION_ID, + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_PHONE_CALL, + ) } if (knownCalls.isEmpty()) { mediaSession?.isActive = false diff --git a/vector/src/main/java/im/vector/app/core/services/VectorSyncAndroidService.kt b/vector/src/main/java/im/vector/app/core/services/VectorSyncAndroidService.kt index 6ab6514a6a4..a54505ff038 100644 --- a/vector/src/main/java/im/vector/app/core/services/VectorSyncAndroidService.kt +++ b/vector/src/main/java/im/vector/app/core/services/VectorSyncAndroidService.kt @@ -11,7 +11,9 @@ import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent +import android.content.pm.ServiceInfo import android.os.Build +import androidx.core.app.ServiceCompat import androidx.core.content.getSystemService import androidx.work.Constraints import androidx.work.Data @@ -22,7 +24,6 @@ import androidx.work.WorkRequest import androidx.work.Worker import androidx.work.WorkerParameters import dagger.hilt.android.AndroidEntryPoint -import im.vector.app.core.extensions.startForegroundCompat import im.vector.app.core.platform.PendingIntentCompat import im.vector.app.features.notifications.NotificationUtils import im.vector.app.features.settings.BackgroundSyncMode @@ -90,7 +91,12 @@ class VectorSyncAndroidService : SyncAndroidService() { CommonStrings.notification_listening_for_notifications } val notification = notificationUtils.buildForegroundServiceNotification(notificationSubtitleRes, false) - startForegroundCompat(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification) + ServiceCompat.startForeground( + this, + NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC, + ) } override fun onRescheduleAsked( diff --git a/vector/src/main/java/im/vector/app/features/call/audio/MicrophoneAccessService.kt b/vector/src/main/java/im/vector/app/features/call/audio/MicrophoneAccessService.kt index bb570442ae2..876557da9e7 100644 --- a/vector/src/main/java/im/vector/app/features/call/audio/MicrophoneAccessService.kt +++ b/vector/src/main/java/im/vector/app/features/call/audio/MicrophoneAccessService.kt @@ -17,10 +17,11 @@ package im.vector.app.features.call.audio import android.content.Intent +import android.content.pm.ServiceInfo import android.os.Binder import android.os.IBinder +import androidx.core.app.ServiceCompat import dagger.hilt.android.AndroidEntryPoint -import im.vector.app.core.extensions.startForegroundCompat import im.vector.app.core.services.VectorAndroidService import im.vector.app.features.notifications.NotificationUtils import im.vector.lib.core.utils.timer.Clock @@ -42,7 +43,12 @@ class MicrophoneAccessService : VectorAndroidService() { private fun showMicrophoneAccessNotification() { val notificationId = clock.epochMillis().toInt() val notification = notificationUtils.buildMicrophoneAccessNotification() - startForegroundCompat(notificationId, notification) + ServiceCompat.startForeground( + this, + notificationId, + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE, + ) } override fun onBind(intent: Intent?): IBinder { diff --git a/vector/src/main/java/im/vector/app/features/call/webrtc/ScreenCaptureAndroidService.kt b/vector/src/main/java/im/vector/app/features/call/webrtc/ScreenCaptureAndroidService.kt index 812abd8b94f..3ae5bb5e7d6 100644 --- a/vector/src/main/java/im/vector/app/features/call/webrtc/ScreenCaptureAndroidService.kt +++ b/vector/src/main/java/im/vector/app/features/call/webrtc/ScreenCaptureAndroidService.kt @@ -8,10 +8,11 @@ package im.vector.app.features.call.webrtc import android.content.Intent +import android.content.pm.ServiceInfo import android.os.Binder import android.os.IBinder +import androidx.core.app.ServiceCompat import dagger.hilt.android.AndroidEntryPoint -import im.vector.app.core.extensions.startForegroundCompat import im.vector.app.core.services.VectorAndroidService import im.vector.app.features.notifications.NotificationUtils import im.vector.lib.core.utils.timer.Clock @@ -33,7 +34,12 @@ class ScreenCaptureAndroidService : VectorAndroidService() { private fun showStickyNotification() { val notificationId = clock.epochMillis().toInt() val notification = notificationUtils.buildScreenSharingNotification() - startForegroundCompat(notificationId, notification) + ServiceCompat.startForeground( + this, + notificationId, + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PROJECTION, + ) } override fun onBind(intent: Intent?): IBinder { diff --git a/vector/src/main/java/im/vector/app/features/lifecycle/VectorActivityLifecycleCallbacks.kt b/vector/src/main/java/im/vector/app/features/lifecycle/VectorActivityLifecycleCallbacks.kt index 968c422f433..10aaf0b56c0 100644 --- a/vector/src/main/java/im/vector/app/features/lifecycle/VectorActivityLifecycleCallbacks.kt +++ b/vector/src/main/java/im/vector/app/features/lifecycle/VectorActivityLifecycleCallbacks.kt @@ -7,6 +7,7 @@ package im.vector.app.features.lifecycle +import android.annotation.SuppressLint import android.app.Activity import android.app.ActivityManager import android.app.Application @@ -50,6 +51,7 @@ class VectorActivityLifecycleCallbacks constructor(private val popupAlertManager override fun onActivityStopped(activity: Activity) {} + @SuppressLint("WrongConstant") override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { if (activitiesInfo == null) { val context = activity.applicationContext diff --git a/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt b/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt index 76205f98cfe..ebfe854e687 100644 --- a/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt +++ b/vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt @@ -10,13 +10,14 @@ package im.vector.app.features.location.live.tracking import android.Manifest import android.content.Intent import android.content.pm.PackageManager +import android.content.pm.ServiceInfo import android.os.IBinder import android.os.Parcelable import androidx.core.app.ActivityCompat import androidx.core.app.NotificationManagerCompat +import androidx.core.app.ServiceCompat import dagger.hilt.android.AndroidEntryPoint import im.vector.app.core.di.ActiveSessionHolder -import im.vector.app.core.extensions.startForegroundCompat import im.vector.app.core.services.VectorAndroidService import im.vector.app.core.utils.PermissionChecker import im.vector.app.features.location.LocationData @@ -114,7 +115,12 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca NotificationManagerCompat.from(this).notify(FOREGROUND_SERVICE_NOTIFICATION_ID, notification) } } else { - startForegroundCompat(FOREGROUND_SERVICE_NOTIFICATION_ID, notification) + ServiceCompat.startForeground( + this, + FOREGROUND_SERVICE_NOTIFICATION_ID, + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_LOCATION, + ) foregroundModeStarted = true } diff --git a/vector/src/main/java/im/vector/app/features/start/StartAppAndroidService.kt b/vector/src/main/java/im/vector/app/features/start/StartAppAndroidService.kt index cc737486be6..13b86d71a5c 100644 --- a/vector/src/main/java/im/vector/app/features/start/StartAppAndroidService.kt +++ b/vector/src/main/java/im/vector/app/features/start/StartAppAndroidService.kt @@ -8,10 +8,11 @@ package im.vector.app.features.start import android.content.Intent +import android.content.pm.ServiceInfo +import androidx.core.app.ServiceCompat import dagger.hilt.android.AndroidEntryPoint import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.di.NamedGlobalScope -import im.vector.app.core.extensions.startForegroundCompat import im.vector.app.core.services.VectorAndroidService import im.vector.app.features.notifications.NotificationUtils import kotlinx.coroutines.CoroutineScope @@ -50,6 +51,11 @@ class StartAppAndroidService : VectorAndroidService() { private fun showStickyNotification() { val notificationId = Random.nextInt() val notification = notificationUtils.buildStartAppNotification() - startForegroundCompat(notificationId, notification) + ServiceCompat.startForeground( + this, + notificationId, + notification, + ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC, + ) } }