• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.os.UserManager;
26 
27 import androidx.preference.Preference;
28 
29 import com.android.car.settings.common.FragmentController;
30 import com.android.car.settings.common.Logger;
31 import com.android.car.settings.common.PreferenceController;
32 
33 import java.util.Objects;
34 
35 /**
36  * Base class for controllers in the device admin details screen.
37  */
38 abstract class BaseDeviceAdminAddPreferenceController<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     protected DeviceAdminInfo mDeviceAdminInfo;
48 
BaseDeviceAdminAddPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)49     protected BaseDeviceAdminAddPreferenceController(Context context, String preferenceKey,
50             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
51         super(context, preferenceKey, fragmentController, uxRestrictions);
52 
53         mDpm = context.getSystemService(DevicePolicyManager.class);
54         mPm = context.getPackageManager();
55         mUm = context.getSystemService(UserManager.class);
56     }
57 
58     @Override
getPreferenceType()59     protected Class<P> getPreferenceType() {
60         return (Class<P>) Preference.class;
61     }
62 
63     @Override
getAvailabilityStatus()64     protected final int getAvailabilityStatus() {
65         return mDeviceAdminInfo == null ? CONDITIONALLY_UNAVAILABLE : getRealAvailabilityStatus();
66     }
67 
68     /**
69      * Method that can be overridden to define the real value of {@link #getAvailabilityStatus()}
70      * when the {@link #setDeviceAdmin(DeviceAdminInfo) DeviceAdminInfo} is set.
71      */
getRealAvailabilityStatus()72     protected int getRealAvailabilityStatus() {
73         return AVAILABLE;
74     }
75 
setDeviceAdmin( DeviceAdminInfo deviceAdminInfo)76     final <T extends BaseDeviceAdminAddPreferenceController<P>> T setDeviceAdmin(
77             DeviceAdminInfo deviceAdminInfo) {
78         mDeviceAdminInfo = Objects.requireNonNull(deviceAdminInfo);
79         @SuppressWarnings("unchecked")
80         T safeCast = (T) this;
81         return safeCast;
82     }
83 
isProfileOrDeviceOwner()84     final boolean isProfileOrDeviceOwner() {
85         return isProfileOrDeviceOwner(mDeviceAdminInfo.getComponent());
86     }
87 
isProfileOrDeviceOwner(ComponentName admin)88     final boolean isProfileOrDeviceOwner(ComponentName admin) {
89         return admin.equals(mDpm.getProfileOwner())
90                 || admin.equals(mDpm.getDeviceOwnerComponentOnCallingUser());
91     }
92 }
93