Merge "Fix lockscreen bugs with SHOW_WHEN_LOCKED"
diff --git a/Android.mk b/Android.mk
index d7e8e4e..7ec010d 100644
--- a/Android.mk
+++ b/Android.mk
@@ -74,6 +74,7 @@
 	core/java/android/app/IBackupAgent.aidl \
 	core/java/android/app/IInstrumentationWatcher.aidl \
 	core/java/android/app/INotificationManager.aidl \
+	core/java/android/app/INotificationManagerCallback.aidl \
 	core/java/android/app/IProcessObserver.aidl \
 	core/java/android/app/ISearchManager.aidl \
 	core/java/android/app/ISearchManagerCallback.aidl \
@@ -555,6 +556,7 @@
 	frameworks/base/core/java/android/app/AssistStructure.aidl \
 	frameworks/base/core/java/android/app/AssistContent.aidl \
 	frameworks/base/core/java/android/app/Notification.aidl \
+	frameworks/base/core/java/android/app/NotificationManager.aidl \
 	frameworks/base/core/java/android/app/WallpaperInfo.aidl \
 	frameworks/base/core/java/android/app/AppOpsManager.aidl \
 	frameworks/base/core/java/android/app/ActivityManager.aidl \
diff --git a/api/current.txt b/api/current.txt
index 8ae6b47..3a23b0f 100644
--- a/api/current.txt
+++ b/api/current.txt
@@ -815,6 +815,7 @@
     field public static final int listSeparatorTextViewStyle = 16843272; // 0x1010208
     field public static final int listViewStyle = 16842868; // 0x1010074
     field public static final int listViewWhiteStyle = 16842869; // 0x1010075
+    field public static final int lockTaskMode = 16844015; // 0x10104ef
     field public static final int logo = 16843454; // 0x10102be
     field public static final int longClickable = 16842982; // 0x10100e6
     field public static final int loopViews = 16843527; // 0x1010307
@@ -5107,8 +5108,44 @@
     method public void cancel(int);
     method public void cancel(java.lang.String, int);
     method public void cancelAll();
+    method public android.app.NotificationManager.Policy getNotificationPolicy(android.app.NotificationManager.Policy.Token);
+    method public boolean isNotificationPolicyTokenValid(android.app.NotificationManager.Policy.Token);
     method public void notify(int, android.app.Notification);
     method public void notify(java.lang.String, int, android.app.Notification);
