fix: ensure thread safety for class StringRef (#273)

This commit is contained in:
LisoUseInAIKyrios 2023-01-06 01:21:41 +04:00 committed by GitHub
parent 954e60191e
commit f5c5fcb5e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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<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,
@ -90,9 +94,11 @@ public class StringRef {
@NonNull
public String toString() {
if (!resolved) {
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);