UserScopedService: Create services for users
This facility creates instances for '@UserHandleAware' system service APIs which do not provide methods which accept an explicit user id. In these cases, the service must be retreived from a user-specific Context using context.createContextAsUser. This replaces the typical pattern: @Application val context: Context = // injected fun isPrivateProfile(user: UserHandle): Boolean { val ctx = context.createContextAsUser(user) val service = ctx.getSystemService<UserManager>() return service.isPrivateProfile() } With the following: val userMgr: UserScopedService<UserManager> // Injected fun isPrivateProfile(user: UserHandle): Boolean { return userMgr.forUser(user).isPrivateProfile() } For testing, the functional interface can be implemented as: val mockUserMgr = mock<UserManager>() val scopedUserMgr = UserScopedService { _ -> mockUserManager } Bug: 327613051 Flag: NONE; CLs introducing usage should be guarded instead Test: NA Change-Id: I87881405ae1b93c6288ae998ae2c170d13db8d57
Loading
Please register or sign in to comment