Merge "Implement some control over ALLOW_WHILE_IDLE alarms." into mnc-dev
diff --git a/core/java/android/app/Notification.java b/core/java/android/app/Notification.java
index 96c6878..33a47b24 100644
--- a/core/java/android/app/Notification.java
+++ b/core/java/android/app/Notification.java
@@ -1331,11 +1331,14 @@
     public Notification(Context context, int icon, CharSequence tickerText, long when,
             CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
     {
-        this.when = when;
-        this.icon = icon;
-        this.tickerText = tickerText;
-        setLatestEventInfo(context, contentTitle, contentText,
-                PendingIntent.getActivity(context, 0, contentIntent, 0));
+        new Builder(context)
+                .setWhen(when)
+                .setSmallIcon(icon)
+                .setTicker(tickerText)
+                .setContentTitle(contentTitle)
+                .setContentText(contentText)
+                .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
+                .buildInto(this);
     }
 
     /**
diff --git a/core/java/android/app/admin/DevicePolicyManager.java b/core/java/android/app/admin/DevicePolicyManager.java
index 42d0dcb..9f49154 100644
--- a/core/java/android/app/admin/DevicePolicyManager.java
+++ b/core/java/android/app/admin/DevicePolicyManager.java
@@ -149,6 +149,7 @@
      * <li>{@link #EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME}</li>
      * <li>{@link #EXTRA_PROVISIONING_SKIP_ENCRYPTION}, optional</li>
      * <li>{@link #EXTRA_PROVISIONING_LEAVE_ALL_SYSTEM_APPS_ENABLED}, optional</li>
+     * <li>{@link #EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE}, optional</li>
      * </ul>
      *
      * <p> When device owner provisioning has completed, an intent of the type
@@ -163,14 +164,19 @@
         = "android.app.action.PROVISION_MANAGED_DEVICE";
 
     /**
-     * A {@link android.os.Parcelable} extra of type {@link android.os.PersistableBundle} that allows
-     * a mobile device management application that starts managed profile provisioning to pass data
-     * to itself on the managed profile when provisioning completes. The mobile device management
-     * application sends this extra in an intent with the action
-     * {@link #ACTION_PROVISION_MANAGED_PROFILE} and receives it in
+     * A {@link android.os.Parcelable} extra of type {@link android.os.PersistableBundle} that
+     * allows a mobile device management application which starts managed provisioning to pass data
+     * to itself.
+     * <p>
+     * If used with {@link #ACTION_PROVISION_MANAGED_PROFILE} it can be used by the application that
+     * sends the intent to pass data to itself on the newly created profile.
+     * If used with {@link #ACTION_PROVISION_MANAGED_DEVICE} it allows passing data to the same
+     * instance of the app on the primary user.
+     * <p>
+     * In both cases the application receives the data in
      * {@link DeviceAdminReceiver#onProfileProvisioningComplete} via an intent with the action
      * {@link DeviceAdminReceiver#ACTION_PROFILE_PROVISIONING_COMPLETE}. The bundle is not changed
-     * during the managed profile provisioning.
+     * during the managed provisioning.
      */
     public static final String EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE =
             "android.app.extra.PROVISIONING_ADMIN_EXTRAS_BUNDLE";
diff --git a/core/java/android/content/ContentProviderClient.java b/core/java/android/content/ContentProviderClient.java
index e15ac94..d12595f 100644
--- a/core/java/android/content/ContentProviderClient.java
+++ b/core/java/android/content/ContentProviderClient.java
@@ -16,6 +16,8 @@
 
 package android.content;
 
+import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.content.res.AssetFileDescriptor;
 import android.database.Cursor;
 import android.net.Uri;
@@ -30,6 +32,7 @@
 import android.util.Log;
 
 import com.android.internal.annotations.GuardedBy;
+import com.android.internal.util.Preconditions;
 
 import dalvik.system.CloseGuard;
 
@@ -109,14 +112,19 @@
     }
 
     /** See {@link ContentProvider#query ContentProvider.query} */
