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 17 package com.android.commands.monkey; 18 19 import android.app.ActivityManager; 20 import android.app.AppGlobals; 21 import android.app.IActivityManager; 22 import android.content.pm.IPackageManager; 23 import android.content.pm.PackageManager; 24 import android.content.pm.PermissionInfo; 25 import android.os.RemoteException; 26 import android.permission.IPermissionManager; 27 import android.view.IWindowManager; 28 29 public class MonkeyPermissionEvent extends MonkeyEvent { 30 private String mPkg; 31 private PermissionInfo mPermissionInfo; 32 MonkeyPermissionEvent(String pkg, PermissionInfo permissionInfo)33 public MonkeyPermissionEvent(String pkg, PermissionInfo permissionInfo) { 34 super(EVENT_TYPE_PERMISSION); 35 mPkg = pkg; 36 mPermissionInfo = permissionInfo; 37 } 38 39 @Override injectEvent(IWindowManager iwm, IActivityManager iam, int verbose)40 public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) { 41 final IPackageManager packageManager = AppGlobals.getPackageManager(); 42 final IPermissionManager permissionManager = AppGlobals.getPermissionManager(); 43 final int currentUser = ActivityManager.getCurrentUser(); 44 try { 45 // determine if we should grant or revoke permission 46 int perm = packageManager.checkPermission(mPermissionInfo.name, mPkg, currentUser); 47 boolean grant = perm == PackageManager.PERMISSION_DENIED; 48 // log before calling pm in case we hit an error 49 Logger.out.println(String.format(":Permission %s %s to package %s", 50 grant ? "grant" : "revoke", mPermissionInfo.name, mPkg)); 51 if (grant) { 52 permissionManager.grantRuntimePermission(mPkg, mPermissionInfo.name, currentUser); 53 } else { 54 permissionManager.revokeRuntimePermission(mPkg, mPermissionInfo.name, currentUser, 55 null); 56 } 57 return MonkeyEvent.INJECT_SUCCESS; 58 } catch (RemoteException re) { 59 return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION; 60 } 61 } 62 } 63