From b05a239b9009b98cb5d60c888f82a6d66d477dcd Mon Sep 17 00:00:00 2001 From: Tahgolov Date: Sun, 18 Feb 2024 23:05:31 +0300 Subject: [PATCH] improve code readability, remove redundant constructions --- .../ru/vtosters/lite/themes/VTLResources.java | 86 ++++++++++--------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/ru/vtosters/lite/themes/VTLResources.java b/app/src/main/java/ru/vtosters/lite/themes/VTLResources.java index 328043365..120c0f5e6 100644 --- a/app/src/main/java/ru/vtosters/lite/themes/VTLResources.java +++ b/app/src/main/java/ru/vtosters/lite/themes/VTLResources.java @@ -1,5 +1,6 @@ package ru.vtosters.lite.themes; +import android.annotation.SuppressLint; import android.content.Context; import android.content.res.Resources; import android.content.res.TypedArray; @@ -7,30 +8,35 @@ import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; + import androidx.annotation.Nullable; -import ru.vtosters.hooks.other.ThemesUtils; import java.lang.reflect.Field; -import java.util.ArrayList; +import java.util.Arrays; import java.util.List; -import java.util.stream.IntStream; +import ru.vtosters.hooks.other.ThemesUtils; + +@SuppressLint("DiscouragedPrivateApi") public class VTLResources extends Resources { private static final String TAG = "VTLResources"; - private static final List attributesToTheme = new ArrayList<>(); - private static Field typedArrayField; + private static final List attributesToTheme = + Arrays.asList( + 0x1010121, + android.R.attr.backgroundTint, + 0x10101a5 + ); + private static final int STYLE_NUM_ENTRIES = 6; + + private static Field typedArrayDataFld = null; static { try { - typedArrayField = TypedArray.class.getDeclaredField("mData"); - typedArrayField.setAccessible(true); - } catch (NoSuchFieldException e) { + typedArrayDataFld = TypedArray.class.getDeclaredField("mData"); + typedArrayDataFld.setAccessible(true); + } catch (final NoSuchFieldException e) { e.printStackTrace(); } - - attributesToTheme.add(16843041); - attributesToTheme.add(android.R.attr.backgroundTint); - attributesToTheme.add(16843173); } public VTLResources(Context context, Resources parent) { @@ -41,42 +47,42 @@ public class VTLResources extends Resources { return !ThemesUtils.isDarkTheme() && attrID == com.vtosters.lite.R.attr.button_primary_background; } - private static int[] getArrayData(TypedArray arr) { - try { - return (int[]) typedArrayField.get(arr); - } catch (IllegalAccessException e) { - Log.e(TAG, "getArrayData: ", e); - e.fillInStackTrace(); - return new int[0]; - } + private static int[] getArrayData(final TypedArray ta) { + if (typedArrayDataFld != null) + try { + return (int[]) typedArrayDataFld.get(ta); + } catch (final IllegalAccessException e) { + Log.e(TAG, "getArrayData: ", e); + e.fillInStackTrace(); + } + return null; } @Override public TypedArray obtainAttributes(AttributeSet set, int[] attrs) { - TypedArray typedArray = super.obtainAttributes(set, attrs); - + final var ta = super.obtainAttributes(set, attrs); if (ThemesUtils.isMonetTheme()) { - int[] data = getArrayData(typedArray); - IntStream.range(0, attrs.length).forEach(i -> { - try { - int attrID = attrs[i]; - if (attributesToTheme.contains(attrID)) { - int type = data[i * 6]; - int cnt = data[(i * 6) + 1]; + final var mData = getArrayData(ta); + if (mData != null) + for (var i = 0; i < attrs.length; ++i) { + final var attr = attrs[i]; + if (attributesToTheme.contains(attr)) continue; - if (type == TypedValue.TYPE_ATTRIBUTE && isAttrThemeable(cnt)) { - data[i * 6] = TypedValue.TYPE_INT_COLOR_RGB8; - data[(i * 6) + 1] = ThemesUtils.getAccentColor(); - data[(i * 6) + 2] = 0; // clear reference content - } - } - } catch (Exception e) { - Log.e(TAG, "TAVzlom failed! (obtainAttributes)"); - e.fillInStackTrace(); + final var index = i * STYLE_NUM_ENTRIES; + final var typeIndex = index; + final var dataIndex = index + 1; + final var resourceIdIndex = index + 2; + + if(mData[typeIndex] == TypedValue.TYPE_ATTRIBUTE + || isAttrThemeable(mData[dataIndex])) + continue; + + mData[typeIndex] = TypedValue.TYPE_INT_COLOR_ARGB8; + mData[dataIndex] = ThemesUtils.getAccentColor(); + mData[resourceIdIndex] = 0; } - }); } - return typedArray; + return ta; } @Override