• 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 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.ViewModelProvider;
28 import androidx.preference.Preference;
29 import androidx.preference.PreferenceFragmentCompat;
30 import androidx.preference.PreferenceGroup;
31 import androidx.preference.PreferenceManager;
32 
33 import com.android.devicelockcontroller.R;
34 
35 /**
36  * The screen which provides info about Device Lock in Settings' style.
37  */
38 public final class DeviceInfoSettingsFragment extends PreferenceFragmentCompat {
39 
40     @Override
onCreatePreferences(Bundle savedInstanceState, String rootKey)41     public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
42         setPreferencesFromResource(R.xml.device_info_settings, rootKey);
43     }
44 
45     @Override
onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)46     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
47         super.onViewCreated(view, savedInstanceState);
48         requireActivity().setTitle(getString(R.string.settings_screen_title));
49 
50         DeviceInfoSettingsViewModel viewModel = new ViewModelProvider(this).get(
51                 DeviceInfoSettingsViewModel.class);
52         viewModel.mProviderNameLiveData.observe(getViewLifecycleOwner(), providerName -> {
53             PreferenceManager preferenceManager = getPreferenceManager();
54             hideIconView(preferenceManager.getPreferenceScreen());
55             for (Pair<Integer, Integer> keyTitlePair : viewModel.mPreferenceKeyTitlePairs) {
56                 Preference preference = preferenceManager.findPreference(
57                         getString(keyTitlePair.first));
58                 checkNotNull(preference);
59                 preference.setTitle(getString(keyTitlePair.second, providerName));
60             }
61         });
62     }
63 
64     /**
65      * Hide the unused icon view of the given {@code preference} and its child preference if any.
66      */
hideIconView(Preference preference)67     private static void hideIconView(Preference preference) {
68         preference.setIconSpaceReserved(false);
69         if (preference instanceof PreferenceGroup) {
70             PreferenceGroup preferenceGroup = (PreferenceGroup) preference;
71             for (int i = 0; i < preferenceGroup.getPreferenceCount(); i++) {
72                 hideIconView(preferenceGroup.getPreference(i));
73             }
74         }
75     }
76 }
77