From f5c5fcb5e46f43b2e5abbbc4f55a1cf1c52e0549 Mon Sep 17 00:00:00 2001 From: LisoUseInAIKyrios <118716522+LisoUseInAIKyrios@users.noreply.github.com> Date: Fri, 6 Jan 2023 01:21:41 +0400 Subject: [PATCH] fix: ensure thread safety for class `StringRef` (#273) --- .../integrations/sponsorblock/StringRef.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/app/revanced/integrations/sponsorblock/StringRef.java b/app/src/main/java/app/revanced/integrations/sponsorblock/StringRef.java index 71ed866b..14f1a201 100644 --- a/app/src/main/java/app/revanced/integrations/sponsorblock/StringRef.java +++ b/app/src/main/java/app/revanced/integrations/sponsorblock/StringRef.java @@ -5,16 +5,20 @@ import android.content.res.Resources; import androidx.annotation.NonNull; +import java.util.Collections; import java.util.HashMap; +import java.util.Map; import app.revanced.integrations.utils.LogHelper; import app.revanced.integrations.utils.ReVancedUtils; +// should probably move this class into utils package public class StringRef { private static Resources resources; private static String packageName; - private static final HashMap strings = new HashMap<>(); + // must use a thread safe map, as this class is used both on and off the main thread + private static final Map strings = Collections.synchronizedMap(new HashMap()); /** * Gets strings reference from shared collection or creates if not exists yet, @@ -90,9 +94,11 @@ public class StringRef { @NonNull public String toString() { if (!resolved) { - Context context = ReVancedUtils.getContext(); - resources = context.getResources(); - packageName = context.getPackageName(); + if (resources == null || packageName == null) { + Context context = ReVancedUtils.getContext(); + resources = context.getResources(); + packageName = context.getPackageName(); + } resolved = true; if (resources != null) { final int identifier = resources.getIdentifier(value, "string", packageName);