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.users;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.content.Context;
21 import android.graphics.drawable.Drawable;
22 
23 import com.android.car.settings.R;
24 import com.android.car.settings.common.ButtonPreference;
25 import com.android.car.settings.common.FragmentController;
26 
27 /** Business logic for the preference which opens the EditUserNameFragment. */
28 public class EditUserNameEntryPreferenceController extends
29         UserDetailsBasePreferenceController<ButtonPreference> {
30 
EditUserNameEntryPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)31     public EditUserNameEntryPreferenceController(Context context, String preferenceKey,
32             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
33         super(context, preferenceKey, fragmentController, uxRestrictions);
34     }
35 
36     @Override
getPreferenceType()37     protected Class<ButtonPreference> getPreferenceType() {
38         return ButtonPreference.class;
39     }
40 
41     @Override
updateState(ButtonPreference preference)42     protected void updateState(ButtonPreference preference) {
43         preference.setOnButtonClickListener(pref -> {
44             getFragmentController().launchFragment(EditUsernameFragment.newInstance(getUserInfo()));
45         });
46 
47         Drawable icon = new UserIconProvider(getCarUserManagerHelper()).getUserIcon(getUserInfo(),
48                 getContext());
49         preference.setIcon(icon);
50         preference.setTitle(UserUtils.getUserDisplayName(getContext(), getCarUserManagerHelper(),
51                 getUserInfo()));
52 
53         if (!getCarUserManagerHelper().isCurrentProcessUser(getUserInfo())) {
54             preference.showAction(false);
55         }
56         preference.setSummary(getSummary());
57     }
58 
getSummary()59     private CharSequence getSummary() {
60         if (!getUserInfo().isInitialized()) {
61             return getContext().getString(R.string.user_summary_not_set_up);
62         }
63         if (getUserInfo().isAdmin()) {
64             return getCarUserManagerHelper().isCurrentProcessUser(getUserInfo())
65                     ? getContext().getString(R.string.signed_in_admin_user)
66                     : getContext().getString(R.string.user_admin);
67         }
68         return null;
69     }
70 }
71