OSDN Git Service

Merge change 6506
[android-x86/development.git] / cmds / monkey / src / com / android / commands / monkey / MonkeyKeyEvent.java
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     public MonkeyKeyEvent(int action, int keycode) {
37         super(EVENT_TYPE_KEY);
38         mAction = action;
39         mKeyCode = keycode;
40     }
41     
42     public MonkeyKeyEvent(long downTime, long eventTime, int action,
43             int code, int repeat, int metaState,
44             int device, int scancode) {
45         super(EVENT_TYPE_KEY);
46         
47         mAction = action;
48         mKeyCode = code;
49         mMetaState = metaState;
50         mScancode = scancode;
51         mRepeatCount = repeat;
52         mDeviceId = device;
53         mDownTime = downTime;
54         mEventTime = eventTime;
55     }
56     
57     public int getKeyCode() {
58         return mKeyCode;
59     }
60     
61     public int getAction() {
62         return mAction;
63     }
64     
65     public long getDownTime() {
66         return mDownTime;
67     }
68     
69     public long getEventTime() {
70         return mEventTime;
71     }
72     
73     public void setDownTime(long downTime) {
74         mDownTime = downTime;
75     }
76     
77     public void setEventTime(long eventTime) {
78         mEventTime = eventTime;
79     }
80     
81     /** 
82      * @return the key event
83      */        
84     private KeyEvent getEvent() {
85         if (mDeviceId < 0) {
86             return new KeyEvent(mAction, mKeyCode);
87         }       
88         
89         // for scripts
90         return new KeyEvent(mDownTime, mEventTime, mAction, 
91                 mKeyCode, mRepeatCount, mMetaState, mDeviceId, mScancode);
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(":SendKey (" + note + "): "
111                         + mKeyCode + "    // "
112                         + MonkeySourceRandom.getKeyName(mKeyCode));
113             } catch (ArrayIndexOutOfBoundsException e) {
114                 System.out.println(":SendKey (ACTION_UP): "
115                         + mKeyCode + "    // Unknown key event");
116             }
117         }
118
119         // inject key event
120         try {
121             if (!iwm.injectKeyEvent(getEvent(), false)) {
122                 return MonkeyEvent.INJECT_FAIL;
123             }
124         } catch (RemoteException ex) {
125             return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION;
126         }
127         
128         return MonkeyEvent.INJECT_SUCCESS;
129     }
130 }