1 /*
2  * Copyright (C) 2020 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.documentsui.util;
18 
19 import android.content.Intent;
20 import android.content.pm.PackageManager;
21 import android.content.pm.ResolveInfo;
22 
23 import androidx.annotation.Nullable;
24 
25 /**
26  * A utility class for cross-profile usage.
27  */
28 public class CrossProfileUtils {
29     private static final String PROFILE_TARGET_ACTIVITY =
30             "com.android.internal.app.IntentForwarderActivity";
31 
CrossProfileUtils()32     private CrossProfileUtils() {
33     }
34 
35     /**
36      * Returns whether this resolution represents the intent forwarder activity.
37      */
isCrossProfileIntentForwarderActivity(ResolveInfo info)38     public static boolean isCrossProfileIntentForwarderActivity(ResolveInfo info) {
39         if (VersionUtils.isAtLeastR()) {
40             return info.isCrossProfileIntentForwarderActivity();
41         }
42         if (info.activityInfo != null) {
43             return PROFILE_TARGET_ACTIVITY.equals(info.activityInfo.targetActivity);
44         }
45         return false;
46     }
47 
48     /**
49      * Returns the {@ResolveInfo} if this intent is a cross-profile intent or {@code null}
50      * otherwise.
51      */
52     @Nullable
getCrossProfileResolveInfo(PackageManager packageManager, Intent intent)53     public static ResolveInfo getCrossProfileResolveInfo(PackageManager packageManager,
54             Intent intent) {
55         for (ResolveInfo info : packageManager.queryIntentActivities(intent,
56                 PackageManager.MATCH_DEFAULT_ONLY)) {
57             if (isCrossProfileIntentForwarderActivity(info)) {
58                 return info;
59             }
60         }
61         return null;
62     }
63 }
64