Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
*/
Expand All @@ -97,7 +93,6 @@ class AutoUploadWorker(
}

collectFileChangesFromContentObserverWork(contentUris)
updateNotification()
uploadFiles(syncedFolder)

Log_OC.d(TAG, "✅ ${syncedFolder.remotePath} finished checking files.")
Expand All @@ -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(
Expand All @@ -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}")
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
)
}
Loading