• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.intentresolver.data.repository
18 
19 import android.content.Context
20 import android.os.UserHandle
21 import androidx.core.content.getSystemService
22 import dagger.hilt.android.qualifiers.ApplicationContext
23 import kotlin.reflect.KClass
24 
25 /**
26  * Provides instances of a [system service][Context.getSystemService] created with
27  * [the context of a specified user][Context.createContextAsUser].
28  *
29  * Some services which have only `@UserHandleAware` APIs operate on the user id available from
30  * [Context.getUser], the context used to retrieve the service. This utility helps adapt a per-user
31  * API model to work in multi-user manner.
32  *
33  * Example usage:
34  * ```
35  *     @Provides
36  *     fun scopedUserManager(@ApplicationContext ctx: Context): UserScopedService<UserManager> {
37  *         return UserScopedServiceImpl(ctx, UserManager::class)
38  *     }
39  *
40  *     class MyUserHelper @Inject constructor(
41  *         private val userMgr: UserScopedService<UserManager>,
42  *     ) {
43  *         fun isPrivateProfile(user: UserHandle): UserManager {
44  *             return userMgr.forUser(user).isPrivateProfile()
45  *         }
46  *     }
47  * ```
48  */
interfacenull49 fun interface UserScopedService<T> {
50     /** Create a service instance for the given user. */
51     fun forUser(user: UserHandle): T
52 }
53 
54 class UserScopedServiceImpl<T : Any>(
55     @ApplicationContext private val context: Context,
56     private val serviceType: KClass<T>,
57 ) : UserScopedService<T> {
forUsernull58     override fun forUser(user: UserHandle): T {
59         val context =
60             if (context.user == user) {
61                 context
62             } else {
63                 context.createContextAsUser(user, 0)
64             }
65         return requireNotNull(context.getSystemService(serviceType.java))
66     }
67 }
68