Skip to content

Commit b9eb271

Browse files
authored
Merge pull request #7712 from vector-im/feature/bma/startServiceApi
Use the latest startForeground API
2 parents 3b3947d + febf01a commit b9eb271

File tree

7 files changed

+58
-11
lines changed

7 files changed

+58
-11
lines changed

vector/src/main/AndroidManifest.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@
7272
<application android:supportsRtl="true">
7373

7474
<!-- Sentry auto-initialization disable -->
75-
<meta-data android:name="io.sentry.auto-init" android:value="false" />
75+
<meta-data
76+
android:name="io.sentry.auto-init"
77+
android:value="false" />
7678

7779
<!-- No limit for screen ratio: avoid black strips -->
7880
<meta-data
@@ -330,6 +332,7 @@
330332

331333
<service
332334
android:name=".core.services.CallAndroidService"
335+
android:foregroundServiceType="phoneCall"
333336
android:exported="false">
334337
<!-- in order to get headset button events -->
335338
<intent-filter>
@@ -341,6 +344,7 @@
341344
<service
342345
android:name=".core.services.VectorSyncAndroidService"
343346
android:exported="false"
347+
android:foregroundServiceType="dataSync"
344348
tools:ignore="Instantiatable" />
345349

346350
<service
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2022 New Vector Ltd
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package im.vector.app.core.extensions
18+
19+
import android.app.Notification
20+
import android.app.Service
21+
import android.content.pm.ServiceInfo
22+
import android.os.Build
23+
24+
fun Service.startForegroundCompat(
25+
id: Int,
26+
notification: Notification,
27+
provideForegroundServiceType: (() -> Int)? = null
28+
) {
29+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
30+
startForeground(
31+
id,
32+
notification,
33+
provideForegroundServiceType?.invoke() ?: ServiceInfo.FOREGROUND_SERVICE_TYPE_MANIFEST
34+
)
35+
} else {
36+
startForeground(id, notification)
37+
}
38+
}

