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 static com.android.devicelockcontroller.common.DeviceLockConstants.ProvisioningType.TYPE_FINANCED; 20 import static com.android.devicelockcontroller.common.DeviceLockConstants.ProvisioningType.TYPE_SUBSIDY; 21 22 import android.util.Pair; 23 24 import androidx.lifecycle.MutableLiveData; 25 import androidx.lifecycle.ViewModel; 26 27 import com.android.devicelockcontroller.R; 28 import com.android.devicelockcontroller.storage.SetupParametersClient; 29 import com.android.devicelockcontroller.util.LogUtil; 30 31 import com.google.common.util.concurrent.FutureCallback; 32 import com.google.common.util.concurrent.Futures; 33 import com.google.common.util.concurrent.ListenableFuture; 34 import com.google.common.util.concurrent.MoreExecutors; 35 36 import java.util.ArrayList; 37 import java.util.Arrays; 38 import java.util.List; 39 40 /** 41 * ViewModel class which provides data for the {@link DeviceInfoSettingsFragment}. 42 */ 43 public final class DeviceInfoSettingsViewModel extends ViewModel { 44 45 private static final String TAG = "DeviceInfoSettingsViewModel"; 46 private final List<Pair<Integer, Integer>> mPreferenceWithProviderNameKeyTitlePairs = 47 new ArrayList<>(Arrays.asList( 48 new Pair<>( 49 R.string.settings_credit_provider_capabilities_category_preference_key, 50 R.string.settings_credit_provider_capabilities_category), 51 new Pair<>(R.string.settings_allowlisted_apps_preference_key, 52 R.string.settings_allowlisted_apps), 53 new Pair<>(R.string.settings_restrictions_removed_preference_key, 54 R.string.settings_restrictions_removed), 55 new Pair<>(R.string.settings_uninstall_kiosk_app_preference_key, 56 R.string.settings_uninstall_kiosk_app))); 57 58 private String mSupportUrl; 59 private final MutableLiveData<String> mProviderNameLiveData = new MutableLiveData<>(); 60 private final MutableLiveData<Boolean> mInstallFromUnknownSourcesDisallowedLiveData = 61 new MutableLiveData<>(); 62 DeviceInfoSettingsViewModel()63 public DeviceInfoSettingsViewModel() { 64 65 SetupParametersClient setupParametersClient = SetupParametersClient.getInstance(); 66 ListenableFuture<Integer> getProvisioningTypeFuture = 67 setupParametersClient.getProvisioningType(); 68 ListenableFuture<String> getKioskAppProviderNameFuture = 69 setupParametersClient.getKioskAppProviderName(); 70 ListenableFuture<Boolean> isInstallFromUnknownSourcesDisallowedFuture = 71 setupParametersClient.isInstallingFromUnknownSourcesDisallowed(); 72 ListenableFuture<String> getSupportUrlFuture = setupParametersClient.getSupportUrl(); 73 Futures.addCallback(isInstallFromUnknownSourcesDisallowedFuture, 74 new FutureCallback<>() { 75 @Override 76 public void onSuccess(Boolean result) { 77 mInstallFromUnknownSourcesDisallowedLiveData.postValue(result); 78 } 79 80 @Override 81 public void onFailure(Throwable t) { 82 LogUtil.e(TAG, 83 "Failed to get isInstallingFromUnknownSourcesDisallowed from " 84 + "setup parameters", 85 t); 86 } 87 }, MoreExecutors.directExecutor()); 88 89 Futures.addCallback( 90 Futures.whenAllSucceed(getProvisioningTypeFuture, getKioskAppProviderNameFuture, 91 getSupportUrlFuture) 92 .call(() -> { 93 Integer provisioningType = Futures.getDone( 94 getProvisioningTypeFuture); 95 switch (provisioningType) { 96 case TYPE_FINANCED: 97 mPreferenceWithProviderNameKeyTitlePairs.add( 98 new Pair<>( 99 R.string.settings_intro_preference_key, 100 R.string.settings_intro_device_financing)); 101 break; 102 case TYPE_SUBSIDY: 103 mPreferenceWithProviderNameKeyTitlePairs.add( 104 new Pair<>( 105 R.string.settings_intro_preference_key, 106 R.string.settings_intro_device_subsidy)); 107 break; 108 default: 109 throw new IllegalStateException( 110 "Invalid provisioning type"); 111 } 112 mSupportUrl = Futures.getDone(getSupportUrlFuture); 113 mProviderNameLiveData.postValue( 114 Futures.getDone(getKioskAppProviderNameFuture)); 115 return null; 116 }, MoreExecutors.directExecutor()), 117 new FutureCallback<Void>() { 118 @Override 119 public void onSuccess(Void result) { 120 LogUtil.d(TAG, "Successfully retrieved setup parameters"); 121 } 122 123 @Override 124 public void onFailure(Throwable t) { 125 LogUtil.e(TAG, "Failed to retrieve setup parameters", t); 126 } 127 }, MoreExecutors.directExecutor()); 128 } 129 getProviderNameLiveData()130 public MutableLiveData<String> getProviderNameLiveData() { 131 return mProviderNameLiveData; 132 } 133 getInstallFromUnknownSourcesDisallowedLiveData()134 public MutableLiveData<Boolean> getInstallFromUnknownSourcesDisallowedLiveData() { 135 return mInstallFromUnknownSourcesDisallowedLiveData; 136 } 137 getSupportUrl()138 public String getSupportUrl() { 139 return mSupportUrl; 140 } 141 getPreferenceWithProviderNameKeyTitlePairs()142 public List<Pair<Integer, Integer>> getPreferenceWithProviderNameKeyTitlePairs() { 143 return mPreferenceWithProviderNameKeyTitlePairs; 144 } 145 } 146