1 /*
2  * Copyright (C) 2021 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 import android.app.AppOpsManager;
18 import android.app.AppProtoEnums;
19 import android.content.Context;
20 import android.os.IBinder;
21 
22 import java.util.ArrayList;
23 import java.util.List;
24 
25 public class AppOpsManagerCompat {
26     private final AppOpsManager mAppOpsManager;
27 
28     public static class PackageOpsCompat {
29         private final AppOpsManager.PackageOps mPackageOps;
30 
PackageOpsCompat(AppOpsManager.PackageOps packageOps)31         private PackageOpsCompat(AppOpsManager.PackageOps packageOps) {
32             this.mPackageOps = packageOps;
33         }
34 
getUid()35         public int getUid() {
36             return mPackageOps.getUid();
37         }
38 
getPackageName()39         public String getPackageName() {
40             return mPackageOps.getPackageName();
41         }
42     }
43 
getPackagesForOps(int[] ops)44     public List<PackageOpsCompat> getPackagesForOps(int[] ops) {
45         List<PackageOpsCompat> list = new ArrayList<>();
46         for (AppOpsManager.PackageOps op : mAppOpsManager.getPackagesForOps(ops)) {
47             list.add(new PackageOpsCompat(op));
48         }
49         return list;
50     }
51 
AppOpsManagerCompat(AppOpsManager appOpsManager)52     public AppOpsManagerCompat(AppOpsManager appOpsManager) {
53         mAppOpsManager = appOpsManager;
54     }
55 
AppOpsManagerCompat(Context context)56     public AppOpsManagerCompat(Context context) {
57         mAppOpsManager = context.getSystemService(AppOpsManager.class);
58     }
59 
60 
setMode(int code, int uid, String packageName, @AppOpsManager.Mode int mode)61     public void setMode(int code, int uid, String packageName, @AppOpsManager.Mode int mode) {
62         mAppOpsManager.setMode(code, uid, packageName, mode);
63     }
64 
setUidMode(int code, int uid, @AppOpsManager.Mode int mode)65     public void setUidMode(int code, int uid, @AppOpsManager.Mode int mode) {
66         mAppOpsManager.setUidMode(code, uid, mode);
67     }
68 
checkOpNoThrow(int op, int uid, String packageName)69     public int checkOpNoThrow(int op, int uid, String packageName) {
70         return mAppOpsManager.checkOpNoThrow(op, uid, packageName);
71     }
72 
73     public static final int OP_WRITE_SETTINGS = AppOpsManager.OP_WRITE_SETTINGS;
74     public static final int OP_PICTURE_IN_PICTURE = AppOpsManager.OP_PICTURE_IN_PICTURE;
75 
76     public static final int OP_GET_USAGE_STATS = AppOpsManager.OP_GET_USAGE_STATS;
77 
78     public static final int OP_SCHEDULE_EXACT_ALARM = AppOpsManager.OP_SCHEDULE_EXACT_ALARM;
79 
80     public static final int OP_MONITOR_LOCATION = AppOpsManager.OP_MONITOR_LOCATION;
81     public static final int OP_MONITOR_HIGH_POWER_LOCATION =
82             AppOpsManager.OP_MONITOR_HIGH_POWER_LOCATION;
83 
84     public static final int OP_FINE_LOCATION = AppOpsManager.OP_FINE_LOCATION;
85     public static final int OP_COARSE_LOCATION = AppOpsManager.OP_COARSE_LOCATION;
86 
87     public static final int OP_CAMERA = AppOpsManager.OP_CAMERA;
88     public static final int OP_PHONE_CALL_CAMERA = AppOpsManager.OP_PHONE_CALL_CAMERA;
89 
90     public static final int OP_RECORD_AUDIO = AppOpsManager.OP_RECORD_AUDIO;
91     public static final int OP_PHONE_CALL_MICROPHONE = AppOpsManager.OP_PHONE_CALL_MICROPHONE;
92 
93     public static final int OP_FLAGS_ALL = AppOpsManager.OP_FLAGS_ALL;
94     public static final int OP_SYSTEM_ALERT_WINDOW = AppOpsManager.OP_SYSTEM_ALERT_WINDOW;
95 
96     public static final int OP_TOAST_WINDOW = AppProtoEnums.APP_OP_TOAST_WINDOW;
97     public static final int OP_MOCK_LOCATION = AppProtoEnums.APP_OP_MOCK_LOCATION;
98 
99     public static final int OP_REQUEST_INSTALL_PACKAGES =
100             AppProtoEnums.APP_OP_REQUEST_INSTALL_PACKAGES;
101 
setUserRestriction(int code, boolean restricted, IBinder token)102     public void setUserRestriction(int code, boolean restricted, IBinder token) {
103         mAppOpsManager.setUserRestriction(code, restricted, token);
104     }
105 }
106