Skip to content

Commit a47a108

Browse files
committed
fix notification error id handling
Signed-off-by: alperozturk <[email protected]>
1 parent fb3a7dd commit a47a108

File tree

5 files changed

+31
-40
lines changed

5 files changed

+31
-40
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class AutoUploadWorker(
6363
private val syncedFolderProvider: SyncedFolderProvider,
6464
private val backgroundJobManager: BackgroundJobManager,
6565
private val repository: FileSystemRepository,
66-
val viewThemeUtils: ViewThemeUtils,
66+
val viewThemeUtils: ViewThemeUtils
6767
) : CoroutineWorker(context, params) {
6868

6969
companion object {

app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadBroadcastReceiver.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
package com.nextcloud.client.jobs.upload
99

1010
import android.app.NotificationManager
11+
import android.app.PendingIntent
1112
import android.content.BroadcastReceiver
1213
import android.content.Context
1314
import android.content.Intent
1415
import com.owncloud.android.MainApp
1516
import com.owncloud.android.datamodel.UploadsStorageManager
16-
import com.owncloud.android.ui.notifications.NotificationUtils
1717
import javax.inject.Inject
1818

1919
class FileUploadBroadcastReceiver : BroadcastReceiver() {
@@ -22,27 +22,35 @@ class FileUploadBroadcastReceiver : BroadcastReceiver() {
2222
lateinit var uploadsStorageManager: UploadsStorageManager
2323

2424
companion object {
25-
const val UPLOAD_ID = "UPLOAD_ID"
26-
const val REMOTE_PATH = "REMOTE_PATH"
27-
const val STORAGE_PATH = "STORAGE_PATH"
25+
private const val UPLOAD_ID = "UPLOAD_ID"
26+
27+
fun getBroadcast(context: Context, id: Int): PendingIntent {
28+
val intent = Intent(context, FileUploadBroadcastReceiver::class.java).apply {
29+
putExtra(UPLOAD_ID, id)
30+
setClass(context, FileUploadBroadcastReceiver::class.java)
31+
setPackage(context.packageName)
32+
}
33+
34+
return PendingIntent.getBroadcast(
35+
context,
36+
id,
37+
intent,
38+
PendingIntent.FLAG_IMMUTABLE
39+
)
40+
}
2841
}
2942

3043
@Suppress("ReturnCount")
3144
override fun onReceive(context: Context, intent: Intent) {
3245
MainApp.getAppComponent().inject(this)
3346

34-
val remotePath = intent.getStringExtra(REMOTE_PATH) ?: return
35-
val storagePath = intent.getStringExtra(STORAGE_PATH) ?: return
3647
val uploadId = intent.getLongExtra(UPLOAD_ID, -1L)
3748
if (uploadId == -1L) {
3849
return
3950
}
4051

4152
uploadsStorageManager.removeUpload(uploadId)
4253
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
43-
notificationManager.cancel(
44-
NotificationUtils.createUploadNotificationTag(remotePath, storagePath),
45-
FileUploadWorker.NOTIFICATION_ERROR_ID
46-
)
54+
notificationManager.cancel(uploadId.toInt())
4755
}
4856
}

app/src/main/java/com/nextcloud/client/jobs/upload/FileUploadWorker.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ class FileUploadWorker(
353353
notificationManager,
354354
operation,
355355
result,
356-
showSameFileAlreadyExistsNotification = {
356+
showSameFileAlreadyExistsNotification = {
357357
withContext(Dispatchers.Main) {
358358
val showSameFileAlreadyExistsNotification =
359359
inputData.getBoolean(SHOW_SAME_FILE_ALREADY_EXISTS_NOTIFICATION, false)

app/src/main/java/com/nextcloud/client/jobs/utils/UploadErrorNotificationManager.kt

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import com.owncloud.android.utils.ErrorMessageAdapter
2727
import kotlinx.coroutines.Dispatchers
2828
import kotlinx.coroutines.withContext
2929
import java.io.File
30-
import java.security.SecureRandom
3130

3231
object UploadErrorNotificationManager {
3332
private const val TAG = "UploadErrorNotificationManager"
@@ -38,7 +37,7 @@ object UploadErrorNotificationManager {
3837
operation: UploadFileOperation,
3938
result: RemoteOperationResult<Any?>,
4039
showSameFileAlreadyExistsNotification: suspend () -> Unit = {}
41-
) {
40+
) {
4241
Log_OC.d(TAG, "handle upload result with result code: " + result.code)
4342

4443
val notification = withContext(Dispatchers.IO) {
@@ -66,7 +65,7 @@ object UploadErrorNotificationManager {
6665

6766
withContext(Dispatchers.Main) {
6867
if (result.code.isFileSpecificError()) {
69-
notificationManager.showNotification(operation.file.fileId.toInt(), notification)
68+
notificationManager.showNotification(operation.ocUploadId.toInt(), notification)
7069
} else {
7170
notificationManager.showNotification(notification)
7271
}
@@ -106,7 +105,7 @@ object UploadErrorNotificationManager {
106105
addAction(
107106
R.drawable.ic_delete,
108107
context.getString(R.string.upload_list_cancel_upload),
109-
cancelUploadPendingIntent(context, operation)
108+
FileUploadBroadcastReceiver.getBroadcast(context, operation.ocUploadId.toInt())
110109
)
111110
}
112111

@@ -147,31 +146,19 @@ object UploadErrorNotificationManager {
147146
val intent = ConflictsResolveActivity.createIntent(
148147
operation.file,
149148
operation.user,
150-
operation.ocUploadId,
149+
conflictUploadId = operation.ocUploadId,
151150
Intent.FLAG_ACTIVITY_CLEAR_TOP,
152151
context
153152
).apply {
154153
setClass(context, ConflictsResolveActivity::class.java)
155154
setPackage(context.packageName)
156155
}
157156

158-
return PendingIntent.getActivity(context, SecureRandom().nextInt(), intent, PendingIntent.FLAG_IMMUTABLE)
159-
}
160-
161-
private fun cancelUploadPendingIntent(context: Context, operation: UploadFileOperation): PendingIntent {
162-
val intent = Intent(context, FileUploadBroadcastReceiver::class.java).apply {
163-
putExtra(FileUploadBroadcastReceiver.UPLOAD_ID, operation.ocUploadId)
164-
putExtra(FileUploadBroadcastReceiver.REMOTE_PATH, operation.file.remotePath)
165-
putExtra(FileUploadBroadcastReceiver.STORAGE_PATH, operation.file.storagePath)
166-
setClass(context, FileUploadBroadcastReceiver::class.java)
167-
setPackage(context.packageName)
168-
}
169-
170-
return PendingIntent.getBroadcast(
157+
return PendingIntent.getActivity(
171158
context,
172-
operation.file.fileId.toInt(),
159+
operation.ocUploadId.toInt(),
173160
intent,
174-
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
161+
PendingIntent.FLAG_IMMUTABLE
175162
)
176163
}
177164

app/src/main/java/com/owncloud/android/ui/activity/ConflictsResolveActivity.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ class ConflictsResolveActivity :
130130
updateThumbnailIfNeeded(decision, file, oldFile)
131131
}
132132

133-
dismissConflictResolveNotification(file)
133+
dismissConflictResolveNotification()
134134
finish()
135135
}
136136
}
@@ -148,13 +148,9 @@ class ConflictsResolveActivity :
148148
}
149149
}
150150

151-
// notification id must be file id because only if upload failed via SYNC_CONFLICT can create conflict
152-
// resolve activity
153-
private fun dismissConflictResolveNotification(file: OCFile?) {
154-
file?.let {
155-
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
156-
notificationManager.cancel(file.fileId.toInt())
157-
}
151+
private fun dismissConflictResolveNotification() {
152+
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
153+
notificationManager.cancel(conflictUploadId.toInt())
158154
}
159155

160156
private fun keepBothFolder(offlineOperation: OfflineOperationEntity?, serverFile: OCFile?) {

0 commit comments

Comments
 (0)