Skip to content

Commit 1639e73

Browse files
Fixed(NotificationAPI): Fix termux-notification --icon not working for release builds as icon drawables were being removed by resource shrinker possibly related to 6ef2618
Use standard Android API to get resource id for drawable icon instead of using reflection into drawable class. Additionally use `String.format()` so that resource shrinker does not remove icons used by `NotificationAPI` for release builds. Closes #483
1 parent 7c1759a commit 1639e73

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

app/src/main/java/com/termux/api/apis/NotificationAPI.java

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)