1717import android .os .Binder ;
1818import android .os .Handler ;
1919import android .os .Looper ;
20+ import android .text .TextUtils ;
2021import android .util .DisplayMetrics ;
2122import android .util .TypedValue ;
2223import android .widget .Toast ;
3839import java .io .OutputStream ;
3940import java .text .SimpleDateFormat ;
4041import java .util .Date ;
42+ import java .util .HashMap ;
4143import java .util .Locale ;
4244import java .util .Objects ;
4345import 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