NotificationTestList.java revision 6c01a11a20d5fc796360e5e97998401a6cc84f27
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 #1 (when=create)") {
127            public void run() {
128                Notification n = new Notification(R.layout.chrono_notification /* not an icon */,
129                        null, mActivityCreateTime);
130                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
131                            "This is the same notification!!!", makeIntent());
132                mNM.notify(1, n);
133            }
134        },
135
136        new Test("Bad Icon #1 (when=now)") {
137            public void run() {
138                Notification n = new Notification(R.layout.chrono_notification /* not an icon */,
139                        null, System.currentTimeMillis());
140                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
141                            "This is the same notification!!!", makeIntent());
142                mNM.notify(1, n);
143            }
144        },
145
146        new Test("Bad resource #1 (when=create)") {
147            public void run() {
148                Notification n = new Notification(R.drawable.icon2,
149                        null, mActivityCreateTime);
150                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
151                            "This is the same notification!!!", makeIntent());
152                n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
153                mNM.notify(1, n);
154            }
155        },
156
157        new Test("Bad resource #1 (when=now)") {
158            public void run() {
159                Notification n = new Notification(R.drawable.icon2,
160                        null, System.currentTimeMillis());
161                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
162                            "This is the same notification!!!", makeIntent());
163                n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
164                mNM.notify(1, n);
165            }
166        },
167
168
169        new Test("Bad resource #3") {
170            public void run()
171            {
172                Notification n = new Notification(NotificationTestList.this,
173                            R.drawable.ic_statusbar_missedcall,
174                            null, System.currentTimeMillis()-(1000*60*60*24),
175                            "(453) 123-2328",
176                            "", null);
177                n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
178                mNM.notify(3, n);
179            }
180        },
181
182        new Test("Times") {
183            public void run()
184            {
185                long now = System.currentTimeMillis();
186
187                timeNotification(7, "24 hours from now", now+(1000*60*60*24));
188                timeNotification(6, "12:01:00 from now", now+(1000*60*60*12)+(60*1000));
189                timeNotification(5, "12 hours from now", now+(1000*60*60*12));
190                timeNotification(4, "now", now);
191                timeNotification(3, "11:59:00 ago", now-((1000*60*60*12)-(60*1000)));
192                timeNotification(2, "12 hours ago", now-(1000*60*60*12));
193                timeNotification(1, "24 hours ago", now-(1000*60*60*24));
194            }
195        },
196        new StateStress("Stress - Ongoing / Latest", 100, 100, new Runnable[] {
197                new Runnable() {
198                    public void run() {
199                        Log.d(TAG, "Stress - Ongoing/Latest 0");
200                        Notification n = new Notification(NotificationTestList.this,
201                                R.drawable.icon3,
202                                null, System.currentTimeMillis(), "Stress - Ongoing",
203                                "Notify me!!!", null);
204                        n.flags |= Notification.FLAG_ONGOING_EVENT;
205                        mNM.notify(1, n);
206                    }
207                },
208                new Runnable() {
209                    public void run() {
210                        Log.d(TAG, "Stress - Ongoing/Latest 1");
211                        Notification n = new Notification(NotificationTestList.this,
212                                R.drawable.icon4,
213                                null, System.currentTimeMillis(), "Stress - Latest",
214                                "Notify me!!!", null);
215                        //n.flags |= Notification.FLAG_ONGOING_EVENT;
216                        mNM.notify(1, n);
217                    }
218                }
219            }),
220
221        new Test("Long") {
222            public void run()
223            {
224                Notification n = new Notification();
225                n.defaults |= Notification.DEFAULT_SOUND ;
226                n.vibrate = new long[] {
227                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
228                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
229                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 };
230                mNM.notify(1, n);
231            }
232        },
233
234        new Test("Blue Lights") {
235            public void run()
236            {
237                Notification n = new Notification();
238                n.flags |= Notification.FLAG_SHOW_LIGHTS;
239                n.ledARGB = 0xff0000ff;
240                n.ledOnMS = 1;
241                n.ledOffMS = 0;
242                mNM.notify(1, n);
243            }
244        },
245
246        new Test("Red Lights") {
247            public void run()
248            {
249                Notification n = new Notification();
250                n.flags |= Notification.FLAG_SHOW_LIGHTS;
251                n.ledARGB = 0xffff0000;
252                n.ledOnMS = 1;
253                n.ledOffMS = 0;
254                mNM.notify(1, n);
255            }
256        },
257
258        new Test("Yellow Lights") {
259            public void run()
260            {
261                Notification n = new Notification();
262                n.flags |= Notification.FLAG_SHOW_LIGHTS;
263                n.ledARGB = 0xffffff00;
264                n.ledOnMS = 1;
265                n.ledOffMS = 0;
266                mNM.notify(1, n);
267            }
268        },
269
270        new Test("Lights off") {
271            public void run()
272            {
273                Notification n = new Notification();
274                n.flags |= Notification.FLAG_SHOW_LIGHTS;
275                n.ledARGB = 0x00000000;
276                n.ledOnMS = 0;
277                n.ledOffMS = 0;
278                mNM.notify(1, n);
279            }
280        },
281
282        new Test("Blue Blinking Slow") {
283            public void run()
284            {
285                Notification n = new Notification();
286                n.flags |= Notification.FLAG_SHOW_LIGHTS;
287                n.ledARGB = 0xff0000ff;
288                n.ledOnMS = 1300;
289                n.ledOffMS = 1300;
290                mNM.notify(1, n);
291            }
292        },
293
294        new Test("Blue Blinking Fast") {
295            public void run()
296            {
297                Notification n = new Notification();
298                n.flags |= Notification.FLAG_SHOW_LIGHTS;
299                n.ledARGB = 0xff0000ff;
300                n.ledOnMS = 300;
301                n.ledOffMS = 300;
302                mNM.notify(1, n);
303            }
304        },
305
306        new Test("Default All") {
307            public void run()
308            {
309                Notification n = new Notification();
310                n.defaults |= Notification.DEFAULT_ALL;
311                mNM.notify(1, n);
312            }
313        },
314
315        new Test("Default All, once") {
316            public void run()
317            {
318                Notification n = new Notification();
319                n.defaults |= Notification.DEFAULT_ALL;
320                n.flags |= Notification.FLAG_ONLY_ALERT_ONCE ;
321                mNM.notify(1, n);
322            }
323        },
324
325        new Test("Content Sound") {
326            public void run()
327            {
328                Notification n = new Notification();
329                n.sound = Uri.parse(
330                        "content://media/internal/audio/media/7");
331
332                mNM.notify(1, n);
333            }
334        },
335
336        new Test("Resource Sound") {
337            public void run()
338            {
339                Notification n = new Notification();
340                n.sound = Uri.parse(
341                        ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
342                        getPackageName() + "/raw/ringer");
343                Log.d(TAG, "n.sound=" + n.sound);
344
345                mNM.notify(1, n);
346            }
347        },
348
349        new Test("Sound and Cancel") {
350            public void run()
351            {
352                Notification n = new Notification();
353                n.sound = Uri.parse(
354                            "content://media/internal/audio/media/7");
355
356                mNM.notify(1, n);
357                SystemClock.sleep(200);
358                mNM.cancel(1);
359            }
360        },
361
362        new Test("Vibrate") {
363            public void run()
364            {
365                Notification n = new Notification();
366                    n.vibrate = new long[] { 0, 700, 500, 1000 };
367
368                mNM.notify(1, n);
369            }
370        },
371
372        new Test("Vibrate and cancel") {
373            public void run()
374            {
375                Notification n = new Notification();
376                    n.vibrate = new long[] { 0, 700, 500, 1000 };
377
378                mNM.notify(1, n);
379                SystemClock.sleep(500);
380                mNM.cancel(1);
381            }
382        },
383
384        new Test("Vibrate pattern") {
385            public void run()
386            {
387                mVibrator.vibrate(new long[] { 250, 1000, 500, 2000 }, -1);
388            }
389        },
390
391        new Test("Vibrate pattern repeating") {
392            public void run()
393            {
394                mVibrator.vibrate(new long[] { 250, 1000, 500 }, 1);
395            }
396        },
397
398        new Test("Vibrate 3s") {
399            public void run()
400            {
401                mVibrator.vibrate(3000);
402            }
403        },
404
405        new Test("Vibrate 100s") {
406            public void run()
407            {
408                mVibrator.vibrate(100000);
409            }
410        },
411
412        new Test("Vibrate off") {
413            public void run()
414            {
415                mVibrator.cancel();
416            }
417        },
418
419        new Test("Cancel #1") {
420            public void run() {
421                mNM.cancel(1);
422            }
423        },
424
425        new Test("Cancel #1 in 3 sec") {
426            public void run() {
427                mHandler.postDelayed(new Runnable() {
428                            public void run() {
429                                Log.d(TAG, "Cancelling now...");
430                                mNM.cancel(1);
431                            }
432                        }, 3000);
433            }
434        },
435
436        new Test("Cancel #2") {
437            public void run() {
438                mNM.cancel(2);
439            }
440        },
441
442        new Test("Persistent #1") {
443            public void run() {
444                Notification n = new Notification(R.drawable.icon1, "tick tick tick",
445                        mActivityCreateTime);
446                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
447                            "This is a notification!!!", makeIntent());
448                mNM.notify(1, n);
449            }
450        },
451
452        new Test("Persistent #1 in 3 sec") {
453            public void run() {
454                mHandler.postDelayed(new Runnable() {
455                            public void run() {
456                                Notification n = new Notification(R.drawable.icon1,
457                                        "            "
458                                        + "tick tock tick tock\n\nSometimes notifications can "
459                                        + "be really long and wrap to more than one line.\n"
460                                        + "Sometimes."
461                                        + "Ohandwhathappensifwehaveonereallylongstringarewesure"
462                                        + "thatwesegmentitcorrectly?\n",
463                                        System.currentTimeMillis());
464                                n.setLatestEventInfo(NotificationTestList.this,
465                                        "Still Persistent #1",
466                                        "This is still a notification!!!",
467                                        makeIntent());
468                                mNM.notify(1, n);
469                            }
470                        }, 3000);
471            }
472        },
473
474        new Test("Persistent #2") {
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                mNM.notify(2, n);
481            }
482        },
483
484        new Test("Persistent #3") {
485            public void run() {
486                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
487                        System.currentTimeMillis());
488                n.setLatestEventInfo(NotificationTestList.this, "Persistent #3",
489                            "Notify me!!!", makeIntent());
490                mNM.notify(3, n);
491            }
492        },
493
494        new Test("Persistent #2 Vibrate") {
495            public void run() {
496                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
497                        System.currentTimeMillis());
498                n.setLatestEventInfo(NotificationTestList.this, "Persistent #2",
499                            "Notify me!!!", makeIntent());
500                n.defaults = Notification.DEFAULT_VIBRATE;
501                mNM.notify(2, n);
502            }
503        },
504
505        new Test("Persistent #1 - different icon") {
506            public void run() {
507                Notification n = new Notification(R.drawable.icon2, null,
508                        mActivityCreateTime);
509                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
510                            "This is the same notification!!!", makeIntent());
511                mNM.notify(1, n);
512            }
513        },
514
515        new Test("Chronometer Start") {
516            public void run() {
517                Notification n = new Notification(R.drawable.icon2, "me me me me",
518                                                    System.currentTimeMillis());
519                n.contentView = new RemoteViews(getPackageName(), R.layout.chrono_notification);
520                mChronometerBase = SystemClock.elapsedRealtime();
521                n.contentView.setChronometer(R.id.time, mChronometerBase, "Yay! (%s)", true);
522                n.flags |= Notification.FLAG_ONGOING_EVENT;
523                n.contentIntent = makeIntent();
524                mNM.notify(2, n);
525            }
526        },
527
528        new Test("Chronometer Stop") {
529            public void run() {
530                mHandler.postDelayed(new Runnable() {
531                        public void run() {
532                            Log.d(TAG, "Chronometer Stop");
533                            Notification n = new Notification();
534                            n.icon = R.drawable.icon1;
535                            n.contentView = new RemoteViews(getPackageName(),
536                                                             R.layout.chrono_notification);
537                            n.contentView.setChronometer(R.id.time, mChronometerBase, null, false);
538                            n.contentIntent = makeIntent();
539                            mNM.notify(2, n);
540                        }
541                    }, 3000);
542            }
543        },
544
545        new Test("Sequential Persistent") {
546            public void run() {
547                mNM.notify(1, notificationWithNumbers(1));
548                mNM.notify(2, notificationWithNumbers(2));
549            }
550        },
551
552        new Test("Replace Persistent") {
553            public void run() {
554                mNM.notify(1, notificationWithNumbers(1));
555                mNM.notify(1, notificationWithNumbers(1));
556            }
557        },
558
559        new Test("Run and Cancel (n=1)") {
560            public void run() {
561                mNM.notify(1, notificationWithNumbers(1));
562                mNM.cancel(1);
563            }
564        },
565
566        new Test("Run an Cancel (n=2)") {
567            public void run() {
568                mNM.notify(1, notificationWithNumbers(1));
569                mNM.notify(2, notificationWithNumbers(2));
570                mNM.cancel(2);
571            }
572        },
573
574        // Repeatedly notify and cancel -- triggers bug #670627
575        new Test("Bug 670627") {
576            public void run() {
577                for (int i = 0; i < 10; i++) {
578                  Log.d(TAG, "Add two notifications");
579                  mNM.notify(1, notificationWithNumbers(1));
580                  mNM.notify(2, notificationWithNumbers(2));
581                  Log.d(TAG, "Cancel two notifications");
582                  mNM.cancel(1);
583                  mNM.cancel(2);
584                }
585            }
586        },
587
588        new Test("Ten Notifications") {
589            public void run() {
590                for (int i = 0; i < 2; i++) {
591                    Notification n = new Notification(NotificationTestList.this, R.drawable.icon2,
592                            null, System.currentTimeMillis(), "Persistent #" + i,
593                            "Notify me!!!" + i, null);
594                    n.flags |= Notification.FLAG_ONGOING_EVENT;
595                    n.number = i;
596                    mNM.notify((i+1)*10, n);
597                }
598                for (int i = 2; i < 10; i++) {
599                    Notification n = new Notification(NotificationTestList.this, R.drawable.icon2,
600                            null, System.currentTimeMillis(), "Persistent #" + i,
601                            "Notify me!!!" + i, null);
602                    n.number = i;
603                    mNM.notify((i+1)*10, n);
604                }
605            }
606        },
607
608        new Test("Cancel eight notifications") {
609            public void run() {
610                for (int i = 1; i < 9; i++) {
611                    mNM.cancel((i+1)*10);
612                }
613            }
614        },
615
616        new Test("Persistent with numbers 1") {
617            public void run() {
618                mNM.notify(1, notificationWithNumbers(1));
619            }
620        },
621
622        new Test("Persistent with numbers 22") {
623            public void run() {
624                mNM.notify(1, notificationWithNumbers(22));
625            }
626        },
627
628        new Test("Persistent with numbers 333") {
629            public void run() {
630                mNM.notify(1, notificationWithNumbers(333));
631            }
632        },
633
634        new Test("Persistent with numbers 4444") {
635            public void run() {
636                mNM.notify(1, notificationWithNumbers(4444));
637            }
638        },
639
640        new Test("Ticker") {
641            public void run() {
642                Notification not = new Notification(
643                    R.drawable.app_gmail,
644                    "New mail from joeo@example.com, on the topic of very long ticker texts",
645                    System.currentTimeMillis());
646                not.setLatestEventInfo(NotificationTestList.this,
647                    "A new message awaits",
648                    "The contents are very interesting and important",
649                    makeIntent());
650                mNM.notify(1, not);
651            }
652        },
653
654        new Test("Crash") {
655            public void run()
656            {
657                PowerManager.WakeLock wl
658                        = ((PowerManager)NotificationTestList.this.getSystemService(Context.POWER_SERVICE))
659                            .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "crasher");
660                wl.acquire();
661                mHandler.postDelayed(new Runnable() {
662                            public void run() {
663                                throw new RuntimeException("Die!");
664                            }
665                        }, 10000);
666
667            }
668        },
669
670    };
671
672    private Notification notificationWithNumbers(int num) {
673        Notification n = new Notification(this, R.drawable.icon2, null, System.currentTimeMillis(),
674                "Persistent #2", "Notify me!!!", null);
675        n.number = num;
676        return n;
677    }
678
679    private PendingIntent makeIntent() {
680        Intent intent = new Intent(Intent.ACTION_MAIN);
681        intent.addCategory(Intent.CATEGORY_HOME);
682        return PendingIntent.getActivity(this, 0, intent, 0);
683    }
684
685    class StateStress extends Test {
686        StateStress(String name, int pause, int iterations, Runnable[] tasks) {
687            super(name);
688            mPause = pause;
689            mTasks = tasks;
690            mIteration = iterations;
691        }
692        Runnable[] mTasks;
693        int mNext;
694        int mIteration;
695        long mPause;
696        Runnable mRunnable = new Runnable() {
697            public void run() {
698                mTasks[mNext].run();
699                mNext++;
700                if (mNext >= mTasks.length) {
701                    mNext = 0;
702                    mIteration--;
703                    if (mIteration <= 0) {
704                        return;
705                    }
706                }
707                mHandler.postDelayed(mRunnable, mPause);
708            }
709        };
710        public void run() {
711            mNext = 0;
712            mHandler.postDelayed(mRunnable, mPause);
713        }
714    }
715
716    void timeNotification(int n, String label, long time) {
717        mNM.notify(n, new Notification(NotificationTestList.this,
718                    R.drawable.ic_statusbar_missedcall, null,
719                    time, label, "" + new java.util.Date(time), null));
720
721    }
722}
723
724