1 /* 2 * Copyright (C) 2020 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.systemui.user 17 18 import android.app.Dialog 19 import android.content.Context 20 import android.content.pm.UserInfo 21 import android.graphics.drawable.Drawable 22 import android.os.UserManager 23 import com.android.internal.util.UserIcons 24 import com.android.settingslib.users.UserCreatingDialog 25 import com.android.systemui.dagger.qualifiers.Background 26 import com.android.systemui.dagger.qualifiers.Main 27 import java.util.concurrent.Executor 28 import java.util.function.Consumer 29 import javax.inject.Inject 30 31 /** 32 * A class to do the user creation process. It shows a progress dialog, and manages the user 33 * creation 34 */ 35 class UserCreator 36 @Inject 37 constructor( 38 private val context: Context, 39 private val userManager: UserManager, 40 @Main private val mainExecutor: Executor, 41 @Background private val bgExecutor: Executor 42 ) { 43 /** 44 * Shows a progress dialog then starts the user creation process on the main thread. 45 * 46 * @param successCallback is called when the user creation is successful. 47 * @param errorCallback is called when userManager.createUser returns null. (Exceptions are not 48 * handled by this class) 49 */ createUsernull50 fun createUser( 51 userName: String?, 52 userIcon: Drawable?, 53 successCallback: Consumer<UserInfo?>, 54 errorCallback: Runnable 55 ) { 56 val userCreationProgressDialog: Dialog = UserCreatingDialog(context) 57 userCreationProgressDialog.show() 58 59 // userManager.createUser will block the thread so post is needed for the dialog to show 60 bgExecutor.execute { 61 val user = userManager.createUser(userName, UserManager.USER_TYPE_FULL_SECONDARY, 0) 62 mainExecutor.execute main@{ 63 if (user == null) { 64 // Couldn't create user for some reason 65 userCreationProgressDialog.dismiss() 66 errorCallback.run() 67 return@main 68 } 69 bgExecutor.execute { 70 var newUserIcon = userIcon 71 val res = context.resources 72 if (newUserIcon == null) { 73 newUserIcon = UserIcons.getDefaultUserIcon(res, user.id, false) 74 } 75 userManager.setUserIcon( 76 user.id, 77 UserIcons.convertToBitmapAtUserIconSize(res, newUserIcon) 78 ) 79 } 80 userCreationProgressDialog.dismiss() 81 successCallback.accept(user) 82 } 83 } 84 } 85 setUserAdminnull86 fun setUserAdmin(userId: Int) { 87 userManager.setUserAdmin(userId) 88 } 89 isUserAdminnull90 fun isUserAdmin(): Boolean { 91 return userManager.isAdminUser 92 } 93 isMultipleAdminEnablednull94 fun isMultipleAdminEnabled(): Boolean { 95 return UserManager.isMultipleAdminEnabled() 96 } 97 } 98