• 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.Activity;
20 import android.app.Dialog;
21 import android.app.IActivityManager;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.graphics.drawable.Drawable;
25 import android.os.Bundle;
26 import android.os.RemoteException;
27 import android.util.Log;
28 
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 
32 import com.android.settingslib.users.EditUserInfoController;
33 import com.android.systemui.R;
34 
35 import javax.inject.Inject;
36 
37 /**
38  * This screen shows a Dialog for choosing nickname and photo for a new user, and then delegates the
39  * user creation to a UserCreator.
40  */
41 public class CreateUserActivity extends Activity {
42 
43     /**
44      * Creates an intent to start this activity.
45      */
createIntentForStart(Context context)46     public static Intent createIntentForStart(Context context) {
47         return new Intent(context, CreateUserActivity.class);
48     }
49 
50     private static final String TAG = "CreateUserActivity";
51     private static final String DIALOG_STATE_KEY = "create_user_dialog_state";
52 
53     private final UserCreator mUserCreator;
54     private final EditUserInfoController mEditUserInfoController;
55     private final IActivityManager mActivityManager;
56 
57     private Dialog mSetupUserDialog;
58 
59     @Inject
CreateUserActivity(UserCreator userCreator, EditUserInfoController editUserInfoController, IActivityManager activityManager)60     public CreateUserActivity(UserCreator userCreator,
61             EditUserInfoController editUserInfoController, IActivityManager activityManager) {
62         mUserCreator = userCreator;
63         mEditUserInfoController = editUserInfoController;
64         mActivityManager = activityManager;
65     }
66 
67     @Override
onCreate(@ullable Bundle savedInstanceState)68     protected void onCreate(@Nullable Bundle savedInstanceState) {
69         super.onCreate(savedInstanceState);
70         setShowWhenLocked(true);
71         setContentView(R.layout.activity_create_new_user);
72 
73         if (savedInstanceState != null) {
74             mEditUserInfoController.onRestoreInstanceState(savedInstanceState);
75         }
76 
77         mSetupUserDialog = createDialog();
78         mSetupUserDialog.show();
79     }
80 
81     @Override
onSaveInstanceState(@onNull Bundle outState)82     protected void onSaveInstanceState(@NonNull Bundle outState) {
83         if (mSetupUserDialog != null && mSetupUserDialog.isShowing()) {
84             outState.putBundle(DIALOG_STATE_KEY, mSetupUserDialog.onSaveInstanceState());
85         }
86 
87         mEditUserInfoController.onSaveInstanceState(outState);
88         super.onSaveInstanceState(outState);
89     }
90 
91     @Override
onRestoreInstanceState(@onNull Bundle savedInstanceState)92     protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
93         super.onRestoreInstanceState(savedInstanceState);
94         Bundle savedDialogState = savedInstanceState.getBundle(DIALOG_STATE_KEY);
95         if (savedDialogState != null && mSetupUserDialog != null) {
96             mSetupUserDialog.onRestoreInstanceState(savedDialogState);
97         }
98     }
99 
createDialog()100     private Dialog createDialog() {
101         String defaultUserName = getString(com.android.settingslib.R.string.user_new_user_name);
102 
103         return mEditUserInfoController.createDialog(
104                 this,
105                 (intent, requestCode) -> {
106                     mEditUserInfoController.startingActivityForResult();
107                     startActivityForResult(intent, requestCode);
108                 },
109                 null,
110                 defaultUserName,
111                 getString(R.string.user_add_user),
112                 this::addUserNow,
113                 this::finish
114         );
115     }
116 
117     @Override
onActivityResult(int requestCode, int resultCode, Intent data)118     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
119         super.onActivityResult(requestCode, resultCode, data);
120         mEditUserInfoController.onActivityResult(requestCode, resultCode, data);
121     }
122 
123     @Override
onBackPressed()124     public void onBackPressed() {
125         super.onBackPressed();
126         if (mSetupUserDialog != null) {
127             mSetupUserDialog.dismiss();
128         }
129     }
130 
addUserNow(String userName, Drawable userIcon)131     private void addUserNow(String userName, Drawable userIcon) {
132         mSetupUserDialog.dismiss();
133 
134         userName = (userName == null || userName.trim().isEmpty())
135                 ? getString(R.string.user_new_user_name)
136                 : userName;
137 
138         mUserCreator.createUser(userName, userIcon,
139                 userInfo -> {
140                     switchToUser(userInfo.id);
141                     finishIfNeeded();
142                 }, () -> {
143                     Log.e(TAG, "Unable to create user");
144                     finishIfNeeded();
145                 });
146     }
147 
finishIfNeeded()148     private void finishIfNeeded() {
149         if (!isFinishing() && !isDestroyed()) {
150             finish();
151         }
152     }
153 
switchToUser(int userId)154     private void switchToUser(int userId) {
155         try {
156             mActivityManager.switchUser(userId);
157         } catch (RemoteException e) {
158             Log.e(TAG, "Couldn't switch user.", e);
159         }
160     }
161 }
162