Clean up certain state transitions in DreamService.

Instead of posting onDreamingStarted() to a handler from attach(), do
the work immediately.  This ensures that the dream is actually in the
expected state when the callback runs.  Previously it was possible
for the callback to run after detach() occurred which could cause
exceptions and unexpected behavior.  As it happens, there's no need
to post this callback since attach() already runs on the UI thread.

Handle certain races involving the window token lifecycle a little
better.  When the dream manager shuts down a dream, it removes the
window token.  This can happen before the dream completes its attach()
phase in which case a BadTokenException is thrown.  We now handle this
exception and abort the dream in anticipation of receiving a request
to finish it immediately.

Add a safeguard to getDozeHardware() to handle the case where it
might inadvertently be called at the wrong point in the lifecycle.

Bug: 13475612
Bug: 13760290
Change-Id: I9bc9c154370d08d7727b568d398c460a38592099
1 file changed