Merge "Fix other colors loading" into main
diff --git a/src/com/android/customization/model/grid/DefaultShapeGridManager.kt b/src/com/android/customization/model/grid/DefaultShapeGridManager.kt
index 6f20dd3..f8b0c3a 100644
--- a/src/com/android/customization/model/grid/DefaultShapeGridManager.kt
+++ b/src/com/android/customization/model/grid/DefaultShapeGridManager.kt
@@ -18,7 +18,9 @@
 
 import android.content.ContentValues
 import android.content.Context
+import android.content.res.Resources
 import android.graphics.drawable.Drawable
+import android.util.Log
 import androidx.core.content.res.ResourcesCompat
 import com.android.wallpaper.R
 import com.android.wallpaper.picker.di.modules.BackgroundDispatcher
@@ -156,15 +158,26 @@
         )
     }
 
-    override fun getGridOptionDrawble(iconId: Int): Drawable? {
-        return ResourcesCompat.getDrawable(
-            context.packageManager.getResourcesForApplication(APP_RESOURCES_PACKAGE_NAME),
-            iconId,
-            /* theme = */ null,
-        )
+    override fun getGridOptionDrawable(iconId: Int): Drawable? {
+        try {
+            val drawable =
+                ResourcesCompat.getDrawable(
+                    context.packageManager.getResourcesForApplication(APP_RESOURCES_PACKAGE_NAME),
+                    iconId,
+                    /* theme = */ null,
+                )
+            return drawable
+        } catch (exception: Resources.NotFoundException) {
+            Log.w(
+                TAG,
+                "Unable to find drawable resource from package $APP_RESOURCES_PACKAGE_NAME with resource ID $iconId",
+            )
+            return null
+        }
     }
 
     companion object {
+        const val TAG = "DefaultShapeGridManager"
         const val SHAPE_OPTIONS: String = "shape_options"
         const val GRID_OPTIONS: String = "list_options"
         const val SHAPE_GRID: String = "default_grid"
diff --git a/src/com/android/customization/model/grid/ShapeGridManager.kt b/src/com/android/customization/model/grid/ShapeGridManager.kt
index 7a74b8d..7699783 100644
--- a/src/com/android/customization/model/grid/ShapeGridManager.kt
+++ b/src/com/android/customization/model/grid/ShapeGridManager.kt
@@ -26,5 +26,5 @@
 
     fun applyShapeGridOption(shapeKey: String, gridKey: String): Int
 
-    fun getGridOptionDrawble(iconId: Int): Drawable?
+    fun getGridOptionDrawable(iconId: Int): Drawable?
 }
diff --git a/src/com/android/customization/picker/grid/data/repository/ShapeGridRepository.kt b/src/com/android/customization/picker/grid/data/repository/ShapeGridRepository.kt
index 84462ad..93a2d01 100644
--- a/src/com/android/customization/picker/grid/data/repository/ShapeGridRepository.kt
+++ b/src/com/android/customization/picker/grid/data/repository/ShapeGridRepository.kt
@@ -72,6 +72,6 @@
         }
 
     fun getGridOptionDrawable(iconId: Int): Drawable? {
-        return manager.getGridOptionDrawble(iconId)
+        return manager.getGridOptionDrawable(iconId)
     }
 }
diff --git a/src/com/android/wallpaper/customization/ui/viewmodel/ShapeGridPickerViewModel.kt b/src/com/android/wallpaper/customization/ui/viewmodel/ShapeGridPickerViewModel.kt
index ce97154..fe1c6f8 100644
--- a/src/com/android/wallpaper/customization/ui/viewmodel/ShapeGridPickerViewModel.kt
+++ b/src/com/android/wallpaper/customization/ui/viewmodel/ShapeGridPickerViewModel.kt
@@ -17,11 +17,14 @@
 package com.android.wallpaper.customization.ui.viewmodel
 
 import android.content.Context
+import android.content.res.Resources
 import android.graphics.drawable.Drawable
