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 final 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 private AppOpsManagerCompat() {} 91 92 /** 93 * Gets the app op name associated with a given permission. 94 * 95 * @param permission The permission. 96 * @return The app op associated with the permission or null. 97 */ 98 public static String permissionToOp(@NonNull String permission) { 99 return IMPL.permissionToOp(permission); 100 } 101 102 /** 103 * Make note of an application performing an operation. Note that you must pass 104 * in both the uid and name of the application to be checked; this function will verify 105 * that these two match, and if not, return {@link #MODE_IGNORED}. If this call 106 * succeeds, the last execution time of the operation for this app will be updated to 107 * the current time. 108 * @param context Your context. 109 * @param op The operation to note. One of the OPSTR_* constants. 110 * @param uid The user id of the application attempting to perform the operation. 111 * @param packageName The name of the application attempting to perform the operation. 112 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 113 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 114 * causing the app to crash). 115 * @throws SecurityException If the app has been configured to crash on this op. 116 */ 117 public static int noteOp(@NonNull Context context, @NonNull String op, int uid, 118 @NonNull String packageName) { 119 return IMPL.noteOp(context, op, uid, packageName); 120 } 121 122 /** 123 * Make note of an application performing an operation on behalf of another 124 * application when handling an IPC. Note that you must pass the package name 125 * of the application that is being proxied while its UID will be inferred from 126 * the IPC state; this function will verify that the calling uid and proxied 127 * package name match, and if not, return {@link #MODE_IGNORED}. If this call 128 * succeeds, the last execution time of the operation for the proxied app and 129 * your app will be updated to the current time. 130 * @param context Your context. 131 * @param op The operation to note. One of the OPSTR_* constants. 132 * @param proxiedPackageName The name of the application calling into the proxy application. 133 * @return Returns {@link #MODE_ALLOWED} if the operation is allowed, or 134 * {@link #MODE_IGNORED} if it is not allowed and should be silently ignored (without 135 * causing the app to crash). 136 * @throws SecurityException If the app has been configured to crash on this op. 137 */ 138 public static int noteProxyOp(@NonNull Context context, @NonNull String op, 139 @NonNull String proxiedPackageName) { 140 return IMPL.noteProxyOp(context, op, proxiedPackageName); 141 } 142} 143