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.pm.UserInfo;
26 import android.content.res.Resources;
27 import android.os.UserHandle;
28 import android.os.UserManager;
29 
30 import androidx.annotation.Nullable;
31 import androidx.preference.Preference;
32 
33 import com.android.car.settings.common.FragmentController;
34 import com.android.car.settings.common.Logger;
35 import com.android.car.settings.common.PreferenceController;
36 
37 /**
38  * Base class for controllers in the enterprise module.
39  */
40 abstract class BaseEnterprisePreferenceController<P extends Preference>
41         extends PreferenceController<P> {
42 
43     protected final Logger mLogger = new Logger(getClass());
44 
45     protected final DevicePolicyManager mDpm;
46     protected final PackageManager mPm;
47     protected final UserManager mUm;
48 
49     private final boolean mHasFeature;
50 
BaseEnterprisePreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)51     protected BaseEnterprisePreferenceController(Context context, String preferenceKey,
52             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
53         super(context, preferenceKey, fragmentController, uxRestrictions);
54 
55         mHasFeature = context.getPackageManager()
56                 .hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN);
57 
58         mDpm = context.getSystemService(DevicePolicyManager.class);
59         mPm = context.getPackageManager();
60         mUm = context.getSystemService(UserManager.class);
61     }
62 
63     @Override
getPreferenceType()64     protected Class<P> getPreferenceType() {
65         return (Class<P>) Preference.class;
66     }
67 
68     @Override
getDefaultAvailabilityStatus()69     protected int getDefaultAvailabilityStatus() {
70         return mHasFeature ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
71     }
72 
isProfileOrDeviceOwner(ComponentName admin)73     final boolean isProfileOrDeviceOwner(ComponentName admin) {
74         return admin.equals(mDpm.getProfileOwner())
75                 || admin.equals(mDpm.getDeviceOwnerComponentOnCallingUser());
76     }
77 
78     @Nullable
getDescription(DeviceAdminInfo deviceAdminInfo)79     protected final CharSequence getDescription(DeviceAdminInfo deviceAdminInfo) {
80         try {
81             return deviceAdminInfo.loadDescription(mPm);
82         } catch (Resources.NotFoundException e) {
83             mLogger.v("No description for "
84                     + deviceAdminInfo.getComponent().flattenToShortString());
85         }
86         return null;
87     }
88 
89     /**
90      * Returns whether the device is in COMP mode (primary user managed by a Device Owner app and
91      * work profile managed by a Profile Owner app).
92      */
isInCompMode()93     protected boolean isInCompMode() {
94         return EnterpriseUtils.hasDeviceOwner(getContext())
95                 && getManagedProfileUserId() != UserHandle.USER_NULL;
96     }
97 
getManagedProfileUserId()98     private int getManagedProfileUserId() {
99         UserInfo userInfo = getManagedProfileUserInfo();
100         if (userInfo != null) {
101             return userInfo.id;
102         }
103         return UserHandle.USER_NULL;
104     }
105 
getManagedProfileUserInfo()106     private UserInfo getManagedProfileUserInfo() {
107         for (UserInfo userInfo : mUm.getProfiles(UserHandle.myUserId())) {
108             if (userInfo.isManagedProfile()) {
109                 return userInfo;
110             }
111         }
112         return null;
113     }
114 }
115