Skip to content

Commit 7993447

Browse files
committed
fix
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent c91ac0a commit 7993447

File tree

3 files changed

+18
-35
lines changed

3 files changed

+18
-35
lines changed

app/src/main/java/com/nextcloud/client/jobs/autoUpload/AutoUploadWorker.kt

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ import com.nextcloud.client.jobs.upload.FileUploadWorker
2525
import com.nextcloud.client.jobs.utils.UploadErrorNotificationManager
2626
import com.nextcloud.client.network.ConnectivityService
2727
import com.nextcloud.client.preferences.SubFolderRule
28-
import com.nextcloud.utils.ForegroundServiceHelper
2928
import com.nextcloud.utils.extensions.updateStatus
3029
import com.owncloud.android.R
3130
import com.owncloud.android.datamodel.ArbitraryDataProviderImpl
3231
import com.owncloud.android.datamodel.FileDataStorageManager
33-
import com.owncloud.android.datamodel.ForegroundServiceType
3432
import com.owncloud.android.datamodel.MediaFolderType
3533
import com.owncloud.android.datamodel.SyncedFolder
3634
import com.owncloud.android.datamodel.SyncedFolderProvider
@@ -85,8 +83,6 @@ class AutoUploadWorker(
8583
syncedFolder = syncedFolderProvider.getSyncedFolderByID(syncFolderId)
8684
?.takeIf { it.isEnabled } ?: return Result.failure()
8785

88-
trySetForeground()
89-
9086
/**
9187
* Receives from [com.nextcloud.client.jobs.ContentObserverWork.checkAndTriggerAutoUpload]
9288
*/
@@ -97,7 +93,6 @@ class AutoUploadWorker(
9793
}
9894

9995
collectFileChangesFromContentObserverWork(contentUris)
100-
updateNotification()
10196
uploadFiles(syncedFolder)
10297

10398
Log_OC.d(TAG, "${syncedFolder.remotePath} finished checking files.")
@@ -109,18 +104,11 @@ class AutoUploadWorker(
109104
}
110105

111106
override suspend fun getForegroundInfo(): ForegroundInfo {
112-
val notification = createNotification(
113-
context.getString(R.string.upload_files)
114-
)
115-
116-
return ForegroundServiceHelper.createWorkerForegroundInfo(
117-
NOTIFICATION_ID,
118-
notification,
119-
ForegroundServiceType.DataSync
120-
)
107+
val notification = createNotification(context.getString(R.string.upload_files))
108+
return notificationManager.getForegroundInfo(notification)
121109
}
122110

123-
private fun updateNotification() {
111+
private suspend fun updateNotification() {
124112
getStartNotificationTitle()?.let { (localFolderName, remoteFolderName) ->
125113
try {
126114
val startNotification = createNotification(
@@ -131,7 +119,7 @@ class AutoUploadWorker(
131119
)
132120
)
133121

134-
notificationManager.showNotification(startNotification)
122+
setForeground(notificationManager.getForegroundInfo(startNotification))
135123
} catch (e: Exception) {
136124
Log_OC.w(TAG, "⚠️ Could not update notification: ${e.message}")
137125
}
@@ -141,21 +129,12 @@ class AutoUploadWorker(
141129
private suspend fun trySetForeground() {
142130
try {
143131
val notification = createNotification(context.getString(R.string.upload_files))
144-
updateForegroundInfo(notification)
132+
setForeground(notificationManager.getForegroundInfo(notification))
145133
} catch (e: Exception) {
146134
Log_OC.w(TAG, "⚠️ Could not set foreground service: ${e.message}")
147135
}
148136
}
149137

150-
private suspend fun updateForegroundInfo(notification: Notification) {
151-
val foregroundInfo = ForegroundServiceHelper.createWorkerForegroundInfo(
152-
NOTIFICATION_ID,
153-
notification,
154-
ForegroundServiceType.DataSync
155-
)
156-
setForeground(foregroundInfo)
157-
}
158-
159138
private fun createNotification(title: String): Notification = notificationManager.notificationBuilder
160139
.setContentTitle(title)
161140
.setSmallIcon(R.drawable.uploads)
@@ -297,6 +276,9 @@ class AutoUploadWorker(
297276
val lightVersion = context.resources.getBoolean(R.bool.syncedFolder_light)
298277
val currentLocale = context.resources.configuration.locales[0]
299278

279+
trySetForeground()
280+
updateNotification()
281+
300282
var lastId = 0
301283
while (true) {
302284
val filePathsWithIds = repository.getFilePathsWithIds(syncedFolder, lastId)

app/src/main/java/com/nextcloud/client/jobs/folderDownload/FolderDownloadWorkerNotificationManager.kt

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ import android.content.Context
1313
import android.content.Intent
1414
import androidx.work.ForegroundInfo
1515
import com.nextcloud.client.jobs.notification.WorkerNotificationManager
16-
import com.nextcloud.utils.ForegroundServiceHelper
1716
import com.owncloud.android.R
18-
import com.owncloud.android.datamodel.ForegroundServiceType
1917
import com.owncloud.android.datamodel.OCFile
2018
import com.owncloud.android.ui.notifications.NotificationUtils
2119
import com.owncloud.android.utils.theme.ViewThemeUtils
@@ -109,13 +107,6 @@ class FolderDownloadWorkerNotificationManager(private val context: Context, view
109107
return getForegroundInfo(notification)
110108
}
111109

112-
fun getForegroundInfo(notification: Notification): ForegroundInfo =
113-
ForegroundServiceHelper.createWorkerForegroundInfo(
114-
NOTIFICATION_ID,
115-
notification,
116-
ForegroundServiceType.DataSync
117-
)
118-
119110
fun dismiss() {
120111
notificationManager.cancel(NOTIFICATION_ID)
121112
}

app/src/main/java/com/nextcloud/client/jobs/notification/WorkerNotificationManager.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ import android.graphics.BitmapFactory
1414
import android.os.Handler
1515
import android.os.Looper
1616
import androidx.core.app.NotificationCompat
17+
import androidx.work.ForegroundInfo
18+
import com.nextcloud.utils.ForegroundServiceHelper
1719
import com.owncloud.android.R
20+
import com.owncloud.android.datamodel.ForegroundServiceType
1821
import com.owncloud.android.utils.theme.ViewThemeUtils
1922

2023
open class WorkerNotificationManager(
@@ -75,4 +78,11 @@ open class WorkerNotificationManager(
7578
fun getId(): Int = id
7679

7780
fun getNotification(): Notification = notificationBuilder.build()
81+
82+
fun getForegroundInfo(notification: Notification): ForegroundInfo =
83+
ForegroundServiceHelper.createWorkerForegroundInfo(
84+
id,
85+
notification,
86+
ForegroundServiceType.DataSync
87+
)
7888
}

0 commit comments

Comments
 (0)