1 /*
2  * Copyright (C) 2022 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.permission.util;
18 
19 import android.annotation.NonNull;
20 import android.annotation.UserIdInt;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.PackageManager;
24 import android.content.pm.ResolveInfo;
25 import android.os.Binder;
26 import android.os.UserHandle;
27 
28 import java.util.List;
29 
30 /** Utility class for dealing with packages. */
31 public final class PackageUtils {
PackageUtils()32     private PackageUtils() {}
33 
34     /**
35      * Returns {@code true} if the calling package is able to query for details about the package.
36      *
37      * @see PackageManager#canPackageQuery
38      */
canCallingOrSelfPackageQuery( @onNull String packageName, @UserIdInt int userId, @NonNull Context context)39     public static boolean canCallingOrSelfPackageQuery(
40             @NonNull String packageName, @UserIdInt int userId, @NonNull Context context) {
41         final Context userContext = context.createContextAsUser(UserHandle.of(userId), 0);
42         final PackageManager userPackageManager = userContext.getPackageManager();
43         try {
44             userPackageManager.getPackageInfo(packageName, 0);
45             return true;
46         } catch (PackageManager.NameNotFoundException ignored) {
47             return false;
48         }
49     }
50 
51     /**
52      * Returns the activities {@link ResolveInfo} that match the given {@link Intent} for the given
53      * {@code flags} and {@code userId}.
54      */
55     @NonNull
queryUnfilteredIntentActivitiesAsUser( @onNull Intent intent, int flags, @UserIdInt int userId, @NonNull Context context)56     public static List<ResolveInfo> queryUnfilteredIntentActivitiesAsUser(
57             @NonNull Intent intent, int flags, @UserIdInt int userId, @NonNull Context context) {
58         PackageManager packageManager = context.getPackageManager();
59         // This call requires the INTERACT_ACROSS_USERS permission.
60         final long callingId = Binder.clearCallingIdentity();
61         try {
62             return packageManager.queryIntentActivitiesAsUser(intent, flags, UserHandle.of(userId));
63         } finally {
64             Binder.restoreCallingIdentity(callingId);
65         }
66     }
67 
68     /**
69      * Returns the broadcasts {@link ResolveInfo} that match the given {@link Intent} for the given
70      * {@code flags} and {@code userId}.
71      */
72     @NonNull
queryUnfilteredBroadcastReceiversAsUser( @onNull Intent intent, int flags, @UserIdInt int userId, @NonNull Context context)73     public static List<ResolveInfo> queryUnfilteredBroadcastReceiversAsUser(
74             @NonNull Intent intent, int flags, @UserIdInt int userId, @NonNull Context context) {
75         PackageManager packageManager = context.getPackageManager();
76         // This call requires the INTERACT_ACROSS_USERS permission.
77         final long callingId = Binder.clearCallingIdentity();
78         try {
79             return packageManager.queryBroadcastReceiversAsUser(
80                     intent, flags, UserHandle.of(userId));
81         } finally {
82             Binder.restoreCallingIdentity(callingId);
83         }
84     }
85 }
86