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.PackageManager; 23 import android.content.pm.PermissionInfo; 24 import android.os.RemoteException; 25 import android.os.ServiceManager; 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 IPermissionManager permissionManager = AppGlobals.getPermissionManager(); 42 final int currentUser = ActivityManager.getCurrentUser(); 43 try { 44 // determine if we should grant or revoke permission 45 int perm = permissionManager.checkPermission(mPermissionInfo.name, mPkg, currentUser); 46 boolean grant = perm == PackageManager.PERMISSION_DENIED; 47 // log before calling pm in case we hit an error 48 Logger.out.println(String.format(":Permission %s %s to package %s", 49 grant ? "grant" : "revoke", mPermissionInfo.name, mPkg)); 50 if (grant) { 51 permissionManager.grantRuntimePermission(mPkg, mPermissionInfo.name, currentUser); 52 } else { 53 permissionManager.revokeRuntimePermission(mPkg, mPermissionInfo.name, currentUser, 54 null); 55 } 56 return MonkeyEvent.INJECT_SUCCESS; 57 } catch (RemoteException re) { 58 return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION; 59 } 60 } 61 } 62