Fix dispatcher crash when input monitoring enabled.
Also ensure that we actually log assertion failures.

Bug: 4203092
Change-Id: I5c14bc41b2255f876858121f5572e2de75cabcc3
diff --git a/libs/ui/Input.cpp b/libs/ui/Input.cpp
index 440ec00..bbe579e 100644
--- a/libs/ui/Input.cpp
+++ b/libs/ui/Input.cpp
@@ -724,7 +724,7 @@
             LOGD("  %d: position (%0.3f, %0.3f), vx=%0.3f, vy=%0.3f, speed=%0.3f",
                     id, positions[index].x, positions[index].y, vx, vy, sqrtf(vx * vx + vy * vy));
         } else {
-            assert(vx == 0 && vy == 0);
+            LOG_ASSERT(vx == 0 && vy == 0);
             LOGD("  %d: position (%0.3f, %0.3f), velocity not available",
                     id, positions[index].x, positions[index].y);
         }
diff --git a/services/input/EventHub.cpp b/services/input/EventHub.cpp
index 7625adb..9a9d9e5 100644
--- a/services/input/EventHub.cpp
+++ b/services/input/EventHub.cpp
@@ -450,7 +450,7 @@
 size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
     // Note that we only allow one caller to getEvents(), so don't need
     // to do locking here...  only when adding/removing devices.
-    assert(bufferSize >= 1);
+    LOG_ASSERT(bufferSize >= 1);
 
     if (!mOpened) {
         mError = openPlatformInput() ? NO_ERROR : UNKNOWN_ERROR;
diff --git a/services/input/InputDispatcher.cpp b/services/input/InputDispatcher.cpp
index 8363e8b..253d070 100644
--- a/services/input/InputDispatcher.cpp
+++ b/services/input/InputDispatcher.cpp
@@ -369,7 +369,7 @@
 
     // Now we have an event to dispatch.
     // All events are eventually dequeued and processed this way, even if we intend to drop them.
-    assert(mPendingEvent != NULL);
+    LOG_ASSERT(mPendingEvent != NULL);
     bool done = false;
     DropReason dropReason = DROP_REASON_NOT_DROPPED;
     if (!(mPendingEvent->policyFlags & POLICY_FLAG_PASS_TO_USER)) {
@@ -431,7 +431,7 @@
     }
 
     default:
-        assert(false);
+        LOG_ASSERT(false);
         break;
     }
 
@@ -558,7 +558,7 @@
         reason = "inbound event was dropped because it is stale";
         break;
     default:
-        assert(false);
+        LOG_ASSERT(false);
         return;
     }
 
@@ -962,7 +962,7 @@
             toString(resumeWithAppendedMotionSample));
 #endif
 
-    assert(eventEntry->dispatchInProgress); // should already have been set to true
+    LOG_ASSERT(eventEntry->dispatchInProgress); // should already have been set to true
 
     pokeUserActivityLocked(eventEntry);
 
@@ -1596,9 +1596,10 @@
 
         InputTarget& target = mCurrentInputTargets.editTop();
         target.inputChannel = mMonitoringChannels[i];
-        target.flags = 0;
+        target.flags = InputTarget::FLAG_DISPATCH_AS_IS;
         target.xOffset = 0;
         target.yOffset = 0;
+        target.pointerIds.clear();
     }
 }
 
@@ -1711,7 +1712,7 @@
 
     // Make sure we are never called for streaming when splitting across multiple windows.
     bool isSplit = inputTarget->flags & InputTarget::FLAG_SPLIT;
-    assert(! (resumeWithAppendedMotionSample && isSplit));
+    LOG_ASSERT(! (resumeWithAppendedMotionSample && isSplit));
 
     // Skip this event if the connection status is not normal.
     // We don't want to enqueue additional outbound events if the connection is broken.
@@ -1725,7 +1726,7 @@
 
     // Split a motion event if needed.
     if (isSplit) {
-        assert(eventEntry->type == EventEntry::TYPE_MOTION);
+        LOG_ASSERT(eventEntry->type == EventEntry::TYPE_MOTION);
 
         MotionEntry* originalMotionEntry = static_cast<MotionEntry*>(eventEntry);
         if (inputTarget->pointerIds.count() != originalMotionEntry->pointerCount) {
@@ -1832,7 +1833,7 @@
             resumeWithAppendedMotionSample, InputTarget::FLAG_DISPATCH_AS_IS);
 
     // If the outbound queue was previously empty, start the dispatch cycle going.
-    if (wasEmpty) {
+    if (wasEmpty && !connection->outboundQueue.isEmpty()) {
         activateConnectionLocked(connection.get());
         startDispatchCycleLocked(currentTime, connection);
     }
@@ -1880,11 +1881,11 @@
             connection->getInputChannelName());
 #endif
 
-    assert(connection->status == Connection::STATUS_NORMAL);
-    assert(! connection->outboundQueue.isEmpty());
+    LOG_ASSERT(connection->status == Connection::STATUS_NORMAL);
+    LOG_ASSERT(! connection->outboundQueue.isEmpty());
 
     DispatchEntry* dispatchEntry = connection->outboundQueue.headSentinel.next;
-    assert(! dispatchEntry->inProgress);
+    LOG_ASSERT(! dispatchEntry->inProgress);
 
     // Mark the dispatch entry as in progress.
     dispatchEntry->inProgress = true;
@@ -2005,7 +2006,7 @@
     }
 
     default: {
-        assert(false);
+        LOG_ASSERT(false);
     }
     }
 
