From ef2ebb9176a354cc3448d22d82aad52d90b2576f Mon Sep 17 00:00:00 2001 From: alperozturk Date: Wed, 10 Jan 2024 10:07:58 +0100 Subject: [PATCH] Add long running task support Signed-off-by: alperozturk --- app/src/main/AndroidManifest.xml | 6 ++++++ .../files/downloader/DownloadNotificationManager.kt | 10 +++++++++- .../client/files/downloader/FileDownloadWorker.kt | 10 ++++++++++ .../com/nextcloud/utils/ForegroundServiceHelper.kt | 13 +++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f83f59ef32..4fda4a4860 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -239,6 +239,12 @@ android:exported="false" android:configChanges="orientation|screenLayout|screenSize|keyboardHidden" android:theme="@style/Theme.ownCloud.Media" /> + diff --git a/app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt b/app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt index f3c7cb0d5b..60ce0c1911 100644 --- a/app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt +++ b/app/src/main/java/com/nextcloud/client/files/downloader/DownloadNotificationManager.kt @@ -111,7 +111,7 @@ class DownloadNotificationManager( @Suppress("MagicNumber") fun showCompleteNotification(text: String) { Handler(Looper.getMainLooper()).postDelayed({ - updateNotificationText(null,text, true) + updateNotificationText(null, text, true) dismissNotification() }, 3000) } @@ -156,4 +156,12 @@ class DownloadNotificationManager( ) ) } + + fun getId(): Int { + return id + } + + fun getNotification(): Notification { + return notificationBuilder.build() + } } diff --git a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt index bf3c165f3d..b4534cfa0a 100644 --- a/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt +++ b/app/src/main/java/com/nextcloud/client/files/downloader/FileDownloadWorker.kt @@ -36,8 +36,10 @@ import com.nextcloud.client.account.UserAccountManager import com.nextcloud.java.util.Optional import com.nextcloud.model.WorkerState import com.nextcloud.model.WorkerStateLiveData +import com.nextcloud.utils.ForegroundServiceHelper import com.owncloud.android.R import com.owncloud.android.datamodel.FileDataStorageManager +import com.owncloud.android.datamodel.ForegroundServiceType import com.owncloud.android.datamodel.OCFile import com.owncloud.android.files.services.IndexedForest import com.owncloud.android.lib.common.OwnCloudAccount @@ -123,6 +125,14 @@ class FileDownloadWorker( addAccountUpdateListener() + setForegroundAsync( + ForegroundServiceHelper.createWorkerForegroundInfo( + notificationManager.getId(), + notificationManager.getNotification(), + ForegroundServiceType.DataSync + ) + ) + requestDownloads.forEach { downloadFile(it) } diff --git a/app/src/main/java/com/nextcloud/utils/ForegroundServiceHelper.kt b/app/src/main/java/com/nextcloud/utils/ForegroundServiceHelper.kt index 9dd7d8008c..9c9676679d 100644 --- a/app/src/main/java/com/nextcloud/utils/ForegroundServiceHelper.kt +++ b/app/src/main/java/com/nextcloud/utils/ForegroundServiceHelper.kt @@ -25,6 +25,7 @@ import android.app.Notification import android.app.Service import android.os.Build import androidx.core.app.ServiceCompat +import androidx.work.ForegroundInfo import com.owncloud.android.datamodel.ForegroundServiceType object ForegroundServiceHelper { @@ -45,4 +46,16 @@ object ForegroundServiceHelper { service.startForeground(id, notification) } } + + fun createWorkerForegroundInfo( + id: Int, + notification: Notification, + foregroundServiceType: ForegroundServiceType + ): ForegroundInfo { + return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + ForegroundInfo(id, notification, foregroundServiceType.getId()) + } else { + ForegroundInfo(id, notification) + } + } }