More improvements to the display manager.

Added more complete support for logical displays with
support for mirroring, rotation and scaling.

Improved the overlay display adapter's touch interactions.

A big change here is that the display manager no longer relies
on a single-threaded model to maintain its synchronization
invariants.  Unfortunately we had to change this so as to play
nice with the fact that the window manager wants to own
the surface flinger transaction around display and surface
manipulations.  As a result, the display manager has to be able
to update displays from the context of any thread.

It would be nice to make this process more cooperative.
There are already several components competing to perform
surface flinger transactions including the window manager,
display manager, electron beam, overlay display window,
and mouse pointer.  They are not manipulating the same surfaces
but they can collide with one another when they make global
changes to the displays.

Change-Id: I04f448594241f2004f6f3d1a81ccd12c566bf296
diff --git a/core/java/android/os/Handler.java b/core/java/android/os/Handler.java
index 0f9be9c..94de448 100644
--- a/core/java/android/os/Handler.java
+++ b/core/java/android/os/Handler.java
@@ -431,7 +431,12 @@
      * set up a Handler thread and need to perform some initialization steps on
      * it before continuing execution.
      *
+     * If timeout occurs then this method returns <code>false</code> but the runnable
+     * will remain posted on the handler and may already be in progress or
+     * complete at a later time.
+     *
      * @param r The Runnable that will be executed synchronously.
+     * @param timeout The timeout in milliseconds, or 0 to wait indefinitely.
      *
      * @return Returns true if the Runnable was successfully executed.
      *         Returns false on failure, usually because the
@@ -441,10 +446,13 @@
      * If we ever do make it part of the API, we might want to rename it to something
      * less funny like runUnsafe().
      */
-    public final boolean runWithScissors(final Runnable r) {
+    public final boolean runWithScissors(final Runnable r, long timeout) {
         if (r == null) {
             throw new IllegalArgumentException("runnable must not be null");
         }
+        if (timeout < 0) {
+            throw new IllegalArgumentException("timeout must be non-negative");
+        }
 
         if (Looper.myLooper() == mLooper) {
             r.run();
@@ -452,7 +460,7 @@
         }
 
         BlockingRunnable br = new BlockingRunnable(r);
-        return br.postAndWait(this);
+        return br.postAndWait(this, timeout);
     }
 
     /**
@@ -743,16 +751,30 @@
             }
         }
 
-        public boolean postAndWait(Handler handler) {
+        public boolean postAndWait(Handler handler, long timeout) {
             if (!handler.post(this)) {
                 return false;
             }
 
             synchronized (this) {
-                while (!mDone) {
-                    try {
-                        wait();
-                    } catch (InterruptedException ex) {
+                if (timeout > 0) {
+                    final long expirationTime = SystemClock.uptimeMillis() + timeout;
+                    while (!mDone) {
+                        long delay = expirationTime - SystemClock.uptimeMillis();
+                        if (delay <= 0) {
+                            return false; // timeout
+                        }
+                        try {
+                            wait(delay);
+                        } catch (InterruptedException ex) {
+                        }
+                    }
+                } else {
+                    while (!mDone) {
+                        try {
+                            wait();
+                        } catch (InterruptedException ex) {
+                        }
                     }
                 }
             }