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