/
NotificationProxy.java
324 lines (288 loc) Β· 9.26 KB
/
NotificationProxy.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
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2014 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.android.notificationmanager;
import java.util.Date;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.TiContext;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
import ti.modules.titanium.android.AndroidModule;
import ti.modules.titanium.android.PendingIntentProxy;
import ti.modules.titanium.android.RemoteViewsProxy;
import android.app.Notification;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationCompat.Builder;
@SuppressWarnings("deprecation")
@Kroll.proxy(creatableInModule=AndroidModule.class, propertyAccessors = {
TiC.PROPERTY_CONTENT_TEXT,
TiC.PROPERTY_CONTENT_TITLE
})
public class NotificationProxy extends KrollProxy
{
private static final String TAG = "TiNotification";
protected Builder notificationBuilder;
private int flags, ledARGB, ledOnMS, ledOffMS;
private Uri sound;
private int audioStreamType;
public NotificationProxy()
{
super();
notificationBuilder = new NotificationCompat.Builder(TiApplication.getAppCurrentActivity())
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setWhen(System.currentTimeMillis());
//set up default values
flags = Notification.FLAG_AUTO_CANCEL;
audioStreamType = Notification.STREAM_DEFAULT;
}
public NotificationProxy(TiContext tiContext)
{
this();
}
@Override
public void handleCreationDict(KrollDict d)
{
super.handleCreationDict(d);
if (d == null) {
return;
}
if (d.containsKey(TiC.PROPERTY_ICON)) {
setIcon(d.get(TiC.PROPERTY_ICON));
}
if (d.containsKey(TiC.PROPERTY_TICKER_TEXT)) {
setTickerText(TiConvert.toString(d, TiC.PROPERTY_TICKER_TEXT));
}
if (d.containsKey(TiC.PROPERTY_WHEN)) {
setWhen(d.get(TiC.PROPERTY_WHEN));
}
if (d.containsKey(TiC.PROPERTY_AUDIO_STREAM_TYPE)) {
setAudioStreamType(TiConvert.toInt(d, TiC.PROPERTY_AUDIO_STREAM_TYPE));
}
if (d.containsKey(TiC.PROPERTY_CONTENT_VIEW)) {
setContentView((RemoteViewsProxy) d.get(TiC.PROPERTY_CONTENT_VIEW));
}
if (d.containsKey(TiC.PROPERTY_CONTENT_INTENT)) {
setContentIntent((PendingIntentProxy) d.get(TiC.PROPERTY_CONTENT_INTENT));
}
if (d.containsKey(TiC.PROPERTY_DEFAULTS)) {
setDefaults(TiConvert.toInt(d, TiC.PROPERTY_DEFAULTS));
}
if (d.containsKey(TiC.PROPERTY_DELETE_INTENT)) {
setDeleteIntent((PendingIntentProxy) d.get(TiC.PROPERTY_DELETE_INTENT));
}
if (d.containsKey(TiC.PROPERTY_FLAGS)) {
setFlags(TiConvert.toInt(d, TiC.PROPERTY_FLAGS));
}
if (d.containsKey(TiC.PROPERTY_LED_ARGB)) {
setLedARGB(TiConvert.toInt(d, TiC.PROPERTY_LED_ARGB));
}
if (d.containsKey(TiC.PROPERTY_LED_OFF_MS)) {
setLedOffMS(TiConvert.toInt(d, TiC.PROPERTY_LED_OFF_MS));
}
if (d.containsKey(TiC.PROPERTY_LED_ON_MS)) {
setLedOnMS(TiConvert.toInt(d, TiC.PROPERTY_LED_ON_MS));
}
if (d.containsKey(TiC.PROPERTY_NUMBER)) {
setNumber(TiConvert.toInt(d, TiC.PROPERTY_NUMBER));
}
if (d.containsKey(TiC.PROPERTY_SOUND)) {
setSound(TiConvert.toString(d, TiC.PROPERTY_SOUND));
}
if (d.containsKey(TiC.PROPERTY_VIBRATE_PATTERN)) {
setVibratePattern((Object[]) d.get(TiC.PROPERTY_VIBRATE_PATTERN));
}
if (d.containsKey(TiC.PROPERTY_VISIBILITY)) {
setVisibility(TiConvert.toInt(d, TiC.PROPERTY_VISIBILITY));
}
if (d.containsKey(TiC.PROPERTY_CATEGORY)) {
setCategory(TiConvert.toString(d, TiC.PROPERTY_CATEGORY));
}
if (d.containsKey(TiC.PROPERTY_PRIORITY)) {
setPriority(TiConvert.toInt(d, TiC.PROPERTY_PRIORITY));
}
checkLatestEventInfoProperties(d);
}
@Kroll.method @Kroll.setProperty
public void setCategory(String category)
{
notificationBuilder.setCategory(category);
setProperty(TiC.PROPERTY_CATEGORY, category);
}
@Kroll.method @Kroll.setProperty
public void setIcon(Object icon)
{
if (icon instanceof Number) {
notificationBuilder.setSmallIcon(((Number)icon).intValue());
} else {
String iconUrl = TiConvert.toString(icon);
if (iconUrl == null) {
Log.e(TAG, "Url is null");
return;
}
String iconFullUrl = resolveUrl(null, iconUrl);
notificationBuilder.setSmallIcon(TiUIHelper.getResourceId(iconFullUrl));
}
setProperty(TiC.PROPERTY_ICON, icon);
}
@Kroll.method @Kroll.setProperty
public void setVisibility(int visibility)
{
notificationBuilder.setVisibility(visibility);
setProperty(TiC.PROPERTY_VISIBILITY, visibility);
}
@Kroll.method @Kroll.setProperty
public void setPriority(int priority)
{
notificationBuilder.setPriority(priority);
setProperty(TiC.PROPERTY_PRIORITY, priority);
}
@Kroll.method @Kroll.setProperty
public void setTickerText(String tickerText)
{
notificationBuilder.setTicker(tickerText);
//set the javascript object
setProperty(TiC.PROPERTY_TICKER_TEXT, tickerText);
}
@Kroll.method @Kroll.setProperty
public void setWhen(Object when)
{
if (when instanceof Date) {
notificationBuilder.setWhen(((Date)when).getTime());
} else {
notificationBuilder.setWhen(((Double) TiConvert.toDouble(when)).longValue());
}
setProperty(TiC.PROPERTY_WHEN, when);
}
@Kroll.method @Kroll.setProperty
public void setAudioStreamType(int type)
{
audioStreamType = type;
if (sound != null) {
notificationBuilder.setSound(this.sound, audioStreamType);
}
setProperty(TiC.PROPERTY_AUDIO_STREAM_TYPE, type);
}
@Kroll.method @Kroll.setProperty
public void setContentView(RemoteViewsProxy contentView)
{
notificationBuilder.setContent(contentView.getRemoteViews());
setProperty(TiC.PROPERTY_CONTENT_VIEW, contentView);
}
@Kroll.method @Kroll.setProperty
public void setContentIntent(PendingIntentProxy contentIntent)
{
notificationBuilder.setContentIntent(contentIntent.getPendingIntent());
setProperty(TiC.PROPERTY_CONTENT_INTENT, contentIntent);
}
@Kroll.method @Kroll.setProperty
public void setDefaults(int defaults)
{
notificationBuilder.setDefaults(defaults);
setProperty(TiC.PROPERTY_DEFAULTS, defaults);
}
@Kroll.method @Kroll.setProperty
public void setDeleteIntent(PendingIntentProxy deleteIntent)
{
notificationBuilder.setDeleteIntent(deleteIntent.getPendingIntent());
setProperty(TiC.PROPERTY_DELETE_INTENT, deleteIntent);
}
@Kroll.method @Kroll.setProperty
public void setFlags(int flags)
{
this.flags = flags;
setProperty(TiC.PROPERTY_FLAGS, flags);
}
@Kroll.method @Kroll.setProperty
public void setLedARGB(int ledARGB)
{
this.ledARGB = ledARGB;
notificationBuilder.setLights(this.ledARGB, ledOnMS, ledOffMS);
setProperty(TiC.PROPERTY_LED_ARGB, ledARGB);
}
@Kroll.method @Kroll.setProperty
public void setLedOffMS(int ledOffMS)
{
this.ledOffMS = ledOffMS;
notificationBuilder.setLights(ledARGB, ledOnMS, this.ledOffMS);
setProperty(TiC.PROPERTY_LED_OFF_MS, ledOffMS);
}
@Kroll.method @Kroll.setProperty
public void setLedOnMS(int ledOnMS)
{
this.ledOnMS = ledOnMS;
notificationBuilder.setLights(ledARGB, this.ledOnMS, ledOffMS);
setProperty(TiC.PROPERTY_LED_ON_MS, ledOnMS);
}
@Kroll.method @Kroll.setProperty
public void setNumber(int number)
{
notificationBuilder.setNumber(number);
setProperty(TiC.PROPERTY_NUMBER, number);
}
@Kroll.method @Kroll.setProperty
public void setSound(String url)
{
if (url == null) {
Log.e(TAG, "Url is null");
return;
}
sound = Uri.parse(resolveUrl(null, url));
notificationBuilder.setSound(sound, audioStreamType);
setProperty(TiC.PROPERTY_SOUND, url);
}
@Kroll.method @Kroll.setProperty
public void setVibratePattern(Object[] pattern)
{
if (pattern != null) {
long[] vibrate = new long[pattern.length];
for (int i = 0; i < pattern.length; i++) {
vibrate[i] = ((Double)TiConvert.toDouble(pattern[i])).longValue();
}
notificationBuilder.setVibrate(vibrate);
}
setProperty(TiC.PROPERTY_VIBRATE_PATTERN, pattern);
}
protected void checkLatestEventInfoProperties(KrollDict d)
{
if (d.containsKeyAndNotNull(TiC.PROPERTY_CONTENT_TITLE)
|| d.containsKeyAndNotNull(TiC.PROPERTY_CONTENT_TEXT)) {
String contentTitle = "";
String contentText = "";
if (d.containsKeyAndNotNull(TiC.PROPERTY_CONTENT_TITLE)) {
contentTitle = TiConvert.toString(d, TiC.PROPERTY_CONTENT_TITLE);
notificationBuilder.setContentTitle(contentTitle);
}
if (d.containsKeyAndNotNull(TiC.PROPERTY_CONTENT_TEXT)) {
contentText = TiConvert.toString(d, TiC.PROPERTY_CONTENT_TEXT);
notificationBuilder.setContentText(contentText);
}
}
}
@Kroll.method
public void setLatestEventInfo(String contentTitle, String contentText, PendingIntentProxy contentIntent)
{
notificationBuilder.setContentIntent(contentIntent.getPendingIntent())
.setContentText(contentText)
.setContentTitle(contentTitle);
}
public Notification buildNotification()
{
Notification notification = notificationBuilder.build();
notification.flags = this.flags;
return notification;
}
@Override
public String getApiName()
{
return "Ti.Android.Notification";
}
}