-    public Cursor query(Uri url, String[] projection, String selection,
-            String[] selectionArgs, String sortOrder) throws RemoteException {
+    public @Nullable Cursor query(@NonNull Uri url, @Nullable String[] projection,
+            @Nullable String selection, @Nullable String[] selectionArgs,
+            @Nullable String sortOrder) throws RemoteException {
         return query(url, projection, selection,  selectionArgs, sortOrder, null);
     }
 
     /** See {@link ContentProvider#query ContentProvider.query} */
-    public Cursor query(Uri url, String[] projection, String selection, String[] selectionArgs,
-            String sortOrder, CancellationSignal cancellationSignal) throws RemoteException {
+    public @Nullable Cursor query(@NonNull Uri url, @Nullable String[] projection,
+            @Nullable String selection, @Nullable String[] selectionArgs,
+            @Nullable String sortOrder, @Nullable CancellationSignal cancellationSignal)
+                    throws RemoteException {
+        Preconditions.checkNotNull(url, "url");
+
         beforeRemote();
         try {
             ICancellationSignal remoteCancellationSignal = null;
@@ -138,7 +146,9 @@
     }
 
     /** See {@link ContentProvider#getType ContentProvider.getType} */
-    public String getType(Uri url) throws RemoteException {
+    public @Nullable String getType(@NonNull Uri url) throws RemoteException {
+        Preconditions.checkNotNull(url, "url");
+
         beforeRemote();
         try {
             return mContentProvider.getType(url);
@@ -153,7 +163,11 @@
     }
 
     /** See {@link ContentProvider#getStreamTypes ContentProvider.getStreamTypes} */
-    public String[] getStreamTypes(Uri url, String mimeTypeFilter) throws RemoteException {
+    public @Nullable String[] getStreamTypes(@NonNull Uri url, @NonNull String mimeTypeFilter)
+            throws RemoteException {
+        Preconditions.checkNotNull(url, "url");
+        Preconditions.checkNotNull(mimeTypeFilter, "mimeTypeFilter");
+
         beforeRemote();
         try {
             return mContentProvider.getStreamTypes(url, mimeTypeFilter);
@@ -168,7 +182,9 @@
     }
 
     /** See {@link ContentProvider#canonicalize} */
-    public final Uri canonicalize(Uri url) throws RemoteException {
+    public final @Nullable Uri canonicalize(@NonNull Uri url) throws RemoteException {
+        Preconditions.checkNotNull(url, "url");
+
         beforeRemote();
         try {
             return mContentProvider.canonicalize(mPackageName, url);
@@ -183,7 +199,9 @@
     }
 
     /** See {@link ContentProvider#uncanonicalize} */
-    public final Uri uncanonicalize(Uri url) throws RemoteException {
+    public final @Nullable Uri uncanonicalize(@NonNull Uri url) throws RemoteException {
+        Preconditions.checkNotNull(url, "url");
+
         beforeRemote();
         try {
             return mContentProvider.uncanonicalize(mPackageName, url);
@@ -198,7 +216,10 @@
     }
 
     /** See {@link ContentProvider#insert ContentProvider.insert} */
-    public Uri insert(Uri url, ContentValues initialValues) throws RemoteException {
+    public @Nullable Uri insert(@NonNull Uri url, @Nullable ContentValues initialValues)
+            throws RemoteException {
+        Preconditions.checkNotNull(url, "url");
+
         beforeRemote();
         try {
             return mContentProvider.insert(mPackageName, url, initialValues);
@@ -213,7 +234,11 @@
     }
 
     /** See {@link ContentProvider#bulkInsert ContentProvider.bulkInsert} */
-    public int bulkInsert(Uri url, ContentValues[] initialValues) throws RemoteException {
+    public int bulkInsert(@NonNull Uri url, @NonNull ContentValues[] initialValues)
+            throws RemoteException {
+        Preconditions.checkNotNull(url, "url");
+        Preconditions.checkNotNull(initialValues, "initialValues");
+
         beforeRemote();
         try {
             return mContentProvider.bulkInsert(mPackageName, url, initialValues);
@@ -228,8 +253,10 @@
     }
 
     /** See {@link ContentProvider#delete ContentProvider.delete} */
-    public int delete(Uri url, String selection, String[] selectionArgs)
-            throws RemoteException {
+    public int delete(@NonNull Uri url, @Nullable String selection,
+            @Nullable String[] selectionArgs) throws RemoteException {
+        Preconditions.checkNotNull(url, "url");
+
         beforeRemote();
         try {
             return mContentProvider.delete(mPackageName, url, selection, selectionArgs);
@@ -244,8 +271,10 @@
     }
 
     /** See {@link ContentProvider#update ContentProvider.update} */
-    public int update(Uri url, ContentValues values, String selection,
-            String[] selectionArgs) throws RemoteException {
+    public int update(@NonNull Uri url, @Nullable ContentValues values, @Nullable String selection,
+            @Nullable String[] selectionArgs) throws RemoteException {
+        Preconditions.checkNotNull(url, "url");
+
         beforeRemote();
         try {
             return mContentProvider.update(mPackageName, url, values, selection, selectionArgs);
@@ -266,7 +295,7 @@
      * you use the {@link ContentResolver#openFileDescriptor
      * ContentResolver.openFileDescriptor} API instead.
      */
-    public ParcelFileDescriptor openFile(Uri url, String mode)
+    public @Nullable ParcelFileDescriptor openFile(@NonNull Uri url, @NonNull String mode)
             throws RemoteException, FileNotFoundException {
         return openFile(url, mode, null);
     }
@@ -278,8 +307,11 @@
      * you use the {@link ContentResolver#openFileDescriptor
      * ContentResolver.openFileDescriptor} API instead.
      */
-    public ParcelFileDescriptor openFile(Uri url, String mode, CancellationSignal signal)
-            throws RemoteException, FileNotFoundException {
+    public @Nullable ParcelFileDescriptor openFile(@NonNull Uri url, @NonNull String mode,
+            @Nullable CancellationSignal signal) throws RemoteException, FileNotFoundException {
+        Preconditions.checkNotNull(url, "url");
+        Preconditions.checkNotNull(mode, "mode");
+
         beforeRemote();
         try {
             ICancellationSignal remoteSignal = null;
@@ -306,7 +338,7 @@
      * you use the {@link ContentResolver#openAssetFileDescriptor
      * ContentResolver.openAssetFileDescriptor} API instead.
      */
-    public AssetFileDescriptor openAssetFile(Uri url, String mode)
+    public @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri url, @NonNull String mode)
             throws RemoteException, FileNotFoundException {
         return openAssetFile(url, mode, null);
     }
@@ -318,8 +350,11 @@
      * you use the {@link ContentResolver#openAssetFileDescriptor
      * ContentResolver.openAssetFileDescriptor} API instead.
      */
-    public AssetFileDescriptor openAssetFile(Uri url, String mode, CancellationSignal signal)
-            throws RemoteException, FileNotFoundException {
+    public @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri url, @NonNull String mode,
+            @Nullable CancellationSignal signal) throws RemoteException, FileNotFoundException {
+        Preconditions.checkNotNull(url, "url");
+        Preconditions.checkNotNull(mode, "mode");
+
         beforeRemote();
         try {
             ICancellationSignal remoteSignal = null;
@@ -340,15 +375,19 @@
     }
 
     /** See {@link ContentProvider#openTypedAssetFile ContentProvider.openTypedAssetFile} */
-    public final AssetFileDescriptor openTypedAssetFileDescriptor(Uri uri,
-            String mimeType, Bundle opts) throws RemoteException, FileNotFoundException {
+    public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
+            @NonNull String mimeType, @Nullable Bundle opts)
+                    throws RemoteException, FileNotFoundException {
         return openTypedAssetFileDescriptor(uri, mimeType, opts, null);
     }
 
     /** See {@link ContentProvider#openTypedAssetFile ContentProvider.openTypedAssetFile} */
-    public final AssetFileDescriptor openTypedAssetFileDescriptor(Uri uri,
-            String mimeType, Bundle opts, CancellationSignal signal)
-            throws RemoteException, FileNotFoundException {
+    public final @Nullable AssetFileDescriptor openTypedAssetFileDescriptor(@NonNull Uri uri,
+            @NonNull String mimeType, @Nullable Bundle opts, @Nullable CancellationSignal signal)
+                    throws RemoteException, FileNotFoundException {
+        Preconditions.checkNotNull(uri, "uri");
+        Preconditions.checkNotNull(mimeType, "mimeType");
+
         beforeRemote();
         try {
             ICancellationSignal remoteSignal = null;
@@ -370,8 +409,11 @@
     }
 
     /** See {@link ContentProvider#applyBatch ContentProvider.applyBatch} */
-    public ContentProviderResult[] applyBatch(ArrayList<ContentProviderOperation> operations)
-            throws RemoteException, OperationApplicationException {
+    public @NonNull ContentProviderResult[] applyBatch(
+            @NonNull ArrayList<ContentProviderOperation> operations)
+                    throws RemoteException, OperationApplicationException {
+        Preconditions.checkNotNull(operations, "operations");
+
         beforeRemote();
         try {
             return mContentProvider.applyBatch(mPackageName, operations);
@@ -386,7 +428,10 @@
     }
 
     /** See {@link ContentProvider#call(String, String, Bundle)} */
-    public Bundle call(String method, String arg, Bundle extras) throws RemoteException {
+    public @Nullable Bundle call(@NonNull String method, @Nullable String arg,
+            @Nullable Bundle extras) throws RemoteException {
+        Preconditions.checkNotNull(method, "method");
+
         beforeRemote();
         try {
             return mContentProvider.call(mPackageName, method, arg, extras);
@@ -436,7 +481,7 @@
      * @return If the associated {@link ContentProvider} is local, returns it.
      * Otherwise returns null.
      */
-    public ContentProvider getLocalContentProvider() {
+    public @Nullable ContentProvider getLocalContentProvider() {
         return ContentProvider.coerceToLocalContentProvider(mContentProvider);
     }
 
diff --git a/core/java/android/os/BatteryStats.java b/core/java/android/os/BatteryStats.java
index a6efc58..f76192e 100644
--- a/core/java/android/os/BatteryStats.java
+++ b/core/java/android/os/BatteryStats.java
@@ -1177,8 +1177,13 @@
         public static final int EVENT_ALARM = 0x000e;
         // Record that we have decided we need to collect new stats data.
         public static final int EVENT_COLLECT_EXTERNAL_STATS = 0x000f;
+        // Event for a package becoming inactive due to being unused for a period of time.
+        public static final int EVENT_PACKAGE_INACTIVE = 0x0010;
+        // Event for a package becoming active due to an interaction.
+        public static final int EVENT_PACKAGE_ACTIVE = 0x0011;
+
         // Number of event types.
-        public static final int EVENT_COUNT = 0x0010;
+        public static final int EVENT_COUNT = 0x0012;
         // Mask to extract out only the type part of the event.
         public static final int EVENT_TYPE_MASK = ~(EVENT_FLAG_START|EVENT_FLAG_FINISH);
 
@@ -1835,12 +1840,12 @@
 
     public static final String[] HISTORY_EVENT_NAMES = new String[] {
             "null", "proc", "fg", "top", "sync", "wake_lock_in", "job", "user", "userfg", "conn",
-            "motion", "active", "pkginst", "pkgunin", "alarm", "stats"
+            "motion", "active", "pkginst", "pkgunin", "alarm", "stats", "inactive", "active"
     };
 
     public static final String[] HISTORY_EVENT_CHECKIN_NAMES = new String[] {
             "Enl", "Epr", "Efg", "Etp", "Esy", "Ewl", "Ejb", "Eur", "Euf", "Ecn",
-            "Esm", "Eac", "Epi", "Epu", "Eal", "Est"
+            "Esm", "Eac", "Epi", "Epu", "Eal", "Est", "Eai", "Eaa"
     };
 
     /**
diff --git a/core/java/android/speech/tts/UtteranceProgressListener.java b/core/java/android/speech/tts/UtteranceProgressListener.java
index 9eb22ef..890ea3d 100644
--- a/core/java/android/speech/tts/UtteranceProgressListener.java
+++ b/core/java/android/speech/tts/UtteranceProgressListener.java
@@ -61,16 +61,16 @@
 
     /**
      * Called when an utterance has been stopped while in progress or flushed from the
-     * synthesis queue. This can happen if client calls {@link TextToSpeech#stop()}
-     * or use {@link TextToSpeech#QUEUE_FLUSH} as an argument in
+     * synthesis queue. This can happen if a client calls {@link TextToSpeech#stop()}
+     * or uses {@link TextToSpeech#QUEUE_FLUSH} as an argument with the
      * {@link TextToSpeech#speak} or {@link TextToSpeech#synthesizeToFile} methods.
      *
      * @param utteranceId the utterance ID of the utterance.
-     * @param isStarted If true, then utterance was interrupted while being synthesized
-     *        and it's output is incomplete. If it's false, then utterance was flushed
+     * @param interrupted If true, then the utterance was interrupted while being synthesized
+     *        and its output is incomplete. If false, then the utterance was flushed
      *        before the synthesis started.
      */
-    public void onStop(String utteranceId, boolean isStarted) {
+    public void onStop(String utteranceId, boolean interrupted) {
     }
 
     /**
@@ -99,7 +99,7 @@
             }
 
             @Override
-            public void onStop(String utteranceId, boolean isStarted) {
+            public void onStop(String utteranceId, boolean interrupted) {
                 listener.onUtteranceCompleted(utteranceId);
             }
         };
diff --git a/core/tests/notificationtests/src/android/app/NotificationStressTest.java b/core/tests/notificationtests/src/android/app/NotificationStressTest.java
index 52ea1c4..4cb617e 100644
--- a/core/tests/notificationtests/src/android/app/NotificationStressTest.java
+++ b/core/tests/notificationtests/src/android/app/NotificationStressTest.java
@@ -77,15 +77,20 @@
     }
 
     private void sendNotification(int id, CharSequence text) {
-        // Create "typical" notification with random icon
-        Notification notification = new Notification(ICONS[mRandom.nextInt(ICONS.length)], text,
-                System.currentTimeMillis());
         // Fill in arbitrary content
         Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
         PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
         CharSequence title = text + " " + id;
         CharSequence subtitle = String.valueOf(System.currentTimeMillis());
-        notification.setLatestEventInfo(mContext, title, subtitle, pendingIntent);
+        // Create "typical" notification with random icon
+        Notification notification = new Notification.Builder(mContext)
+                .setSmallIcon(ICONS[mRandom.nextInt(ICONS.length)])
+                .setTicker(text)
+                .setWhen(System.currentTimeMillis())
+                .setContentTitle(title)
+                .setContentText(subtitle)
+                .setContentIntent(pendingIntent)
+                .build();
         mNotificationManager.notify(id, notification);
         SystemClock.sleep(10);
     }
diff --git a/libs/hwui/RenderNode.cpp b/libs/hwui/RenderNode.cpp
index 4ac4362..75e700a 100644
--- a/libs/hwui/RenderNode.cpp
+++ b/libs/hwui/RenderNode.cpp
@@ -392,11 +392,27 @@
         if (isLayer) {
             clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
         }
-        LOG_ALWAYS_FATAL_IF(!isLayer && properties().getHasOverlappingRendering());
-        renderer.scaleAlpha(properties().getAlpha());
+        if (CC_LIKELY(isLayer || !properties().getHasOverlappingRendering())) {
+            // simply scale rendering content's alpha
+            renderer.scaleAlpha(properties().getAlpha());
+        } else {
+            // savelayer needed to create an offscreen buffer
+            Rect layerBounds(0, 0, getWidth(), getHeight());
+            if (clipFlags) {
+                properties().getClippingRectForFlags(clipFlags, &layerBounds);
+                clipFlags = 0; // all clipping done by savelayer
+            }
+            SaveLayerOp* op = new (handler.allocator()) SaveLayerOp(
+                    layerBounds.left, layerBounds.top,
+                    layerBounds.right, layerBounds.bottom,
+                    (int) (properties().getAlpha() * 255),
+                    SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
+            handler(op, PROPERTY_SAVECOUNT, properties().getClipToBounds());
+        }
 
         if (CC_UNLIKELY(ATRACE_ENABLED() && properties().promotedToLayer())) {
-            // pretend to cause savelayer to warn about performance problem affecting old versions
+            // pretend alpha always causes savelayer to warn about
+            // performance problem affecting old versions
             ATRACE_FORMAT("%s alpha caused saveLayer %dx%d", getName(),
                     static_cast<int>(getWidth()),
                     static_cast<int>(getHeight()));
diff --git a/libs/hwui/RenderProperties.cpp b/libs/hwui/RenderProperties.cpp
index 07b8ce6..4f6ef4e 100644
--- a/libs/hwui/RenderProperties.cpp
+++ b/libs/hwui/RenderProperties.cpp
@@ -152,7 +152,24 @@
             clipFlags &= ~CLIP_TO_BOUNDS; // bounds clipping done by layer
         }
 
-        ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
+        if (CC_LIKELY(isLayer || !getHasOverlappingRendering())) {
+            // simply scale rendering content's alpha
+            ALOGD("%*sScaleAlpha %.2f", level * 2, "", mPrimitiveFields.mAlpha);
+        } else {
+            // savelayeralpha to create an offscreen buffer to apply alpha
+            Rect layerBounds(0, 0, getWidth(), getHeight());
+            if (clipFlags) {
+                getClippingRectForFlags(clipFlags, &layerBounds);
+                clipFlags = 0; // all clipping done by savelayer
+            }
+            ALOGD("%*sSaveLayerAlpha %d, %d, %d, %d, %d, 0x%x", level * 2, "",
+                    (int)layerBounds.left, (int)layerBounds.top,
+                    (int)layerBounds.right, (int)layerBounds.bottom,
+                    (int)(mPrimitiveFields.mAlpha * 255),
+                    SkCanvas::kHasAlphaLayer_SaveFlag | SkCanvas::kClipToLayer_SaveFlag);
+        }
+
+
     }
     if (clipFlags) {
         Rect clipRect;
diff --git a/libs/hwui/RenderProperties.h b/libs/hwui/RenderProperties.h
index 98029a8..65c1c4a 100644
--- a/libs/hwui/RenderProperties.h
+++ b/libs/hwui/RenderProperties.h
@@ -28,6 +28,7 @@
 #include <SkRegion.h>
 #include <SkXfermode.h>
 
+#include "Caches.h"
 #include "Rect.h"
 #include "RevealClip.h"
 #include "Outline.h"
@@ -577,10 +578,13 @@
     }
 
     bool promotedToLayer() const {
+        const int maxTextureSize = Caches::getInstance().maxTextureSize;
         return mLayerProperties.mType == LayerType::None
                 && !MathUtils::isZero(mPrimitiveFields.mAlpha)
                 && mPrimitiveFields.mAlpha < 1
-                && mPrimitiveFields.mHasOverlappingRendering;
+                && mPrimitiveFields.mHasOverlappingRendering
+                && mPrimitiveFields.mWidth <= maxTextureSize
+                && mPrimitiveFields.mHeight <= maxTextureSize;
     }
 
     LayerType effectiveLayerType() const {
diff --git a/location/java/com/android/internal/location/GpsNetInitiatedHandler.java b/location/java/com/android/internal/location/GpsNetInitiatedHandler.java
index 95b3eb3..260f380 100644
--- a/location/java/com/android/internal/location/GpsNetInitiatedHandler.java
+++ b/location/java/com/android/internal/location/GpsNetInitiatedHandler.java
@@ -172,7 +172,7 @@
      * <p>
      * This is lazily created, so use {@link #setNINotification()}.
      */
-    private Notification mNiNotification;
+    private Notification.Builder mNiNotificationBuilder;
 
     public GpsNetInitiatedHandler(Context context,
                                   INetInitiatedListener netInitiatedListener,
@@ -367,29 +367,31 @@
                 ", message: " + message);
 
         // Construct Notification
-        if (mNiNotification == null) {
-            mNiNotification = new Notification();
-            mNiNotification.icon = com.android.internal.R.drawable.stat_sys_gps_on; /* Change notification icon here */
-            mNiNotification.when = 0;
+        if (mNiNotificationBuilder == null) {
+            mNiNotificationBuilder = new Notification.Builder(mContext)
+                    .setSmallIcon(com.android.internal.R.drawable.stat_sys_gps_on)
+                    .setWhen(0)
+                    .setOngoing(true)
+                    .setAutoCancel(true)
+                    .setColor(mContext.getColor(
+                            com.android.internal.R.color.system_notification_accent_color));
         }
 
         if (mPlaySounds) {
-            mNiNotification.defaults |= Notification.DEFAULT_SOUND;
+            mNiNotificationBuilder.setDefaults(Notification.DEFAULT_SOUND);
         } else {
-            mNiNotification.defaults &= ~Notification.DEFAULT_SOUND;
+            mNiNotificationBuilder.setDefaults(0);
         }
 
-        mNiNotification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_AUTO_CANCEL;
-        mNiNotification.tickerText = getNotifTicker(notif, mContext);
-
         // if not to popup dialog immediately, pending intent will open the dialog
         Intent intent = !mPopupImmediately ? getDlgIntent(notif) : new Intent();
         PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, intent, 0);
-        mNiNotification.color = mContext.getColor(
-                com.android.internal.R.color.system_notification_accent_color);
-        mNiNotification.setLatestEventInfo(mContext, title, message, pi);
+        mNiNotificationBuilder.setTicker(getNotifTicker(notif, mContext))
+                .setContentTitle(title)
+                .setContentText(message)
+                .setContentIntent(pi);
 
-        notificationManager.notifyAsUser(null, notif.notificationId, mNiNotification,
+        notificationManager.notifyAsUser(null, notif.notificationId, mNiNotificationBuilder.build(),
                 UserHandle.ALL);
     }
 
diff --git a/services/core/java/com/android/server/BluetoothManagerService.java b/services/core/java/com/android/server/BluetoothManagerService.java
index 66fd36f..fbb6dc9 100644
--- a/services/core/java/com/android/server/BluetoothManagerService.java
+++ b/services/core/java/com/android/server/BluetoothManagerService.java
@@ -21,8 +21,8 @@
 import android.bluetooth.BluetoothAdapter;
 import android.bluetooth.BluetoothProfile;
 import android.bluetooth.IBluetooth;
-import android.bluetooth.IBluetoothGatt;
 import android.bluetooth.IBluetoothCallback;
+import android.bluetooth.IBluetoothGatt;
 import android.bluetooth.IBluetoothHeadset;
 import android.bluetooth.IBluetoothManager;
 import android.bluetooth.IBluetoothManagerCallback;
@@ -37,6 +37,7 @@
 import android.content.ServiceConnection;
 import android.content.pm.PackageManager;
 import android.content.pm.UserInfo;
+import android.database.ContentObserver;
 import android.os.Binder;
 import android.os.Handler;
 import android.os.IBinder;
@@ -56,11 +57,8 @@
 import java.io.FileDescriptor;
 import java.io.IOException;
 import java.io.PrintWriter;
-
 import java.util.HashMap;
 import java.util.Map;
-
-import java.util.*;
 class BluetoothManagerService extends IBluetoothManager.Stub {
     private static final String TAG = "BluetoothManagerService";
     private static final boolean DBG = true;
@@ -259,6 +257,8 @@
         mName = null;
         mErrorRecoveryRetryCounter = 0;
         mContentResolver = context.getContentResolver();
+        // Observe BLE scan only mode settings change.
+        registerForBleScanModeChange();
         mCallbacks = new RemoteCallbackList<IBluetoothManagerCallback>();
         mStateChangeCallbacks = new RemoteCallbackList<IBluetoothStateChangeCallback>();
         IntentFilter filter = new IntentFilter(Intent.ACTION_BOOT_COMPLETED);
@@ -458,6 +458,40 @@
         return false;
     }
 
+    // Monitor change of BLE scan only mode settings.
+    private void registerForBleScanModeChange() {
+        ContentObserver contentObserver = new ContentObserver(null) {
+            @Override
+            public void onChange(boolean selfChange) {
+                if (!isBleScanAlwaysAvailable()) {
+                    disableBleScanMode();
+                    clearBleApps();
+                    try {
+                        if (mBluetooth != null) mBluetooth.onBrEdrDown();
+                    } catch (RemoteException e) {
+                        Log.e(TAG, "error when disabling bluetooth", e);
+                    }
+                }
+            }
+        };
+
+        mContentResolver.registerContentObserver(
+                Settings.Global.getUriFor(Settings.Global.BLE_SCAN_ALWAYS_AVAILABLE),
+                false, contentObserver);
+    }
+
+    // Disable ble scan only mode.
+    private void disableBleScanMode() {
+        try {
+            if (mBluetooth != null && (mBluetooth.getState() != BluetoothAdapter.STATE_ON)) {
+                if (DBG) Log.d(TAG, "Reseting the mEnable flag for clean disable");
+                mEnable = false;
+            }
+        } catch (RemoteException e) {
+            Log.e(TAG, "getState()", e);
+        }
+    }
+
     public int updateBleAppCount(IBinder token, boolean enable) {
         if (enable) {
             ClientDeathRecipient r = mBleApps.get(token);
@@ -478,11 +512,8 @@
         } else  {
             ClientDeathRecipient r = mBleApps.get(token);
             if (r != null) {
-                try {
-                    token.linkToDeath(r, 0);
-                } catch (RemoteException ex) {
-                    throw new IllegalArgumentException("Wake lock is already dead.");
-                }
+                // Unregister death recipient as the app goes away.
+                token.unlinkToDeath(r, 0);
                 mBleApps.remove(token);
                 synchronized (this) {
                     if (mBleAppCount > 0) --mBleAppCount;
@@ -492,18 +523,19 @@
         }
         if (DBG) Log.d(TAG, "Updated BleAppCount" + mBleAppCount);
         if (mBleAppCount == 0 && mEnable) {
-            try {
-                if (mBluetooth != null && (mBluetooth.getState() != BluetoothAdapter.STATE_ON)) {
-                    if (DBG) Log.d(TAG, "Reseting the mEnable flag for clean disable");
-                    mEnable = false;
-                }
-            } catch (RemoteException e) {
-                Log.e(TAG, "getState()", e);
-            }
+            disableBleScanMode();
         }
         return mBleAppCount;
     }
 
+    // Clear all apps using BLE scan only mode.
+    private void clearBleApps() {
+        synchronized (this) {
+            mBleApps.clear();
+            mBleAppCount = 0;
+        }
+    }
+
     /** @hide*/
     public boolean isBleAppPresent() {
         if (DBG) Log.d(TAG, "isBleAppPresent() count: " + mBleAppCount);
diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java
index 9c6e16f..f645764 100644
--- a/services/core/java/com/android/server/ConnectivityService.java
+++ b/services/core/java/com/android/server/ConnectivityService.java
@@ -3289,7 +3289,6 @@
             CharSequence title;
             CharSequence details;
             int icon;
-            Notification notification = new Notification();
             if (notifyType == NotificationType.NO_INTERNET &&
                     networkType == ConnectivityManager.TYPE_WIFI) {
                 title = r.getString(R.string.wifi_no_internet, 0);
@@ -3324,14 +3323,17 @@
                 return;
             }
 
-            notification.when = 0;
-            notification.icon = icon;
-            notification.flags = Notification.FLAG_AUTO_CANCEL;
-            notification.tickerText = title;
-            notification.color = mContext.getColor(
-                    com.android.internal.R.color.system_notification_accent_color);
-            notification.setLatestEventInfo(mContext, title, details, notification.contentIntent);
-            notification.contentIntent = intent;
+            Notification notification = new Notification.Builder(mContext)
+                    .setWhen(0)
+                    .setSmallIcon(icon)
+                    .setAutoCancel(true)
+                    .setTicker(title)
+                    .setColor(mContext.getColor(
+                            com.android.internal.R.color.system_notification_accent_color))
+                    .setContentTitle(title)
+                    .setContentText(details)
+                    .setContentIntent(intent)
+                    .build();
 
             try {
                 notificationManager.notify(NOTIFICATION_ID, id, notification);
diff --git a/services/core/java/com/android/server/InputMethodManagerService.java b/services/core/java/com/android/server/InputMethodManagerService.java
index 6e6fb7f..a5d536e 100644
--- a/services/core/java/com/android/server/InputMethodManagerService.java
+++ b/services/core/java/com/android/server/InputMethodManagerService.java
@@ -71,6 +71,7 @@
 import android.inputmethodservice.InputMethodService;
 import android.net.Uri;
 import android.os.Binder;
+import android.os.Bundle;
 import android.os.Environment;
 import android.os.Handler;
 import android.os.IBinder;
@@ -212,7 +213,7 @@
     private NotificationManager mNotificationManager;
     private KeyguardManager mKeyguardManager;
     private StatusBarManagerService mStatusBar;
-    private Notification mImeSwitcherNotification;
+    private Notification.Builder mImeSwitcherNotification;
     private PendingIntent mImeSwitchPendingIntent;
     private boolean mShowOngoingImeSwitcherForPhones;
     private boolean mNotificationShown;
@@ -798,18 +799,15 @@
         mHasFeature = context.getPackageManager().hasSystemFeature(
                 PackageManager.FEATURE_INPUT_METHODS);
 
-        mImeSwitcherNotification = new Notification();
-        mImeSwitcherNotification.icon = com.android.internal.R.drawable.ic_notification_ime_default;
-        mImeSwitcherNotification.when = 0;
-        mImeSwitcherNotification.flags = Notification.FLAG_ONGOING_EVENT;
-        mImeSwitcherNotification.tickerText = null;
-        mImeSwitcherNotification.defaults = 0; // please be quiet
-        mImeSwitcherNotification.sound = null;
-        mImeSwitcherNotification.vibrate = null;
-
-        // Tag this notification specially so SystemUI knows it's important
-        mImeSwitcherNotification.extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
-        mImeSwitcherNotification.category = Notification.CATEGORY_SYSTEM;
+        Bundle extras = new Bundle();
+        extras.putBoolean(Notification.EXTRA_ALLOW_DURING_SETUP, true);
+        mImeSwitcherNotification = new Notification.Builder(mContext)
+            .setSmallIcon(com.android.internal.R.drawable.ic_notification_ime_default)
+            .setWhen(0)
+            .setOngoing(true)
+            .addExtras(extras)
+            .setCategory(Notification.CATEGORY_SYSTEM)
+            .setColor(com.android.internal.R.color.system_notification_accent_color);
 
         Intent intent = new Intent(Settings.ACTION_SHOW_INPUT_METHOD_PICKER);
         mImeSwitchPendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0);
@@ -1766,11 +1764,9 @@
                         com.android.internal.R.string.select_input_method);
                 final CharSequence summary = InputMethodUtils.getImeAndSubtypeDisplayName(
                         mContext, imi, mCurrentSubtype);
-
-                mImeSwitcherNotification.color = mContext.getColor(
-                        com.android.internal.R.color.system_notification_accent_color);
-                mImeSwitcherNotification.setLatestEventInfo(
-                        mContext, title, summary, mImeSwitchPendingIntent);
+                mImeSwitcherNotification.setContentTitle(title)
+                        .setContentText(summary)
+                        .setContentIntent(mImeSwitchPendingIntent);
                 if ((mNotificationManager != null)
                         && !mWindowManagerService.hasNavigationBar()) {
                     if (DEBUG) {
@@ -1778,7 +1774,7 @@
                     }
                     mNotificationManager.notifyAsUser(null,
                             com.android.internal.R.string.select_input_method,
-                            mImeSwitcherNotification, UserHandle.ALL);
+                            mImeSwitcherNotification.build(), UserHandle.ALL);
                     mNotificationShown = true;
                 }
             } else {
diff --git a/services/core/java/com/android/server/UiModeManagerService.java b/services/core/java/com/android/server/UiModeManagerService.java
index 64f3070..0b67ad8 100644
--- a/services/core/java/com/android/server/UiModeManagerService.java
+++ b/services/core/java/com/android/server/UiModeManagerService.java
@@ -601,21 +601,22 @@
             if (mCarModeEnabled) {
                 Intent carModeOffIntent = new Intent(context, DisableCarModeActivity.class);
 
-                Notification n = new Notification();
-                n.icon = R.drawable.stat_notify_car_mode;
-                n.defaults = Notification.DEFAULT_LIGHTS;
-                n.flags = Notification.FLAG_ONGOING_EVENT;
-                n.when = 0;
-                n.color = context.getColor(
-                        com.android.internal.R.color.system_notification_accent_color);
-                n.setLatestEventInfo(
-                        context,
-                        context.getString(R.string.car_mode_disable_notification_title),
-                        context.getString(R.string.car_mode_disable_notification_message),
-                        PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
-                                null, UserHandle.CURRENT));
+                Notification.Builder n = new Notification.Builder(context)
+                        .setSmallIcon(R.drawable.stat_notify_car_mode)
+                        .setDefaults(Notification.DEFAULT_LIGHTS)
+                        .setOngoing(true)
+                        .setWhen(0)
+                        .setColor(context.getColor(
+                                com.android.internal.R.color.system_notification_accent_color))
+                        .setContentTitle(
+                                context.getString(R.string.car_mode_disable_notification_title))
+                        .setContentText(
+                                context.getString(R.string.car_mode_disable_notification_message))
+                        .setContentIntent(
+                                PendingIntent.getActivityAsUser(context, 0, carModeOffIntent, 0,
+                                        null, UserHandle.CURRENT));
                 mNotificationManager.notifyAsUser(null,
-                        R.string.car_mode_disable_notification_title, n, UserHandle.ALL);
+                        R.string.car_mode_disable_notification_title, n.build(), UserHandle.ALL);
             } else {
                 mNotificationManager.cancelAsUser(null,
                         R.string.car_mode_disable_notification_title, UserHandle.ALL);
diff --git a/services/core/java/com/android/server/accounts/AccountManagerService.java b/services/core/java/com/android/server/accounts/AccountManagerService.java
index 49d9988..3456dbc 100644
--- a/services/core/java/com/android/server/accounts/AccountManagerService.java
+++ b/services/core/java/com/android/server/accounts/AccountManagerService.java
@@ -2005,8 +2005,6 @@
         String authTokenLabel = intent.getStringExtra(
                 GrantCredentialsPermissionActivity.EXTRAS_AUTH_TOKEN_LABEL);
 
-        Notification n = new Notification(android.R.drawable.stat_sys_warning, null,
-                0 /* when */);
         final String titleAndSubtitle =
                 mContext.getString(R.string.permission_request_notification_with_subtitle,
                 account.name);
@@ -2019,11 +2017,16 @@
         }
         UserHandle user = new UserHandle(userId);
         Context contextForUser = getContextForUser(user);
-        n.color = contextForUser.getColor(
-                com.android.internal.R.color.system_notification_accent_color);
-        n.setLatestEventInfo(contextForUser, title, subtitle,
-                PendingIntent.getActivityAsUser(mContext, 0, intent,
-                        PendingIntent.FLAG_CANCEL_CURRENT, null, user));
+        Notification n = new Notification.Builder(contextForUser)
+                .setSmallIcon(android.R.drawable.stat_sys_warning)
+                .setWhen(0)
+                .setColor(contextForUser.getColor(
+                        com.android.internal.R.color.system_notification_accent_color))
+                .setContentTitle(title)
+                .setContentText(subtitle)
+                .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0, intent,
+                        PendingIntent.FLAG_CANCEL_CURRENT, null, user))
+                .build();
         installNotification(getCredentialPermissionNotificationId(
                 account, authTokenType, uid), n, user);
     }
@@ -3542,19 +3545,21 @@
             } else {
                 final Integer notificationId = getSigninRequiredNotificationId(accounts, account);
                 intent.addCategory(String.valueOf(notificationId));
-                Notification n = new Notification(android.R.drawable.stat_sys_warning, null,
-                        0 /* when */);
                 UserHandle user = new UserHandle(userId);
                 Context contextForUser = getContextForUser(user);
                 final String notificationTitleFormat =
                         contextForUser.getText(R.string.notification_title).toString();
-                n.color = contextForUser.getColor(
-                        com.android.internal.R.color.system_notification_accent_color);
-                n.setLatestEventInfo(contextForUser,
-                        String.format(notificationTitleFormat, account.name),
-                        message, PendingIntent.getActivityAsUser(
-                        mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT,
-                        null, user));
+                Notification n = new Notification.Builder(contextForUser)
+                        .setWhen(0)
+                        .setSmallIcon(android.R.drawable.stat_sys_warning)
+                        .setColor(contextForUser.getColor(
+                                com.android.internal.R.color.system_notification_accent_color))
+                        .setContentTitle(String.format(notificationTitleFormat, account.name))
+                        .setContentText(message)
+                        .setContentIntent(PendingIntent.getActivityAsUser(
+                                mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT,
+                                null, user))
+                        .build();
                 installNotification(notificationId, n, user);
             }
         } finally {
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index 029a3b2..421ba86 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -1714,22 +1714,20 @@
                     Context context = mContext.createPackageContext(process.info.packageName, 0);
                     String text = mContext.getString(R.string.heavy_weight_notification,
                             context.getApplicationInfo().loadLabel(context.getPackageManager()));
-                    Notification notification = new Notification();
-                    notification.icon = com.android.internal.R.drawable.stat_sys_adb; //context.getApplicationInfo().icon;
-                    notification.when = 0;
-                    notification.flags = Notification.FLAG_ONGOING_EVENT;
-                    notification.tickerText = text;
-                    notification.defaults = 0; // please be quiet
-                    notification.sound = null;
-                    notification.vibrate = null;
-                    notification.color = mContext.getColor(
-                            com.android.internal.R.color.system_notification_accent_color);
-                    notification.setLatestEventInfo(context, text,
-                            mContext.getText(R.string.heavy_weight_notification_detail),
-                            PendingIntent.getActivityAsUser(mContext, 0, root.intent,
-                                    PendingIntent.FLAG_CANCEL_CURRENT, null,
-                                    new UserHandle(root.userId)));
-
+                    Notification notification = new Notification.Builder(context)
+                            .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb)
+                            .setWhen(0)
+                            .setOngoing(true)
+                            .setTicker(text)
+                            .setColor(mContext.getColor(
+                                    com.android.internal.R.color.system_notification_accent_color))
+                            .setContentTitle(text)
+                            .setContentText(
+                                    mContext.getText(R.string.heavy_weight_notification_detail))
+                            .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0,
+                                    root.intent, PendingIntent.FLAG_CANCEL_CURRENT, null,
+                                    new UserHandle(root.userId)))
+                            .build();
                     try {
                         int[] outId = new int[1];
                         inm.enqueueNotificationWithTag("android", "android", null,
@@ -1948,20 +1946,10 @@
                 }
 
                 String text = mContext.getString(R.string.dump_heap_notification, procName);
-                Notification notification = new Notification();
-                notification.icon = com.android.internal.R.drawable.stat_sys_adb;
-                notification.when = 0;
-                notification.flags = Notification.FLAG_ONGOING_EVENT|Notification.FLAG_AUTO_CANCEL;
-                notification.tickerText = text;
-                notification.defaults = 0; // please be quiet
-                notification.sound = null;
-                notification.vibrate = null;
-                notification.color = mContext.getColor(
-                        com.android.internal.R.color.system_notification_accent_color);
+
+
                 Intent deleteIntent = new Intent();
                 deleteIntent.setAction(DumpHeapActivity.ACTION_DELETE_DUMPHEAP);
-                notification.deleteIntent = PendingIntent.getBroadcastAsUser(mContext, 0,
-                        deleteIntent, 0, UserHandle.OWNER);
                 Intent intent = new Intent();
                 intent.setClassName("android", DumpHeapActivity.class.getName());
                 intent.putExtra(DumpHeapActivity.KEY_PROCESS, procName);
@@ -1970,11 +1958,23 @@
                     intent.putExtra(DumpHeapActivity.KEY_DIRECT_LAUNCH, reportPackage);
                 }
                 int userId = UserHandle.getUserId(uid);
-                notification.setLatestEventInfo(mContext, text,
-                        mContext.getText(R.string.dump_heap_notification_detail),
-                        PendingIntent.getActivityAsUser(mContext, 0, intent,
-                                PendingIntent.FLAG_CANCEL_CURRENT, null,
-                                new UserHandle(userId)));
+                Notification notification = new Notification.Builder(mContext)
+                        .setSmallIcon(com.android.internal.R.drawable.stat_sys_adb)
+                        .setWhen(0)
+                        .setOngoing(true)
+                        .setAutoCancel(true)
+                        .setTicker(text)
+                        .setColor(mContext.getColor(
+                                com.android.internal.R.color.system_notification_accent_color))
+                        .setContentTitle(text)
+                        .setContentText(
+                                mContext.getText(R.string.dump_heap_notification_detail))
+                        .setContentIntent(PendingIntent.getActivityAsUser(mContext, 0,
+                                intent, PendingIntent.FLAG_CANCEL_CURRENT, null,
+                                new UserHandle(userId)))
+                        .setDeleteIntent(PendingIntent.getBroadcastAsUser(mContext, 0,
+                                deleteIntent, 0, UserHandle.OWNER))
+                        .build();
 
                 try {
                     int[] outId = new int[1];
diff --git a/services/core/java/com/android/server/connectivity/Tethering.java b/services/core/java/com/android/server/connectivity/Tethering.java
index 897300f..c1aaf07 100644
--- a/services/core/java/com/android/server/connectivity/Tethering.java
+++ b/services/core/java/com/android/server/connectivity/Tethering.java
@@ -130,7 +130,8 @@
 
     private StateMachine mTetherMasterSM;
 
-    private Notification mTetheredNotification;
+    private Notification.Builder mTetheredNotificationBuilder;
+    private int mLastNotificationId;
 
     private boolean mRndisEnabled;       // track the RNDIS function enabled state
     private boolean mUsbTetherRequested; // true if USB tethering should be started
@@ -450,12 +451,13 @@
             return;
         }
 
-        if (mTetheredNotification != null) {
-            if (mTetheredNotification.icon == icon) {
+        if (mLastNotificationId != 0) {
+            if (mLastNotificationId == icon) {
                 return;
             }
-            notificationManager.cancelAsUser(null, mTetheredNotification.icon,
+            notificationManager.cancelAsUser(null, mLastNotificationId,
                     UserHandle.ALL);
+            mLastNotificationId = 0;
         }
 
         Intent intent = new Intent();
@@ -470,31 +472,32 @@
         CharSequence message = r.getText(com.android.internal.R.string.
                 tethered_notification_message);
 
-        if (mTetheredNotification == null) {
-            mTetheredNotification = new Notification();
-            mTetheredNotification.when = 0;
+        if (mTetheredNotificationBuilder == null) {
+            mTetheredNotificationBuilder = new Notification.Builder(mContext);
+            mTetheredNotificationBuilder.setWhen(0)
+                    .setOngoing(true)
+                    .setColor(mContext.getColor(
+                            com.android.internal.R.color.system_notification_accent_color))
+                    .setVisibility(Notification.VISIBILITY_PUBLIC)
+                    .setCategory(Notification.CATEGORY_STATUS);
         }
-        mTetheredNotification.icon = icon;
-        mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
-        mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
-        mTetheredNotification.tickerText = title;
-        mTetheredNotification.visibility = Notification.VISIBILITY_PUBLIC;
-        mTetheredNotification.color = mContext.getColor(
-                com.android.internal.R.color.system_notification_accent_color);
-        mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);
-        mTetheredNotification.category = Notification.CATEGORY_STATUS;
+        mTetheredNotificationBuilder.setSmallIcon(icon)
+                .setContentTitle(title)
+                .setContentText(message)
+                .setContentIntent(pi);
+        mLastNotificationId = icon;
 
-        notificationManager.notifyAsUser(null, mTetheredNotification.icon,
-                mTetheredNotification, UserHandle.ALL);
+        notificationManager.notifyAsUser(null, mLastNotificationId,
+                mTetheredNotificationBuilder.build(), UserHandle.ALL);
     }
 
     private void clearTetheredNotification() {
         NotificationManager notificationManager =
             (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
-        if (notificationManager != null && mTetheredNotification != null) {
-            notificationManager.cancelAsUser(null, mTetheredNotification.icon,
+        if (notificationManager != null && mLastNotificationId != 0) {
+            notificationManager.cancelAsUser(null, mLastNotificationId,
                     UserHandle.ALL);
-            mTetheredNotification = null;
+            mLastNotificationId = 0;
         }
     }
 
diff --git a/services/core/java/com/android/server/content/SyncManager.java b/services/core/java/com/android/server/content/SyncManager.java
index 3dc282b..f222dba 100644
--- a/services/core/java/com/android/server/content/SyncManager.java
+++ b/services/core/java/com/android/server/content/SyncManager.java
@@ -3260,16 +3260,18 @@
                     R.string.contentServiceTooManyDeletesNotificationDesc);
 
             Context contextForUser = getContextForUser(user);
-            Notification notification =
-                new Notification(R.drawable.stat_notify_sync_error,
-                        mContext.getString(R.string.contentServiceSync),
-                        System.currentTimeMillis());
-            notification.color = contextForUser.getColor(
-                    com.android.internal.R.color.system_notification_accent_color);
-            notification.setLatestEventInfo(contextForUser,
-                    contextForUser.getString(R.string.contentServiceSyncNotificationTitle),
-                    String.format(tooManyDeletesDescFormat.toString(), authorityName),
-                    pendingIntent);
+            Notification notification = new Notification.Builder(contextForUser)
+                    .setSmallIcon(R.drawable.stat_notify_sync_error)
+                    .setTicker(mContext.getString(R.string.contentServiceSync))
+                    .setWhen(System.currentTimeMillis())
+                    .setColor(contextForUser.getColor(
+                            com.android.internal.R.color.system_notification_accent_color))
+                    .setContentTitle(contextForUser.getString(
+                            R.string.contentServiceSyncNotificationTitle))
+                    .setContentText(
+                            String.format(tooManyDeletesDescFormat.toString(), authorityName))
+                    .setContentIntent(pendingIntent)
+                    .build();
             notification.flags |= Notification.FLAG_ONGOING_EVENT;
             mNotificationMgr.notifyAsUser(null, account.hashCode() ^ authority.hashCode(),
                     notification, user);
diff --git a/services/usage/java/com/android/server/usage/AppIdleHistory.java b/services/usage/java/com/android/server/usage/AppIdleHistory.java
index 9d3db16..e3c0868 100644
--- a/services/usage/java/com/android/server/usage/AppIdleHistory.java
+++ b/services/usage/java/com/android/server/usage/AppIdleHistory.java
@@ -27,34 +27,27 @@
  */
 public class AppIdleHistory {
 
-    private SparseArray<ArrayMap<String,byte[]>> idleHistory = new SparseArray<>();
+    private SparseArray<ArrayMap<String,byte[]>> mIdleHistory = new SparseArray<>();
     private long lastPeriod = 0;
     private static final long ONE_MINUTE = 60 * 1000;
     private static final int HISTORY_SIZE = 100;
     private static final int FLAG_LAST_STATE = 2;
     private static final int FLAG_PARTIAL_ACTIVE = 1;
-    private static final long PERIOD_DURATION = UsageStatsService.DEBUG ? ONE_MINUTE
+    private static final long PERIOD_DURATION = UsageStatsService.COMPRESS_TIME ? ONE_MINUTE
             : 60 * ONE_MINUTE;
 
     public void addEntry(String packageName, int userId, boolean idle, long timeNow) {
-        ArrayMap<String, byte[]> userHistory = idleHistory.get(userId);
-        if (userHistory == null) {
-            userHistory = new ArrayMap<>();
-            idleHistory.put(userId, userHistory);
-        }
-        byte[] packageHistory = userHistory.get(packageName);
-        if (packageHistory == null) {
-            packageHistory = new byte[HISTORY_SIZE];
-            userHistory.put(packageName, packageHistory);
-        }
+        ArrayMap<String, byte[]> userHistory = getUserHistory(userId);
+        byte[] packageHistory = getPackageHistory(userHistory, packageName);
+
         long thisPeriod = timeNow / PERIOD_DURATION;
         // Has the period switched over? Slide all users' package histories
         if (lastPeriod != 0 && lastPeriod < thisPeriod
                 && (thisPeriod - lastPeriod) < HISTORY_SIZE - 1) {
             int diff = (int) (thisPeriod - lastPeriod);
-            final int NUSERS = idleHistory.size();
+            final int NUSERS = mIdleHistory.size();
             for (int u = 0; u < NUSERS; u++) {
-                userHistory = idleHistory.valueAt(u);
+                userHistory = mIdleHistory.valueAt(u);
                 for (byte[] history : userHistory.values()) {
                     // Shift left
                     System.arraycopy(history, diff, history, 0, HISTORY_SIZE - diff);
@@ -74,12 +67,36 @@
         }
     }
 
+    private ArrayMap<String, byte[]> getUserHistory(int userId) {
+        ArrayMap<String, byte[]> userHistory = mIdleHistory.get(userId);
+        if (userHistory == null) {
+            userHistory = new ArrayMap<>();
+            mIdleHistory.put(userId, userHistory);
+        }
+        return userHistory;
+    }
+
+    private byte[] getPackageHistory(ArrayMap<String, byte[]> userHistory, String packageName) {
+        byte[] packageHistory = userHistory.get(packageName);
+        if (packageHistory == null) {
+            packageHistory = new byte[HISTORY_SIZE];
+            userHistory.put(packageName, packageHistory);
+        }
+        return packageHistory;
+    }
+
     public void removeUser(int userId) {
-        idleHistory.remove(userId);
+        mIdleHistory.remove(userId);
+    }
+
+    public boolean isIdle(int userId, String packageName) {
+        ArrayMap<String, byte[]> userHistory = getUserHistory(userId);
+        byte[] packageHistory = getPackageHistory(userHistory, packageName);
+        return (packageHistory[HISTORY_SIZE - 1] & FLAG_LAST_STATE) == 0;
     }
 
     public void dump(IndentingPrintWriter idpw, int userId) {
-        ArrayMap<String, byte[]> userHistory = idleHistory.get(userId);
+        ArrayMap<String, byte[]> userHistory = mIdleHistory.get(userId);
         if (userHistory == null) return;
         final int P = userHistory.size();
         for (int p = 0; p < P; p++) {
diff --git a/services/usage/java/com/android/server/usage/UsageStatsService.java b/services/usage/java/com/android/server/usage/UsageStatsService.java
index 7a34757..8e1895e 100644
--- a/services/usage/java/com/android/server/usage/UsageStatsService.java
+++ b/services/usage/java/com/android/server/usage/UsageStatsService.java
@@ -43,6 +43,7 @@
 import android.hardware.display.DisplayManager;
 import android.net.Uri;
 import android.os.BatteryManager;
+import android.os.BatteryStats;
 import android.os.Binder;
 import android.os.Environment;
 import android.os.Handler;
@@ -65,6 +66,7 @@
 import android.view.Display;
 
 import com.android.internal.annotations.GuardedBy;
+import com.android.internal.app.IBatteryStats;
 import com.android.internal.os.BackgroundThread;
 import com.android.internal.util.IndentingPrintWriter;
 import com.android.server.DeviceIdleController;
@@ -91,7 +93,7 @@
     static final String TAG = "UsageStatsService";
 
     static final boolean DEBUG = false;
-    private static final boolean COMPRESS_TIME = false;
+    static final boolean COMPRESS_TIME = false;
 
     private static final long TEN_SECONDS = 10 * 1000;
     private static final long ONE_MINUTE = 60 * 1000;
@@ -128,6 +130,7 @@
     IDeviceIdleController mDeviceIdleController;
     private DisplayManager mDisplayManager;
     private PowerManager mPowerManager;
+    private IBatteryStats mBatteryStats;
 
     private final SparseArray<UserUsageStatsService> mUserState = new SparseArray<>();
     private File mUsageStatsDir;
@@ -200,6 +203,8 @@
             mAppWidgetManager = getContext().getSystemService(AppWidgetManager.class);
             mDeviceIdleController = IDeviceIdleController.Stub.asInterface(
                     ServiceManager.getService(DeviceIdleController.SERVICE_NAME));
+            mBatteryStats = IBatteryStats.Stub.asInterface(
+                    ServiceManager.getService(BatteryStats.SERVICE_NAME));
             mDisplayManager = (DisplayManager) getContext().getSystemService(
                     Context.DISPLAY_SERVICE);
             mPowerManager = getContext().getSystemService(PowerManager.class);
@@ -228,7 +233,7 @@
                 }
             } else if (Intent.ACTION_USER_STARTED.equals(intent.getAction())) {
                 if (userId >=0) {
-                    postCheckIdleStates();
+                    postCheckIdleStates(userId);
                 }
             }
         }
@@ -307,7 +312,7 @@
                     mLastAppIdleParoledTime = checkAndGetTimeLocked();
                     postNextParoleTimeout();
                 }
-                postCheckIdleStates();
+                postCheckIdleStates(UserHandle.USER_ALL);
             }
         }
     }
@@ -332,22 +337,25 @@
         mHandler.sendEmptyMessageDelayed(MSG_PAROLE_END_TIMEOUT, DEFAULT_PAROLE_DURATION);
     }
 
-    void postCheckIdleStates() {
-        mHandler.removeMessages(MSG_CHECK_IDLE_STATES);
-        mHandler.sendEmptyMessage(MSG_CHECK_IDLE_STATES);
+    void postCheckIdleStates(int userId) {
+        mHandler.sendMessage(mHandler.obtainMessage(MSG_CHECK_IDLE_STATES, userId, 0));
     }
 
-    /** Check all running users' apps to see if they enter an idle state. */
-    void checkIdleStates() {
-        final int[] runningUsers;
+    /** Check all running users' or specified user's apps to see if they enter an idle state. */
+    void checkIdleStates(int checkUserId) {
+        final int[] userIds;
         try {
-            runningUsers = ActivityManagerNative.getDefault().getRunningUserIds();
+            if (checkUserId == UserHandle.USER_ALL) {
+                userIds = ActivityManagerNative.getDefault().getRunningUserIds();
+            } else {
+                userIds = new int[] { checkUserId };
+            }
         } catch (RemoteException re) {
             return;
         }
 
-        for (int i = 0; i < runningUsers.length; i++) {
-            final int userId = runningUsers[i];
+        for (int i = 0; i < userIds.length; i++) {
+            final int userId = userIds[i];
             List<PackageInfo> packages =
                     getContext().getPackageManager().getInstalledPackages(
                             PackageManager.GET_DISABLED_COMPONENTS
@@ -355,17 +363,22 @@
                             userId);
             synchronized (mLock) {
                 final long timeNow = checkAndGetTimeLocked();
+                final long screenOnTime = getScreenOnTimeLocked(timeNow);
+                UserUsageStatsService service = getUserDataAndInitializeIfNeededLocked(userId,
+                        timeNow);
                 final int packageCount = packages.size();
                 for (int p = 0; p < packageCount; p++) {
                     final String packageName = packages.get(p).packageName;
-                    final boolean isIdle = isAppIdleFiltered(packageName, userId, timeNow);
+                    final boolean isIdle = isAppIdleFiltered(packageName, userId, service, timeNow,
+                            screenOnTime);
                     mHandler.sendMessage(mHandler.obtainMessage(MSG_INFORM_LISTENERS,
                             userId, isIdle ? 1 : 0, packageName));
                     mAppIdleHistory.addEntry(packageName, userId, isIdle, timeNow);
                 }
             }
         }
-        mHandler.sendEmptyMessageDelayed(MSG_CHECK_IDLE_STATES, mCheckIdleIntervalMillis);
+        mHandler.sendMessageDelayed(mHandler.obtainMessage(MSG_CHECK_IDLE_STATES, checkUserId, 0),
+                mCheckIdleIntervalMillis);
     }
 
     /** Check if it's been a while since last parole and let idle apps do some work */
@@ -386,6 +399,20 @@
         }
     }
 
+    private void notifyBatteryStats(String packageName, int userId, boolean idle) {
+        try {
+            int uid = AppGlobals.getPackageManager().getPackageUid(packageName, userId);
+            if (idle) {
+                mBatteryStats.noteEvent(BatteryStats.HistoryItem.EVENT_PACKAGE_INACTIVE,
+                        packageName, uid);
+            } else {
+                mBatteryStats.noteEvent(BatteryStats.HistoryItem.EVENT_PACKAGE_ACTIVE,
+                        packageName, uid);
+            }
+        } catch (RemoteException re) {
+        }
+    }
+
     void updateDisplayLocked() {
         boolean screenOn = mDisplayManager.getDisplay(Display.DEFAULT_DISPLAY).getState()
                 == Display.STATE_ON;
@@ -545,7 +572,7 @@
             final long lastUsedTime = service.getSystemLastUsedTime(event.mPackage);
             final boolean previouslyIdle = hasPassedIdleTimeoutLocked(beginIdleTime,
                     lastUsedTime, screenOnTime, timeNow);
-            service.reportEvent(event, getScreenOnTimeLocked(timeNow));
+            service.reportEvent(event, screenOnTime);
             // Inform listeners if necessary
             if ((event.mEventType == Event.MOVE_TO_FOREGROUND
                     || event.mEventType == Event.MOVE_TO_BACKGROUND
@@ -555,6 +582,7 @@
                     // Slog.d(TAG, "Informing listeners of out-of-idle " + event.mPackage);
                     mHandler.sendMessage(mHandler.obtainMessage(MSG_INFORM_LISTENERS, userId,
                             /* idle = */ 0, event.mPackage));
+                    notifyBatteryStats(event.mPackage, userId, false);
                     mAppIdleHistory.addEntry(event.mPackage, userId, false, timeNow);
                 }
             }
@@ -586,6 +614,9 @@
                 // Slog.d(TAG, "Informing listeners of out-of-idle " + event.mPackage);
                 mHandler.sendMessage(mHandler.obtainMessage(MSG_INFORM_LISTENERS, userId,
                         /* idle = */ idle ? 1 : 0, packageName));
+                if (!idle) {
+                    notifyBatteryStats(packageName, userId, idle);
+                }
                 mAppIdleHistory.addEntry(packageName, userId, idle, timeNow);
             }
         }
@@ -660,19 +691,20 @@
         }
     }
 
-    private boolean isAppIdleUnfiltered(String packageName, int userId, long timeNow) {
+    private boolean isAppIdleUnfiltered(String packageName, UserUsageStatsService userService,
+            long timeNow, long screenOnTime) {
         synchronized (mLock) {
-            final long screenOnTime = getScreenOnTimeLocked(timeNow);
-            final UserUsageStatsService service =
-                    getUserDataAndInitializeIfNeededLocked(userId, timeNow);
-            long beginIdleTime = service.getBeginIdleTime(packageName);
-            long lastUsedTime = service.getSystemLastUsedTime(packageName);
-            return hasPassedIdleTimeoutLocked(beginIdleTime, lastUsedTime, screenOnTime, timeNow);
+            long beginIdleTime = userService.getBeginIdleTime(packageName);
+            long lastUsedTime = userService.getSystemLastUsedTime(packageName);
+            return hasPassedIdleTimeoutLocked(beginIdleTime, lastUsedTime, screenOnTime,
+                    timeNow);
         }
     }
 
     /**
-     * @param timestamp when the app was last used in device usage timebase
+     * @param beginIdleTime when the app was last used in device usage timebase
+     * @param lastUsedTime wallclock time of when the app was last used
+     * @param screenOnTime screen-on timebase time
      * @param currentTime current time in device usage timebase
      * @return whether it's been used far enough in the past to be considered inactive
      */
@@ -696,18 +728,29 @@
         }
     }
 
+    boolean isAppIdleFiltered(String packageName, int userId, long timeNow) {
+        final UserUsageStatsService userService;
+        final long screenOnTime;
+        synchronized (mLock) {
+            if (timeNow == -1) {
+                timeNow = checkAndGetTimeLocked();
+            }
+            userService = getUserDataAndInitializeIfNeededLocked(userId, timeNow);
+            screenOnTime = getScreenOnTimeLocked(timeNow);
+        }
+        return isAppIdleFiltered(packageName, userId, userService, timeNow, screenOnTime);
+    }
+
     /**
      * Checks if an app has been idle for a while and filters out apps that are excluded.
      * It returns false if the current system state allows all apps to be considered active.
      * This happens if the device is plugged in or temporarily allowed to make exceptions.
      * Called by interface impls.
      */
-    boolean isAppIdleFiltered(String packageName, int userId, long timeNow) {
+    private boolean isAppIdleFiltered(String packageName, int userId,
+            UserUsageStatsService userService, long timeNow, long screenOnTime) {
         if (packageName == null) return false;
         synchronized (mLock) {
-            if (timeNow == -1) {
-                timeNow = checkAndGetTimeLocked();
-            }
             // Temporary exemption, probably due to device charging or occasional allowance to
             // be allowed to sync, etc.
             if (mAppIdleParoled) {
@@ -735,7 +778,7 @@
             return false;
         }
 
-        return isAppIdleUnfiltered(packageName, userId, timeNow);
+        return isAppIdleUnfiltered(packageName, userService, timeNow, screenOnTime);
     }
 
     void setAppIdle(String packageName, boolean idle, int userId) {
@@ -844,7 +887,7 @@
                     break;
 
                 case MSG_CHECK_IDLE_STATES:
-                    checkIdleStates();
+                    checkIdleStates(msg.arg1);
                     break;
 
                 case MSG_CHECK_PAROLE_TIMEOUT:
@@ -884,7 +927,7 @@
                     UserHandle.USER_OWNER);
             mCheckIdleIntervalMillis = Math.min(DEFAULT_CHECK_IDLE_INTERVAL,
                     mAppIdleDurationMillis / 4);
-            postCheckIdleStates();
+            postCheckIdleStates(UserHandle.USER_ALL);
         }
     }
 
diff --git a/telecomm/java/android/telecom/Call.java b/telecomm/java/android/telecom/Call.java
index a2e0706..e756a57 100644
--- a/telecomm/java/android/telecom/Call.java
+++ b/telecomm/java/android/telecom/Call.java
@@ -380,7 +380,7 @@
                 builder.append(" PROPERTY_HIGH_DEF_AUDIO");
             }
             if (hasProperty(properties, PROPERTY_EMERGENCY_CALLBACK_MODE)) {
-                builder.append(" EMERGENCY_CALLBACK_MODE");
+                builder.append(" PROPERTY_EMERGENCY_CALLBACK_MODE");
             }
             builder.append("]");
             return builder.toString();
diff --git a/tests/FixVibrateSetting/src/com/android/fixvibratesetting/FixVibrateSetting.java b/tests/FixVibrateSetting/src/com/android/fixvibratesetting/FixVibrateSetting.java
index 947ea78..2e51570 100644
--- a/tests/FixVibrateSetting/src/com/android/fixvibratesetting/FixVibrateSetting.java
+++ b/tests/FixVibrateSetting/src/com/android/fixvibratesetting/FixVibrateSetting.java
@@ -109,14 +109,20 @@
     }
 
     private void test() {
-        Notification n = new Notification(R.drawable.stat_sys_warning, "Test notification",
-                        System.currentTimeMillis());
         Intent intent = new Intent(this, FixVibrateSetting.class);
         PendingIntent pending = PendingIntent.getActivity(this, 0, intent, 0);
-        n.setLatestEventInfo(this, "Test notification", "Test notification", pending);
 
-        n.vibrate = new long[] { 0, 700, 500, 1000 };
-        n.flags |= Notification.FLAG_AUTO_CANCEL;
+        Notification n = new Notification.Builder(this)
+                .setSmallIcon(R.drawable.stat_sys_warning)
+                .setTicker("Test notification")
+                .setWhen(System.currentTimeMillis())
+                .setContentTitle("Test notification")
+                .setContentText("Test notification")
+                .setContentIntent(pending)
+                .setVibrate(new long[] { 0, 700, 500, 1000 })
+                .setAutoCancel(true)
+                .build();
+
         mNotificationManager.notify(1, n);
     }
 }
diff --git a/tests/FrameworkPerf/src/com/android/frameworkperf/SchedulerService.java b/tests/FrameworkPerf/src/com/android/frameworkperf/SchedulerService.java
index 7691e64..fc3f390 100644
--- a/tests/FrameworkPerf/src/com/android/frameworkperf/SchedulerService.java
+++ b/tests/FrameworkPerf/src/com/android/frameworkperf/SchedulerService.java
@@ -26,15 +26,18 @@
 
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
-        Notification status = new Notification(R.drawable.stat_happy, null,
-                System.currentTimeMillis());
-        status.flags |= Notification.FLAG_ONGOING_EVENT;
-        status.setLatestEventInfo(this, "Scheduler Test running",
-                "Scheduler Test running", PendingIntent.getActivity(this, 0,
-                    new Intent(this, FrameworkPerfActivity.class)
-                    .setAction(Intent.ACTION_MAIN)
-                    .addCategory(Intent.CATEGORY_LAUNCHER)
-                    .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0));
+        Notification status = new Notification.Builder(this)
+                .setSmallIcon(R.drawable.stat_happy)
+                .setWhen(System.currentTimeMillis())
+                .setContentTitle("Scheduler Test running")
+                .setContentText("Scheduler Test running")
+                .setContentIntent(PendingIntent.getActivity(this, 0,
+                        new Intent(this, FrameworkPerfActivity.class)
+                                .setAction(Intent.ACTION_MAIN)
+                                .addCategory(Intent.CATEGORY_LAUNCHER)
+                                .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK), 0))
+                .setOngoing(true)
+                .build();
         startForeground(1, status);
         return START_STICKY;
     }
diff --git a/tests/StatusBar/res/drawable-hdpi/stat_sys_warning.png b/tests/StatusBar/res/drawable-hdpi/stat_sys_warning.png
new file mode 100644
index 0000000..dbaf944
--- /dev/null
+++ b/tests/StatusBar/res/drawable-hdpi/stat_sys_warning.png
Binary files differ
diff --git a/tests/StatusBar/res/drawable-mdpi/stat_sys_warning.png b/tests/StatusBar/res/drawable-mdpi/stat_sys_warning.png
new file mode 100644
index 0000000..168f8f6
--- /dev/null
+++ b/tests/StatusBar/res/drawable-mdpi/stat_sys_warning.png
Binary files differ
diff --git a/tests/StatusBar/src/com/android/statusbartest/NotificationTestList.java b/tests/StatusBar/src/com/android/statusbartest/NotificationTestList.java
index ba160b18..67b9d77 100644
--- a/tests/StatusBar/src/com/android/statusbartest/NotificationTestList.java
+++ b/tests/StatusBar/src/com/android/statusbartest/NotificationTestList.java
@@ -25,7 +25,6 @@
 import android.graphics.Bitmap;
 import android.graphics.drawable.BitmapDrawable;
 import android.os.Bundle;
-import android.os.Environment;
 import android.os.Vibrator;
 import android.os.Handler;
 import android.os.UserHandle;
@@ -85,7 +84,7 @@
     }
 
     private Test[] mTests = new Test[] {
-        new Test("Off and sound") {
+        new Test("Off") {
             public void run() {
                 PowerManager pm = (PowerManager)NotificationTestList.this.getSystemService(Context.POWER_SERVICE);
                 PowerManager.WakeLock wl = 
@@ -94,9 +93,12 @@
 
                 pm.goToSleep(SystemClock.uptimeMillis());
 
-                Notification n = new Notification();
-                n.sound = Uri.parse("file://" + Environment.getExternalStorageDirectory() +
-                        "/virtual-void.mp3");
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.stat_sys_phone)
+                        .setContentTitle(name)
+                        .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
+                                        getPackageName() + "/raw/ringer"))
+                        .build();
                 Log.d(TAG, "n.sound=" + n.sound);
 
                 mNM.notify(1, n);
@@ -114,122 +116,120 @@
             }
         },
 
-        new Test("Button") {
+        new Test("Custom Button") {
             public void run() {
-                Notification n = new Notification(R.drawable.icon1, null,
-                        mActivityCreateTime);
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon1)
+                        .setWhen(mActivityCreateTime)
+                        .setContentTitle(name)
+                        .setOngoing(true)
+                        .build();
                 n.contentView = new RemoteViews(getPackageName(), R.layout.button_notification);
-                n.flags |= Notification.FLAG_ONGOING_EVENT;
-                n.contentIntent = makeIntent();
                 n.contentView.setOnClickPendingIntent(R.id.button, makeIntent2());
 
                 mNM.notify(1, n);
             }
         },
 
-        new Test("custom intent on text view") {
+        new Test("Action Button") {
             public void run() {
-                Notification n = new Notification(R.drawable.icon1, null,
-                        mActivityCreateTime);
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
-                            "This is a notification!!!", null);
-                n.contentView.setOnClickPendingIntent(com.android.internal.R.id.text,
-                        makeIntent2());
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon1)
+                        .setWhen(mActivityCreateTime)
+                        .setContentTitle(name)
+                        .setOngoing(true)
+                        .addAction(R.drawable.ic_statusbar_chat, "Button", makeIntent2())
+                        .build();
+
                 mNM.notify(1, n);
             }
         },
 
-        new Test("Ticker 1 line") {
+        new Test("with intent") {
             public void run() {
-                Notification n = new Notification(R.drawable.icon1, "tick tick tick",
-                        mActivityCreateTime);
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
-                            "This is a notification!!!", makeIntent());
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon1)
+                        .setWhen(mActivityCreateTime)
+                        .setContentTitle("Persistent #1")
+                        .setContentText("This is a notification!!!")
+                        .setContentIntent(makeIntent2())
+                        .setOngoing(true)
+                        .build();
+
                 mNM.notify(1, n);
             }
         },
 
-        new Test("No view") {
-            public void run() {
-                Notification n = new Notification(R.drawable.icon1, "No view",
-                        System.currentTimeMillis());
-                mNM.notify(1, n);
-            }
-        },
-
-        new Test("No intent") {
-            public void run() {
-                Notification n = new Notification(R.drawable.icon1, "No intent",
-                        System.currentTimeMillis());
-                n.setLatestEventInfo(NotificationTestList.this, "No intent",
-                            "No intent", null);
-                mNM.notify(1, n);
-            }
-        },
-
-        new Test("Layout") {
+        new Test("Whens") {
             public void run()
             {
-                Notification n;
+                Notification.Builder n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon1)
+                        .setContentTitle(name)
+                        .setOngoing(true);
 
-                n = new Notification(NotificationTestList.this,
-                            R.drawable.ic_statusbar_missedcall,
-                            null, System.currentTimeMillis()-(1000*60*60*24),
-                            "(453) 123-2328",
-                            "", null);
-                n.flags |= Notification.FLAG_ONGOING_EVENT;
+                mNM.notify(1, n.setContentTitle("(453) 123-2328")
+                .setWhen(System.currentTimeMillis()-(1000*60*60*24))
+                .build());
 
-                mNM.notify(1, n);
+                mNM.notify(1, n.setContentTitle("Mark Willem, Me (2)")
+                .setWhen(System.currentTimeMillis())
+                .build());
 
-                mNM.notify(2, new Notification(NotificationTestList.this,
-                            R.drawable.ic_statusbar_email,
-                            null, System.currentTimeMillis(),
-                            "Mark Willem, Me (2)",
-                            "Re: Didn't you get the memo?", null));
-
-                mNM.notify(3, new Notification(NotificationTestList.this,
-                            R.drawable.ic_statusbar_chat,
-                            null, System.currentTimeMillis()+(1000*60*60*24),
-                            "Sophia Winterlanden",
-                            "Lorem ipsum dolor sit amet.", null));
+                mNM.notify(1, n.setContentTitle("Sophia Winterlanden")
+                        .setWhen(System.currentTimeMillis() + (1000 * 60 * 60 * 24))
+                        .build());
             }
         },
 
         new Test("Bad Icon #1 (when=create)") {
             public void run() {
-                Notification n = new Notification(R.layout.chrono_notification /* not an icon */,
-                        null, mActivityCreateTime);
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
-                            "This is the same notification!!!", makeIntent());
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.layout.chrono_notification /* not an icon */)
+                        .setWhen(mActivityCreateTime)
+                        .setContentTitle("Persistent #1")
+                        .setContentText("This is the same notification!!")
+                        .setContentIntent(makeIntent())
+                        .build();
                 mNM.notify(1, n);
             }
         },
 
         new Test("Bad Icon #1 (when=now)") {
             public void run() {
-                Notification n = new Notification(R.layout.chrono_notification /* not an icon */,
-                        null, System.currentTimeMillis());
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
-                            "This is the same notification!!!", makeIntent());
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.layout.chrono_notification /* not an icon */)
+                        .setWhen(System.currentTimeMillis())
+                        .setContentTitle("Persistent #1")
+                        .setContentText("This is the same notification!!")
+                        .setContentIntent(makeIntent())
+                        .build();
                 mNM.notify(1, n);
             }
         },
 
         new Test("Null Icon #1 (when=now)") {
             public void run() {
-                Notification n = new Notification(0, null, System.currentTimeMillis());
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
-                            "This is the same notification!!!", makeIntent());
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(0)
+                        .setWhen(System.currentTimeMillis())
+                        .setContentTitle("Persistent #1")
+                        .setContentText("This is the same notification!!")
+                        .setContentIntent(makeIntent())
+                        .build();
                 mNM.notify(1, n);
             }
         },
 
         new Test("Bad resource #1 (when=create)") {
             public void run() {
-                Notification n = new Notification(R.drawable.icon2,
-                        null, mActivityCreateTime);
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
-                            "This is the same notification!!!", makeIntent());
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon2)
+                        .setWhen(mActivityCreateTime)
+                        .setContentTitle("Persistent #1")
+                        .setContentText("This is the same notification!!")
+                        .setContentIntent(makeIntent())
+                        .build();
                 n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
                 mNM.notify(1, n);
             }
@@ -237,29 +237,18 @@
 
         new Test("Bad resource #1 (when=now)") {
             public void run() {
-                Notification n = new Notification(R.drawable.icon2,
-                        null, System.currentTimeMillis());
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
-                            "This is the same notification!!!", makeIntent());
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon2)
+                        .setWhen(System.currentTimeMillis())
+                        .setContentTitle("Persistent #1")
+                        .setContentText("This is the same notification!!")
+                        .setContentIntent(makeIntent())
+                        .build();
                 n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
                 mNM.notify(1, n);
             }
         },
 
-
-        new Test("Bad resource #3") {
-            public void run()
-            {
-                Notification n = new Notification(NotificationTestList.this,
-                            R.drawable.ic_statusbar_missedcall,
-                            null, System.currentTimeMillis()-(1000*60*60*24),
-                            "(453) 123-2328",
-                            "", null);
-                n.contentView.setInt(1 /*bogus*/, "bogus method", 666);
-                mNM.notify(3, n);
-            }
-        },
-
         new Test("Times") {
             public void run()
             {
@@ -278,22 +267,25 @@
                 new Runnable() {
                     public void run() {
                         Log.d(TAG, "Stress - Ongoing/Latest 0");
-                        Notification n = new Notification(NotificationTestList.this,
-                                R.drawable.icon3,
-                                null, System.currentTimeMillis(), "Stress - Ongoing",
-                                "Notify me!!!", null);
-                        n.flags |= Notification.FLAG_ONGOING_EVENT;
+                        Notification n = new Notification.Builder(NotificationTestList.this)
+                                .setSmallIcon(R.drawable.icon3)
+                                .setWhen(System.currentTimeMillis())
+                                .setContentTitle("Stress - Ongoing")
+                                .setContentText("Notify me!!!")
+                                .setOngoing(true)
+                                .build();
                         mNM.notify(1, n);
                     }
                 },
                 new Runnable() {
                     public void run() {
                         Log.d(TAG, "Stress - Ongoing/Latest 1");
-                        Notification n = new Notification(NotificationTestList.this,
-                                R.drawable.icon4,
-                                null, System.currentTimeMillis(), "Stress - Latest",
-                                "Notify me!!!", null);
-                        //n.flags |= Notification.FLAG_ONGOING_EVENT;
+                        Notification n = new Notification.Builder(NotificationTestList.this)
+                                .setSmallIcon(R.drawable.icon4)
+                                .setWhen(System.currentTimeMillis())
+                                .setContentTitle("Stress - Latest")
+                                .setContentText("Notify me!!!")
+                                .build();
                         mNM.notify(1, n);
                     }
                 }
@@ -302,12 +294,15 @@
         new Test("Long") {
             public void run()
             {
-                Notification n = new Notification();
-                n.defaults |= Notification.DEFAULT_SOUND ;
-                n.vibrate = new long[] {
-                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
-                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 
-                        300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 };
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon1)
+                        .setContentTitle(name)
+                        .setDefaults(Notification.DEFAULT_SOUND)
+                        .setVibrate(new long[] {
+                                300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
+                                300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400,
+                                300, 400, 300, 400, 300, 400, 300, 400, 300, 400, 300, 400 })
+                        .build();
                 mNM.notify(1, n);
             }
         },
@@ -320,21 +315,19 @@
                 Thread t = new Thread() {
                     public void run() {
                         int x = 0;
+                        final Notification.Builder n = new Notification.Builder(NotificationTestList.this)
+                                .setSmallIcon(R.drawable.icon1)
+                                .setContentTitle(name)
+                                .setOngoing(true);
+
                         while (!mProgressDone) {
-                            Notification n = new Notification(R.drawable.icon1, null,
-                                    PROGRESS_UPDATES_WHEN
+                            n.setWhen(PROGRESS_UPDATES_WHEN
                                     ? System.currentTimeMillis()
                                     : mActivityCreateTime);
-                            RemoteViews v = new RemoteViews(getPackageName(),
-                                    R.layout.progress_notification);
-                            
-                            v.setProgressBar(R.id.progress_bar, 100, x, false);
-                            v.setTextViewText(R.id.status_text, "Progress: " + x + "%");
-                    
-                            n.contentView = v;
-                            n.flags |= Notification.FLAG_ONGOING_EVENT;
+                            n.setProgress(100, x, false);
+                            n.setContentText("Progress: " + x + "%");
 
-                            mNM.notify(500, n);
+                            mNM.notify(500, n.build());
                             x = (x + 7) % 100;
 
                             try {
@@ -359,11 +352,12 @@
         new Test("Blue Lights") {
             public void run()
             {
-                Notification n = new Notification();
-                n.flags |= Notification.FLAG_SHOW_LIGHTS;
-                n.ledARGB = 0xff0000ff;
-                n.ledOnMS = 1;
-                n.ledOffMS = 0;
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon2)
+                        .setContentTitle(name)
+                        .setLights(0xff0000ff, 1, 0)
+                        .setDefaults(Notification.DEFAULT_LIGHTS)
+                        .build();
                 mNM.notify(1, n);
             }
         },
@@ -371,11 +365,12 @@
         new Test("Red Lights") {
             public void run()
             {
-                Notification n = new Notification();
-                n.flags |= Notification.FLAG_SHOW_LIGHTS;
-                n.ledARGB = 0xffff0000;
-                n.ledOnMS = 1;
-                n.ledOffMS = 0;
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon2)
+                        .setContentTitle(name)
+                        .setLights(0xffff0000, 1, 0)
+                        .setDefaults(Notification.DEFAULT_LIGHTS)
+                        .build();
                 mNM.notify(1, n);
             }
         },
@@ -383,11 +378,12 @@
         new Test("Yellow Lights") {
             public void run()
             {
-                Notification n = new Notification();
-                n.flags |= Notification.FLAG_SHOW_LIGHTS;
-                n.ledARGB = 0xffffff00;
-                n.ledOnMS = 1;
-                n.ledOffMS = 0;
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon2)
+                        .setContentTitle(name)
+                        .setLights(0xffffff00, 1, 0)
+                        .setDefaults(Notification.DEFAULT_LIGHTS)
+                        .build();
                 mNM.notify(1, n);
             }
         },
@@ -395,11 +391,12 @@
         new Test("Lights off") {
             public void run()
             {
-                Notification n = new Notification();
-                n.flags |= Notification.FLAG_SHOW_LIGHTS;
-                n.ledARGB = 0x00000000;
-                n.ledOnMS = 0;
-                n.ledOffMS = 0;
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon2)
+                        .setContentTitle(name)
+                        .setLights(0x00000000, 0, 0)
+                        .setDefaults(Notification.DEFAULT_LIGHTS)
+                        .build();
                 mNM.notify(1, n);
             }
         },
@@ -407,11 +404,12 @@
         new Test("Blue Blinking Slow") {
             public void run()
             {
-                Notification n = new Notification();
-                n.flags |= Notification.FLAG_SHOW_LIGHTS;
-                n.ledARGB = 0xff0000ff;
-                n.ledOnMS = 1300;
-                n.ledOffMS = 1300;
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon2)
+                        .setContentTitle(name)
+                        .setLights(0xff0000ff, 1300, 1300)
+                        .setDefaults(Notification.DEFAULT_LIGHTS)
+                        .build();
                 mNM.notify(1, n);
             }
         },
@@ -419,11 +417,12 @@
         new Test("Blue Blinking Fast") {
             public void run()
             {
-                Notification n = new Notification();
-                n.flags |= Notification.FLAG_SHOW_LIGHTS;
-                n.ledARGB = 0xff0000ff;
-                n.ledOnMS = 300;
-                n.ledOffMS = 300;
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon2)
+                        .setContentTitle(name)
+                        .setLights(0xff0000ff, 300, 300)
+                        .setDefaults(Notification.DEFAULT_LIGHTS)
+                        .build();
                 mNM.notify(1, n);
             }
         },
@@ -431,8 +430,11 @@
         new Test("Default All") {
             public void run()
             {
-                Notification n = new Notification();
-                n.defaults |= Notification.DEFAULT_ALL;
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon2)
+                        .setContentTitle(name)
+                        .setDefaults(Notification.DEFAULT_ALL)
+                        .build();
                 mNM.notify(1, n);
             }
         },
@@ -440,20 +442,12 @@
         new Test("Default All, once") {
             public void run()
             {
-                Notification n = new Notification();
-                n.defaults |= Notification.DEFAULT_ALL;
-                n.flags |= Notification.FLAG_ONLY_ALERT_ONCE ;
-                mNM.notify(1, n);
-            }
-        },
-
-        new Test("Content Sound") {
-            public void run()
-            {
-                Notification n = new Notification();
-                n.sound = Uri.parse(
-                        "content://media/internal/audio/media/7");
-
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon2)
+                        .setContentTitle(name)
+                        .setOnlyAlertOnce(true)
+                        .setDefaults(Notification.DEFAULT_ALL)
+                        .build();
                 mNM.notify(1, n);
             }
         },
@@ -461,10 +455,12 @@
         new Test("Resource Sound") {
             public void run()
             {
-                Notification n = new Notification();
-                n.sound = Uri.parse(
-                        ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
-                        getPackageName() + "/raw/ringer");
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.stat_sys_phone)
+                        .setContentTitle(name)
+                        .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
+                                getPackageName() + "/raw/ringer"))
+                        .build();
                 Log.d(TAG, "n.sound=" + n.sound);
 
                 mNM.notify(1, n);
@@ -474,9 +470,13 @@
         new Test("Sound and Cancel") {
             public void run()
             {
-                Notification n = new Notification();
-                n.sound = Uri.parse(
-                            "content://media/internal/audio/media/7");
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.stat_sys_phone)
+                        .setContentTitle(name)
+                        .setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
+                                getPackageName() + "/raw/ringer"))
+                        .build();
+                Log.d(TAG, "n.sound=" + n.sound);
 
                 mNM.notify(1, n);
                 SystemClock.sleep(200);
@@ -487,8 +487,11 @@
         new Test("Vibrate") {
             public void run()
             {
-                Notification n = new Notification();
-                    n.vibrate = new long[] { 0, 700, 500, 1000 };
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.stat_sys_phone)
+                        .setContentTitle(name)
+                        .setVibrate(new long[]{0, 700, 500, 1000})
+                        .build();
 
                 mNM.notify(1, n);
             }
@@ -497,8 +500,11 @@
         new Test("Vibrate and cancel") {
             public void run()
             {
-                Notification n = new Notification();
-                    n.vibrate = new long[] { 0, 700, 500, 1000 };
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.stat_sys_phone)
+                        .setContentTitle(name)
+                        .setVibrate(new long[]{0, 700, 500, 1000})
+                        .build();
 
                 mNM.notify(1, n);
                 SystemClock.sleep(500);
@@ -566,10 +572,13 @@
 
         new Test("Persistent #1") {
             public void run() {
-                Notification n = new Notification(R.drawable.icon1, "tick tick tick",
-                        mActivityCreateTime);
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
-                            "This is a notification!!!", makeIntent());
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon1)
+                        .setWhen(mActivityCreateTime)
+                        .setContentTitle(name)
+                        .setContentText("This is a notification!!!")
+                        .setContentIntent(makeIntent())
+                        .build();
                 mNM.notify(1, n);
             }
         },
