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.car.settings.profiles;
18 
19 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByDpm;
20 
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.content.Context;
23 import android.os.UserManager;
24 
25 import androidx.preference.Preference;
26 
27 import com.android.car.settings.R;
28 import com.android.car.settings.common.FragmentController;
29 import com.android.car.settings.common.PreferenceController;
30 import com.android.internal.annotations.VisibleForTesting;
31 
32 /**
33  * Business Logic for preference either adds a new profile or exits retail mode, depending on the
34  * current profile.
35  */
36 public class AddProfilePreferenceController extends PreferenceController<Preference> {
37 
38     @VisibleForTesting
39     static final String MAX_PROFILES_LIMIT_REACHED_DIALOG_TAG =
40             "com.android.car.settings.profiles.MaxProfilesLimitReachedDialog";
41 
42     private UserManager mUserManager;
43     private DemoProfileDialogHandler mDemoProfileDialogHandler;
44     private AddProfileHandler mAddProfileHandler;
45 
AddProfilePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)46     public AddProfilePreferenceController(Context context, String preferenceKey,
47             FragmentController fragmentController,
48             CarUxRestrictions uxRestrictions) {
49         super(context, preferenceKey, fragmentController, uxRestrictions);
50         mUserManager = context.getSystemService(UserManager.class);
51         mDemoProfileDialogHandler = new DemoProfileDialogHandler(context, fragmentController);
52         mAddProfileHandler = new AddProfileHandler(context, fragmentController, this);
53     }
54 
55     @Override
getPreferenceType()56     protected Class<Preference> getPreferenceType() {
57         return Preference.class;
58     }
59 
60     @Override
onCreateInternal()61     protected void onCreateInternal() {
62         mDemoProfileDialogHandler.onCreateInternal();
63         mAddProfileHandler.onCreateInternal();
64 
65         if (mUserManager.isDemoUser()) {
66             getPreference().setTitle(R.string.exit_retail_button_text);
67         } else {
68             getPreference().setTitle(R.string.add_profile_text);
69             getPreference().setIcon(R.drawable.ic_add);
70         }
71         setClickableWhileDisabled(getPreference(), /* clickable= */ true, p ->
72                 mAddProfileHandler.runClickableWhileDisabled());
73     }
74 
75     @Override
onStopInternal()76     protected void onStopInternal() {
77         mAddProfileHandler.onStopInternal();
78     }
79 
80     @Override
onDestroyInternal()81     protected void onDestroyInternal() {
82         mAddProfileHandler.onDestroyInternal();
83     }
84 
85     @Override
updateState(Preference preference)86     protected void updateState(Preference preference) {
87         mAddProfileHandler.updateState(preference);
88     }
89 
90     @Override
handlePreferenceClicked(Preference preference)91     protected boolean handlePreferenceClicked(Preference preference) {
92         // If the user is a demo user, show a dialog asking if they want to exit retail/demo mode.
93         if (mUserManager.isDemoUser()) {
94             mDemoProfileDialogHandler.showExitRetailDialog();
95             return true;
96         }
97 
98         if (!mUserManager.canAddMoreUsers()
99                 || hasUserRestrictionByDpm(getContext(), UserManager.DISALLOW_ADD_USER)) {
100             mAddProfileHandler.runClickableWhileDisabled();
101             return true;
102         }
103 
104         // Add the add profile button if the current profile is allowed to add a profile.
105         if (mAddProfileHandler.canAddProfiles(mUserManager)) {
106             mAddProfileHandler.showAddProfileDialog();
107             return true;
108         }
109 
110         return false;
111     }
112 
113     @Override
getDefaultAvailabilityStatus()114     protected int getDefaultAvailabilityStatus() {
115         return mAddProfileHandler
116                 .getAddProfilePreferenceAvailabilityStatus(getContext());
117     }
118 
119     @VisibleForTesting
setUserManager(UserManager userManager)120     void setUserManager(UserManager userManager) {
121         mUserManager = userManager;
122     }
123 }
124