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 package com.android.bedstead.harrier.components 17 18 import com.android.bedstead.harrier.BedsteadServiceLocator 19 import com.android.bedstead.harrier.UserType 20 import com.android.bedstead.harrier.UserType.ADDITIONAL_USER 21 import com.android.bedstead.harrier.UserType.ADMIN_USER 22 import com.android.bedstead.harrier.UserType.ANY 23 import com.android.bedstead.harrier.UserType.CLONE_PROFILE 24 import com.android.bedstead.harrier.UserType.CURRENT_USER 25 import com.android.bedstead.harrier.UserType.DPC_USER 26 import com.android.bedstead.harrier.UserType.INITIAL_USER 27 import com.android.bedstead.harrier.UserType.INSTRUMENTED_USER 28 import com.android.bedstead.harrier.UserType.PRIMARY_USER 29 import com.android.bedstead.harrier.UserType.PRIVATE_PROFILE 30 import com.android.bedstead.harrier.UserType.SECONDARY_USER 31 import com.android.bedstead.harrier.UserType.SYSTEM_USER 32 import com.android.bedstead.harrier.UserType.TV_PROFILE 33 import com.android.bedstead.harrier.UserType.WORK_PROFILE 34 import com.android.bedstead.nene.TestApis.users 35 import com.android.bedstead.nene.users.UserReference 36 37 /** 38 * An interface to create the appropriate [UserReference] for the [UserType] 39 */ 40 interface IUserTypeResolver { toUsernull41 fun toUser(userType: UserType): UserReference 42 } 43 44 /** 45 * A tool to create the appropriate [UserReference] for the [UserType] 46 */ 47 class UserTypeResolver(locator: BedsteadServiceLocator) { 48 49 private val multiUserResolver: IUserTypeResolver by lazy { 50 locator.get("com.android.bedstead.multiuser.MultiUserUserTypeResolver") 51 } 52 private val enterpriseResolver: IUserTypeResolver by lazy { 53 locator.get("com.android.bedstead.enterprise.EnterpriseUserTypeResolver") 54 } 55 56 /** 57 * Creates appropriate [UserReference] for the [UserType] 58 */ 59 @Suppress("DEPRECATION") 60 fun toUser(userType: UserType): UserReference { 61 return when (userType) { 62 SYSTEM_USER -> users().system() 63 INSTRUMENTED_USER -> users().instrumented() 64 CURRENT_USER -> users().current() 65 PRIMARY_USER -> users().primary() 66 INITIAL_USER -> users().initial() 67 ADMIN_USER -> users().admin() 68 WORK_PROFILE, DPC_USER -> enterpriseResolver.toUser(userType) 69 SECONDARY_USER, TV_PROFILE, ADDITIONAL_USER, CLONE_PROFILE, PRIVATE_PROFILE 70 -> multiUserResolver.toUser(userType) 71 72 ANY -> throw IllegalStateException("ANY UserType can not be used here") 73 } 74 } 75 76 /** 77 * Calls the provided [action] with the appropriate [UserReference]s for the [UserType]. 78 * It will be called for all users if [userType] is [ANY]. 79 */ 80 inline fun toUser(userType: UserType, crossinline action: (UserReference) -> Unit ) { 81 if (userType == ANY) { 82 users().all().forEach(action) 83 } else { 84 action(toUser(userType)) 85 } 86 } 87 } 88