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 android.text.TextUtils; 20 import android.util.Pair; 21 22 import androidx.lifecycle.MediatorLiveData; 23 import androidx.lifecycle.MutableLiveData; 24 import androidx.lifecycle.ViewModel; 25 26 import com.android.devicelockcontroller.storage.SetupParametersClient; 27 import com.android.devicelockcontroller.util.LogUtil; 28 29 import com.google.common.util.concurrent.FutureCallback; 30 import com.google.common.util.concurrent.Futures; 31 import com.google.common.util.concurrent.MoreExecutors; 32 33 /** 34 * Generic abstract ViewModel for device enrollment info data. 35 */ 36 abstract class DeviceEnrollmentInfoViewModel extends ViewModel { 37 38 private static final String TAG = "DeviceEnrollmentInfoViewModel"; 39 private static final String PROVIDER_NAME_PLACEHOLDER = ""; 40 private static final int TEXT_ID_PLACEHOLDER = -1; 41 final MutableLiveData<Integer> mHeaderDrawableIdLiveData; 42 final MutableLiveData<Integer> mHeaderTextIdLiveData; 43 final MutableLiveData<String> mProviderNameLiveData; 44 final MediatorLiveData<Pair<Integer, String>> mHeaderTextLiveData; 45 final MutableLiveData<Integer> mBodyTextIdLiveData; 46 final MediatorLiveData<Pair<Integer, String>> mBodyTextLiveData; 47 DeviceEnrollmentInfoViewModel()48 DeviceEnrollmentInfoViewModel() { 49 mHeaderDrawableIdLiveData = new MutableLiveData<>(); 50 mHeaderTextIdLiveData = new MutableLiveData<>(); 51 52 mProviderNameLiveData = new MutableLiveData<>(); 53 mHeaderTextLiveData = new MediatorLiveData<>(); 54 mHeaderTextLiveData.addSource(mHeaderTextIdLiveData, 55 id -> { 56 Pair<Integer, String> oldValue = mHeaderTextLiveData.getValue(); 57 mHeaderTextLiveData.setValue(oldValue == null 58 ? new Pair<>(id, PROVIDER_NAME_PLACEHOLDER) 59 : new Pair<>(id, oldValue.second)); 60 }); 61 mHeaderTextLiveData.addSource(mProviderNameLiveData, 62 providerName -> { 63 Pair<Integer, String> oldValue = mHeaderTextLiveData.getValue(); 64 mHeaderTextLiveData.setValue(oldValue == null 65 ? new Pair<>(TEXT_ID_PLACEHOLDER, providerName) 66 : new Pair<>(oldValue.first, providerName)); 67 68 }); 69 mBodyTextIdLiveData = new MutableLiveData<>(); 70 mBodyTextLiveData = new MediatorLiveData<>(); 71 mBodyTextLiveData.addSource(mBodyTextIdLiveData, 72 id -> { 73 Pair<Integer, String> oldValue = mBodyTextLiveData.getValue(); 74 mBodyTextLiveData.setValue(oldValue == null 75 ? new Pair<>(id, PROVIDER_NAME_PLACEHOLDER) 76 : new Pair<>(id, oldValue.second)); 77 }); 78 mBodyTextLiveData.addSource(mProviderNameLiveData, 79 providerName -> { 80 Pair<Integer, String> oldValue = mBodyTextLiveData.getValue(); 81 mBodyTextLiveData.setValue(oldValue == null 82 ? new Pair<>(TEXT_ID_PLACEHOLDER, providerName) 83 : new Pair<>(oldValue.first, providerName)); 84 }); 85 Futures.addCallback(SetupParametersClient.getInstance().getKioskAppProviderName(), 86 new FutureCallback<>() { 87 @Override 88 public void onSuccess(String providerName) { 89 if (TextUtils.isEmpty(providerName)) { 90 LogUtil.e(TAG, "Device provider name is empty, should not reach here."); 91 return; 92 } 93 mProviderNameLiveData.postValue(providerName); 94 } 95 96 @Override 97 public void onFailure(Throwable t) { 98 LogUtil.e(TAG, "Failed to get Kiosk app provider name", t); 99 } 100 }, MoreExecutors.directExecutor()); 101 } 102 } 103