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.Preference;
24 
25 import com.android.car.settings.common.FragmentController;
26 
27 import java.util.Objects;
28 
29 /**
30  * Base class for controllers in the device admin details screen.
31  */
32 abstract class BaseDeviceAdminAddPreferenceController<P extends Preference>
33         extends BaseEnterprisePreferenceController<P> {
34 
35     protected DeviceAdminInfo mDeviceAdminInfo;
36 
BaseDeviceAdminAddPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)37     protected BaseDeviceAdminAddPreferenceController(Context context, String preferenceKey,
38             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
39         super(context, preferenceKey, fragmentController, uxRestrictions);
40     }
41 
42     @Override
getDefaultAvailabilityStatus()43     protected int getDefaultAvailabilityStatus() {
44         int superStatus = super.getDefaultAvailabilityStatus();
45         if (superStatus != AVAILABLE) return superStatus;
46 
47         return mDeviceAdminInfo == null ? CONDITIONALLY_UNAVAILABLE : AVAILABLE;
48     }
49 
setDeviceAdmin( DeviceAdminInfo deviceAdminInfo)50     final <T extends BaseDeviceAdminAddPreferenceController<P>> T setDeviceAdmin(
51             DeviceAdminInfo deviceAdminInfo) {
52         mDeviceAdminInfo = Objects.requireNonNull(deviceAdminInfo);
53         @SuppressWarnings("unchecked")
54         T safeCast = (T) this;
55         return safeCast;
56     }
57 
isProfileOrDeviceOwner()58     final boolean isProfileOrDeviceOwner() {
59         return mDeviceAdminInfo == null
60                 ? false : isProfileOrDeviceOwner(mDeviceAdminInfo.getComponent());
61     }
62 }
63