blob: 9ac4dbfed65a3b25ebd52c217e8a556fbecd2145 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.statusbartest;
18
Joe Onoratoef1e7762010-09-17 18:38:38 -040019import android.app.Notification;
20import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.app.PendingIntent;
Christian Mehlmaueref367522010-05-31 23:08:30 +020022import android.content.Context;
The Android Open Source Project4df24232009-03-05 14:34:35 -080023import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.content.Intent;
Joe Onoratoef1e7762010-09-17 18:38:38 -040025import android.graphics.Bitmap;
Julia Reynoldsa07af882015-12-17 08:32:48 -050026import android.graphics.BitmapFactory;
Joe Onoratoef1e7762010-09-17 18:38:38 -040027import android.graphics.drawable.BitmapDrawable;
Daniel Sandlerbc5559f2012-04-19 01:08:15 -040028import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.os.Vibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.os.Handler;
Dianne Hackborn41203752012-08-31 14:05:51 -070031import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080032import android.util.Log;
33import android.net.Uri;
34import android.os.SystemClock;
35import android.widget.RemoteViews;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.PowerManager;
37
Daniel Sandlere40451a2011-02-03 14:51:35 -050038// private NM API
39import android.app.INotificationManager;
Julia Reynolds81afbcd2016-02-09 14:54:08 -050040import android.widget.Toast;
Daniel Sandlere40451a2011-02-03 14:51:35 -050041
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042public class NotificationTestList extends TestActivity
43{
44 private final static String TAG = "NotificationTestList";
45
46 NotificationManager mNM;
Daniel Sandlerbc5559f2012-04-19 01:08:15 -040047 Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048 Handler mHandler = new Handler();
49
Daniel Sandlerbc5559f2012-04-19 01:08:15 -040050 long mActivityCreateTime;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051 long mChronometerBase = 0;
52
Daniel Sandler373a9982010-11-30 12:03:59 -050053 boolean mProgressDone = true;
54
Daniel Sandlerb0cc50d2010-10-26 16:55:56 -040055 final int[] kNumberedIconResIDs = {
56 R.drawable.notification0,
57 R.drawable.notification1,
58 R.drawable.notification2,
59 R.drawable.notification3,
60 R.drawable.notification4,
61 R.drawable.notification5,
62 R.drawable.notification6,
63 R.drawable.notification7,
64 R.drawable.notification8,
65 R.drawable.notification9
66 };
67 final int kUnnumberedIconResID = R.drawable.notificationx;
68
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 @Override
Daniel Sandlerbc5559f2012-04-19 01:08:15 -040070 public void onCreate(Bundle icicle) {
71 super.onCreate(icicle);
72 mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
73 mActivityCreateTime = System.currentTimeMillis();
74 }
75
76 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 protected String tag() {
78 return TAG;
79 }
80
81 @Override
82 protected Test[] tests() {
83 mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
84
85 return mTests;
86 }
87
88 private Test[] mTests = new Test[] {
Chris Wren1ce4b6d2015-06-11 10:19:43 -040089 new Test("Off") {
Joe Onoratoe71d9e42009-11-05 17:12:18 -050090 public void run() {
Christian Mehlmaueref367522010-05-31 23:08:30 +020091 PowerManager pm = (PowerManager)NotificationTestList.this.getSystemService(Context.POWER_SERVICE);
Joe Onoratoe71d9e42009-11-05 17:12:18 -050092 PowerManager.WakeLock wl =
93 pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sound");
94 wl.acquire();
95
96 pm.goToSleep(SystemClock.uptimeMillis());
97
Chris Wren1ce4b6d2015-06-11 10:19:43 -040098 Notification n = new Notification.Builder(NotificationTestList.this)
99 .setSmallIcon(R.drawable.stat_sys_phone)
100 .setContentTitle(name)
101 .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
102 getPackageName() + "/raw/ringer"))
103 .build();
Joe Onoratoe71d9e42009-11-05 17:12:18 -0500104 Log.d(TAG, "n.sound=" + n.sound);
105
106 mNM.notify(1, n);
107
108 Log.d(TAG, "releasing wake lock");
109 wl.release();
110 Log.d(TAG, "released wake lock");
111 }
112 },
113
Joe Onoratoef1e7762010-09-17 18:38:38 -0400114 new Test("Cancel #1") {
115 public void run()
116 {
117 mNM.cancel(1);
118 }
119 },
120
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400121 new Test("Custom Button") {
Joe Onorato4058ba02010-10-31 11:38:04 -0700122 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400123 Notification n = new Notification.Builder(NotificationTestList.this)
124 .setSmallIcon(R.drawable.icon1)
125 .setWhen(mActivityCreateTime)
126 .setContentTitle(name)
127 .setOngoing(true)
128 .build();
Joe Onorato4058ba02010-10-31 11:38:04 -0700129 n.contentView = new RemoteViews(getPackageName(), R.layout.button_notification);
Joe Onorato4058ba02010-10-31 11:38:04 -0700130 n.contentView.setOnClickPendingIntent(R.id.button, makeIntent2());
131
132 mNM.notify(1, n);
133 }
134 },
135
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400136 new Test("Action Button") {
Joe Onorato184498c2010-10-08 17:57:18 -0400137 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400138 Notification n = new Notification.Builder(NotificationTestList.this)
139 .setSmallIcon(R.drawable.icon1)
140 .setWhen(mActivityCreateTime)
141 .setContentTitle(name)
142 .setOngoing(true)
143 .addAction(R.drawable.ic_statusbar_chat, "Button", makeIntent2())
144 .build();
145
Joe Onorato184498c2010-10-08 17:57:18 -0400146 mNM.notify(1, n);
147 }
148 },
149
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400150 new Test("with intent") {
Joe Onoratoef1e7762010-09-17 18:38:38 -0400151 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400152 Notification n = new Notification.Builder(NotificationTestList.this)
153 .setSmallIcon(R.drawable.icon1)
154 .setWhen(mActivityCreateTime)
155 .setContentTitle("Persistent #1")
156 .setContentText("This is a notification!!!")
157 .setContentIntent(makeIntent2())
158 .setOngoing(true)
159 .build();
160
Joe Onoratoef1e7762010-09-17 18:38:38 -0400161 mNM.notify(1, n);
162 }
163 },
164
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500165 new Test("Is blocked?") {
166 public void run() {
167 Toast.makeText(NotificationTestList.this,
168 "package enabled? " + mNM.areNotificationsEnabled(),
169 Toast.LENGTH_LONG).show();
170 }
171 },
172
Julia Reynoldsef37f282016-02-12 09:11:27 -0500173 new Test("importance?") {
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500174 public void run() {
175 Toast.makeText(NotificationTestList.this,
Julia Reynoldsef37f282016-02-12 09:11:27 -0500176 "importance? " + mNM.getImportance(),
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500177 Toast.LENGTH_LONG).show();
178 }
179 },
180
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400181 new Test("Whens") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 public void run()
183 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400184 Notification.Builder n = new Notification.Builder(NotificationTestList.this)
185 .setSmallIcon(R.drawable.icon1)
186 .setContentTitle(name)
187 .setOngoing(true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400189 mNM.notify(1, n.setContentTitle("(453) 123-2328")
190 .setWhen(System.currentTimeMillis()-(1000*60*60*24))
191 .build());
Joe Onoratoddf680b2010-09-26 13:59:40 -0700192
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400193 mNM.notify(1, n.setContentTitle("Mark Willem, Me (2)")
194 .setWhen(System.currentTimeMillis())
195 .build());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400197 mNM.notify(1, n.setContentTitle("Sophia Winterlanden")
198 .setWhen(System.currentTimeMillis() + (1000 * 60 * 60 * 24))
199 .build());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 }
201 },
202
Joe Onorato005847b2010-06-04 16:08:02 -0400203 new Test("Bad Icon #1 (when=create)") {
Joe Onorato871bdb92010-05-24 18:36:53 -0400204 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400205 Notification n = new Notification.Builder(NotificationTestList.this)
206 .setSmallIcon(R.layout.chrono_notification /* not an icon */)
207 .setWhen(mActivityCreateTime)
208 .setContentTitle("Persistent #1")
209 .setContentText("This is the same notification!!")
210 .setContentIntent(makeIntent())
211 .build();
Joe Onorato005847b2010-06-04 16:08:02 -0400212 mNM.notify(1, n);
Joe Onorato871bdb92010-05-24 18:36:53 -0400213 }
214 },
215
Joe Onorato005847b2010-06-04 16:08:02 -0400216 new Test("Bad Icon #1 (when=now)") {
217 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400218 Notification n = new Notification.Builder(NotificationTestList.this)
219 .setSmallIcon(R.layout.chrono_notification /* not an icon */)
220 .setWhen(System.currentTimeMillis())
221 .setContentTitle("Persistent #1")
222 .setContentText("This is the same notification!!")
223 .setContentIntent(makeIntent())
224 .build();
Joe Onorato005847b2010-06-04 16:08:02 -0400225 mNM.notify(1, n);
Joe Onorato68065e02010-02-03 20:21:41 -0800226 }
227 },
228
Daniel Sandlere40451a2011-02-03 14:51:35 -0500229 new Test("Null Icon #1 (when=now)") {
230 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400231 Notification n = new Notification.Builder(NotificationTestList.this)
232 .setSmallIcon(0)
233 .setWhen(System.currentTimeMillis())
234 .setContentTitle("Persistent #1")
235 .setContentText("This is the same notification!!")
236 .setContentIntent(makeIntent())
237 .build();
Daniel Sandlere40451a2011-02-03 14:51:35 -0500238 mNM.notify(1, n);
239 }
240 },
241
Joe Onorato005847b2010-06-04 16:08:02 -0400242 new Test("Bad resource #1 (when=create)") {
243 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400244 Notification n = new Notification.Builder(NotificationTestList.this)
245 .setSmallIcon(R.drawable.icon2)
246 .setWhen(mActivityCreateTime)
247 .setContentTitle("Persistent #1")
248 .setContentText("This is the same notification!!")
249 .setContentIntent(makeIntent())
250 .build();
Joe Onorato005847b2010-06-04 16:08:02 -0400251 n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
252 mNM.notify(1, n);
253 }
254 },
255
256 new Test("Bad resource #1 (when=now)") {
257 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400258 Notification n = new Notification.Builder(NotificationTestList.this)
259 .setSmallIcon(R.drawable.icon2)
260 .setWhen(System.currentTimeMillis())
261 .setContentTitle("Persistent #1")
262 .setContentText("This is the same notification!!")
263 .setContentIntent(makeIntent())
264 .build();
Joe Onorato005847b2010-06-04 16:08:02 -0400265 n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
266 mNM.notify(1, n);
267 }
268 },
269
Joe Onoratoc83bb732010-01-19 16:32:22 -0800270 new Test("Times") {
271 public void run()
272 {
273 long now = System.currentTimeMillis();
274
275 timeNotification(7, "24 hours from now", now+(1000*60*60*24));
276 timeNotification(6, "12:01:00 from now", now+(1000*60*60*12)+(60*1000));
277 timeNotification(5, "12 hours from now", now+(1000*60*60*12));
278 timeNotification(4, "now", now);
279 timeNotification(3, "11:59:00 ago", now-((1000*60*60*12)-(60*1000)));
280 timeNotification(2, "12 hours ago", now-(1000*60*60*12));
281 timeNotification(1, "24 hours ago", now-(1000*60*60*24));
282 }
283 },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 new StateStress("Stress - Ongoing / Latest", 100, 100, new Runnable[] {
285 new Runnable() {
286 public void run() {
287 Log.d(TAG, "Stress - Ongoing/Latest 0");
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400288 Notification n = new Notification.Builder(NotificationTestList.this)
289 .setSmallIcon(R.drawable.icon3)
290 .setWhen(System.currentTimeMillis())
291 .setContentTitle("Stress - Ongoing")
292 .setContentText("Notify me!!!")
293 .setOngoing(true)
294 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 mNM.notify(1, n);
296 }
297 },
298 new Runnable() {
299 public void run() {
300 Log.d(TAG, "Stress - Ongoing/Latest 1");
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400301 Notification n = new Notification.Builder(NotificationTestList.this)
302 .setSmallIcon(R.drawable.icon4)
303 .setWhen(System.currentTimeMillis())
304 .setContentTitle("Stress - Latest")
305 .setContentText("Notify me!!!")
306 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 mNM.notify(1, n);
308 }
309 }
310 }),
311
312 new Test("Long") {
313 public void run()
314 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400315 Notification n = new Notification.Builder(NotificationTestList.this)
316 .setSmallIcon(R.drawable.icon1)
317 .setContentTitle(name)
318 .setDefaults(Notification.DEFAULT_SOUND)
319 .setVibrate(new long[] {
320 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
321 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
322 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 })
323 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 mNM.notify(1, n);
325 }
326 },
327
Daniel Sandler373a9982010-11-30 12:03:59 -0500328 new Test("Progress #1") {
329 public void run() {
330 final boolean PROGRESS_UPDATES_WHEN = true;
331 if (!mProgressDone) return;
332 mProgressDone = false;
333 Thread t = new Thread() {
334 public void run() {
335 int x = 0;
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400336 final Notification.Builder n = new Notification.Builder(NotificationTestList.this)
337 .setSmallIcon(R.drawable.icon1)
338 .setContentTitle(name)
339 .setOngoing(true);
340
Daniel Sandler373a9982010-11-30 12:03:59 -0500341 while (!mProgressDone) {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400342 n.setWhen(PROGRESS_UPDATES_WHEN
Daniel Sandler373a9982010-11-30 12:03:59 -0500343 ? System.currentTimeMillis()
344 : mActivityCreateTime);
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400345 n.setProgress(100, x, false);
346 n.setContentText("Progress: " + x + "%");
Daniel Sandler373a9982010-11-30 12:03:59 -0500347
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400348 mNM.notify(500, n.build());
Daniel Sandler373a9982010-11-30 12:03:59 -0500349 x = (x + 7) % 100;
350
351 try {
352 Thread.sleep(1000);
353 } catch (InterruptedException e) {
354 break;
355 }
356 }
357 }
358 };
359 t.start();
360 }
361 },
362
363 new Test("Stop Progress") {
364 public void run() {
365 mProgressDone = true;
366 mNM.cancel(500);
367 }
368 },
369
The Android Open Source Project10592532009-03-18 17:39:46 -0700370 new Test("Blue Lights") {
371 public void run()
372 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400373 Notification n = new Notification.Builder(NotificationTestList.this)
374 .setSmallIcon(R.drawable.icon2)
375 .setContentTitle(name)
376 .setLights(0xff0000ff, 1, 0)
377 .setDefaults(Notification.DEFAULT_LIGHTS)
378 .build();
The Android Open Source Project10592532009-03-18 17:39:46 -0700379 mNM.notify(1, n);
380 }
381 },
382
383 new Test("Red Lights") {
384 public void run()
385 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400386 Notification n = new Notification.Builder(NotificationTestList.this)
387 .setSmallIcon(R.drawable.icon2)
388 .setContentTitle(name)
389 .setLights(0xffff0000, 1, 0)
390 .setDefaults(Notification.DEFAULT_LIGHTS)
391 .build();
The Android Open Source Project10592532009-03-18 17:39:46 -0700392 mNM.notify(1, n);
393 }
394 },
395
396 new Test("Yellow Lights") {
397 public void run()
398 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400399 Notification n = new Notification.Builder(NotificationTestList.this)
400 .setSmallIcon(R.drawable.icon2)
401 .setContentTitle(name)
402 .setLights(0xffffff00, 1, 0)
403 .setDefaults(Notification.DEFAULT_LIGHTS)
404 .build();
Mattias Östergrenb0fbe292010-03-05 09:44:15 +0100405 mNM.notify(1, n);
406 }
407 },
408
409 new Test("Lights off") {
410 public void run()
411 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400412 Notification n = new Notification.Builder(NotificationTestList.this)
413 .setSmallIcon(R.drawable.icon2)
414 .setContentTitle(name)
415 .setLights(0x00000000, 0, 0)
416 .setDefaults(Notification.DEFAULT_LIGHTS)
417 .build();
The Android Open Source Project10592532009-03-18 17:39:46 -0700418 mNM.notify(1, n);
419 }
420 },
421
422 new Test("Blue Blinking Slow") {
423 public void run()
424 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400425 Notification n = new Notification.Builder(NotificationTestList.this)
426 .setSmallIcon(R.drawable.icon2)
427 .setContentTitle(name)
428 .setLights(0xff0000ff, 1300, 1300)
429 .setDefaults(Notification.DEFAULT_LIGHTS)
430 .build();
The Android Open Source Project10592532009-03-18 17:39:46 -0700431 mNM.notify(1, n);
432 }
433 },
434
435 new Test("Blue Blinking Fast") {
436 public void run()
437 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400438 Notification n = new Notification.Builder(NotificationTestList.this)
439 .setSmallIcon(R.drawable.icon2)
440 .setContentTitle(name)
441 .setLights(0xff0000ff, 300, 300)
442 .setDefaults(Notification.DEFAULT_LIGHTS)
443 .build();
The Android Open Source Project10592532009-03-18 17:39:46 -0700444 mNM.notify(1, n);
445 }
446 },
447
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 new Test("Default All") {
449 public void run()
450 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400451 Notification n = new Notification.Builder(NotificationTestList.this)
452 .setSmallIcon(R.drawable.icon2)
453 .setContentTitle(name)
454 .setDefaults(Notification.DEFAULT_ALL)
455 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 mNM.notify(1, n);
457 }
458 },
459
460 new Test("Default All, once") {
461 public void run()
462 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400463 Notification n = new Notification.Builder(NotificationTestList.this)
464 .setSmallIcon(R.drawable.icon2)
465 .setContentTitle(name)
466 .setOnlyAlertOnce(true)
467 .setDefaults(Notification.DEFAULT_ALL)
468 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 mNM.notify(1, n);
470 }
471 },
472
473 new Test("Resource Sound") {
474 public void run()
475 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400476 Notification n = new Notification.Builder(NotificationTestList.this)
477 .setSmallIcon(R.drawable.stat_sys_phone)
478 .setContentTitle(name)
479 .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
480 getPackageName() + "/raw/ringer"))
481 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 Log.d(TAG, "n.sound=" + n.sound);
483
484 mNM.notify(1, n);
485 }
486 },
487
488 new Test("Sound and Cancel") {
489 public void run()
490 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400491 Notification n = new Notification.Builder(NotificationTestList.this)
492 .setSmallIcon(R.drawable.stat_sys_phone)
493 .setContentTitle(name)
494 .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
495 getPackageName() + "/raw/ringer"))
496 .build();
497 Log.d(TAG, "n.sound=" + n.sound);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498
499 mNM.notify(1, n);
500 SystemClock.sleep(200);
501 mNM.cancel(1);
502 }
503 },
504
505 new Test("Vibrate") {
506 public void run()
507 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400508 Notification n = new Notification.Builder(NotificationTestList.this)
509 .setSmallIcon(R.drawable.stat_sys_phone)
510 .setContentTitle(name)
511 .setVibrate(new long[]{0, 700, 500, 1000})
512 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513
514 mNM.notify(1, n);
515 }
516 },
517
518 new Test("Vibrate and cancel") {
519 public void run()
520 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400521 Notification n = new Notification.Builder(NotificationTestList.this)
522 .setSmallIcon(R.drawable.stat_sys_phone)
523 .setContentTitle(name)
524 .setVibrate(new long[]{0, 700, 500, 1000})
525 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800526
527 mNM.notify(1, n);
528 SystemClock.sleep(500);
529 mNM.cancel(1);
530 }
531 },
532
533 new Test("Vibrate pattern") {
534 public void run()
535 {
536 mVibrator.vibrate(new long[] { 250, 1000, 500, 2000 }, -1);
537 }
538 },
539
540 new Test("Vibrate pattern repeating") {
541 public void run()
542 {
543 mVibrator.vibrate(new long[] { 250, 1000, 500 }, 1);
544 }
545 },
546
547 new Test("Vibrate 3s") {
548 public void run()
549 {
550 mVibrator.vibrate(3000);
551 }
552 },
553
554 new Test("Vibrate 100s") {
555 public void run()
556 {
557 mVibrator.vibrate(100000);
558 }
559 },
560
561 new Test("Vibrate off") {
562 public void run()
563 {
564 mVibrator.cancel();
565 }
566 },
567
568 new Test("Cancel #1") {
569 public void run() {
570 mNM.cancel(1);
571 }
572 },
573
574 new Test("Cancel #1 in 3 sec") {
575 public void run() {
576 mHandler.postDelayed(new Runnable() {
577 public void run() {
578 Log.d(TAG, "Cancelling now...");
579 mNM.cancel(1);
580 }
581 }, 3000);
582 }
583 },
584
585 new Test("Cancel #2") {
586 public void run() {
587 mNM.cancel(2);
588 }
589 },
590
591 new Test("Persistent #1") {
592 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400593 Notification n = new Notification.Builder(NotificationTestList.this)
594 .setSmallIcon(R.drawable.icon1)
595 .setWhen(mActivityCreateTime)
596 .setContentTitle(name)
597 .setContentText("This is a notification!!!")
598 .setContentIntent(makeIntent())
599 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 mNM.notify(1, n);
601 }
602 },
603
604 new Test("Persistent #1 in 3 sec") {
605 public void run() {
606 mHandler.postDelayed(new Runnable() {
607 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400608 String message = " "
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 + "tick tock tick tock\n\nSometimes notifications can "
610 + "be really long and wrap to more than one line.\n"
611 + "Sometimes."
612 + "Ohandwhathappensifwehaveonereallylongstringarewesure"
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400613 + "thatwesegmentitcorrectly?\n";
614 Notification n = new Notification.Builder(NotificationTestList.this)
615 .setSmallIcon(R.drawable.icon1)
616 .setContentTitle(name)
617 .setContentText("This is still a notification!!!")
618 .setContentIntent(makeIntent())
619 .setStyle(new Notification.BigTextStyle().bigText(message))
620 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800621 mNM.notify(1, n);
622 }
623 }, 3000);
624 }
625 },
626
627 new Test("Persistent #2") {
628 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400629 Notification n = new Notification.Builder(NotificationTestList.this)
630 .setSmallIcon(R.drawable.icon1)
631 .setWhen(mActivityCreateTime)
632 .setContentTitle(name)
633 .setContentText("This is a notification!!!")
634 .setContentIntent(makeIntent())
635 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 mNM.notify(2, n);
637 }
638 },
639
Joe Onorato68065e02010-02-03 20:21:41 -0800640 new Test("Persistent #3") {
641 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400642 Notification n = new Notification.Builder(NotificationTestList.this)
643 .setSmallIcon(R.drawable.icon1)
644 .setWhen(mActivityCreateTime)
645 .setContentTitle(name)
646 .setContentText("This is a notification!!!")
647 .setContentIntent(makeIntent())
648 .build();
Joe Onorato68065e02010-02-03 20:21:41 -0800649 mNM.notify(3, n);
650 }
651 },
652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 new Test("Persistent #2 Vibrate") {
654 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400655 Notification n = new Notification.Builder(NotificationTestList.this)
656 .setSmallIcon(R.drawable.icon1)
657 .setWhen(mActivityCreateTime)
658 .setContentTitle(name)
659 .setContentText("This is a notification!!!")
660 .setContentIntent(makeIntent())
661 .setDefaults(Notification.DEFAULT_VIBRATE)
662 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800663 mNM.notify(2, n);
664 }
665 },
666
Joe Onoratod2b1f002010-06-04 10:42:41 -0700667 new Test("Persistent #1 - different icon") {
668 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400669 Notification n = new Notification.Builder(NotificationTestList.this)
670 .setSmallIcon(R.drawable.icon2)
671 .setWhen(mActivityCreateTime)
672 .setContentTitle(name)
673 .setContentText("This is a notification!!!")
674 .setContentIntent(makeIntent())
675 .build();
Joe Onoratod2b1f002010-06-04 10:42:41 -0700676 mNM.notify(1, n);
677 }
678 },
679
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800680 new Test("Chronometer Start") {
681 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400682 Notification n = new Notification.Builder(NotificationTestList.this)
683 .setSmallIcon(R.drawable.icon1)
684 .setWhen(System.currentTimeMillis())
685 .setContentTitle(name)
686 .setContentIntent(makeIntent())
687 .setOngoing(true)
688 .setUsesChronometer(true)
689 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800690 mNM.notify(2, n);
691 }
692 },
693
694 new Test("Chronometer Stop") {
695 public void run() {
696 mHandler.postDelayed(new Runnable() {
697 public void run() {
698 Log.d(TAG, "Chronometer Stop");
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400699 Notification n = new Notification.Builder(NotificationTestList.this)
700 .setSmallIcon(R.drawable.icon1)
701 .setWhen(System.currentTimeMillis())
702 .setContentTitle(name)
703 .setContentIntent(makeIntent())
704 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 mNM.notify(2, n);
706 }
707 }, 3000);
708 }
709 },
710
711 new Test("Sequential Persistent") {
712 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400713 mNM.notify(1, notificationWithNumbers(name, 1));
714 mNM.notify(2, notificationWithNumbers(name, 2));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 }
716 },
717
718 new Test("Replace Persistent") {
719 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400720 mNM.notify(1, notificationWithNumbers(name, 1));
721 mNM.notify(1, notificationWithNumbers(name, 1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
723 },
724
725 new Test("Run and Cancel (n=1)") {
726 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400727 mNM.notify(1, notificationWithNumbers(name, 1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 mNM.cancel(1);
729 }
730 },
731
732 new Test("Run an Cancel (n=2)") {
733 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400734 mNM.notify(1, notificationWithNumbers(name, 1));
735 mNM.notify(2, notificationWithNumbers(name, 2));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 mNM.cancel(2);
737 }
738 },
739
740 // Repeatedly notify and cancel -- triggers bug #670627
741 new Test("Bug 670627") {
742 public void run() {
743 for (int i = 0; i < 10; i++) {
744 Log.d(TAG, "Add two notifications");
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400745 mNM.notify(1, notificationWithNumbers(name, 1));
746 mNM.notify(2, notificationWithNumbers(name, 2));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 Log.d(TAG, "Cancel two notifications");
748 mNM.cancel(1);
749 mNM.cancel(2);
750 }
751 }
752 },
753
754 new Test("Ten Notifications") {
755 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400756 for (int i = 0; i < 10; i++) {
757 Notification n = new Notification.Builder(NotificationTestList.this)
758 .setSmallIcon(kNumberedIconResIDs[i])
759 .setContentTitle("Persistent #" + i)
760 .setContentText("Notify me!!!" + i)
761 .setOngoing(i < 2)
762 .setNumber(i)
763 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 mNM.notify((i+1)*10, n);
765 }
766 }
767 },
768
769 new Test("Cancel eight notifications") {
770 public void run() {
771 for (int i = 1; i < 9; i++) {
772 mNM.cancel((i+1)*10);
773 }
774 }
775 },
776
Daniel Sandlerb0cc50d2010-10-26 16:55:56 -0400777 new Test("Cancel the other two notifications") {
778 public void run() {
779 mNM.cancel(10);
780 mNM.cancel(100);
781 }
782 },
783
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 new Test("Persistent with numbers 1") {
785 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400786 mNM.notify(1, notificationWithNumbers(name, 1));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 }
788 },
789
Joe Onorato6c01a112010-10-04 17:38:47 -0400790 new Test("Persistent with numbers 22") {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400792 mNM.notify(1, notificationWithNumbers(name, 22));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800793 }
794 },
795
796 new Test("Persistent with numbers 333") {
797 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400798 mNM.notify(1, notificationWithNumbers(name, 333));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 }
800 },
801
802 new Test("Persistent with numbers 4444") {
803 public void run() {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400804 mNM.notify(1, notificationWithNumbers(name, 4444));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 }
806 },
807
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500808 new Test("PRIORITY_HIGH") {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500809 public void run() {
810 Notification n = new Notification.Builder(NotificationTestList.this)
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500811 .setSmallIcon(R.drawable.notification5)
812 .setContentTitle("High priority")
Daniel Sandlere40451a2011-02-03 14:51:35 -0500813 .setContentText("This should appear before all others")
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500814 .setPriority(Notification.PRIORITY_HIGH)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400815 .build();
Daniel Sandlere40451a2011-02-03 14:51:35 -0500816
817 int[] idOut = new int[1];
818 try {
819 INotificationManager directLine = mNM.getService();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500820 directLine.enqueueNotificationWithTag(
821 getPackageName(),
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800822 getPackageName(),
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500823 null,
824 100,
825 n,
Dianne Hackborn41203752012-08-31 14:05:51 -0700826 idOut,
827 UserHandle.myUserId());
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500828 } catch (android.os.RemoteException ex) {
829 // oh well
830 }
831 }
832 },
833
834 new Test("PRIORITY_MAX") {
835 public void run() {
836 Notification n = new Notification.Builder(NotificationTestList.this)
837 .setSmallIcon(R.drawable.notification9)
838 .setContentTitle("MAX priority")
839 .setContentText("This might appear as an intruder alert")
840 .setPriority(Notification.PRIORITY_MAX)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400841 .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500842
843 int[] idOut = new int[1];
844 try {
845 INotificationManager directLine = mNM.getService();
846 directLine.enqueueNotificationWithTag(
847 getPackageName(),
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800848 getPackageName(),
849 null,
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500850 200,
851 n,
Dianne Hackborn41203752012-08-31 14:05:51 -0700852 idOut,
853 UserHandle.myUserId());
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500854 } catch (android.os.RemoteException ex) {
855 // oh well
856 }
857 }
858 },
859
860 new Test("PRIORITY_MIN") {
861 public void run() {
862 Notification n = new Notification.Builder(NotificationTestList.this)
863 .setSmallIcon(R.drawable.notification0)
864 .setContentTitle("MIN priority")
865 .setContentText("You should not see this")
866 .setPriority(Notification.PRIORITY_MIN)
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400867 .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500868
869 int[] idOut = new int[1];
870 try {
871 INotificationManager directLine = mNM.getService();
872 directLine.enqueueNotificationWithTag(
Daniel Sandlere40451a2011-02-03 14:51:35 -0500873 getPackageName(),
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800874 getPackageName(),
875 null,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500876 1,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500877 n,
Dianne Hackborn41203752012-08-31 14:05:51 -0700878 idOut,
879 UserHandle.myUserId());
Daniel Sandlere40451a2011-02-03 14:51:35 -0500880 } catch (android.os.RemoteException ex) {
881 // oh well
882 }
883 }
884 },
885
The Android Open Source Project10592532009-03-18 17:39:46 -0700886 new Test("Crash") {
887 public void run()
888 {
889 PowerManager.WakeLock wl
Christian Mehlmaueref367522010-05-31 23:08:30 +0200890 = ((PowerManager)NotificationTestList.this.getSystemService(Context.POWER_SERVICE))
The Android Open Source Project10592532009-03-18 17:39:46 -0700891 .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "crasher");
892 wl.acquire();
893 mHandler.postDelayed(new Runnable() {
894 public void run() {
895 throw new RuntimeException("Die!");
896 }
897 }, 10000);
898
899 }
900 },
901
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800902 };
903
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400904 private Notification notificationWithNumbers(String name, int num) {
905 Notification n = new Notification.Builder(NotificationTestList.this)
906 .setSmallIcon((num >= 0 && num < kNumberedIconResIDs.length)
907 ? kNumberedIconResIDs[num]
908 : kUnnumberedIconResID)
909 .setContentTitle(name)
910 .setContentText("Number=" + num)
911 .setNumber(num)
912 .build();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800913 return n;
914 }
915
916 private PendingIntent makeIntent() {
917 Intent intent = new Intent(Intent.ACTION_MAIN);
Joe Onorato0e26dff2010-05-24 16:17:02 -0400918 intent.addCategory(Intent.CATEGORY_HOME);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 return PendingIntent.getActivity(this, 0, intent, 0);
920 }
921
Joe Onorato184498c2010-10-08 17:57:18 -0400922 private PendingIntent makeIntent2() {
923 Intent intent = new Intent(this, StatusBarTest.class);
924 return PendingIntent.getActivity(this, 0, intent, 0);
925 }
926
927
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 class StateStress extends Test {
929 StateStress(String name, int pause, int iterations, Runnable[] tasks) {
930 super(name);
931 mPause = pause;
932 mTasks = tasks;
933 mIteration = iterations;
934 }
935 Runnable[] mTasks;
936 int mNext;
937 int mIteration;
938 long mPause;
939 Runnable mRunnable = new Runnable() {
940 public void run() {
941 mTasks[mNext].run();
942 mNext++;
943 if (mNext >= mTasks.length) {
944 mNext = 0;
945 mIteration--;
946 if (mIteration <= 0) {
947 return;
948 }
949 }
950 mHandler.postDelayed(mRunnable, mPause);
951 }
952 };
953 public void run() {
954 mNext = 0;
955 mHandler.postDelayed(mRunnable, mPause);
956 }
957 }
Joe Onoratoc83bb732010-01-19 16:32:22 -0800958
959 void timeNotification(int n, String label, long time) {
Chris Wren1ce4b6d2015-06-11 10:19:43 -0400960 mNM.notify(n, new Notification.Builder(NotificationTestList.this)
961 .setSmallIcon(R.drawable.ic_statusbar_missedcall)
962 .setWhen(time)
963 .setContentTitle(label)
964 .setContentText(new java.util.Date(time).toString())
965 .build());
Joe Onoratoc83bb732010-01-19 16:32:22 -0800966
967 }
Joe Onoratoef1e7762010-09-17 18:38:38 -0400968
969 Bitmap loadBitmap(int resId) {
970 BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(resId);
971 return Bitmap.createBitmap(bd.getBitmap());
972 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973}
974