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.app.admin.DevicePolicyManager; 21 import android.car.drivingstate.CarUxRestrictions; 22 import android.content.ComponentName; 23 import android.content.Context; 24 import android.content.pm.PackageManager; 25 import android.content.res.Resources; 26 import android.os.UserManager; 27 28 import androidx.annotation.Nullable; 29 import androidx.preference.Preference; 30 31 import com.android.car.settings.common.FragmentController; 32 import com.android.car.settings.common.Logger; 33 import com.android.car.settings.common.PreferenceController; 34 35 /** 36 * Base class for controllers in the enterprise module. 37 */ 38 abstract class BaseEnterprisePreferenceController<P extends Preference> 39 extends PreferenceController<P> { 40 41 protected final Logger mLogger = new Logger(getClass()); 42 43 protected final DevicePolicyManager mDpm; 44 protected final PackageManager mPm; 45 protected final UserManager mUm; 46 47 private final boolean mHasFeature; 48 BaseEnterprisePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)49 protected BaseEnterprisePreferenceController(Context context, String preferenceKey, 50 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 51 super(context, preferenceKey, fragmentController, uxRestrictions); 52 53 mHasFeature = context.getPackageManager() 54 .hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN); 55 56 mDpm = context.getSystemService(DevicePolicyManager.class); 57 mPm = context.getPackageManager(); 58 mUm = context.getSystemService(UserManager.class); 59 } 60 61 @Override getPreferenceType()62 protected Class<P> getPreferenceType() { 63 return (Class<P>) Preference.class; 64 } 65 66 @Override getAvailabilityStatus()67 protected int getAvailabilityStatus() { 68 return mHasFeature ? AVAILABLE : UNSUPPORTED_ON_DEVICE; 69 } 70 isProfileOrDeviceOwner(ComponentName admin)71 final boolean isProfileOrDeviceOwner(ComponentName admin) { 72 return admin.equals(mDpm.getProfileOwner()) 73 || admin.equals(mDpm.getDeviceOwnerComponentOnCallingUser()); 74 } 75 76 @Nullable getDescription(DeviceAdminInfo deviceAdminInfo)77 protected final CharSequence getDescription(DeviceAdminInfo deviceAdminInfo) { 78 try { 79 return deviceAdminInfo.loadDescription(mPm); 80 } catch (Resources.NotFoundException e) { 81 mLogger.v("No description for " 82 + deviceAdminInfo.getComponent().flattenToShortString()); 83 } 84 return null; 85 } 86 } 87