@@ -41,7 +41,9 @@ import com.nasahacker.convertit.util.AppConfig.IS_SUCCESS
41
41
import com.nasahacker.convertit.util.AppConfig.URI_LIST
42
42
import kotlinx.coroutines.CoroutineScope
43
43
import kotlinx.coroutines.Dispatchers
44
+ import kotlinx.coroutines.Job
44
45
import kotlinx.coroutines.launch
46
+ import com.arthenica.ffmpegkit.FFmpegKit
45
47
46
48
class ConvertItService : Service () {
47
49
companion object {
@@ -50,9 +52,23 @@ class ConvertItService : Service() {
50
52
}
51
53
52
54
private val notificationId = 1
55
+ private var conversionJob: Job ? = null
53
56
54
57
override fun onBind (intent : Intent ? ): IBinder ? = null
55
58
59
+ override fun onDestroy () {
60
+ super .onDestroy()
61
+ Log .i(TAG , " Service destroyed" )
62
+
63
+ // Cancel ongoing conversion job
64
+ conversionJob?.cancel()
65
+ conversionJob = null
66
+
67
+ // Cancel all FFmpeg sessions
68
+ FFmpegKit .cancel()
69
+ Log .i(TAG , " Cancelled all FFmpeg sessions in onDestroy" )
70
+ }
71
+
56
72
override fun onCreate () {
57
73
super .onCreate()
58
74
Log .i(TAG , " Service created - Process ID: ${android.os.Process .myPid()} " )
@@ -70,7 +86,16 @@ class ConvertItService : Service() {
70
86
71
87
if (intent?.action == ACTION_STOP_SERVICE ) {
72
88
Log .i(TAG , " Stopping service as per user request. startId: $startId " )
73
- showCompletionNotification(false )
89
+
90
+ // Cancel ongoing conversion job
91
+ conversionJob?.cancel()
92
+ conversionJob = null
93
+
94
+ // Cancel all FFmpeg sessions
95
+ FFmpegKit .cancel()
96
+ Log .i(TAG , " Cancelled all FFmpeg sessions" )
97
+
98
+ showCompletionNotification(success = false , cancelled = true )
74
99
broadcastConversionResult(Intent ().apply { action = CONVERT_BROADCAST_ACTION }, false )
75
100
stopForegroundService()
76
101
return START_NOT_STICKY
@@ -105,7 +130,7 @@ class ConvertItService : Service() {
105
130
return START_NOT_STICKY
106
131
}
107
132
108
- CoroutineScope (Dispatchers .IO ).launch {
133
+ conversionJob = CoroutineScope (Dispatchers .IO ).launch {
109
134
try {
110
135
Log .i(TAG , " Starting audio conversion for ${uriList.size} files. startId: $startId " )
111
136
AppUtil .convertAudio(
@@ -210,22 +235,30 @@ class ConvertItService : Service() {
210
235
PendingIntent .FLAG_UPDATE_CURRENT or PendingIntent .FLAG_IMMUTABLE ,
211
236
)
212
237
213
- val progressText = if (isIndeterminate) {
214
- getString(R .string.label_converting_audio)
238
+ val (title, contentText) = if (isIndeterminate) {
239
+ getString(R .string.converting_audio_files) to getString( R .string. label_converting_audio)
215
240
} else {
216
- " Conversion in progress: $progress %"
241
+ // Put percentage in title for better visibility on Android 15+
242
+ " ${getString(R .string.converting_audio_files)} ($progress %)" to getString(R .string.label_conversion_progress, progress)
217
243
}
218
244
219
245
Log .d(
220
- TAG , " Creating progress notification: $progressText (Indeterminate: $isIndeterminate )"
246
+ TAG , " Creating progress notification: $title - $contentText (Indeterminate: $isIndeterminate )"
221
247
)
222
248
223
249
return NotificationCompat .Builder (this , CHANNEL_ID )
224
- .setContentTitle(getString(R .string.converting_audio_files))
225
- .setContentText(progressText).setSmallIcon(R .mipmap.ic_launcher_foreground)
226
- .setProgress(100 , progress, isIndeterminate).setAutoCancel(false ).setOngoing(true )
227
- .setDefaults(0 ).setOnlyAlertOnce(true )
228
- .addAction(R .drawable.baseline_stop_24, " Stop" , stopPendingIntent).build()
250
+ .setContentTitle(title)
251
+ .setContentText(contentText)
252
+ .setSmallIcon(R .mipmap.ic_launcher_foreground)
253
+ .setProgress(100 , progress, isIndeterminate)
254
+ .setAutoCancel(false )
255
+ .setOngoing(true )
256
+ .setDefaults(0 )
257
+ .setOnlyAlertOnce(true )
258
+ .setPriority(NotificationCompat .PRIORITY_LOW )
259
+ .setCategory(NotificationCompat .CATEGORY_PROGRESS )
260
+ .addAction(R .drawable.baseline_stop_24, getString(R .string.label_stop), stopPendingIntent)
261
+ .build()
229
262
}
230
263
231
264
private fun updateNotification (progress : Int ) {
@@ -244,13 +277,13 @@ class ConvertItService : Service() {
244
277
Log .v(TAG , " Updated notification: Progress $progress %" )
245
278
}
246
279
247
- private fun showCompletionNotification (success : Boolean ) {
280
+ private fun showCompletionNotification (success : Boolean , cancelled : Boolean = false ) {
248
281
stopForegroundService()
249
282
250
- val notificationText = if (success) {
251
- getString(R .string.conversion_success )
252
- } else {
253
- getString(R .string.conversion_failed)
283
+ val notificationText = when {
284
+ cancelled -> getString(R .string.conversion_cancelled )
285
+ success -> getString( R .string.conversion_success)
286
+ else -> getString(R .string.conversion_failed)
254
287
}
255
288
256
289
Log .i(TAG , " Showing completion notification: $notificationText " )
0 commit comments