Merge "Long-press to configure quick affordances (2/2)." into tm-qpr-dev
diff --git a/src/com/android/customization/module/ThemePickerInjector.kt b/src/com/android/customization/module/ThemePickerInjector.kt
index 5b43654..ed4e935 100644
--- a/src/com/android/customization/module/ThemePickerInjector.kt
+++ b/src/com/android/customization/module/ThemePickerInjector.kt
@@ -175,7 +175,9 @@
                     getKeyguardQuickAffordancePickerInteractor(context),
                     getUndoInteractor(context),
                     getCurrentWallpaperInfoFactory(context),
-                )
+                ) { intent ->
+                    context.startActivity(intent)
+                }
                 .also { keyguardQuickAffordancePickerViewModelFactory = it }
     }
 
diff --git a/src/com/android/customization/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepository.kt b/src/com/android/customization/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepository.kt
index fd553fe..c432bd9 100644
--- a/src/com/android/customization/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepository.kt
+++ b/src/com/android/customization/picker/quickaffordance/data/repository/KeyguardQuickAffordancePickerRepository.kt
@@ -83,6 +83,7 @@
             enablementInstructions = enablementInstructions ?: emptyList(),
             enablementActionText = enablementActionText,
             enablementActionComponentName = enablementActionComponentName,
+            configureIntent = configureIntent,
         )
     }
 
diff --git a/src/com/android/customization/picker/quickaffordance/shared/model/KeyguardQuickAffordancePickerAffordanceModel.kt b/src/com/android/customization/picker/quickaffordance/shared/model/KeyguardQuickAffordancePickerAffordanceModel.kt
index 1b18af7..7b04ff1 100644
--- a/src/com/android/customization/picker/quickaffordance/shared/model/KeyguardQuickAffordancePickerAffordanceModel.kt
+++ b/src/com/android/customization/picker/quickaffordance/shared/model/KeyguardQuickAffordancePickerAffordanceModel.kt
@@ -17,6 +17,7 @@
 
 package com.android.customization.picker.quickaffordance.shared.model
 
+import android.content.Intent
 import androidx.annotation.DrawableRes
 
 /** Models a quick affordance. */
@@ -42,4 +43,6 @@
      * user to a destination where they can re-enable it.
      */
     val enablementActionComponentName: String?,
+    /** Optional [Intent] to use to start an activity to configure this affordance. */
+    val configureIntent: Intent?,
 )
diff --git a/src/com/android/customization/picker/quickaffordance/ui/adapter/AffordancesAdapter.kt b/src/com/android/customization/picker/quickaffordance/ui/adapter/AffordancesAdapter.kt
index b0dc350..e11643a 100644
--- a/src/com/android/customization/picker/quickaffordance/ui/adapter/AffordancesAdapter.kt
+++ b/src/com/android/customization/picker/quickaffordance/ui/adapter/AffordancesAdapter.kt
@@ -74,6 +74,15 @@
                 null
             }
         )
+        holder.itemView.onLongClickListener =
+            if (item.onLongClicked != null) {
+                View.OnLongClickListener {
+                    item.onLongClicked.invoke()
+                    true
+                }
+            } else {
+                null
+            }
         holder.iconContainerView.setBackgroundResource(
             if (item.isSelected) {
                 R.drawable.keyguard_quick_affordance_icon_container_background_selected
diff --git a/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModel.kt b/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModel.kt
index d879045..1b3dbe3 100644
--- a/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModel.kt
+++ b/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModel.kt
@@ -54,6 +54,7 @@
     private val quickAffordanceInteractor: KeyguardQuickAffordancePickerInteractor,
     undoInteractor: UndoInteractor,
     private val wallpaperInfoFactory: CurrentWallpaperInfoFactory,
+    activityStarter: (Intent) -> Unit,
 ) : ViewModel() {
 
     @SuppressLint("StaticFieldLeak") private val applicationContext = context.applicationContext
@@ -129,6 +130,7 @@
                                         contentDescription = affordanceModel.name,
                                         isSelected = true,
                                         onClicked = null,
+                                        onLongClicked = null,
                                         isEnabled = affordanceModel.isEnabled,
                                     )
                                 },
@@ -201,6 +203,12 @@
                                     )
                                 }
                             },
