• 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.car.drivingstate.CarUxRestrictions;
20 import android.content.ComponentName;
21 import android.content.Context;
22 
23 import androidx.annotation.Nullable;
24 import androidx.annotation.VisibleForTesting;
25 import androidx.preference.Preference;
26 
27 import com.android.car.settings.R;
28 import com.android.car.settings.common.FragmentController;
29 
30 /**
31  * Controller for the action (activate / deactivate).
32  */
33 public final class DeviceAdminAddActionPreferenceController
34         extends BaseDeviceAdminAddPreferenceController<Preference> {
35 
36     /*
37      * 3-state status for button:
38      *
39      *  - null: button disabled
40      *  - true: admin active, button deactivates
41      *  - false: admin inactive, button activates
42      */
43     @Nullable
44     private Boolean mIsActive;
45 
DeviceAdminAddActionPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)46     public DeviceAdminAddActionPreferenceController(Context context, String preferenceKey,
47             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
48         super(context, preferenceKey, fragmentController, uxRestrictions);
49     }
50 
51     @Override
updateState(Preference preference)52     protected void updateState(Preference preference) {
53         setIsActive();
54 
55         preference.setEnabled(mIsActive != null);
56         preference.setTitle(mIsActive == null || mIsActive
57                 ? R.string.remove_device_admin
58                 : R.string.add_device_admin);
59     }
60 
61     @Override
handlePreferenceClicked(Preference preference)62     protected boolean handlePreferenceClicked(Preference preference) {
63         if (mIsActive == null) {
64             mLogger.wtf("handlePreferenceClicked() called when admin is a profile / device owner");
65         } else {
66             ComponentName admin = mDeviceAdminInfo.getComponent();
67             if (mIsActive) {
68                 mLogger.i("Deactivating " + admin.flattenToShortString());
69                 mDpm.removeActiveAdmin(admin);
70             } else {
71                 mLogger.i("Activating " + admin.flattenToShortString());
72                 // TODO(b/192372143): support refreshing
73                 mDpm.setActiveAdmin(admin, /* refreshing= */ false);
74             }
75         }
76         getFragmentController().goBack();
77         return true;
78     }
79 
80     @VisibleForTesting
setIsActive()81     void setIsActive() {
82         ComponentName admin = mDeviceAdminInfo.getComponent();
83         if (isProfileOrDeviceOwner(admin)) {
84             // TODO(b/170332519): once work profiles are supported, they could be removed
85             mLogger.d("updateState(): " + admin.toShortString() + " is PO or DO");
86             mIsActive = null;
87         } else {
88             mIsActive = mDpm.isAdminActive(admin);
89         }
90 
91         mLogger.d("updateState(): active = " + mIsActive);
92     }
93 }
94