• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.car.settings.applications.managedomainurls;
18 
19 import static android.content.pm.verify.domain.DomainVerificationUserState.DOMAIN_STATE_SELECTED;
20 
21 import android.car.drivingstate.CarUxRestrictions;
22 import android.content.Context;
23 
24 import com.android.car.settings.R;
25 import com.android.car.settings.common.FragmentController;
26 import com.android.car.settings.common.Logger;
27 import com.android.car.ui.preference.CarUiMultiSelectListPreference;
28 import com.android.settingslib.utils.StringUtil;
29 
30 import java.util.Arrays;
31 import java.util.HashSet;
32 import java.util.List;
33 import java.util.Set;
34 import java.util.stream.Collectors;
35 
36 /**
37  * Controls the SelectedLinksList preference.
38  */
39 public class SelectedLinksListPreferenceController extends
40         AppLaunchSettingsBasePreferenceController<CarUiMultiSelectListPreference> {
41     private static final Logger LOG = new Logger(SelectedLinksListPreferenceController.class);
42 
SelectedLinksListPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)43     public SelectedLinksListPreferenceController(Context context, String preferenceKey,
44             FragmentController fragmentController, CarUxRestrictions uxRestrictions) {
45         super(context, preferenceKey, fragmentController, uxRestrictions);
46     }
47 
48     @Override
getPreferenceType()49     protected Class<CarUiMultiSelectListPreference> getPreferenceType() {
50         return CarUiMultiSelectListPreference.class;
51     }
52 
53     @Override
getDefaultAvailabilityStatus()54     protected int getDefaultAvailabilityStatus() {
55         if (isLinkHandlingEnabled() && getLinksNumber(DOMAIN_STATE_SELECTED) > 0) {
56             return AVAILABLE;
57         }
58         return CONDITIONALLY_UNAVAILABLE;
59     }
60 
61     @Override
updateState(CarUiMultiSelectListPreference preference)62     protected void updateState(CarUiMultiSelectListPreference preference) {
63         super.updateState(preference);
64         updatePreferenceOptions();
65     }
66 
67     @Override
handlePreferenceChanged(CarUiMultiSelectListPreference preference, Object newValue)68     protected boolean handlePreferenceChanged(CarUiMultiSelectListPreference preference,
69             Object newValue) {
70         Set<String> selectedEntries = (Set<String>) newValue;
71         handleRemovedSupportedLinks(selectedEntries);
72         return true;
73     }
74 
handleRemovedSupportedLinks(Set<String> selectedEntries)75     private void handleRemovedSupportedLinks(Set<String> selectedEntries) {
76         List<CharSequence> entryValuesList = Arrays.asList(getPreference().getEntryValues());
77         if (selectedEntries.size() == entryValuesList.size()) {
78             return;
79         }
80         Set<String> entryValuesSet = entryValuesList.stream().map(CharSequence::toString).collect(
81                 Collectors.toSet());
82 
83         entryValuesSet.removeAll(selectedEntries);
84         getDomainVerificationManager().setDomainVerificationUserSelection(getPackageName(),
85                 entryValuesSet, /* isEnabled= */ false);
86         refreshUi();
87     }
88 
updatePreferenceOptions()89     private void updatePreferenceOptions() {
90         List<String> entries = getDomainVerificationManager().getLinksList(getPackageName(),
91                 DOMAIN_STATE_SELECTED);
92 
93         getPreference().setEntries(entries.toArray(new CharSequence[entries.size()]));
94         getPreference().setEntryValues(entries.toArray(new CharSequence[entries.size()]));
95         getPreference().setValues(new HashSet<>(entries));
96         getPreference().setSummary(
97                 StringUtil.getIcuPluralsString(getContext(), getLinksNumber(DOMAIN_STATE_SELECTED),
98                         R.string.opening_links_supported_links_summary));
99     }
100 }
101