Clean up the lock task APIs for COSU devices.

Clean up and increase readability of internal handling of lock task mode APIs.
Add a public API to query the lock task mode state with pinned and locked as
possible outcomes. Additionally, change wording in lock task toasts when in
locked mode and update the javadoc regarding onLockTaskModeEntering and
onLockTaskModeExiting to represent the actual behaviour.

Bug: 19377096
Change-Id: Ia563078ca6ef6d6fc7e75130e6b94ba18af69340
diff --git a/services/core/java/com/android/server/am/ActivityStackSupervisor.java b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
index a260330..b3d3d6a 100644
--- a/services/core/java/com/android/server/am/ActivityStackSupervisor.java
+++ b/services/core/java/com/android/server/am/ActivityStackSupervisor.java
@@ -267,9 +267,11 @@
     /** 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;
-    /** Whether lock task has been entered by an authorized app and cannot
-     * be exited. */
-    private boolean mLockTaskIsLocked;
+    /** Store the current lock task mode. Possible values:
+     * {@link ActivityManager#LOCK_TASK_MODE_NONE}, {@link ActicityManager#LOCK_TASK_MODE_LOCKED},
+     * {@link ActicityManager#LOCK_TASK_MODE_PINNED}
+     */
+    private int mLockTaskModeState;
     /**
      * Notifies the user when entering/exiting lock-task.
      */
@@ -3546,10 +3548,10 @@
     }
 
     void showLockTaskToast() {
-        mLockTaskNotify.showToast(mLockTaskIsLocked);
+        mLockTaskNotify.showToast(mLockTaskModeState);
     }
 
-    void setLockTaskModeLocked(TaskRecord task, boolean isLocked, String reason) {
+    void setLockTaskModeLocked(TaskRecord task, int lockTaskModeState, String reason) {
         if (task == null) {
             // Take out of lock task mode if necessary
             if (mLockTaskModeTask != null) {
@@ -3573,7 +3575,7 @@
         lockTaskMsg.obj = mLockTaskModeTask.intent.getComponent().getPackageName();
         lockTaskMsg.arg1 = mLockTaskModeTask.userId;
         lockTaskMsg.what = LOCK_TASK_START_MSG;
-        lockTaskMsg.arg2 = !isLocked ? 1 : 0;
+        lockTaskMsg.arg2 = lockTaskModeState;
         mHandler.sendMessage(lockTaskMsg);
     }
 
@@ -3591,8 +3593,8 @@
         }
     }
 
-    boolean isInLockTaskMode() {
-        return mLockTaskModeTask != null;
+    int getLockTaskModeState() {
+        return mLockTaskModeState;
     }
 
     private final class ActivityStackSupervisorHandler extends Handler {
@@ -3684,13 +3686,18 @@
                             mLockTaskNotify = new LockTaskNotify(mService.mContext);
                         }
                         mLockTaskNotify.show(true);
-                        mLockTaskIsLocked = msg.arg2 == 0;
+                        mLockTaskModeState = msg.arg2;
                         if (getStatusBarService() != null) {
-                            int flags =
-                                    StatusBarManager.DISABLE_MASK ^ StatusBarManager.DISABLE_BACK;
-                            if (!mLockTaskIsLocked) {
-                                flags ^= StatusBarManager.DISABLE_HOME
-                                        | StatusBarManager.DISABLE_RECENT;
+                            int flags = 0;
+                            if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_LOCKED) {
+                                flags = StatusBarManager.DISABLE_MASK
+                                        & (~StatusBarManager.DISABLE_BACK);
+                            } else if (mLockTaskModeState ==
+                                    ActivityManager.LOCK_TASK_MODE_PINNED) {
+                                flags = StatusBarManager.DISABLE_MASK
+                                        & (~StatusBarManager.DISABLE_BACK)
+                                        & (~StatusBarManager.DISABLE_HOME)
+                                        & (~StatusBarManager.DISABLE_RECENT);
                             }
                             getStatusBarService().disable(flags, mToken,
                                     mService.mContext.getPackageName());
@@ -3724,7 +3731,8 @@
                             boolean shouldLockKeyguard = Settings.Secure.getInt(
                                     mService.mContext.getContentResolver(),
                                     Settings.Secure.LOCK_TO_APP_EXIT_LOCKED) != 0;
-                            if (!mLockTaskIsLocked && shouldLockKeyguard) {
+                            if (mLockTaskModeState == ActivityManager.LOCK_TASK_MODE_PINNED &&
+                                    shouldLockKeyguard) {
                                 mWindowManager.lockNow(null);
                                 mWindowManager.dismissKeyguard();
                                 new LockPatternUtils(mService.mContext)
@@ -3735,6 +3743,8 @@
                         }
                     } catch (RemoteException ex) {
                         throw new RuntimeException(ex);
+                    } finally {
+                        mLockTaskModeState = ActivityManager.LOCK_TASK_MODE_NONE;
                     }
                 } break;
                 case CONTAINER_CALLBACK_TASK_LIST_EMPTY: {