Improve how LockSettingsService cleans up stale user

LSS keys a user's on-disk states using its user ID. When the user
is removed these states needs to be cleared. This normally happens
during ACTION_USER_REMOVED but if LSS misses the broadcast (device
reboot, system server crashes) the state needs to be clean up later
in another way. At the moment LSS performs this clean up when a new
user which reuses the user id is created during ACTION_USER_ADDED,
however that's causing issues: ACTION_USER_ADDED is asynchronously
delivered so when it's delivered late (later than onUnlockUser), it
can destroy credential data created for the new user.

As the fix, we move the clean up to the beginning of onUnlockUser.
Serial number for each user is recorded and compared to check for stale
user and trigger the clean up.

Bug: 142707469
Bug: 131834071
Test: manual
Change-Id: I1621c5384b2a8b937a269680880e7840de4c7552
1 file changed