@@ -578,18 +587,19 @@
             public void run() {
                 mHandler.postDelayed(new Runnable() {
                             public void run() {
-                                Notification n = new Notification(R.drawable.icon1,
-                                        "            "
+                                String message = "            "
                                         + "tick tock tick tock\n\nSometimes notifications can "
                                         + "be really long and wrap to more than one line.\n"
                                         + "Sometimes."
                                         + "Ohandwhathappensifwehaveonereallylongstringarewesure"
-                                        + "thatwesegmentitcorrectly?\n",
-                                        System.currentTimeMillis());
-                                n.setLatestEventInfo(NotificationTestList.this,
-                                        "Still Persistent #1",
-                                        "This is still a notification!!!",
-                                        makeIntent());
+                                        + "thatwesegmentitcorrectly?\n";
+                                Notification n = new Notification.Builder(NotificationTestList.this)
+                                        .setSmallIcon(R.drawable.icon1)
+                                        .setContentTitle(name)
+                                        .setContentText("This is still a notification!!!")
+                                        .setContentIntent(makeIntent())
+                                        .setStyle(new Notification.BigTextStyle().bigText(message))
+                                        .build();
                                 mNM.notify(1, n);
                             }
                         }, 3000);
@@ -598,54 +608,67 @@
 
         new Test("Persistent #2") {
             public void run() {
-                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
-                        System.currentTimeMillis());
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #2",
-                            "Notify me!!!", makeIntent());
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon1)
+                        .setWhen(mActivityCreateTime)
+                        .setContentTitle(name)
+                        .setContentText("This is a notification!!!")
+                        .setContentIntent(makeIntent())
+                        .build();
                 mNM.notify(2, n);
             }
         },
 
         new Test("Persistent #3") {
             public void run() {
-                Notification n = new Notification(R.drawable.icon2, "tock tock tock\nmooooo",
-                        System.currentTimeMillis());
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #3",
-                            "Notify me!!!", makeIntent());
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon1)
+                        .setWhen(mActivityCreateTime)
+                        .setContentTitle(name)
+                        .setContentText("This is a notification!!!")
+                        .setContentIntent(makeIntent())
+                        .build();
                 mNM.notify(3, n);
             }
         },
 
         new Test("Persistent #2 Vibrate") {
             public void run() {
-                Notification n = new Notification(R.drawable.icon2, "tock tock tock",
-                        System.currentTimeMillis());
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #2",
-                            "Notify me!!!", makeIntent());
-                n.defaults = Notification.DEFAULT_VIBRATE;
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon1)
+                        .setWhen(mActivityCreateTime)
+                        .setContentTitle(name)
+                        .setContentText("This is a notification!!!")
+                        .setContentIntent(makeIntent())
+                        .setDefaults(Notification.DEFAULT_VIBRATE)
+                        .build();
                 mNM.notify(2, n);
             }
         },
 
         new Test("Persistent #1 - different icon") {
             public void run() {
-                Notification n = new Notification(R.drawable.icon2, null,
-                        mActivityCreateTime);
-                n.setLatestEventInfo(NotificationTestList.this, "Persistent #1",
-                            "This is the same notification!!!", makeIntent());
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon2)
+                        .setWhen(mActivityCreateTime)
+                        .setContentTitle(name)
+                        .setContentText("This is a notification!!!")
+                        .setContentIntent(makeIntent())
+                        .build();
                 mNM.notify(1, n);
             }
         },
 
         new Test("Chronometer Start") {
             public void run() {
-                Notification n = new Notification(R.drawable.icon2, "me me me me",
-                                                    System.currentTimeMillis());
-                n.contentView = new RemoteViews(getPackageName(), R.layout.chrono_notification);
-                mChronometerBase = SystemClock.elapsedRealtime();
-                n.contentView.setChronometer(R.id.time, mChronometerBase, "Yay! (%s)", true);
-                n.flags |= Notification.FLAG_ONGOING_EVENT;
-                n.contentIntent = makeIntent();
+                Notification n = new Notification.Builder(NotificationTestList.this)
+                        .setSmallIcon(R.drawable.icon1)
+                        .setWhen(System.currentTimeMillis())
+                        .setContentTitle(name)
+                        .setContentIntent(makeIntent())
+                        .setOngoing(true)
+                        .setUsesChronometer(true)
+                        .build();
                 mNM.notify(2, n);
             }
         },