+import com.android.customization.model.ResourceConstants
 import com.android.customization.model.grid.GridOptionModel
 import com.android.customization.model.grid.ShapeOptionModel
 import com.android.customization.picker.grid.domain.interactor.ShapeGridInteractor
 import com.android.customization.picker.grid.ui.viewmodel.ShapeIconViewModel
+import com.android.customization.widget.GridTileDrawable
 import com.android.themepicker.R
 import com.android.wallpaper.picker.common.icon.ui.viewmodel.Icon
 import com.android.wallpaper.picker.common.text.ui.viewmodel.Text
@@ -186,6 +189,21 @@
     }
 
     private fun toGridOptionItemViewModel(option: GridOptionModel): OptionItemViewModel2<Drawable> {
+        // Fallback to use GridTileDrawable when no resource found for the icon ID
+        val drawable =
+            interactor.getGridOptionDrawable(option.iconId)
+                ?: GridTileDrawable(
+                    option.cols,
+                    option.rows,
+                    context.resources.getString(
+                        Resources.getSystem()
+                            .getIdentifier(
+                                ResourceConstants.CONFIG_ICON_MASK,
+                                "string",
+                                ResourceConstants.ANDROID_PACKAGE,
+                            )
+                    ),
+                )
         val isSelected =
             previewingGridKey
                 .map { it == option.key }
@@ -194,10 +212,9 @@
                     started = SharingStarted.Lazily,
                     initialValue = false,
                 )
-
         return OptionItemViewModel2(
             key = MutableStateFlow(option.key),
-            payload = interactor.getGridOptionDrawable(option.iconId),
+            payload = drawable,
             text = Text.Loaded(option.title),
             isSelected = isSelected,
             onClicked =
diff --git a/tests/common/src/com/android/customization/model/grid/FakeShapeGridManager.kt b/tests/common/src/com/android/customization/model/grid/FakeShapeGridManager.kt
index 6269fee..b39988a 100644
--- a/tests/common/src/com/android/customization/model/grid/FakeShapeGridManager.kt
+++ b/tests/common/src/com/android/customization/model/grid/FakeShapeGridManager.kt
@@ -42,7 +42,7 @@
         return 0
     }
 
