Merge "Reject zen rule icon if its resource name is too long" into main
diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java
index ae29e1b..454bd20 100644
--- a/services/core/java/com/android/server/notification/ZenModeHelper.java
+++ b/services/core/java/com/android/server/notification/ZenModeHelper.java
@@ -149,6 +149,8 @@
 
     private static final String IMPLICIT_RULE_ID_PREFIX = "implicit_"; // + pkg_name
 
+    private static final int MAX_ICON_RESOURCE_NAME_LENGTH = 1000;
+
     /**
      * Send new activation AutomaticZenRule statuses to apps with a min target SDK version
      */
@@ -2645,7 +2647,13 @@
         requireNonNull(packageName);
         try {
             final Resources res = mPm.getResourcesForApplication(packageName);
-            return res.getResourceName(resId);
+            String resourceName = res.getResourceName(resId);
+            if (resourceName != null && resourceName.length() > MAX_ICON_RESOURCE_NAME_LENGTH) {
+                Slog.e(TAG, "Resource name for ID=" + resId + " in package " + packageName
+                        + " is too long (" + resourceName.length() + "); ignoring it");
+                return null;
+            }
+            return resourceName;
         } catch (PackageManager.NameNotFoundException | Resources.NotFoundException e) {
             Slog.e(TAG, "Resource name for ID=" + resId + " not found in package " + packageName
                     + ". Resource IDs may change when the application is upgraded, and the system"
diff --git a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
index e4188b0..72ace84 100644
--- a/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
+++ b/services/tests/uiservicestests/src/com/android/server/notification/ZenModeHelperTest.java
@@ -6168,6 +6168,23 @@
                 .isEqualTo(previousManualZenPolicy);
     }
 
+    @Test
+    @EnableFlags(Flags.FLAG_MODES_API)
+    public void addRule_iconIdWithResourceNameTooLong_ignoresIcon() {
+        int resourceId = 999;
+        String veryLongResourceName = "com.android.server.notification:drawable/"
+                + "omg_this_is_one_long_resource_name".repeat(100);
+        when(mResources.getResourceName(resourceId)).thenReturn(veryLongResourceName);
+        when(mResources.getIdentifier(veryLongResourceName, null, null)).thenReturn(resourceId);
+
+        String ruleId = mZenModeHelper.addAutomaticZenRule(mContext.getPackageName(),
+                new AutomaticZenRule.Builder("Rule", CONDITION_ID).setIconResId(resourceId).build(),
+                UPDATE_ORIGIN_APP, "reason", CUSTOM_PKG_UID);
+        AutomaticZenRule storedRule = mZenModeHelper.getAutomaticZenRule(ruleId);
+
+        assertThat(storedRule.getIconResId()).isEqualTo(0);
+    }
+
     private static void addZenRule(ZenModeConfig config, String id, String ownerPkg, int zenMode,
             @Nullable ZenPolicy zenPolicy) {
         ZenRule rule = new ZenRule();