Skip to content

Commit 78c613c

Browse files
committed
fix: comply with fgs type on versions above 33
1 parent 307c3da commit 78c613c

File tree

5 files changed

+57
-32
lines changed

5 files changed

+57
-32
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
<uses-permission
2525
android:name="android.permission.POST_NOTIFICATIONS"
2626
android:minSdkVersion="33" />
27+
<uses-permission
28+
android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"
29+
android:minSdkVersion="34" />
2730

2831
<application
2932
android:name=".App"
@@ -82,6 +85,7 @@
8285

8386
<service
8487
android:name="com.my.kizzy.feature_rpc_base.services.AppDetectionService"
88+
android:foregroundServiceType="mediaPlayback"
8589
android:exported="false" />
8690
<service
8791
android:name="com.my.kizzy.feature_rpc_base.services.MediaRpcService"
@@ -97,6 +101,7 @@
97101
</service>
98102
<service
99103
android:name="com.my.kizzy.feature_rpc_base.services.CustomRpcService"
104+
android:foregroundServiceType="mediaPlayback"
100105
android:exported="false" >
101106
</service>
102107

@@ -110,6 +115,7 @@
110115
</service>
111116

112117
<service android:name=".feature_rpc_base.services.ExperimentalRpc"
118+
android:foregroundServiceType="mediaPlayback"
113119
android:exported="false"/>
114120

115121
<service

feature_rpc_base/src/main/java/com/my/kizzy/feature_rpc_base/services/AppDetectionService.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import android.app.Service
2121
import android.app.usage.UsageStats
2222
import android.app.usage.UsageStatsManager
2323
import android.content.Intent
24+
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
25+
import android.os.Build
2426
import android.os.IBinder
2527
import com.blankj.utilcode.util.AppUtils
2628
import com.my.kizzy.data.rpc.KizzyRPC
@@ -36,7 +38,6 @@ import kotlinx.coroutines.cancel
3638
import kotlinx.coroutines.delay
3739
import kotlinx.coroutines.isActive
3840
import kotlinx.coroutines.launch
39-
import kotlinx.serialization.decodeFromString
4041
import kotlinx.serialization.json.Json
4142
import java.util.SortedMap
4243
import java.util.TreeMap
@@ -100,7 +101,11 @@ class AppDetectionService : Service() {
100101
.addAction(R.drawable.ic_apps, getString(R.string.exit), pendingIntent)
101102

102103

103-
startForeground(Constants.NOTIFICATION_ID, createDefaultNotification())
104+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
105+
startForeground(Constants.NOTIFICATION_ID, createDefaultNotification())
106+
} else {
107+
startForeground(Constants.NOTIFICATION_ID, createDefaultNotification(), FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
108+
}
104109

105110
val rpcButtons = getRpcButtons()
106111

feature_rpc_base/src/main/java/com/my/kizzy/feature_rpc_base/services/CustomRpcService.kt

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ package com.my.kizzy.feature_rpc_base.services
1515
import android.annotation.SuppressLint
1616
import android.app.*
1717
import android.content.Intent
18+
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
19+
import android.os.Build
1820
import android.os.IBinder
1921
import android.os.PowerManager
2022
import android.os.PowerManager.WakeLock
@@ -27,7 +29,6 @@ import dagger.hilt.android.AndroidEntryPoint
2729
import kotlinx.coroutines.CoroutineScope
2830
import kotlinx.coroutines.cancel
2931
import kotlinx.coroutines.launch
30-
import kotlinx.serialization.decodeFromString
3132
import kotlinx.serialization.json.Json
3233
import javax.inject.Inject
3334

@@ -64,14 +65,18 @@ class CustomRpcService : Service() {
6465
this, 0, stopIntent, PendingIntent.FLAG_IMMUTABLE
6566
)
6667

67-
startForeground(
68-
Constants.NOTIFICATION_ID, notificationBuilder
69-
.setContentTitle(getString(R.string.custom_rpc_running))
70-
.setContentText(rpcData?.name ?: "")
71-
.setSmallIcon(R.drawable.ic_rpc_placeholder)
72-
.addAction(R.drawable.ic_rpc_placeholder, getString(R.string.exit), pendingIntent)
73-
.build()
74-
)
68+
val notification = notificationBuilder
69+
.setContentTitle(getString(R.string.custom_rpc_running))
70+
.setContentText(rpcData?.name ?: "")
71+
.setSmallIcon(R.drawable.ic_rpc_placeholder)
72+
.addAction(R.drawable.ic_rpc_placeholder, getString(R.string.exit), pendingIntent)
73+
.build()
74+
75+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
76+
startForeground(Constants.NOTIFICATION_ID, notification)
77+
} else {
78+
startForeground(Constants.NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
79+
}
7580

7681
val powerManager = getSystemService(POWER_SERVICE) as PowerManager
7782
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, WAKELOCK)

