1/*
2 * Copyright (C) 2015 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
17package android.support.v4.app;
18
19import android.content.Context;
20import android.os.Build;
21import android.support.annotation.NonNull;
22
23/**
24 * Helper for accessing features in android.app.AppOpsManager
25 * introduced after API level 4 in a backwards compatible fashion.
26 */
27public class AppOpsManagerCompat {
28
29    /**
30     * Result from {@link #noteOp}: the given caller is allowed to
31     * perform the given operation.
32     */
33    public static final int MODE_ALLOWED = 0;
34
35    /**
36     * Result from {@link #noteOp}: the given caller is not allowed to perform
37     * the given operation, and this attempt should <em>silently fail</em> (it
38     * should not cause the app to crash).
39     */
40    public static final int MODE_IGNORED = 1;
41
42    /**
43     * Result from {@link #noteOp}: the given caller should use its default
44     * security check.  This mode is not normally used; it should only be used
45     * with appop permissions, and callers must explicitly check for it and
46     * deal with it.
47     */
48    public static final int MODE_DEFAULT = 3;
49
50    private static class AppOpsManagerImpl {
51        public String permissionToOp(String permission) {
52            return null;
53        }
54
55        public int noteOp(Context context, String op, int uid, String packageName) {
56            return MODE_IGNORED;
57        }
58
59        public int noteProxyOp(Context context, String op, String proxiedPackageName) {
60            return MODE_IGNORED;
61        }
62    }
63
64    private static class AppOpsManager23 extends AppOpsManagerImpl {
65        @Override
66        public String permissionToOp(String permission) {
67            return AppOpsManagerCompat23.permissionToOp(permission);
68        }
69
70        @Override
71        public int noteOp(Context context, String op, int uid, String packageName) {
72            return AppOpsManagerCompat23.noteOp(context, op, uid, packageName);
73        }
74
75        @Override
76        public int noteProxyOp(Context context, String op, String proxiedPackageName) {
77            return AppOpsManagerCompat23.noteProxyOp(context, op, proxiedPackageName);
78        }
79    }
80
81    private static final AppOpsManagerImpl IMPL;
82    static {
83        if (Build.VERSION.SDK_INT >= 23) {
84            IMPL = new AppOpsManager23();
85        } else {
86            IMPL = new AppOpsManagerImpl();
87        }
88    }
89
90    /**
91     * Gets the app op name associated with a given permission.
92     *
93     * @param permission The permission.
94     * @return The app op associated with the permission or null.
95     */
96    public static String permissionToOp(@NonNull String permission) {
97        return IMPL.permissionToOp(permission);
98    }
99
100    /**
101     * Make note of an application performing an operation.  Note that you must pass
102     * in both the uid and name of the application to be checked; this function will verify
103     * that these two match, and if not, return {@link #MODE_IGNORED}.  If this call
104     * succeeds, the last execution time of the operation for this app will be updated to
105     * the current time.
106     * @param context Your context.
107     * @param op The operation to note.  One of the OPSTR_* constants.
108     * @param uid The user id of the application attempting to perform the operation.
109     * @param packageName The name of the application attempting to perform the operation.
110     * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
111     * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
112     * causing the app to crash).
113     * @throws SecurityException If the app has been configured to crash on this op.
114     */
115    public static int noteOp(@NonNull Context context, @NonNull String op, int uid,
116            @NonNull String packageName) {
117        return IMPL.noteOp(context, op, uid, packageName);
118    }
119
120    /**
121     * Make note of an application performing an operation on behalf of another
122     * application when handling an IPC. Note that you must pass the package name
123     * of the application that is being proxied while its UID will be inferred from
124     * the IPC state; this function will verify that the calling uid and proxied
125     * package name match, and if not, return {@link #MODE_IGNORED}. If this call
126     * succeeds, the last execution time of the operation for the proxied app and
127     * your app will be updated to the current time.
128     * @param context Your context.
129     * @param op The operation to note.  One of the OPSTR_* constants.
130     * @param proxiedPackageName The name of the application calling into the proxy application.
131     * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or
132     * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without
133     * causing the app to crash).
134     * @throws SecurityException If the app has been configured to crash on this op.
135     */
136    public static int noteProxyOp(@NonNull Context context, @NonNull String op,
137            @NonNull String proxiedPackageName) {
138        return IMPL.noteProxyOp(context, op, proxiedPackageName);
139    }
140}
141