@@ -2242,7 +2243,7 @@
 
 InputDispatcher::MotionEntry*
 InputDispatcher::splitMotionEvent(const MotionEntry* originalMotionEntry, BitSet32 pointerIds) {
-    assert(pointerIds.value != 0);
+    LOG_ASSERT(pointerIds.value != 0);
 
     uint32_t splitPointerIndexMap[MAX_POINTERS];
     int32_t splitPointerIds[MAX_POINTERS];
@@ -3510,7 +3511,7 @@
                         return;
                     }
 
-                    assert(connection->outboundQueue.headSentinel.next == dispatchEntry);
+                    LOG_ASSERT(connection->outboundQueue.headSentinel.next == dispatchEntry);
 
                     // Latch the fallback keycode for this key on an initial down.
                     // The fallback keycode cannot change at any other point in the lifecycle.
@@ -3523,7 +3524,7 @@
                         connection->inputState.setFallbackKey(originalKeyCode, fallbackKeyCode);
                     }
 
-                    assert(fallbackKeyCode != -1);
+                    LOG_ASSERT(fallbackKeyCode != -1);
 
                     // Cancel the fallback key if the policy decides not to send it anymore.
                     // We will continue to dispatch the key to the policy but we will no
@@ -3761,7 +3762,7 @@
     if (injectionState->refCount == 0) {
         mInjectionStatePool.free(injectionState);
     } else {
-        assert(injectionState->refCount > 0);
+        LOG_ASSERT(injectionState->refCount > 0);
     }
 }
 
@@ -3777,7 +3778,7 @@
         releaseMotionEntry(static_cast<MotionEntry*>(entry));
         break;
     default:
-        assert(false);
+        LOG_ASSERT(false);
         break;
     }
 }
@@ -3789,7 +3790,7 @@
         releaseEventEntryInjectionState(entry);
         mConfigurationChangeEntryPool.free(entry);
     } else {
-        assert(entry->refCount > 0);
+        LOG_ASSERT(entry->refCount > 0);
     }
 }
 
@@ -3799,7 +3800,7 @@
         releaseEventEntryInjectionState(entry);
         mKeyEntryPool.free(entry);
     } else {
-        assert(entry->refCount > 0);
+        LOG_ASSERT(entry->refCount > 0);
     }
 }
 
@@ -3814,7 +3815,7 @@
         }
         mMotionEntryPool.free(entry);
     } else {
-        assert(entry->refCount > 0);
+        LOG_ASSERT(entry->refCount > 0);
     }
 }
 
diff --git a/services/input/InputReader.cpp b/services/input/InputReader.cpp
index 82cf62f..6db445e 100644
--- a/services/input/InputReader.cpp
+++ b/services/input/InputReader.cpp
@@ -293,7 +293,7 @@
                 handleConfigurationChanged(rawEvent->when);
                 break;
             default:
-                assert(false); // can't happen
+                LOG_ASSERT(false); // can't happen
                 break;
             }
         }
@@ -1378,7 +1378,7 @@
         dump.append(INDENT4 "Mode: navigation\n");
         break;
     default:
-        assert(false);
+        LOG_ASSERT(false);
     }
 
     dump.appendFormat(INDENT4 "OrientationAware: %s\n",
@@ -1801,7 +1801,7 @@
         mPointerSource = AINPUT_SOURCE_MOUSE;
         break;
     default:
-        assert(false);
+        LOG_ASSERT(false);
     }
 
     // Configure absolute axis information.
@@ -1874,7 +1874,7 @@
         dump.append(INDENT4 "DeviceType: pointer\n");
         break;
     default:
-        assert(false);
+        LOG_ASSERT(false);
     }
 
     dump.appendFormat(INDENT4 "AssociatedDisplayId: %d\n",
@@ -2509,7 +2509,7 @@
         dump.append(INDENT4 "touch.touchSize.calibration: pressure\n");
         break;
     default:
-        assert(false);
+        LOG_ASSERT(false);
     }
 
     // Tool Size
