• 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.common;
18 
19 import android.content.pm.PackageManager;
20 
21 /** Utility class for common permission behaviors. */
22 public class PermissionUtil {
23     private static final Logger LOG = new Logger(PermissionUtil.class);
PermissionUtil()24     private PermissionUtil() {}
25 
26     /** Checks if a given app requests a given permission */
doesPackageRequestPermission(String packageName, PackageManager packageManager, String permission)27     public static boolean doesPackageRequestPermission(String packageName,
28             PackageManager packageManager, String permission) {
29         try {
30             String[] requestedPermissions = packageManager.getPackageInfo(
31                     packageName, PackageManager.GET_PERMISSIONS)
32                     .requestedPermissions;
33             if (requestedPermissions != null) {
34                 for (String requestedPermission : requestedPermissions) {
35                     if (permission.equals(requestedPermission)) {
36                         return true;
37                     }
38                 }
39             }
40         } catch (PackageManager.NameNotFoundException e) {
41             LOG.e("Unable to query app permissions for " + packageName + " " + e);
42         }
43         return false;
44     }
45 }
46