@@ -655,12 +678,12 @@
                 mHandler.postDelayed(new Runnable() {
                         public void run() {
                             Log.d(TAG, "Chronometer Stop");
-                            Notification n = new Notification();
-                            n.icon = R.drawable.icon1;
-                            n.contentView = new RemoteViews(getPackageName(),
-                                                             R.layout.chrono_notification);
-                            n.contentView.setChronometer(R.id.time, mChronometerBase, null, false);
-                            n.contentIntent = makeIntent();
+                            Notification n = new Notification.Builder(NotificationTestList.this)
+                                    .setSmallIcon(R.drawable.icon1)
+                                    .setWhen(System.currentTimeMillis())
+                                    .setContentTitle(name)
+                                    .setContentIntent(makeIntent())
+                                    .build();
                             mNM.notify(2, n);
                         }
                     }, 3000);
@@ -669,29 +692,29 @@
 
         new Test("Sequential Persistent") {
             public void run() {
-                mNM.notify(1, notificationWithNumbers(1));
-                mNM.notify(2, notificationWithNumbers(2));
+                mNM.notify(1, notificationWithNumbers(name, 1));
+                mNM.notify(2, notificationWithNumbers(name, 2));
             }
         },
 
         new Test("Replace Persistent") {
             public void run() {
-                mNM.notify(1, notificationWithNumbers(1));
-                mNM.notify(1, notificationWithNumbers(1));
+                mNM.notify(1, notificationWithNumbers(name, 1));
+                mNM.notify(1, notificationWithNumbers(name, 1));
             }
         },
 
         new Test("Run and Cancel (n=1)") {
             public void run() {
-                mNM.notify(1, notificationWithNumbers(1));
+                mNM.notify(1, notificationWithNumbers(name, 1));
                 mNM.cancel(1);
             }
         },
 
         new Test("Run an Cancel (n=2)") {
             public void run() {
-                mNM.notify(1, notificationWithNumbers(1));
-                mNM.notify(2, notificationWithNumbers(2));
+                mNM.notify(1, notificationWithNumbers(name, 1));
+                mNM.notify(2, notificationWithNumbers(name, 2));
                 mNM.cancel(2);
             }
         },
