termux-app
146 строк · 6.7 Кб
1package com.termux.shared.notification;
2
3import android.app.Notification;
4import android.app.NotificationChannel;
5import android.app.NotificationManager;
6import android.app.PendingIntent;
7import android.content.Context;
8import android.os.Build;
9
10import androidx.annotation.Nullable;
11
12import com.termux.shared.logger.Logger;
13
14public class NotificationUtils {
15
16/** Do not show notification */
17public static final int NOTIFICATION_MODE_NONE = 0;
18/** Show notification without sound, vibration or lights */
19public static final int NOTIFICATION_MODE_SILENT = 1;
20/** Show notification with sound */
21public static final int NOTIFICATION_MODE_SOUND = 2;
22/** Show notification with vibration */
23public static final int NOTIFICATION_MODE_VIBRATE = 3;
24/** Show notification with lights */
25public static final int NOTIFICATION_MODE_LIGHTS = 4;
26/** Show notification with sound and vibration */
27public static final int NOTIFICATION_MODE_SOUND_AND_VIBRATE = 5;
28/** Show notification with sound and lights */
29public static final int NOTIFICATION_MODE_SOUND_AND_LIGHTS = 6;
30/** Show notification with vibration and lights */
31public static final int NOTIFICATION_MODE_VIBRATE_AND_LIGHTS = 7;
32/** Show notification with sound, vibration and lights */
33public static final int NOTIFICATION_MODE_ALL = 8;
34
35private static final String LOG_TAG = "NotificationUtils";
36
37/**
38* Get the {@link NotificationManager}.
39*
40* @param context The {@link Context} for operations.
41* @return Returns the {@link NotificationManager}.
42*/
43@Nullable
44public static NotificationManager getNotificationManager(final Context context) {
45if (context == null) return null;
46return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
47}
48
49/**
50* Get {@link Notification.Builder}.
51*
52* @param context The {@link Context} for operations.
53* @param title The title for the notification.
54* @param channelId The channel id for the notification.
55* @param priority The priority for the notification.
56* @param notificationText The second line text of the notification.
57* @param notificationBigText The full text of the notification that may optionally be styled.
58* @param contentIntent The {@link PendingIntent} which should be sent when notification is clicked.
59* @param deleteIntent The {@link PendingIntent} which should be sent when notification is deleted.
60* @param notificationMode The notification mode. It must be one of {@code NotificationUtils.NOTIFICATION_MODE_*}.
61* The builder returned will be {@code null} if {@link #NOTIFICATION_MODE_NONE}
62* is passed. That case should ideally be handled before calling this function.
63* @return Returns the {@link Notification.Builder}.
64*/
65@Nullable
66public static Notification.Builder geNotificationBuilder(
67final Context context, final String channelId, final int priority, final CharSequence title,
68final CharSequence notificationText, final CharSequence notificationBigText,
69final PendingIntent contentIntent, final PendingIntent deleteIntent, final int notificationMode) {
70if (context == null) return null;
71Notification.Builder builder = new Notification.Builder(context);
72builder.setContentTitle(title);
73builder.setContentText(notificationText);
74builder.setStyle(new Notification.BigTextStyle().bigText(notificationBigText));
75builder.setContentIntent(contentIntent);
76builder.setDeleteIntent(deleteIntent);
77
78builder.setPriority(priority);
79
80if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
81builder.setChannelId(channelId);
82
83builder = setNotificationDefaults(builder, notificationMode);
84
85return builder;
86}
87
88/**
89* Setup the notification channel if Android version is greater than or equal to
90* {@link Build.VERSION_CODES#O}.
91*
92* @param context The {@link Context} for operations.
93* @param channelId The id of the channel. Must be unique per package.
94* @param channelName The user visible name of the channel.
95* @param importance The importance of the channel. This controls how interruptive notifications
96* posted to this channel are.
97*/
98public static void setupNotificationChannel(final Context context, final String channelId, final CharSequence channelName, final int importance) {
99if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;
100
101NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
102
103NotificationManager notificationManager = getNotificationManager(context);
104if (notificationManager != null)
105notificationManager.createNotificationChannel(channel);
106}
107
108public static Notification.Builder setNotificationDefaults(Notification.Builder builder, final int notificationMode) {
109
110// TODO: setDefaults() is deprecated and should also implement setting notification mode via notification channel
111switch (notificationMode) {
112case NOTIFICATION_MODE_NONE:
113Logger.logWarn(LOG_TAG, "The NOTIFICATION_MODE_NONE passed to setNotificationDefaults(), force setting builder to null.");
114return null; // return null since notification is not supposed to be shown
115case NOTIFICATION_MODE_SILENT:
116break;
117case NOTIFICATION_MODE_SOUND:
118builder.setDefaults(Notification.DEFAULT_SOUND);
119break;
120case NOTIFICATION_MODE_VIBRATE:
121builder.setDefaults(Notification.DEFAULT_VIBRATE);
122break;
123case NOTIFICATION_MODE_LIGHTS:
124builder.setDefaults(Notification.DEFAULT_LIGHTS);
125break;
126case NOTIFICATION_MODE_SOUND_AND_VIBRATE:
127builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
128break;
129case NOTIFICATION_MODE_SOUND_AND_LIGHTS:
130builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS);
131break;
132case NOTIFICATION_MODE_VIBRATE_AND_LIGHTS:
133builder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
134break;
135case NOTIFICATION_MODE_ALL:
136builder.setDefaults(Notification.DEFAULT_ALL);
137break;
138default:
139Logger.logError(LOG_TAG, "Invalid notificationMode: \"" + notificationMode + "\" passed to setNotificationDefaults()");
140break;
141}
142
143return builder;
144}
145
146}
147