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