termux-app

Форк
0
146 строк · 6.7 Кб
1
package com.termux.shared.notification;
2

3
import android.app.Notification;
4
import android.app.NotificationChannel;
5
import android.app.NotificationManager;
6
import android.app.PendingIntent;
7
import android.content.Context;
8
import android.os.Build;
9

10
import androidx.annotation.Nullable;
11

12
import com.termux.shared.logger.Logger;
13

14
public class NotificationUtils {
15

16
    /** Do not show notification */
17
    public static final int NOTIFICATION_MODE_NONE = 0;
18
    /** Show notification without sound, vibration or lights */
19
    public static final int NOTIFICATION_MODE_SILENT = 1;
20
    /** Show notification with sound */
21
    public static final int NOTIFICATION_MODE_SOUND = 2;
22
    /** Show notification with vibration */
23
    public static final int NOTIFICATION_MODE_VIBRATE = 3;
24
    /** Show notification with lights */
25
    public static final int NOTIFICATION_MODE_LIGHTS = 4;
26
    /** Show notification with sound and vibration */
27
    public static final int NOTIFICATION_MODE_SOUND_AND_VIBRATE = 5;
28
    /** Show notification with sound and lights */
29
    public static final int NOTIFICATION_MODE_SOUND_AND_LIGHTS = 6;
30
    /** Show notification with vibration and lights */
31
    public static final int NOTIFICATION_MODE_VIBRATE_AND_LIGHTS = 7;
32
    /** Show notification with sound, vibration and lights */
33
    public static final int NOTIFICATION_MODE_ALL = 8;
34

35
    private 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
44
    public static NotificationManager getNotificationManager(final Context context) {
45
        if (context == null) return null;
46
        return (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
66
    public static Notification.Builder geNotificationBuilder(
67
        final Context context, final String channelId, final int priority, final CharSequence title,
68
        final CharSequence notificationText, final CharSequence notificationBigText,
69
        final PendingIntent contentIntent, final PendingIntent deleteIntent, final int notificationMode) {
70
        if (context == null) return null;
71
        Notification.Builder builder = new Notification.Builder(context);
72
        builder.setContentTitle(title);
73
        builder.setContentText(notificationText);
74
        builder.setStyle(new Notification.BigTextStyle().bigText(notificationBigText));
75
        builder.setContentIntent(contentIntent);
76
        builder.setDeleteIntent(deleteIntent);
77

78
        builder.setPriority(priority);
79

80
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
81
            builder.setChannelId(channelId);
82

83
        builder = setNotificationDefaults(builder, notificationMode);
84

85
        return 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
     */
98
    public static void setupNotificationChannel(final Context context, final String channelId, final CharSequence channelName, final int importance) {
99
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) return;
100

101
        NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
102

103
        NotificationManager notificationManager = getNotificationManager(context);
104
        if (notificationManager != null)
105
            notificationManager.createNotificationChannel(channel);
106
    }
107

108
    public 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
111
        switch (notificationMode) {
112
            case NOTIFICATION_MODE_NONE:
113
                Logger.logWarn(LOG_TAG, "The NOTIFICATION_MODE_NONE passed to setNotificationDefaults(), force setting builder to null.");
114
                return null; // return null since notification is not supposed to be shown
115
            case NOTIFICATION_MODE_SILENT:
116
                break;
117
            case NOTIFICATION_MODE_SOUND:
118
                builder.setDefaults(Notification.DEFAULT_SOUND);
119
                break;
120
            case NOTIFICATION_MODE_VIBRATE:
121
                builder.setDefaults(Notification.DEFAULT_VIBRATE);
122
                break;
123
            case NOTIFICATION_MODE_LIGHTS:
124
                builder.setDefaults(Notification.DEFAULT_LIGHTS);
125
                break;
126
            case NOTIFICATION_MODE_SOUND_AND_VIBRATE:
127
                builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE);
128
                break;
129
            case NOTIFICATION_MODE_SOUND_AND_LIGHTS:
130
                builder.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_LIGHTS);
131
                break;
132
            case NOTIFICATION_MODE_VIBRATE_AND_LIGHTS:
133
                builder.setDefaults(Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
134
                break;
135
            case NOTIFICATION_MODE_ALL:
136
                builder.setDefaults(Notification.DEFAULT_ALL);
137
                break;
138
            default:
139
                Logger.logError(LOG_TAG, "Invalid notificationMode: \"" + notificationMode + "\" passed to setNotificationDefaults()");
140
                break;
141
        }
142

143
        return builder;
144
    }
145

146
}
147

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.