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