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

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
+8 −0
Original line number Diff line number Diff line
@@ -119,6 +119,8 @@ import com.android.systemui.dagger.qualifiers.DisplayId;
import com.android.systemui.dagger.qualifiers.Main;
import com.android.systemui.dagger.qualifiers.TestHarness;
import com.android.systemui.shared.system.PackageManagerWrapper;
import com.android.systemui.user.utils.UserScopedService;
import com.android.systemui.user.utils.UserScopedServiceImpl;

import dagger.Module;
import dagger.Provides;
@@ -623,6 +625,12 @@ public class FrameworkServicesModule {
        return context.getSystemService(UserManager.class);
    }

    @Provides
    @Singleton
    static UserScopedService<UserManager> provideScopedUserManager(@Application Context context) {
        return new UserScopedServiceImpl<>(context, UserManager.class);
    }

    @Provides
    static WallpaperManager provideWallpaperManager(Context context) {
        return context.getSystemService(WallpaperManager.class);
+66 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.user.utils

import android.content.Context
import android.os.UserHandle
import androidx.core.content.getSystemService
import com.android.systemui.dagger.qualifiers.Application

/**
 * Provides instances of a [system service][Context.getSystemService] created with
 * [the context of a specified user][Context.createContextAsUser].
 *
 * Some services which have only `@UserHandleAware` APIs operate on the user id available from
 * [Context.getUser], the context used to retrieve the service. This utility helps adapt a per-user
 * API model to work in multi-user manner.
 *
 * Example usage:
 * ```
 *     @Provides
 *     fun scopedUserManager(@Application ctx: Context): UserScopedService<UserManager> {
 *         return UserScopedServiceImpl(ctx, UserManager::class)
 *     }
 *
 *     class MyUserHelper @Inject constructor(
 *         private val userMgr: UserScopedService<UserManager>,
 *     ) {
 *         fun isPrivateProfile(user: UserHandle): UserManager {
 *             return userMgr.forUser(user).isPrivateProfile()
 *         }
 *     }
 * ```
 */
fun interface UserScopedService<T> {
    /** Create a service instance for the given user. */
    fun forUser(user: UserHandle): T
}

class UserScopedServiceImpl<T : Any>(
    @Application private val context: Context,
    private val serviceType: Class<T>,
) : UserScopedService<T> {
    override fun forUser(user: UserHandle): T {
        val context =
            if (context.user == user) {
                context
            } else {
                context.createContextAsUser(user, 0)
            }
        return requireNotNull(context.getSystemService(serviceType))
    }
}