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 @Inject constructor( 36 private val context: Context, 37 private val userManager: UserManager, 38 @Main private val mainExecutor: Executor, 39 @Background private val bgExecutor: Executor 40 ) { 41 /** 42 * Shows a progress dialog then starts the user creation process on the main thread. 43 * 44 * @param successCallback is called when the user creation is successful. 45 * @param errorCallback is called when userManager.createUser returns null. 46 * (Exceptions are not handled by this class) 47 */ createUsernull48 fun createUser( 49 userName: String?, 50 userIcon: Drawable?, 51 successCallback: Consumer<UserInfo?>, 52 errorCallback: Runnable 53 ) { 54 val userCreationProgressDialog: Dialog = UserCreatingDialog(context) 55 userCreationProgressDialog.show() 56 57 // userManager.createUser will block the thread so post is needed for the dialog to show 58 bgExecutor.execute { 59 val user = userManager.createUser(userName, UserManager.USER_TYPE_FULL_SECONDARY, 0) 60 mainExecutor.execute main@{ 61 if (user == null) { 62 // Couldn't create user for some reason 63 userCreationProgressDialog.dismiss() 64 errorCallback.run() 65 return@main 66 } 67 bgExecutor.execute { 68 var newUserIcon = userIcon 69 val res = context.resources 70 if (newUserIcon == null) { 71 newUserIcon = UserIcons.getDefaultUserIcon(res, user.id, false) 72 } 73 userManager.setUserIcon( 74 user.id, UserIcons.convertToBitmapAtUserIconSize(res, newUserIcon)) 75 } 76 userCreationProgressDialog.dismiss() 77 successCallback.accept(user) 78 } 79 } 80 } 81 } 82