Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Skip to content
Commit e7fd568d authored by Mark Renouf's avatar Mark Renouf
Browse files

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
parent cf909694
Loading
Loading
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment