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 android.app.admin.DeviceAdminInfo; 20 import android.car.drivingstate.CarUxRestrictions; 21 import android.content.Context; 22 23 import androidx.preference.PreferenceGroup; 24 25 import com.android.car.settings.R; 26 import com.android.car.settings.common.FragmentController; 27 import com.android.car.ui.preference.CarUiPreference; 28 29 /** 30 * Controller for the screen that shows the device policies requested by a device admin app. 31 */ 32 public final class DeviceAdminAddPoliciesPreferenceController 33 extends BaseDeviceAdminAddPreferenceController<PreferenceGroup> { 34 35 private final int mMaxDevicePoliciesShown; 36 DeviceAdminAddPoliciesPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)37 public DeviceAdminAddPoliciesPreferenceController(Context context, String preferenceKey, 38 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 39 super(context, preferenceKey, fragmentController, uxRestrictions); 40 mMaxDevicePoliciesShown = context.getResources() 41 .getInteger(R.integer.max_device_policies_shown); 42 } 43 44 @Override getPreferenceType()45 protected Class<PreferenceGroup> getPreferenceType() { 46 return PreferenceGroup.class; 47 } 48 49 @Override getRealAvailabilityStatus()50 protected int getRealAvailabilityStatus() { 51 return isProfileOrDeviceOwner() ? DISABLED_FOR_PROFILE : AVAILABLE; 52 } 53 54 @Override updateState(PreferenceGroup preferenceGroup)55 protected void updateState(PreferenceGroup preferenceGroup) { 56 Context context = getContext(); 57 preferenceGroup.removeAll(); 58 preferenceGroup.setInitialExpandedChildrenCount(mMaxDevicePoliciesShown); 59 60 boolean isAdminUser = mUm.isAdminUser(); 61 62 for (DeviceAdminInfo.PolicyInfo pi : mDeviceAdminInfo.getUsedPolicies()) { 63 int descriptionId = isAdminUser ? pi.description : pi.descriptionForSecondaryUsers; 64 int labelId = isAdminUser ? pi.label : pi.labelForSecondaryUsers; 65 CharSequence label = context.getText(labelId); 66 CharSequence description = context.getText(descriptionId); 67 mLogger.v("Adding policy '" + label + "': " + description); 68 CarUiPreference preference = new CarUiPreference(context); 69 preference.setTitle(label); 70 preference.setSummary(description); 71 preferenceGroup.addPreference(preference); 72 } 73 } 74 } 75