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);