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 package com.android.settings.applications.intentpicker;
17 
18 import android.content.pm.PackageManager;
19 import android.content.pm.verify.domain.DomainVerificationManager;
20 import android.content.pm.verify.domain.DomainVerificationUserState;
21 import android.os.Build;
22 import android.text.Layout;
23 import android.text.SpannableString;
24 import android.text.style.AlignmentSpan;
25 import android.util.Log;
26 
27 import java.util.List;
28 import java.util.stream.Collectors;
29 
30 /** The common APIs for intent picker */
31 public class IntentPickerUtils {
32     private static final String TAG = "IntentPickerUtils";
33     private static final boolean DEBUG = Build.IS_DEBUGGABLE;
34 
IntentPickerUtils()35     private IntentPickerUtils() {
36     }
37 
38     /**
39      * Gets the centralized title.
40      *
41      * @param title The title of the dialog box.
42      * @return The spannable string with centralized title.
43      */
getCentralizedDialogTitle(String title)44     public static SpannableString getCentralizedDialogTitle(String title) {
45         final SpannableString dialogTitle = new SpannableString(title);
46         dialogTitle.setSpan(new AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER), /* start= */
47                 0, title.length(), /* flags= */ 0);
48         return dialogTitle;
49     }
50 
51     /**
52      * Gets the {@link DomainVerificationUserState} for specific application.
53      *
54      * @param manager The {@link DomainVerificationManager}.
55      * @param pkgName The package name of the target application.
56      */
getDomainVerificationUserState( DomainVerificationManager manager, String pkgName)57     public static DomainVerificationUserState getDomainVerificationUserState(
58             DomainVerificationManager manager, String pkgName) {
59         try {
60             final DomainVerificationUserState domainVerificationUserState =
61                     manager.getDomainVerificationUserState(pkgName);
62             return domainVerificationUserState;
63         } catch (PackageManager.NameNotFoundException e) {
64             Log.w(TAG, e.getMessage());
65             return null;
66         }
67     }
68 
69     /**
70      * Gets the links list by {@link DomainVerificationUserState.DomainState}
71      *
72      * @param manager The {@link DomainVerificationManager}.
73      * @param pkgName The package name of the target application.
74      * @param state   The user state you want to query.
75      * @return A links list.
76      */
getLinksList(DomainVerificationManager manager, String pkgName, @DomainVerificationUserState.DomainState int state)77     public static List<String> getLinksList(DomainVerificationManager manager, String pkgName,
78             @DomainVerificationUserState.DomainState int state) {
79         final DomainVerificationUserState userStage = getDomainVerificationUserState(manager,
80                 pkgName);
81         if (userStage == null) {
82             return null;
83         }
84         return userStage.getHostToStateMap()
85                 .entrySet()
86                 .stream()
87                 .filter(it -> it.getValue() == state)
88                 .map(it -> it.getKey())
89                 .collect(Collectors.toList());
90     }
91 
92     /** Logs the message in debug ROM. */
logd(String msg)93     public static void logd(String msg) {
94         if (DEBUG) {
95             Log.d(TAG, msg);
96         }
97     }
98 }
99