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