vector/src/main/java/im/vector/app/core/services/CallAndroidService.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import androidx.media.session.MediaButtonReceiver
2828
import com.airbnb.mvrx.Mavericks
2929
import dagger.hilt.android.AndroidEntryPoint
3030
import im.vector.app.core.extensions.singletonEntryPoint
31+
import im.vector.app.core.extensions.startForegroundCompat
3132
import im.vector.app.features.call.CallArgs
3233
import im.vector.app.features.call.VectorCallActivity
3334
import im.vector.app.features.call.telecom.CallConnection
@@ -181,7 +182,7 @@ class CallAndroidService : VectorAndroidService() {
181182
fromBg = fromBg
182183
)
183184
if (knownCalls.isEmpty()) {
184-
startForeground(callId.hashCode(), notification)
185+
startForegroundCompat(callId.hashCode(), notification)
185186
} else {
186187
notificationManager.notify(callId.hashCode(), notification)
187188
}
@@ -201,7 +202,7 @@ class CallAndroidService : VectorAndroidService() {
201202
}
202203
val notification = notificationUtils.buildCallEndedNotification(false)
203204
val notificationId = callId.hashCode()
204-
startForeground(notificationId, notification)
205+
startForegroundCompat(notificationId, notification)
205206
if (knownCalls.isEmpty()) {
206207
Timber.tag(loggerTag.value).v("No more call, stop the service")
207208
stopForegroundCompat()
@@ -236,7 +237,7 @@ class CallAndroidService : VectorAndroidService() {
236237
title = callInformation.opponentMatrixItem?.getBestName() ?: callInformation.opponentUserId
237238
)
238239
if (knownCalls.isEmpty()) {
239-
startForeground(callId.hashCode(), notification)
240+
startForegroundCompat(callId.hashCode(), notification)
240241
} else {
241242
notificationManager.notify(callId.hashCode(), notification)
242243
}
@@ -260,7 +261,7 @@ class CallAndroidService : VectorAndroidService() {
260261
title = callInformation.opponentMatrixItem?.getBestName() ?: callInformation.opponentUserId
261262
)
262263
if (knownCalls.isEmpty()) {
263-
startForeground(callId.hashCode(), notification)
264+
startForegroundCompat(callId.hashCode(), notification)
264265
} else {
265266
notificationManager.notify(callId.hashCode(), notification)
266267
}
@@ -273,9 +274,9 @@ class CallAndroidService : VectorAndroidService() {
273274
callRingPlayerOutgoing?.stop()
274275
val notification = notificationUtils.buildCallEndedNotification(false)
275276
if (callId != null) {
276-
startForeground(callId.hashCode(), notification)
277+
startForegroundCompat(callId.hashCode(), notification)
277278
} else {
278-
startForeground(DEFAULT_NOTIFICATION_ID, notification)
279+
startForegroundCompat(DEFAULT_NOTIFICATION_ID, notification)
279280
}
280281
if (knownCalls.isEmpty()) {
281282
mediaSession?.isActive = false

vector/src/main/java/im/vector/app/core/services/VectorSyncAndroidService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import androidx.work.Worker
3232
import androidx.work.WorkerParameters
3333
import dagger.hilt.android.AndroidEntryPoint
3434
import im.vector.app.R
35+
import im.vector.app.core.extensions.startForegroundCompat
3536
import im.vector.app.core.platform.PendingIntentCompat
3637
import im.vector.app.core.time.Clock
3738
import im.vector.app.core.time.DefaultClock
@@ -98,7 +99,7 @@ class VectorSyncAndroidService : SyncAndroidService() {
9899
R.string.notification_listening_for_notifications
99100
}
100101
val notification = notificationUtils.buildForegroundServiceNotification(notificationSubtitleRes, false)
101-
startForeground(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification)
102+
startForegroundCompat(NotificationUtils.NOTIFICATION_ID_FOREGROUND_SERVICE, notification)
102103
}
103104

104105
override fun onRescheduleAsked(

vector/src/main/java/im/vector/app/features/call/webrtc/ScreenCaptureAndroidService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.content.Intent
2020
import android.os.Binder
2121
import android.os.IBinder
2222
import dagger.hilt.android.AndroidEntryPoint
23+
import im.vector.app.core.extensions.startForegroundCompat
2324
import im.vector.app.core.services.VectorAndroidService
2425
import im.vector.app.core.time.Clock
2526
import im.vector.app.features.notifications.NotificationUtils
@@ -41,7 +42,7 @@ class ScreenCaptureAndroidService : VectorAndroidService() {
4142
private fun showStickyNotification() {
4243
val notificationId = clock.epochMillis().toInt()
4344
val notification = notificationUtils.buildScreenSharingNotification()
44-
startForeground(notificationId, notification)
45+
startForegroundCompat(notificationId, notification)
4546
}
4647

4748
override fun onBind(intent: Intent?): IBinder {

vector/src/main/java/im/vector/app/features/location/live/tracking/LocationSharingAndroidService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import android.os.Parcelable
2222
import androidx.core.app.NotificationManagerCompat
2323
import dagger.hilt.android.AndroidEntryPoint
2424
import im.vector.app.core.di.ActiveSessionHolder
25+
import im.vector.app.core.extensions.startForegroundCompat
2526
import im.vector.app.core.services.VectorAndroidService
2627
import im.vector.app.features.location.LocationData
2728
import im.vector.app.features.location.LocationTracker
@@ -105,7 +106,7 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca
105106
if (foregroundModeStarted) {
106107
NotificationManagerCompat.from(this).notify(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
107108
} else {
108-
startForeground(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
109+
startForegroundCompat(FOREGROUND_SERVICE_NOTIFICATION_ID, notification)
109110
foregroundModeStarted = true
110111
}
111112

vector/src/main/java/im/vector/app/features/start/StartAppAndroidService.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import android.content.Intent
2020
import dagger.hilt.android.AndroidEntryPoint
2121
import im.vector.app.core.di.ActiveSessionHolder
2222
import im.vector.app.core.di.NamedGlobalScope
23+
import im.vector.app.core.extensions.startForegroundCompat
2324
import im.vector.app.core.services.VectorAndroidService
2425
import im.vector.app.features.notifications.NotificationUtils
2526
import kotlinx.coroutines.CoroutineScope
@@ -58,6 +59,6 @@ class StartAppAndroidService : VectorAndroidService() {
5859
private fun showStickyNotification() {
5960
val notificationId = Random.nextInt()
6061
val notification = notificationUtils.buildStartAppNotification()
61-
startForeground(notificationId, notification)
62+
startForegroundCompat(notificationId, notification)
6263
}
6364
}

0 commit comments

Comments
 (0)