• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.MutableLiveData;
23 import androidx.lifecycle.ViewModel;
24 
25 import com.android.devicelockcontroller.R;
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 import java.util.Arrays;
34 import java.util.List;
35 
36 /**
37  * ViewModel class which provides data for the {@link DeviceInfoSettingsFragment}.
38  */
39 public final class DeviceInfoSettingsViewModel extends ViewModel {
40 
41     private static final String TAG = "DeviceInfoSettingsViewModel";
42 
43     // Preferences that need to be updated with provider name
44     private static final List<Pair<Integer, Integer>> PREFERENCE_KEY_TITLE_PAIRS = Arrays.asList(
45             new Pair<>(R.string.settings_intro_preference_key,
46                     R.string.settings_intro),
47             new Pair<>(R.string.settings_credit_provider_capabilities_category_preference_key,
48                     R.string.settings_credit_provider_capabilities_category),
49             new Pair<>(R.string.settings_allowlisted_apps_preference_key,
50                     R.string.settings_allowlisted_apps),
51             new Pair<>(R.string.settings_restrictions_removed_preference_key,
52                     R.string.settings_restrictions_removed),
53             new Pair<>(R.string.settings_uninstall_kiosk_app_preference_key,
54                     R.string.settings_uninstall_kiosk_app));
55 
56     final List<Pair<Integer, Integer>> mPreferenceKeyTitlePairs;
57 
58     final MutableLiveData<String> mProviderNameLiveData;
59 
DeviceInfoSettingsViewModel()60     public DeviceInfoSettingsViewModel() {
61         mPreferenceKeyTitlePairs = PREFERENCE_KEY_TITLE_PAIRS;
62         mProviderNameLiveData = new MutableLiveData<>();
63 
64         Futures.addCallback(
65                 SetupParametersClient.getInstance().getKioskAppProviderName(),
66                 new FutureCallback<>() {
67                     @Override
68                     public void onSuccess(String providerName) {
69                         if (TextUtils.isEmpty(providerName)) {
70                             LogUtil.e(TAG, "Device provider name is empty, should not reach here.");
71                             return;
72                         }
73                         mProviderNameLiveData.postValue(providerName);
74                     }
75 
76                     @Override
77                     public void onFailure(Throwable t) {
78                         LogUtil.e(TAG, "Failed to get Kiosk app provider name", t);
79                     }
80                 }, MoreExecutors.directExecutor());
81     }
82 
83 
84 }
85