-    override fun getGridOptionDrawble(iconId: Int): Drawable? {
+    override fun getGridOptionDrawable(iconId: Int): Drawable? {
         return when (iconId) {
             0 -> gridOptionDrawable0
             1 -> gridOptionDrawable1
@@ -95,13 +95,6 @@
                     isCurrent = false,
                 ),
                 ShapeOptionModel(
-                    key = "sunny",
-                    title = "sunny",
-                    path =
-                        "M42.846 4.873C46.084-.531 53.916-.531 57.154 4.873L60.796 10.951C62.685 14.103 66.414 15.647 69.978 14.754L76.851 13.032C82.962 11.5 88.5 17.038 86.968 23.149L85.246 30.022C84.353 33.586 85.897 37.315 89.049 39.204L95.127 42.846C100.531 46.084 100.531 53.916 95.127 57.154L89.049 60.796C85.897 62.685 84.353 66.414 85.246 69.978L86.968 76.851C88.5 82.962 82.962 88.5 76.851 86.968L69.978 85.246C66.414 84.353 62.685 85.898 60.796 89.049L57.154 95.127C53.916 100.531 46.084 100.531 42.846 95.127L39.204 89.049C37.315 85.898 33.586 84.353 30.022 85.246L23.149 86.968C17.038 88.5 11.5 82.962 13.032 76.851L14.754 69.978C15.647 66.414 14.103 62.685 10.951 60.796L4.873 57.154C-.531 53.916-.531 46.084 4.873 42.846L10.951 39.204C14.103 37.315 15.647 33.586 14.754 30.022L13.032 23.149C11.5 17.038 17.038 11.5 23.149 13.032L30.022 14.754C33.586 15.647 37.315 14.103 39.204 10.951L42.846 4.873Z",
-                    isCurrent = false,
-                ),
-                ShapeOptionModel(
                     key = "circle",
                     title = "circle",
                     path =
diff --git a/tests/robotests/src/com/android/customization/picker/grid/data/repository/ShapeGridRepositoryTest.kt b/tests/robotests/src/com/android/customization/picker/grid/data/repository/ShapeGridRepositoryTest.kt
index 985d983..5fa1545 100644
--- a/tests/robotests/src/com/android/customization/picker/grid/data/repository/ShapeGridRepositoryTest.kt
+++ b/tests/robotests/src/com/android/customization/picker/grid/data/repository/ShapeGridRepositoryTest.kt
@@ -104,11 +104,14 @@
     fun selectedShapeOption_shouldUpdateAfterApplyShapeGridOption() =
         testScope.runTest {
             val selectedShapeOption = collectLastValue(underTest.selectedShapeOption)
+            val expectedShapeKey = "circle"
+            val expectedShapeOption =
+                FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST.first { it.key == expectedShapeKey }
+                    .copy(isCurrent = true)
 
-            underTest.applySelectedOption("circle", "practical")
+            underTest.applySelectedOption(expectedShapeKey, "practical")
 
-            assertThat(selectedShapeOption())
-                .isEqualTo(FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST[4].copy(isCurrent = true))
+            assertThat(selectedShapeOption()).isEqualTo(expectedShapeOption)
         }
 
     @Test
diff --git a/tests/robotests/src/com/android/customization/picker/grid/domain/interactor/ShapeGridInteractorTest.kt b/tests/robotests/src/com/android/customization/picker/grid/domain/interactor/ShapeGridInteractorTest.kt
index c0f519c..3886690 100644
--- a/tests/robotests/src/com/android/customization/picker/grid/domain/interactor/ShapeGridInteractorTest.kt
+++ b/tests/robotests/src/com/android/customization/picker/grid/domain/interactor/ShapeGridInteractorTest.kt
@@ -96,11 +96,14 @@
     fun selectedShapeOption_shouldUpdateAfterApplyGridOption() =
         testScope.runTest {
             val selectedShapeOption = collectLastValue(underTest.selectedShapeOption)
+            val expectedShapeKey = "circle"
+            val expectedShapeOption =
+                FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST.first { it.key == expectedShapeKey }
+                    .copy(isCurrent = true)
 
-            underTest.applySelectedOption("circle", "practical")
+            underTest.applySelectedOption(expectedShapeKey, "practical")
 
-            assertThat(selectedShapeOption())
-                .isEqualTo(FakeShapeGridManager.DEFAULT_SHAPE_OPTION_LIST[4].copy(isCurrent = true))
+            assertThat(selectedShapeOption()).isEqualTo(expectedShapeOption)
         }
 
     @Test
diff --git a/tests/robotests/src/com/android/wallpaper/customization/ui/viewmodel/ShapeGridPickerViewModelTest.kt b/tests/robotests/src/com/android/wallpaper/customization/ui/viewmodel/ShapeGridPickerViewModelTest.kt
index 0bf5125..15d1487 100644
--- a/tests/robotests/src/com/android/wallpaper/customization/ui/viewmodel/ShapeGridPickerViewModelTest.kt
+++ b/tests/robotests/src/com/android/wallpaper/customization/ui/viewmodel/ShapeGridPickerViewModelTest.kt
@@ -116,8 +116,8 @@
         testScope.runTest {
             val shapeOptions = collectLastValue(underTest.shapeOptions)
             val previewingShapeKey = collectLastValue(underTest.previewingShapeKey)
-            val onCircleOptionClicked =
-                shapeOptions()?.get(4)?.onClicked?.let { collectLastValue(it) }
+            val circleOption = shapeOptions()?.firstOrNull { it.key.value == "circle" }
+            val onCircleOptionClicked = circleOption?.onClicked?.let { collectLastValue(it) }
             checkNotNull(onCircleOptionClicked)
 
             onCircleOptionClicked()?.invoke()