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