• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.car.settings.profiles;
18 
19 import android.car.user.CarUserManager;
20 import android.car.user.UserCreationResult;
21 import android.car.util.concurrent.AsyncFuture;
22 import android.content.Context;
23 import android.content.pm.UserInfo;
24 import android.os.AsyncTask;
25 import android.os.UserManager;
26 
27 import com.android.car.internal.user.UserHelper;
28 import com.android.car.settings.common.Logger;
29 
30 import java.util.concurrent.ExecutionException;
31 
32 /**
33  * Task to add a new profile to the device
34  */
35 public class AddNewProfileTask extends AsyncTask<String, Void, UserInfo> {
36     private static final Logger LOG = new Logger(AddNewProfileTask.class);
37 
38     private final Context mContext;
39     private final CarUserManager mCarUserManager;
40     private final AddNewProfileListener mAddNewProfileListener;
41     private final UserManager mUserManager;
42 
AddNewProfileTask(Context context, CarUserManager carUserManager, AddNewProfileListener addNewProfileListener)43     public AddNewProfileTask(Context context, CarUserManager carUserManager,
44             AddNewProfileListener addNewProfileListener) {
45         mContext = context;
46         mCarUserManager = carUserManager;
47         mAddNewProfileListener = addNewProfileListener;
48         mUserManager = context.getSystemService(UserManager.class);
49     }
50 
51     @Override
doInBackground(String... profileNames)52     protected UserInfo doInBackground(String... profileNames) {
53         AsyncFuture<UserCreationResult> future = mCarUserManager.createUser(profileNames[0],
54                 /* flags= */ 0);
55         try {
56             UserCreationResult result = future.get();
57             if (result.isSuccess()) {
58                 UserInfo user = mUserManager.getUserInfo(result.getUser().getIdentifier());
59                 if (user != null) {
60                     UserHelper.setDefaultNonAdminRestrictions(mContext, user.getUserHandle(),
61                             /* enable= */ true);
62                     UserHelper.assignDefaultIcon(mContext, user.getUserHandle());
63                 } else {
64                     LOG.wtf("Inconsistent state: successful future with null profile - "
65                             + result.toString());
66                 }
67                 return user;
68             }
69         } catch (InterruptedException | ExecutionException e) {
70             if (e instanceof InterruptedException) {
71                 Thread.currentThread().interrupt();
72             }
73             LOG.e("Error creating new profile: ", e);
74         }
75         return null;
76     }
77 
78     @Override
onPreExecute()79     protected void onPreExecute() { }
80 
81     @Override
onPostExecute(UserInfo user)82     protected void onPostExecute(UserInfo user) {
83         if (user != null) {
84             mAddNewProfileListener.onProfileAddedSuccess();
85             mCarUserManager.switchUser(user.id);
86         } else {
87             mAddNewProfileListener.onProfileAddedFailure();
88         }
89     }
90 
91     /**
92      * Interface for getting notified when AddNewProfileTask has been completed.
93      */
94     public interface AddNewProfileListener {
95         /**
96          * Invoked in AddNewProfileTask.onPostExecute after the profile has been created
97          * successfully.
98          */
onProfileAddedSuccess()99         void onProfileAddedSuccess();
100 
101         /**
102          * Invoked in AddNewProfileTask.onPostExecute if new profile creation failed.
103          */
onProfileAddedFailure()104         void onProfileAddedFailure();
105     }
106 }
107