Refreshing a widget changes its owning profile
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 52/100
Research direction
Start with the regression scenario in AnkiDroid/src/test/java/com/ichi2/anki/multiprofile/ProfileWidgetRefreshRegressionTest.kt and trace how DeckPickerWidget reads its selected decks during refresh after the active profile changes. The test expects a widget created in Default to stay tied to that source profile and avoid launching Reviewer for Work's deck; add or adjust a regression test and verify the refresh and tap behavior.
Written by the indexing model from the issue text.
Description
Assisted-by: GPT-6.1-Sol xhigh & GPT-6-Astra xhigh
Subject: [EXISTING] Refreshing a widget changes its owning profile
After switching profiles, a periodic widget refresh reads Default's selection and opens the same deck ID in the active profile.
⚠️ This issue existed before the PR and should likely be split off into a separate issue.
---
diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/multiprofile/ProfileWidgetRefreshRegressionTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/multiprofile/ProfileWidgetRefreshRegressionTest.kt
new file mode 100644
index 0000000000000000000000000000000000000000..19b091fc43102853dfe24eee5c0ab39f6511f2cf
--- /dev/null
+++ b/AnkiDroid/src/test/java/com/ichi2/anki/multiprofile/ProfileWidgetRefreshRegressionTest.kt
@@ -0,0 +1,105 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package com.ichi2.anki.multiprofile
+
+import android.appwidget.AppWidgetManager
+import android.content.ComponentName
+import android.content.Context
+import android.content.ContextWrapper
+import android.content.Intent
+import android.os.Bundle
+import android.view.View
+import android.view.ViewGroup
+import androidx.core.content.edit
+import androidx.test.ext.junit.runners.AndroidJUnit4
+import com.ichi2.anki.AnkiDroidApp
+import com.ichi2.anki.CollectionManager
+import com.ichi2.anki.IntentHandler
+import com.ichi2.anki.Reviewer
+import com.ichi2.anki.RobolectricTest
+import com.ichi2.widget.ACTION_UPDATE_WIDGET
+import com.ichi2.widget.AppWidgetId
+import com.ichi2.widget.deckpicker.DeckPickerWidget
+import com.ichi2.widget.deckpicker.DeckPickerWidgetPreferences
+import kotlinx.coroutines.test.TestScope
+import kotlinx.coroutines.test.advanceUntilIdle
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.robolectric.Robolectric
+import org.robolectric.Shadows.shadowOf
+import kotlin.test.assertFalse
+import kotlin.test.assertNotNull
+import com.ichi2.anki.widgets.R as WidgetR
+
+@RunWith(AndroidJUnit4::class)
+class ProfileWidgetRefreshRegressionTest : RobolectricTest() {
+ override fun getCollectionStorageMode() = CollectionStorageMode.ON_DISK
+
+ @Test
+ fun `refreshing a widget preserves its source profile`() =
+ runTest {
+ val app = AnkiDroidApp.instance
+ val original = requireNotNull(app.profileManager)
+ val work = original.createNewProfile((ProfileName.validate("Work") as ProfileName.ValidationResult.Valid).name)
+ addNoteToDeck(1L, count = 1)
+ DeckPickerWidgetPreferences(app.baseContext).saveSelectedDecks(AppWidgetId(101), listOf("1"))
+ renderWidget(101)
+ try {
+ CollectionManager.discardBackend()
+ app.baseContext.getSharedPreferences(ProfileManager.PROFILE_REGISTRY_FILENAME, Context.MODE_PRIVATE).edit(commit = true) {
+ putString(ProfileManager.PREF_LAST_ACTIVE_PROFILE_ID, work.value)
+ }
+ app.profileManager =
+ ProfileManager.create(
+ object : ContextWrapper(app.baseContext) {
+ override fun getApplicationContext(): Context? = null
+ },
+ )
+ addNoteToDeck(1L, count = 1)
+ DeckPickerWidget().onReceive(
+ app.baseContext,
+ Intent(ACTION_UPDATE_WIDGET).putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 101),
+ )
+ advanceUntilIdle()
+ val manager = AppWidgetManager.getInstance(app)
+ val widget = assertNotNull(shadowOf(manager).getViewFor(101))
+ val row = widget.findViewById<ViewGroup>(WidgetR.id.deckCollection).getChildAt(0)
+ row.findViewById<View>(WidgetR.id.widget_deck_row).performClick()
+ val tap = assertNotNull(shadowOf(app).nextStartedActivity)
+ Robolectric.buildActivity(IntentHandler::class.java, tap).use { controller ->
+ controller.create()
+ val launches =
+ buildList {
+ val activity = shadowOf(controller.get())
+ while (true) add(activity.nextStartedActivity ?: break)
+ }
+ assertFalse(
+ launches.any { it.component?.className == Reviewer::class.java.name },
+ "Refreshing Default's widget must not retarget it to Work's deck",
+ )
+ }
+ } finally {
+ CollectionManager.discardBackend()
+ app.profileManager = original
+ }
+ }
+
+ private fun TestScope.renderWidget(id: Int): View {
+ val app = AnkiDroidApp.instance
+ val manager = AppWidgetManager.getInstance(app)
+ shadowOf(manager).bindAppWidgetId(id, ComponentName(app, DeckPickerWidget::class.java))
+ manager.updateAppWidgetOptions(
+ id,
+ Bundle().apply {
+ putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, 349)
+ putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, 349)
+ putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, 220)
+ putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, 220)
+ },
+ )
+ DeckPickerWidget.updateWidget(app, manager, AppWidgetId(id), longArrayOf(1L))
+ advanceUntilIdle()
+ val root = assertNotNull(shadowOf(manager).getViewFor(id))
+ return root.findViewById<ViewGroup>(WidgetR.id.deckCollection).getChildAt(0).findViewById(WidgetR.id.widget_deck_row)
+ }
+}
Originally posted by @david-allison in https://github.com/ankidroid/Anki-Android/pull/23068#pullrequestreview-5925243102
- Dominant language
- Kotlin
- Stars
- 12.3k
- Forks
- 3.1k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 383
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ankidroid/Anki-Android
-
Needs Triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
ankidroid/Anki-Android#23392 ·
Maintainers usually reply within 1 day
-
Needs Triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
ankidroid/Anki-Android#23390 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
ankidroid/Anki-Android#23385 · 1 reaction ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
ankidroid/Anki-Android#23374 ·
Maintainers usually reply within 1 day
-
Selected custom-study tags become unreadablePossibly taken @chesh-ire claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
ankidroid/Anki-Android#23352 · 1 comment · 1 assignee ·
Maintainers usually reply within 1 day
All issues in ankidroid/Anki-Android
Similar issues
-
Bug Domain changed
Difficulty 1/5 Under an hour Newbie friendliness 79/100
keiyoushi/extensions-source#21174 ·
Maintainers usually reply within 1 day
-
Unified account interface not translated into Simplified ChinesePossibly taken @xuenhua claimed this 3 days ago. Openunconfirmed
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
thunderbird/thunderbird-android#11936 · 1 comment ·
Maintainers usually reply within 3 days
-
Update toast stylingOpendesign good first issue help wanted
Difficulty 2/5 1-3 hours Newbie friendliness 73/100
scribe-org/Scribe-Android#778 ·
Maintainers usually reply within 1 day
-
AI Generated CI/CD ⚒️ Flaky-tests 🐦 go 🏃
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
valkey-io/valkey-glide#7772 ·
Maintainers usually reply within 1 day
-
Error Handling Swift
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Automattic/wordpress-rs#1759 ·
Maintainers usually reply within 1 day