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.util.Pair;
20 
21 import androidx.lifecycle.MutableLiveData;
22 import androidx.lifecycle.ViewModel;
23 
24 import com.android.devicelockcontroller.R;
25 import com.android.devicelockcontroller.storage.GlobalParametersClient;
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.ListenableFuture;
32 import com.google.common.util.concurrent.MoreExecutors;
33 
34 import java.util.List;
35 
36 /**
37  * This class provides the resources and {@link ProvisionInfo} to render the
38  * {@link ProvisionInfoFragment}.
39  */
40 public abstract class ProvisionInfoViewModel extends ViewModel {
41 
42     private static final String TAG = "ProvisionInfoViewModel";
43     static final int HEADER_DRAWABLE_ID = R.drawable.ic_info_24px;
44     static final int HEADER_TEXT_ID = R.string.enroll_your_device_header;
45     int mMandatoryHeaderTextId;
46     int mSubHeaderTextId;
47     List<ProvisionInfo> mProvisionInfoList;
48     final MutableLiveData<Boolean> mIsMandatoryLiveData = new MutableLiveData<>();
49     final MutableLiveData<Boolean> mIsProvisionForcedLiveData = new MutableLiveData<>();
50     final MutableLiveData<Pair<Integer, String>> mHeaderTextLiveData = new MutableLiveData<>();
51     final MutableLiveData<Pair<Integer, String>> mSubHeaderTextLiveData = new MutableLiveData<>();
52     final MutableLiveData<List<ProvisionInfo>> mProvisionInfoListLiveData = new MutableLiveData<>();
53 
retrieveData()54     void retrieveData() {
55         SetupParametersClient setupParametersClient = SetupParametersClient.getInstance();
56         ListenableFuture<String> kioskAppProviderNameFuture =
57                 setupParametersClient.getKioskAppProviderName();
58         ListenableFuture<Boolean> isMandatoryFuture = setupParametersClient.isProvisionMandatory();
59         ListenableFuture<String> termsAndConditionsUrlFuture =
60                 setupParametersClient.getTermsAndConditionsUrl();
61         ListenableFuture<String> supportUrlFuture = setupParametersClient.getSupportUrl();
62         ListenableFuture<Boolean> isProvisionForcedFuture =
63                 GlobalParametersClient.getInstance().isProvisionForced();
64         ListenableFuture<Void> result = Futures.whenAllSucceed(isMandatoryFuture,
65                         kioskAppProviderNameFuture, termsAndConditionsUrlFuture, supportUrlFuture,
66                         isProvisionForcedFuture)
67                 .call(() -> {
68                     boolean isMandatory = Futures.getDone(isMandatoryFuture);
69                     String providerName = Futures.getDone(kioskAppProviderNameFuture);
70                     String termsAndConditionUrl = Futures.getDone(termsAndConditionsUrlFuture);
71                     String supportUrl = Futures.getDone(supportUrlFuture);
72                     for (int i = 0, size = mProvisionInfoList.size(); i < size; ++i) {
73                         ProvisionInfo provisionInfo = mProvisionInfoList.get(i);
74                         switch (provisionInfo.getType()) {
75                             case ProvisionInfo.ProvisionInfoType.REGULAR -> provisionInfo.setUrl(
76                                     "");
77                             case ProvisionInfo.ProvisionInfoType.TERMS_AND_CONDITIONS ->
78                                     provisionInfo.setUrl(termsAndConditionUrl);
79                             case ProvisionInfo.ProvisionInfoType.SUPPORT -> provisionInfo.setUrl(
80                                     supportUrl);
81                             default -> throw new IllegalStateException(
82                                     "Unexpected value: " + provisionInfo.getType());
83                         }
84                         provisionInfo.setProviderName(providerName);
85                     }
86                     mProvisionInfoListLiveData.postValue(mProvisionInfoList);
87                     mIsMandatoryLiveData.postValue(isMandatory);
88                     mHeaderTextLiveData.postValue(
89                             new Pair<>(isMandatory ? mMandatoryHeaderTextId : HEADER_TEXT_ID,
90                                     providerName));
91                     mSubHeaderTextLiveData.postValue(
92                             new Pair<>(isMandatory ? 0 : mSubHeaderTextId,
93                                     providerName));
94                     mIsProvisionForcedLiveData.postValue(Futures.getDone(isProvisionForcedFuture));
95                     return null;
96                 }, MoreExecutors.directExecutor());
97         Futures.addCallback(result, new FutureCallback<>() {
98             @Override
99             public void onSuccess(Void result) {
100                 LogUtil.i(TAG, "Successfully updated live data");
101             }
102 
103             @Override
104             public void onFailure(Throwable t) {
105                 throw new RuntimeException(t);
106             }
107         }, MoreExecutors.directExecutor());
108     }
109 }
110