feature_rpc_base/src/main/java/com/my/kizzy/feature_rpc_base/services/ExperimentalRpc.kt

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ import android.app.PendingIntent
1818
import android.app.Service
1919
import android.content.ComponentName
2020
import android.content.Intent
21+
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
2122
import android.media.MediaMetadata
2223
import android.media.session.MediaController
2324
import android.media.session.MediaSessionManager
2425
import android.media.session.PlaybackState
26+
import android.os.Build
2527
import android.os.IBinder
2628
import com.my.kizzy.data.get_current_data.app.GetCurrentlyRunningApp
2729
import com.my.kizzy.data.get_current_data.media.GetCurrentPlayingMediaAll
@@ -117,19 +119,21 @@ class ExperimentalRpc : Service() {
117119
this, 0, restartIntent, PendingIntent.FLAG_IMMUTABLE
118120
)
119121

120-
startForeground(
121-
Constants.NOTIFICATION_ID,
122-
notificationBuilder
123-
.setSmallIcon(R.drawable.ic_dev_rpc)
124-
.setContentTitle(getString(R.string.service_enabled))
125-
.addAction(
126-
R.drawable.ic_dev_rpc,
127-
getString(R.string.restart),
128-
restartPendingIntent
129-
)
130-
.addAction(R.drawable.ic_dev_rpc, getString(R.string.exit), pendingIntent)
131-
.build()
132-
)
122+
val notification = notificationBuilder
123+
.setSmallIcon(R.drawable.ic_dev_rpc)
124+
.setContentTitle(getString(R.string.service_enabled))
125+
.addAction(
126+
R.drawable.ic_dev_rpc,
127+
getString(R.string.restart),
128+
restartPendingIntent
129+
)
130+
.addAction(R.drawable.ic_dev_rpc, getString(R.string.exit), pendingIntent)
131+
.build()
132+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
133+
startForeground(Constants.NOTIFICATION_ID, notification)
134+
} else {
135+
startForeground(Constants.NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
136+
}
133137

134138

135139
mediaSessionManager = getSystemService(MEDIA_SESSION_SERVICE) as MediaSessionManager

feature_rpc_base/src/main/java/com/my/kizzy/feature_rpc_base/services/MediaRpcService.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ import android.annotation.SuppressLint
1616
import android.app.*
1717
import android.content.ComponentName
1818
import android.content.Intent
19+
import android.content.pm.ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK
1920
import android.media.MediaMetadata
2021
import android.media.session.MediaController
2122
import android.media.session.MediaSessionManager
2223
import android.media.session.PlaybackState
24+
import android.os.Build
2325
import android.os.IBinder
2426
import android.os.PowerManager
2527
import android.os.PowerManager.WakeLock
@@ -86,14 +88,17 @@ class MediaRpcService : Service() {
8688
0, restartIntent, PendingIntent.FLAG_IMMUTABLE
8789
)
8890

89-
startForeground(
90-
Constants.NOTIFICATION_ID, notificationBuilder
91-
.setSmallIcon(R.drawable.ic_media_rpc)
92-
.addAction(R.drawable.ic_media_rpc, getString(R.string.restart), restartPendingIntent)
93-
.addAction(R.drawable.ic_media_rpc, getString(R.string.exit), pendingIntent)
94-
.setContentText(getString(R.string.idling_notification))
95-
.build()
96-
)
91+
val notification = notificationBuilder
92+
.setSmallIcon(R.drawable.ic_media_rpc)
93+
.addAction(R.drawable.ic_media_rpc, getString(R.string.restart), restartPendingIntent)
94+
.addAction(R.drawable.ic_media_rpc, getString(R.string.exit), pendingIntent)
95+
.setContentText(getString(R.string.idling_notification))
96+
.build()
97+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
98+
startForeground(Constants.NOTIFICATION_ID, notification)
99+
} else {
100+
startForeground(Constants.NOTIFICATION_ID, notification, FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
101+
}
97102

98103
mediaSessionManager = getSystemService(MEDIA_SESSION_SERVICE) as MediaSessionManager
99104
mediaSessionManager.addOnActiveSessionsChangedListener(::activeSessionsListener, ComponentName(this, NotificationListener::class.java))

0 commit comments

Comments
 (0)