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