Merge "Wait for the user to fully stop before starting" into pi-dev
diff --git a/services/core/java/com/android/server/am/UserController.java b/services/core/java/com/android/server/am/UserController.java
index 8ad8256..18c0957 100644
--- a/services/core/java/com/android/server/am/UserController.java
+++ b/services/core/java/com/android/server/am/UserController.java
@@ -64,6 +64,7 @@
 import android.os.IProgressListener;
 import android.os.IRemoteCallback;
 import android.os.IUserManager;
+import android.os.Looper;
 import android.os.Message;
 import android.os.Process;
 import android.os.RemoteCallbackList;
@@ -962,6 +963,11 @@
                     updateStartedUserArrayLU();
                     needStart = true;
                     updateUmState = true;
+                } else if (uss.state == UserState.STATE_SHUTDOWN && !isCallingOnHandlerThread()) {
+                    Slog.i(TAG, "User #" + userId
+                            + " is shutting down - will start after full stop");
+                    mHandler.post(() -> startUser(userId, foreground, unlockListener));
+                    return true;
                 }
                 final Integer userIdInt = userId;
                 mUserLru.remove(userIdInt);
@@ -1086,6 +1092,10 @@
         return true;
     }
 
+    private boolean isCallingOnHandlerThread() {
+        return Looper.myLooper() == mHandler.getLooper();
+    }
+
     /**
      * Start user, if its not already running, and bring it to foreground.
      */