1 /*
2  * Copyright (C) 2023 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.intentresolver.emptystate;
17 
18 import android.annotation.NonNull;
19 import android.annotation.UserIdInt;
20 import android.app.AppGlobals;
21 import android.content.ContentResolver;
22 import android.content.Intent;
23 import android.content.pm.IPackageManager;
24 
25 import com.android.intentresolver.IntentForwarderActivity;
26 
27 import java.util.List;
28 
29 /**
30  * Utility class to check if there are cross profile intents, it is in a separate class so
31  * it could be mocked in tests
32  */
33 public class CrossProfileIntentsChecker {
34 
35     private final ContentResolver mContentResolver;
36     private final IPackageManager mPackageManager;
37 
CrossProfileIntentsChecker(@onNull ContentResolver contentResolver)38     public CrossProfileIntentsChecker(@NonNull ContentResolver contentResolver) {
39         this(contentResolver, AppGlobals.getPackageManager());
40     }
41 
CrossProfileIntentsChecker( @onNull ContentResolver contentResolver, IPackageManager packageManager)42     CrossProfileIntentsChecker(
43             @NonNull ContentResolver contentResolver, IPackageManager packageManager) {
44         mContentResolver = contentResolver;
45         mPackageManager = packageManager;
46     }
47 
48     /**
49      * Returns {@code true} if at least one of the provided {@code intents} can be forwarded
50      * from {@code source} (user id) to {@code target} (user id).
51      */
hasCrossProfileIntents( List<Intent> intents, @UserIdInt int source, @UserIdInt int target)52     public boolean hasCrossProfileIntents(
53             List<Intent> intents, @UserIdInt int source, @UserIdInt int target) {
54         return intents.stream().anyMatch(intent ->
55                 null != IntentForwarderActivity.canForward(intent, source, target,
56                         mPackageManager, mContentResolver));
57     }
58 }
59 
60