resolve merge conflicts of d2310e6ce337acd5f31a9342151458422826a5d8 to qt-r1-bubbles-dev

Bug: 128932906

Test: build
Change-Id: Idabad8379cded664bcf302d5c6fb69564d158eb7
diff --git a/core/java/android/app/ITaskStackListener.aidl b/core/java/android/app/ITaskStackListener.aidl
index 8c43ae5..6fd43a6 100644
--- a/core/java/android/app/ITaskStackListener.aidl
+++ b/core/java/android/app/ITaskStackListener.aidl
@@ -184,4 +184,12 @@
      * @param displayId the id of the display from which the window is removed.
      */
     void onSingleTaskDisplayEmpty(int displayId);
+
+    /**
+     * Called when a task is reparented to a stack on a different display.
+     *
+     * @param taskId id of the task which was moved to a different display.
+     * @param newDisplayId id of the new display.
+     */
+    void onTaskDisplayChanged(int taskId, int newDisplayId);
 }
diff --git a/core/java/android/app/TaskStackListener.java b/core/java/android/app/TaskStackListener.java
index fcf17bf..b4b7df9 100644
--- a/core/java/android/app/TaskStackListener.java
+++ b/core/java/android/app/TaskStackListener.java
@@ -26,6 +26,7 @@
 /**
  * Classes interested in observing only a subset of changes using ITaskStackListener can extend
  * this class to avoid having to implement all the methods.
+ *
  * @hide
  */
 public abstract class TaskStackListener extends ITaskStackListener.Stub {
@@ -181,4 +182,8 @@
     @Override
     public void onSingleTaskDisplayEmpty(int displayId) throws RemoteException {
     }
+
+    @Override
+    public void onTaskDisplayChanged(int taskId, int newDisplayId) throws RemoteException {
+    }
 }
diff --git a/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java b/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
index a07bb8f..2874ce6 100644
--- a/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
+++ b/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
@@ -346,7 +346,7 @@
 
         CarSystemUIFactory factory = SystemUIFactory.getInstance();
         mCarFacetButtonController = factory.getCarDependencyComponent()
-            .getCarFacetButtonController();
+                .getCarFacetButtonController();
         mNotificationPanelBackground = getDefaultWallpaper();
         mScrimController.setScrimBehindDrawable(mNotificationPanelBackground);
 
@@ -925,6 +925,16 @@
                 Log.e(TAG, "Getting StackInfo from activity manager failed", e);
             }
         }
+
+        @Override
+        public void onTaskDisplayChanged(int taskId, int newDisplayId) {
+            try {
+                mCarFacetButtonController.taskChanged(
+                        ActivityTaskManager.getService().getAllStackInfos());
+            } catch (Exception e) {
+                Log.e(TAG, "Getting StackInfo from activity manager failed", e);
+            }
+        }
     }
 
     private void onDrivingStateChanged(CarDrivingStateEvent notUsed) {
diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/system/TaskStackChangeListener.java b/packages/SystemUI/shared/src/com/android/systemui/shared/system/TaskStackChangeListener.java
index 3ec5642..4c49494 100644
--- a/packages/SystemUI/shared/src/com/android/systemui/shared/system/TaskStackChangeListener.java
+++ b/packages/SystemUI/shared/src/com/android/systemui/shared/system/TaskStackChangeListener.java
@@ -94,6 +94,14 @@
     public void onBackPressedOnTaskRoot(RunningTaskInfo taskInfo) { }
 
     /**
+     * Called when a task is reparented to a stack on a different display.
+     *
+     * @param taskId id of the task which was moved to a different display.
+     * @param newDisplayId id of the new display.
+     */
+    public void onTaskDisplayChanged(int taskId, int newDisplayId) { }
+
+    /**
      * Checks that the current user matches the process. Since
      * {@link android.app.ITaskStackListener} is not multi-user aware, handlers of
      * {@link TaskStackChangeListener} should make this call to verify that we don't act on events
diff --git a/packages/SystemUI/shared/src/com/android/systemui/shared/system/TaskStackChangeListeners.java b/packages/SystemUI/shared/src/com/android/systemui/shared/system/TaskStackChangeListeners.java
index d514a75..4465f04 100644
--- a/packages/SystemUI/shared/src/com/android/systemui/shared/system/TaskStackChangeListeners.java
+++ b/packages/SystemUI/shared/src/com/android/systemui/shared/system/TaskStackChangeListeners.java
@@ -112,7 +112,7 @@
 
     @Override
     public void onPinnedActivityRestartAttempt(boolean clearedTask)
-            throws RemoteException{
+            throws RemoteException {
         mHandler.removeMessages(H.ON_PINNED_ACTIVITY_RESTART_ATTEMPT);
         mHandler.obtainMessage(H.ON_PINNED_ACTIVITY_RESTART_ATTEMPT, clearedTask ? 1 : 0, 0)
                 .sendToTarget();
@@ -154,7 +154,7 @@
     public void onActivityLaunchOnSecondaryDisplayRerouted(RunningTaskInfo taskInfo,
             int requestedDisplayId) throws RemoteException {
         mHandler.obtainMessage(H.ON_ACTIVITY_LAUNCH_ON_SECONDARY_DISPLAY_REROUTED,
-                 requestedDisplayId, 0 /* unused */, taskInfo).sendToTarget();
+                requestedDisplayId, 0 /* unused */, taskInfo).sendToTarget();
     }
 
     @Override
@@ -214,6 +214,11 @@
                 0 /* unused */).sendToTarget();
     }
 
