-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathNotificationUtils.java
337 lines (298 loc) · 12.9 KB
/
NotificationUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package com.dds.common.utils;
import static android.Manifest.permission.EXPAND_STATUS_BAR;
import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import androidx.annotation.IntDef;
import androidx.annotation.RequiresPermission;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.dds.common.lifecycle.Utils;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
public class NotificationUtils {
public static final int IMPORTANCE_UNSPECIFIED = -1000;
public static final int IMPORTANCE_NONE = 0;
public static final int IMPORTANCE_MIN = 1;
public static final int IMPORTANCE_LOW = 2;
public static final int IMPORTANCE_DEFAULT = 3;
public static final int IMPORTANCE_HIGH = 4;
@IntDef({IMPORTANCE_UNSPECIFIED, IMPORTANCE_NONE, IMPORTANCE_MIN, IMPORTANCE_LOW, IMPORTANCE_DEFAULT, IMPORTANCE_HIGH})
@Retention(RetentionPolicy.SOURCE)
public @interface Importance {
}
/**
* Return whether the notifications enabled.
*
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean areNotificationsEnabled() {
return NotificationManagerCompat.from(Utils.getApp()).areNotificationsEnabled();
}
/**
* Post a notification to be shown in the status bar.
*
* @param id An identifier for this notification.
* @param consumer The consumer of create the builder of notification.
*/
public static void notify(int id, Consumer<NotificationCompat.Builder> consumer) {
notify(null, id, ChannelConfig.DEFAULT_CHANNEL_CONFIG, consumer);
}
/**
* Post a notification to be shown in the status bar.
*
* @param tag A string identifier for this notification. May be {@code null}.
* @param id An identifier for this notification.
* @param consumer The consumer of create the builder of notification.
*/
public static void notify(String tag, int id, Consumer<NotificationCompat.Builder> consumer) {
notify(tag, id, ChannelConfig.DEFAULT_CHANNEL_CONFIG, consumer);
}
/**
* Post a notification to be shown in the status bar.
*
* @param id An identifier for this notification.
* @param channelConfig The notification channel of config.
* @param consumer The consumer of create the builder of notification.
*/
public static void notify(int id, ChannelConfig channelConfig, Consumer<NotificationCompat.Builder> consumer) {
notify(null, id, channelConfig, consumer);
}
/**
* Post a notification to be shown in the status bar.
*
* @param tag A string identifier for this notification. May be {@code null}.
* @param id An identifier for this notification.
* @param channelConfig The notification channel of config.
* @param consumer The consumer of create the builder of notification.
*/
public static void notify(String tag, int id, ChannelConfig channelConfig, Consumer<NotificationCompat.Builder> consumer) {
NotificationManagerCompat.from(Utils.getApp()).notify(tag, id, getNotification(channelConfig, consumer));
}
public static Notification getNotification(ChannelConfig channelConfig, Consumer<NotificationCompat.Builder> consumer) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager nm = (NotificationManager) Utils.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
//noinspection ConstantConditions
nm.createNotificationChannel(channelConfig.getNotificationChannel());
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(Utils.getApp());
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId(channelConfig.mNotificationChannel.getId());
}
if (consumer != null) {
consumer.accept(builder);
}
return builder.build();
}
/**
* Cancel The notification.
*
* @param tag The tag for the notification will be cancelled.
* @param id The identifier for the notification will be cancelled.
*/
public static void cancel(String tag, final int id) {
NotificationManagerCompat.from(Utils.getApp()).cancel(tag, id);
}
/**
* Cancel The notification.
*
* @param id The identifier for the notification will be cancelled.
*/
public static void cancel(final int id) {
NotificationManagerCompat.from(Utils.getApp()).cancel(id);
}
/**
* Cancel all of the notifications.
*/
public static void cancelAll() {
NotificationManagerCompat.from(Utils.getApp()).cancelAll();
}
/**
* Set the notification bar's visibility.
* <p>Must hold {@code <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />}</p>
*
* @param isVisible True to set notification bar visible, false otherwise.
*/
@RequiresPermission(EXPAND_STATUS_BAR)
public static void setNotificationBarVisibility(final boolean isVisible) {
String methodName;
if (isVisible) {
methodName = (Build.VERSION.SDK_INT <= 16) ? "expand" : "expandNotificationsPanel";
} else {
methodName = (Build.VERSION.SDK_INT <= 16) ? "collapse" : "collapsePanels";
}
invokePanels(methodName);
}
private static void invokePanels(final String methodName) {
try {
@SuppressLint("WrongConstant")
Object service = Utils.getApp().getSystemService("statusbar");
@SuppressLint("PrivateApi")
Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
Method expand = statusBarManager.getMethod(methodName);
expand.invoke(service);
} catch (Exception e) {
e.printStackTrace();
}
}
public static class ChannelConfig {
public static final ChannelConfig DEFAULT_CHANNEL_CONFIG = new ChannelConfig(
Utils.getApp().getPackageName(), Utils.getApp().getPackageName(), IMPORTANCE_DEFAULT
);
private NotificationChannel mNotificationChannel;
public ChannelConfig(String id, CharSequence name, @Importance int importance) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel = new NotificationChannel(id, name, importance);
}
}
public NotificationChannel getNotificationChannel() {
return mNotificationChannel;
}
/**
* Sets whether or not notifications posted to this channel can interrupt the user in
* {@link NotificationManager.Policy#INTERRUPTION_FILTER_PRIORITY} mode.
* <p>
* Only modifiable by the system and notification ranker.
*/
public ChannelConfig setBypassDnd(boolean bypassDnd) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel.setBypassDnd(bypassDnd);
}
return this;
}
/**
* Sets the user visible description of this channel.
*
* <p>The recommended maximum length is 300 characters; the value may be truncated if it is too
* long.
*/
public ChannelConfig setDescription(String description) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel.setDescription(description);
}
return this;
}
/**
* Sets what group this channel belongs to.
* <p>
* Group information is only used for presentation, not for behavior.
* <p>
* Only modifiable before the channel is submitted to
* {@link NotificationManager#createNotificationChannel(NotificationChannel)}, unless the
* channel is not currently part of a group.
*
* @param groupId the id of a group created by
* {@link NotificationManager#createNotificationChannelGroup)}.
*/
public ChannelConfig setGroup(String groupId) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel.setGroup(groupId);
}
return this;
}
/**
* Sets the level of interruption of this notification channel.
* <p>
* Only modifiable before the channel is submitted to
* {@link NotificationManager#createNotificationChannel(NotificationChannel)}.
*
* @param importance the amount the user should be interrupted by
* notifications from this channel.
*/
public ChannelConfig setImportance(@Importance int importance) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel.setImportance(importance);
}
return this;
}
/**
* Sets the notification light color for notifications posted to this channel, if lights are
* {@link NotificationChannel#enableLights(boolean) enabled} on this channel and the device supports that feature.
* <p>
* Only modifiable before the channel is submitted to
* {@link NotificationManager#createNotificationChannel(NotificationChannel)}.
*/
public ChannelConfig setLightColor(int argb) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel.setLightColor(argb);
}
return this;
}
/**
* Sets whether notifications posted to this channel appear on the lockscreen or not, and if so,
* whether they appear in a redacted form. See e.g. {@link Notification#VISIBILITY_SECRET}.
* <p>
* Only modifiable by the system and notification ranker.
*/
public ChannelConfig setLockscreenVisibility(int lockscreenVisibility) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel.setLockscreenVisibility(lockscreenVisibility);
}
return this;
}
/**
* Sets the user visible name of this channel.
*
* <p>The recommended maximum length is 40 characters; the value may be truncated if it is too
* long.
*/
public ChannelConfig setName(CharSequence name) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel.setName(name);
}
return this;
}
/**
* Sets whether notifications posted to this channel can appear as application icon badges
* in a Launcher.
* <p>
* Only modifiable before the channel is submitted to
* {@link NotificationManager#createNotificationChannel(NotificationChannel)}.
*
* @param showBadge true if badges should be allowed to be shown.
*/
public ChannelConfig setShowBadge(boolean showBadge) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel.setShowBadge(showBadge);
}
return this;
}
/**
* Sets the sound that should be played for notifications posted to this channel and its
* audio attributes. Notification channels with an {@link NotificationChannel#getImportance() importance} of at
* least {@link NotificationManager#IMPORTANCE_DEFAULT} should have a sound.
* <p>
* Only modifiable before the channel is submitted to
* {@link NotificationManager#createNotificationChannel(NotificationChannel)}.
*/
public ChannelConfig setSound(Uri sound, AudioAttributes audioAttributes) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel.setSound(sound, audioAttributes);
}
return this;
}
/**
* Sets the vibration pattern for notifications posted to this channel. If the provided
* pattern is valid (non-null, non-empty), will {@link NotificationChannel#enableVibration(boolean)} enable
* vibration} as well. Otherwise, vibration will be disabled.
* <p>
* Only modifiable before the channel is submitted to
* {@link NotificationManager#createNotificationChannel(NotificationChannel)}.
*/
public ChannelConfig setVibrationPattern(long[] vibrationPattern) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationChannel.setVibrationPattern(vibrationPattern);
}
return this;
}
}
public interface Consumer<T> {
void accept(T t);
}
}