-
Notifications
You must be signed in to change notification settings - Fork 35
/
Builder.java
496 lines (404 loc) · 15.3 KB
/
Builder.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
/*
* Apache 2.0 License
*
* Copyright (c) Sebastian Katzer 2017
* Contributor Bhumin Bhandari
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apache License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://opensource.org/licenses/Apache-2.0/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*/
package de.appplant.cordova.plugin.notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationCompat.MessagingStyle.Message;
import androidx.media.app.NotificationCompat.MediaStyle;
import android.support.v4.media.session.MediaSessionCompat;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Paint;
import android.graphics.Canvas;
import java.util.List;
import de.appplant.cordova.plugin.notification.action.Action;
import de.appplant.cordova.plugin.notification.util.LaunchUtils;
import static de.appplant.cordova.plugin.notification.Notification.EXTRA_UPDATE;
/**
* Builder class for local notifications. Build fully configured local
* notification specified by JSON object passed from JS side.
*/
public final class Builder {
// Application context passed by constructor
private final Context context;
// Notification options passed by JS
private final Options options;
// Receiver to handle the clear event
private Class<?> clearReceiver;
// Activity to handle the click event
private Class<?> clickActivity;
// Additional extras to merge into each intent
private Bundle extras;
/**
* Constructor
*
* @param options Notification options
*/
public Builder(Options options) {
this.context = options.getContext();
this.options = options;
}
/**
* Set clear receiver.
*
* @param receiver Broadcast receiver for the clear event.
*/
public Builder setClearReceiver(Class<?> receiver) {
this.clearReceiver = receiver;
return this;
}
/**
* Set click activity.
*
* @param activity The activity to handler the click event.
*/
public Builder setClickActivity(Class<?> activity) {
this.clickActivity = activity;
return this;
}
/**
* Set bundle extras.
*
* @param extras The bundled extras to merge into.
*/
public Builder setExtras(Bundle extras) {
this.extras = extras;
return this;
}
/**
* Creates the notification with all its options passed through JS.
*
* @return The final notification to display.
*/
public Notification build() {
NotificationCompat.Builder builder;
if (options.isSilent()) {
return new Notification(context, options);
}
Uri sound = options.getSound();
Bundle extras = new Bundle();
extras.putInt(Notification.EXTRA_ID, options.getId());
extras.putString(Options.EXTRA_SOUND, sound.toString());
builder = findOrCreateBuilder()
.setDefaults(options.getDefaults())
.setExtras(extras)
.setOnlyAlertOnce(false)
.setChannelId(options.getChannel())
.setContentTitle(options.getTitle())
.setContentText(options.getText())
.setTicker(options.getText())
.setNumber(options.getNumber())
.setAutoCancel(options.isAutoClear())
.setOngoing(options.isSticky())
.setColor(options.getColor())
.setVisibility(options.getVisibility())
.setPriority(options.getPrio())
.setShowWhen(options.showClock())
.setUsesChronometer(options.showChronometer())
.setGroup(options.getGroup())
.setGroupSummary(options.getGroupSummary())
.setTimeoutAfter(options.getTimeout())
.setLights(options.getLedColor(), options.getLedOn(), options.getLedOff());
if (!sound.equals(Uri.EMPTY) && !isUpdate()) {
builder.setSound(sound);
}
// API < 26. Setting sound to null will prevent playing if we have no sound for any reason,
// including a 0 volume.
if (options.isWithoutSound()) {
builder.setSound(null);
}
if (options.isWithProgressBar()) {
builder.setProgress(
options.getProgressMaxValue(),
options.getProgressValue(),
options.isIndeterminateProgress());
}
if (options.hasLargeIcon()) {
builder.setSmallIcon(options.getSmallIcon());
Bitmap largeIcon = options.getLargeIcon();
if (options.getLargeIconType().equals("circle")) {
largeIcon = getCircleBitmap(largeIcon);
}
builder.setLargeIcon(largeIcon);
} else {
builder.setSmallIcon(options.getSmallIcon());
}
if (options.useFullScreenIntent()) {
applyFullScreenIntent(builder);
}
applyStyle(builder);
applyActions(builder);
applyDeleteReceiver(builder);
applyContentReceiver(builder);
return new Notification(context, options, builder);
}
void applyFullScreenIntent(NotificationCompat.Builder builder) {
String pkgName = context.getPackageName();
Intent intent = context
.getPackageManager()
.getLaunchIntentForPackage(pkgName)
.putExtra("launchNotificationId", options.getId());
PendingIntent pendingIntent = LaunchUtils.getActivityPendingIntent(context, intent);
builder.setFullScreenIntent(pendingIntent, true);
}
/**
* Convert a bitmap to a circular bitmap.
* This code has been extracted from the Phonegap Plugin Push plugin:
* https://github.com/phonegap/phonegap-plugin-push
*
* @param bitmap Bitmap to convert.
* @return Circular bitmap.
*/
private Bitmap getCircleBitmap(Bitmap bitmap) {
if (bitmap == null) {
return null;
}
final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(output);
final int color = Color.RED;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
//final RectF rectF = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
float cx = bitmap.getWidth() / 2.0f;
float cy = bitmap.getHeight() / 2.0f;
float radius = Math.min(cx, cy);
canvas.drawCircle(cx, cy, radius, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
bitmap.recycle();
return output;
}
/**
* Find out and set the notification style.
*
* @param builder Local notification builder instance.
*/
private void applyStyle(NotificationCompat.Builder builder) {
Message[] messages = options.getMessages();
String summary = options.getSummary();
if (messages != null) {
applyMessagingStyle(builder, messages);
return;
}
MediaSessionCompat.Token token = options.getMediaSessionToken();
if (token != null) {
applyMediaStyle(builder, token);
return;
}
List<Bitmap> pics = options.getAttachments();
if (pics.size() > 0) {
applyBigPictureStyle(builder, pics);
return;
}
String text = options.getText();
if (text != null && text.contains("\n")) {
applyInboxStyle(builder);
return;
}
if (text == null || summary == null && text.length() < 45)
return;
applyBigTextStyle(builder);
}
/**
* Apply inbox style.
*
* @param builder Local notification builder instance.
* @param messages The messages to add to the conversation.
*/
private void applyMessagingStyle(NotificationCompat.Builder builder,
Message[] messages) {
NotificationCompat.MessagingStyle style;
style = new NotificationCompat.MessagingStyle("Me")
.setConversationTitle(options.getTitle());
for (Message msg : messages) {
style.addMessage(msg);
}
builder.setStyle(style);
}
/**
* Apply inbox style.
*
* @param builder Local notification builder instance.
* @param pics The pictures to show.
*/
private void applyBigPictureStyle(NotificationCompat.Builder builder,
List<Bitmap> pics) {
NotificationCompat.BigPictureStyle style;
String summary = options.getSummary();
String text = options.getText();
style = new NotificationCompat.BigPictureStyle(builder)
.setSummaryText(summary == null ? text : summary)
.bigPicture(pics.get(0));
builder.setStyle(style);
}
/**
* Apply inbox style.
*
* @param builder Local notification builder instance.
*/
private void applyInboxStyle(NotificationCompat.Builder builder) {
NotificationCompat.InboxStyle style;
String text = options.getText();
style = new NotificationCompat.InboxStyle(builder)
.setSummaryText(options.getSummary());
for (String line : text.split("\n")) {
style.addLine(line);
}
builder.setStyle(style);
}
/**
* Apply big text style.
*
* @param builder Local notification builder instance.
*/
private void applyBigTextStyle(NotificationCompat.Builder builder) {
NotificationCompat.BigTextStyle style;
style = new NotificationCompat.BigTextStyle(builder)
.setSummaryText(options.getSummary())
.bigText(options.getText());
builder.setStyle(style);
}
/**
* Apply media style.
*
* @param builder Local notification builder instance.
* @param token The media session token.
*/
private void applyMediaStyle(NotificationCompat.Builder builder,
MediaSessionCompat.Token token) {
MediaStyle style;
style = new MediaStyle(builder)
.setMediaSession(token)
.setShowActionsInCompactView(1);
builder.setStyle(style);
}
/**
* Set intent to handle the delete event. Will clean up some persisted
* preferences.
*
* @param builder Local notification builder instance.
*/
private void applyDeleteReceiver(NotificationCompat.Builder builder) {
if (clearReceiver == null)
return;
Intent intent = new Intent(context, clearReceiver)
.setAction(options.getIdentifier())
.putExtra(Notification.EXTRA_ID, options.getId());
if (extras != null) {
intent.putExtras(extras);
}
PendingIntent deleteIntent = LaunchUtils.getBroadcastPendingIntent(context, intent);
builder.setDeleteIntent(deleteIntent);
}
/**
* Set intent to handle the click event. Will bring the app to
* foreground.
*
* @param builder Local notification builder instance.
*/
private void applyContentReceiver(NotificationCompat.Builder builder) {
if (clickActivity == null)
return;
Action[] actions = options.getActions();
if (actions != null && actions.length > 0 ) {
// if actions are defined, the user must click on button actions to launch the app.
// Don't make the notification clickable in this case
return;
}
Intent intent = new Intent(context, clickActivity)
.putExtra(Notification.EXTRA_ID, options.getId())
.putExtra(Action.EXTRA_ID, Action.CLICK_ACTION_ID)
.putExtra(Options.EXTRA_LAUNCH, options.isLaunchingApp())
.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
if (extras != null) {
intent.putExtras(extras);
}
PendingIntent contentIntent = LaunchUtils.getTaskStackPendingIntent(context, intent);
builder.setContentIntent(contentIntent);
}
/**
* Add all actions to the builder if there are any actions.
*
* @param builder Local notification builder instance.
*/
private void applyActions (NotificationCompat.Builder builder) {
Action[] actions = options.getActions();
NotificationCompat.Action.Builder btn;
if (actions == null || actions.length == 0)
return;
for (Action action : actions) {
btn = new NotificationCompat.Action.Builder(
action.getIcon(), action.getTitle(),
getPendingIntentForAction(action));
if (action.isWithInput()) {
btn.addRemoteInput(action.getInput());
}
builder.addAction(btn.build());
}
}
/**
* Returns a new PendingIntent for a notification action, including the
* action's identifier.
*
* @param action Notification action needing the PendingIntent
*/
private PendingIntent getPendingIntentForAction (Action action) {
Intent intent = new Intent(context, clickActivity)
.putExtra(Notification.EXTRA_ID, options.getId())
.putExtra(Action.EXTRA_ID, action.getId())
.putExtra(Options.EXTRA_LAUNCH, action.isLaunchingApp())
.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
if (extras != null) {
intent.putExtras(extras);
}
return LaunchUtils.getTaskStackPendingIntent(context, intent);
}
/**
* If the builder shall build an notification or an updated version.
*
* @return true in case of an updated version.
*/
private boolean isUpdate() {
return extras != null && extras.getBoolean(EXTRA_UPDATE, false);
}
/**
* Returns a cached builder instance or creates a new one.
*/
private NotificationCompat.Builder findOrCreateBuilder() {
int key = options.getId();
NotificationCompat.Builder builder = Notification.getCachedBuilder(key);
if (builder == null) {
builder = new NotificationCompat.Builder(context, options.getChannel());
}
return builder;
}
}