Refactoring of the screen magnification feature.

1. This patch takes care of the case where a magnified window is covering an unmagnigied
   one. One example is a dialog that covers the IME window.

bug:7634430

2. Ensuring that the UI automator tool can connect and correctly dump the screen.

bug:7694696

3. Removed the partial implementation for multi display magnification. It adds
   unnecessary complexity since it cannot be implemented without support for
   input from multiple screens. We will revisit when necessary.

4. Moved the magnified border window as a surface in the window manager.

5. Moved the mediator APIs on the window manager and the policy methods on the
   WindowManagerPolicy.

6. Implemented batch event processing for the accessibility input filter.

Change-Id: I4ebf68b94fb07201e124794f69611ece388ec116
diff --git a/core/java/android/view/IDisplayMagnificationMediator.aidl b/core/java/android/view/IDisplayMagnificationMediator.aidl
deleted file mode 100644
index aa25dac..0000000
--- a/core/java/android/view/IDisplayMagnificationMediator.aidl
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
-** Copyright 2012, 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.view;
-
-import android.view.IDisplayMagnificationController;
-import android.view.MagnificationSpec;
-
-/**
- * {@hide}
- */
-interface IDisplayMagnificationMediator {
-    void addController(int displayId, in IDisplayMagnificationController controller);
-    void removeController(in IDisplayMagnificationController controller);
-    void setMagnificationSpec(in IDisplayMagnificationController controller,
-            in MagnificationSpec spec);
-    MagnificationSpec getCompatibleMagnificationSpec(in IBinder windowToken);
-}
diff --git a/core/java/android/view/IDisplayMagnificationController.aidl b/core/java/android/view/IMagnificationCallbacks.aidl
similarity index 86%
rename from core/java/android/view/IDisplayMagnificationController.aidl
rename to core/java/android/view/IMagnificationCallbacks.aidl
index efe2775..032d073 100644
--- a/core/java/android/view/IDisplayMagnificationController.aidl
+++ b/core/java/android/view/IMagnificationCallbacks.aidl
@@ -16,11 +16,13 @@
 
 package android.view;
 
+import android.graphics.Region;
+
 /**
  * {@hide}
  */
-oneway interface IDisplayMagnificationController {
-    void onMagnifedFrameChanged(int left, int top, int right, int bottom);
+oneway interface IMagnificationCallbacks {
+    void onMagnifedBoundsChanged(in Region bounds);
     void onRectangleOnScreenRequested(int left, int top, int right, int bottom);
     void onRotationChanged(int rotation);
     void onUserContextChanged();
diff --git a/core/java/android/view/IWindowManager.aidl b/core/java/android/view/IWindowManager.aidl
index 17f04e9..1ee2bb3 100644
--- a/core/java/android/view/IWindowManager.aidl
+++ b/core/java/android/view/IWindowManager.aidl
@@ -27,7 +27,7 @@
 import android.os.Bundle;
 import android.os.IRemoteCallback;
 import android.view.IApplicationToken;
-import android.view.IDisplayMagnificationMediator;
+import android.view.IMagnificationCallbacks;
 import android.view.IOnKeyguardExitResult;
 import android.view.IRotationWatcher;
 import android.view.IWindowSession;
@@ -226,11 +226,6 @@
     void setInputFilter(in IInputFilter filter);
 
     /**
-     * Gets the display magnification mediator.
-     */
-    IDisplayMagnificationMediator getDisplayMagnificationMediator();
-
-    /**
      * Gets the frame of a window given its token.
      */
     void getWindowFrame(IBinder token, out Rect outFrame);
@@ -245,4 +240,30 @@
      * credentials.
      */
     void showAssistant();
+
+    /**
+     * Sets the display magnification callbacks. These callbacks notify
+     * the client for contextual changes related to display magnification.
+     *
+     * @param callbacks The magnification callbacks.
+     */
+    void setMagnificationCallbacks(IMagnificationCallbacks callbacks);
+
+    /**
+     * Sets the magnification spec to be applied to all windows that can be
+     * magnified.
+     *
+     * @param spec The current magnification spec.
+     */
+    void setMagnificationSpec(in MagnificationSpec spec);
+
+    /**
+     * Gets the magnification spec for a window given its token. If the
+     * window has a compatibility scale it is also folded in the returned
+     * magnification spec.
+     *
+     * @param windowToken The unique window token.
+     * @return The magnification spec if such or null.
+     */
+    MagnificationSpec getCompatibleMagnificationSpecForWindow(in IBinder windowToken);
 }
diff --git a/core/java/android/view/MagnificationSpec.java b/core/java/android/view/MagnificationSpec.java
index 7fb5615..0ee6714 100644
--- a/core/java/android/view/MagnificationSpec.java
+++ b/core/java/android/view/MagnificationSpec.java
@@ -39,6 +39,9 @@
     }
 
     public void initialize(float scale, float offsetX, float offsetY) {
+        if (scale < 1) {
+            throw new IllegalArgumentException("Scale must be greater than or equal to one!");
+        }
         this.scale = scale;
         this.offsetX = offsetX;
         this.offsetY = offsetY;
diff --git a/core/java/android/view/Surface.java b/core/java/android/view/Surface.java
index f2c5eac..a972b75 100644
--- a/core/java/android/view/Surface.java
+++ b/core/java/android/view/Surface.java
@@ -812,6 +812,8 @@
      *
      * @param rotation The rotation.
      * @return The rotation symbolic name.
+     *
+     * @hide
      */
     public static String rotationToString(int rotation) {
         switch (rotation) {
diff --git a/core/java/android/view/WindowManagerPolicy.java b/core/java/android/view/WindowManagerPolicy.java
index 7bdb44c..47ef638 100644
--- a/core/java/android/view/WindowManagerPolicy.java
+++ b/core/java/android/view/WindowManagerPolicy.java
@@ -1134,4 +1134,23 @@
      * {@link android.content.Intent#ACTION_ASSIST}
      */
     public void showAssistant();
+
+    /**
+     * Returns whether a given window type can be magnified.
+     *
+     * @param windowType The window type.
+     * @return True if the window can be magnified.
+     */
+    public boolean canMagnifyWindow(int windowType);
+
+    /**
+     * Returns whether a given window type is considered a top level one.
+     * A top level window does not have a container, i.e. attached window,
+     * or if it has a container it is laid out as a top-level window, not
+     * as a child of its container.
+     *
+     * @param windowType The window type.
+     * @return True if the window is a top level one.
+     */
+    public boolean isTopLevelWindow(int windowType);
 }