[WM] Don't moveTaskToBackInner if it's already detached

The root cause is moveTaskToBackInner may start at a later time,
should ensure that the task is still attached before calling
moveTaskToBackInner.

Similar code was applied at "492dac69a31a67878b8c7bbd97a0227f82b7fc2b".
However, it cannot cover if a child task is removed in a root task
with deferred transition.

Bug: 372834400
Test: Automation Test in SetupWizard (Root - Child Task)
Change-Id: I9341fd5edda879e33c42865df279440591546b54
diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java
index 49086af..7b27084 100644
--- a/services/core/java/com/android/server/wm/Task.java
+++ b/services/core/java/com/android/server/wm/Task.java
@@ -5807,6 +5807,12 @@
     }
 
     private boolean canMoveTaskToBack(Task task) {
+        // Checks whether a task is a child of this task because it can be reparetned when
+        // transition is deferred.
+        if (task != this && task.getParent() != this) {
+            return false;
+        }
+
         // In LockTask mode, moving a locked task to the back of the root task may expose unlocked
         // ones. Therefore we need to check if this operation is allowed.
         if (!mAtmService.getLockTaskController().canMoveTaskToBack(task)) {
@@ -5876,7 +5882,7 @@
                     (deferred) -> {
                         // Need to check again if deferred since the system might
                         // be in a different state.
-                        if (!isAttached() || (deferred && !canMoveTaskToBack(tr))) {
+                        if (!tr.isAttached() || (deferred && !canMoveTaskToBack(tr))) {
                             Slog.e(TAG, "Failed to move task to back after saying we could: "
                                     + tr.mTaskId);
                             transition.abort();