AppOpsManager.java revision a06de0f29b58df9246779cc4bfd8f06f7205ddb6
1/* 2 * Copyright (C) 2012 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.app; 18 19import com.android.internal.app.IAppOpsService; 20 21import android.content.Context; 22import android.os.Process; 23import android.os.RemoteException; 24 25/** @hide */ 26public class AppOpsManager { 27 final Context mContext; 28 final IAppOpsService mService; 29 30 public static final int MODE_ALLOWED = 0; 31 public static final int MODE_IGNORED = 1; 32 public static final int MODE_ERRORED = 2; 33 34 public static final int OP_LOCATION = 0; 35 public static final int OP_GPS = 1; 36 public static final int OP_VIBRATE = 2; 37 38 public static String opToString(int op) { 39 switch (op) { 40 case OP_LOCATION: return "LOCATION"; 41 case OP_GPS: return "GPS"; 42 case OP_VIBRATE: return "VIBRATE"; 43 default: return "Unknown(" + op + ")"; 44 } 45 } 46 47 public AppOpsManager(Context context, IAppOpsService service) { 48 mContext = context; 49 mService = service; 50 } 51 52 public int noteOp(int op, int uid, String packageName) { 53 try { 54 int mode = mService.noteOperation(op, uid, packageName); 55 if (mode == MODE_ERRORED) { 56 throw new SecurityException("Operation not allowed"); 57 } 58 return mode; 59 } catch (RemoteException e) { 60 } 61 return MODE_IGNORED; 62 } 63 64 public int noteOpNoThrow(int op, int uid, String packageName) { 65 try { 66 return mService.noteOperation(op, uid, packageName); 67 } catch (RemoteException e) { 68 } 69 return MODE_IGNORED; 70 } 71 72 public int noteOp(int op) { 73 return noteOp(op, Process.myUid(), mContext.getPackageName()); 74 } 75 76 public int startOp(int op, int uid, String packageName) { 77 try { 78 int mode = mService.startOperation(op, uid, packageName); 79 if (mode == MODE_ERRORED) { 80 throw new SecurityException("Operation not allowed"); 81 } 82 return mode; 83 } catch (RemoteException e) { 84 } 85 return MODE_IGNORED; 86 } 87 88 public int startOpNoThrow(int op, int uid, String packageName) { 89 try { 90 return mService.startOperation(op, uid, packageName); 91 } catch (RemoteException e) { 92 } 93 return MODE_IGNORED; 94 } 95 96 public int startOp(int op) { 97 return startOp(op, Process.myUid(), mContext.getPackageName()); 98 } 99 100 public void finishOp(int op, int uid, String packageName) { 101 try { 102 mService.finishOperation(op, uid, packageName); 103 } catch (RemoteException e) { 104 } 105 } 106 107 public void finishOp(int op) { 108 finishOp(op, Process.myUid(), mContext.getPackageName()); 109 } 110} 111