From a87678a79e4bd263d1153d59071e34023b60f924 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Tue, 21 Nov 2023 11:44:48 +0100 Subject: [PATCH] Fix foreground service on Android 14 Signed-off-by: alperozturk --- app/src/main/AndroidManifest.xml | 7 +++++++ .../files/downloader/FileTransferService.kt | 19 +++++++++++++++---- .../nextcloud/client/media/PlayerService.kt | 11 +++++++++-- .../files/services/FileDownloader.java | 8 +++++++- .../android/files/services/FileUploader.java | 9 +++++++-- 5 files changed, 45 insertions(+), 9 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 349d5da8a9..2ab614fca2 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -72,6 +72,9 @@ + + + @@ -378,15 +381,19 @@ android:exported="false" /> = Build.VERSION_CODES.Q) { + startForeground( + AppNotificationManager.TRANSFER_NOTIFICATION_ID, + notificationsManager.buildDownloadServiceForegroundNotification(), + ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC + ) + } else { + startForeground( + AppNotificationManager.TRANSFER_NOTIFICATION_ID, + notificationsManager.buildDownloadServiceForegroundNotification() + ) + } } val request: Request = intent.getParcelableExtra(EXTRA_REQUEST)!! diff --git a/app/src/main/java/com/nextcloud/client/media/PlayerService.kt b/app/src/main/java/com/nextcloud/client/media/PlayerService.kt index 5d8e7a3364..96ac329105 100644 --- a/app/src/main/java/com/nextcloud/client/media/PlayerService.kt +++ b/app/src/main/java/com/nextcloud/client/media/PlayerService.kt @@ -22,7 +22,9 @@ package com.nextcloud.client.media import android.app.PendingIntent import android.app.Service import android.content.Intent +import android.content.pm.ServiceInfo import android.media.AudioManager +import android.os.Build import android.os.Bundle import android.os.IBinder import android.widget.MediaController @@ -167,11 +169,16 @@ class PlayerService : Service() { notificationBuilder.setContentTitle(ticker) notificationBuilder.setContentText(content) - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { notificationBuilder.setChannelId(NotificationUtils.NOTIFICATION_CHANNEL_MEDIA) } - startForeground(R.string.media_notif_ticker, notificationBuilder.build()) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + startForeground(R.string.media_notif_ticker, notificationBuilder.build(), ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK) + } else { + startForeground(R.string.media_notif_ticker, notificationBuilder.build()) + } + isRunning = true } diff --git a/app/src/main/java/com/owncloud/android/files/services/FileDownloader.java b/app/src/main/java/com/owncloud/android/files/services/FileDownloader.java index cbe4d37aea..c7b80930bb 100644 --- a/app/src/main/java/com/owncloud/android/files/services/FileDownloader.java +++ b/app/src/main/java/com/owncloud/android/files/services/FileDownloader.java @@ -28,8 +28,10 @@ import android.app.NotificationManager; import android.app.PendingIntent; import android.app.Service; import android.content.Intent; +import android.content.pm.ServiceInfo; import android.graphics.BitmapFactory; import android.os.Binder; +import android.os.Build; import android.os.Handler; import android.os.HandlerThread; import android.os.IBinder; @@ -200,7 +202,11 @@ public class FileDownloader extends Service public int onStartCommand(Intent intent, int flags, int startId) { Log_OC.d(TAG, "Starting command with id " + startId); - startForeground(FOREGROUND_SERVICE_ID, mNotification); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + startForeground(FOREGROUND_SERVICE_ID, mNotification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC); + } else { + startForeground(FOREGROUND_SERVICE_ID, mNotification); + } if (intent == null || !intent.hasExtra(EXTRA_USER) || !intent.hasExtra(EXTRA_FILE)) { Log_OC.e(TAG, "Not enough information provided in intent"); diff --git a/app/src/main/java/com/owncloud/android/files/services/FileUploader.java b/app/src/main/java/com/owncloud/android/files/services/FileUploader.java index c5a3de0cf3..77b94ec1f5 100644 --- a/app/src/main/java/com/owncloud/android/files/services/FileUploader.java +++ b/app/src/main/java/com/owncloud/android/files/services/FileUploader.java @@ -36,6 +36,7 @@ import android.app.PendingIntent; import android.app.Service; import android.content.Context; import android.content.Intent; +import android.content.pm.ServiceInfo; import android.graphics.BitmapFactory; import android.os.Binder; import android.os.Build; @@ -304,7 +305,11 @@ public class FileUploader extends Service public int onStartCommand(Intent intent, int flags, int startId) { Log_OC.d(TAG, "Starting command with id " + startId); - startForeground(FOREGROUND_SERVICE_ID, mNotification); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + startForeground(FOREGROUND_SERVICE_ID, mNotification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC); + } else { + startForeground(FOREGROUND_SERVICE_ID, mNotification); + } if (intent == null) { Log_OC.e(TAG, "Intent is null"); @@ -602,7 +607,7 @@ public class FileUploader extends Service /** * Core upload method: sends the file(s) to upload WARNING: legacy code, must be in sync with @{{@link - * FilesUploadWorker#upload(UploadFileOperation, User)} + * FilesUploadWorker upload(UploadFileOperation, User)} * * @param uploadKey Key to access the upload to perform, contained in mPendingUploads */