diff --git a/app/src/main/java/com/nextcloud/client/jobs/autoUpload/AutoUploadWorker.kt b/app/src/main/java/com/nextcloud/client/jobs/autoUpload/AutoUploadWorker.kt index 2c54909872ae..17b7a7ebad6f 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/autoUpload/AutoUploadWorker.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/autoUpload/AutoUploadWorker.kt @@ -25,12 +25,10 @@ import com.nextcloud.client.jobs.upload.FileUploadWorker import com.nextcloud.client.jobs.utils.UploadErrorNotificationManager import com.nextcloud.client.network.ConnectivityService import com.nextcloud.client.preferences.SubFolderRule -import com.nextcloud.utils.ForegroundServiceHelper import com.nextcloud.utils.extensions.updateStatus import com.owncloud.android.R import com.owncloud.android.datamodel.ArbitraryDataProviderImpl import com.owncloud.android.datamodel.FileDataStorageManager -import com.owncloud.android.datamodel.ForegroundServiceType import com.owncloud.android.datamodel.MediaFolderType import com.owncloud.android.datamodel.SyncedFolder import com.owncloud.android.datamodel.SyncedFolderProvider @@ -85,8 +83,6 @@ class AutoUploadWorker( syncedFolder = syncedFolderProvider.getSyncedFolderByID(syncFolderId) ?.takeIf { it.isEnabled } ?: return Result.failure() - trySetForeground() - /** * Receives from [com.nextcloud.client.jobs.ContentObserverWork.checkAndTriggerAutoUpload] */ @@ -97,7 +93,6 @@ class AutoUploadWorker( } collectFileChangesFromContentObserverWork(contentUris) - updateNotification() uploadFiles(syncedFolder) Log_OC.d(TAG, "✅ ${syncedFolder.remotePath} finished checking files.") @@ -109,18 +104,11 @@ class AutoUploadWorker( } override suspend fun getForegroundInfo(): ForegroundInfo { - val notification = createNotification( - context.getString(R.string.upload_files) - ) - - return ForegroundServiceHelper.createWorkerForegroundInfo( - NOTIFICATION_ID, - notification, - ForegroundServiceType.DataSync - ) + val notification = createNotification(context.getString(R.string.upload_files)) + return notificationManager.getForegroundInfo(notification) } - private fun updateNotification() { + private suspend fun updateNotification() { getStartNotificationTitle()?.let { (localFolderName, remoteFolderName) -> try { val startNotification = createNotification( @@ -131,7 +119,7 @@ class AutoUploadWorker( ) ) - notificationManager.showNotification(startNotification) + setForeground(notificationManager.getForegroundInfo(startNotification)) } catch (e: Exception) { Log_OC.w(TAG, "⚠️ Could not update notification: ${e.message}") } @@ -141,21 +129,12 @@ class AutoUploadWorker( private suspend fun trySetForeground() { try { val notification = createNotification(context.getString(R.string.upload_files)) - updateForegroundInfo(notification) + setForeground(notificationManager.getForegroundInfo(notification)) } catch (e: Exception) { Log_OC.w(TAG, "⚠️ Could not set foreground service: ${e.message}") } } - private suspend fun updateForegroundInfo(notification: Notification) { - val foregroundInfo = ForegroundServiceHelper.createWorkerForegroundInfo( - NOTIFICATION_ID, - notification, - ForegroundServiceType.DataSync - ) - setForeground(foregroundInfo) - } - private fun createNotification(title: String): Notification = notificationManager.notificationBuilder .setContentTitle(title) .setSmallIcon(R.drawable.uploads) @@ -297,6 +276,9 @@ class AutoUploadWorker( val lightVersion = context.resources.getBoolean(R.bool.syncedFolder_light) val currentLocale = context.resources.configuration.locales[0] + trySetForeground() + updateNotification() + var lastId = 0 while (true) { val filePathsWithIds = repository.getFilePathsWithIds(syncedFolder, lastId) diff --git a/app/src/main/java/com/nextcloud/client/jobs/folderDownload/FolderDownloadWorkerNotificationManager.kt b/app/src/main/java/com/nextcloud/client/jobs/folderDownload/FolderDownloadWorkerNotificationManager.kt index 8502f4632ad6..fed0b8feaff0 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/folderDownload/FolderDownloadWorkerNotificationManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/folderDownload/FolderDownloadWorkerNotificationManager.kt @@ -13,9 +13,7 @@ import android.content.Context import android.content.Intent import androidx.work.ForegroundInfo import com.nextcloud.client.jobs.notification.WorkerNotificationManager -import com.nextcloud.utils.ForegroundServiceHelper import com.owncloud.android.R -import com.owncloud.android.datamodel.ForegroundServiceType import com.owncloud.android.datamodel.OCFile import com.owncloud.android.ui.notifications.NotificationUtils import com.owncloud.android.utils.theme.ViewThemeUtils @@ -109,13 +107,6 @@ class FolderDownloadWorkerNotificationManager(private val context: Context, view return getForegroundInfo(notification) } - fun getForegroundInfo(notification: Notification): ForegroundInfo = - ForegroundServiceHelper.createWorkerForegroundInfo( - NOTIFICATION_ID, - notification, - ForegroundServiceType.DataSync - ) - fun dismiss() { notificationManager.cancel(NOTIFICATION_ID) } diff --git a/app/src/main/java/com/nextcloud/client/jobs/notification/WorkerNotificationManager.kt b/app/src/main/java/com/nextcloud/client/jobs/notification/WorkerNotificationManager.kt index 573e1d1d02ac..e5c9f31bb73b 100644 --- a/app/src/main/java/com/nextcloud/client/jobs/notification/WorkerNotificationManager.kt +++ b/app/src/main/java/com/nextcloud/client/jobs/notification/WorkerNotificationManager.kt @@ -14,7 +14,10 @@ import android.graphics.BitmapFactory import android.os.Handler import android.os.Looper import androidx.core.app.NotificationCompat +import androidx.work.ForegroundInfo +import com.nextcloud.utils.ForegroundServiceHelper import com.owncloud.android.R +import com.owncloud.android.datamodel.ForegroundServiceType import com.owncloud.android.utils.theme.ViewThemeUtils open class WorkerNotificationManager( @@ -75,4 +78,11 @@ open class WorkerNotificationManager( fun getId(): Int = id fun getNotification(): Notification = notificationBuilder.build() + + fun getForegroundInfo(notification: Notification): ForegroundInfo = + ForegroundServiceHelper.createWorkerForegroundInfo( + id, + notification, + ForegroundServiceType.DataSync + ) }