1 /* 2 * Copyright (C) 2021 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.settings.applications.intentpicker; 18 19 import android.app.Dialog; 20 import android.content.Context; 21 import android.content.pm.PackageManager; 22 import android.content.pm.verify.domain.DomainVerificationManager; 23 import android.content.pm.verify.domain.DomainVerificationUserState; 24 import android.os.Bundle; 25 import android.util.ArraySet; 26 import android.util.Log; 27 28 import androidx.appcompat.app.AlertDialog; 29 import androidx.fragment.app.Fragment; 30 import androidx.fragment.app.FragmentManager; 31 import androidx.lifecycle.ViewModelProviders; 32 33 import com.android.settings.R; 34 import com.android.settings.core.instrumentation.InstrumentedDialogFragment; 35 36 import java.util.List; 37 import java.util.Set; 38 import java.util.UUID; 39 40 /** A customized {@link InstrumentedDialogFragment} with multiple checkboxes. */ 41 public class SupportedLinksDialogFragment extends InstrumentedDialogFragment { 42 private static final String TAG = "SupportedLinksDialogFrg"; 43 private static final String DLG_ID = "SupportedLinksDialog"; 44 45 private SupportedLinkViewModel mViewModel; 46 private List<SupportedLinkWrapper> mSupportedLinkWrapperList; 47 private String mPackage; 48 49 @Override onCreate(Bundle savedInstanceState)50 public void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 mPackage = getArguments().getString(AppLaunchSettings.APP_PACKAGE_KEY); 53 mViewModel = ViewModelProviders.of(this.getActivity()).get(SupportedLinkViewModel.class); 54 mSupportedLinkWrapperList = mViewModel.getSupportedLinkWrapperList(); 55 } 56 57 @Override onCreateDialog(Bundle savedInstanceState)58 public Dialog onCreateDialog(Bundle savedInstanceState) { 59 final Context context = getActivity(); 60 final SupportedLinksAdapter adapter = new SupportedLinksAdapter(context, 61 mSupportedLinkWrapperList); 62 final AlertDialog.Builder builder = new AlertDialog 63 .Builder(context) 64 .setTitle(IntentPickerUtils.getCentralizedDialogTitle(getSupportedLinksTitle())) 65 .setAdapter(adapter, /* listener= */ null) 66 .setCancelable(true) 67 .setPositiveButton(R.string.app_launch_supported_links_add, (dialog, id) -> { 68 doSelectedAction(); 69 }) 70 .setNegativeButton(R.string.app_launch_dialog_cancel, /* listener= */ null); 71 return builder.create(); 72 } 73 74 @Override getMetricsCategory()75 public int getMetricsCategory() { 76 return 0; 77 } 78 79 /** Display the dialog. */ showDialog(FragmentManager manager)80 public void showDialog(FragmentManager manager) { 81 show(manager, DLG_ID); 82 } 83 getSupportedLinksTitle()84 private String getSupportedLinksTitle() { 85 final int supportedLinksNo = mSupportedLinkWrapperList.size(); 86 return getResources().getQuantityString( 87 R.plurals.app_launch_supported_links_title, supportedLinksNo, supportedLinksNo); 88 } 89 doSelectedAction()90 private void doSelectedAction() { 91 final DomainVerificationManager manager = getActivity().getSystemService( 92 DomainVerificationManager.class); 93 final DomainVerificationUserState userState = 94 IntentPickerUtils.getDomainVerificationUserState(manager, mPackage); 95 if (userState == null || mSupportedLinkWrapperList == null) { 96 return; 97 } 98 99 updateUserSelection(manager, userState); 100 displaySelectedItem(); 101 } 102 updateUserSelection(DomainVerificationManager manager, DomainVerificationUserState userState)103 private void updateUserSelection(DomainVerificationManager manager, 104 DomainVerificationUserState userState) { 105 final Set<String> domainSet = new ArraySet<>(); 106 for (SupportedLinkWrapper wrapper : mSupportedLinkWrapperList) { 107 if (wrapper.isChecked()) { 108 domainSet.add(wrapper.getHost()); 109 } 110 } 111 if (domainSet.size() > 0) { 112 setDomainVerificationUserSelection(manager, userState.getIdentifier(), 113 domainSet, /* enabled= */true); 114 } 115 } 116 setDomainVerificationUserSelection(DomainVerificationManager manager, UUID identifier, Set<String> domainSet, boolean isEnabled)117 private void setDomainVerificationUserSelection(DomainVerificationManager manager, 118 UUID identifier, Set<String> domainSet, boolean isEnabled) { 119 try { 120 manager.setDomainVerificationUserSelection(identifier, domainSet, isEnabled); 121 } catch (PackageManager.NameNotFoundException e) { 122 Log.w(TAG, "addSelectedItems : " + e.getMessage()); 123 } 124 } 125 displaySelectedItem()126 private void displaySelectedItem() { 127 final List<Fragment> fragments = getActivity().getSupportFragmentManager().getFragments(); 128 for (Fragment fragment : fragments) { 129 if (fragment instanceof AppLaunchSettings) { 130 ((AppLaunchSettings) fragment).addSelectedLinksPreference(); 131 } 132 } 133 } 134 } 135