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 package com.android.car.settings.enterprise;
17 
18 import android.car.drivingstate.CarUxRestrictions;
19 import android.content.ComponentName;
20 import android.content.Context;
21 
22 import androidx.preference.Preference;
23 
24 import com.android.car.settings.R;
25 import com.android.car.settings.common.FragmentController;
26 import com.android.settingslib.utils.StringUtil;
27 
28 import java.util.List;
29 
30 /**
31  * Controller for showing the device admin apps.
32  */
33 public final class ManageDeviceAdminPreferenceController
34         extends BaseEnterprisePreferenceController<Preference> {
35 
ManageDeviceAdminPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)36     public ManageDeviceAdminPreferenceController(Context context, String preferenceKey,
37             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
38         super(context, preferenceKey, fragmentController, uxRestrictions);
39     }
40 
41     @Override
updateState(Preference preference)42     protected void updateState(Preference preference) {
43         int activeAdmins = getNumberOfAdmins();
44         mLogger.d("updateState(): Number of active device admin apps: " + activeAdmins);
45         String summary = activeAdmins == 0
46                 ? getContext().getString(R.string.number_of_device_admins_none)
47                 : StringUtil.getIcuPluralsString(getContext(), activeAdmins,
48                         R.string.number_of_device_admins);
49         preference.setSummary(summary);
50     }
51 
getNumberOfAdmins()52     private int getNumberOfAdmins() {
53         // NOTE: Only considering the current user for now. Later we may need to support profiles.
54         List<ComponentName> activeAdmins = mDpm.getActiveAdmins();
55         mLogger.d("Active admin apps: " + activeAdmins
56                 + " for current user: " + getContext().getUserId());
57         if (activeAdmins == null) {
58             return 0;
59         }
60         return activeAdmins.size();
61     }
62 }
63