Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e0d2468d authored by Matías Hernández's avatar Matías Hernández Committed by Android (Google) Code Review
Browse files

Merge "Reject zen rule icon if its resource name is too long" into main

parents dff28db1 e0bff5eb
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -149,6 +149,8 @@ public class ZenModeHelper {

    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 @@ public class ZenModeHelper {
        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"
+17 −0
Original line number Diff line number Diff line
@@ -6168,6 +6168,23 @@ public class ZenModeHelperTest extends UiServiceTestCase {
                .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();