1 /*
2  * Copyright (C) 2023 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.settings.devicelock;
18 
19 import android.content.Context;
20 import android.devicelock.DeviceLockManager;
21 import android.util.Log;
22 
23 import androidx.preference.Preference;
24 
25 import com.android.settings.core.BasePreferenceController;
26 
27 /**
28  * The PreferenceController that manages the Device Lock entry preference.
29  */
30 public final class DeviceLockPreferenceController extends BasePreferenceController {
31 
32     private static final String TAG = "DeviceLockPreferenceController";
33 
34     private final DeviceLockManager mDeviceLockManager;
35 
DeviceLockPreferenceController(Context context, String preferenceKey)36     public DeviceLockPreferenceController(Context context, String preferenceKey) {
37         super(context, preferenceKey);
38         mDeviceLockManager = mContext.getSystemService(DeviceLockManager.class);
39     }
40 
41     @Override
getAvailabilityStatus()42     public int getAvailabilityStatus() {
43         // TODO(b/282856378): make this entry searchable
44         return AVAILABLE_UNSEARCHABLE;
45     }
46 
47     @Override
updateState(Preference preference)48     public void updateState(Preference preference) {
49         super.updateState(preference);
50         if (mDeviceLockManager == null) {
51             Log.w(TAG, "DeviceLockManager is not available");
52             preference.setVisible(false);
53             return;
54         }
55         mDeviceLockManager.getKioskApps(mContext.getMainExecutor(),
56                 result -> {
57                     // if kiosk apps present on the device, the device is provisioned by Device Lock
58                     boolean isDeviceProvisionedByDeviceLock = result != null && !result.isEmpty();
59                     Log.d(TAG, "Set preference visibility to " + isDeviceProvisionedByDeviceLock);
60                     // TODO(b/282179089): find alternatives instead of calling setVisible
61                     preference.setVisible(isDeviceProvisionedByDeviceLock);
62                 });
63     }
64 }
65