+                        onLongClicked =
+                            if (affordance.configureIntent != null) {
+                                { activityStarter(affordance.configureIntent) }
+                            } else {
+                                null
+                            },
                         isEnabled = affordance.isEnabled,
                     )
                 }
@@ -374,6 +382,7 @@
         private val quickAffordanceInteractor: KeyguardQuickAffordancePickerInteractor,
         private val undoInteractor: UndoInteractor,
         private val wallpaperInfoFactory: CurrentWallpaperInfoFactory,
+        private val activityStarter: (Intent) -> Unit,
     ) : ViewModelProvider.Factory {
         override fun <T : ViewModel> create(modelClass: Class<T>): T {
             @Suppress("UNCHECKED_CAST")
@@ -382,6 +391,7 @@
                 quickAffordanceInteractor = quickAffordanceInteractor,
                 undoInteractor = undoInteractor,
                 wallpaperInfoFactory = wallpaperInfoFactory,
+                activityStarter = activityStarter,
             )
                 as T
         }
diff --git a/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordanceViewModel.kt b/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordanceViewModel.kt
index d720b0c..8f6d327 100644
--- a/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordanceViewModel.kt
+++ b/src/com/android/customization/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordanceViewModel.kt
@@ -38,6 +38,9 @@
 
     /** Notifies that the quick affordance has been clicked by the user. */
     val onClicked: (() -> Unit)?,
+
+    /** Notifies that the quick affordance has been long-clicked by the user. */
+    val onLongClicked: (() -> Unit)?,
 ) {
     companion object {
         @SuppressLint("UseCompatLoadingForDrawables")
@@ -56,6 +59,7 @@
                     } else {
                         onSelected
                     },
+                onLongClicked = null,
                 isEnabled = true,
             )
         }
diff --git a/tests/src/com/android/customization/model/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModelTest.kt b/tests/src/com/android/customization/model/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModelTest.kt
index d1214c1..cf09f9b 100644
--- a/tests/src/com/android/customization/model/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModelTest.kt
+++ b/tests/src/com/android/customization/model/picker/quickaffordance/ui/viewmodel/KeyguardQuickAffordancePickerViewModelTest.kt
@@ -18,6 +18,7 @@
 package com.android.customization.model.picker.quickaffordance.ui.viewmodel
 
 import android.content.Context
+import android.content.Intent
 import androidx.test.filters.SmallTest
 import androidx.test.platform.app.InstrumentationRegistry
 import com.android.customization.picker.quickaffordance.data.repository.KeyguardQuickAffordancePickerRepository
@@ -65,6 +66,8 @@
     private lateinit var client: FakeCustomizationProviderClient
     private lateinit var quickAffordanceInteractor: KeyguardQuickAffordancePickerInteractor
 
+    private var latestStartedActivityIntent: Intent? = null
+
     @Before
     fun setUp() {
         InjectorProvider.setInjector(TestInjector())
@@ -102,6 +105,7 @@
                     quickAffordanceInteractor = quickAffordanceInteractor,
                     undoInteractor = undoInteractor,
                     wallpaperInfoFactory = TestCurrentWallpaperInfoFactory(context),
+                    activityStarter = { intent -> latestStartedActivityIntent = intent },
                 )
                 .create(KeyguardQuickAffordancePickerViewModel::class.java)
     }
@@ -273,6 +277,30 @@
         }
 
     @Test
+    fun `Start settings activity when long-pressing an affordance`() =
+        testScope.runTest {
+            val quickAffordances = collectLastValue(underTest.quickAffordances)
+
+            // Lets add a configurable affordance to the picker:
+            val configureIntent = Intent("some.action")
+            val affordanceIndex =
+                client.addAffordance(
+                    CustomizationProviderClient.Affordance(
+                        id = "affordance",
+                        name = "affordance",
+                        iconResourceId = 1,
+                        isEnabled = true,
+                        configureIntent = configureIntent,
+                    )
+                )
+
+            // Lets try to long-click the affordance:
+            quickAffordances()?.get(affordanceIndex + 1)?.onLongClicked?.invoke()
+
+            assertThat(latestStartedActivityIntent).isEqualTo(configureIntent)
+        }
+
+    @Test
     fun `summary - affordance selected in both bottom-start and bottom-end`() =
         testScope.runTest {
             val slots = collectLastValue(underTest.slots)