Give SystemUI a chance to participate in display rotation

This adds support for registering a single DisplayRotationController
to WMS. It gives a chance for the controller to suggest some
task changes to be executed along with a display rotation. There
is only one because it's a 2-way communication and there is only
intended to be one client for now.

This allows us to move Split and PiP presentation/layout logic out
of WM into systemui because WM no-longer needs to be the one
calculating the new bounds of everything during rotation.

This uses the windowcontainer transaction because all the
configuration changes and the display rotation need to happen
synchronously; otherwise, relayouts can occur after the display
is rotated, but before the configuration changes are applied.
When this happens, apps get incorrect bounds/insets which can
trigger erroneous lifecycle events in the app.

The flow is like this:
1. DisplayContent/Rotation freezes screen
2. DisplayRotationController is notified of a rotation and provided a
   callback.
3. The Controller then evaluates/queues some task changes in
   a transaction and, when done, fires the provided callback.
4. The callback applies the config change transaction and continues
   the rest of the rotation synchronously.

The DisplayWindowController is sys-ui piece that serves as an
interface between system-ui components and display-related wm
logic. For now it just facilitates the rotation calculation, but
in the future it will have more general utility for display logic
like inset/bounds calculation.

Bug: 124011688
Bug: 133381284
Test: Added some wmtests and coretests.
Change-Id: If10695f44fa076725ba17746842f6fbd64da5d20
diff --git a/services/core/java/com/android/server/wm/DisplayWindowListenerController.java b/services/core/java/com/android/server/wm/DisplayWindowListenerController.java
new file mode 100644
index 0000000..dbc452f
--- /dev/null
+++ b/services/core/java/com/android/server/wm/DisplayWindowListenerController.java
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.server.wm;
+
+import android.os.RemoteCallbackList;
+import android.os.RemoteException;
+import android.view.IDisplayWindowListener;
+
+/**
+ * Manages dispatch of relevant hierarchy changes to interested listeners. Listeners are assumed
+ * to be remote.
+ */
+class DisplayWindowListenerController {
+    RemoteCallbackList<IDisplayWindowListener> mDisplayListeners = new RemoteCallbackList<>();
+
+//    private final ArrayList<DisplayContainerListener> mDisplayListeners = new ArrayList<>();
+    private final WindowManagerService mService;
+
+    DisplayWindowListenerController(WindowManagerService service) {
+        mService = service;
+    }
+
+    void registerListener(IDisplayWindowListener listener) {
+        synchronized (mService.mGlobalLock) {
+            mDisplayListeners.register(listener);
+            try {
+                for (int i = 0; i < mService.mAtmService.mRootActivityContainer.getChildCount();
+                        ++i) {
+                    ActivityDisplay d = mService.mAtmService.mRootActivityContainer.getChildAt(i);
+                    listener.onDisplayAdded(d.mDisplayId);
+                }
+            } catch (RemoteException e) { }
+        }
+    }
+
+    void unregisterListener(IDisplayWindowListener listener) {
+        mDisplayListeners.unregister(listener);
+    }
+
+    void dispatchDisplayAdded(ActivityDisplay display) {
+        int count = mDisplayListeners.beginBroadcast();
+        for (int i = 0; i < count; ++i) {
+            try {
+                mDisplayListeners.getBroadcastItem(i).onDisplayAdded(display.mDisplayId);
+            } catch (RemoteException e) {
+            }
+        }
+        mDisplayListeners.finishBroadcast();
+    }
+
+    void dispatchDisplayRemoved(ActivityDisplay display) {
+        int count = mDisplayListeners.beginBroadcast();
+        for (int i = 0; i < count; ++i) {
+            try {
+                mDisplayListeners.getBroadcastItem(i).onDisplayRemoved(display.mDisplayId);
+            } catch (RemoteException e) {
+            }
+        }
+        mDisplayListeners.finishBroadcast();
+    }
+}