Use safe access to OnPreDrawListener.

Bug 32472451

It is important to remove an OnPreDrawListener from the correct
ViewTreeObserver. When a View is added to the view hierarchy, the
initial ViewTreeObserver is not active. The listener must then be
removed from the current OnPreDrawListener. When a View has been
removed from the hierarchy, it is important to remove the listener
from the orignal ViewTreeObserver.

Test: cts-tradefed run singleCommand cts -d --skip-preconditions
--skip-connectivity-check -m CtsUsageStatsTestCases
Test: cts-tradefed run singleCommand cts -d --skip-preconditions
--skip-connectivity-check -m CtsFragmentTestCases

Change-Id: I735f71d2d9c84e86ef846aab0088a8651300fbe8
7 files changed