Use the API `startForeground(int id, @NonNull Notification notification, @ForegroundServiceType int foregroundServiceType)` when available.

Add missing android:foregroundServiceType in the manifest
This commit is contained in:
Benoit Marty 2022-12-05 16:36:16 +01:00
parent 201873f5a7
commit febf01a2e6
7 changed files with 58 additions and 11 deletions

View File

@ -72,7 +72,9 @@
<application android:supportsRtl="true">
<!-- Sentry auto-initialization disable -->
<meta-data android:name="io.sentry.auto-init" android:value="false" />
<meta-data
android:name="io.sentry.auto-init"
android:value="false" />
<!-- No limit for screen ratio: avoid black strips -->
<meta-data
@ -330,6 +332,7 @@
<service
android:name=".core.services.CallAndroidService"
android:foregroundServiceType="phoneCall"
android:exported="false">
<!-- in order to get headset button events -->
<intent-filter>
@ -341,6 +344,7 @@
<service
android:name=".core.services.VectorSyncAndroidService"
android:exported="false"
android:foregroundServiceType="dataSync"
tools:ignore="Instantiatable" />
<service

View File

@ -0,0 +1,38 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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)
}
}

View File

@ -28,6 +28,7 @@ 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.telecom.CallConnection
@ -181,7 +182,7 @@ class CallAndroidService : VectorAndroidService() {
fromBg = fromBg
)
if (knownCalls.isEmpty()) {
startForeground(callId.hashCode(), notification)
startForegroundCompat(callId.hashCode(), notification)
} else {
notificationManager.notify(callId.hashCode(), notification)
}
@ -201,7 +202,7 @@ class CallAndroidService : VectorAndroidService() {
}
val notification = notificationUtils.buildCallEndedNotification(false)
val notificationId = callId.hashCode()
startForeground(notificationId, notification)
startForegroundCompat(notificationId, notification)
if (knownCalls.isEmpty()) {
Timber.tag(loggerTag.value).v("No more call, stop the service")
stopForegroundCompat()
@ -236,7 +237,7 @@ class CallAndroidService : VectorAndroidService() {
title = callInformation.opponentMatrixItem?.getBestName() ?: callInformation.opponentUserId
)
if (knownCalls.isEmpty()) {
startForeground(callId.hashCode(), notification)
startForegroundCompat(callId.hashCode(), notification)
} else {
notificationManager.notify(callId.hashCode(), notification)
}
@ -260,7 +261,7 @@ class CallAndroidService : VectorAndroidService() {
title = callInformation.opponentMatrixItem?.getBestName() ?: callInformation.opponentUserId
)
if (knownCalls.isEmpty()) {
startForeground(callId.hashCode(), notification)
startForegroundCompat(callId.hashCode(), notification)
} else {
notificationManager.notify(callId.hashCode(), notification)
}
@ -273,9 +274,9 @@ class CallAndroidService : VectorAndroidService() {
callRingPlayerOutgoing?.stop()
val notification = notificationUtils.buildCallEndedNotification(false)
if (callId != null) {
startForeground(callId.hashCode(), notification)
startForegroundCompat(callId.hashCode(), notification)
} else {
startForeground(DEFAULT_NOTIFICATION_ID, notification)
startForegroundCompat(DEFAULT_NOTIFICATION_ID, notification)
}
if (knownCalls.isEmpty()) {
mediaSession?.isActive = false

View File

@ -32,6 +32,7 @@ import androidx.work.Worker
import androidx.work.WorkerParameters
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.R
import im.vector.app.core.extensions.startForegroundCompat
import im.vector.app.core.platform.PendingIntentCompat
import im.vector.app.core.time.Clock
import im.vector.app.core.time.DefaultClock
@ -98,7 +99,7 @@ class VectorSyncAndroidService : SyncAndroidService() {
R.string.notification_listening_for_notifications
}
val notification = notificationUtils.buildForegroundServiceNotification(notificationSubtitleRes, false)
startForeground(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification)
startForegroundCompat(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification)
}
override fun onRescheduleAsked(

View File

@ -20,6 +20,7 @@ import android.content.Intent
import android.os.Binder
import android.os.IBinder
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.core.extensions.startForegroundCompat
import im.vector.app.core.services.VectorAndroidService
import im.vector.app.core.time.Clock
import im.vector.app.features.notifications.NotificationUtils
@ -41,7 +42,7 @@ class ScreenCaptureAndroidService : VectorAndroidService() {
private fun showStickyNotification() {
val notificationId = clock.epochMillis().toInt()
val notification = notificationUtils.buildScreenSharingNotification()
startForeground(notificationId, notification)
startForegroundCompat(notificationId, notification)
}
override fun onBind(intent: Intent?): IBinder {

View File

@ -22,6 +22,7 @@ import android.os.Parcelable
import androidx.core.app.NotificationManagerCompat
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.features.location.LocationData
import im.vector.app.features.location.LocationTracker
@ -105,7 +106,7 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca
if (foregroundModeStarted) {
NotificationManagerCompat.from(this).notify(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
} else {
startForeground(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
startForegroundCompat(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
foregroundModeStarted = true
}

View File

@ -20,6 +20,7 @@ import android.content.Intent
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
@ -58,6 +59,6 @@ class StartAppAndroidService : VectorAndroidService() {
private fun showStickyNotification() {
val notificationId = Random.nextInt()
val notification = notificationUtils.buildStartAppNotification()
startForeground(notificationId, notification)
startForegroundCompat(notificationId, notification)
}
}