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