+    @Override
+    public void onTaskDisplayChanged(int taskId, int newDisplayId) throws RemoteException {
+        mHandler.obtainMessage(H.ON_TASK_DISPLAY_CHANGED, taskId, newDisplayId).sendToTarget();
+    }
+
     private final class H extends Handler {
         private static final int ON_TASK_STACK_CHANGED = 1;
         private static final int ON_TASK_SNAPSHOT_CHANGED = 2;
@@ -235,6 +240,7 @@
         private static final int ON_BACK_PRESSED_ON_TASK_ROOT = 18;
         private static final int ON_SINGLE_TASK_DISPLAY_DRAWN = 19;
         private static final int ON_SINGLE_TASK_DISPLAY_EMPTY = 20;
+        private static final int ON_TASK_DISPLAY_CHANGED = 21;
 
 
         public H(Looper looper) {
@@ -320,7 +326,7 @@
                         final RunningTaskInfo info = (RunningTaskInfo) msg.obj;
                         for (int i = mTaskStackListeners.size() - 1; i >= 0; i--) {
                             mTaskStackListeners.get(i)
-                                .onActivityLaunchOnSecondaryDisplayRerouted(info);
+                                    .onActivityLaunchOnSecondaryDisplayRerouted(info);
                         }
                         break;
                     }
@@ -384,6 +390,12 @@
                         }
                         break;
                     }
+                    case ON_TASK_DISPLAY_CHANGED: {
+                        for (int i = mTaskStackListeners.size() - 1; i >= 0; i--) {
+                            mTaskStackListeners.get(i).onTaskDisplayChanged(msg.arg1, msg.arg2);
+                        }
+                        break;
+                    }
                 }
             }
         }
diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java
index 3820106..fd86faa 100644
--- a/services/core/java/com/android/server/wm/Task.java
+++ b/services/core/java/com/android/server/wm/Task.java
@@ -348,6 +348,9 @@
     void onDisplayChanged(DisplayContent dc) {
         adjustBoundsForDisplayChangeIfNeeded(dc);
         super.onDisplayChanged(dc);
+        final int displayId = (dc != null) ? dc.getDisplayId() : Display.INVALID_DISPLAY;
+        mWmService.mAtmService.getTaskChangeNotificationController().notifyTaskDisplayChanged(
+                mTaskId, displayId);
     }
 
     /**
diff --git a/services/core/java/com/android/server/wm/TaskChangeNotificationController.java b/services/core/java/com/android/server/wm/TaskChangeNotificationController.java
index 80470c6..4c50527 100644
--- a/services/core/java/com/android/server/wm/TaskChangeNotificationController.java
+++ b/services/core/java/com/android/server/wm/TaskChangeNotificationController.java
@@ -56,6 +56,7 @@
     private static final int NOTIFY_BACK_PRESSED_ON_TASK_ROOT = 21;
     private static final int NOTIFY_SINGLE_TASK_DISPLAY_DRAWN = 22;
     private static final int NOTIFY_SINGLE_TASK_DISPLAY_EMPTY = 23;
+    private static final int NOTIFY_TASK_DISPLAY_CHANGED_LISTENERS_MSG = 24;
 
     // Delay in notifying task stack change listeners (in millis)
     private static final int NOTIFY_TASK_STACK_CHANGE_LISTENERS_DELAY = 100;
@@ -164,6 +165,10 @@
         l.onSingleTaskDisplayEmpty(m.arg1);
     };
 
+    private final TaskStackConsumer mNotifyTaskDisplayChanged = (l, m) -> {
+        l.onTaskDisplayChanged(m.arg1, m.arg2);
+    };
+
     @FunctionalInterface
     public interface TaskStackConsumer {
         void accept(ITaskStackListener t, Message m) throws RemoteException;
@@ -249,6 +254,9 @@
                 case NOTIFY_SINGLE_TASK_DISPLAY_EMPTY:
                     forAllRemoteListeners(mNotifySingleTaskDisplayEmpty, msg);
                     break;
+                case NOTIFY_TASK_DISPLAY_CHANGED_LISTENERS_MSG:
+                    forAllRemoteListeners(mNotifyTaskDisplayChanged, msg);
+                    break;
             }
         }
     }
@@ -514,4 +522,14 @@
         forAllLocalListeners(mNotifySingleTaskDisplayEmpty, msg);
         msg.sendToTarget();
     }
+
+    /**
+     * Notify listeners that a task is reparented to another display.
+     */
+    void notifyTaskDisplayChanged(int taskId, int newDisplayId) {
+        final Message msg = mHandler.obtainMessage(NOTIFY_TASK_DISPLAY_CHANGED_LISTENERS_MSG,
+                taskId, newDisplayId);
+        forAllLocalListeners(mNotifyTaskStackChanged, msg);
+        msg.sendToTarget();
+    }
 }
diff --git a/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java b/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java
index 366acea..d0ee634 100644
--- a/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java
+++ b/services/tests/wmtests/src/com/android/server/wm/SystemServicesTestRule.java
@@ -162,6 +162,9 @@
         }
 
         final ActivityTaskManagerService atms = mock(ActivityTaskManagerService.class);
+        final TaskChangeNotificationController taskChangeNotificationController = mock(
+                TaskChangeNotificationController.class);
+        doReturn(taskChangeNotificationController).when(atms).getTaskChangeNotificationController();
         final WindowManagerGlobalLock wmLock = new WindowManagerGlobalLock();
         doReturn(wmLock).when(atms).getGlobalLock();