NotificationTestList.java revision d2b1f00e1e34cdce0047af7374aa57b1e4b5b040
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 android.app.ListActivity;
20import android.app.PendingIntent;
21import android.widget.ArrayAdapter;
22import android.view.View;
23import android.widget.ListView;
24import android.content.Context;
25import android.content.ContentResolver;
26import android.content.Intent;
27import android.app.Notification;
28import android.app.NotificationManager;
29import android.os.Vibrator;
30import android.os.Bundle;
31import android.os.Handler;
32import android.util.Log;
33import android.net.Uri;
34import android.os.SystemClock;
35import android.widget.RemoteViews;
36import android.widget.TextView;
37import android.os.PowerManager;
38
39public class NotificationTestList extends TestActivity
40{
41    private final static String TAG = "NotificationTestList";
42
43    NotificationManager mNM;
44    Vibrator mVibrator = new Vibrator();
45    Handler mHandler = new Handler();
46
47    long mActivityCreateTime = System.currentTimeMillis();
48    long mChronometerBase = 0;
49
50    @Override
51    protected String tag() {
52        return TAG;
53    }
54
55    @Override
56    protected Test[] tests() {
57        mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
58
59        return mTests;
60    }
61
62    private Test[] mTests = new Test[] {
63        new Test("Off and sound") {
64            public void run() {
65                PowerManager pm = (PowerManager)NotificationTestList.this.getSystemService(Context.POWER_SERVICE);
66                PowerManager.WakeLock wl =
67                            pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "sound");
68                wl.acquire();
69
70                pm.goToSleep(SystemClock.uptimeMillis());
71
72                Notification n = new Notification();
73                n.sound = Uri.parse("file:///sdcard/virtual-void.mp3");
74                Log.d(TAG, "n.sound=" + n.sound);
75
76                mNM.notify(1, n);
77
78                Log.d(TAG, "releasing wake lock");
79                wl.release();
80                Log.d(TAG, "released wake lock");
81            }
82        },
83
84        new Test("No view") {
85            public void run() {
86                Notification n = new Notification(R.drawable.icon1, "No view",
87                        System.currentTimeMillis());
88                mNM.notify(1, n);
89            }
90        },
91
92        new Test("No intent") {
93            public void run() {
94                Notification n = new Notification(R.drawable.icon1, "No intent",
95                        System.currentTimeMillis());
96                n.setLatestEventInfo(NotificationTestList.this, "No intent",
97                            "No intent", null);
98                mNM.notify(1, n);
99            }
100        },
101
102        new Test("Layout") {
103            public void run()
104            {
105
106                mNM.notify(1, new Notification(NotificationTestList.this,
107                            R.drawable.ic_statusbar_missedcall,
108                            null, System.currentTimeMillis()-(1000*60*60*24),
109                            "(453) 123-2328",
110                            "", null));
111
112                mNM.notify(2, new Notification(NotificationTestList.this,
113                            R.drawable.ic_statusbar_email,
114                            null, System.currentTimeMillis(),
115                            "Mark Willem, Me (2)",
116                            "Re: Didn't you get the memo?", null));
117
118                mNM.notify(3, new Notification(NotificationTestList.this,
119                            R.drawable.ic_statusbar_chat,
120                            null, System.currentTimeMillis()+(1000*60*60*24),
121                            "Sophia Winterlanden",
122                            "Lorem ipsum dolor sit amet.", null));
123            }
124        },
125
126        new Test("Bad Icon") {
127            public void run() {
128                mNM.notify(1, new Notification(NotificationTestList.this,
129                            R.layout.chrono_notification, /* not a drawable! */
130                            null, System.currentTimeMillis()-(1000*60*60*24),
131                            "(453) 123-2328",
132                            "", null));
133            }
134        },
135
136        new Test("Bad resource #2") {
137            public void run()
138            {
139                Notification n = new Notification(NotificationTestList.this,
140                            R.drawable.ic_statusbar_missedcall,
141                            null, System.currentTimeMillis()-(1000*60*60*24),
142                            "(453) 123-2328",
143                            "", null);
144                n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
145                mNM.notify(2, n);
146            }
147        },
148
149        new Test("Bad resource #3") {
150            public void run()
151            {
152                Notification n = new Notification(NotificationTestList.this,
153                            R.drawable.ic_statusbar_missedcall,
154                            null, System.currentTimeMillis()-(1000*60*60*24),
155                            "(453) 123-2328",
156                            "", null);
157                n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
158                mNM.notify(3, n);
159            }
160        },
161
162        new Test("Times") {
163            public void run()
164            {
165                long now = System.currentTimeMillis();
166
167                timeNotification(7, "24 hours from now", now+(1000*60*60*24));
168                timeNotification(6, "12:01:00 from now", now+(1000*60*60*12)+(60*1000));
169                timeNotification(5, "12 hours from now", now+(1000*60*60*12));
170                timeNotification(4, "now", now);
171                timeNotification(3, "11:59:00 ago", now-((1000*60*60*12)-(60*1000)));
172                timeNotification(2, "12 hours ago", now-(1000*60*60*12));
173                timeNotification(1, "24 hours ago", now-(1000*60*60*24));
174            }
175        },
176        new StateStress("Stress - Ongoing / Latest", 100, 100, new Runnable[] {
177                new Runnable() {
178                    public void run() {
179                        Log.d(TAG, "Stress - Ongoing/Latest 0");
180                        Notification n = new Notification(NotificationTestList.this,
181                                R.drawable.icon3,
182                                null, System.currentTimeMillis(), "Stress - Ongoing",
183                                "Notify me!!!", null);
184                        n.flags |= Notification.FLAG_ONGOING_EVENT;
185                        mNM.notify(1, n);
186                    }
187                },
188                new Runnable() {
189                    public void run() {
190                        Log.d(TAG, "Stress - Ongoing/Latest 1");
191                        Notification n = new Notification(NotificationTestList.this,
192                                R.drawable.icon4,
193                                null, System.currentTimeMillis(), "Stress - Latest",
194                                "Notify me!!!", null);
195                        //n.flags |= Notification.FLAG_ONGOING_EVENT;
196                        mNM.notify(1, n);
197                    }
198                }
199            }),
200
201        new Test("Long") {
202            public void run()
203            {
204                Notification n = new Notification();
205                n.defaults |= Notification.DEFAULT_SOUND ;
206                n.vibrate = new long[] {
207                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
208                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
209                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 };
210                mNM.notify(1, n);
211            }
212        },
213
214        new Test("Blue Lights") {
215            public void run()
216            {
217                Notification n = new Notification();
218                n.flags |= Notification.FLAG_SHOW_LIGHTS;
219                n.ledARGB = 0xff0000ff;
220                n.ledOnMS = 1;
221                n.ledOffMS = 0;
222                mNM.notify(1, n);
223            }
224        },
225
226        new Test("Red Lights") {
227            public void run()
228            {
229                Notification n = new Notification();
230                n.flags |= Notification.FLAG_SHOW_LIGHTS;
231                n.ledARGB = 0xffff0000;
232                n.ledOnMS = 1;
233                n.ledOffMS = 0;
234                mNM.notify(1, n);
235            }
236        },
237
238        new Test("Yellow Lights") {
239            public void run()
240            {
241                Notification n = new Notification();
242                n.flags |= Notification.FLAG_SHOW_LIGHTS;
243                n.ledARGB = 0xffffff00;
244                n.ledOnMS = 1;
245                n.ledOffMS = 0;
246                mNM.notify(1, n);
247            }
248        },
249
250        new Test("Lights off") {
251            public void run()
252            {
253                Notification n = new Notification();
254                n.flags |= Notification.FLAG_SHOW_LIGHTS;
255                n.ledARGB = 0x00000000;
256                n.ledOnMS = 0;
257                n.ledOffMS = 0;
258                mNM.notify(1, n);
259            }
260        },
261
262        new Test("Blue Blinking Slow") {
263            public void run()
264            {
265                Notification n = new Notification();
266                n.flags |= Notification.FLAG_SHOW_LIGHTS;
267                n.ledARGB = 0xff0000ff;
268                n.ledOnMS = 1300;
269                n.ledOffMS = 1300;
270                mNM.notify(1, n);
271            }
272        },
273
274        new Test("Blue Blinking Fast") {
275            public void run()
276            {
277                Notification n = new Notification();
278                n.flags |= Notification.FLAG_SHOW_LIGHTS;
279                n.ledARGB = 0xff0000ff;
280                n.ledOnMS = 300;
281                n.ledOffMS = 300;
282                mNM.notify(1, n);
283            }
284        },
285
286        new Test("Default All") {
287            public void run()
288            {
289                Notification n = new Notification();
290                n.defaults |= Notification.DEFAULT_ALL;
291                mNM.notify(1, n);
292            }
293        },
294
295        new Test("Default All, once") {
296            public void run()
297            {
298                Notification n = new Notification();
299                n.defaults |= Notification.DEFAULT_ALL;
300                n.flags |= Notification.FLAG_ONLY_ALERT_ONCE ;
301                mNM.notify(1, n);
302            }
303        },
304
305        new Test("Content Sound") {
306            public void run()
307            {
308                Notification n = new Notification();
309                n.sound = Uri.parse(
310                        "content://media/internal/audio/media/7");
311
312                mNM.notify(1, n);
313            }
314        },
315
316        new Test("Resource Sound") {
317            public void run()
318            {
319                Notification n = new Notification();
320                n.sound = Uri.parse(
321                        ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
322                        getPackageName() + "/raw/ringer");
323                Log.d(TAG, "n.sound=" + n.sound);
324
325                mNM.notify(1, n);
326            }
327        },
328
329        new Test("Sound and Cancel") {
330            public void run()
331            {
332                Notification n = new Notification();
333                n.sound = Uri.parse(
334                            "content://media/internal/audio/media/7");
335
336                mNM.notify(1, n);
337                SystemClock.sleep(200);
338                mNM.cancel(1);
339            }
340        },
341
342        new Test("Vibrate") {
343            public void run()
344            {
345                Notification n = new Notification();
346                    n.vibrate = new long[] { 0, 700, 500, 1000 };
347
348                mNM.notify(1, n);
349            }
350        },
351
352        new Test("Vibrate and cancel") {
353            public void run()
354            {
355                Notification n = new Notification();
356                    n.vibrate = new long[] { 0, 700, 500, 1000 };
357
358                mNM.notify(1, n);
359                SystemClock.sleep(500);
360                mNM.cancel(1);
361            }
362        },
363
364        new Test("Vibrate pattern") {
365            public void run()
366            {
367                mVibrator.vibrate(new long[] { 250, 1000, 500, 2000 }, -1);
368            }
369        },
370
371        new Test("Vibrate pattern repeating") {
372            public void run()
373            {
374                mVibrator.vibrate(new long[] { 250, 1000, 500 }, 1);
375            }
376        },
377
378        new Test("Vibrate 3s") {
379            public void run()
380            {
381                mVibrator.vibrate(3000);
382            }
383        },
384
385        new Test("Vibrate 100s") {
386            public void run()
387            {
388                mVibrator.vibrate(100000);
389            }
390        },
391
392        new Test("Vibrate off") {
393            public void run()
394            {
395                mVibrator.cancel();
396            }
397        },
398
399        new Test("Cancel #1") {
400            public void run() {
401                mNM.cancel(1);
402            }
403        },
404
405        new Test("Cancel #1 in 3 sec") {
406            public void run() {
407                mHandler.postDelayed(new Runnable() {
408                            public void run() {
409                                Log.d(TAG, "Cancelling now...");
410                                mNM.cancel(1);
411                            }
412                        }, 3000);
413            }
414        },
415
416        new Test("Cancel #2") {
417            public void run() {
418                mNM.cancel(2);
419            }
420        },
421
422        new Test("Persistent #1") {
423            public void run() {
424                Notification n = new Notification(R.drawable.icon1, "tick tick tick",
425                        mActivityCreateTime);
426                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
427                            "This is a notification!!!", makeIntent());
428                mNM.notify(1, n);
429            }
430        },
431
432        new Test("Persistent #1 in 3 sec") {
433            public void run() {
434                mHandler.postDelayed(new Runnable() {
435                            public void run() {
436                                Notification n = new Notification(R.drawable.icon1,
437                                        "            "
438                                        + "tick tock tick tock\n\nSometimes notifications can "
439                                        + "be really long and wrap to more than one line.\n"
440                                        + "Sometimes."
441                                        + "Ohandwhathappensifwehaveonereallylongstringarewesure"
442                                        + "thatwesegmentitcorrectly?\n",
443                                        System.currentTimeMillis());
444                                n.setLatestEventInfo(NotificationTestList.this,
445                                        "Still Persistent #1",
446                                        "This is still a notification!!!",
447                                        makeIntent());
448                                mNM.notify(1, n);
449                            }
450                        }, 3000);
451            }
452        },
453
454        new Test("Persistent #2") {
455            public void run() {
456                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
457                        System.currentTimeMillis());
458                n.setLatestEventInfo(NotificationTestList.this, "Persistent #2",
459                            "Notify me!!!", makeIntent());
460                mNM.notify(2, n);
461            }
462        },
463
464        new Test("Persistent #3") {
465            public void run() {
466                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
467                        System.currentTimeMillis());
468                n.setLatestEventInfo(NotificationTestList.this, "Persistent #3",
469                            "Notify me!!!", makeIntent());
470                mNM.notify(3, n);
471            }
472        },
473
474        new Test("Persistent #2 Vibrate") {
475            public void run() {
476                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
477                        System.currentTimeMillis());
478                n.setLatestEventInfo(NotificationTestList.this, "Persistent #2",
479                            "Notify me!!!", makeIntent());
480                n.defaults = Notification.DEFAULT_VIBRATE;
481                mNM.notify(2, n);
482            }
483        },
484
485        new Test("Persistent #1 - different icon") {
486            public void run() {
487                Notification n = new Notification(R.drawable.icon2, null,
488                        mActivityCreateTime);
489                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
490                            "This is the same notification!!!", makeIntent());
491                mNM.notify(1, n);
492            }
493        },
494
495        new Test("Chronometer Start") {
496            public void run() {
497                Notification n = new Notification(R.drawable.icon2, "me me me me",
498                                                    System.currentTimeMillis());
499                n.contentView = new RemoteViews(getPackageName(), R.layout.chrono_notification);
500                mChronometerBase = SystemClock.elapsedRealtime();
501                n.contentView.setChronometer(R.id.time, mChronometerBase, "Yay! (%s)", true);
502                n.flags |= Notification.FLAG_ONGOING_EVENT;
503                n.contentIntent = makeIntent();
504                mNM.notify(2, n);
505            }
506        },
507
508        new Test("Chronometer Stop") {
509            public void run() {
510                mHandler.postDelayed(new Runnable() {
511                        public void run() {
512                            Log.d(TAG, "Chronometer Stop");
513                            Notification n = new Notification();
514                            n.icon = R.drawable.icon1;
515                            n.contentView = new RemoteViews(getPackageName(),
516                                                             R.layout.chrono_notification);
517                            n.contentView.setChronometer(R.id.time, mChronometerBase, null, false);
518                            n.contentIntent = makeIntent();
519                            mNM.notify(2, n);
520                        }
521                    }, 3000);
522            }
523        },
524
525        new Test("Sequential Persistent") {
526            public void run() {
527                mNM.notify(1, notificationWithNumbers(1));
528                mNM.notify(2, notificationWithNumbers(2));
529            }
530        },
531
532        new Test("Replace Persistent") {
533            public void run() {
534                mNM.notify(1, notificationWithNumbers(1));
535                mNM.notify(1, notificationWithNumbers(1));
536            }
537        },
538
539        new Test("Run and Cancel (n=1)") {
540            public void run() {
541                mNM.notify(1, notificationWithNumbers(1));
542                mNM.cancel(1);
543            }
544        },
545
546        new Test("Run an Cancel (n=2)") {
547            public void run() {
548                mNM.notify(1, notificationWithNumbers(1));
549                mNM.notify(2, notificationWithNumbers(2));
550                mNM.cancel(2);
551            }
552        },
553
554        // Repeatedly notify and cancel -- triggers bug #670627
555        new Test("Bug 670627") {
556            public void run() {
557                for (int i = 0; i < 10; i++) {
558                  Log.d(TAG, "Add two notifications");
559                  mNM.notify(1, notificationWithNumbers(1));
560                  mNM.notify(2, notificationWithNumbers(2));
561                  Log.d(TAG, "Cancel two notifications");
562                  mNM.cancel(1);
563                  mNM.cancel(2);
564                }
565            }
566        },
567
568        new Test("Ten Notifications") {
569            public void run() {
570                for (int i = 0; i < 2; i++) {
571                    Notification n = new Notification(NotificationTestList.this, R.drawable.icon2,
572                            null, System.currentTimeMillis(), "Persistent #" + i,
573                            "Notify me!!!" + i, null);
574                    n.flags |= Notification.FLAG_ONGOING_EVENT;
575                    n.number = i;
576                    mNM.notify((i+1)*10, n);
577                }
578                for (int i = 2; i < 10; i++) {
579                    Notification n = new Notification(NotificationTestList.this, R.drawable.icon2,
580                            null, System.currentTimeMillis(), "Persistent #" + i,
581                            "Notify me!!!" + i, null);
582                    n.number = i;
583                    mNM.notify((i+1)*10, n);
584                }
585            }
586        },
587
588        new Test("Cancel eight notifications") {
589            public void run() {
590                for (int i = 1; i < 9; i++) {
591                    mNM.cancel((i+1)*10);
592                }
593            }
594        },
595
596        new Test("Persistent with numbers 1") {
597            public void run() {
598                mNM.notify(1, notificationWithNumbers(1));
599            }
600        },
601
602        new Test("Persistent with numbers 222") {
603            public void run() {
604                mNM.notify(1, notificationWithNumbers(22));
605            }
606        },
607
608        new Test("Persistent with numbers 333") {
609            public void run() {
610                mNM.notify(1, notificationWithNumbers(333));
611            }
612        },
613
614        new Test("Persistent with numbers 4444") {
615            public void run() {
616                mNM.notify(1, notificationWithNumbers(4444));
617            }
618        },
619
620        new Test("Crash") {
621            public void run()
622            {
623                PowerManager.WakeLock wl
624                        = ((PowerManager)NotificationTestList.this.getSystemService(Context.POWER_SERVICE))
625                            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "crasher");
626                wl.acquire();
627                mHandler.postDelayed(new Runnable() {
628                            public void run() {
629                                throw new RuntimeException("Die!");
630                            }
631                        }, 10000);
632
633            }
634        },
635
636    };
637
638    private Notification notificationWithNumbers(int num) {
639        Notification n = new Notification(this, R.drawable.icon2, null, System.currentTimeMillis(),
640                "Persistent #2", "Notify me!!!", null);
641        n.number = num;
642        return n;
643    }
644
645    private PendingIntent makeIntent() {
646        Intent intent = new Intent(Intent.ACTION_MAIN);
647        intent.addCategory(Intent.CATEGORY_HOME);
648        return PendingIntent.getActivity(this, 0, intent, 0);
649    }
650
651    class StateStress extends Test {
652        StateStress(String name, int pause, int iterations, Runnable[] tasks) {
653            super(name);
654            mPause = pause;
655            mTasks = tasks;
656            mIteration = iterations;
657        }
658        Runnable[] mTasks;
659        int mNext;
660        int mIteration;
661        long mPause;
662        Runnable mRunnable = new Runnable() {
663            public void run() {
664                mTasks[mNext].run();
665                mNext++;
666                if (mNext >= mTasks.length) {
667                    mNext = 0;
668                    mIteration--;
669                    if (mIteration <= 0) {
670                        return;
671                    }
672                }
673                mHandler.postDelayed(mRunnable, mPause);
674            }
675        };
676        public void run() {
677            mNext = 0;
678            mHandler.postDelayed(mRunnable, mPause);
679        }
680    }
681
682    void timeNotification(int n, String label, long time) {
683        mNM.notify(n, new Notification(NotificationTestList.this,
684                    R.drawable.ic_statusbar_missedcall, null,
685                    time, label, "" + new java.util.Date(time), null));
686
687    }
688}
689
690