Prevent crashes when user is stopping

Added defensive checks to prevent sporadic crashes due to various race
conditions when user is stopped:
* onPackageBroadcastReceived - moved the check inside the synchronized block.
  This guarantees that onUserStopped (that also grabs mLock) is not yet
  called and we can proceed.
* stopListening - Since it is invoked before onUserStopped callback, we can call
  ensureGroupStateLoadedLocked(userId, false). The state will be subsequently cleaned
  up in the onUserStopped.

Test: manual user switching + SecondaryUserContentResolverTest and
      ManagedUserContentResolverTest
Fixes: 37472670
Fixes: 36890589

Change-Id: I44d0a0c004e9c3e6cfb37a24cb8a5b6f20a42f13
1 file changed