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