Merge "Audio focus: fix mapping of old requestAudioFocus to AudioFocusRequest" into oc-dev
diff --git a/core/java/android/accessibilityservice/AccessibilityButtonController.java b/core/java/android/accessibilityservice/AccessibilityButtonController.java
index ee19768..a70085c 100644
--- a/core/java/android/accessibilityservice/AccessibilityButtonController.java
+++ b/core/java/android/accessibilityservice/AccessibilityButtonController.java
@@ -19,6 +19,7 @@
 import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.os.Handler;
+import android.os.Looper;
 import android.os.RemoteException;
 import android.util.ArrayMap;
 import android.util.Slog;
@@ -91,7 +92,7 @@
      * @param callback the callback to add, must be non-null
      */
     public void registerAccessibilityButtonCallback(@NonNull AccessibilityButtonCallback callback) {
-        registerAccessibilityButtonCallback(callback, new Handler());
+        registerAccessibilityButtonCallback(callback, new Handler(Looper.getMainLooper()));
     }
 
     /**
diff --git a/services/core/java/com/android/server/SystemService.java b/services/core/java/com/android/server/SystemService.java
index c105b12..94397d0 100644
--- a/services/core/java/com/android/server/SystemService.java
+++ b/services/core/java/com/android/server/SystemService.java
@@ -173,6 +173,9 @@
      * state they maintain for running users.  This is called prior to sending the SHUTDOWN
      * broadcast to the user; it is a good place to stop making use of any resources of that
      * user (such as binding to a service running in the user).
+     *
+     * <p>NOTE: This is the last callback where the callee may access the target user's CE storage.
+     *
      * @param userHandle The identifier of the user.
      */
     public void onStopUser(int userHandle) {}
@@ -181,6 +184,10 @@
      * Called when an existing user is stopping, for system services to finalize any per-user
      * state they maintain for running users.  This is called after all application process
      * teardown of the user is complete.
+     *
+     * <p>NOTE: When this callback is called, the CE storage for the target user may not be
+     * accessible already.  Use {@link #onStopUser} instead if you need to access the CE storage.
+     *
      * @param userHandle The identifier of the user.
      */
     public void onCleanupUser(int userHandle) {}
diff --git a/services/core/java/com/android/server/am/ActiveServices.java b/services/core/java/com/android/server/am/ActiveServices.java
index 5edf19a..e0fc531 100644
--- a/services/core/java/com/android/server/am/ActiveServices.java
+++ b/services/core/java/com/android/server/am/ActiveServices.java
@@ -2808,7 +2808,7 @@
     void removeUninstalledPackageLocked(String packageName, int userId) {
         ServiceMap smap = mServiceMap.get(userId);
         if (smap != null && smap.mActiveForegroundApps.size() > 0) {
-            for (int i = smap.mActiveForegroundApps.size(); i >= 0; i--) {
+            for (int i = smap.mActiveForegroundApps.size()-1; i >= 0; i--) {
                 ActiveForegroundApp aa = smap.mActiveForegroundApps.valueAt(i);
                 if (aa.mPackageName.equals(packageName)) {
                     smap.mActiveForegroundApps.removeAt(i);
diff --git a/services/core/java/com/android/server/pm/ShortcutService.java b/services/core/java/com/android/server/pm/ShortcutService.java
index 7c89e1c..bed8f1a 100644
--- a/services/core/java/com/android/server/pm/ShortcutService.java
+++ b/services/core/java/com/android/server/pm/ShortcutService.java
@@ -555,8 +555,8 @@
         }
 
         @Override
-        public void onCleanupUser(int userHandle) {
-            mService.handleCleanupUser(userHandle);
+        public void onStopUser(int userHandle) {
+            mService.handleStopUser(userHandle);
         }
 
         @Override
@@ -606,9 +606,9 @@
     }
 
     /** lifecycle event */
-    void handleCleanupUser(int userId) {
+    void handleStopUser(int userId) {
         if (DEBUG) {
-            Slog.d(TAG, "handleCleanupUser: user=" + userId);
+            Slog.d(TAG, "handleStopUser: user=" + userId);
         }
         synchronized (mLock) {
             unloadUserLocked(userId);
@@ -3777,7 +3777,7 @@
 
                 Slog.i(TAG, "cmd: handleUnloadUser: user=" + mUserId);
 
-                ShortcutService.this.handleCleanupUser(mUserId);
+                ShortcutService.this.handleStopUser(mUserId);
             }
         }
 
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java
index cd39d88..4c7bf4d 100644
--- a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest1.java
@@ -3241,7 +3241,7 @@
         assertNull(mService.getShortcutsForTest().get(USER_10).getLastKnownLauncher());
 
         // Try stopping the user
-        mService.handleCleanupUser(USER_10);
+        mService.handleStopUser(USER_10);
 
         // Now it's unloaded.
         assertEquals(1, mService.getShortcutsForTest().size());
@@ -6106,7 +6106,7 @@
             assertEmpty(mManager.getPinnedShortcuts());
         });
         // Send add broadcast, but the user is not running, so should be ignored.
-        mService.handleCleanupUser(USER_10);
+        mService.handleStopUser(USER_10);
         mRunningUsers.put(USER_10, false);
         mUnlockedUsers.put(USER_10, false);
 
diff --git a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
index 9880caa..2b40c51 100644
--- a/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
+++ b/services/tests/servicestests/src/com/android/server/pm/ShortcutManagerTest2.java
@@ -2091,7 +2091,7 @@
         assertFalse(mService.isUserUnlockedL(USER_10));
 
         // Directly call the stop lifecycle event.  Goes back to the initial state.
-        mService.handleCleanupUser(USER_0);
+        mService.handleStopUser(USER_0);
         assertFalse(mService.isUserUnlockedL(USER_0));
         assertFalse(mService.isUserUnlockedL(USER_10));
     }