Home | History | Annotate | Download | only in monkey
      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.IActivityManager;
     20 import android.content.ComponentName;
     21 import android.content.Intent;
     22 import android.os.Bundle;
     23 import android.os.RemoteException;
     24 import android.view.IWindowManager;
     25 import android.util.Log;
     26 
     27 /**
     28  * monkey activity event
     29  */
     30 public class MonkeyActivityEvent extends MonkeyEvent {
     31     private ComponentName mApp;
     32     long mAlarmTime = 0;
     33 
     34     public MonkeyActivityEvent(ComponentName app) {
     35         super(EVENT_TYPE_ACTIVITY);
     36         mApp = app;
     37     }
     38 
     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      */
     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);
     53         return intent;
     54     }
     55 
     56     @Override
     57     public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
     58         Intent intent = getEvent();
     59         if (verbose > 0) {
     60             System.out.println(":Switch: " + intent.toURI());
     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.startActivity(null, intent, null, null, 0, null, null, 0,
     71                     false, false);
     72         } catch (RemoteException e) {
     73             System.err.println("** Failed talking with activity manager!");
     74             return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION;
     75         } catch (SecurityException e) {
     76             if (verbose > 0) {
     77                 System.out.println("** Permissions error starting activity "
     78                         + intent.toURI());
     79             }
     80             return MonkeyEvent.INJECT_ERROR_SECURITY_EXCEPTION;
     81         }
     82         return MonkeyEvent.INJECT_SUCCESS;
     83     }
     84 }
     85