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.google.common.base.Preconditions.checkNotNull;
20 
21 import android.os.Bundle;
22 import android.util.Pair;
23 import android.view.View;
24 
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.lifecycle.LifecycleOwner;
28 import androidx.lifecycle.ViewModelProvider;
29 import androidx.preference.Preference;
30 import androidx.preference.PreferenceFragmentCompat;
31 import androidx.preference.PreferenceGroup;
32 import androidx.preference.PreferenceManager;
33 
34 import com.android.devicelockcontroller.R;
35 
36 /**
37  * The screen which provides info about Device Lock in Settings' style.
38  */
39 public final class DeviceInfoSettingsFragment extends PreferenceFragmentCompat {
40 
41     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)42     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
43         setPreferencesFromResource(R.xml.device_info_settings, rootKey);
44     }
45 
46     @Override
onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)47     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
48         super.onViewCreated(view, savedInstanceState);
49 
50         DeviceInfoSettingsViewModel viewModel = new ViewModelProvider(this).get(
51                 DeviceInfoSettingsViewModel.class);
52         PreferenceManager preferenceManager = getPreferenceManager();
53         LifecycleOwner lifecycleOwner = getViewLifecycleOwner();
54         viewModel.getProviderNameLiveData().observe(lifecycleOwner, providerName -> {
55             requireActivity().setTitle(
56                     getString(R.string.device_provided_by_provider, providerName));
57 
58             hideIconView(preferenceManager.getPreferenceScreen());
59             for (Pair<Integer, Integer> keyTitlePair :
60                     viewModel.getPreferenceWithProviderNameKeyTitlePairs()) {
61                 Preference preference = preferenceManager.findPreference(
62                         getString(keyTitlePair.first));
63                 checkNotNull(preference);
64                 preference.setTitle(getString(keyTitlePair.second, providerName));
65             }
66             Preference supportUrlPreference = preferenceManager.findPreference(
67                     getString(R.string.settings_contact_provider_preference_key));
68             checkNotNull(supportUrlPreference);
69             supportUrlPreference.setTitle(
70                     String.format(getString(R.string.settings_contact_provider), providerName,
71                             viewModel.getSupportUrl()));
72         });
73         viewModel.getInstallFromUnknownSourcesDisallowedLiveData().observe(lifecycleOwner,
74                 disallowed -> {
75                     Preference preference = preferenceManager.findPreference(
76                             getString(R.string.settings_install_apps_preference_key));
77                     checkNotNull(preference);
78                     preference.setVisible(disallowed);
79                 });
80     }
81 
82     /**
83      * Hide the unused icon view of the given {@code preference} and its child preference if any.
84      */
hideIconView(Preference preference)85     private static void hideIconView(Preference preference) {
86         preference.setIconSpaceReserved(false);
87         if (preference instanceof PreferenceGroup preferenceGroup) {
88             for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
89                 hideIconView(preferenceGroup.getPreference(i));
90             }
91         }
92     }
93 }
94