Introduce WindowOrganizer

WM currently only allows the organization of tasks, however we will soon
be allowing the organization of DisplayAreas. To help with the code
structure, we are introducing WindowOrganizer interface which will
contain common APIs for all types of windows organizers (e.g.
applyTransaction) and also be the interfaece for getting the controller
for other organizers.

Test: they pass!
Bug: 147406652
Bug: 152113464
Bug: 152117221

Change-Id: Id2797b288ce92430206c25345d60e7b0e3be98c8
diff --git a/core/java/android/window/WindowOrganizer.java b/core/java/android/window/WindowOrganizer.java
new file mode 100644
index 0000000..b8969c1
--- /dev/null
+++ b/core/java/android/window/WindowOrganizer.java
@@ -0,0 +1,157 @@
+/*
+ * Copyright (C) 2020 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 android.window;
+
+import android.annotation.RequiresPermission;
+import android.app.ActivityManager;
+import android.app.ActivityTaskManager;
+import android.os.RemoteException;
+import android.util.Singleton;
+
+import java.util.List;
+
+/**
+ * Class for organizing specific types of windows like Tasks and DisplayAreas
+ *
+ * @hide
+ */
+public class WindowOrganizer {
+
+    /**
+     * Apply multiple WindowContainer operations at once.
+     * @param t The transaction to apply.
+     * @hide
+     */
+    @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS)
+    public static void applyTransaction(WindowContainerTransaction t) throws RemoteException {
+        getWindowOrganizerController().applyTransaction(t);
+    }
+
+    /**
+     * Apply multiple WindowContainer operations at once.
+     * @param t The transaction to apply.
+     * @param callback This transaction will use the synchronization scheme described in
+     *        BLASTSyncEngine.java. The SurfaceControl transaction containing the effects of this
+     *        WindowContainer transaction will be passed to this callback when ready.
+     * @return An ID for the sync operation which will later be passed to transactionReady callback.
+     *         This lets the caller differentiate overlapping sync operations.
+     * @hide
+     */
+    @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS)
+    public static int applySyncTransaction(WindowContainerTransaction t,
+            IWindowContainerTransactionCallback callback) throws RemoteException {
+        return getWindowOrganizerController().applySyncTransaction(t, callback);
+    }
+
+    /** @hide */
+    @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS)
+    private static IWindowOrganizerController getWindowOrganizerController() {
+        return IWindowOrganizerControllerSingleton.get();
+    }
+
+    private static final Singleton<IWindowOrganizerController> IWindowOrganizerControllerSingleton =
+            new Singleton<IWindowOrganizerController>() {
+                @Override
+                protected IWindowOrganizerController create() {
+                    try {
+                        return ActivityTaskManager.getService().getWindowOrganizerController();
+                    } catch (RemoteException e) {
+                        return null;
+                    }
+                }
+            };
+
+    public static class TaskOrganizer {
+
+        /**
+         * Register a TaskOrganizer to manage tasks as they enter the given windowing mode.
+         * If there was already a TaskOrganizer for this windowing mode it will be evicted
+         * and receive taskVanished callbacks in the process.
+         */
+        @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS)
+        public static void registerOrganizer(ITaskOrganizer organizer, int windowingMode)
+                throws RemoteException {
+            getController().registerTaskOrganizer(organizer, windowingMode);
+        }
+
+        /** Unregisters a previously registered task organizer. */
+        @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS)
+        public static void unregisterOrganizer(ITaskOrganizer organizer) throws RemoteException {
+            getController().unregisterTaskOrganizer(organizer);
+        }
+
+        /** Creates a persistent root task in WM for a particular windowing-mode. */
+        @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS)
+        public static ActivityManager.RunningTaskInfo createRootTask(
+                int displayId, int windowingMode) throws RemoteException {
+            return getController().createRootTask(displayId, windowingMode);
+        }
+
+        /** Deletes a persistent root task in WM */
+        @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS)
+        public static boolean deleteRootTask(IWindowContainer task) throws RemoteException {
+            return getController().deleteRootTask(task);
+        }
+
+        /** Gets direct child tasks (ordered from top-to-bottom) */
+        @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS)
+        public static List<ActivityManager.RunningTaskInfo> getChildTasks(IWindowContainer parent,
+                int[] activityTypes) throws RemoteException {
+            return getController().getChildTasks(parent, activityTypes);
+        }
+
+        /** Gets all root tasks on a display (ordered from top-to-bottom) */
+        @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS)
+        public static List<ActivityManager.RunningTaskInfo> getRootTasks(
+                int displayId, int[] activityTypes) throws RemoteException {
+            return getController().getRootTasks(displayId, activityTypes);
+        }
+
+        /** Get the root task which contains the current ime target */
+        @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS)
+        public static IWindowContainer getImeTarget(int display) throws RemoteException {
+            return getController().getImeTarget(display);
+        }
+
+        /**
+         * Set's the root task to launch new tasks into on a display. {@code null} means no launch
+         * root and thus new tasks just end up directly on the display.
+         */
+        @RequiresPermission(android.Manifest.permission.MANAGE_ACTIVITY_STACKS)
+        public static void setLaunchRoot(int displayId, IWindowContainer root)
+                throws RemoteException {
+            getController().setLaunchRoot(displayId, root);
+        }
+
+        private static ITaskOrganizerController getController() {
+            return ITaskOrganizerControllerSingleton.get();
+        }
+
+        private static final Singleton<ITaskOrganizerController> ITaskOrganizerControllerSingleton =
+                new Singleton<ITaskOrganizerController>() {
+                    @Override
+                    protected ITaskOrganizerController create() {
+                        try {
+                            return getWindowOrganizerController().getTaskOrganizerController();
+                        } catch (RemoteException e) {
+                            return null;
+                        }
+                    }
+                };
+
+    }
+}