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.enterprise;
18 
19 import static android.text.Html.FROM_HTML_MODE_LEGACY;
20 
21 import android.app.admin.DeviceAdminInfo;
22 import android.car.drivingstate.CarUxRestrictions;
23 import android.content.Context;
24 import android.text.Html;
25 
26 import androidx.preference.Preference;
27 
28 import com.android.car.settings.common.FragmentController;
29 
30 /**
31  * Controller for the screen that shows the device policies requested by a device admin app.
32  */
33 public final class DeviceAdminAddPoliciesPreferenceController
34         extends BaseDeviceAdminAddPreferenceController<Preference> {
35 
DeviceAdminAddPoliciesPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)36     public DeviceAdminAddPoliciesPreferenceController(Context context, String preferenceKey,
37                 FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
38         super(context, preferenceKey, fragmentController, uxRestrictions);
39     }
40 
41     @Override
getDefaultAvailabilityStatus()42     protected int getDefaultAvailabilityStatus() {
43         int superStatus = super.getDefaultAvailabilityStatus();
44         if (superStatus != AVAILABLE) return superStatus;
45 
46         return isProfileOrDeviceOwner() ? DISABLED_FOR_PROFILE : AVAILABLE;
47     }
48 
49     @Override
updateState(Preference preference)50     protected void updateState(Preference preference) {
51         preference.setTitle(getPolicyText());
52     }
53 
getPolicyText()54     private CharSequence getPolicyText() {
55         Context context = getContext();
56         boolean isSystemUser = mUm.isSystemUser();
57         StringBuilder result = new StringBuilder();
58 
59         for (DeviceAdminInfo.PolicyInfo pi : mDeviceAdminInfo.getUsedPolicies()) {
60             int labelId = isSystemUser ? pi.label : pi.labelForSecondaryUsers;
61             CharSequence label = context.getText(labelId);
62             mLogger.v("Adding policy: " + label);
63             result.append("<li>&nbsp;").append(label).append("</li>");
64         }
65 
66         return Html.fromHtml(result.toString(), FROM_HTML_MODE_LEGACY);
67     }
68 }
69