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.os.RemoteException;
     21 import android.view.IWindowManager;
     22 import android.view.KeyEvent;
     23 /**
     24  * monkey key event
     25  */
     26 public class MonkeyKeyEvent extends MonkeyEvent {
     27     private long mDownTime = -1;
     28     private int mMetaState = -1;
     29     private int mAction = -1;
     30     private int mKeyCode = -1;
     31     private int mScancode = -1;
     32     private int mRepeatCount = -1;
     33     private int mDeviceId = -1;
     34     private long mEventTime = -1;
     35 
     36     private KeyEvent keyEvent = null;
     37 
     38     public MonkeyKeyEvent(int action, int keycode) {
     39         super(EVENT_TYPE_KEY);
     40         mAction = action;
     41         mKeyCode = keycode;
     42     }
     43 
     44     public MonkeyKeyEvent(KeyEvent e) {
     45         super(EVENT_TYPE_KEY);
     46         keyEvent = e;
     47     }
     48 
     49     public MonkeyKeyEvent(long downTime, long eventTime, int action,
     50             int code, int repeat, int metaState,
     51             int device, int scancode) {
     52         super(EVENT_TYPE_KEY);
     53 
     54         mAction = action;
     55         mKeyCode = code;
     56         mMetaState = metaState;
     57         mScancode = scancode;
     58         mRepeatCount = repeat;
     59         mDeviceId = device;
     60         mDownTime = downTime;
     61         mEventTime = eventTime;
     62     }
     63 
     64     public int getKeyCode() {
     65         return mKeyCode;
     66     }
     67 
     68     public int getAction() {
     69         return mAction;
     70     }
     71 
     72     public long getDownTime() {
     73         return mDownTime;
     74     }
     75 
     76     public long getEventTime() {
     77         return mEventTime;
     78     }
     79 
     80     public void setDownTime(long downTime) {
     81         mDownTime = downTime;
     82     }
     83 
     84     public void setEventTime(long eventTime) {
     85         mEventTime = eventTime;
     86     }
     87 
     88     /**
     89      * @return the key event
     90      */
     91     private KeyEvent getEvent() {
     92         if (keyEvent == null) {
     93             if (mDeviceId < 0) {
     94                 keyEvent = new KeyEvent(mAction, mKeyCode);
     95             } else {
     96                 // for scripts
     97                 keyEvent = new KeyEvent(mDownTime, mEventTime, mAction,
     98                                         mKeyCode, mRepeatCount, mMetaState, mDeviceId, mScancode);
     99             }
    100         }
    101         return keyEvent;
    102     }
    103 
    104     @Override
    105     public boolean isThrottlable() {
    106         return (getAction() == KeyEvent.ACTION_UP);
    107     }
    108 
    109     @Override
    110     public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
    111         if (verbose > 1) {
    112             String note;
    113             if (mAction == KeyEvent.ACTION_UP) {
    114                 note = "ACTION_UP";
    115             } else {
    116                 note = "ACTION_DOWN";
    117             }
    118 
    119             try {
    120                 System.out.println(":SendKey (" + note + "): "
    121                         + mKeyCode + "    // "
    122                         + MonkeySourceRandom.getKeyName(mKeyCode));
    123             } catch (ArrayIndexOutOfBoundsException e) {
    124                 System.out.println(":SendKey (ACTION_UP): "
    125                         + mKeyCode + "    // Unknown key event");
    126             }
    127         }
    128 
    129         // inject key event
    130         try {
    131             if (!iwm.injectKeyEvent(getEvent(), false)) {
    132                 return MonkeyEvent.INJECT_FAIL;
    133             }
    134         } catch (RemoteException ex) {
    135             return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION;
    136         }
    137 
    138         return MonkeyEvent.INJECT_SUCCESS;
    139     }
    140 }
    141