Fix change of behavior in Looper.quit().

It seems some applications rely on Looper.quit() terminating the
loop immediately without processing all messages.  Rather than
risk breaking them, make the safer behavior optional.

Also take care to properly drain the message queue before quitting
so that all of the Message instances are recycled.  This may
help release storage sooner in case the Looper doesn't get GC'd
promptly and its remaining queue of undelivered messages sticks
around.

Improve docs on runWithScissors.

Bug: 8596303
Change-Id: I8cbeb6f7a5f6b8e618b5109f87a03defc1486b9f
diff --git a/core/java/android/os/Handler.java b/core/java/android/os/Handler.java
index 94de448..14d8f07 100644
--- a/core/java/android/os/Handler.java
+++ b/core/java/android/os/Handler.java
@@ -413,27 +413,32 @@
 
     /**
      * Runs the specified task synchronously.
-     *
+     * <p>
      * If the current thread is the same as the handler thread, then the runnable
      * runs immediately without being enqueued.  Otherwise, posts the runnable
      * to the handler and waits for it to complete before returning.
-     *
+     * </p><p>
      * This method is dangerous!  Improper use can result in deadlocks.
      * Never call this method while any locks are held or use it in a
      * possibly re-entrant manner.
-     *
+     * </p><p>
      * This method is occasionally useful in situations where a background thread
      * must synchronously await completion of a task that must run on the
      * handler's thread.  However, this problem is often a symptom of bad design.
      * Consider improving the design (if possible) before resorting to this method.
-     *
+     * </p><p>
      * One example of where you might want to use this method is when you just
      * set up a Handler thread and need to perform some initialization steps on
      * it before continuing execution.
-     *
+     * </p><p>
      * 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.
+     * </p><p>
+     * When using this method, be sure to use {@link Looper#quitSafely} when
+     * quitting the looper.  Otherwise {@link #runWithScissors} may hang indefinitely.
+     * (TODO: We should fix this by making MessageQueue aware of blocking runnables.)
+     * </p>
      *
      * @param r The Runnable that will be executed synchronously.
      * @param timeout The timeout in milliseconds, or 0 to wait indefinitely.