• 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 android.annotation.NonNull;
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 
25 import androidx.annotation.Nullable;
26 
27 import com.android.car.settings.common.Logger;
28 
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Set;
32 import java.util.stream.Collectors;
33 
34 /**
35  * Manages domain verifications.
36  */
37 public class CarDomainVerificationManager {
38     private static final Logger LOG = new Logger(CarDomainVerificationManager.class);
39     private DomainVerificationManager mDomainVerificationManager;
40     private DomainVerificationStateChangeListener mStateChangeListener;
41 
42     /**
43      * A listener for the {@link DomainVerificationUserState} change.
44      */
45     public interface DomainVerificationStateChangeListener {
46         /**
47          * Called when {@link DomainVerificationUserState} changes.
48          */
onDomainVerificationStateChanged()49         void onDomainVerificationStateChanged();
50     }
51 
52     /**
53      * The constructor for this class.
54      */
CarDomainVerificationManager(Context context)55     public CarDomainVerificationManager(Context context) {
56         mDomainVerificationManager = context.getSystemService(DomainVerificationManager.class);
57     }
58 
59     /**
60      * Returns {@link DomainVerificationManager}
61      */
getDomainVerificationManager()62     public DomainVerificationManager getDomainVerificationManager() {
63         return mDomainVerificationManager;
64     }
65 
66     /**
67      * Registers a listener.
68      */
addListener(DomainVerificationStateChangeListener listener)69     public void addListener(DomainVerificationStateChangeListener listener) {
70         mStateChangeListener = listener;
71     }
72 
73     /**
74      * Returns the {@link DomainVerificationUserState} for a package.
75      */
76     @Nullable
getDomainVerificationUserState(String pkg)77     public DomainVerificationUserState getDomainVerificationUserState(String pkg) {
78         if (mDomainVerificationManager == null) {
79             return null;
80         }
81         DomainVerificationUserState domainVerificationUserState = null;
82         if (pkg != null) {
83             try {
84                 domainVerificationUserState =
85                         getDomainVerificationManager().getDomainVerificationUserState(pkg);
86             } catch (PackageManager.NameNotFoundException e) {
87                 LOG.e("Fail to get DomainVerificationUserState. The package name is not found: "
88                         + e);
89             }
90         }
91         return domainVerificationUserState;
92     }
93 
94     /**
95      * Set whether link handling is enabled.
96      */
setDomainVerificationLinkHandlingAllowed(@onNull String pkg, boolean allowed)97     public void setDomainVerificationLinkHandlingAllowed(@NonNull String pkg, boolean allowed) {
98         if (mDomainVerificationManager == null) {
99             return;
100         }
101         try {
102             mDomainVerificationManager.setDomainVerificationLinkHandlingAllowed(pkg, allowed);
103             mStateChangeListener.onDomainVerificationStateChanged();
104         } catch (PackageManager.NameNotFoundException e) {
105             LOG.e("There is an exception that the package name cannot be found: " + e);
106         }
107     }
108 
109     /**
110      * Gets the links list by {@link DomainVerificationUserState.DomainState}
111      *
112      * @return A links list.
113      */
getLinksList(String pkgName, @DomainVerificationUserState.DomainState int state)114     public List<String> getLinksList(String pkgName,
115             @DomainVerificationUserState.DomainState int state) {
116         if (mDomainVerificationManager == null) {
117             return Collections.emptyList();
118         }
119         DomainVerificationUserState userState = getDomainVerificationUserState(pkgName);
120         if (userState == null) {
121             return Collections.emptyList();
122         }
123         return userState.getHostToStateMap()
124                 .entrySet()
125                 .stream()
126                 .filter(it -> it.getValue() == state)
127                 .map(it -> it.getKey())
128                 .collect(Collectors.toList());
129     }
130 
131     /**
132      * Update the recorded user selection for the given domains for the given domainSet
133      */
setDomainVerificationUserSelection(String pkg, Set<String> domainSet, boolean isEnabled)134     public void setDomainVerificationUserSelection(String pkg, Set<String> domainSet,
135             boolean isEnabled) {
136         if (mDomainVerificationManager == null) {
137             return;
138         }
139         DomainVerificationUserState userState = getDomainVerificationUserState(pkg);
140         if (userState == null) {
141             return;
142         }
143         try {
144             mDomainVerificationManager.setDomainVerificationUserSelection(userState.getIdentifier(),
145                     domainSet, isEnabled);
146         } catch (PackageManager.NameNotFoundException e) {
147             LOG.e("There is an exception that the package name cannot be found: " + e);
148         }
149     }
150 }
151