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
195 lines (159 loc) · 7.9 KB
/
SantaNotificationBuilder.java
File metadata and controls
195 lines (159 loc) · 7.9 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
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
/*
* Copyright (C) 2016 Google Inc. All Rights Reserved.
*
* 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
*
* http://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.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.graphics.drawable.Drawable;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v4.app.TaskStackBuilder;
import android.support.v4.content.res.ResourcesCompat;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.google.android.apps.santatracker.common.NotificationConstants;
import com.google.android.apps.santatracker.launch.StartupActivity;
public class SantaNotificationBuilder {
// private static final String TAG = "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 mBuilder = new NotificationCompat.Builder(c)
.setSmallIcon(R.drawable.notification_small)
.setColor(ResourcesCompat.getColor(c.getResources(),
R.color.brandSantaTracker, c.getTheme()))
.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);
mBuilder.setContentIntent(resultPendingIntent);
return mBuilder.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);
}
public static void createInfoNotification(final Context c, final String title,
final String text, final String photoUrl) {
if (photoUrl != null) {
Glide.with(c).load(photoUrl).asBitmap().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource,
GlideAnimation<? super Bitmap> glideAnimation) {
createInfoNotificationWithBitmap(c, title, text, resource);
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
createInfoNotificationWithBitmap(c, title, text, null);
}
});
} else {
createInfoNotificationWithBitmap(c, title, text, null);
}
}
private static void createInfoNotificationWithBitmap(Context c, String title, String text,
Bitmap photo) {
Resources r = c.getResources();
Bitmap largeIcon = BitmapFactory.decodeResource(r,
R.drawable.santa_info_notification_background);
NotificationCompat.WearableExtender wearableExtender =
new NotificationCompat.WearableExtender()
.setHintHideIcon(false)
.setBackground(largeIcon);
if (photo != null) {
NotificationCompat.Builder page = new NotificationCompat.Builder(c)
.setSmallIcon(R.drawable.notification_small)
.setContentText(text)
.extend(new NotificationCompat.WearableExtender().setBackground(photo));
wearableExtender.addPage(page.build());
}
// Make main notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(c)
.setSmallIcon(R.drawable.notification_small)
.setColor(ResourcesCompat.getColor(c.getResources(),
R.color.brandSantaTrackerDark, c.getTheme()))
.setLargeIcon(largeIcon)
.setAutoCancel(true)
.setContentTitle(title)
.extend(wearableExtender);
if (photo == null) {
mBuilder.setContentText(text);
}
Intent i = new Intent(c, StartupActivity.class);
i.putExtra(NotificationConstants.KEY_NOTIFICATION_TYPE, NotificationConstants.KEY_LOCATION);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(c);
stackBuilder.addParentStack(StartupActivity.class);
stackBuilder.addNextIntent(i);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
Notification n = mBuilder.build();
NotificationManager mNotificationManager = (NotificationManager) c
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NotificationConstants.NOTIFICATION_ID, n);
}
/**
* Dismiss all notifications.
*/
public static void dismissNotifications(Context c) {
NotificationManager mNotificationManager = (NotificationManager) c
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
// Send a broadcast to the phone's service, so it will update the data API to
// let the watch know to cancel all its corresponding notifications.
Intent dismissWearableNotifications = new Intent(c, PhoneNotificationService.class);
dismissWearableNotifications.setAction(NotificationConstants.ACTION_DISMISS);
c.sendBroadcast(dismissWearableNotifications);
}
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);
}
}