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.os.SystemClock;
     22 import android.view.IWindowManager;
     23 import android.view.KeyEvent;
     24 import android.view.MotionEvent;
     25 
     26 
     27 /**
     28  * monkey motion event
     29  */
     30 public class MonkeyMotionEvent extends MonkeyEvent {
     31     private long mDownTime = -1;
     32     private long mEventTime = -1;
     33     private int mAction = -1;
     34     private float mX = -1;
     35     private float mY = -1;
     36     private float mPressure = -1;
     37     private float mSize = -1;
     38     private int mMetaState = -1;
     39     private float mXPrecision = -1;
     40     private float mYPrecision = -1;
     41     private int mDeviceId = -1;
     42     private int mEdgeFlags = -1;
     43 
     44     //If true, this is an intermediate step (more verbose logging, only)
     45     private boolean mIntermediateNote;
     46 
     47     public MonkeyMotionEvent(int type, long downAt, int action,
     48             float x, float y, int metaState) {
     49         super(type);
     50         mDownTime = downAt;
     51         mAction = action;
     52         mX = x;
     53         mY = y;
     54         mMetaState = metaState;
     55     }
     56 
     57     public MonkeyMotionEvent(int type, long downTime, long eventTime, int action,
     58             float x, float y, float pressure, float size, int metaState,
     59             float xPrecision, float yPrecision, int deviceId, int edgeFlags) {
     60         super(type);
     61         mDownTime = downTime;
     62         mEventTime = eventTime;
     63         mAction = action;
     64         mX = x;
     65         mY = y;
     66         mPressure = pressure;
     67         mSize = size;
     68         mMetaState = metaState;
     69         mXPrecision = xPrecision;
     70         mYPrecision = yPrecision;
     71         mDeviceId = deviceId;
     72         mEdgeFlags = edgeFlags;
     73     }
     74 
     75     public void setIntermediateNote(boolean b) {
     76         mIntermediateNote = b;
     77     }
     78 
     79     public boolean getIntermediateNote() {
     80         return mIntermediateNote;
     81     }
     82 
     83     public float getX() {
     84         return mX;
     85     }
     86 
     87     public float getY() {
     88         return mY;
     89     }
     90 
     91     public int getAction() {
     92         return mAction;
     93     }
     94 
     95     public long getDownTime() {
     96         return mDownTime;
     97     }
     98 
     99     public long getEventTime() {
    100         return mEventTime;
    101     }
    102 
    103     public void setDownTime(long downTime) {
    104         mDownTime = downTime;
    105     }
    106 
    107     public void setEventTime(long eventTime) {
    108         mEventTime = eventTime;
    109     }
    110 
    111     /**
    112      *
    113      * @return instance of a motion event
    114      */
    115     private MotionEvent getEvent() {
    116         if (mDeviceId < 0) {
    117             return MotionEvent.obtain(mDownTime, SystemClock.uptimeMillis(),
    118                 mAction, mX, mY, mMetaState);
    119         }
    120 
    121         // for scripts
    122         return MotionEvent.obtain(mDownTime, mEventTime,
    123                 mAction, mX, mY, mPressure, mSize, mMetaState,
    124                 mXPrecision, mYPrecision, mDeviceId, mEdgeFlags);
    125     }
    126 
    127     @Override
    128     public boolean isThrottlable() {
    129         return (getAction() == KeyEvent.ACTION_UP);
    130     }
    131 
    132     @Override
    133     public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
    134 
    135         String note;
    136         if ((verbose > 0 && !mIntermediateNote) || verbose > 1) {
    137             if (mAction == MotionEvent.ACTION_DOWN) {
    138                 note = "DOWN";
    139             } else if (mAction == MotionEvent.ACTION_UP) {
    140                 note = "UP";
    141             } else {
    142                 note = "MOVE";
    143             }
    144             System.out.println(":Sending Pointer ACTION_" + note +
    145                     " x=" + mX + " y=" + mY);
    146         }
    147         try {
    148             int type = this.getEventType();
    149             MotionEvent me = getEvent();
    150 
    151             if ((type == MonkeyEvent.EVENT_TYPE_POINTER &&
    152                     !iwm.injectPointerEvent(me, false))
    153                     || (type == MonkeyEvent.EVENT_TYPE_TRACKBALL &&
    154                             !iwm.injectTrackballEvent(me, false))) {
    155                 return MonkeyEvent.INJECT_FAIL;
    156             }
    157         } catch (RemoteException ex) {
    158             return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION;
    159         }
    160         return MonkeyEvent.INJECT_SUCCESS;
    161     }
    162 }
    163