-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix: Sync Media Fail Notifications are expandable and copyable #20976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // SPDX-License-Identifier: GPL-3.0-or-later | ||
| // SPDX-FileCopyrightText: Copyright (c) 2026 LUwUcifer <luwucifwer@proton.me> | ||
| package com.ichi2.anki.receiver | ||
|
|
||
| import android.content.BroadcastReceiver | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import androidx.core.app.NotificationManagerCompat | ||
| import com.ichi2.anki.R | ||
| import com.ichi2.anki.common.utils.android.showThemedToast | ||
| import com.ichi2.anki.notifications.NotificationId | ||
| import com.ichi2.utils.copyToClipboard | ||
| import timber.log.Timber | ||
|
|
||
| class CopyToClipboardReceiver : BroadcastReceiver() { | ||
| override fun onReceive( | ||
| context: Context, | ||
| intent: Intent, | ||
| ) { | ||
| val text = | ||
| intent.getStringExtra(EXTRA_SYNC_ERROR_LOG) ?: run { | ||
| Timber.w("CopyToClipboardReceiver: no error log found") | ||
| showThemedToast(context, R.string.something_wrong, shortLength = true) | ||
| return | ||
| } | ||
| NotificationManagerCompat.from(context).cancel(NotificationId.SYNC_MEDIA) | ||
| context.copyToClipboard(text) | ||
| } | ||
|
|
||
| companion object { | ||
| const val EXTRA_SYNC_ERROR_LOG = "COPY ERROR" | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,9 @@ | |
| package com.ichi2.anki.worker | ||
|
|
||
| import android.app.Notification | ||
| import android.app.PendingIntent | ||
| import android.content.Context | ||
| import android.content.Intent | ||
| import android.content.pm.ServiceInfo | ||
| import android.os.Build | ||
| import androidx.core.app.NotificationCompat | ||
|
|
@@ -37,9 +39,12 @@ import anki.sync.SyncAuth | |
| import anki.sync.syncAuth | ||
| import com.ichi2.anki.Channel | ||
| import com.ichi2.anki.CollectionManager | ||
| import com.ichi2.anki.CollectionManager.TR | ||
| import com.ichi2.anki.R | ||
| import com.ichi2.anki.cancelMediaSync | ||
| import com.ichi2.anki.notifications.NotificationId | ||
| import com.ichi2.anki.receiver.CopyToClipboardReceiver | ||
| import com.ichi2.anki.ui.internationalization.sentenceCase | ||
| import com.ichi2.anki.utils.ext.trySetForeground | ||
| import com.ichi2.utils.Permissions | ||
| import kotlinx.coroutines.CancellationException | ||
|
|
@@ -91,9 +96,19 @@ class SyncMediaWorker( | |
| } catch (throwable: Throwable) { | ||
| Timber.w(throwable, "SyncMediaWorker failed") | ||
| notify { | ||
| setContentTitle(CollectionManager.TR.syncMediaFailed()) | ||
| setContentTitle(TR.syncMediaFailed()) | ||
| throwable.localizedMessage?.let { message -> | ||
| setContentText(message) | ||
| setStyle( | ||
| NotificationCompat | ||
| .BigTextStyle() | ||
| .bigText(message), | ||
| ) | ||
| addAction( | ||
| R.drawable.baseline_content_copy_24, | ||
| with(applicationContext) { TR.sentenceCase.copyToClipboard }, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why Application context over |
||
| getCopyToClipboardIntent(message), | ||
| ) | ||
| } | ||
| } | ||
| Timber.d("SyncMediaWorker: showing failure notification") | ||
|
|
@@ -129,7 +144,7 @@ class SyncMediaWorker( | |
|
|
||
| override suspend fun getForegroundInfo(): ForegroundInfo { | ||
| val title = applicationContext.getString(R.string.syncing_media) | ||
| val cancelTitle = CollectionManager.TR.syncAbortButton() | ||
| val cancelTitle = TR.syncAbortButton() | ||
| val notification = | ||
| buildNotification { | ||
| setContentTitle(title) | ||
|
|
@@ -145,6 +160,19 @@ class SyncMediaWorker( | |
| } | ||
| } | ||
|
|
||
| private fun getCopyToClipboardIntent(text: String): PendingIntent { | ||
| val intent = | ||
| Intent(applicationContext, CopyToClipboardReceiver::class.java).apply { | ||
| putExtra(CopyToClipboardReceiver.EXTRA_SYNC_ERROR_LOG, text) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a limit to text, about 1MB, it needs to be checked and trimmed
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would that issue ever arise given error messages are exclusively provided from the backend?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unlikely, most likely from a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this was handled |
||
| } | ||
| return PendingIntent.getBroadcast( | ||
| applicationContext, | ||
| 0, | ||
| intent, | ||
| PendingIntent.FLAG_UPDATE_CURRENT, | ||
| ) | ||
| } | ||
|
|
||
| private fun notify(notification: Notification) = notificationManager?.notify(NotificationId.SYNC_MEDIA, notification) | ||
|
|
||
| private fun notify(builder: NotificationCompat.Builder.() -> Unit) { | ||
|
|
@@ -164,7 +192,7 @@ class SyncMediaWorker( | |
|
|
||
| private fun getProgressNotification(progress: CharSequence): Notification { | ||
| val title = applicationContext.getString(R.string.syncing_media) | ||
| val cancelTitle = CollectionManager.TR.syncAbortButton() | ||
| val cancelTitle = TR.syncAbortButton() | ||
|
|
||
| return buildNotification { | ||
| setContentTitle(title) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.