[go: nahoru, domu]

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