@@ -701,8 +724,8 @@
             public void run() {
                 for (int i = 0; i < 10; i++) {
                   Log.d(TAG, "Add two notifications");
-                  mNM.notify(1, notificationWithNumbers(1));
-                  mNM.notify(2, notificationWithNumbers(2));
+                  mNM.notify(1, notificationWithNumbers(name, 1));
+                  mNM.notify(2, notificationWithNumbers(name, 2));
                   Log.d(TAG, "Cancel two notifications");
                   mNM.cancel(1);
                   mNM.cancel(2);
@@ -712,29 +735,14 @@
 
         new Test("Ten Notifications") {
             public void run() {
-                for (int i = 0; i < 2; i++) {
-                    Notification n = new Notification(
-                            kNumberedIconResIDs[i],
-                            null, System.currentTimeMillis());
-                    n.number = i;
-                    n.setLatestEventInfo(
-                            NotificationTestList.this,
-                            "Persistent #" + i,
-                            "Notify me!!!" + i, 
-                            null);
-                    n.flags |= Notification.FLAG_ONGOING_EVENT;
-                    mNM.notify((i+1)*10, n);
-                }
-                for (int i = 2; i < 10; i++) {
-                    Notification n = new Notification(
-                            kNumberedIconResIDs[i],
-                            null, System.currentTimeMillis());
-                    n.number = i;
-                    n.setLatestEventInfo(
-                            NotificationTestList.this,
-                            "Persistent #" + i,
-                            "Notify me!!!" + i, 
-                            null);
+                for (int i = 0; i < 10; i++) {
+                    Notification n = new Notification.Builder(NotificationTestList.this)
+                            .setSmallIcon(kNumberedIconResIDs[i])
+                            .setContentTitle("Persistent #" + i)
+                            .setContentText("Notify me!!!" + i)
+                            .setOngoing(i < 2)
+                            .setNumber(i)
+                            .build();
                     mNM.notify((i+1)*10, n);
                 }
             }
@@ -757,25 +765,25 @@
         
         new Test("Persistent with numbers 1") {
             public void run() {
-                mNM.notify(1, notificationWithNumbers(1));
+                mNM.notify(1, notificationWithNumbers(name, 1));
             }
         },
 
         new Test("Persistent with numbers 22") {
             public void run() {
-                mNM.notify(1, notificationWithNumbers(22));
+                mNM.notify(1, notificationWithNumbers(name, 22));
             }
         },
 
         new Test("Persistent with numbers 333") {
             public void run() {
-                mNM.notify(1, notificationWithNumbers(333));
+                mNM.notify(1, notificationWithNumbers(name, 333));
             }
         },
 
         new Test("Persistent with numbers 4444") {
             public void run() {
-                mNM.notify(1, notificationWithNumbers(4444));
+                mNM.notify(1, notificationWithNumbers(name, 4444));
             }
         },
 
@@ -786,7 +794,7 @@
                     .setContentTitle("High priority")
                     .setContentText("This should appear before all others")
                     .setPriority(Notification.PRIORITY_HIGH)
-                    .getNotification();
+                    .build();
 
                 int[] idOut = new int[1];
                 try {
@@ -812,7 +820,7 @@
                     .setContentTitle("MAX priority")
                     .setContentText("This might appear as an intruder alert")
                     .setPriority(Notification.PRIORITY_MAX)
-                    .getNotification();
+                    .build();
 
                 int[] idOut = new int[1];
                 try {
@@ -838,7 +846,7 @@
                     .setContentTitle("MIN priority")
                     .setContentText("You should not see this")
                     .setPriority(Notification.PRIORITY_MIN)
-                    .getNotification();
+                    .build();
 
                 int[] idOut = new int[1];
                 try {
@@ -875,16 +883,15 @@
 
     };
 
-    private Notification notificationWithNumbers(int num) {
-        Notification n = new Notification(this,
-                (num >= 0 && num < kNumberedIconResIDs.length)
-                    ? kNumberedIconResIDs[num]
-                    : kUnnumberedIconResID,
-                null,
-                System.currentTimeMillis(),
-                "Notification", "Number=" + num,
-                null);
-        n.number = num;
+    private Notification notificationWithNumbers(String name, int num) {
+        Notification n = new Notification.Builder(NotificationTestList.this)
+                .setSmallIcon((num >= 0 && num < kNumberedIconResIDs.length)
+                        ? kNumberedIconResIDs[num]
+                        : kUnnumberedIconResID)
+                .setContentTitle(name)
+                .setContentText("Number=" + num)
+                .setNumber(num)
+                .build();
         return n;
     }
 
@@ -932,9 +939,12 @@
     }
 
     void timeNotification(int n, String label, long time) {
-        mNM.notify(n, new Notification(NotificationTestList.this,
-                    R.drawable.ic_statusbar_missedcall, null,
-                    time, label, "" + new java.util.Date(time), null));
+        mNM.notify(n, new Notification.Builder(NotificationTestList.this)
+                .setSmallIcon(R.drawable.ic_statusbar_missedcall)
+                .setWhen(time)
+                .setContentTitle(label)
+                .setContentText(new java.util.Date(time).toString())
+                .build());
 
     }
 
diff --git a/tests/StatusBar/src/com/android/statusbartest/StatusBarTest.java b/tests/StatusBar/src/com/android/statusbartest/StatusBarTest.java
index 50f98b8..cd04c2e 100644
--- a/tests/StatusBar/src/com/android/statusbartest/StatusBarTest.java
+++ b/tests/StatusBar/src/com/android/statusbartest/StatusBarTest.java
@@ -153,25 +153,24 @@
         },
         new Test("Priority notification") {
             public void run() {
-                Notification not = new Notification();
-                not.icon = R.drawable.stat_sys_phone;
-                not.when = System.currentTimeMillis()-(1000*60*60*24);
-                not.setLatestEventInfo(StatusBarTest.this,
-                                "Incoming call",
-                                "from: Imperious Leader",
-                                null
-                                );
-                not.flags |= Notification.FLAG_HIGH_PRIORITY;
                 Intent fullScreenIntent = new Intent(StatusBarTest.this, TestAlertActivity.class);
                 int id = (int)System.currentTimeMillis(); // XXX HAX
                 fullScreenIntent.putExtra("id", id);
-                not.fullScreenIntent = PendingIntent.getActivity(
+                PendingIntent pi = PendingIntent.getActivity(
                     StatusBarTest.this,
                     0,
                     fullScreenIntent,
                     PendingIntent.FLAG_CANCEL_CURRENT);
-                // if you tap on it you should get the original alert box
-                not.contentIntent = not.fullScreenIntent;
+                Notification not = new Notification.Builder(StatusBarTest.this)
+                        .setSmallIcon(R.drawable.stat_sys_phone)
+                        .setWhen(System.currentTimeMillis() - (1000 * 60 * 60 * 24))
+                        .setContentTitle("Incoming call")
+                        .setContentText("from: Imperious Leader")
+                        .setContentIntent(pi)
+                        .setFullScreenIntent(pi, true)
+                        .setPriority(Notification.PRIORITY_HIGH)
+                        .build();
+
                 mNotificationManager.notify(id, not);
             }
         },