@@ -202,27 +202,35 @@ static Pair<NotificationCompat.Builder, String> buildNotification(final Context
202202 notification .setWhen (System .currentTimeMillis ());
203203 notification .setShowWhen (true );
204204
205- String SmallIcon = intent .getStringExtra ("icon" );
206-
207- if (SmallIcon != null ) {
208- final Class <?> clz = R .drawable .class ;
209- final Field [] fields = clz .getDeclaredFields ();
210- for (Field field : fields ) {
211- String name = field .getName ();
212- if (name .equals ("ic_" + SmallIcon + "_black_24dp" )) {
213- try {
214- notification .setSmallIcon (field .getInt (clz ));
215- } catch (Exception e ) {
216- break ;
217- }
205+
206+ String smallIconName = intent .getStringExtra ("icon" );
207+ if (smallIconName != null ) {
208+ // TODO: Add prefix to all icons used by `NotificationAPI` to force keep only those icons when providing termux-api-app as a library.
209+ // TODO: Add new icons from https://fonts.google.com/icons | https://github.com/google/material-design-icons
210+ // Use `String.format()` so that resource shrinker does not remove icons used by `NotificationAPI` for release builds.
211+ // - https://web.archive.org/web/20250516083801/https://developer.android.com/build/shrink-code#keep-resources
212+ String smallIconResourceName = String .format ("ic_%1s_black_24dp" , smallIconName );
213+ Integer smallIconResourceId = null ;
214+ try {
215+ //noinspection DiscouragedApi
216+ smallIconResourceId = context .getResources ().getIdentifier (smallIconResourceName , "drawable" , context .getPackageName ());
217+ if (smallIconResourceId == 0 ) {
218+ smallIconResourceId = null ;
219+ Logger .logError (LOG_TAG , "Failed to find \" " + smallIconResourceName + "\" icon" );
218220 }
221+ } catch (Exception e ) {
222+ Logger .logError (LOG_TAG , "Failed to find \" " + smallIconResourceName + "\" icon: " + e .getMessage ());
223+ }
224+
225+ if (smallIconResourceId != null ) {
226+ notification .setSmallIcon (smallIconResourceId );
219227 }
220228 }
221229
222- String ImagePath = intent .getStringExtra ("image-path" );
223230
224- if (ImagePath != null ) {
225- File imgFile = new File (ImagePath );
231+ String imagePath = intent .getStringExtra ("image-path" );
232+ if (imagePath != null ) {
233+ File imgFile = new File (imagePath );
226234 if (imgFile .exists ()) {
227235 Bitmap myBitmap = BitmapFactory .decodeFile (imgFile .getAbsolutePath ());
228236
@@ -240,7 +248,7 @@ static Pair<NotificationCompat.Builder, String> buildNotification(final Context
240248 String mediaNext = intent .getStringExtra ("media-next" );
241249
242250 if (mediaPrevious != null && mediaPause != null && mediaPlay != null && mediaNext != null ) {
243- if (SmallIcon == null ) {
251+ if (smallIconName == null ) {
244252 notification .setSmallIcon (android .R .drawable .ic_media_play );
245253 }
246254
0 commit comments