Mark input and sensor messages as asynchronous.

Set a barrier on traversals.

Vsync is still not enabled by default in this patch so there
should be no observable effect from these changes.

Change-Id: Ie12081b95a8f1e81ed686edf747cc62f2e044b7e
diff --git a/core/java/android/hardware/SensorManager.java b/core/java/android/hardware/SensorManager.java
index aeb5d92..3fdf246 100644
--- a/core/java/android/hardware/SensorManager.java
+++ b/core/java/android/hardware/SensorManager.java
@@ -616,6 +616,7 @@
             Message msg = Message.obtain();
             msg.what = 0;
             msg.obj = t;
+            msg.setAsynchronous(true);
             mHandler.sendMessage(msg);
         }
     }
diff --git a/core/java/android/view/Choreographer.java b/core/java/android/view/Choreographer.java
index a74b737..42c3913 100644
--- a/core/java/android/view/Choreographer.java
+++ b/core/java/android/view/Choreographer.java
@@ -235,8 +235,9 @@
                 if (isRunningOnLooperThreadLocked()) {
                     doScheduleVsyncLocked();
                 } else {
-                    mHandler.sendMessageAtFrontOfQueue(
-                            mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC));
+                    Message msg = mHandler.obtainMessage(MSG_DO_SCHEDULE_VSYNC);
+                    msg.setAsynchronous(true);
+                    mHandler.sendMessageAtFrontOfQueue(msg);
                 }
             } else {
                 final long now = SystemClock.uptimeMillis();
@@ -244,7 +245,9 @@
                 if (DEBUG) {
                     Log.d(TAG, "Scheduling animation in " + (nextAnimationTime - now) + " ms.");
                 }
-                mHandler.sendEmptyMessageAtTime(MSG_DO_ANIMATION, nextAnimationTime);
+                Message msg = mHandler.obtainMessage(MSG_DO_ANIMATION);
+                msg.setAsynchronous(true);
+                mHandler.sendMessageAtTime(msg, nextAnimationTime);
             }
         }
     }
@@ -258,7 +261,9 @@
                 if (DEBUG) {
                     Log.d(TAG, "Scheduling draw immediately.");
                 }
-                mHandler.sendEmptyMessage(MSG_DO_DRAW);
+                Message msg = mHandler.obtainMessage(MSG_DO_DRAW);
+                msg.setAsynchronous(true);
+                mHandler.sendMessage(msg);
             }
         }
     }
diff --git a/core/java/android/view/ViewRootImpl.java b/core/java/android/view/ViewRootImpl.java
index 41fa572..d41d168 100644
--- a/core/java/android/view/ViewRootImpl.java
+++ b/core/java/android/view/ViewRootImpl.java
@@ -213,6 +213,7 @@
     final Rect mVisRect; // used to retrieve visible rect of focused view.
 
     boolean mTraversalScheduled;
+    int mTraversalBarrier;
     long mLastTraversalFinishedTimeNanos;
     long mLastDrawFinishedTimeNanos;
     boolean mWillDrawSoon;
@@ -839,22 +840,28 @@
     public void scheduleTraversals() {
         if (!mTraversalScheduled) {
             mTraversalScheduled = true;
+            mTraversalBarrier = mHandler.getLooper().postSyncBarrier();
             scheduleFrame();
         }
     }
 
     public void unscheduleTraversals() {
-        mTraversalScheduled = false;
+        if (mTraversalScheduled) {
+            mTraversalScheduled = false;
+            mHandler.getLooper().removeSyncBarrier(mTraversalBarrier);
+        }
     }
 
     void scheduleFrame() {
         if (!mFrameScheduled) {
-            mChoreographer.postDrawCallback(mFrameRunnable);
             mFrameScheduled = true;
+            mChoreographer.postDrawCallback(mFrameRunnable);
         }
     }
 
     void unscheduleFrame() {
+        unscheduleTraversals();
+
         if (mFrameScheduled) {
             mFrameScheduled = false;
             mChoreographer.removeDrawCallback(mFrameRunnable);
@@ -869,6 +876,7 @@
 
         if (mTraversalScheduled) {
             mTraversalScheduled = false;
+            mHandler.getLooper().removeSyncBarrier(mTraversalBarrier);
             doTraversal();
         }
     }
@@ -3989,11 +3997,13 @@
 
     public void dispatchKey(KeyEvent event) {
         Message msg = mHandler.obtainMessage(MSG_DISPATCH_KEY, event);
+        msg.setAsynchronous(true);
         mHandler.sendMessage(msg);
     }
 
     public void dispatchKeyFromIme(KeyEvent event) {
         Message msg = mHandler.obtainMessage(MSG_DISPATCH_KEY_FROM_IME, event);
+        msg.setAsynchronous(true);
         mHandler.sendMessage(msg);
     }
 
diff --git a/core/java/android/view/inputmethod/BaseInputConnection.java b/core/java/android/view/inputmethod/BaseInputConnection.java
index d4f6f9c..89aba3c 100644
--- a/core/java/android/view/inputmethod/BaseInputConnection.java
+++ b/core/java/android/view/inputmethod/BaseInputConnection.java
@@ -19,7 +19,6 @@
 import android.content.Context;
 import android.content.res.TypedArray;
 import android.os.Bundle;
-import android.os.Handler;
 import android.os.SystemClock;
 import android.text.Editable;
 import android.text.NoCopySpan;