Added|Fixed: Add TermuxNotificationUtils.getTermuxOrPluginAppNotificationBuilder() helper function and fix notification icon drawable resource id issue on Android 5

This commit is contained in:
agnostic-apollo
2022-04-26 02:40:12 +05:00
parent ab9b620c88
commit 5290ce1f77
4 changed files with 77 additions and 38 deletions

View File

@@ -51,6 +51,8 @@ public class NotificationUtils {
* *
* @param context The {@link Context} for operations. * @param context The {@link Context} for operations.
* @param title The title for the notification. * @param title The title for the notification.
* @param channelId The channel id for the notification.
* @param priority The priority for the notification.
* @param notificationText The second line text of the notification. * @param notificationText The second line text of the notification.
* @param notificationBigText The full text of the notification that may optionally be styled. * @param notificationBigText The full text of the notification that may optionally be styled.
* @param contentIntent The {@link PendingIntent} which should be sent when notification is clicked. * @param contentIntent The {@link PendingIntent} which should be sent when notification is clicked.

View File

@@ -5,13 +5,11 @@ import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.drawable.Icon;
import android.os.Environment; import android.os.Environment;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.termux.shared.R;
import com.termux.shared.activities.ReportActivity; import com.termux.shared.activities.ReportActivity;
import com.termux.shared.android.AndroidUtils; import com.termux.shared.android.AndroidUtils;
import com.termux.shared.crash.CrashHandler; import com.termux.shared.crash.CrashHandler;
@@ -391,26 +389,10 @@ public class TermuxCrashUtils implements CrashHandler.CrashHandlerClient {
final PendingIntent contentIntent, final PendingIntent contentIntent,
final PendingIntent deleteIntent, final PendingIntent deleteIntent,
final int notificationMode) { final int notificationMode) {
return TermuxNotificationUtils.getTermuxOrPluginAppNotificationBuilder(
Notification.Builder builder = NotificationUtils.geNotificationBuilder(termuxPackageContext, currentPackageContext, termuxPackageContext,
TermuxConstants.TERMUX_CRASH_REPORTS_NOTIFICATION_CHANNEL_ID, Notification.PRIORITY_HIGH, TermuxConstants.TERMUX_CRASH_REPORTS_NOTIFICATION_CHANNEL_ID, Notification.PRIORITY_HIGH,
title, notificationText, notificationBigText, contentIntent, deleteIntent, notificationMode); title, notificationText, notificationBigText, contentIntent, deleteIntent, notificationMode);
if (builder == null) return null;
// Enable timestamp
builder.setShowWhen(true);
// Set notification icon
builder.setSmallIcon(Icon.createWithResource(currentPackageContext, R.drawable.ic_error_notification));
// Set background color for small notification icon
builder.setColor(0xFF607D8B);
// Dismiss on click
builder.setAutoCancel(true);
return builder;
} }
/** /**

View File

@@ -1,7 +1,16 @@
package com.termux.shared.termux.notification; package com.termux.shared.termux.notification;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Icon;
import android.os.Build;
import androidx.annotation.Nullable;
import com.termux.shared.R;
import com.termux.shared.android.resource.ResourceUtils;
import com.termux.shared.notification.NotificationUtils;
import com.termux.shared.termux.settings.preferences.TermuxAppSharedPreferences; import com.termux.shared.termux.settings.preferences.TermuxAppSharedPreferences;
import com.termux.shared.termux.settings.preferences.TermuxPreferenceConstants; import com.termux.shared.termux.settings.preferences.TermuxPreferenceConstants;
import com.termux.shared.termux.TermuxConstants; import com.termux.shared.termux.TermuxConstants;
@@ -35,4 +44,66 @@ public class TermuxNotificationUtils {
preferences.setLastNotificationId(nextNotificationId); preferences.setLastNotificationId(nextNotificationId);
return nextNotificationId; return nextNotificationId;
} }
/**
* Get {@link Notification.Builder} for termux app or its plugin.
*
* @param currentPackageContext The {@link Context} of current package.
* @param termuxPackageContext The {@link Context} of termux package.
* @param channelId The channel id for the notification.
* @param priority The priority for the notification.
* @param title The title for the notification.
* @param notificationText The second line text of the notification.
* @param notificationBigText The full text of the notification that may optionally be styled.
* @param contentIntent The {@link PendingIntent} which should be sent when notification is clicked.
* @param deleteIntent The {@link PendingIntent} which should be sent when notification is deleted.
* @param notificationMode The notification mode. It must be one of {@code NotificationUtils.NOTIFICATION_MODE_*}.
* @return Returns the {@link Notification.Builder}.
*/
@Nullable
public static Notification.Builder getTermuxOrPluginAppNotificationBuilder(final Context currentPackageContext,
final Context termuxPackageContext,
final String channelId,
final int priority,
final CharSequence title,
final CharSequence notificationText,
final CharSequence notificationBigText,
final PendingIntent contentIntent,
final PendingIntent deleteIntent,
final int notificationMode) {
Notification.Builder builder = NotificationUtils.geNotificationBuilder(termuxPackageContext,
channelId, priority,
title, notificationText, notificationBigText, contentIntent, deleteIntent, notificationMode);
if (builder == null) return null;
// Enable timestamp
builder.setShowWhen(true);
// Set notification icon
// If a notification is to be shown by a termux plugin app, then we can't use the drawable
// resource id for the plugin app with setSmallIcon(@DrawableRes int icon) since notification
// is shown with termuxPackageContext and termux-app package would have a different id and
// when android tries to load the drawable an exception would be thrown and notification will
// not be thrown.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Set Icon instead of drawable resource id
builder.setSmallIcon(Icon.createWithResource(currentPackageContext, R.drawable.ic_error_notification));
} else {
// Set drawable resource id used by termux-app package
Integer iconResId = ResourceUtils.getDrawableResourceId(termuxPackageContext, "ic_error_notification",
termuxPackageContext.getPackageName(), true);
if (iconResId != null)
builder.setSmallIcon(iconResId);
}
// Set background color for small notification icon
builder.setColor(0xFF607D8B);
// Dismiss on click
builder.setAutoCancel(true);
return builder;
}
} }

