From 99be40df05ee108da49ca210c3300ee2ac77804b Mon Sep 17 00:00:00 2001 From: Yurii Zubrytskyi Date: Tue, 20 Jun 2023 17:36:51 -0700 Subject: [PATCH] [res] Preserve the resid order in ThemeKey Resource ID order matters for ThemeKey objects - we apply them one after another, overriding all attributes that happen to repeat. This means duplicate filtering has to move the "newly added" ids to the end of the list, making sure they have the final word. Bug: 278667073 Test: build + boot + bug repro Merged-In: If547e16a15f5f7294fc863852171c96e10402cf7 Change-Id: If547e16a15f5f7294fc863852171c96e10402cf7 --- core/java/android/content/res/Resources.java | 36 ++++++++++++++------ 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/core/java/android/content/res/Resources.java b/core/java/android/content/res/Resources.java index 885060f19efac..c3d5b71e73498 100644 --- a/core/java/android/content/res/Resources.java +++ b/core/java/android/content/res/Resources.java @@ -2003,13 +2003,25 @@ public class Resources { private int mHashCode = 0; - private boolean containsValue(int resId, boolean force) { + private int findValue(int resId, boolean force) { for (int i = 0; i < mCount; ++i) { if (mResId[i] == resId && mForce[i] == force) { - return true; + return i; } } - return false; + return -1; + } + + private void moveToLast(int index) { + if (index < 0 || index >= mCount - 1) { + return; + } + final int id = mResId[index]; + final boolean force = mForce[index]; + System.arraycopy(mResId, index + 1, mResId, index, mCount - index - 1); + mResId[mCount - 1] = id; + System.arraycopy(mForce, index + 1, mForce, index, mCount - index - 1); + mForce[mCount - 1] = force; } public void append(int resId, boolean force) { @@ -2022,15 +2034,17 @@ public class Resources { } // Some apps tend to keep adding same resources over and over, let's protect from it. - if (containsValue(resId, force)) { - return; + // Note: the order still matters, as the values that come later override the earlier + // ones. + final int index = findValue(resId, force); + if (index >= 0) { + moveToLast(index); + } else { + mResId = GrowingArrayUtils.append(mResId, mCount, resId); + mForce = GrowingArrayUtils.append(mForce, mCount, force); + mCount++; + mHashCode = 31 * (31 * mHashCode + resId) + (force ? 1 : 0); } - - mResId = GrowingArrayUtils.append(mResId, mCount, resId); - mForce = GrowingArrayUtils.append(mForce, mCount, force); - mCount++; - - mHashCode = 31 * (31 * mHashCode + resId) + (force ? 1 : 0); } /**