Dismiss Dialog in post instead of immediately

When ViewGroup is dispatching touch events to children, it can
happen that a child, upon receiving a touch event, may decide
to close the window. In this case, dispatchDetachedFromWindow will be
called, which would invalidate (=recycle) the TouchTargets. As the
ViewGroup would continue dispatching the event to other children, those
TouchTargets would already no longer be valid. This could cause an NPE.

In one particular example, there could be several views in a Dialog, and
one of the views would dismiss the Dialog in a click handler.
Since the click handler executes events immediately, this makes
ViewGroup.dispatchTouchEvent recurse on itself, and modify the internal
state. This would later cause the NPE.

Calling dismiss in a message avoids this issue by letting the event
handling finish before dispatching dispatchDetachedFromWindow, which
would cause another ACTION_CANCEL to be sent to the view.

Bug: 26611563
Test: created a sample app and recorded a sequence of touch events using
inputstudio in order to reproduce the crash. The repro rate was 100%.
Could no longer repro with this change.

Change-Id: Ia1d8070152c7ba2d14cddebcc5844994fe3ab5da
Merged-In: Ia1d8070152c7ba2d14cddebcc5844994fe3ab5da
2 files changed