• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 
18 package com.android.role.controller.compat;
19 
20 import android.annotation.SuppressLint;
21 import android.app.AppOpsManager;
22 import android.os.Build;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.annotation.RequiresApi;
27 
28 import com.android.modules.utils.build.SdkLevel;
29 
30 import java.util.Objects;
31 
32 /** Helper for accessing features in {@link AppOpsManager}. */
33 public class AppOpsManagerCompat {
34 
35     @RequiresApi(Build.VERSION_CODES.TIRAMISU)
36     @SuppressLint("NewApi")
37     public static final String OPSTR_ACCESS_RESTRICTED_SETTINGS =
38             AppOpsManager.OPSTR_ACCESS_RESTRICTED_SETTINGS;
39 
AppOpsManagerCompat()40     private AppOpsManagerCompat() {}
41 
42     /**
43      * @see AppOpsManager#permissionToOp().
44     */
45     @Nullable
permissionToOp(@onNull String permission)46     public static String permissionToOp(@NonNull String permission) {
47         if (!SdkLevel.isAtLeastV()) {
48             // On Android U and below, PACKAGE_USAGE_STATUS is missing from the mapping
49             // in the framework.
50             if (Objects.equals(permission, android.Manifest.permission.PACKAGE_USAGE_STATS)) {
51                 return AppOpsManager.OPSTR_GET_USAGE_STATS;
52             }
53         }
54         return AppOpsManager.permissionToOp(permission);
55     }
56 }