1 /*
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 
17 package com.android.statusbartest;
18 
19 import static android.app.NotificationManager.IMPORTANCE_DEFAULT;
20 import static android.app.NotificationManager.IMPORTANCE_HIGH;
21 import static android.app.NotificationManager.IMPORTANCE_LOW;
22 import static android.app.NotificationManager.IMPORTANCE_MIN;
23 
24 import android.app.Notification;
25 import android.app.NotificationChannel;
26 import android.app.NotificationManager;
27 import android.app.PendingIntent;
28 import android.content.ContentResolver;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.graphics.Bitmap;
32 import android.graphics.Color;
33 import android.graphics.drawable.BitmapDrawable;
34 import android.graphics.drawable.Icon;
35 import android.media.AudioAttributes;
36 import android.net.Uri;
37 import android.os.Bundle;
38 import android.os.Handler;
39 import android.os.PowerManager;
40 import android.os.SystemClock;
41 import android.os.Vibrator;
42 import android.util.Log;
43 import android.widget.RemoteViews;
44 import android.widget.Toast;
45 
46 public class NotificationTestList extends TestActivity
47 {
48     private final static String TAG = "NotificationTestList";
49 
50     NotificationManager mNM;
51     Vibrator mVibrator;
52     Handler mHandler = new Handler();
53 
54     long mActivityCreateTime;
55     long mChronometerBase = 0;
56 
57     boolean mProgressDone = true;
58 
59     final int[] kNumberedIconResIDs = {
60             R.drawable.notification0,
61             R.drawable.notification1,
62             R.drawable.notification2,
63             R.drawable.notification3,
64             R.drawable.notification4,
65             R.drawable.notification5,
66             R.drawable.notification6,
67             R.drawable.notification7,
68             R.drawable.notification8,
69             R.drawable.notification9
70     };
71     final int kUnnumberedIconResID = R.drawable.notificationx;
72 
73     @Override
onCreate(Bundle icicle)74     public void onCreate(Bundle icicle) {
75         super.onCreate(icicle);
76         mVibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
77         mActivityCreateTime = System.currentTimeMillis();
78         mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
79 
80         mNM.createNotificationChannel(new NotificationChannel("min", "Min", IMPORTANCE_MIN));
81         mNM.createNotificationChannel(new NotificationChannel("low", "Low", IMPORTANCE_LOW));
82         mNM.createNotificationChannel(
83                 new NotificationChannel("default", "Default", IMPORTANCE_DEFAULT));
84         mNM.createNotificationChannel(new NotificationChannel("high", "High", IMPORTANCE_HIGH));
85     }
86 
87     @Override
tag()88     protected String tag() {
89         return TAG;
90     }
91 
92     @Override
tests()93     protected Test[] tests() {
94         return mTests;
95     }
96 
97     private Test[] mTests = new Test[] {
98             new Test("cancel all") {
99                 public void run() {
100                     mNM.cancelAll();
101                 }
102             },
103             new Test("Phone call") {
104                 public void run()
105                 {
106                     NotificationChannel phoneCall =
107                             new NotificationChannel("phone call", "Phone Call", IMPORTANCE_HIGH);
108                     phoneCall.setVibrationPattern(new long[] {
109                             300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
110                             300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
111                             300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 });
112                     phoneCall.enableVibration(true);
113                     phoneCall.setLightColor(0xff0000ff);
114                     phoneCall.enableLights(true);
115                     phoneCall.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
116                                     getPackageName() + "/raw/ringer"),
117                             new AudioAttributes.Builder().setUsage(
118                                     AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build());
119                     Notification n = new Notification.Builder(NotificationTestList.this,
120                             "phone call")
121                             .setSmallIcon(R.drawable.icon2)
122                             .setFullScreenIntent(makeIntent2(), true)
123                             .build();
124                     mNM.notify(7001, n);
125                 }
126             },
127             new Test("with zen") {
128                 public void run()
129                 {
130                     mNM.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALARMS);
131                     Notification n = new Notification.Builder(NotificationTestList.this,
132                             "default")
133                             .setSmallIcon(R.drawable.icon2)
134                             .setContentTitle("Default priority")
135                             .build();
136                     mNM.notify("default", 7004, n);
137                     try {
138                         Thread.sleep(8000);
139                     } catch (InterruptedException e) {
140                         e.printStackTrace();
141                     }
142                     mNM.setInterruptionFilter(NotificationManager.INTERRUPTION_FILTER_ALL);
143                 }
144             },
145             new Test("repeated") {
146                 public void run()
147                 {
148                     for (int i = 0; i < 50; i++) {
149                         Notification n = new Notification.Builder(NotificationTestList.this,
150                                 "default")
151                                 .setSmallIcon(R.drawable.icon2)
152                                 .setContentTitle("Default priority")
153                                 .build();
154                         mNM.notify("default", 7004, n);
155                         try {
156                             Thread.sleep(100);
157                         } catch (InterruptedException e) {
158                             e.printStackTrace();
159                         }
160                     }
161                 }
162             },
163             new Test("Post a group") {
164                 public void run()
165                 {
166                     Notification n = new Notification.Builder(NotificationTestList.this, "min")
167                             .setSmallIcon(R.drawable.icon2)
168                             .setContentTitle("Min priority group 1")
169                             .setGroup("group1")
170                             .build();
171                     mNM.notify(6000, n);
172                     n = new Notification.Builder(NotificationTestList.this, "low")
173                             .setSmallIcon(R.drawable.icon2)
174                             .setContentTitle("low priority group 1")
175                             .setGroup("group1")
176                             .build();
177                     mNM.notify(6001, n);
178                     n = new Notification.Builder(NotificationTestList.this, "default")
179                             .setSmallIcon(R.drawable.icon2)
180                             .setContentTitle("default priority group 1")
181                             .setGroup("group1")
182                             .setOngoing(true)
183                             .setColor(Color.WHITE)
184                             .setColorized(true)
185                             .build();
186                     mNM.notify(6002, n);
187                     n = new Notification.Builder(NotificationTestList.this, "low")
188                             .setSmallIcon(R.drawable.icon2)
189                             .setContentTitle("summary group 1")
190                             .setGroup("group1")
191                             .setGroupSummary(true)
192                             .build();
193                     mNM.notify(6003, n);
194                 }
195             },
196             new Test("Post a group (2) w/o summary") {
197                 public void run()
198                 {
199                     Notification n = new Notification.Builder(NotificationTestList.this, "min")
200                             .setSmallIcon(R.drawable.icon2)
201                             .setContentTitle("Min priority group 2")
202                             .setGroup("group2")
203                             .build();
204                     mNM.notify(6100, n);
205                     n = new Notification.Builder(NotificationTestList.this, "low")
206                             .setSmallIcon(R.drawable.icon2)
207                             .setContentTitle("low priority group 2")
208                             .setGroup("group2")
209                             .build();
210                     mNM.notify(6101, n);
211                     n = new Notification.Builder(NotificationTestList.this, "default")
212                             .setSmallIcon(R.drawable.icon2)
213                             .setContentTitle("default priority group 2")
214                             .setGroup("group2")
215                             .build();
216                     mNM.notify(6102, n);
217                 }
218             },
219             new Test("Summary for group 2") {
220                 public void run()
221                 {
222                     Notification n = new Notification.Builder(NotificationTestList.this, "min")
223                             .setSmallIcon(R.drawable.icon2)
224                             .setContentTitle("summary group 2")
225                             .setGroup("group2")
226                             .setGroupSummary(true)
227                             .build();
228                     mNM.notify(6103, n);
229                 }
230             },
231             new Test("Group up public-secret") {
232                 public void run()
233                 {
234                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
235                             .setSmallIcon(R.drawable.icon2)
236                             .setContentTitle("public notification")
237                             .setVisibility(Notification.VISIBILITY_PUBLIC)
238                             .setGroup("public-secret")
239                             .build();
240                     mNM.notify("public", 7009, n);
241                     n = new Notification.Builder(NotificationTestList.this, "default")
242                             .setSmallIcon(R.drawable.icon2)
243                             .setContentTitle("private only notification")
244                             .setVisibility(Notification.VISIBILITY_PRIVATE)
245                             .setGroup("public-secret")
246                             .build();
247                     mNM.notify("no public", 7010, n);
248                     n = new Notification.Builder(NotificationTestList.this, "default")
249                             .setSmallIcon(R.drawable.icon2)
250                             .setContentTitle("private version of notification")
251                             .setVisibility(Notification.VISIBILITY_PRIVATE)
252                             .setGroup("public-secret")
253                             .setPublicVersion(new Notification.Builder(
254                                     NotificationTestList.this, "default")
255                                     .setSmallIcon(R.drawable.icon2)
256                                     .setContentTitle("public notification of private notification")
257                                     .setVisibility(Notification.VISIBILITY_PUBLIC)
258                                     .build())
259                             .build();
260                     mNM.notify("priv with pub", 7011, n);
261                     n = new Notification.Builder(NotificationTestList.this, "default")
262                             .setSmallIcon(R.drawable.icon2)
263                             .setContentTitle("secret notification")
264                             .setVisibility(Notification.VISIBILITY_SECRET)
265                             .setGroup("public-secret")
266                             .build();
267                     mNM.notify("secret", 7012, n);
268 
269                     Notification s = new Notification.Builder(NotificationTestList.this, "default")
270                             .setSmallIcon(R.drawable.icon2)
271                             .setContentTitle("summary group public-secret")
272                             .setGroup("public-secret")
273                             .setGroupSummary(true)
274                             .build();
275                     mNM.notify(7113, s);
276                 }
277             },
278             new Test("Cancel priority autogroup") {
279                 public void run()
280                 {
281                     try {
282                         mNM.cancel(Integer.MAX_VALUE);
283                     } catch (Exception e) {
284                         Toast.makeText(NotificationTestList.this, "cancel failed (yay)",
285                                 Toast.LENGTH_LONG).show();
286                     }
287                 }
288             },
289             new Test("Min priority") {
290                 public void run()
291                 {
292                     Notification n = new Notification.Builder(NotificationTestList.this, "min")
293                             .setSmallIcon(R.drawable.icon2)
294                             .setContentTitle("Min priority")
295                             .setTicker("Min priority")
296                             .build();
297                     mNM.notify("min", 7000, n);
298                 }
299             },
300             new Test("Low priority") {
301                 public void run()
302                 {
303                     Notification n = new Notification.Builder(NotificationTestList.this, "low")
304                             .setSmallIcon(R.drawable.icon2)
305                             .setContentTitle("Low priority")
306                             .setTicker("Low priority")
307                             .build();
308                     mNM.notify("low", 7002, n);
309                 }
310             },
311             new Test("Default priority") {
312                 public void run()
313                 {
314                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
315                             .setSmallIcon(R.drawable.icon2)
316                             .setContentTitle("Default priority")
317                             .build();
318                     mNM.notify("default", 7004, n);
319                 }
320             },
321             new Test("High priority") {
322                 public void run()
323                 {
324                     Notification n = new Notification.Builder(NotificationTestList.this, "high")
325                             .setSmallIcon(R.drawable.icon2)
326                             .setContentTitle("High priority")
327                             .setTicker("High priority")
328                             .build();
329                     mNM.notify("high", 7006, n);
330                 }
331             },
332             new Test("high priority with delay") {
333                 public void run()
334                 {
335                     try {
336                         Thread.sleep(5000);
337                     } catch (InterruptedException e) {
338                     }
339                     Notification n = new Notification.Builder(NotificationTestList.this, "high")
340                             .setSmallIcon(R.drawable.icon2)
341                             .setContentTitle("High priority")
342                             .setFullScreenIntent(makeIntent2(), false)
343                             .build();
344                     mNM.notify(7008, n);
345                 }
346             },
347             new Test("public notification") {
348                 public void run()
349                 {
350                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
351                             .setSmallIcon(R.drawable.icon2)
352                             .setContentTitle("public notification")
353                             .setVisibility(Notification.VISIBILITY_PUBLIC)
354                             .build();
355                     mNM.notify("public", 7009, n);
356                 }
357             },
358             new Test("private notification, no public") {
359                 public void run()
360                 {
361                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
362                             .setSmallIcon(R.drawable.icon2)
363                             .setContentTitle("private only notification")
364                             .setVisibility(Notification.VISIBILITY_PRIVATE)
365                             .build();
366                     mNM.notify("no public", 7010, n);
367                 }
368             },
369             new Test("private notification, has public") {
370                 public void run()
371                 {
372                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
373                             .setSmallIcon(R.drawable.icon2)
374                             .setContentTitle("private version of notification")
375                             .setVisibility(Notification.VISIBILITY_PRIVATE)
376                             .setPublicVersion(new Notification.Builder(
377                                     NotificationTestList.this, "low")
378                                     .setSmallIcon(R.drawable.icon2)
379                                     .setContentTitle("public notification of private notification")
380                                     .setVisibility(Notification.VISIBILITY_PUBLIC)
381                                     .build())
382                             .build();
383                     mNM.notify("priv with pub", 7011, n);
384                 }
385             },
386             new Test("secret notification") {
387                 public void run()
388                 {
389                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
390                             .setSmallIcon(R.drawable.icon2)
391                             .setContentTitle("secret notification")
392                             .setVisibility(Notification.VISIBILITY_SECRET)
393                             .build();
394                     mNM.notify("secret", 7012, n);
395                 }
396             },
397             new Test("1 minute timeout") {
398                 public void run()
399                 {
400                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
401                             .setSmallIcon(R.drawable.icon2)
402                             .setContentTitle("timeout in a minute")
403                             .setTimeoutAfter(System.currentTimeMillis() + (1000 * 60))
404                             .build();
405                     mNM.notify("timeout_min", 7013, n);
406                 }
407             },
408             new Test("Colorized") {
409                 public void run()
410                 {
411                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
412                             .setSmallIcon(R.drawable.icon2)
413                             .setContentTitle("RED IS BEST")
414                             .setContentText("or is blue?")
415                             .setTimeoutAfter(System.currentTimeMillis() + (1000 * 60))
416                             .setColor(Color.RED)
417                             .setFlag(Notification.FLAG_ONGOING_EVENT, true)
418                             .setColorized(true)
419                             .build();
420                     mNM.notify("timeout_min", 7013, n);
421                 }
422             },
423             new Test("Too many cancels") {
424                 public void run()
425                 {
426                     mNM.cancelAll();
427                     try {
428                         Thread.sleep(1000);
429                     } catch (InterruptedException e) {
430                         e.printStackTrace();
431                     }
432                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
433                             .setSmallIcon(R.drawable.icon2)
434                             .setContentTitle("Cancel then post")
435                             .setContentText("instead of just updating the existing notification")
436                             .build();
437                     mNM.notify("cancel_madness", 7014, n);
438                 }
439             },
440             new Test("Off") {
441                 public void run() {
442                     PowerManager pm = (PowerManager) NotificationTestList.this.getSystemService(
443                             Context.POWER_SERVICE);
444                     PowerManager.WakeLock wl =
445                             pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sound");
446                     wl.acquire();
447 
448                     pm.goToSleep(SystemClock.uptimeMillis());
449 
450                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
451                             .setSmallIcon(R.drawable.stat_sys_phone)
452                             .setContentTitle(name)
453                             .build();
454                     Log.d(TAG, "n.sound=" + n.sound);
455 
456                     mNM.notify(1, n);
457 
458                     Log.d(TAG, "releasing wake lock");
459                     wl.release();
460                     Log.d(TAG, "released wake lock");
461                 }
462             },
463 
464             new Test("Cancel #1") {
465                 public void run()
466                 {
467                     mNM.cancel(1);
468                 }
469             },
470 
471             new Test("Custom Button") {
472                 public void run() {
473                     RemoteViews view = new RemoteViews(getPackageName(), R.layout.button_notification);
474                     view.setOnClickPendingIntent(R.id.button, makeIntent2());
475                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
476                             .setSmallIcon(R.drawable.icon1)
477                             .setWhen(mActivityCreateTime)
478                             .setContentTitle(name)
479                             .setOngoing(true)
480                             .setCustomContentView(view)
481                             .build();
482 
483                     mNM.notify(1, n);
484                 }
485             },
486 
487             new Test("Action Button") {
488                 public void run() {
489                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
490                             .setSmallIcon(R.drawable.icon1)
491                             .setWhen(mActivityCreateTime)
492                             .setContentTitle(name)
493                             .setOngoing(true)
494                             .addAction(new Notification.Action.Builder(
495                                     Icon.createWithResource(NotificationTestList.this,
496                                             R.drawable.ic_statusbar_chat),
497                                     "Button", makeIntent2())
498                                     .build())
499                             .build();
500 
501                     mNM.notify(1, n);
502                 }
503             },
504 
505             new Test("with intent") {
506                 public void run() {
507                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
508                             .setSmallIcon(R.drawable.icon1)
509                             .setWhen(mActivityCreateTime)
510                             .setContentTitle("Persistent #1")
511                             .setContentText("This is a notification!!!")
512                             .setContentIntent(makeIntent2())
513                             .setOngoing(true)
514                             .build();
515 
516                     mNM.notify(1, n);
517                 }
518             },
519 
520             new Test("Is blocked?") {
521                 public void run() {
522                     Toast.makeText(NotificationTestList.this,
523                             "package enabled? " + mNM.areNotificationsEnabled(),
524                             Toast.LENGTH_LONG).show();
525                 }
526             },
527 
528             new Test("importance?") {
529                 public void run() {
530                     Toast.makeText(NotificationTestList.this,
531                             "importance? " + mNM.getImportance(),
532                             Toast.LENGTH_LONG).show();
533                 }
534             },
535 
536             new Test("Whens") {
537                 public void run()
538                 {
539                     Notification.Builder n = new Notification.Builder(
540                             NotificationTestList.this, "default")
541                             .setSmallIcon(R.drawable.icon1)
542                             .setContentTitle(name)
543                             .setOngoing(true);
544 
545                     mNM.notify(1, n.setContentTitle("(453) 123-2328")
546                             .setWhen(System.currentTimeMillis()-(1000*60*60*24))
547                             .build());
548 
549                     mNM.notify(1, n.setContentTitle("Mark Willem, Me (2)")
550                             .setWhen(System.currentTimeMillis())
551                             .build());
552 
553                     mNM.notify(1, n.setContentTitle("Sophia Winterlanden")
554                             .setWhen(System.currentTimeMillis() + (1000 * 60 * 60 * 24))
555                             .build());
556                 }
557             },
558 
559             new Test("Bad Icon #1 (when=create)") {
560                 public void run() {
561                     Notification n = new Notification.Builder(NotificationTestList.this, "low")
562                             .setSmallIcon(R.layout.chrono_notification /* not an icon */)
563                             .setWhen(mActivityCreateTime)
564                             .setContentTitle("Persistent #1")
565                             .setContentText("This is the same notification!!")
566                             .setContentIntent(makeIntent())
567                             .build();
568                     mNM.notify(1, n);
569                 }
570             },
571 
572             new Test("Bad Icon #1 (when=now)") {
573                 public void run() {
574                     Notification n = new Notification.Builder(NotificationTestList.this, "low")
575                             .setSmallIcon(R.layout.chrono_notification /* not an icon */)
576                             .setWhen(System.currentTimeMillis())
577                             .setContentTitle("Persistent #1")
578                             .setContentText("This is the same notification!!")
579                             .setContentIntent(makeIntent())
580                             .build();
581                     mNM.notify(1, n);
582                 }
583             },
584 
585             new Test("Null Icon #1 (when=now)") {
586                 public void run() {
587                     Notification n = new Notification.Builder(NotificationTestList.this, "low")
588                             .setSmallIcon(0)
589                             .setWhen(System.currentTimeMillis())
590                             .setContentTitle("Persistent #1")
591                             .setContentText("This is the same notification!!")
592                             .setContentIntent(makeIntent())
593                             .build();
594                     mNM.notify(1, n);
595                 }
596             },
597 
598             new Test("Bad resource #1 (when=create)") {
599                 public void run() {
600                     Notification n = new Notification.Builder(NotificationTestList.this, "low")
601                             .setSmallIcon(R.drawable.icon2)
602                             .setWhen(mActivityCreateTime)
603                             .setContentTitle("Persistent #1")
604                             .setContentText("This is the same notification!!")
605                             .setContentIntent(makeIntent())
606                             .build();
607                     n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
608                     mNM.notify(1, n);
609                 }
610             },
611 
612             new Test("Bad resource #1 (when=now)") {
613                 public void run() {
614                     Notification n = new Notification.Builder(NotificationTestList.this, "low")
615                             .setSmallIcon(R.drawable.icon2)
616                             .setWhen(System.currentTimeMillis())
617                             .setContentTitle("Persistent #1")
618                             .setContentText("This is the same notification!!")
619                             .setContentIntent(makeIntent())
620                             .build();
621                     n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
622                     mNM.notify(1, n);
623                 }
624             },
625 
626             new Test("Times") {
627                 public void run()
628                 {
629                     long now = System.currentTimeMillis();
630 
631                     timeNotification(7, "24 hours from now", now+(1000*60*60*24));
632                     timeNotification(6, "12:01:00 from now", now+(1000*60*60*12)+(60*1000));
633                     timeNotification(5, "12 hours from now", now+(1000*60*60*12));
634                     timeNotification(4, "now", now);
635                     timeNotification(3, "11:59:00 ago", now-((1000*60*60*12)-(60*1000)));
636                     timeNotification(2, "12 hours ago", now-(1000*60*60*12));
637                     timeNotification(1, "24 hours ago", now-(1000*60*60*24));
638                 }
639             },
640             new StateStress("Stress - Ongoing / Latest", 100, 100, new Runnable[] {
641                     new Runnable() {
642                         public void run() {
643                             Log.d(TAG, "Stress - Ongoing/Latest 0");
644                             Notification n = new Notification.Builder(NotificationTestList.this, "low")
645                                     .setSmallIcon(R.drawable.icon3)
646                                     .setWhen(System.currentTimeMillis())
647                                     .setContentTitle("Stress - Ongoing")
648                                     .setContentText("Notify me!!!")
649                                     .setOngoing(true)
650                                     .build();
651                             mNM.notify(1, n);
652                         }
653                     },
654                     new Runnable() {
655                         public void run() {
656                             Log.d(TAG, "Stress - Ongoing/Latest 1");
657                             Notification n = new Notification.Builder(NotificationTestList.this, "low")
658                                     .setSmallIcon(R.drawable.icon4)
659                                     .setWhen(System.currentTimeMillis())
660                                     .setContentTitle("Stress - Latest")
661                                     .setContentText("Notify me!!!")
662                                     .build();
663                             mNM.notify(1, n);
664                         }
665                     }
666             }),
667 
668             new Test("Long") {
669                 public void run()
670                 {
671                     NotificationChannel channel = new NotificationChannel("v. noisy",
672                             "channel for sound and a custom vibration", IMPORTANCE_DEFAULT);
673                     channel.enableVibration(true);
674                     channel.setVibrationPattern(new long[] {
675                             300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
676                             300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
677                             300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 });
678                     mNM.createNotificationChannel(channel);
679 
680                     Notification n = new Notification.Builder(NotificationTestList.this, "v. noisy")
681                             .setSmallIcon(R.drawable.icon1)
682                             .setContentTitle(name)
683                             .build();
684                     mNM.notify(1, n);
685                 }
686             },
687 
688             new Test("Progress #1") {
689                 public void run() {
690                     final boolean PROGRESS_UPDATES_WHEN = true;
691                     if (!mProgressDone) return;
692                     mProgressDone = false;
693                     Thread t = new Thread() {
694                         public void run() {
695                             int x = 0;
696                             final Notification.Builder n = new Notification.Builder(
697                                     NotificationTestList.this, "low")
698                                     .setSmallIcon(R.drawable.icon1)
699                                     .setContentTitle(name)
700                                     .setOngoing(true);
701 
702                             while (!mProgressDone) {
703                                 n.setWhen(PROGRESS_UPDATES_WHEN
704                                         ? System.currentTimeMillis()
705                                         : mActivityCreateTime);
706                                 n.setProgress(100, x, false);
707                                 n.setContentText("Progress: " + x + "%");
708 
709                                 mNM.notify(500, n.build());
710                                 x = (x + 7) % 100;
711 
712                                 try {
713                                     Thread.sleep(1000);
714                                 } catch (InterruptedException e) {
715                                     break;
716                                 }
717                             }
718                         }
719                     };
720                     t.start();
721                 }
722             },
723 
724             new Test("Stop Progress") {
725                 public void run() {
726                     mProgressDone = true;
727                     mNM.cancel(500);
728                 }
729             },
730 
731             new Test("Blue Lights") {
732                 public void run()
733                 {
734                     NotificationChannel channel = new NotificationChannel("blue",
735                             "blue", IMPORTANCE_DEFAULT);
736                     channel.enableLights(true);
737                     channel.setLightColor(0xff0000ff);
738                     mNM.createNotificationChannel(channel);
739 
740                     Notification n = new Notification.Builder(NotificationTestList.this, "blue")
741                             .setSmallIcon(R.drawable.icon2)
742                             .setContentTitle(name)
743                             .build();
744                     mNM.notify(1, n);
745                 }
746             },
747 
748             new Test("Red Lights") {
749                 public void run()
750                 {
751                     NotificationChannel channel = new NotificationChannel("red",
752                             "red", IMPORTANCE_DEFAULT);
753                     channel.enableLights(true);
754                     channel.setLightColor(0xffff0000);
755                     mNM.createNotificationChannel(channel);
756 
757                     Notification n = new Notification.Builder(NotificationTestList.this, "red")
758                             .setSmallIcon(R.drawable.icon2)
759                             .setContentTitle(name)
760                             .build();
761                     mNM.notify(1, n);
762                 }
763             },
764 
765             new Test("Lights off") {
766                 public void run()
767                 {
768                     Notification n = new Notification.Builder(NotificationTestList.this, "default")
769                             .setSmallIcon(R.drawable.icon2)
770                             .setContentTitle(name)
771                             .build();
772                     mNM.notify(1, n);
773                 }
774             },
775 
776             new Test("Alert once") {
777                 public void run()
778                 {
779                     Notification n = new Notification.Builder(NotificationTestList.this, "high")
780                             .setSmallIcon(R.drawable.icon2)
781                             .setContentTitle(name)
782                             .setOnlyAlertOnce(true)
783                             .build();
784                     mNM.notify(1, n);
785                 }
786             },
787 
788             new Test("Resource Sound") {
789                 public void run()
790                 {
791                     NotificationChannel channel = new NotificationChannel("res_sound",
792                             "resource sound", IMPORTANCE_DEFAULT);
793                     channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
794                             getPackageName() + "/raw/ringer"), Notification.AUDIO_ATTRIBUTES_DEFAULT);
795                     mNM.createNotificationChannel(channel);
796 
797                     Notification n = new Notification.Builder(NotificationTestList.this, "res_sound")
798                             .setSmallIcon(R.drawable.stat_sys_phone)
799                             .setContentTitle(name)
800                             .build();
801                     Log.d(TAG, "n.sound=" + n.sound);
802 
803                     mNM.notify(1, n);
804                 }
805             },
806 
807             new Test("Sound and Cancel") {
808                 public void run()
809                 {
810                     NotificationChannel channel = new NotificationChannel("res_sound",
811                             "resource sound", IMPORTANCE_DEFAULT);
812                     channel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
813                             getPackageName() + "/raw/ringer"), Notification.AUDIO_ATTRIBUTES_DEFAULT);
814                     mNM.createNotificationChannel(channel);
815 
816                     Notification n = new Notification.Builder(NotificationTestList.this, "res_sound")
817                             .setSmallIcon(R.drawable.stat_sys_phone)
818                             .setContentTitle(name)
819                             .build();
820 
821                     mNM.notify(1, n);
822                     SystemClock.sleep(600);
823                     mNM.cancel(1);
824                 }
825             },
826 
827             new Test("Vibrate and cancel") {
828                 public void run()
829                 {
830                     NotificationChannel channel = new NotificationChannel("vibrate",
831                             "vibrate", IMPORTANCE_DEFAULT);
832                     channel.enableVibration(true);
833                     channel.setVibrationPattern(new long[] {0, 700, 500, 1000, 0, 700, 500, 1000,
834                             0, 700, 500, 1000, 0, 700, 500, 1000, 0, 700, 500, 1000, 0, 700, 500, 1000,
835                             0, 700, 500, 1000, 0, 700, 500, 1000});
836                     mNM.createNotificationChannel(channel);
837 
838                     Notification n = new Notification.Builder(NotificationTestList.this, "vibrate")
839                             .setSmallIcon(R.drawable.stat_sys_phone)
840                             .setContentTitle(name)
841                             .build();
842 
843                     mNM.notify(1, n);
844                     SystemClock.sleep(500);
845                     mNM.cancel(1);
846                 }
847             },
848 
849             new Test("Vibrate pattern") {
850                 public void run()
851                 {
852                     mVibrator.vibrate(new long[] { 250, 1000, 500, 2000 }, -1);
853                 }
854             },
855 
856             new Test("Vibrate pattern repeating") {
857                 public void run()
858                 {
859                     mVibrator.vibrate(new long[] { 250, 1000, 500 }, 1);
860                 }
861             },
862 
863             new Test("Vibrate 3s") {
864                 public void run()
865                 {
866                     mVibrator.vibrate(3000);
867                 }
868             },
869 
870             new Test("Vibrate 100s") {
871                 public void run()
872                 {
873                     mVibrator.vibrate(100000);
874                 }
875             },
876 
877             new Test("Vibrate off") {
878                 public void run()
879                 {
880                     mVibrator.cancel();
881                 }
882             },
883 
884             new Test("Cancel #1") {
885                 public void run() {
886                     mNM.cancel(1);
887                 }
888             },
889 
890             new Test("Cancel #1 in 3 sec") {
891                 public void run() {
892                     mHandler.postDelayed(new Runnable() {
893                         public void run() {
894                             Log.d(TAG, "Cancelling now...");
895                             mNM.cancel(1);
896                         }
897                     }, 3000);
898                 }
899             },
900 
901             new Test("Cancel #2") {
902                 public void run() {
903                     mNM.cancel(2);
904                 }
905             },
906 
907             new Test("Persistent #1") {
908                 public void run() {
909                     Notification n = new Notification.Builder(NotificationTestList.this)
910                             .setSmallIcon(R.drawable.icon1)
911                             .setWhen(mActivityCreateTime)
912                             .setContentTitle(name)
913                             .setContentText("This is a notification!!!")
914                             .setContentIntent(makeIntent())
915                             .build();
916                     mNM.notify(1, n);
917                 }
918             },
919 
920             new Test("Persistent #1 in 3 sec") {
921                 public void run() {
922                     mHandler.postDelayed(new Runnable() {
923                         public void run() {
924                             String message = "            "
925                                     + "tick tock tick tock\n\nSometimes notifications can "
926                                     + "be really long and wrap to more than one line.\n"
927                                     + "Sometimes."
928                                     + "Ohandwhathappensifwehaveonereallylongstringarewesure"
929                                     + "thatwesegmentitcorrectly?\n";
930                             Notification n = new Notification.Builder(
931                                     NotificationTestList.this, "low")
932                                     .setSmallIcon(R.drawable.icon1)
933                                     .setContentTitle(name)
934                                     .setContentText("This is still a notification!!!")
935                                     .setContentIntent(makeIntent())
936                                     .setStyle(new Notification.BigTextStyle().bigText(message))
937                                     .build();
938                             mNM.notify(1, n);
939                         }
940                     }, 3000);
941                 }
942             },
943 
944             new Test("Persistent #2") {
945                 public void run() {
946                     Notification n = new Notification.Builder(NotificationTestList.this, "low")
947                             .setSmallIcon(R.drawable.icon1)
948                             .setWhen(mActivityCreateTime)
949                             .setContentTitle(name)
950                             .setContentText("This is a notification!!!")
951                             .setContentIntent(makeIntent())
952                             .build();
953                     mNM.notify(2, n);
954                 }
955             },
956 
957             new Test("Persistent #3") {
958                 public void run() {
959                     Notification n = new Notification.Builder(NotificationTestList.this, "low")
960                             .setSmallIcon(R.drawable.icon1)
961                             .setWhen(mActivityCreateTime)
962                             .setContentTitle(name)
963                             .setContentText("This is a notification!!!")
964                             .setContentIntent(makeIntent())
965                             .build();
966                     mNM.notify(3, n);
967                 }
968             },
969 
970             new Test("Persistent #2 Vibrate") {
971                 public void run() {
972                     Notification n = new Notification.Builder(NotificationTestList.this, "low")
973                             .setSmallIcon(R.drawable.icon1)
974                             .setWhen(mActivityCreateTime)
975                             .setContentTitle(name)
976                             .setContentText("This is a notification!!!")
977                             .setContentIntent(makeIntent())
978                             .setDefaults(Notification.DEFAULT_VIBRATE)
979                             .build();
980                     mNM.notify(2, n);
981                 }
982             },
983 
984             new Test("Persistent #1 - different icon") {
985                 public void run() {
986                     Notification n = new Notification.Builder(NotificationTestList.this, "low")
987                             .setSmallIcon(R.drawable.icon2)
988                             .setWhen(mActivityCreateTime)
989                             .setContentTitle(name)
990                             .setContentText("This is a notification!!!")
991                             .setContentIntent(makeIntent())
992                             .build();
993                     mNM.notify(1, n);
994                 }
995             },
996 
997             new Test("Chronometer Start") {
998                 public void run() {
999                     Notification n = new Notification.Builder(NotificationTestList.this, "low")
1000                             .setSmallIcon(R.drawable.icon1)
1001                             .setWhen(System.currentTimeMillis())
1002                             .setContentTitle(name)
1003                             .setContentIntent(makeIntent())
1004                             .setOngoing(true)
1005                             .setUsesChronometer(true)
1006                             .build();
1007                     mNM.notify(2, n);
1008                 }
1009             },
1010 
1011             new Test("Chronometer Stop") {
1012                 public void run() {
1013                     mHandler.postDelayed(new Runnable() {
1014                         public void run() {
1015                             Log.d(TAG, "Chronometer Stop");
1016                             Notification n = new Notification.Builder(
1017                                     NotificationTestList.this, "low")
1018                                     .setSmallIcon(R.drawable.icon1)
1019                                     .setWhen(System.currentTimeMillis())
1020                                     .setContentTitle(name)
1021                                     .setContentIntent(makeIntent())
1022                                     .build();
1023                             mNM.notify(2, n);
1024                         }
1025                     }, 3000);
1026                 }
1027             },
1028 
1029             new Test("Sequential Persistent") {
1030                 public void run() {
1031                     mNM.notify(1, notificationWithNumbers(name, 1));
1032                     mNM.notify(2, notificationWithNumbers(name, 2));
1033                 }
1034             },
1035 
1036             new Test("Replace Persistent") {
1037                 public void run() {
1038                     mNM.notify(1, notificationWithNumbers(name, 1));
1039                     mNM.notify(1, notificationWithNumbers(name, 1));
1040                 }
1041             },
1042 
1043             new Test("Run and Cancel (n=1)") {
1044                 public void run() {
1045                     mNM.notify(1, notificationWithNumbers(name, 1));
1046                     mNM.cancel(1);
1047                 }
1048             },
1049 
1050             new Test("Run an Cancel (n=2)") {
1051                 public void run() {
1052                     mNM.notify(1, notificationWithNumbers(name, 1));
1053                     mNM.notify(2, notificationWithNumbers(name, 2));
1054                     mNM.cancel(2);
1055                 }
1056             },
1057 
1058             // Repeatedly notify and cancel -- triggers bug #670627
1059             new Test("Bug 670627") {
1060                 public void run() {
1061                     for (int i = 0; i < 10; i++) {
1062                         Log.d(TAG, "Add two notifications");
1063                         mNM.notify(1, notificationWithNumbers(name, 1));
1064                         mNM.notify(2, notificationWithNumbers(name, 2));
1065                         Log.d(TAG, "Cancel two notifications");
1066                         mNM.cancel(1);
1067                         mNM.cancel(2);
1068                     }
1069                 }
1070             },
1071 
1072             new Test("Ten Notifications") {
1073                 public void run() {
1074                     for (int i = 0; i < 10; i++) {
1075                         Notification n = new Notification.Builder(NotificationTestList.this, "low")
1076                                 .setSmallIcon(kNumberedIconResIDs[i])
1077                                 .setContentTitle("Persistent #" + i)
1078                                 .setContentText("Notify me!!!" + i)
1079                                 .setOngoing(i < 2)
1080                                 .setNumber(i)
1081                                 .build();
1082                         mNM.notify((i+1)*10, n);
1083                     }
1084                 }
1085             },
1086 
1087             new Test("Cancel eight notifications") {
1088                 public void run() {
1089                     for (int i = 1; i < 9; i++) {
1090                         mNM.cancel((i+1)*10);
1091                     }
1092                 }
1093             },
1094 
1095             new Test("Cancel the other two notifications") {
1096                 public void run() {
1097                     mNM.cancel(10);
1098                     mNM.cancel(100);
1099                 }
1100             },
1101 
1102             new Test("Persistent with numbers 1") {
1103                 public void run() {
1104                     mNM.notify(1, notificationWithNumbers(name, 1));
1105                 }
1106             },
1107 
1108             new Test("Persistent with numbers 22") {
1109                 public void run() {
1110                     mNM.notify(1, notificationWithNumbers(name, 22));
1111                 }
1112             },
1113 
1114             new Test("Persistent with numbers 333") {
1115                 public void run() {
1116                     mNM.notify(1, notificationWithNumbers(name, 333));
1117                 }
1118             },
1119 
1120             new Test("Persistent with numbers 4444") {
1121                 public void run() {
1122                     mNM.notify(1, notificationWithNumbers(name, 4444));
1123                 }
1124             },
1125 
1126             new Test("Crash") {
1127                 public void run()
1128                 {
1129                     PowerManager.WakeLock wl =
1130                             ((PowerManager) NotificationTestList.this.getSystemService(Context.POWER_SERVICE))
1131                                     .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "crasher");
1132                     wl.acquire();
1133                     mHandler.postDelayed(new Runnable() {
1134                         public void run() {
1135                             throw new RuntimeException("Die!");
1136                         }
1137                     }, 10000);
1138 
1139                 }
1140             },
1141 
1142     };
1143 
notificationWithNumbers(String name, int num)1144     private Notification notificationWithNumbers(String name, int num) {
1145         Notification n = new Notification.Builder(NotificationTestList.this, "low")
1146                 .setSmallIcon((num >= 0 && num < kNumberedIconResIDs.length)
1147                         ? kNumberedIconResIDs[num]
1148                         : kUnnumberedIconResID)
1149                 .setContentTitle(name)
1150                 .setContentText("Number=" + num)
1151                 .setNumber(num)
1152                 .build();
1153         return n;
1154     }
1155 
makeIntent()1156     private PendingIntent makeIntent() {
1157         Intent intent = new Intent(Intent.ACTION_MAIN);
1158         intent.addCategory(Intent.CATEGORY_HOME);
1159         return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
1160     }
1161 
makeIntent2()1162     private PendingIntent makeIntent2() {
1163         Intent intent = new Intent(this, StatusBarTest.class);
1164         return PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE);
1165     }
1166 
1167 
1168     class StateStress extends Test {
StateStress(String name, int pause, int iterations, Runnable[] tasks)1169         StateStress(String name, int pause, int iterations, Runnable[] tasks) {
1170             super(name);
1171             mPause = pause;
1172             mTasks = tasks;
1173             mIteration = iterations;
1174         }
1175         Runnable[] mTasks;
1176         int mNext;
1177         int mIteration;
1178         long mPause;
1179         Runnable mRunnable = new Runnable() {
1180             public void run() {
1181                 mTasks[mNext].run();
1182                 mNext++;
1183                 if (mNext >= mTasks.length) {
1184                     mNext = 0;
1185                     mIteration--;
1186                     if (mIteration <= 0) {
1187                         return;
1188                     }
1189                 }
1190                 mHandler.postDelayed(mRunnable, mPause);
1191             }
1192         };
run()1193         public void run() {
1194             mNext = 0;
1195             mHandler.postDelayed(mRunnable, mPause);
1196         }
1197     }
1198 
timeNotification(int n, String label, long time)1199     void timeNotification(int n, String label, long time) {
1200         mNM.notify(n, new Notification.Builder(NotificationTestList.this, "low")
1201                 .setSmallIcon(R.drawable.ic_statusbar_missedcall)
1202                 .setWhen(time)
1203                 .setContentTitle(label)
1204                 .setContentText(new java.util.Date(time).toString())
1205                 .build());
1206 
1207     }
1208 
loadBitmap(int resId)1209     Bitmap loadBitmap(int resId) {
1210         BitmapDrawable bd = (BitmapDrawable)getResources().getDrawable(resId);
1211         return Bitmap.createBitmap(bd.getBitmap());
1212     }
1213 }
1214