1 /* 2 * Copyright (C) 2008 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.IActivityManager; 21 import android.content.ComponentName; 22 import android.content.Intent; 23 import android.os.Bundle; 24 import android.os.RemoteException; 25 import android.view.IWindowManager; 26 27 /** 28 * monkey activity event 29 */ 30 public class MonkeyActivityEvent extends MonkeyEvent { 31 private ComponentName mApp; 32 long mAlarmTime = 0; 33 MonkeyActivityEvent(ComponentName app)34 public MonkeyActivityEvent(ComponentName app) { 35 super(EVENT_TYPE_ACTIVITY); 36 mApp = app; 37 } 38 MonkeyActivityEvent(ComponentName app, long arg)39 public MonkeyActivityEvent(ComponentName app, long arg) { 40 super(EVENT_TYPE_ACTIVITY); 41 mApp = app; 42 mAlarmTime = arg; 43 } 44 45 /** 46 * @return Intent for the new activity 47 */ getEvent()48 private Intent getEvent() { 49 Intent intent = new Intent(Intent.ACTION_MAIN); 50 intent.addCategory(Intent.CATEGORY_LAUNCHER); 51 intent.setComponent(mApp); 52 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); 53 return intent; 54 } 55 56 @Override injectEvent(IWindowManager iwm, IActivityManager iam, int verbose)57 public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) { 58 Intent intent = getEvent(); 59 if (verbose > 0) { 60 Logger.out.println(":Switch: " + intent.toUri(0)); 61 } 62 63 if (mAlarmTime != 0){ 64 Bundle args = new Bundle(); 65 args.putLong("alarmTime", mAlarmTime); 66 intent.putExtras(args); 67 } 68 69 try { 70 iam.startActivityAsUser(null, null, intent, null, null, null, 0, 71 0, null, null, ActivityManager.getCurrentUser()); 72 } catch (RemoteException e) { 73 Logger.err.println("** Failed talking with activity manager!"); 74 return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION; 75 } catch (SecurityException e) { 76 if (verbose > 0) { 77 Logger.out.println("** Permissions error starting activity " 78 + intent.toUri(0)); 79 } 80 return MonkeyEvent.INJECT_ERROR_SECURITY_EXCEPTION; 81 } 82 return MonkeyEvent.INJECT_SUCCESS; 83 } 84 } 85