• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.systemui.user;
18 
19 import android.app.Dialog;
20 import android.content.Context;
21 import android.content.pm.UserInfo;
22 import android.graphics.drawable.Drawable;
23 import android.os.UserManager;
24 
25 import com.android.internal.util.UserIcons;
26 import com.android.settingslib.users.UserCreatingDialog;
27 import com.android.settingslib.utils.ThreadUtils;
28 
29 import java.util.function.Consumer;
30 
31 import javax.inject.Inject;
32 
33 /**
34  * A class to do the user creation process. It shows a progress dialog, and manages the user
35  * creation
36  */
37 public class UserCreator {
38 
39     private final Context mContext;
40     private final UserManager mUserManager;
41 
42     @Inject
UserCreator(Context context, UserManager userManager)43     public UserCreator(Context context, UserManager userManager) {
44         mContext = context;
45         mUserManager = userManager;
46     }
47 
48     /**
49      * Shows a progress dialog then starts the user creation process on the main thread.
50      *
51      * @param successCallback is called when the user creation is successful.
52      * @param errorCallback   is called when userManager.createUser returns null.
53      *                        (Exceptions are not handled by this class)
54      */
createUser(String userName, Drawable userIcon, Consumer<UserInfo> successCallback, Runnable errorCallback)55     public void createUser(String userName, Drawable userIcon, Consumer<UserInfo> successCallback,
56             Runnable errorCallback) {
57 
58         Dialog userCreationProgressDialog = new UserCreatingDialog(mContext);
59         userCreationProgressDialog.show();
60 
61         // userManager.createUser will block the thread so post is needed for the dialog to show
62         ThreadUtils.postOnMainThread(() -> {
63             UserInfo user =
64                     mUserManager.createUser(userName, UserManager.USER_TYPE_FULL_SECONDARY, 0);
65             if (user == null) {
66                 // Couldn't create user for some reason
67                 userCreationProgressDialog.dismiss();
68                 errorCallback.run();
69                 return;
70             }
71 
72             Drawable newUserIcon = userIcon;
73             if (newUserIcon == null) {
74                 newUserIcon = UserIcons.getDefaultUserIcon(mContext.getResources(), user.id, false);
75             }
76             mUserManager.setUserIcon(user.id, UserIcons.convertToBitmap(newUserIcon));
77 
78             userCreationProgressDialog.dismiss();
79             successCallback.accept(user);
80         });
81     }
82 }
83