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