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_NONE; 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.HashSet; 31 import java.util.List; 32 import java.util.Set; 33 34 /** 35 * A controller for adding links that can be opened by the app. 36 */ 37 public class AddLinksListPreferenceController extends 38 AppLaunchSettingsBasePreferenceController<CarUiMultiSelectListPreference> { 39 private static final Logger LOG = new Logger(AddLinksListPreferenceController.class); 40 AddLinksListPreferenceController(Context context, String preferenceKey, FragmentController fragmentController, CarUxRestrictions uxRestrictions)41 public AddLinksListPreferenceController(Context context, String preferenceKey, 42 FragmentController fragmentController, CarUxRestrictions uxRestrictions) { 43 super(context, preferenceKey, fragmentController, uxRestrictions); 44 } 45 46 @Override getPreferenceType()47 protected Class<CarUiMultiSelectListPreference> getPreferenceType() { 48 return CarUiMultiSelectListPreference.class; 49 } 50 51 @Override getDefaultAvailabilityStatus()52 protected int getDefaultAvailabilityStatus() { 53 if (isLinkHandlingEnabled() && getLinksNumber(DOMAIN_STATE_NONE) > 0) { 54 return AVAILABLE; 55 } 56 return CONDITIONALLY_UNAVAILABLE; 57 } 58 59 @Override updateState(CarUiMultiSelectListPreference preference)60 protected void updateState(CarUiMultiSelectListPreference preference) { 61 super.updateState(preference); 62 updatePreferenceOptions(); 63 } 64 65 @Override handlePreferenceChanged(CarUiMultiSelectListPreference preference, Object newValue)66 protected boolean handlePreferenceChanged(CarUiMultiSelectListPreference preference, 67 Object newValue) { 68 Set<String> selectedEntries = (Set<String>) newValue; 69 handleAddedLinks(selectedEntries); 70 return true; 71 } 72 handleAddedLinks(Set<String> selectedEntries)73 private void handleAddedLinks(Set<String> selectedEntries) { 74 if (selectedEntries.size() == 0) { 75 return; 76 } 77 getDomainVerificationManager().setDomainVerificationUserSelection(getPackageName(), 78 selectedEntries, true); 79 refreshUi(); 80 } 81 updatePreferenceOptions()82 private void updatePreferenceOptions() { 83 List<String> entries = getDomainVerificationManager().getLinksList(getPackageName(), 84 DOMAIN_STATE_NONE); 85 86 getPreference().setEntries(entries.toArray(new CharSequence[entries.size()])); 87 getPreference().setEntryValues(entries.toArray(new CharSequence[entries.size()])); 88 getPreference().setValues(new HashSet<>()); 89 getPreference().setSummary( 90 StringUtil.getIcuPluralsString(getContext(), getLinksNumber(DOMAIN_STATE_NONE), 91 R.string.opening_links_add_links_summary)); 92 } 93 } 94