+    method public void requestNotificationPolicyToken(android.app.NotificationManager.Policy.Token.RequestCallback, android.os.Handler);
+    method public void setNotificationPolicy(android.app.NotificationManager.Policy.Token, android.app.NotificationManager.Policy);
+    field public static final java.lang.String ACTION_NOTIFICATION_POLICY_CHANGED = "android.app.action.NOTIFICATION_POLICY_CHANGED";
+  }
+
+  public static class NotificationManager.Policy implements android.os.Parcelable {
+    ctor public NotificationManager.Policy(int, int);
+    method public int describeContents();
+    method public static java.lang.String priorityCategoriesToString(int);
+    method public static java.lang.String prioritySendersToString(int);
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.app.NotificationManager.Policy> CREATOR;
+    field public static final int PRIORITY_CATEGORY_CALLS = 8; // 0x8
+    field public static final int PRIORITY_CATEGORY_EVENTS = 2; // 0x2
+    field public static final int PRIORITY_CATEGORY_MESSAGES = 4; // 0x4
+    field public static final int PRIORITY_CATEGORY_REMINDERS = 1; // 0x1
+    field public static final int PRIORITY_CATEGORY_REPEAT_CALLERS = 16; // 0x10
+    field public static final int PRIORITY_SENDERS_ANY = 0; // 0x0
+    field public static final int PRIORITY_SENDERS_CONTACTS = 1; // 0x1
+    field public static final int PRIORITY_SENDERS_STARRED = 2; // 0x2
+    field public final int priorityCategories;
+    field public final int prioritySenders;
+  }
+
+  public static class NotificationManager.Policy.Token implements android.os.Parcelable {
+    method public int describeContents();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.app.NotificationManager.Policy.Token> CREATOR;
+  }
+
+  public static abstract class NotificationManager.Policy.Token.RequestCallback {
+    ctor public NotificationManager.Policy.Token.RequestCallback();
+    method public abstract void onTokenDenied();
+    method public abstract void onTokenGranted(android.app.NotificationManager.Policy.Token);
   }
 
   public final class PendingIntent implements android.os.Parcelable {
@@ -13770,6 +13807,8 @@
 
   public class FingerprintManager {
     method public void authenticate(android.hardware.fingerprint.FingerprintManager.CryptoObject, android.os.CancellationSignal, android.hardware.fingerprint.FingerprintManager.AuthenticationCallback, int);
+    method public boolean hasEnrolledFingerprints();
+    method public boolean isHardwareDetected();
     field public static final int FINGERPRINT_ACQUIRED_GOOD = 0; // 0x0
     field public static final int FINGERPRINT_ACQUIRED_IMAGER_DIRTY = 3; // 0x3
     field public static final int FINGERPRINT_ACQUIRED_INSUFFICIENT = 2; // 0x2
@@ -13800,6 +13839,8 @@
   }
 
   public static class FingerprintManager.CryptoObject {
+    ctor public FingerprintManager.CryptoObject(java.security.Signature);
+    ctor public FingerprintManager.CryptoObject(javax.crypto.Cipher);
     method public javax.crypto.Cipher getCipher();
     method public java.security.Signature getSignature();
   }
@@ -27259,7 +27300,6 @@
     method public static android.renderscript.AllocationAdapter create1D(android.renderscript.RenderScript, android.renderscript.Allocation);
     method public static android.renderscript.AllocationAdapter create2D(android.renderscript.RenderScript, android.renderscript.Allocation);
     method public static android.renderscript.AllocationAdapter createTyped(android.renderscript.RenderScript, android.renderscript.Allocation, android.renderscript.Type);
-    method public void setArray(int, int);
     method public void setFace(android.renderscript.Type.CubemapFace);
     method public void setLOD(int);
     method public void setX(int);
@@ -28082,8 +28122,6 @@
     method public static android.renderscript.Type createX(android.renderscript.RenderScript, android.renderscript.Element, int);
     method public static android.renderscript.Type createXY(android.renderscript.RenderScript, android.renderscript.Element, int, int);
     method public static android.renderscript.Type createXYZ(android.renderscript.RenderScript, android.renderscript.Element, int, int, int);
-    method public int getArray(int);
-    method public int getArrayCount();
     method public int getCount();
     method public android.renderscript.Element getElement();
     method public int getX();
@@ -28097,7 +28135,6 @@
   public static class Type.Builder {
     ctor public Type.Builder(android.renderscript.RenderScript, android.renderscript.Element);
     method public android.renderscript.Type create();
-    method public android.renderscript.Type.Builder setArray(int, int);
     method public android.renderscript.Type.Builder setFaces(boolean);
     method public android.renderscript.Type.Builder setMipmaps(boolean);
     method public android.renderscript.Type.Builder setX(int);
@@ -28602,6 +28639,7 @@
     method public final int getCurrentInterruptionFilter();
     method public final int getCurrentListenerHints();
     method public android.service.notification.NotificationListenerService.RankingMap getCurrentRanking();
+    method public final android.app.NotificationManager.Policy.Token getNotificationPolicyToken();
     method public android.os.IBinder onBind(android.content.Intent);
     method public void onInterruptionFilterChanged(int);
     method public void onListenerConnected();
@@ -30331,7 +30369,6 @@
     method public java.lang.String getLine1Number(android.telecom.PhoneAccountHandle);
     method public android.telecom.PhoneAccount getPhoneAccount(android.telecom.PhoneAccountHandle);
     method public android.telecom.PhoneAccountHandle getSimCallManager();
-    method public android.telecom.PhoneAccountHandle getUserSelectedOutgoingPhoneAccount();
     method public boolean handleMmi(java.lang.String);
     method public boolean handleMmi(java.lang.String, android.telecom.PhoneAccountHandle);
     method public boolean hasVoiceMailNumber(android.telecom.PhoneAccountHandle);
diff --git a/api/system-current.txt b/api/system-current.txt
index fb44b61..ef3d3df 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -888,6 +888,7 @@
     field public static final int listSeparatorTextViewStyle = 16843272; // 0x1010208
     field public static final int listViewStyle = 16842868; // 0x1010074
     field public static final int listViewWhiteStyle = 16842869; // 0x1010075
+    field public static final int lockTaskMode = 16844015; // 0x10104ef
     field public static final int logo = 16843454; // 0x10102be
     field public static final int longClickable = 16842982; // 0x10100e6
     field public static final int loopViews = 16843527; // 0x1010307
@@ -5198,8 +5199,44 @@
     method public void cancel(int);
     method public void cancel(java.lang.String, int);
     method public void cancelAll();
+    method public android.app.NotificationManager.Policy getNotificationPolicy(android.app.NotificationManager.Policy.Token);
+    method public boolean isNotificationPolicyTokenValid(android.app.NotificationManager.Policy.Token);
     method public void notify(int, android.app.Notification);
     method public void notify(java.lang.String, int, android.app.Notification);
+    method public void requestNotificationPolicyToken(android.app.NotificationManager.Policy.Token.RequestCallback, android.os.Handler);
+    method public void setNotificationPolicy(android.app.NotificationManager.Policy.Token, android.app.NotificationManager.Policy);
+    field public static final java.lang.String ACTION_NOTIFICATION_POLICY_CHANGED = "android.app.action.NOTIFICATION_POLICY_CHANGED";
+  }
+
+  public static class NotificationManager.Policy implements android.os.Parcelable {
+    ctor public NotificationManager.Policy(int, int);
+    method public int describeContents();
+    method public static java.lang.String priorityCategoriesToString(int);
+    method public static java.lang.String prioritySendersToString(int);
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.app.NotificationManager.Policy> CREATOR;
+    field public static final int PRIORITY_CATEGORY_CALLS = 8; // 0x8
+    field public static final int PRIORITY_CATEGORY_EVENTS = 2; // 0x2
+    field public static final int PRIORITY_CATEGORY_MESSAGES = 4; // 0x4
+    field public static final int PRIORITY_CATEGORY_REMINDERS = 1; // 0x1
+    field public static final int PRIORITY_CATEGORY_REPEAT_CALLERS = 16; // 0x10
+    field public static final int PRIORITY_SENDERS_ANY = 0; // 0x0
+    field public static final int PRIORITY_SENDERS_CONTACTS = 1; // 0x1
+    field public static final int PRIORITY_SENDERS_STARRED = 2; // 0x2
+    field public final int priorityCategories;
+    field public final int prioritySenders;
+  }
+
+  public static class NotificationManager.Policy.Token implements android.os.Parcelable {
+    method public int describeContents();
+    method public void writeToParcel(android.os.Parcel, int);
+    field public static final android.os.Parcelable.Creator<android.app.NotificationManager.Policy.Token> CREATOR;
+  }
+
+  public static abstract class NotificationManager.Policy.Token.RequestCallback {
+    ctor public NotificationManager.Policy.Token.RequestCallback();
+    method public abstract void onTokenDenied();
+    method public abstract void onTokenGranted(android.app.NotificationManager.Policy.Token);
   }
 
   public final class PendingIntent implements android.os.Parcelable {
@@ -14066,6 +14103,8 @@
 
   public class FingerprintManager {
     method public void authenticate(android.hardware.fingerprint.FingerprintManager.CryptoObject, android.os.CancellationSignal, android.hardware.fingerprint.FingerprintManager.AuthenticationCallback, int);
+    method public boolean hasEnrolledFingerprints();
+    method public boolean isHardwareDetected();
     field public static final int FINGERPRINT_ACQUIRED_GOOD = 0; // 0x0
     field public static final int FINGERPRINT_ACQUIRED_IMAGER_DIRTY = 3; // 0x3
     field public static final int FINGERPRINT_ACQUIRED_INSUFFICIENT = 2; // 0x2
@@ -14096,6 +14135,8 @@
   }
 
   public static class FingerprintManager.CryptoObject {
+    ctor public FingerprintManager.CryptoObject(java.security.Signature);
+    ctor public FingerprintManager.CryptoObject(javax.crypto.Cipher);
     method public javax.crypto.Cipher getCipher();
     method public java.security.Signature getSignature();
   }
@@ -29262,7 +29303,6 @@
     method public static android.renderscript.AllocationAdapter create1D(android.renderscript.RenderScript, android.renderscript.Allocation);
     method public static android.renderscript.AllocationAdapter create2D(android.renderscript.RenderScript, android.renderscript.Allocation);
     method public static android.renderscript.AllocationAdapter createTyped(android.renderscript.RenderScript, android.renderscript.Allocation, android.renderscript.Type);
-    method public void setArray(int, int);
     method public void setFace(android.renderscript.Type.CubemapFace);
     method public void setLOD(int);
     method public void setX(int);
@@ -30085,8 +30125,6 @@
     method public static android.renderscript.Type createX(android.renderscript.RenderScript, android.renderscript.Element, int);
     method public static android.renderscript.Type createXY(android.renderscript.RenderScript, android.renderscript.Element, int, int);
     method public static android.renderscript.Type createXYZ(android.renderscript.RenderScript, android.renderscript.Element, int, int, int);
-    method public int getArray(int);
-    method public int getArrayCount();
     method public int getCount();
     method public android.renderscript.Element getElement();
     method public int getX();
@@ -30100,7 +30138,6 @@
   public static class Type.Builder {
     ctor public Type.Builder(android.renderscript.RenderScript, android.renderscript.Element);
     method public android.renderscript.Type create();
-    method public android.renderscript.Type.Builder setArray(int, int);
     method public android.renderscript.Type.Builder setFaces(boolean);
     method public android.renderscript.Type.Builder setMipmaps(boolean);
     method public android.renderscript.Type.Builder setX(int);
@@ -30645,6 +30682,7 @@
     method public final int getCurrentInterruptionFilter();
     method public final int getCurrentListenerHints();
     method public android.service.notification.NotificationListenerService.RankingMap getCurrentRanking();
+    method public final android.app.NotificationManager.Policy.Token getNotificationPolicyToken();
     method public android.os.IBinder onBind(android.content.Intent);
     method public void onInterruptionFilterChanged(int);
     method public void onListenerConnected();
@@ -32446,7 +32484,6 @@
     method public java.util.List<android.telecom.PhoneAccountHandle> getPhoneAccountsSupportingScheme(java.lang.String);
     method public java.util.List<android.telecom.PhoneAccountHandle> getRegisteredConnectionManagers();
     method public android.telecom.PhoneAccountHandle getSimCallManager();
-    method public android.telecom.PhoneAccountHandle getUserSelectedOutgoingPhoneAccount();
     method public boolean handleMmi(java.lang.String);
     method public boolean handleMmi(java.lang.String, android.telecom.PhoneAccountHandle);
     method public boolean hasVoiceMailNumber(android.telecom.PhoneAccountHandle);
diff --git a/core/java/android/app/Activity.java b/core/java/android/app/Activity.java
index 6cf6481..4ccde1c 100644
--- a/core/java/android/app/Activity.java
+++ b/core/java/android/app/Activity.java
@@ -6434,18 +6434,22 @@
      * Request to put this Activity in a mode where the user is locked to the
      * current task.
      *
-     * This will prevent the user from launching other apps, going to settings,
-     * or reaching the home screen.
+     * This will prevent the user from launching other apps, going to settings, or reaching the
+     * home screen. This does not include those apps whose {@link android.R.attr#lockTaskMode}
+     * values permit launching while locked.
      *
-     * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true
-     * for this component then the app will go directly into Lock Task mode.  The user
-     * will not be able to exit this mode until {@link Activity#stopLockTask()} is called.
+     * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns true or
+     * lockTaskMode=lockTaskModeAlways for this component then the app will go directly into
+     * Lock Task mode. The user will not be able to exit this mode until
+     * {@link Activity#stopLockTask()} is called.
      *
      * If {@link DevicePolicyManager#isLockTaskPermitted(String)} returns false
      * then the system will prompt the user with a dialog requesting permission to enter
      * this mode.  When entered through this method the user can exit at any time through
      * an action described by the request dialog.  Calling stopLockTask will also exit the
      * mode.
+     *
+     * @see android.R.attr#lockTaskMode
      */
     public void startLockTask() {
         try {
@@ -6462,6 +6466,14 @@
      * startLockTask previously.
      *
      * This will allow the user to exit this app and move onto other activities.
+     * <p>Note: This method should only be called when the activity is user-facing. That is,
+     * between onResume() and onPause().
+     * <p>Note: If there are other tasks below this one that are also locked then calling this
+     * method will immediately finish this task and resume the previous locked one, remaining in
+     * lockTask mode.
+     *
+     * @see android.R.attr#lockTaskMode
+     * @see ActivityManager#getLockTaskModeState()
      */
     public void stopLockTask() {
         try {
diff --git a/core/java/android/app/INotificationManager.aidl b/core/java/android/app/INotificationManager.aidl
index e2230da..913159a 100644
--- a/core/java/android/app/INotificationManager.aidl
+++ b/core/java/android/app/INotificationManager.aidl
@@ -17,8 +17,10 @@
 
 package android.app;
 
+import android.app.INotificationManagerCallback;
 import android.app.ITransientNotification;
 import android.app.Notification;
+import android.app.NotificationManager;
 import android.content.ComponentName;
 import android.content.Intent;
 import android.content.pm.ParceledListSlice;
@@ -71,6 +73,7 @@
     void requestInterruptionFilterFromListener(in INotificationListener token, int interruptionFilter);
     int getInterruptionFilterFromListener(in INotificationListener token);
     void setOnNotificationPostedTrimFromListener(in INotificationListener token, int trim);
+    NotificationManager.Policy.Token getPolicyTokenFromListener(in INotificationListener listener);
 
     ComponentName getEffectsSuppressor();
     boolean matchesCallFilter(in Bundle extras);
@@ -82,4 +85,8 @@
     oneway void setZenMode(int mode, in Uri conditionId, String reason);
     oneway void notifyConditions(String pkg, in IConditionProvider provider, in Condition[] conditions);
     oneway void requestZenModeConditions(in IConditionListener callback, int relevance);
+    oneway void requestNotificationPolicyToken(String pkg, in INotificationManagerCallback callback);
+    boolean isNotificationPolicyTokenValid(String pkg, in NotificationManager.Policy.Token token);
+    NotificationManager.Policy getNotificationPolicy(in NotificationManager.Policy.Token token);
+    void setNotificationPolicy(in NotificationManager.Policy.Token token, in NotificationManager.Policy policy);
 }
diff --git a/core/java/android/app/INotificationManagerCallback.aidl b/core/java/android/app/INotificationManagerCallback.aidl
new file mode 100644
index 0000000..b9414ca
--- /dev/null
+++ b/core/java/android/app/INotificationManagerCallback.aidl
@@ -0,0 +1,24 @@
+/**
+ * Copyright (c) 2015, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.app;
+
+import android.app.NotificationManager;
+
+/** @hide */
+oneway interface INotificationManagerCallback {
+    void onPolicyToken(in NotificationManager.Policy.Token token);
+}
diff --git a/core/java/android/app/NotificationManager.aidl b/core/java/android/app/NotificationManager.aidl
new file mode 100644
index 0000000..8380b8d
--- /dev/null
+++ b/core/java/android/app/NotificationManager.aidl
@@ -0,0 +1,20 @@
+/**
+ * Copyright (c) 2015, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.app;
+
+parcelable NotificationManager.Policy;
+parcelable NotificationManager.Policy.Token;
\ No newline at end of file
diff --git a/core/java/android/app/NotificationManager.java b/core/java/android/app/NotificationManager.java
index fa61e18..7133dce 100644
--- a/core/java/android/app/NotificationManager.java
+++ b/core/java/android/app/NotificationManager.java
@@ -16,14 +16,19 @@
 
 package android.app;
 
+import android.annotation.NonNull;
+import android.annotation.Nullable;
 import android.annotation.SdkConstant;
 import android.app.Notification.Builder;
+import android.app.NotificationManager.Policy.Token;
 import android.content.ComponentName;
 import android.content.Context;
 import android.net.Uri;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.IBinder;
+import android.os.Parcel;
+import android.os.Parcelable;
 import android.os.RemoteException;
 import android.os.ServiceManager;
 import android.os.StrictMode;
@@ -33,6 +38,8 @@
 import android.service.notification.ZenModeConfig;
 import android.util.Log;
 
+import java.util.Objects;
+
 /**
  * Class to notify the user of events that happen.  This is how you tell
  * the user that something has happened in the background. {@more}
@@ -89,6 +96,14 @@
     public static final String ACTION_EFFECTS_SUPPRESSOR_CHANGED
             = "android.os.action.ACTION_EFFECTS_SUPPRESSOR_CHANGED";
 
+    /**
+     * Intent that is broadcast when the state of getNotificationPolicy() changes.
+     * This broadcast is only sent to registered receivers.
+     */
+    @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION)
+    public static final String ACTION_NOTIFICATION_POLICY_CHANGED
+            = "android.app.action.NOTIFICATION_POLICY_CHANGED";
+
     private static INotificationManager sService;
 
     /** @hide */
@@ -338,5 +353,293 @@
         return null;
     }
 
+    /**
+     * Requests a notification policy token for the calling package.
+     *
+     * @param callback required, used to receive the granted token or the deny signal.
+     * @param handler The handler used when receiving the result.
+     *                If null, the current thread is used.
+     */
+    public void requestNotificationPolicyToken(@NonNull final Policy.Token.RequestCallback callback,
+            @Nullable Handler handler) {
+        checkRequired("callback", callback);
+        final Handler h = handler != null ? handler : new Handler();
+        INotificationManager service = getService();
+        try {
+            service.requestNotificationPolicyToken(mContext.getOpPackageName(),
+                    new INotificationManagerCallback.Stub() {
+                @Override
+                public void onPolicyToken(final Token token) throws RemoteException {
+                    h.post(new Runnable() {
+                        @Override
+                        public void run() {
+                            if (token != null) {
+                                callback.onTokenGranted(token);
+                            } else {
+                                callback.onTokenDenied();
+                            }
+                        }
+                    });
+                }
+            });
+        } catch (RemoteException e) {
+        }
+    }
+
+    /**
+     * Checks a given notification policy token.
+     *
+     * Returns true if the token is still valid for managing policy.
+     */
+    public boolean isNotificationPolicyTokenValid(@NonNull Policy.Token token) {
+        if (token == null) return false;
+        INotificationManager service = getService();
+        try {
+            return service.isNotificationPolicyTokenValid(mContext.getOpPackageName(), token);
+        } catch (RemoteException e) {
+        }
+        return false;
+    }
+
+    /**
+     * Gets the current notification policy.
+     *
+     * @param token A valid notification policy token is required to access the current policy.
+     */
+    public Policy getNotificationPolicy(@NonNull Policy.Token token) {
+        checkRequired("token", token);
+        INotificationManager service = getService();
+        try {
+            return service.getNotificationPolicy(token);
+        } catch (RemoteException e) {
+        }
+        return null;
+    }
+
+    /**
+     * Sets the current notification policy.
+     *
+     * @param token  A valid notification policy token is required to modify the current policy.
+     * @param policy The new desired policy.
+     */
+    public void setNotificationPolicy(@NonNull Policy.Token token, @NonNull Policy policy) {
+        checkRequired("token", token);
+        checkRequired("policy", policy);
+        INotificationManager service = getService();
+        try {
+            service.setNotificationPolicy(token, policy);
+        } catch (RemoteException e) {
+        }
+    }
+
     private Context mContext;
+
+    private static void checkRequired(String name, Object value) {
+        if (value == null) {
+            throw new IllegalArgumentException(name + " is required");
+        }
+    }
+
+    /**
+     * Notification policy configuration.  Represents user-preferences for notification
+     * filtering and prioritization.
+     */
+    public static class Policy implements android.os.Parcelable {
+        /** Reminder notifications are prioritized. */
+        public static final int PRIORITY_CATEGORY_REMINDERS = 1 << 0;
+        /** Event notifications are prioritized. */
+        public static final int PRIORITY_CATEGORY_EVENTS = 1 << 1;
+        /** Message notifications are prioritized. */
+        public static final int PRIORITY_CATEGORY_MESSAGES = 1 << 2;
+        /** Calls are prioritized. */
+        public static final int PRIORITY_CATEGORY_CALLS = 1 << 3;
+        /** Calls from repeat callers are prioritized. */
+        public static final int PRIORITY_CATEGORY_REPEAT_CALLERS = 1 << 4;
+
+        private static final int[] ALL_PRIORITY_CATEGORIES = {
+            PRIORITY_CATEGORY_REMINDERS,
+            PRIORITY_CATEGORY_EVENTS,
+            PRIORITY_CATEGORY_MESSAGES,
+            PRIORITY_CATEGORY_CALLS,
+            PRIORITY_CATEGORY_REPEAT_CALLERS,
+        };
+
+        /** Any sender is prioritized. */
+        public static final int PRIORITY_SENDERS_ANY = 0;
+        /** Saved contacts are prioritized. */
+        public static final int PRIORITY_SENDERS_CONTACTS = 1;
+        /** Only starred contacts are prioritized. */
+        public static final int PRIORITY_SENDERS_STARRED = 2;
+
+        /** Notification categories to prioritize. Bitmask of PRIORITY_CATEGORY_* constants. */
+        public final int priorityCategories;
+
+        /** Notification senders to prioritize. One of:
+         * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */
+        public final int prioritySenders;
+
+        public Policy(int priorityCategories, int prioritySenders) {
+            this.priorityCategories = priorityCategories;
+            this.prioritySenders = prioritySenders;
+        }
+
+        /** @hide */
+        public Policy(Parcel source) {
+            this(source.readInt(), source.readInt());
+        }
+
+        @Override
+        public void writeToParcel(Parcel dest, int flags) {
+            dest.writeInt(priorityCategories);
+            dest.writeInt(prioritySenders);
+        }
+
+        @Override
+        public int describeContents() {
+            return 0;
+        }
+
+        @Override
+        public int hashCode() {
+            return Objects.hash(priorityCategories, prioritySenders);
+        }
+
+        @Override
+        public boolean equals(Object o) {
+            if (!(o instanceof Policy)) return false;
+            if (o == this) return true;
+            final Policy other = (Policy) o;
+            return other.priorityCategories == priorityCategories
+                    && other.prioritySenders == prioritySenders;
+        }
+
+        @Override
+        public String toString() {
+            return "NotificationManager.Policy["
+                    + "priorityCategories=" + priorityCategoriesToString(priorityCategories)
+                    + ",prioritySenders=" + prioritySendersToString(prioritySenders)
+                    + "]";
+        }
+
+        public static String priorityCategoriesToString(int priorityCategories) {
+            if (priorityCategories == 0) return "";
+            final StringBuilder sb = new StringBuilder();
+            for (int i = 0; i < ALL_PRIORITY_CATEGORIES.length; i++) {
+                final int priorityCategory = ALL_PRIORITY_CATEGORIES[i];
+                if ((priorityCategories & priorityCategory) != 0) {
+                    if (sb.length() > 0) sb.append(',');
+                    sb.append(priorityCategoryToString(priorityCategory));
+                }
+                priorityCategories &= ~priorityCategory;
+            }
+            if (priorityCategories != 0) {
+                if (sb.length() > 0) sb.append(',');
+                sb.append("PRIORITY_CATEGORY_UNKNOWN_").append(priorityCategories);
+            }
+            return sb.toString();
+        }
+
+        private static String priorityCategoryToString(int priorityCategory) {
+            switch (priorityCategory) {
+                case PRIORITY_CATEGORY_REMINDERS: return "PRIORITY_CATEGORY_REMINDERS";
+                case PRIORITY_CATEGORY_EVENTS: return "PRIORITY_CATEGORY_EVENTS";
+                case PRIORITY_CATEGORY_MESSAGES: return "PRIORITY_CATEGORY_MESSAGES";
+                case PRIORITY_CATEGORY_CALLS: return "PRIORITY_CATEGORY_CALLS";
+                case PRIORITY_CATEGORY_REPEAT_CALLERS: return "PRIORITY_CATEGORY_REPEAT_CALLERS";
+                default: return "PRIORITY_CATEGORY_UNKNOWN_" + priorityCategory;
+            }
+        }
+
+        public static String prioritySendersToString(int prioritySenders) {
+            switch (prioritySenders) {
+                case PRIORITY_SENDERS_ANY: return "PRIORITY_SENDERS_ANY";
+                case PRIORITY_SENDERS_CONTACTS: return "PRIORITY_SENDERS_CONTACTS";
+                case PRIORITY_SENDERS_STARRED: return "PRIORITY_SENDERS_STARRED";
+                default: return "PRIORITY_SENDERS_UNKNOWN_" + prioritySenders;
+            }
+        }
+
+        public static final Parcelable.Creator<Policy> CREATOR = new Parcelable.Creator<Policy>() {
+            @Override
+            public Policy createFromParcel(Parcel in) {
+                return new Policy(in);
+            }
+
+            @Override
+            public Policy[] newArray(int size) {
+                return new Policy[size];
+            }
+        };
+
+        /**
+         * Represents a client-specific token required to manage notification policy.
+         */
+        public static class Token implements Parcelable {
+            private final IBinder mBinder;
+
+            /** @hide */
+            public Token(IBinder binder) {
+                if (binder == null) throw new IllegalArgumentException("Binder required for token");
+                mBinder = binder;
+            }
+
+            @Override
+            public int describeContents() {
+                return 0;
+            }
+
+            @Override
+            public int hashCode() {
+                return Objects.hash(mBinder);
+            }
+
+            @Override
+            public boolean equals(Object o) {
+                if (!(o instanceof Token)) return false;
+                if (o == this) return true;
+                final Token other = (Token) o;
+                return Objects.equals(other.mBinder, mBinder);
+            }
+
+            @Override
+            public String toString() {
+                return String.format("NotificationManager.Token[0x%08x]",
+                        System.identityHashCode(mBinder));
+            }
+
+            @Override
+            public void writeToParcel(Parcel dest, int flags) {
+                dest.writeStrongBinder(mBinder);
+            }
+
+            public static final Parcelable.Creator<Token> CREATOR
+                    = new Parcelable.Creator<Token>() {
+                @Override
+                public Token createFromParcel(Parcel in) {
+                    return new Token(in.readStrongBinder());
+                }
+
+                @Override
+                public Token[] newArray(int size) {
+                    return new Token[size];
+                }
+            };
+
+            /** Callback for receiving the result of a token request. */
+            public static abstract class RequestCallback {
+                /**
+                 * Received if the request was granted for this package.
+                 *
+                 * @param token can be used to manage notification policy.
+                 */
+                public abstract void onTokenGranted(Policy.Token token);
+
+                /**
+                 * Received if the request was denied for this package.
+                 */
+                public abstract void onTokenDenied();
+            }
+        }
+    }
+
 }
diff --git a/core/java/android/content/pm/ActivityInfo.java b/core/java/android/content/pm/ActivityInfo.java
index 4723c0d..8d82aa2 100644
--- a/core/java/android/content/pm/ActivityInfo.java
+++ b/core/java/android/content/pm/ActivityInfo.java
@@ -609,7 +609,7 @@
      * attribute.
      */
     public int configChanges;
-    
+
     /**
      * The desired soft input mode for this activity's main window.
      * Set from the {@link android.R.attr#windowSoftInputMode} attribute
@@ -648,6 +648,37 @@
      */
     public boolean resizeable;
 
+    /** @hide */
+    public static final int LOCK_TASK_LAUNCH_MODE_DEFAULT = 0;
+    /** @hide */
+    public static final int LOCK_TASK_LAUNCH_MODE_NEVER = 1;
+    /** @hide */
+    public static final int LOCK_TASK_LAUNCH_MODE_ALWAYS = 2;
+    /** @hide */
+    public static final int LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED = 3;
+
+    /** @hide */
+    public static final String lockTaskLaunchModeToString(int lockTaskLaunchMode) {
+        switch (lockTaskLaunchMode) {
+            case LOCK_TASK_LAUNCH_MODE_DEFAULT:
+                return "LOCK_TASK_LAUNCH_MODE_DEFAULT";
+            case LOCK_TASK_LAUNCH_MODE_NEVER:
+                return "LOCK_TASK_LAUNCH_MODE_NEVER";
+            case LOCK_TASK_LAUNCH_MODE_ALWAYS:
+                return "LOCK_TASK_LAUNCH_MODE_ALWAYS";
+            case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
+                return "LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED";
+            default:
+                return "unknown=" + lockTaskLaunchMode;
+        }
+    }
+    /**
+     * Value indicating if the activity is to be locked at startup. Takes on the values from
+     * {@link android.R.attr#lockTaskMode}.
+     * @hide
+     */
+    public int lockTaskLaunchMode;
+
     public ActivityInfo() {
     }
 
@@ -665,13 +696,15 @@
         uiOptions = orig.uiOptions;
         parentActivityName = orig.parentActivityName;
         maxRecents = orig.maxRecents;
+        resizeable = orig.resizeable;
+        lockTaskLaunchMode = orig.lockTaskLaunchMode;
     }
-    
+
     /**
      * Return the theme resource identifier to use for this activity.  If
      * the activity defines a theme, that is used; else, the application
      * theme is used.
-     * 
+     *
      * @return The theme associated with this activity.
      */
     public final int getThemeResource() {
@@ -709,7 +742,8 @@
         if (uiOptions != 0) {
             pw.println(prefix + " uiOptions=0x" + Integer.toHexString(uiOptions));
         }
-        pw.println(prefix + "resizeable=" + resizeable);
+        pw.println(prefix + "resizeable=" + resizeable + " lockTaskLaunchMode="
+                + lockTaskLaunchModeToString(lockTaskLaunchMode));
         super.dumpBack(pw, prefix);
     }
     
@@ -739,6 +773,7 @@
         dest.writeInt(persistableMode);
         dest.writeInt(maxRecents);
         dest.writeInt(resizeable ? 1 : 0);
+        dest.writeInt(lockTaskLaunchMode);
     }
 
     public static final Parcelable.Creator<ActivityInfo> CREATOR
@@ -767,5 +802,6 @@
         persistableMode = source.readInt();
         maxRecents = source.readInt();
         resizeable = (source.readInt() == 1);
+        lockTaskLaunchMode = source.readInt();
     }
 }
diff --git a/core/java/android/content/pm/PackageParser.java b/core/java/android/content/pm/PackageParser.java
index 7464cab..40f4e8f 100644
--- a/core/java/android/content/pm/PackageParser.java
+++ b/core/java/android/content/pm/PackageParser.java
@@ -3159,6 +3159,9 @@
                         R.styleable.AndroidManifestActivity_screenOrientation,
                         ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
             }
+
+            a.info.lockTaskLaunchMode =
+                    sa.getInt(R.styleable.AndroidManifestActivity_lockTaskMode, 0);
         } else {
             a.info.launchMode = ActivityInfo.LAUNCH_MULTIPLE;
             a.info.configChanges = 0;
diff --git a/core/java/android/hardware/fingerprint/FingerprintManager.java b/core/java/android/hardware/fingerprint/FingerprintManager.java
index b757a9a..0d7b261 100644
--- a/core/java/android/hardware/fingerprint/FingerprintManager.java
+++ b/core/java/android/hardware/fingerprint/FingerprintManager.java
@@ -199,12 +199,12 @@
      */
     public static class CryptoObject {
 
-        CryptoObject(Signature signature) {
+        public CryptoObject(Signature signature) {
             mSignature = signature;
             mCipher = null;
         }
 
-        CryptoObject(Cipher cipher) {
+        public CryptoObject(Cipher cipher) {
             mCipher = cipher;
             mSignature = null;
         }
@@ -535,9 +535,9 @@
      *
      * @hide
      */
-    public List<Fingerprint> getEnrolledFingerprints() {
+    public List<Fingerprint> getEnrolledFingerprints(int userId) {
         if (mService != null) try {
-            return mService.getEnrolledFingerprints(getCurrentUserId());
+            return mService.getEnrolledFingerprints(userId);
         } catch (RemoteException e) {
             Log.v(TAG, "Remote exception in getEnrolledFingerprints: ", e);
         }
@@ -545,11 +545,34 @@
     }
 
     /**
-     * Determine if fingerprint hardware is present and functional.
-     * @return true if hardware is present and functional, false otherwise.
+     * Obtain the list of enrolled fingerprints templates.
+     * @return list of current fingerprint items
      *
      * @hide
      */
+    public List<Fingerprint> getEnrolledFingerprints() {
+        return getEnrolledFingerprints(UserHandle.myUserId());
+    }
+
+    /**
+     * Determine if there is at least one fingerprint enrolled.
+     *
+     * @return true if at least one fingerprint is enrolled, false otherwise
+     */
+    public boolean hasEnrolledFingerprints() {
+        if (mService != null) try {
+            return mService.hasEnrolledFingerprints(UserHandle.myUserId());
+        } catch (RemoteException e) {
+            Log.v(TAG, "Remote exception in getEnrolledFingerprints: ", e);
+        }
+        return false;
+    }
+
+    /**
+     * Determine if fingerprint hardware is present and functional.
+     *
+     * @return true if hardware is present and functional, false otherwise.
+     */
     public boolean isHardwareDetected() {
         if (mService != null) {
             try {
diff --git a/core/java/android/hardware/fingerprint/IFingerprintService.aidl b/core/java/android/hardware/fingerprint/IFingerprintService.aidl
index 6fe72d5..51a0e4c 100644
--- a/core/java/android/hardware/fingerprint/IFingerprintService.aidl
+++ b/core/java/android/hardware/fingerprint/IFingerprintService.aidl
@@ -54,6 +54,9 @@
     // Get a pre-enrollment authentication token
     long preEnroll(IBinder token);
 
+    // Determine if a user has at least one enrolled fingerprint
+    boolean hasEnrolledFingerprints(int groupId);
+
     // Gets the number of hardware devices
     // int getHardwareDeviceCount();
 
diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java
index f640f0d..3707694 100644
--- a/core/java/android/provider/Settings.java
+++ b/core/java/android/provider/Settings.java
@@ -7720,6 +7720,13 @@
          * @hide
          */
         public static final String NEW_CONTACT_AGGREGATOR = "new_contact_aggregator";
+
+        /**
+         * Whether to enable contacts metadata syncing or not
+         * The value 1 - enable, 0 - disable
+         * @hide
+         */
+        public static final String CONTACT_METADATA_SYNC = "contact_metadata_sync";
     }
 
     /**
diff --git a/core/java/android/service/notification/NotificationListenerService.java b/core/java/android/service/notification/NotificationListenerService.java
index fa782e4..cc7f880 100644
--- a/core/java/android/service/notification/NotificationListenerService.java
+++ b/core/java/android/service/notification/NotificationListenerService.java
@@ -21,6 +21,7 @@
 import android.app.INotificationManager;
 import android.app.Notification;
 import android.app.Notification.Builder;
+import android.app.NotificationManager.Policy;
 import android.app.Service;
 import android.content.ComponentName;
 import android.content.Context;
@@ -501,6 +502,22 @@
     }
 
     /**
+     * Gets the notification policy token associated with this listener.
+     *
+     * <p>
+     * Returns null if this listener is not currently active.
+     */
+    public final Policy.Token getNotificationPolicyToken() {
+        if (!isBound()) return null;
+        try {
+            return getNotificationInterface().getPolicyTokenFromListener(mWrapper);
+        } catch (android.os.RemoteException ex) {
+            Log.v(TAG, "Unable to contact notification manager", ex);
+            return null;
+        }
+    }
+
+    /**
      * Sets the desired {@link #getCurrentListenerHints() listener hints}.
      *
      * <p>
diff --git a/core/java/android/service/notification/ZenModeConfig.java b/core/java/android/service/notification/ZenModeConfig.java
index 1ed4779..14e947c 100644
--- a/core/java/android/service/notification/ZenModeConfig.java
+++ b/core/java/android/service/notification/ZenModeConfig.java
@@ -16,6 +16,7 @@
 
 package android.service.notification;
 
+import android.app.NotificationManager.Policy;
 import android.content.ComponentName;
 import android.content.Context;
 import android.content.res.Resources;
@@ -470,6 +471,59 @@
         }
     };
 
+    public Policy toNotificationPolicy() {
+        int priorityCategories = 0;
+        int prioritySenders = Policy.PRIORITY_SENDERS_ANY;
+        if (allowCalls) {
+            priorityCategories |= Policy.PRIORITY_CATEGORY_CALLS;
+        }
+        if (allowMessages) {
+            priorityCategories |= Policy.PRIORITY_CATEGORY_MESSAGES;
+        }
+        if (allowEvents) {
+            priorityCategories |= Policy.PRIORITY_CATEGORY_EVENTS;
+        }
+        if (allowReminders) {
+            priorityCategories |= Policy.PRIORITY_CATEGORY_REMINDERS;
+        }
+        if (allowRepeatCallers) {
+            priorityCategories |= Policy.PRIORITY_CATEGORY_REPEAT_CALLERS;
+        }
+        switch (allowFrom) {
+            case SOURCE_ANYONE:
+                prioritySenders = Policy.PRIORITY_SENDERS_ANY;
+                break;
+            case SOURCE_CONTACT:
+                prioritySenders = Policy.PRIORITY_SENDERS_CONTACTS;
+                break;
+            case SOURCE_STAR:
+                prioritySenders = Policy.PRIORITY_SENDERS_STARRED;
+                break;
+        }
+        return new Policy(priorityCategories, prioritySenders);
+    }
+
+    public void applyNotificationPolicy(Policy policy) {
+        if (policy == null) return;
+        allowCalls = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_CALLS) != 0;
+        allowMessages = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_MESSAGES) != 0;
+        allowEvents = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_EVENTS) != 0;
+        allowReminders = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_REMINDERS) != 0;
+        allowRepeatCallers = (policy.priorityCategories & Policy.PRIORITY_CATEGORY_REPEAT_CALLERS)
+                != 0;
+        switch (policy.prioritySenders) {
+            case Policy.PRIORITY_SENDERS_CONTACTS:
+                allowFrom = SOURCE_CONTACT;
+                break;
+            case Policy.PRIORITY_SENDERS_STARRED:
+                allowFrom = SOURCE_STAR;
+                break;
+            default:
+                allowFrom = SOURCE_ANYONE;
+                break;
+        }
+    }
+
     public static Condition toTimeCondition(Context context, int minutesFromNow, int userHandle) {
         final long now = System.currentTimeMillis();
         final long millis = minutesFromNow == 0 ? ZERO_VALUE_MS : minutesFromNow * MINUTES_MS;
@@ -881,4 +935,5 @@
     public interface Migration {
         ZenModeConfig migrate(XmlV1 v1);
     }
+
 }
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index 1be05f3..93ccd1d 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -17,6 +17,7 @@
 package android.widget;
 
 import android.R;
+import android.annotation.Nullable;
 import android.app.PendingIntent;
 import android.app.PendingIntent.CanceledException;
 import android.content.ClipData;
@@ -1281,74 +1282,79 @@
                 EXTRACT_UNKNOWN, outText);
     }
 
-    private boolean extractTextInternal(ExtractedTextRequest request,
+    private boolean extractTextInternal(@Nullable ExtractedTextRequest request,
             int partialStartOffset, int partialEndOffset, int delta,
-            ExtractedText outText) {
-        final CharSequence content = mTextView.getText();
-        if (content != null) {
-            if (partialStartOffset != EXTRACT_NOTHING) {
-                final int N = content.length();
-                if (partialStartOffset < 0) {
-                    outText.partialStartOffset = outText.partialEndOffset = -1;
-                    partialStartOffset = 0;
-                    partialEndOffset = N;
-                } else {
-                    // Now use the delta to determine the actual amount of text
-                    // we need.
-                    partialEndOffset += delta;
-                    // Adjust offsets to ensure we contain full spans.
-                    if (content instanceof Spanned) {
-                        Spanned spanned = (Spanned)content;
-                        Object[] spans = spanned.getSpans(partialStartOffset,
-                                partialEndOffset, ParcelableSpan.class);
-                        int i = spans.length;
-                        while (i > 0) {
-                            i--;
-                            int j = spanned.getSpanStart(spans[i]);
-                            if (j < partialStartOffset) partialStartOffset = j;
-                            j = spanned.getSpanEnd(spans[i]);
-                            if (j > partialEndOffset) partialEndOffset = j;
-                        }
-                    }
-                    outText.partialStartOffset = partialStartOffset;
-                    outText.partialEndOffset = partialEndOffset - delta;
-
-                    if (partialStartOffset > N) {
-                        partialStartOffset = N;
-                    } else if (partialStartOffset < 0) {
-                        partialStartOffset = 0;
-                    }
-                    if (partialEndOffset > N) {
-                        partialEndOffset = N;
-                    } else if (partialEndOffset < 0) {
-                        partialEndOffset = 0;
-                    }
-                }
-                if ((request.flags&InputConnection.GET_TEXT_WITH_STYLES) != 0) {
-                    outText.text = content.subSequence(partialStartOffset,
-                            partialEndOffset);
-                } else {
-                    outText.text = TextUtils.substring(content, partialStartOffset,
-                            partialEndOffset);
-                }
-            } else {
-                outText.partialStartOffset = 0;
-                outText.partialEndOffset = 0;
-                outText.text = "";
-            }
-            outText.flags = 0;
-            if (MetaKeyKeyListener.getMetaState(content, MetaKeyKeyListener.META_SELECTING) != 0) {
-                outText.flags |= ExtractedText.FLAG_SELECTING;
-            }
-            if (mTextView.isSingleLine()) {
-                outText.flags |= ExtractedText.FLAG_SINGLE_LINE;
-            }
-            outText.startOffset = 0;
-            outText.selectionStart = mTextView.getSelectionStart();
-            outText.selectionEnd = mTextView.getSelectionEnd();
-            return true;
+            @Nullable ExtractedText outText) {
+        if (request == null || outText == null) {
+            return false;
         }
-        return false;
+
+        final CharSequence content = mTextView.getText();
+        if (content == null) {
+            return false;
+        }
+
+        if (partialStartOffset != EXTRACT_NOTHING) {
+            final int N = content.length();
+            if (partialStartOffset < 0) {
+                outText.partialStartOffset = outText.partialEndOffset = -1;
+                partialStartOffset = 0;
+                partialEndOffset = N;
+            } else {
+                // Now use the delta to determine the actual amount of text
+                // we need.
+                partialEndOffset += delta;
+                // Adjust offsets to ensure we contain full spans.
+                if (content instanceof Spanned) {
+                    Spanned spanned = (Spanned)content;
+                    Object[] spans = spanned.getSpans(partialStartOffset,
+                            partialEndOffset, ParcelableSpan.class);
+                    int i = spans.length;
+                    while (i > 0) {
+                        i--;
+                        int j = spanned.getSpanStart(spans[i]);
+                        if (j < partialStartOffset) partialStartOffset = j;
+                        j = spanned.getSpanEnd(spans[i]);
+                        if (j > partialEndOffset) partialEndOffset = j;
+                    }
+                }
+                outText.partialStartOffset = partialStartOffset;
+                outText.partialEndOffset = partialEndOffset - delta;
+
+                if (partialStartOffset > N) {
+                    partialStartOffset = N;
+                } else if (partialStartOffset < 0) {
+                    partialStartOffset = 0;
+                }
+                if (partialEndOffset > N) {
+                    partialEndOffset = N;
+                } else if (partialEndOffset < 0) {
+                    partialEndOffset = 0;
+                }
+            }
+            if ((request.flags&InputConnection.GET_TEXT_WITH_STYLES) != 0) {
+                outText.text = content.subSequence(partialStartOffset,
+                        partialEndOffset);
+            } else {
+                outText.text = TextUtils.substring(content, partialStartOffset,
+                        partialEndOffset);
+            }
+        } else {
+            outText.partialStartOffset = 0;
+            outText.partialEndOffset = 0;
+            outText.text = "";
+        }
+        outText.flags = 0;
+        if (MetaKeyKeyListener.getMetaState(content, MetaKeyKeyListener.META_SELECTING) != 0) {
+            outText.flags |= ExtractedText.FLAG_SELECTING;
+        }
+        if (mTextView.isSingleLine()) {
+            outText.flags |= ExtractedText.FLAG_SINGLE_LINE;
+        }
+        outText.startOffset = 0;
+        outText.selectionStart = mTextView.getSelectionStart();
+        outText.selectionEnd = mTextView.getSelectionEnd();
+        return true;
     }
 
     boolean reportExtractedText() {
diff --git a/core/res/res/values/attrs_manifest.xml b/core/res/res/values/attrs_manifest.xml
index 5ffe57e..0ac366d 100644
--- a/core/res/res/values/attrs_manifest.xml
+++ b/core/res/res/values/attrs_manifest.xml
@@ -907,10 +907,11 @@
          what gets persisted. -->
     <attr name="persistableMode">
         <!-- The default. If this activity forms the root of a task then that task will be
-             persisted across reboots but only the launching intent will be used. All
-             activities above this activity in the task will not be persisted. In addition
-             this activity will not be passed a PersistableBundle into which it could have
-             stored its state. -->
+             persisted across reboots but only the launching intent will be used. If the task
+             relinquishes its identity then the intent used is that of the topmost inherited
+             identity. All activities above this activity in the task will not be persisted.
+             In addition this activity will not be passed a PersistableBundle into which it
+             could have stored its state. -->
         <enum name="persistRootOnly" value="0" />
         <!-- If this activity forms the root of a task then that task will not be persisted
              across reboots -->
@@ -1038,6 +1039,47 @@
          activity. -->
     <attr name="resizeableActivity" format="boolean" />
 
+    <!-- This value indicates how tasks rooted at this activity will behave in lockTask mode.
+         While in lockTask mode the system will not launch non-permitted tasks until
+         lockTask mode is disabled.
+         <p>While in lockTask mode with multiple permitted tasks running, each launched task is
+         permitted to finish, transitioning to the previous locked task, until there is only one
+         task remaining. At that point the last task running is not permitted to finish. -->
+    <attr name="lockTaskMode">
+        <!-- This is the default value. Tasks will not launch into lockTask mode but can be
+             placed there by calling {@link android.app.Activity#startLockTask}. If a task with
+             this mode has been whitelisted using {@link
+             android.app.admin.DevicePolicyManager#setLockTaskPackages} then calling startLockTask
+             will enter lockTask mode immediately, otherwise the user will be presented with a
+             dialog to approve entering lockTask mode.
+             <p>If the system is already in lockTask mode when a new task rooted at this activity
+             is launched that task will or will not start depending on whether the package of this
+             activity has been whitelisted.
+             <p>Tasks rooted at this activity can only exit lockTask mode using stopLockTask(). -->
+        <enum name="lockTaskModeDefault" value="0"/>
+        <!-- Tasks will not launch into lockTask mode and cannot be placed there using
+             {@link android.app.Activity#startLockTask} or be pinned from the Overview screen.
+             If the system is already in lockTask mode when a new task rooted at this activity is
+             launched that task will not be started.
+             <p>Note: This mode is only available to system and privileged applications.
+             Non-privileged apps with this value will be treated as lockTaskModeDefault.
+             -->
+        <enum name="lockTaskModeNever" value="1"/>
+        <!-- Tasks rooted at this activity will always launch into lockTask mode. If the system is
+             already in lockTask mode when this task is launched then the new task will be launched
+             on top of the current task. Tasks launched in this mode are capable of exiting
+             lockTask mode using finish(), whereas tasks entering lockTask mode using
+             startLockTask() must use stopLockTask() to exit.
+             <p>Note: This mode is only available to system and privileged applications.
+             Non-privileged apps with this value will be treated as lockTaskModeDefault.
+             -->
+        <enum name="lockTaskModeAlways" value="2"/>
+        <!-- If the DevicePolicyManager (DPM) authorizes this package ({@link
+             android.app.admin.DevicePolicyManager#setLockTaskPackages}) then this mode is
+             identical to lockTaskModeAlways. If the DPM does not authorize this package then this
+             mode is identical to lockTaskModeDefault. -->
+        <enum name="lockTaskModeIfWhitelisted" value="3"/>
+    </attr>
     <!-- When set installer will extract native libraries. If set to false
          libraries in the apk must be stored and page-aligned.  -->
     <attr name="extractNativeLibs" format="boolean"/>
@@ -1684,7 +1726,7 @@
          {@link android.app.Activity} class that is available
          as part of the package's application components, implementing
          a part of the application's user interface.
-         
+
          <p>Zero or more {@link #AndroidManifestIntentFilter intent-filter}
          tags can be included inside of an activity, to specify the Intents
          that it can handle.  If none are specified, the activity can
@@ -1746,12 +1788,13 @@
         <attr name="relinquishTaskIdentity" />
         <attr name="resumeWhilePausing" />
         <attr name="resizeableActivity" />
+        <attr name="lockTaskMode" />
     </declare-styleable>
-    
+
     <!-- The <code>activity-alias</code> tag declares a new
          name for an existing {@link #AndroidManifestActivity activity}
          tag.
-         
+
          <p>Zero or more {@link #AndroidManifestIntentFilter intent-filter}
          tags can be included inside of an activity-alias, to specify the Intents
          that it can handle.  If none are specified, the activity can
@@ -1769,7 +1812,7 @@
              must be in the same manifest as the alias, and have been defined
              in that manifest before the alias here.  This must use a Java-style
              naming convention to ensure the name is unique, for example
-             "com.mycompany.MyName". -->  
+             "com.mycompany.MyName". -->
         <attr name="targetActivity" format="string" />
         <attr name="label" />
         <attr name="description" />
@@ -1785,7 +1828,7 @@
         <attr name="exported" />
         <attr name="parentActivityName" />
     </declare-styleable>
-    
+
     <!-- The <code>meta-data</code> tag is used to attach additional
          arbitrary data to an application component.  The data can later
          be retrieved programmatically from the
diff --git a/core/res/res/values/public.xml b/core/res/res/values/public.xml
index c2f2c6d..8806869 100644
--- a/core/res/res/values/public.xml
+++ b/core/res/res/values/public.xml
@@ -2663,4 +2663,6 @@
 
   <!-- Animation -->
   <public type="attr" name="durationScaleHint" />
+
+  <public type="attr" name="lockTaskMode" />
 </resources>
diff --git a/media/java/android/media/MediaCodec.java b/media/java/android/media/MediaCodec.java
index 59fccda..d82afdf 100644
--- a/media/java/android/media/MediaCodec.java
+++ b/media/java/android/media/MediaCodec.java
@@ -669,10 +669,10 @@
      * Thrown when an internal codec error occurs.
      */
     public final static class CodecException extends IllegalStateException {
-        CodecException(int errorCode, int actionCode, String detailMessage) {
+        CodecException(int errorCode, int actionCode, String detailMessage, int reason) {
             super(detailMessage);
             mErrorCode = errorCode;
-            mReason = REASON_HARDWARE;
+            mReason = reason;
             mActionCode = actionCode;
 
             // TODO get this from codec
diff --git a/media/jni/android_media_MediaCodec.cpp b/media/jni/android_media_MediaCodec.cpp
index 16758d0..5f586a9 100644
--- a/media/jni/android_media_MediaCodec.cpp
+++ b/media/jni/android_media_MediaCodec.cpp
@@ -70,6 +70,11 @@
     jint codecActionRecoverable;
 } gCodecActionCodes;
 
+static struct ExceptionReason {
+    jint reasonHardware;
+    jint reasonReclaimed;
+} gExceptionReason;
+
 struct fields_t {
     jfieldID context;
     jmethodID postEventFromNativeID;
@@ -568,7 +573,7 @@
             env, env->FindClass("android/media/MediaCodec$CodecException"));
     CHECK(clazz.get() != NULL);
 
-    const jmethodID ctor = env->GetMethodID(clazz.get(), "<init>", "(IILjava/lang/String;)V");
+    const jmethodID ctor = env->GetMethodID(clazz.get(), "<init>", "(IILjava/lang/String;I)V");
     CHECK(ctor != NULL);
 
     ScopedLocalRef<jstring> msgObj(
@@ -587,7 +592,9 @@
         break;
     }
 
-    return (jthrowable)env->NewObject(clazz.get(), ctor, err, actionCode, msgObj.get());
+    // TODO: propagate reason from MediaCodec.
+    int reason = gExceptionReason.reasonHardware;
+    return (jthrowable)env->NewObject(clazz.get(), ctor, err, actionCode, msgObj.get(), reason);
 }
 
 void JMediaCodec::handleCallback(const sp<AMessage> &msg) {
@@ -1454,6 +1461,16 @@
     CHECK(field != NULL);
     gCodecActionCodes.codecActionRecoverable =
         env->GetStaticIntField(clazz.get(), field);
+
+    field = env->GetStaticFieldID(clazz.get(), "REASON_HARDWARE", "I");
+    CHECK(field != NULL);
+    gExceptionReason.reasonHardware =
+        env->GetStaticIntField(clazz.get(), field);
+
+    field = env->GetStaticFieldID(clazz.get(), "REASON_RECLAIMED", "I");
+    CHECK(field != NULL);
+    gExceptionReason.reasonReclaimed =
+        env->GetStaticIntField(clazz.get(), field);
 }
 
 static void android_media_MediaCodec_native_setup(
diff --git a/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java b/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java
index a02fb4a..50c9f2d 100644
--- a/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java
+++ b/packages/Keyguard/src/com/android/keyguard/KeyguardUpdateMonitor.java
@@ -783,21 +783,14 @@
 
     private void startListeningForFingerprint() {
         if (DEBUG) Log.v(TAG, "startListeningForFingerprint()");
-        final int userId;
-        try {
-            userId = ActivityManagerNative.getDefault().getCurrentUser().id;
-        } catch (RemoteException e) {
-            Log.e(TAG, "Failed to get current user id: ", e);
-            return;
-        }
+        int userId = ActivityManager.getCurrentUser();
         if (mFpm != null && mFpm.isHardwareDetected() && !isFingerprintDisabled(userId)
-                && mFpm.getEnrolledFingerprints().size() > 0) {
+                && mFpm.getEnrolledFingerprints(userId).size() > 0) {
             if (mFingerprintCancelSignal != null) {
                 mFingerprintCancelSignal.cancel();
             }
             mFingerprintCancelSignal = new CancellationSignal();
-            mFpm.authenticate(null, mFingerprintCancelSignal, mAuthenticationCallback, 0,
-                    ActivityManager.getCurrentUser());
+            mFpm.authenticate(null, mFingerprintCancelSignal, mAuthenticationCallback, 0, userId);
             setFingerprintRunningDetectionRunning(true);
         }
     }
diff --git a/rs/java/android/renderscript/AllocationAdapter.java b/rs/java/android/renderscript/AllocationAdapter.java
index 35d59dd..9bfd6ec 100644
--- a/rs/java/android/renderscript/AllocationAdapter.java
+++ b/rs/java/android/renderscript/AllocationAdapter.java
@@ -208,7 +208,7 @@
     }
 
     /**
-     *
+     * @hide
      */
     public void setArray(int arrayNum, int arrayVal) {
         if (mAdaptedAllocation.getType().getArray(arrayNum) == 0) {
diff --git a/rs/java/android/renderscript/RenderScript.java b/rs/java/android/renderscript/RenderScript.java
index 7c927fd..e7f210b 100644
--- a/rs/java/android/renderscript/RenderScript.java
+++ b/rs/java/android/renderscript/RenderScript.java
@@ -951,6 +951,17 @@
         rsnScriptIntrinsicBLAS_Z(mContext, id, func, TransA, TransB, Side, Uplo, Diag, M, N, K, alphaX, alphaY, A, B, betaX, betaY, C, incX, incY, KL, KU);
     }
 
+    native void rsnScriptIntrinsicBLAS_BNNM(long con, long id, int M, int N, int K,
+                                             long A, int a_offset, long B, int b_offset, long C, int c_offset,
+                                             int c_mult_int);
+    synchronized void nScriptIntrinsicBLAS_BNNM(long id, int M, int N, int K,
+                                             long A, int a_offset, long B, int b_offset, long C, int c_offset,
+                                             int c_mult_int) {
+        validate();
+        rsnScriptIntrinsicBLAS_BNNM(mContext, id, M, N, K, A, a_offset, B, b_offset, C, c_offset, c_mult_int);
+    }
+
+
 
     long     mDev;
     long     mContext;
diff --git a/rs/java/android/renderscript/ScriptIntrinsicBLAS.java b/rs/java/android/renderscript/ScriptIntrinsicBLAS.java
index 90d2300..16b7033 100644
--- a/rs/java/android/renderscript/ScriptIntrinsicBLAS.java
+++ b/rs/java/android/renderscript/ScriptIntrinsicBLAS.java
@@ -176,6 +176,9 @@
     private static final int RsBlas_zherk = 141;
     private static final int RsBlas_zher2k = 142;
 
+    // BLAS extensions start here
+    private static final int RsBlas_bnnm = 1000;
+
     /**
      */
     public static ScriptIntrinsicBLAS create(RenderScript rs) {
@@ -1485,5 +1488,23 @@
     }
 
 
+    /**
+     *
+     * 8-bit GEMM-like operation for neural networks
+     *
+     * @hide
+     **/
+    public void BNNM(Allocation A, int a_offset, Allocation B, int b_offset, Allocation C, int c_offset, int c_mult) {
+        validateL3(Element.U8(mRS), NO_TRANSPOSE, TRANSPOSE, 0, A, B, C);
+
+        int M = -1, N = -1, K = -1;
+        M = A.getType().getY();
+        N = B.getType().getY();
+        K = A.getType().getX();
+
+
+        mRS.nScriptIntrinsicBLAS_BNNM(getID(mRS), M, N, K, A.getID(mRS), a_offset, B.getID(mRS), b_offset, C.getID(mRS), c_offset, c_mult);
+
+    }
 
 }
diff --git a/rs/java/android/renderscript/Type.java b/rs/java/android/renderscript/Type.java
index cc9b58b..dc23785 100644
--- a/rs/java/android/renderscript/Type.java
+++ b/rs/java/android/renderscript/Type.java
@@ -150,6 +150,7 @@
     }
 
     /**
+     * @hide
       * Return the dimension of the specified array.
       *
       * @param arrayNum  The array dimension to query
@@ -169,6 +170,7 @@
     }
 
     /**
+     * @hide
       * Return the number of array dimensions.
       *
       * @return int
@@ -382,6 +384,7 @@
         }
 
         /**
+         * @hide
          * Adds an array dimension to the builder
          *
          * @param dim
diff --git a/rs/jni/android_renderscript_RenderScript.cpp b/rs/jni/android_renderscript_RenderScript.cpp
index 49afa6d..ae48a5f 100644
--- a/rs/jni/android_renderscript_RenderScript.cpp
+++ b/rs/jni/android_renderscript_RenderScript.cpp
@@ -579,6 +579,32 @@
 
 
 static void
+nScriptIntrinsicBLAS_BNNM(JNIEnv *_env, jobject _this, jlong con, jlong id, jint M, jint N, jint K,
+                                             jlong A, jint a_offset, jlong B, jint b_offset, jlong C, jint c_offset,
+                                             jint c_mult_int) {
+    RsBlasCall call;
+    memset(&call, 0, sizeof(call));
+    call.func = RsBlas_bnnm;
+    call.M = M;
+    call.N = N;
+    call.K = K;
+    call.a_offset = a_offset;
+    call.b_offset = b_offset;
+    call.c_offset = c_offset;
+    call.c_mult_int = c_mult_int;
+
+    RsAllocation in_allocs[3];
+    in_allocs[0] = (RsAllocation)A;
+    in_allocs[1] = (RsAllocation)B;
+    in_allocs[2] = (RsAllocation)C;
+
+    rsScriptForEachMulti((RsContext)con, (RsScript)id, 0,
+                         in_allocs, sizeof(in_allocs), nullptr,
+                         &call, sizeof(call), nullptr, 0);
+}
+
+
+static void
 nAssignName(JNIEnv *_env, jobject _this, jlong con, jlong obj, jbyteArray str)
 {
     if (kLogApi) {
@@ -2417,6 +2443,8 @@
 {"rsnScriptIntrinsicBLAS_Complex",   "(JJIIIIIIIIIFFJJFFJIIII)V",             (void*)nScriptIntrinsicBLAS_Complex },
 {"rsnScriptIntrinsicBLAS_Z",         "(JJIIIIIIIIIDDJJDDJIIII)V",             (void*)nScriptIntrinsicBLAS_Z },
 
+{"rsnScriptIntrinsicBLAS_BNNM",      "(JJIIIJIJIJII)V",                       (void*)nScriptIntrinsicBLAS_BNNM },
+
 {"rsnProgramStoreCreate",            "(JZZZZZZIII)J",                         (void*)nProgramStoreCreate },
 
 {"rsnProgramBindConstants",          "(JJIJ)V",                               (void*)nProgramBindConstants },
diff --git a/services/core/java/com/android/server/am/ActivityManagerService.java b/services/core/java/com/android/server/am/ActivityManagerService.java
index b606353..f25808b 100644
--- a/services/core/java/com/android/server/am/ActivityManagerService.java
+++ b/services/core/java/com/android/server/am/ActivityManagerService.java
@@ -26,10 +26,14 @@
 import static com.android.internal.util.XmlUtils.writeBooleanAttribute;
 import static com.android.internal.util.XmlUtils.writeIntAttribute;
 import static com.android.internal.util.XmlUtils.writeLongAttribute;
-import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
-import static com.android.server.am.ActivityManagerDebugConfig.*;
-import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
 import static com.android.server.Watchdog.NATIVE_STACKS_OF_INTEREST;
+import static com.android.server.am.ActivityManagerDebugConfig.*;
+import static com.android.server.am.ActivityStackSupervisor.HOME_STACK_ID;
+import static com.android.server.am.TaskRecord.INVALID_TASK_ID;
+import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
+import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
+import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
+import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
 import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
 import static org.xmlpull.v1.XmlPullParser.START_TAG;
 
@@ -3982,13 +3986,13 @@
             if (rootR == null) {
                 Slog.w(TAG, "Finishing task with all activities already finished");
             }
-            // Do not allow task to finish in Lock Task mode.
-            if (tr == mStackSupervisor.mLockTaskModeTask) {
-                if (rootR == r) {
-                    Slog.i(TAG, "Not finishing task in lock task mode");
-                    mStackSupervisor.showLockTaskToast();
-                    return false;
-                }
+            // Do not allow task to finish if last task in lockTask mode. Launchable apps can
+            // finish themselves.
+            if (tr.mLockTaskAuth != LOCK_TASK_AUTH_LAUNCHABLE && rootR == r &&
+                    mStackSupervisor.isLastLockedTask(tr)) {
+                Slog.i(TAG, "Not finishing task in lock task mode");
+                mStackSupervisor.showLockTaskToast();
+                return false;
             }
             if (mController != null) {
                 // Find the first activity that is not finishing.
@@ -4142,20 +4146,18 @@
             final long origId = Binder.clearCallingIdentity();
             try {
                 ActivityRecord r = ActivityRecord.isInStackLocked(token);
+                if (r == null) {
+                    return false;
+                }
 
-                ActivityRecord rootR = r.task.getRootActivity();
-                // Do not allow task to finish in Lock Task mode.
-                if (r.task == mStackSupervisor.mLockTaskModeTask) {
-                    if (rootR == r) {
-                        mStackSupervisor.showLockTaskToast();
-                        return false;
-                    }
+                // Do not allow the last non-launchable task to finish in Lock Task mode.
+                final TaskRecord task = r.task;
+                if (task.mLockTaskAuth != LOCK_TASK_AUTH_LAUNCHABLE &&
+                        mStackSupervisor.isLastLockedTask(task) && task.getRootActivity() == r) {
+                    mStackSupervisor.showLockTaskToast();
+                    return false;
                 }
-                boolean res = false;
-                if (r != null) {
-                    res = r.task.stack.finishActivityAffinityLocked(r);
-                }
-                return res;
+                return task.stack.finishActivityAffinityLocked(r);
             } finally {
                 Binder.restoreCallingIdentity(origId);
             }
@@ -8336,9 +8338,9 @@
             final long origId = Binder.clearCallingIdentity();
             try {
                 int taskId = ActivityRecord.getTaskForActivityLocked(token, !nonRoot);
-                if (taskId >= 0) {
-                    if ((mStackSupervisor.mLockTaskModeTask != null)
-                            && (mStackSupervisor.mLockTaskModeTask.taskId == taskId)) {
+                final TaskRecord task = mRecentTasks.taskForIdLocked(taskId);
+                if (task != null) {
+                    if (mStackSupervisor.isLockedTask(task)) {
                         mStackSupervisor.showLockTaskToast();
                         return false;
                     }
@@ -8520,47 +8522,45 @@
 
     @Override
     public void updateLockTaskPackages(int userId, String[] packages) {
-        if (Binder.getCallingUid() != Process.SYSTEM_UID) {
+        final int callingUid = Binder.getCallingUid();
+        if (callingUid != 0 && callingUid != Process.SYSTEM_UID) {
             throw new SecurityException("updateLockTaskPackage called from non-system process");
         }
         synchronized (this) {
             mLockTaskPackages.put(userId, packages);
+            mStackSupervisor.onLockTaskPackagesUpdatedLocked();
         }
     }
 
-    private boolean isLockTaskAuthorizedLocked(String pkg) {
-        String[] packages = mLockTaskPackages.get(mCurrentUserId);
-        if (packages == null) {
-            return false;
-        }
-        for (int i = packages.length - 1; i >= 0; --i) {
-            if (pkg.equals(packages[i])) {
-                return true;
-            }
-        }
-        return false;
-    }
 
     void startLockTaskModeLocked(TaskRecord task) {
-        final String pkg = task.intent.getComponent().getPackageName();
+        if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
+            return;
+        }
+
         // isSystemInitiated is used to distinguish between locked and pinned mode, as pinned mode
         // is initiated by system after the pinning request was shown and locked mode is initiated
         // by an authorized app directly
-        boolean isSystemInitiated = Binder.getCallingUid() == Process.SYSTEM_UID;
+        final int callingUid = Binder.getCallingUid();
+        boolean isSystemInitiated = callingUid == Process.SYSTEM_UID;
         long ident = Binder.clearCallingIdentity();
         try {
-            if (!isSystemInitiated && !isLockTaskAuthorizedLocked(pkg)) {
-                StatusBarManagerInternal statusBarManager =
-                        LocalServices.getService(StatusBarManagerInternal.class);
-                if (statusBarManager != null) {
-                    statusBarManager.showScreenPinningRequest();
-                }
-                return;
-            }
-
             final ActivityStack stack = mStackSupervisor.getFocusedStack();
-            if (!isSystemInitiated && (stack == null || task != stack.topTask())) {
-                throw new IllegalArgumentException("Invalid task, not in foreground");
+            if (!isSystemInitiated) {
+                task.mLockTaskUid = callingUid;
+                if (task.mLockTaskAuth == LOCK_TASK_AUTH_PINNABLE) {
+                    // startLockTask() called by app and task mode is lockTaskModeDefault.
+                    StatusBarManagerInternal statusBarManager =
+                            LocalServices.getService(StatusBarManagerInternal.class);
+                    if (statusBarManager != null) {
+                        statusBarManager.showScreenPinningRequest();
+                    }
+                    return;
+                }
+
+                if (stack == null || task != stack.topTask()) {
+                    throw new IllegalArgumentException("Invalid task, not in foreground");
+                }
             }
             mStackSupervisor.setLockTaskModeLocked(task, isSystemInitiated ?
                     ActivityManager.LOCK_TASK_MODE_PINNED :
@@ -8614,23 +8614,15 @@
 
     @Override
     public void stopLockTaskMode() {
-        // Verify that the user matches the package of the intent for the TaskRecord
-        // we are locked to or systtem.  This will ensure the same caller for startLockTaskMode
-        // and stopLockTaskMode.
-        final int callingUid = Binder.getCallingUid();
-        if (callingUid != Process.SYSTEM_UID) {
-            try {
-                String pkg =
-                        mStackSupervisor.mLockTaskModeTask.intent.getComponent().getPackageName();
-                int uid = mContext.getPackageManager().getPackageUid(pkg,
-                        Binder.getCallingUserHandle().getIdentifier());
-                if (uid != callingUid) {
-                    throw new SecurityException("Invalid uid, expected " + uid);
-                }
-            } catch (NameNotFoundException e) {
-                Log.d(TAG, "stopLockTaskMode " + e);
-                return;
-            }
+        final TaskRecord lockTask = mStackSupervisor.getLockedTaskLocked();
+        if (lockTask == null) {
+            // Our work here is done.
+            return;
+        }
+        // Ensure the same caller for startLockTaskMode and stopLockTaskMode.
+        if (getLockTaskModeState() == ActivityManager.LOCK_TASK_MODE_LOCKED &&
+                Binder.getCallingUid() != lockTask.mLockTaskUid) {
+            throw new SecurityException("Invalid uid, expected " + lockTask.mLockTaskUid);
         }
         long ident = Binder.clearCallingIdentity();
         try {
diff --git a/services/core/java/com/android/server/am/ActivityStack.java b/services/core/java/com/android/server/am/ActivityStack.java
index 2362d28..6210d60 100644
--- a/services/core/java/com/android/server/am/ActivityStack.java
+++ b/services/core/java/com/android/server/am/ActivityStack.java
@@ -2875,7 +2875,7 @@
             }
 
             if (endTask) {
-                mStackSupervisor.endLockTaskModeIfTaskEnding(task);
+                mStackSupervisor.removeLockedTaskLocked(task);
             }
         } else if (r.state != ActivityState.PAUSING) {
             // If the activity is PAUSING, we will complete the finish once
@@ -3674,8 +3674,7 @@
         }
 
         Slog.i(TAG, "moveTaskToBack: " + tr);
-
-        mStackSupervisor.endLockTaskModeIfTaskEnding(tr);
+        mStackSupervisor.removeLockedTaskLocked(tr);
 
         // If we have a watcher, preflight the move before committing to it.  First check
         // for *other* available tasks, but if none are available, then try again allowing the
@@ -4240,7 +4239,7 @@
      */
     void removeTask(TaskRecord task, String reason, boolean notMoving) {
         if (notMoving) {
-            mStackSupervisor.endLockTaskModeIfTaskEnding(task);
+            mStackSupervisor.removeLockedTaskLocked(task);
             mWindowManager.removeTask(task.taskId);
         }
 
@@ -4345,4 +4344,10 @@
         mFullscreen = Configuration.EMPTY.equals(mOverrideConfig);
         return !mOverrideConfig.equals(oldConfig);
     }
+
+    void onLockTaskPackagesUpdatedLocked() {
+        for (int taskNdx = mTaskHistory.size() - 1; taskNdx >= 0; --taskNdx) {
+            mTaskHistory.get(taskNdx).setLockTaskAuth();
+        }
+    }
 }
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index c2f6bfd..6908483 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -17,10 +17,14 @@
 package com.android.server.am;
 
 import static android.Manifest.permission.START_ANY_ACTIVITY;
+import static android.app.ActivityManager.LOCK_TASK_MODE_LOCKED;
+import static android.app.ActivityManager.LOCK_TASK_MODE_NONE;
+import static android.app.ActivityManager.LOCK_TASK_MODE_PINNED;
 import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK;
 import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
 import static android.content.Intent.FLAG_ACTIVITY_TASK_ON_HOME;
+import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
 import static android.content.pm.PackageManager.PERMISSION_GRANTED;
 import static com.android.server.am.ActivityManagerDebugConfig.*;
 import static com.android.server.am.ActivityManagerService.FIRST_SUPERVISOR_STACK_MSG;
@@ -28,6 +32,10 @@
 import static com.android.server.am.ActivityRecord.RECENTS_ACTIVITY_TYPE;
 import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
 import static com.android.server.am.ActivityStack.ActivityState.*;
+import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_DONT_LOCK;
+import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_LAUNCHABLE;
+import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_PINNABLE;
+import static com.android.server.am.TaskRecord.LOCK_TASK_AUTH_WHITELISTED;
 
 import android.app.Activity;
 import android.app.ActivityManager;
@@ -261,17 +269,17 @@
 
     // TODO: Add listener for removal of references.
     /** Mapping from (ActivityStack/TaskStack).mStackId to their current state */
-    private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<ActivityContainer>();
+    private SparseArray<ActivityContainer> mActivityContainers = new SparseArray<>();
 
     /** Mapping from displayId to display current state */
-    private final SparseArray<ActivityDisplay> mActivityDisplays =
-            new SparseArray<ActivityDisplay>();
+    private final SparseArray<ActivityDisplay> mActivityDisplays = new SparseArray<>();
 
     InputManagerInternal mInputManagerInternal;
 
-    /** If non-null then the task specified remains in front and no other tasks may be started
-     * until the task exits or #stopLockTaskMode() is called. */
-    TaskRecord mLockTaskModeTask;
+    /** The chain of tasks in lockTask mode. The current frontmost task is at the top, and tasks
+     * may be finished until there is only one entry left. If this is empty the system is not
+     * in lockTask mode. */
+    ArrayList<TaskRecord> mLockTaskModeTasks = new ArrayList<>();
     /** Store the current lock task mode. Possible values:
      * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActivityManager#LOCK_TASK_MODE_LOCKED},
      * {@link ActivityManager#LOCK_TASK_MODE_PINNED}
@@ -282,8 +290,7 @@
      */
     private LockTaskNotify mLockTaskNotify;
 
-    final ArrayList<PendingActivityLaunch> mPendingActivityLaunches
-            = new ArrayList<PendingActivityLaunch>();
+    final ArrayList<PendingActivityLaunch> mPendingActivityLaunches = new ArrayList<>();
 
     /** Used to keep resumeTopActivityLocked() from being entered recursively */
     boolean inResumeTopActivity;
@@ -796,7 +803,7 @@
             ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
             for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
                 final ActivityStack stack = stacks.get(stackNdx);
-                ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<RunningTaskInfo>();
+                ArrayList<RunningTaskInfo> stackTaskList = new ArrayList<>();
                 runningTaskLists.add(stackTaskList);
                 stack.getTasksLocked(stackTaskList, callingUid, allowed);
             }
@@ -894,8 +901,8 @@
         intent = new Intent(intent);
 
         // Collect information about the target of the Intent.
-        ActivityInfo aInfo = resolveActivity(intent, resolvedType, startFlags,
-                profilerInfo, userId);
+        ActivityInfo aInfo =
+                resolveActivity(intent, resolvedType, startFlags, profilerInfo, userId);
 
         ActivityContainer container = (ActivityContainer)iContainer;
         synchronized (mService) {
@@ -1170,7 +1177,12 @@
         mService.updateLruProcessLocked(app, true, null);
         mService.updateOomAdjLocked();
 
-        final ActivityStack stack = r.task.stack;
+        final TaskRecord task = r.task;
+        if (task.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE) {
+            setLockTaskModeLocked(task, LOCK_TASK_MODE_LOCKED, "lockTaskLaunchMode attribute");
+        }
+
+        final ActivityStack stack = task.stack;
         try {
             if (app.thread == null) {
                 throw new RemoteException();
@@ -1187,11 +1199,11 @@
             if (andResume) {
                 EventLog.writeEvent(EventLogTags.AM_RESTART_ACTIVITY,
                         r.userId, System.identityHashCode(r),
-                        r.task.taskId, r.shortComponentName);
+                        task.taskId, r.shortComponentName);
             }
             if (r.isHomeActivity() && r.isNotResolverActivity()) {
                 // Home process is the root process of the task.
-                mService.mHomeProcess = r.task.mActivities.get(0).app;
+                mService.mHomeProcess = task.mActivities.get(0).app;
             }
             mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName());
             r.sleeping = false;
@@ -1233,7 +1245,7 @@
             app.thread.scheduleLaunchActivity(new Intent(r.intent), r.appToken,
                     System.identityHashCode(r), r.info, new Configuration(mService.mConfiguration),
                     new Configuration(stack.mOverrideConfig), r.compat, r.launchedFromPackage,
-                    r.task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
+                    task.voiceInteractor, app.repProcState, r.icicle, r.persistentState, results,
                     newIntents, !andResume, mService.isNextTransitionForward(), profilerInfo);
 
             if ((app.info.privateFlags&ApplicationInfo.PRIVATE_FLAG_CANT_SAVE_STATE) != 0) {
@@ -1946,7 +1958,7 @@
                     if ((launchFlags&Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) != 0) {
                         intentActivity = targetStack.resetTaskIfNeededLocked(intentActivity, r);
                     }
-                    if ((startFlags&ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
+                    if ((startFlags & ActivityManager.START_FLAG_ONLY_IF_NEEDED) != 0) {
                         // We don't need to start a new activity, and
                         // the client said not to do anything if that
                         // is the case, so this is it!  And for paranoia, make
@@ -1964,8 +1976,7 @@
                         }
                         return ActivityManager.START_RETURN_INTENT_TO_CALLER;
                     }
-                    if ((launchFlags &
-                            (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
+                    if ((launchFlags & (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK))
                             == (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK)) {
                         // The caller has requested to completely replace any
                         // existing task with its new activity.  Well that should
@@ -2128,10 +2139,6 @@
         // Should this be considered a new task?
         if (r.resultTo == null && inTask == null && !addingToTask
                 && (launchFlags & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
-            if (isLockTaskModeViolation(reuseTask)) {
-                Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
-                return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
-            }
             newTask = true;
             targetStack = computeStackFocus(r, newTask);
             targetStack.moveToFront("startingNewTask");
@@ -2147,6 +2154,10 @@
             } else {
                 r.setTask(reuseTask, taskToAffiliate);
             }
+            if (isLockTaskModeViolation(r.task)) {
+                Slog.e(TAG, "Attempted Lock Task Mode violation r=" + r);
+                return ActivityManager.START_RETURN_LOCK_TASK_MODE_VIOLATION;
+            }
             if (!movedHome) {
                 if ((launchFlags &
                         (FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_TASK_ON_HOME))
@@ -3292,6 +3303,7 @@
         pw.print(prefix); pw.println("mCurTaskId=" + mCurTaskId);
         pw.print(prefix); pw.println("mUserStackInFront=" + mUserStackInFront);
         pw.print(prefix); pw.println("mActivityContainers=" + mActivityContainers);
+        pw.print(prefix); pw.println("mLockTaskModeTasks" + mLockTaskModeTasks);
     }
 
     ArrayList<ActivityRecord> getDumpActivitiesLocked(String name) {
@@ -3592,6 +3604,32 @@
         return list;
     }
 
+    TaskRecord getLockedTaskLocked() {
+        final int top = mLockTaskModeTasks.size() - 1;
+        if (top >= 0) {
+            return mLockTaskModeTasks.get(top);
+        }
+        return null;
+    }
+
+    boolean isLockedTask(TaskRecord task) {
+        return mLockTaskModeTasks.contains(task);
+    }
+
+    boolean isLastLockedTask(TaskRecord task) {
+        return mLockTaskModeTasks.size() == 1 && mLockTaskModeTasks.contains(task);
+    }
+
+    void removeLockedTaskLocked(final TaskRecord task) {
+        if (mLockTaskModeTasks.remove(task) && mLockTaskModeTasks.isEmpty()) {
+            // Last one.
+            final Message lockTaskMsg = Message.obtain();
+            lockTaskMsg.arg1 = task.userId;
+            lockTaskMsg.what = LOCK_TASK_END_MSG;
+            mHandler.sendMessage(lockTaskMsg);
+        }
+    }
+
     void showLockTaskToast() {
         mLockTaskNotify.showToast(mLockTaskModeState);
     }
@@ -3599,42 +3637,93 @@
     void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
         if (task == null) {
             // Take out of lock task mode if necessary
-            if (mLockTaskModeTask != null) {
-                final Message lockTaskMsg = Message.obtain();
-                lockTaskMsg.arg1 = mLockTaskModeTask.userId;
-                lockTaskMsg.what = LOCK_TASK_END_MSG;
-                mLockTaskModeTask = null;
-                mHandler.sendMessage(lockTaskMsg);
+            final TaskRecord lockedTask = getLockedTaskLocked();
+            if (lockedTask != null) {
+                removeLockedTaskLocked(lockedTask);
+                if (!mLockTaskModeTasks.isEmpty()) {
+                    // There are locked tasks remaining, can only finish this task, not unlock it.
+                    lockedTask.performClearTaskLocked();
+                    resumeTopActivitiesLocked();
+                    return;
+                }
             }
             return;
         }
-        if (isLockTaskModeViolation(task)) {
-            Slog.e(TAG, "setLockTaskMode: Attempt to start a second Lock Task Mode task.");
+
+        // Should have already been checked, but do it again.
+        if (task.mLockTaskAuth == LOCK_TASK_AUTH_DONT_LOCK) {
             return;
         }
-        mLockTaskModeTask = task;
+        if (isLockTaskModeViolation(task)) {
+            Slog.e(TAG, "setLockTaskMode: Attempt to start an unauthorized lock task.");
+            return;
+        }
+
+        if (mLockTaskModeTasks.isEmpty()) {
+            // First locktask.
+            final Message lockTaskMsg = Message.obtain();
+            lockTaskMsg.obj = task.intent.getComponent().getPackageName();
+            lockTaskMsg.arg1 = task.userId;
+            lockTaskMsg.what = LOCK_TASK_START_MSG;
+            lockTaskMsg.arg2 = lockTaskModeState;
+            mHandler.sendMessage(lockTaskMsg);
+        }
+        // Add it or move it to the top.
+        mLockTaskModeTasks.remove(task);
+        mLockTaskModeTasks.add(task);
+
+        if (task.mLockTaskUid == -1) {
+            task.mLockTaskUid = task.mCallingUid;
+        }
         findTaskToMoveToFrontLocked(task, 0, null, reason);
         resumeTopActivitiesLocked();
-
-        final Message lockTaskMsg = Message.obtain();
-        lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
-        lockTaskMsg.arg1 = mLockTaskModeTask.userId;
-        lockTaskMsg.what = LOCK_TASK_START_MSG;
-        lockTaskMsg.arg2 = lockTaskModeState;
-        mHandler.sendMessage(lockTaskMsg);
     }
 
     boolean isLockTaskModeViolation(TaskRecord task) {
-        return mLockTaskModeTask != null && mLockTaskModeTask != task;
+        if (getLockedTaskLocked() == task) {
+            return false;
+        }
+        final int lockTaskAuth = task.mLockTaskAuth;
+        switch (lockTaskAuth) {
+            case LOCK_TASK_AUTH_DONT_LOCK:
+                return !mLockTaskModeTasks.isEmpty();
+            case LOCK_TASK_AUTH_LAUNCHABLE:
+            case LOCK_TASK_AUTH_WHITELISTED:
+                return false;
+            case LOCK_TASK_AUTH_PINNABLE:
+                // Pinnable tasks can't be launched on top of locktask tasks.
+                return !mLockTaskModeTasks.isEmpty();
+            default:
+                Slog.w(TAG, "isLockTaskModeViolation: invalid lockTaskAuth value=" + lockTaskAuth);
+                return true;
+        }
     }
 
-    void endLockTaskModeIfTaskEnding(TaskRecord task) {
-        if (mLockTaskModeTask != null && mLockTaskModeTask == task) {
-            final Message lockTaskMsg = Message.obtain();
-            lockTaskMsg.arg1 = mLockTaskModeTask.userId;
-            lockTaskMsg.what = LOCK_TASK_END_MSG;
-            mLockTaskModeTask = null;
-            mHandler.sendMessage(lockTaskMsg);
+    void onLockTaskPackagesUpdatedLocked() {
+        boolean didSomething = false;
+        for (int taskNdx = mLockTaskModeTasks.size() - 1; taskNdx >= 0; --taskNdx) {
+            final TaskRecord lockedTask = mLockTaskModeTasks.get(taskNdx);
+            if (lockedTask.mLockTaskMode != LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED) {
+                continue;
+            }
+            final boolean wasLaunchable = lockedTask.mLockTaskAuth == LOCK_TASK_AUTH_LAUNCHABLE;
+            lockedTask.setLockTaskAuth();
+            if (wasLaunchable && lockedTask.mLockTaskAuth != LOCK_TASK_AUTH_LAUNCHABLE) {
+                // Lost whitelisting authorization. End it now.
+                removeLockedTaskLocked(lockedTask);
+                lockedTask.performClearTaskLocked();
+                didSomething = true;
+            }
+        }
+        for (int displayNdx = mActivityDisplays.size() - 1; displayNdx >= 0; --displayNdx) {
+            ArrayList<ActivityStack> stacks = mActivityDisplays.valueAt(displayNdx).mStacks;
+            for (int stackNdx = stacks.size() - 1; stackNdx >= 0; --stackNdx) {
+                final ActivityStack stack = stacks.get(stackNdx);
+                stack.onLockTaskPackagesUpdatedLocked();
+            }
+        }
+        if (didSomething) {
+            resumeTopActivitiesLocked();
         }
     }
 
@@ -3734,11 +3823,10 @@
                         mLockTaskModeState = msg.arg2;
                         if (getStatusBarService() != null) {
                             int flags = 0;
-                            if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_LOCKED) {
+                            if (mLockTaskModeState == LOCK_TASK_MODE_LOCKED) {
                                 flags = StatusBarManager.DISABLE_MASK
                                         & (~StatusBarManager.DISABLE_BACK);
-                            } else if (mLockTaskModeState ==
-                                    ActivityManager.LOCK_TASK_MODE_PINNED) {
+                            } else if (mLockTaskModeState == LOCK_TASK_MODE_PINNED) {
                                 flags = StatusBarManager.DISABLE_MASK
                                         & (~StatusBarManager.DISABLE_BACK)
                                         & (~StatusBarManager.DISABLE_HOME)
@@ -3776,8 +3864,7 @@
                             boolean shouldLockKeyguard = Settings.Secure.getInt(
                                     mService.mContext.getContentResolver(),
                                     Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
-                            if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_PINNED &&
-                                    shouldLockKeyguard) {
+                            if (mLockTaskModeState == LOCK_TASK_MODE_PINNED && shouldLockKeyguard) {
                                 mWindowManager.lockNow(null);
                                 mWindowManager.dismissKeyguard();
                                 new LockPatternUtils(mService.mContext)
@@ -3789,7 +3876,7 @@
                     } catch (RemoteException ex) {
                         throw new RuntimeException(ex);
                     } finally {
-                        mLockTaskModeState = ActivityManager.LOCK_TASK_MODE_NONE;
+                        mLockTaskModeState = LOCK_TASK_MODE_NONE;
                     }
                 } break;
                 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {
diff --git a/services/core/java/com/android/server/am/TaskRecord.java b/services/core/java/com/android/server/am/TaskRecord.java
index 82e6d47..790a78d 100644
--- a/services/core/java/com/android/server/am/TaskRecord.java
+++ b/services/core/java/com/android/server/am/TaskRecord.java
@@ -18,6 +18,10 @@
 
 import static android.content.Intent.FLAG_ACTIVITY_NEW_DOCUMENT;
 import static android.content.Intent.FLAG_ACTIVITY_RETAIN_IN_RECENTS;
+import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_ALWAYS;
+import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_DEFAULT;
+import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED;
+import static android.content.pm.ActivityInfo.LOCK_TASK_LAUNCH_MODE_NEVER;
 import static com.android.server.am.ActivityManagerDebugConfig.*;
 import static com.android.server.am.ActivityRecord.HOME_ACTIVITY_TYPE;
 import static com.android.server.am.ActivityRecord.APPLICATION_ACTIVITY_TYPE;
@@ -121,6 +125,20 @@
 
     boolean mResizeable;    // Activities in the task resizeable. Based on the resizable setting of
                             // the root activity.
+    int mLockTaskMode;      // Which tasklock mode to launch this task in. One of
+                            // ActivityManager.LOCK_TASK_LAUNCH_MODE_*
+    /** Can't be put in lockTask mode. */
+    final static int LOCK_TASK_AUTH_DONT_LOCK = 0;
+    /** Can enter lockTask with user approval if not already in lockTask. */
+    final static int LOCK_TASK_AUTH_PINNABLE = 1;
+    /** Starts in LOCK_TASK_MODE_LOCKED automatically. Can start over existing lockTask task. */
+    final static int LOCK_TASK_AUTH_LAUNCHABLE = 2;
+    /** Enters LOCK_TASK_MODE_LOCKED via startLockTask(), enters LOCK_TASK_MODE_PINNED from
+     * Overview. Can start over existing lockTask task. */
+    final static int LOCK_TASK_AUTH_WHITELISTED = 3;
+    int mLockTaskAuth = LOCK_TASK_AUTH_PINNABLE;
+
+    int mLockTaskUid = -1;  // The uid of the application that called startLockTask().
 
     // This represents the last resolved activity values for this task
     // NOTE: This value needs to be persisted with each task
@@ -186,6 +204,8 @@
         voiceInteractor = _voiceInteractor;
         isAvailable = true;
         mActivities = new ArrayList<>();
+        mCallingUid = info.applicationInfo.uid;
+        mCallingPackage = info.packageName;
         setIntent(_intent, info);
     }
 
@@ -201,12 +221,12 @@
         voiceInteractor = null;
         isAvailable = true;
         mActivities = new ArrayList<>();
+        mCallingUid = info.applicationInfo.uid;
+        mCallingPackage = info.packageName;
         setIntent(_intent, info);
 
         taskType = ActivityRecord.APPLICATION_ACTIVITY_TYPE;
         isPersistable = true;
-        mCallingUid = info.applicationInfo.uid;
-        mCallingPackage = info.packageName;
         // Clamp to [1, max].
         maxRecents = Math.min(Math.max(info.maxRecents, 1),
                 ActivityManager.getMaxAppRecentsLimitStatic());
@@ -215,8 +235,6 @@
         mTaskToReturnTo = HOME_ACTIVITY_TYPE;
         userId = UserHandle.getUserId(info.applicationInfo.uid);
         lastTaskDescription = _taskDescription;
-        mCallingUid = info.applicationInfo.uid;
-        mCallingPackage = info.packageName;
     }
 
     private TaskRecord(ActivityManagerService service, int _taskId, Intent _intent,
@@ -278,9 +296,9 @@
 
     /** Sets the original intent, and the calling uid and package. */
     void setIntent(ActivityRecord r) {
-        setIntent(r.intent, r.info);
         mCallingUid = r.launchedFromUid;
         mCallingPackage = r.launchedFromPackage;
+        setIntent(r.intent, r.info);
     }
 
     /** Sets the original intent, _without_ updating the calling uid or package. */
@@ -362,6 +380,8 @@
             autoRemoveRecents = false;
         }
         mResizeable = info.resizeable;
+        mLockTaskMode = info.lockTaskLaunchMode;
+        setLockTaskAuth();
     }
 
     void setTaskToReturnTo(int taskToReturnTo) {
@@ -716,6 +736,53 @@
         performClearTaskAtIndexLocked(0);
     }
 
+    private boolean isPrivileged() {
+        final ProcessRecord proc = mService.mProcessNames.get(mCallingPackage, mCallingUid);
+        if (proc != null) {
+                return (proc.info.privateFlags & ApplicationInfo.PRIVATE_FLAG_PRIVILEGED) != 0;
+        }
+        return false;
+    }
+
+    void setLockTaskAuth() {
+        switch (mLockTaskMode) {
+            case LOCK_TASK_LAUNCH_MODE_DEFAULT:
+                mLockTaskAuth = isLockTaskWhitelistedLocked() ?
+                    LOCK_TASK_AUTH_WHITELISTED : LOCK_TASK_AUTH_PINNABLE;
+                break;
+
+            case LOCK_TASK_LAUNCH_MODE_NEVER:
+                mLockTaskAuth = isPrivileged() ?
+                        LOCK_TASK_AUTH_DONT_LOCK : LOCK_TASK_AUTH_PINNABLE;
+                break;
+
+            case LOCK_TASK_LAUNCH_MODE_ALWAYS:
+                mLockTaskAuth = isPrivileged() ?
+                        LOCK_TASK_AUTH_LAUNCHABLE: LOCK_TASK_AUTH_PINNABLE;
+                break;
+
+            case LOCK_TASK_LAUNCH_MODE_IF_WHITELISTED:
+                mLockTaskAuth = isLockTaskWhitelistedLocked() ?
+                        LOCK_TASK_AUTH_LAUNCHABLE : LOCK_TASK_AUTH_PINNABLE;
+                break;
+        }
+    }
+
+    boolean isLockTaskWhitelistedLocked() {
+        if (mCallingPackage == null) {
+            return false;
+        }
+        String[] packages = mService.mLockTaskPackages.get(userId);
+        if (packages == null) {
+            return false;
+        }
+        for (int i = packages.length - 1; i >= 0; --i) {
+            if (mCallingPackage.equals(packages[i])) {
+                return true;
+            }
+        }
+        return false;
+    }
     boolean isHomeTask() {
         return taskType == HOME_ACTIVITY_TYPE;
     }
diff --git a/services/core/java/com/android/server/fingerprint/FingerprintService.java b/services/core/java/com/android/server/fingerprint/FingerprintService.java
index 90e69d7..0ca0c0e 100644
--- a/services/core/java/com/android/server/fingerprint/FingerprintService.java
+++ b/services/core/java/com/android/server/fingerprint/FingerprintService.java
@@ -358,6 +358,11 @@
         return result;
     }
 
+    public boolean hasEnrolledFingerprints(int groupId) {
+        ContentResolver resolver = mContext.getContentResolver();
+        return FingerprintUtils.getFingerprintIdsForUser(resolver, groupId).length > 0;
+    }
+
     void checkPermission(String permission) {
         getContext().enforceCallingOrSelfPermission(permission,
                 "Must have " + permission + " permission.");
@@ -572,6 +577,13 @@
             checkPermission(USE_FINGERPRINT);
             return FingerprintService.this.getEnrolledFingerprints(groupId);
         }
+
+        @Override
+        // Binder call
+        public boolean hasEnrolledFingerprints(int groupId) {
+            checkPermission(USE_FINGERPRINT);
+            return FingerprintService.this.hasEnrolledFingerprints(groupId);
+        }
     }
 
     @Override
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java
index 4cf2909..997d546 100644
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -29,9 +29,11 @@
 import android.app.AppOpsManager;
 import android.app.IActivityManager;
 import android.app.INotificationManager;
+import android.app.INotificationManagerCallback;
 import android.app.ITransientNotification;
 import android.app.Notification;
 import android.app.NotificationManager;
+import android.app.NotificationManager.Policy;
 import android.app.PendingIntent;
 import android.app.StatusBarManager;
 import android.content.BroadcastReceiver;
@@ -227,6 +229,8 @@
             new ArrayMap<String, NotificationRecord>();
     final ArrayList<ToastRecord> mToastQueue = new ArrayList<ToastRecord>();
     final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>();
+    private final ArrayMap<String, Policy.Token> mPolicyTokens = new ArrayMap<>();
+
 
     // The last key in this list owns the hardware.
     ArrayList<String> mLights = new ArrayList<>();
@@ -893,6 +897,13 @@
                     updateInterruptionFilterLocked();
                 }
             }
+
+            @Override
+            void onPolicyChanged() {
+                getContext().sendBroadcast(
+                        new Intent(NotificationManager.ACTION_NOTIFICATION_POLICY_CHANGED)
+                                .addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY));
+            }
         });
         final File systemDir = new File(Environment.getDataDirectory(), "system");
         mPolicyFile = new AtomicFile(new File(systemDir, "notification_policy.xml"));
@@ -1551,6 +1562,18 @@
                     message);
         }
 
+        private void enforcePolicyToken(Policy.Token token, String method) {
+            if (!checkPolicyToken(token)) {
+                Slog.w(TAG, "Invalid notification policy token calling " + method);
+                throw new SecurityException("Invalid notification policy token");
+            }
+        }
+
+        private boolean checkPolicyToken(Policy.Token token) {
+            return mPolicyTokens.containsValue(token)
+                    || mListeners.mPolicyTokens.containsValue(token);
+        }
+
         @Override
         protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
             if (getContext().checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
@@ -1586,24 +1609,73 @@
             enforceSystemOrSystemUIOrVolume("INotificationManager.isSystemConditionProviderEnabled");
             return mConditionProviders.isSystemProviderEnabled(path);
         }
-    };
 
-    private String[] getActiveNotificationKeys(INotificationListener token) {
-        final ManagedServiceInfo info = mListeners.checkServiceTokenLocked(token);
-        final ArrayList<String> keys = new ArrayList<String>();
-        if (info.isEnabledForCurrentProfiles()) {
-            synchronized (mNotificationList) {
-                final int N = mNotificationList.size();
-                for (int i = 0; i < N; i++) {
-                    final StatusBarNotification sbn = mNotificationList.get(i).sbn;
-                    if (info.enabledAndUserMatches(sbn.getUserId())) {
-                        keys.add(sbn.getKey());
-                    }
-                }
+        @Override
+        public Policy.Token getPolicyTokenFromListener(INotificationListener listener) {
+            final long identity = Binder.clearCallingIdentity();
+            try {
+                return mListeners.getPolicyToken(listener);
+            } finally {
+                Binder.restoreCallingIdentity(identity);
             }
         }
-        return keys.toArray(new String[keys.size()]);
-    }
+
+        @Override
+        public void requestNotificationPolicyToken(String pkg,
+                INotificationManagerCallback callback) throws RemoteException {
+            if (callback == null) {
+                Slog.w(TAG, "requestNotificationPolicyToken: no callback specified");
+                return;
+            }
+            if (pkg == null) {
+                Slog.w(TAG, "requestNotificationPolicyToken denied: no package specified");
+                callback.onPolicyToken(null);
+                return;
+            }
+            Policy.Token token = null;
+            final long identity = Binder.clearCallingIdentity();
+            try {
+                synchronized (mNotificationList) {
+                    token = mPolicyTokens.get(pkg);
+                    if (token == null) {
+                        token = new Policy.Token(new Binder());
+                        mPolicyTokens.put(pkg, token);
+                    }
+                    if (DBG) Slog.w(TAG, "requestNotificationPolicyToken granted for " + pkg);
+                }
+            } finally {
+                Binder.restoreCallingIdentity(identity);
+            }
+            callback.onPolicyToken(token);
+        }
+
+        @Override
+        public boolean isNotificationPolicyTokenValid(String pkg, Policy.Token token) {
+            return checkPolicyToken(token);
+        }
+
+        @Override
+        public Policy getNotificationPolicy(Policy.Token token) {
+            enforcePolicyToken(token, "getNotificationPolicy");
+            final long identity = Binder.clearCallingIdentity();
+            try {
+                return mZenModeHelper.getNotificationPolicy();
+            } finally {
+                Binder.restoreCallingIdentity(identity);
+            }
+        }
+
+        @Override
+        public void setNotificationPolicy(Policy.Token token, Policy policy) {
+            enforcePolicyToken(token, "setNotificationPolicy");
+            final long identity = Binder.clearCallingIdentity();
+            try {
+                mZenModeHelper.setNotificationPolicy(policy);
+            } finally {
+                Binder.restoreCallingIdentity(identity);
+            }
+        }
+    };
 
     private String disableNotificationEffects(NotificationRecord record) {
         if (mDisableNotificationEffects) {
@@ -1718,6 +1790,10 @@
                     pw.print(listener.component);
                 }
                 pw.println(')');
+                pw.print("    mPolicyTokens.keys: ");
+                pw.println(TextUtils.join(",", mPolicyTokens.keySet()));
+                pw.print("    mListeners.mPolicyTokens.keys: ");
+                pw.println(TextUtils.join(",", mListeners.mPolicyTokens.keySet()));
             }
 
             pw.println("\n  Condition providers:");
@@ -2970,12 +3046,18 @@
     public class NotificationListeners extends ManagedServices {
 
         private final ArraySet<ManagedServiceInfo> mLightTrimListeners = new ArraySet<>();
+        private final ArrayMap<ComponentName, Policy.Token> mPolicyTokens = new ArrayMap<>();
         private boolean mNotificationGroupsDesired;
 
         public NotificationListeners() {
             super(getContext(), mHandler, mNotificationList, mUserProfiles);
         }
 
+        public Policy.Token getPolicyToken(INotificationListener listener) {
+            final ManagedServiceInfo info = checkServiceTokenLocked(listener);
+            return info == null ? null : mPolicyTokens.get(info.component);
+        }
+
         @Override
         protected Config getConfig() {
             Config c = new Config();
@@ -3000,6 +3082,7 @@
             synchronized (mNotificationList) {
                 updateNotificationGroupsDesiredLocked();
                 update = makeRankingUpdateLocked(info);
+                mPolicyTokens.put(info.component, new Policy.Token(new Binder()));
             }
             try {
                 listener.onListenerConnected(update);
@@ -3016,6 +3099,7 @@
             }
             mLightTrimListeners.remove(removed);
             updateNotificationGroupsDesiredLocked();
+            mPolicyTokens.remove(removed.component);
         }
 
         public void setOnNotificationPostedTrimLocked(ManagedServiceInfo info, int trim) {
diff --git a/services/core/java/com/android/server/notification/ZenModeHelper.java b/services/core/java/com/android/server/notification/ZenModeHelper.java
index 40218bb..9cb8af5 100644
--- a/services/core/java/com/android/server/notification/ZenModeHelper.java
+++ b/services/core/java/com/android/server/notification/ZenModeHelper.java
@@ -21,6 +21,7 @@
 import static android.media.AudioAttributes.USAGE_NOTIFICATION_RINGTONE;
 
 import android.app.AppOpsManager;
+import android.app.NotificationManager.Policy;
 import android.content.ComponentName;
 import android.content.ContentResolver;
 import android.content.Context;
@@ -57,6 +58,7 @@
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.ArrayList;
+import java.util.Objects;
 
 /**
  * NotificationManagerService helper for functionality related to zen mode.
@@ -230,6 +232,21 @@
         mConfig.writeXml(out);
     }
 
+    public Policy getNotificationPolicy() {
+        return getNotificationPolicy(mConfig);
+    }
+
+    private static Policy getNotificationPolicy(ZenModeConfig config) {
+        return config == null ? null : config.toNotificationPolicy();
+    }
+
+    public void setNotificationPolicy(Policy policy) {
+        if (policy == null || mConfig == null) return;
+        final ZenModeConfig newConfig = mConfig.copy();
+        newConfig.applyNotificationPolicy(policy);
+        setConfig(newConfig, "setNotificationPolicy");
+    }
+
     public ZenModeConfig getConfig() {
         return mConfig;
     }
@@ -247,8 +264,13 @@
         if (config.equals(mConfig)) return true;
         if (DEBUG) Log.d(TAG, "setConfig reason=" + reason);
         ZenLog.traceConfig(mConfig, config);
+        final boolean policyChanged = !Objects.equals(getNotificationPolicy(mConfig),
+                getNotificationPolicy(config));
         mConfig = config;
         dispatchOnConfigChanged();
+        if (policyChanged){
+            dispatchOnPolicyChanged();
+        }
         final String val = Integer.toString(mConfig.hashCode());
         Global.putString(mContext.getContentResolver(), Global.ZEN_MODE_CONFIG_ETAG, val);
         if (!evaluateZenMode(reason, setRingerMode)) {
@@ -355,6 +377,12 @@
         }
     }
 
+    private void dispatchOnPolicyChanged() {
+        for (Callback callback : mCallbacks) {
+            callback.onPolicyChanged();
+        }
+    }
+
     private void dispatchOnZenModeChanged() {
         for (Callback callback : mCallbacks) {
             callback.onZenModeChanged();
@@ -617,6 +645,7 @@
     public static class Callback {
         void onConfigChanged() {}
         void onZenModeChanged() {}
+        void onPolicyChanged() {}
     }
 
 }
diff --git a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
index 452b3eb..e22a2cc 100644
--- a/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
+++ b/services/devicepolicy/java/com/android/server/devicepolicy/DevicePolicyManagerService.java
@@ -1638,10 +1638,13 @@
 
     private void updateLockTaskPackagesLocked(DevicePolicyData policy, int userId) {
         IActivityManager am = ActivityManagerNative.getDefault();
+        long ident = Binder.clearCallingIdentity();
         try {
             am.updateLockTaskPackages(userId, policy.mLockTaskPackages.toArray(new String[0]));
         } catch (RemoteException e) {
             // Not gonna happen.
+        } finally {
+            Binder.restoreCallingIdentity(ident);
         }
     }
 
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
index 8a28d51..7dce83e 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerService.java
@@ -393,12 +393,9 @@
                     throw new SecurityException(
                             "Caller is not the current voice interaction service");
                 }
-                final int callingPid = Binder.getCallingPid();
-                final int callingUid = Binder.getCallingUid();
                 final long caller = Binder.clearCallingIdentity();
                 try {
-                    mImpl.showSessionLocked(callingPid, callingUid, args, flags,
-                            null /* showCallback */);
+                    mImpl.showSessionLocked(args, flags, null /* showCallback */);
                 } finally {
                     Binder.restoreCallingIdentity(caller);
                 }
@@ -432,12 +429,9 @@
                     Slog.w(TAG, "showSessionFromSession without running voice interaction service");
                     return false;
                 }
-                final int callingPid = Binder.getCallingPid();
-                final int callingUid = Binder.getCallingUid();
                 final long caller = Binder.clearCallingIdentity();
                 try {
-                    return mImpl.showSessionLocked(callingPid, callingUid, sessionArgs, flags,
-                            null /* showCallback */);
+                    return mImpl.showSessionLocked(sessionArgs, flags, null /* showCallback */);
                 } finally {
                     Binder.restoreCallingIdentity(caller);
                 }
@@ -506,11 +500,9 @@
                     Slog.w(TAG, "finish without running voice interaction service");
                     return;
                 }
-                final int callingPid = Binder.getCallingPid();
-                final int callingUid = Binder.getCallingUid();
                 final long caller = Binder.clearCallingIdentity();
                 try {
-                    mImpl.finishLocked(callingPid, callingUid, token);
+                    mImpl.finishLocked(token);
                 } finally {
                     Binder.restoreCallingIdentity(caller);
                 }
@@ -708,11 +700,9 @@
                             + "service");
                     return;
                 }
-                final int callingPid = Binder.getCallingPid();
-                final int callingUid = Binder.getCallingUid();
                 final long caller = Binder.clearCallingIdentity();
                 try {
-                    mImpl.showSessionLocked(callingPid, callingUid, new Bundle() /* sessionArgs */,
+                    mImpl.showSessionLocked(new Bundle() /* sessionArgs */,
                             VoiceInteractionService.START_SOURCE_ASSIST_GESTURE
                                     | VoiceInteractionService.START_WITH_ASSIST
                                     | VoiceInteractionService.START_WITH_SCREENSHOT,
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
index bca757b..61ec162 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionManagerServiceImpl.java
@@ -135,11 +135,11 @@
         mContext.registerReceiver(mBroadcastReceiver, filter, null, handler);
     }
 
-    public boolean showSessionLocked(int callingPid, int callingUid, Bundle args, int flags,
+    public boolean showSessionLocked(Bundle args, int flags,
             IVoiceInteractionSessionShowCallback showCallback) {
         if (mActiveSession == null) {
             mActiveSession = new VoiceInteractionSessionConnection(mLock, mSessionComponentName,
-                    mUser, mContext, this, callingPid, callingUid);
+                    mUser, mContext, this, mInfo.getServiceInfo().applicationInfo.uid);
         }
         return mActiveSession.showLocked(args, flags, showCallback);
     }
@@ -196,7 +196,7 @@
         }
     }
 
-    public void finishLocked(int callingPid, int callingUid, IBinder token) {
+    public void finishLocked(IBinder token) {
         if (mActiveSession == null || token != mActiveSession.mToken) {
             Slog.w(TAG, "finish does not match active session");
             return;
@@ -267,7 +267,7 @@
     @Override
     public void sessionConnectionGone(VoiceInteractionSessionConnection connection) {
         synchronized (mLock) {
-            finishLocked(connection.mCallingPid, connection.mCallingUid, connection.mToken);
+            finishLocked(connection.mToken);
         }
     }
 }
diff --git a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java
index fb83956..9634ab8 100644
--- a/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java
+++ b/services/voiceinteraction/java/com/android/server/voiceinteraction/VoiceInteractionSessionConnection.java
@@ -59,7 +59,6 @@
     final int mUser;
     final Context mContext;
     final Callback mCallback;
-    final int mCallingPid;
     final int mCallingUid;
     final IActivityManager mAm;
     final IWindowManager mIWindowManager;
@@ -139,13 +138,12 @@
     };
 
     public VoiceInteractionSessionConnection(Object lock, ComponentName component, int user,
-            Context context, Callback callback, int callingPid, int callingUid) {
+            Context context, Callback callback, int callingUid) {
         mLock = lock;
         mSessionComponentName = component;
         mUser = user;
         mContext = context;
         mCallback = callback;
-        mCallingPid = callingPid;
         mCallingUid = callingUid;
         mAm = ActivityManagerNative.getDefault();
         mIWindowManager = IWindowManager.Stub.asInterface(
diff --git a/telecomm/java/android/telecom/TelecomManager.java b/telecomm/java/android/telecom/TelecomManager.java
index 28534ea..60b9cb9 100644
--- a/telecomm/java/android/telecom/TelecomManager.java
+++ b/telecomm/java/android/telecom/TelecomManager.java
@@ -376,6 +376,7 @@
      * exists no user-chosen default {@code PhoneAccount}.
      *
      * @return The user outgoing phone account selected by the user.
+     * @hide
      */
     public PhoneAccountHandle getUserSelectedOutgoingPhoneAccount() {
         try {
diff --git a/tests/LockTaskTests/Android.mk b/tests/LockTaskTests/Android.mk
new file mode 100644
index 0000000..ed58643
--- /dev/null
+++ b/tests/LockTaskTests/Android.mk
@@ -0,0 +1,15 @@
+LOCAL_PATH:= $(call my-dir)
+include $(CLEAR_VARS)
+
+LOCAL_MODULE_TAGS := optional
+LOCAL_MODULE_PATH := $(PRODUCT_OUT)/system/priv-app
+
+LOCAL_PACKAGE_NAME := LockTaskTests
+LOCAL_CERTIFICATE := platform
+
+LOCAL_SRC_FILES := $(call all-Iaidl-files-under, src) $(call all-java-files-under, src)
+
+include $(BUILD_PACKAGE)
+
+# Use the following include to make our test apk.
+include $(call all-makefiles-under,$(LOCAL_PATH))
diff --git a/tests/LockTaskTests/AndroidManifest.xml b/tests/LockTaskTests/AndroidManifest.xml
new file mode 100644
index 0000000..f88744e
--- /dev/null
+++ b/tests/LockTaskTests/AndroidManifest.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+    package="com.google.android.example.locktasktests"
+    android:versionCode="1"
+    android:versionName="1.0" >
+
+    <uses-sdk
+        android:minSdkVersion="22"
+        android:targetSdkVersion="22" />
+    <uses-permission android:name="android.permission.INTERNET"/>
+
+    <application
+        android:icon="@drawable/ic_launcher"
+        android:label="@string/app_name"
+        android:theme="@style/AppTheme"
+        android:allowBackup="true" >
+        <activity
+            android:name="com.google.android.example.locktasktests.MainActivity"
+            android:label="@string/app_name"
+            android:screenOrientation="portrait"
+            android:theme="@style/AppTheme" >
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN" />
+                <category android:name="android.intent.category.LAUNCHER" />
+            </intent-filter>
+        </activity>
+        <activity
+            android:name="com.google.android.example.locktasktests.LockDefaultActivity"
+            android:label="@string/title_activity_default"
+            android:taskAffinity=""
+            android:documentLaunchMode="always"
+            android:lockTaskMode="lockTaskModeDefault" >
+        </activity>
+        <activity
+            android:name="com.google.android.example.locktasktests.LockTaskNeverActivity"
+            android:label="@string/title_activity_never"
+            android:taskAffinity=""
+            android:documentLaunchMode="always"
+            android:lockTaskMode="lockTaskModeNever" >
+        </activity>
+        <activity
+            android:name="com.google.android.example.locktasktests.LockWhitelistedActivity"
+            android:label="@string/title_activity_whitelist"
+            android:taskAffinity=""
+            android:documentLaunchMode="always"
+            android:lockTaskMode="lockTaskModeIfWhitelisted" >
+        </activity>
+        <activity
+            android:name="com.google.android.example.locktasktests.LockAtLaunchActivity"
+            android:label="@string/title_activity_always"
+            android:taskAffinity=""
+            android:documentLaunchMode="always"
+            android:lockTaskMode="lockTaskModeAlways" >
+        </activity>
+    </application>
+
+</manifest>
diff --git a/tests/LockTaskTests/res/drawable-hdpi/ic_launcher.png b/tests/LockTaskTests/res/drawable-hdpi/ic_launcher.png
new file mode 100644
index 0000000..288b665
--- /dev/null
+++ b/tests/LockTaskTests/res/drawable-hdpi/ic_launcher.png
Binary files differ
diff --git a/tests/LockTaskTests/res/drawable-mdpi/ic_launcher.png b/tests/LockTaskTests/res/drawable-mdpi/ic_launcher.png
new file mode 100644
index 0000000..6ae570b
--- /dev/null
+++ b/tests/LockTaskTests/res/drawable-mdpi/ic_launcher.png
Binary files differ
diff --git a/tests/LockTaskTests/res/drawable-xhdpi/ic_launcher.png b/tests/LockTaskTests/res/drawable-xhdpi/ic_launcher.png
new file mode 100644
index 0000000..d4fb7cd
--- /dev/null
+++ b/tests/LockTaskTests/res/drawable-xhdpi/ic_launcher.png
Binary files differ
diff --git a/tests/LockTaskTests/res/drawable-xxhdpi/ic_launcher.png b/tests/LockTaskTests/res/drawable-xxhdpi/ic_launcher.png
new file mode 100644
index 0000000..85a6081
--- /dev/null
+++ b/tests/LockTaskTests/res/drawable-xxhdpi/ic_launcher.png
Binary files differ
diff --git a/tests/LockTaskTests/res/layout/activity_launch.xml b/tests/LockTaskTests/res/layout/activity_launch.xml
new file mode 100644
index 0000000..b619743
--- /dev/null
+++ b/tests/LockTaskTests/res/layout/activity_launch.xml
@@ -0,0 +1,32 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/root_launch"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical"
+    android:paddingBottom="@dimen/activity_vertical_margin"
+    android:paddingLeft="@dimen/activity_horizontal_margin"
+    android:paddingRight="@dimen/activity_horizontal_margin"
+    android:paddingTop="@dimen/activity_vertical_margin"
+    tools:context="com.google.android.example.locktasktests.LaunchActivity" >
+
+    <Button
+        android:id="@+id/button_try_lock"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:onClick="onTryLock"
+        android:text="@string/try_lock" />
+    <Button
+        android:id="@+id/button_try_unlock"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:onClick="onTryUnlock"
+        android:text="@string/try_unlock" />
+    <Button
+        android:id="@+id/button_launch_main"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:onClick="onLaunchMain"
+        android:text="@string/launch_main" />
+
+</LinearLayout>
diff --git a/tests/LockTaskTests/res/layout/activity_main.xml b/tests/LockTaskTests/res/layout/activity_main.xml
new file mode 100644
index 0000000..c2e93c4
--- /dev/null
+++ b/tests/LockTaskTests/res/layout/activity_main.xml
@@ -0,0 +1,41 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:id="@+id/root_launch"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:orientation="vertical"
+    android:paddingBottom="@dimen/activity_vertical_margin"
+    android:paddingLeft="@dimen/activity_horizontal_margin"
+    android:paddingRight="@dimen/activity_horizontal_margin"
+    android:paddingTop="@dimen/activity_vertical_margin"
+    tools:context="com.google.android.example.locktasktests.MainActivity" >
+    <Button
+        android:id="@+id/button_default"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="16dp"
+        android:onClick="onButtonPressed"
+        android:text="@string/launch_default" />
+    <Button
+        android:id="@+id/button_never"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="16dp"
+        android:onClick="onButtonPressed"
+        android:text="@string/launch_never" />
+    <Button
+        android:id="@+id/button_whitelist"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="16dp"
+        android:onClick="onButtonPressed"
+        android:text="@string/launch_whitelist" />
+    <Button
+        android:id="@+id/button_always"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="16dp"
+        android:onClick="onButtonPressed"
+        android:text="@string/launch_always" />
+
+</LinearLayout>
diff --git a/tests/LockTaskTests/res/values-v11/styles.xml b/tests/LockTaskTests/res/values-v11/styles.xml
new file mode 100644
index 0000000..3c02242
--- /dev/null
+++ b/tests/LockTaskTests/res/values-v11/styles.xml
@@ -0,0 +1,11 @@
+<resources>
+
+    <!--
+        Base application theme for API 11+. This theme completely replaces
+        AppBaseTheme from res/values/styles.xml on API 11+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light">
+        <!-- API 11 theme customizations can go here. -->
+    </style>
+
+</resources>
diff --git a/tests/LockTaskTests/res/values-v14/styles.xml b/tests/LockTaskTests/res/values-v14/styles.xml
new file mode 100644
index 0000000..a91fd03
--- /dev/null
+++ b/tests/LockTaskTests/res/values-v14/styles.xml
@@ -0,0 +1,12 @@
+<resources>
+
+    <!--
+        Base application theme for API 14+. This theme completely replaces
+        AppBaseTheme from BOTH res/values/styles.xml and
+        res/values-v11/styles.xml on API 14+ devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Holo.Light.DarkActionBar">
+        <!-- API 14 theme customizations can go here. -->
+    </style>
+
+</resources>
diff --git a/tests/LockTaskTests/res/values-w820dp/dimens.xml b/tests/LockTaskTests/res/values-w820dp/dimens.xml
new file mode 100644
index 0000000..f3e7020
--- /dev/null
+++ b/tests/LockTaskTests/res/values-w820dp/dimens.xml
@@ -0,0 +1,10 @@
+<resources>
+
+    <!--
+         Example customization of dimensions originally defined in res/values/dimens.xml
+         (such as screen margins) for screens with more than 820dp of available width. This
+         would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
+    -->
+    <dimen name="activity_horizontal_margin">64dp</dimen>
+
+</resources>
diff --git a/tests/LockTaskTests/res/values/dimens.xml b/tests/LockTaskTests/res/values/dimens.xml
new file mode 100644
index 0000000..55c1e59
--- /dev/null
+++ b/tests/LockTaskTests/res/values/dimens.xml
@@ -0,0 +1,7 @@
+<resources>
+
+    <!-- Default screen margins, per the Android Design guidelines. -->
+    <dimen name="activity_horizontal_margin">16dp</dimen>
+    <dimen name="activity_vertical_margin">16dp</dimen>
+
+</resources>
diff --git a/tests/LockTaskTests/res/values/strings.xml b/tests/LockTaskTests/res/values/strings.xml
new file mode 100644
index 0000000..ae7768e
--- /dev/null
+++ b/tests/LockTaskTests/res/values/strings.xml
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+
+    <string name="app_name">Lock Task Tests</string>
+    <string name="title_activity_default">LockDefaultActivity</string>
+    <string name="title_activity_never">LockTaskNeverActivity</string>
+    <string name="title_activity_whitelist">LockWhitelistedActivity</string>
+    <string name="title_activity_always">LockAtLaunchActivity</string>
+    <string name="launch_default">android:lockTaskMode=\n
+            \"lockTaskModeDefault\"\n
+            Pinnable from Overview.</string>
+    <string name="launch_never">android:lockTaskMode=\n
+            \"lockTaskModeNever\"\n
+            Not Lockable or Pinnable.</string>
+    <string name="launch_whitelist">android:lockTaskMode=\n\"lockTaskModeIfWhitelisted\"\n
+            Lockable if whitelisted, Pinnable.\n
+            Use SampleDeviceOwner app to set whitelist.</string>
+    <string name="launch_always">android:lockTaskMode=\n
+            \"lockTaskModeAlways\"\n
+            Launches into lock mode.</string>
+    <string name="launch_main">launch MainActivity (as activity)"</string>
+    <string name="try_lock">Call startLockMode()</string>
+    <string name="try_unlock">Call stopLockMode()</string>
+
+</resources>
diff --git a/tests/LockTaskTests/res/values/styles.xml b/tests/LockTaskTests/res/values/styles.xml
new file mode 100644
index 0000000..6ce89c7
--- /dev/null
+++ b/tests/LockTaskTests/res/values/styles.xml
@@ -0,0 +1,20 @@
+<resources>
+
+    <!--
+        Base application theme, dependent on API level. This theme is replaced
+        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
+    -->
+    <style name="AppBaseTheme" parent="android:Theme.Light">
+        <!--
+            Theme customizations available in newer API levels can go in
+            res/values-vXX/styles.xml, while customizations related to
+            backward-compatibility can go here.
+        -->
+    </style>
+
+    <!-- Application theme. -->
+    <style name="AppTheme" parent="AppBaseTheme">
+        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
+    </style>
+
+</resources>
diff --git a/tests/LockTaskTests/src/com/google/android/example/locktasktests/LaunchActivity.java b/tests/LockTaskTests/src/com/google/android/example/locktasktests/LaunchActivity.java
new file mode 100644
index 0000000..1fc53cb
--- /dev/null
+++ b/tests/LockTaskTests/src/com/google/android/example/locktasktests/LaunchActivity.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.google.android.example.locktasktests;
+
+import android.app.Activity;
+import android.app.ActivityManager;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.view.View;
+import android.widget.EditText;
+
+public class LaunchActivity extends Activity {
+
+    EditText mEditText;
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_launch);
+        setBackgroundOnLockTaskMode();
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        setBackgroundOnLockTaskMode();
+    }
+
+    public void onTryLock(View view) {
+        startLockTask();
+        setBackgroundOnLockTaskMode();
+    }
+
+    public void onTryUnlock(View view) {
+        stopLockTask();
+        setBackgroundOnLockTaskMode();
+    }
+
+    public void onLaunchMain(View view) {
+        Intent intent = new Intent(this, MainActivity.class);
+        startActivity(intent);
+    }
+
+    private void setBackgroundOnLockTaskMode() {
+        ActivityManager activityManager =
+                (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
+        final int color =
+                activityManager.getLockTaskModeState() != ActivityManager.LOCK_TASK_MODE_NONE ?
+                        0xFFFFC0C0 : 0xFFFFFFFF;
+        findViewById(R.id.root_launch).setBackgroundColor(color);
+    }
+}
diff --git a/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockAtLaunchActivity.java b/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockAtLaunchActivity.java
new file mode 100644
index 0000000..4390c94
--- /dev/null
+++ b/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockAtLaunchActivity.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.google.android.example.locktasktests;
+
+public class LockAtLaunchActivity extends LaunchActivity {
+}
diff --git a/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockDefaultActivity.java b/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockDefaultActivity.java
new file mode 100644
index 0000000..7e57ab7
--- /dev/null
+++ b/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockDefaultActivity.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.google.android.example.locktasktests;
+
+public class LockDefaultActivity extends LaunchActivity {
+}
diff --git a/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockTaskNeverActivity.java b/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockTaskNeverActivity.java
new file mode 100644
index 0000000..69c2cbc
--- /dev/null
+++ b/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockTaskNeverActivity.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.google.android.example.locktasktests;
+
+public class LockTaskNeverActivity extends LaunchActivity {
+}
diff --git a/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockWhitelistedActivity.java b/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockWhitelistedActivity.java
new file mode 100644
index 0000000..387baa2
--- /dev/null
+++ b/tests/LockTaskTests/src/com/google/android/example/locktasktests/LockWhitelistedActivity.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package com.google.android.example.locktasktests;
+
+public class LockWhitelistedActivity extends LaunchActivity {
+}
diff --git a/tests/LockTaskTests/src/com/google/android/example/locktasktests/MainActivity.java b/tests/LockTaskTests/src/com/google/android/example/locktasktests/MainActivity.java
new file mode 100644
index 0000000..82fac03
--- /dev/null
+++ b/tests/LockTaskTests/src/com/google/android/example/locktasktests/MainActivity.java
@@ -0,0 +1,58 @@
+
+package com.google.android.example.locktasktests;
+
+import android.app.Activity;
+import android.app.ActivityManager;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+
+public class MainActivity extends Activity {
+
+    private final static String TAG = "LockTaskTests";
+
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_main);
+        setBackgroundOnLockTaskMode();
+    }
+
+    @Override
+    public void onResume() {
+        super.onResume();
+        setBackgroundOnLockTaskMode();
+    }
+
+    public void onButtonPressed(View v) {
+        Class activity = null;
+        switch (v.getId()) {
+            case R.id.button_default:
+                activity = LockDefaultActivity.class;
+                break;
+            case R.id.button_never:
+                activity = LockTaskNeverActivity.class;
+                break;
+            case R.id.button_whitelist:
+                activity = LockWhitelistedActivity.class;
+                break;
+            case R.id.button_always:
+                activity = LockAtLaunchActivity.class;
+                break;
+        }
+        Intent intent = new Intent(this, activity);
+        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        startActivity(intent);
+    }
+
+    private void setBackgroundOnLockTaskMode() {
+        ActivityManager activityManager =
+                (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
+        final int color =
+                activityManager.getLockTaskModeState() != ActivityManager.LOCK_TASK_MODE_NONE ?
+                        0xFFFFC0C0 : 0xFFFFFFFF;
+        findViewById(R.id.root_launch).setBackgroundColor(color);
+    }
+}