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