mirror of
https://github.com/revanced/revanced-integrations.git
synced 2025-01-21 01:07:32 +01:00
fix: ensure thread safety for class StringRef
(#273)
This commit is contained in:
parent
954e60191e
commit
f5c5fcb5e4
@ -5,16 +5,20 @@ import android.content.res.Resources;
|
|||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import app.revanced.integrations.utils.LogHelper;
|
import app.revanced.integrations.utils.LogHelper;
|
||||||
import app.revanced.integrations.utils.ReVancedUtils;
|
import app.revanced.integrations.utils.ReVancedUtils;
|
||||||
|
|
||||||
|
// should probably move this class into utils package
|
||||||
public class StringRef {
|
public class StringRef {
|
||||||
private static Resources resources;
|
private static Resources resources;
|
||||||
private static String packageName;
|
private static String packageName;
|
||||||
|
|
||||||
private static final HashMap<String, StringRef> 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<String, StringRef> strings = Collections.synchronizedMap(new HashMap());
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets strings reference from shared collection or creates if not exists yet,
|
* Gets strings reference from shared collection or creates if not exists yet,
|
||||||
@ -90,9 +94,11 @@ public class StringRef {
|
|||||||
@NonNull
|
@NonNull
|
||||||
public String toString() {
|
public String toString() {
|
||||||
if (!resolved) {
|
if (!resolved) {
|
||||||
Context context = ReVancedUtils.getContext();
|
if (resources == null || packageName == null) {
|
||||||
resources = context.getResources();
|
Context context = ReVancedUtils.getContext();
|
||||||
packageName = context.getPackageName();
|
resources = context.getResources();
|
||||||
|
packageName = context.getPackageName();
|
||||||
|
}
|
||||||
resolved = true;
|
resolved = true;
|
||||||
if (resources != null) {
|
if (resources != null) {
|
||||||
final int identifier = resources.getIdentifier(value, "string", packageName);
|
final int identifier = resources.getIdentifier(value, "string", packageName);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user