Skip to content

Commit 92f025e

Browse files
committed
Add cache in get resource by name
Signed-off-by: Dev4Mod <[email protected]>
1 parent 3c39ab7 commit 92f025e

File tree

1 file changed

+35
-3
lines changed
  • app/src/main/java/com/wmods/wppenhacer/xposed/utils

1 file changed

+35
-3
lines changed

app/src/main/java/com/wmods/wppenhacer/xposed/utils/Utils.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import android.os.Binder;
1818
import android.os.Handler;
1919
import android.os.Looper;
20+
import android.text.TextUtils;
2021
import android.util.DisplayMetrics;
2122
import android.util.TypedValue;
2223
import android.widget.Toast;
@@ -38,6 +39,7 @@
3839
import java.io.OutputStream;
3940
import java.text.SimpleDateFormat;
4041
import java.util.Date;
42+
import java.util.HashMap;
4143
import java.util.Locale;
4244
import java.util.Objects;
4345
import java.util.Properties;
@@ -55,6 +57,7 @@ public class Utils {
5557
private static final ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
5658
private static final ExecutorService executorCachedService = Executors.newCachedThreadPool();
5759
public static XSharedPreferences xprefs;
60+
private static final HashMap<String, Integer> ids = new HashMap<>();
5861

5962
public static void init(ClassLoader loader) {
6063
var context = Utils.getApplication();
@@ -90,16 +93,45 @@ public static boolean doRestart(Context context) {
9093
return true;
9194
}
9295

96+
/**
97+
* Retrieves the resource ID by name and type.
98+
* Uses caching to improve performance for repeated lookups.
99+
*
100+
* @param name The resource name to look up
101+
* @param type The resource type (e.g., "id", "drawable", "layout", "string")
102+
* @return The resource ID or -1 if not found or an error occurred
103+
*/
93104
@SuppressLint("DiscouragedApi")
94105
public static int getID(String name, String type) {
106+
107+
if (TextUtils.isEmpty(name) || TextUtils.isEmpty(type)) {
108+
return -1;
109+
}
110+
111+
final String key = type + "_" + name;
112+
113+
synchronized (ids) {
114+
if (ids.containsKey(key)) {
115+
Integer cachedId = ids.get(key);
116+
return cachedId != null ? cachedId : -1;
117+
}
118+
}
119+
95120
try {
96-
return getApplication().getApplicationContext().getResources().getIdentifier(name, type, getApplication().getPackageName());
121+
Application app = getApplication();
122+
Context context = app.getApplicationContext();
123+
int id = context.getResources().getIdentifier(name, type, app.getPackageName());
124+
125+
synchronized (ids) {
126+
ids.put(key, id);
127+
}
128+
129+
return id;
97130
} catch (Exception e) {
98-
XposedBridge.log("Error while getting ID: " + name + " " + type + " message:" + e);
131+
XposedBridge.log("Error getting resource ID: type=" + type + ", name=" + name + ", error: " + e.getMessage());
99132
return -1;
100133
}
101134
}
102-
103135
public static int dipToPixels(float dipValue) {
104136
DisplayMetrics metrics = FeatureLoader.mApp.getResources().getDisplayMetrics();
105137
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, metrics);

0 commit comments

Comments
 (0)