From 18ef4eda2196b9d296bca7c4e42367e665833342 Mon Sep 17 00:00:00 2001 From: gdlbo <41114720+gdlbo@users.noreply.github.com> Date: Mon, 19 Feb 2024 11:38:10 +0300 Subject: [PATCH] Revert "rollback to for-loops with improvements" This reverts commit 84cf9ba10a35ff7ced2fa2e0f19c64d6421965c8. --- .../lite/themes/utils/ArscEditor.java | 54 ++++++++++--------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/app/src/main/java/ru/vtosters/lite/themes/utils/ArscEditor.java b/app/src/main/java/ru/vtosters/lite/themes/utils/ArscEditor.java index 0d7525089..af0f958b0 100644 --- a/app/src/main/java/ru/vtosters/lite/themes/utils/ArscEditor.java +++ b/app/src/main/java/ru/vtosters/lite/themes/utils/ArscEditor.java @@ -1,37 +1,39 @@ package ru.vtosters.lite.themes.utils; import com.google.devrel.gmscore.tools.apk.arsc.BinaryResourceFile; -import com.google.devrel.gmscore.tools.apk.arsc.BinaryResourceIdentifier; +import com.google.devrel.gmscore.tools.apk.arsc.BinaryResourceValue; import com.google.devrel.gmscore.tools.apk.arsc.ResourceTableChunk; -import ru.vtosters.lite.utils.AndroidUtils; +import java.util.AbstractMap; +import java.util.Arrays; public class ArscEditor { - public static void changeColors(final BinaryResourceFile arsc, final int[] colorResIds, final int color) { - for (var chunk : arsc.getChunks()) { - if (!(chunk instanceof ResourceTableChunk)) continue; + public static void changeColors(BinaryResourceFile arsc, int[] colorIds, int color) { + arsc.getChunks().stream() + .filter(chunk -> chunk instanceof ResourceTableChunk) + .map(chunk -> (ResourceTableChunk) chunk) + .flatMap(resTableChunk -> resTableChunk.getPackages().stream()) + .flatMap(packageChunk -> packageChunk.getTypeChunks("color").stream() + .map(typeChunk -> new AbstractMap.SimpleEntry<>(packageChunk, typeChunk))) + .forEach(entry -> entry.getValue().getEntries().entrySet().stream() + .filter(e -> !e.getValue().isComplex()) + .filter(e -> isColorResourceValue(e.getValue().value())) + .forEach(e -> { + BinaryResourceValue binValue = e.getValue().value(); + int resId = entry.getKey().getId() << 24 | entry.getValue().getId() << 16 | e.getKey(); + if (Arrays.stream(colorIds).anyMatch(colorId -> resId == colorId)) { + if (binValue != null) { + binValue.data = color; + } + } + })); + } - final var resTableChunk = (ResourceTableChunk) chunk; - final var pacChunk = resTableChunk.getPackage(AndroidUtils.getPackageName()); - if (pacChunk == null) continue; - - for (var typeChunk : pacChunk.getTypeChunks("color")) - for (var resId : colorResIds) { - final var binResId = BinaryResourceIdentifier.create(resId); - - final var pacId = pacChunk.getId(); - final var typeId = typeChunk.getId(); - if (pacId != binResId.packageId() - || typeId != binResId.typeId()) - continue; - - final var entry = typeChunk.getEntries().getOrDefault(binResId.entryId(), null); - if (entry == null || entry.isComplex()) continue; - - final var binResVal = entry.value(); - binResVal.data = color; - } - } + private static boolean isColorResourceValue(BinaryResourceValue value) { + return value.type == BinaryResourceValue.Type.INT_COLOR_ARGB8 + || value.type == BinaryResourceValue.Type.INT_COLOR_RGB8 + || value.type == BinaryResourceValue.Type.INT_COLOR_ARGB4 + || value.type == BinaryResourceValue.Type.INT_COLOR_RGB4; } } \ No newline at end of file