@@ -2527,7 +2527,7 @@
         dump.append(INDENT4 "touch.toolSize.calibration: area\n");
         break;
     default:
-        assert(false);
+        LOG_ASSERT(false);
     }
 
     if (mCalibration.haveToolSizeLinearScale) {
@@ -2567,7 +2567,7 @@
         dump.append(INDENT4 "touch.pressure.calibration: amplitude\n");
         break;
     default:
-        assert(false);
+        LOG_ASSERT(false);
     }
 
     switch (mCalibration.pressureSource) {
@@ -2580,7 +2580,7 @@
     case Calibration::PRESSURE_SOURCE_DEFAULT:
         break;
     default:
-        assert(false);
+        LOG_ASSERT(false);
     }
 
     if (mCalibration.havePressureScale) {
@@ -2597,7 +2597,7 @@
         dump.append(INDENT4 "touch.size.calibration: normalized\n");
         break;
     default:
-        assert(false);
+        LOG_ASSERT(false);
     }
 
     // Orientation
@@ -2612,7 +2612,7 @@
         dump.append(INDENT4 "touch.orientation.calibration: vector\n");
         break;
     default:
-        assert(false);
+        LOG_ASSERT(false);
     }
 }
 
@@ -2901,7 +2901,7 @@
         // Although applications receive new locations as part of individual pointer up
         // events, they do not generally handle them except when presented in a move event.
         if (moveNeeded) {
-            assert(moveIdBits.value == dispatchedIdBits.value);
+            LOG_ASSERT(moveIdBits.value == dispatchedIdBits.value);
             dispatchMotion(when, policyFlags, mTouchSource,
                     AMOTION_EVENT_ACTION_MOVE, 0, metaState, 0,
                     mCurrentTouchCoords, mCurrentTouch.idToIndex, dispatchedIdBits, -1,
@@ -3550,7 +3550,7 @@
         // Case 4. Exactly one finger down, button is not pressed. (HOVER)
         // The pointer follows the active touch point.
         // Emit HOVER_MOVE events at the pointer location.
-        assert(activeTouchId >= 0);
+        LOG_ASSERT(activeTouchId >= 0);
 
 #if DEBUG_GESTURES
         LOGD("Gestures: HOVER");
@@ -3600,7 +3600,7 @@
         // Fix the centroid of the figure when the gesture actually starts.
         // We do not recalculate the centroid at any other time during the gesture because
         // it would affect the relationship of the touch points relative to the pointer location.
-        assert(activeTouchId >= 0);
+        LOG_ASSERT(activeTouchId >= 0);
 
         uint32_t currentTouchPointerCount = mCurrentTouch.pointerCount;
         if (currentTouchPointerCount > MAX_POINTERS) {
@@ -3712,7 +3712,7 @@
                     "activeGestureId=%d, currentTouchPointerCount=%d",
                     activeTouchId, mPointerGesture.activeGestureId, currentTouchPointerCount);
 #endif
-            assert(mPointerGesture.activeGestureId >= 0);
+            LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
 
             float x = (mCurrentTouch.pointers[0].x + mCurrentTouch.pointers[1].x
                     - mPointerGesture.initialCentroidX * 2) * 0.5f
@@ -3736,7 +3736,7 @@
                     "activeGestureId=%d, currentTouchPointerCount=%d",
                     activeTouchId, mPointerGesture.activeGestureId, currentTouchPointerCount);
 #endif
-            assert(mPointerGesture.activeGestureId >= 0);
+            LOG_ASSERT(mPointerGesture.activeGestureId >= 0);
 
             mPointerGesture.currentGesturePointerCount = currentTouchPointerCount;
             mPointerGesture.currentGestureIdBits.clear();
@@ -3895,7 +3895,7 @@
         pointerCount += 1;
     }
 
-    assert(pointerCount != 0);
+    LOG_ASSERT(pointerCount != 0);
 
     if (changedId >= 0 && pointerCount == 1) {
         // Replace initial down and final up action.
@@ -3907,7 +3907,7 @@
             action = AMOTION_EVENT_ACTION_UP;
         } else {
             // Can't happen.
-            assert(false);
+            LOG_ASSERT(false);
         }
     }
 
@@ -4072,7 +4072,7 @@
                     // Previous iterations consumed the root element of the heap.
                     // Pop root element off of the heap (sift down).
                     heapSize -= 1;
-                    assert(heapSize > 0);
+                    LOG_ASSERT(heapSize > 0);
 
                     // Sift down.
                     heap[0] = heap[heapSize];