1 /*
2  * Copyright (C) 2021 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.accounts;
18 
19 import static android.os.UserManager.DISALLOW_MODIFY_ACCOUNTS;
20 
21 import static com.android.car.settings.enterprise.EnterpriseUtils.hasUserRestrictionByUm;
22 
23 import android.car.drivingstate.CarUxRestrictions;
24 import android.content.Context;
25 
26 import androidx.annotation.VisibleForTesting;
27 import androidx.preference.PreferenceGroup;
28 
29 import com.android.car.settings.common.FragmentController;
30 import com.android.car.settings.profiles.ProfileDetailsBasePreferenceController;
31 import com.android.car.settings.profiles.ProfileHelper;
32 
33 /**
34  * Controller for displaying accounts and associated actions.
35  * Ensures changes can only be made by the appropriate users.
36  */
37 public class AccountGroupPreferenceController extends
38         ProfileDetailsBasePreferenceController<PreferenceGroup> {
39 
AccountGroupPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)40     public AccountGroupPreferenceController(Context context, String preferenceKey,
41             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
42         super(context, preferenceKey, fragmentController, uxRestrictions);
43     }
44 
45     @Override
getPreferenceType()46     protected Class<PreferenceGroup> getPreferenceType() {
47         return PreferenceGroup.class;
48     }
49 
50     @Override
onCreateInternal()51     protected void onCreateInternal() {
52         super.onCreateInternal();
53         setClickableWhileDisabled(getPreference(), /* clickable= */ true, p -> getProfileHelper()
54                 .runClickableWhileDisabled(getContext(), getFragmentController()));
55     }
56 
57     @Override
getDefaultAvailabilityStatus()58     protected int getDefaultAvailabilityStatus() {
59         ProfileHelper profileHelper = getProfileHelper();
60         boolean isCurrentUser = profileHelper.isCurrentProcessUser(getUserInfo());
61         boolean canModifyAccounts = getProfileHelper().canCurrentProcessModifyAccounts();
62 
63         if (isCurrentUser && canModifyAccounts) {
64             return AVAILABLE;
65         }
66         if (!isCurrentUser || profileHelper.isDemoOrGuest()
67                 || hasUserRestrictionByUm(getContext(), DISALLOW_MODIFY_ACCOUNTS)) {
68             return DISABLED_FOR_PROFILE;
69         }
70         return AVAILABLE_FOR_VIEWING;
71     }
72 
73     @VisibleForTesting
getProfileHelper()74     ProfileHelper getProfileHelper() {
75         return ProfileHelper.getInstance(getContext());
76     }
77 }
78