View File

@@ -4,7 +4,6 @@ import android.app.Notification;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context; import android.content.Context;
import android.graphics.drawable.Icon;
import android.os.Environment; import android.os.Environment;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
@@ -403,25 +402,10 @@ public class TermuxPluginUtils {
final PendingIntent contentIntent, final PendingIntent contentIntent,
final PendingIntent deleteIntent, final PendingIntent deleteIntent,
final int notificationMode) { final int notificationMode) {
Notification.Builder builder = NotificationUtils.geNotificationBuilder(termuxPackageContext, return TermuxNotificationUtils.getTermuxOrPluginAppNotificationBuilder(
currentPackageContext, termuxPackageContext,
TermuxConstants.TERMUX_PLUGIN_COMMAND_ERRORS_NOTIFICATION_CHANNEL_ID, Notification.PRIORITY_HIGH, TermuxConstants.TERMUX_PLUGIN_COMMAND_ERRORS_NOTIFICATION_CHANNEL_ID, Notification.PRIORITY_HIGH,
title, notificationText, notificationBigText, contentIntent, deleteIntent, notificationMode); title, notificationText, notificationBigText, contentIntent, deleteIntent, notificationMode);
if (builder == null) return null;
// Enable timestamp
builder.setShowWhen(true);
// Set notification icon
builder.setSmallIcon(Icon.createWithResource(currentPackageContext, R.drawable.ic_error_notification));
// Set background color for small notification icon
builder.setColor(0xFF607D8B);
// Dismiss on click
builder.setAutoCancel(true);
return builder;
} }
/** /**