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.devicelockcontroller.activities;
18 
19 import androidx.lifecycle.LiveData;
20 import androidx.lifecycle.MediatorLiveData;
21 import androidx.lifecycle.MutableLiveData;
22 import androidx.lifecycle.ViewModel;
23 
24 import com.android.devicelockcontroller.R;
25 import com.android.devicelockcontroller.storage.SetupParametersClient;
26 import com.android.devicelockcontroller.util.LogUtil;
27 
28 import com.google.common.util.concurrent.FutureCallback;
29 import com.google.common.util.concurrent.Futures;
30 import com.google.common.util.concurrent.MoreExecutors;
31 
32 import java.util.Arrays;
33 import java.util.List;
34 
35 /**
36  * This class provides resources and data used to display the polices the device provider enforces
37  * on this device.
38  */
39 public final class DevicePoliciesViewModel extends ViewModel {
40 
41     static final int HEADER_DRAWABLE_ID = R.drawable.ic_info_24px;
42 
43     static final int HEADER_TEXT_ID = R.string.setup_info_title_text;
44 
45     private static final DevicePolicyGroup CONTROL_POLICY_GROUP =
46             new DevicePolicyGroup.Builder()
47                     .setTitleTextId(R.string.control_section_title)
48                     .addDevicePolicy(R.drawable.ic_lock_outline_24px,
49                             R.string.control_lock_device_text)
50                     .addDevicePolicy(R.drawable.ic_file_download_24px,
51                             R.string.control_download_text)
52                     .addDevicePolicy(R.drawable.ic_bug_report_24px,
53                             R.string.control_disable_debug_text)
54                     .build();
55 
56     private static final DevicePolicyGroup LOCK_POLICY_GROUP =
57             new DevicePolicyGroup.Builder()
58                     .setTitleTextId(R.string.locked_section_title)
59                     .addDevicePolicy(R.drawable.ic_local_hospital_24px,
60                             R.string.locked_emergency_text)
61                     .addDevicePolicy(R.drawable.ic_phone_callback_outlined_24px,
62                             R.string.locked_phone_usage_text)
63                     .addDevicePolicy(R.drawable.ic_settings_applications_24px,
64                             R.string.locked_settings_usage_text)
65                     .addDevicePolicy(R.drawable.ic_settings_backup_restore_24px,
66                             R.string.locked_backup_and_restore_text)
67                     .build();
68 
69     private static final DevicePolicyGroup EXPOSURE_POLICY_GROUP =
70             new DevicePolicyGroup.Builder()
71                     .setTitleTextId(R.string.exposure_section_title)
72                     .addDevicePolicy(R.drawable.ic_delete_24px,
73                             R.string.exposure_install_text)
74                     .addDevicePolicy(R.drawable.ic_lock_open_24px,
75                             R.string.exposure_lock_unlock_text)
76                     .addDevicePolicy(R.drawable.ic_block_24px,
77                             R.string.exposure_disable_dlc_text)
78                     .build();
79 
80     private static final List<DevicePolicyGroup> DEVICE_POLICY_GROUPS = Arrays.asList(
81             CONTROL_POLICY_GROUP, LOCK_POLICY_GROUP, EXPOSURE_POLICY_GROUP);
82     public static final String TAG = "DevicePoliciesViewModel";
83 
84     final MutableLiveData<String> mProviderNameLiveData;
85     final MediatorLiveData<List<DevicePolicyGroup>> mDevicePolicyGroupListLiveData;
86 
getIsMandatoryLiveData()87     public LiveData<Boolean> getIsMandatoryLiveData() {
88         return mIsMandatoryLiveData;
89     }
90 
91     private final MutableLiveData<Boolean> mIsMandatoryLiveData = new MutableLiveData<>();
92 
DevicePoliciesViewModel()93     public DevicePoliciesViewModel() {
94         mProviderNameLiveData = new MutableLiveData<>();
95         Futures.addCallback(SetupParametersClient.getInstance().getKioskAppProviderName(),
96                 new FutureCallback<>() {
97                     @Override
98                     public void onSuccess(String result) {
99                         mProviderNameLiveData.postValue(result);
100                     }
101 
102                     @Override
103                     public void onFailure(Throwable t) {
104                         LogUtil.e(TAG, "Failed to get Device Provider name!", t);
105                     }
106                 }, MoreExecutors.directExecutor());
107 
108         Futures.addCallback(SetupParametersClient.getInstance().isProvisionMandatory(),
109                 new FutureCallback<>() {
110                     @Override
111                     public void onSuccess(Boolean result) {
112                         mIsMandatoryLiveData.postValue(result);
113                     }
114 
115                     @Override
116                     public void onFailure(Throwable t) {
117                         LogUtil.e(TAG, "Failed to know if provision is mandatory!", t);
118                     }
119                 }, MoreExecutors.directExecutor());
120         mDevicePolicyGroupListLiveData = new MediatorLiveData<>();
121         mDevicePolicyGroupListLiveData.addSource(mProviderNameLiveData,
122                 unused -> mDevicePolicyGroupListLiveData.setValue(DEVICE_POLICY_GROUPS));
123     }
124 }
125