forked from google/santa-tracker-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSantaNotificationBuilder.java
More file actions
119 lines (100 loc) · 4.97 KB
/
SantaNotificationBuilder.java
File metadata and controls
119 lines (100 loc) · 4.97 KB
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
/*
* Copyright 2019. Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.apps.santatracker;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import androidx.core.app.TaskStackBuilder;
import androidx.core.content.ContextCompat;
import com.google.android.apps.santatracker.common.NotificationConstants;
import com.google.android.apps.santatracker.launch.StartupActivity;
public class SantaNotificationBuilder {
private static Notification getNotification(Context c, int headline) {
Resources r = c.getResources();
Bitmap largeIcon =
BitmapFactory.decodeResource(r, R.drawable.santa_notification_background);
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender()
.setHintHideIcon(false)
.setBackground(largeIcon);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(c, NotificationConstants.CHANNEL_ID)
.setSmallIcon(R.drawable.notification_small)
.setColor(ContextCompat.getColor(c, R.color.brandSantaTracker))
.setAutoCancel(true)
.setContentTitle(r.getString(headline))
.setContentText(r.getString(R.string.track_santa))
.extend(wearableExtender);
Intent i = new Intent(c, StartupActivity.class);
i.putExtra(NotificationConstants.KEY_NOTIFICATION_TYPE, NotificationConstants.TAKEOFF_PATH);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(c);
stackBuilder.addParentStack(StartupActivity.class);
stackBuilder.addNextIntent(i);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
return builder.build();
}
public static void createSantaNotification(Context c, int content) {
Notification n = getNotification(c, content);
// Post the notification.
NotificationManagerCompat.from(c).notify(NotificationConstants.NOTIFICATION_ID, n);
}
/** Dismiss all notifications. */
public static void dismissNotifications(Context c) {
NotificationManager mNotificationManager =
(NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
}
public static void createNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = context.getString(R.string.app_name_santa);
final int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel =
new NotificationChannel(NotificationConstants.CHANNEL_ID, name, importance);
// Register the channel with the system; you can't change the importance
// or other notification behaviors after this
NotificationManager nmgr = context.getSystemService(NotificationManager.class);
nmgr.createNotificationChannel(channel);
}
}
public static void scheduleSantaNotification(Context c, long timestamp, int notificationType) {
// Only schedule a notification if the time is in the future
if (timestamp < System.currentTimeMillis()) {
return;
}
AlarmManager alarm = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(c, NotificationBroadcastReceiver.class);
i.putExtra(
NotificationConstants.KEY_NOTIFICATION_ID, NotificationConstants.NOTIFICATION_ID);
// Type is "takeoff", "location", etc.
i.putExtra(NotificationConstants.KEY_NOTIFICATION_TYPE, notificationType);
// Generate unique pending intent
PendingIntent pi = PendingIntent.getBroadcast(c, notificationType, i, 0);
// Deliver next time the device is woken up
alarm.set(AlarmManager.RTC, timestamp, pi);
}
}