diff --git a/cmds/monkey/src/com/android/commands/monkey/Monkey.java b/cmds/monkey/src/com/android/commands/monkey/Monkey.java index 00fb40cb9..fe0de9968 100644 --- a/cmds/monkey/src/com/android/commands/monkey/Monkey.java +++ b/cmds/monkey/src/com/android/commands/monkey/Monkey.java @@ -362,7 +362,7 @@ public class Monkey { if (mScriptFileName != null) { // script mode, ignore other options - mEventSource = new MonkeySourceScript(mScriptFileName); + mEventSource = new MonkeySourceScript(mScriptFileName, mThrottle); mEventSource.setVerbose(mVerbose); } else { // random source by default diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeyEvent.java b/cmds/monkey/src/com/android/commands/monkey/MonkeyEvent.java index 7176073b9..d926be8e4 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeyEvent.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeyEvent.java @@ -50,6 +50,14 @@ public abstract class MonkeyEvent { return eventType; } + /** + * @return true if it is safe to throttle after this event, and false otherwise. + */ + public boolean isThrottlable() { + return true; + } + + /** * a method for injecting event * @param iwm wires to current window manager diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeyEventQueue.java b/cmds/monkey/src/com/android/commands/monkey/MonkeyEventQueue.java new file mode 100644 index 000000000..dfe389a7a --- /dev/null +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeyEventQueue.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.commands.monkey; + +import java.util.LinkedList; + +/** + * class for keeping a monkey event queue + */ +@SuppressWarnings("serial") +public class MonkeyEventQueue extends LinkedList { + + private long mThrottle; + + public MonkeyEventQueue(long throttle) { + super(); + mThrottle = throttle; + } + + @Override + public void addLast(MonkeyEvent e) { + super.add(e); + if (e.isThrottlable()) { + super.add(new MonkeyThrottleEvent(mThrottle)); + } + } +} diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeyKeyEvent.java b/cmds/monkey/src/com/android/commands/monkey/MonkeyKeyEvent.java index c1e0ffce7..877ebb576 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeyKeyEvent.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeyKeyEvent.java @@ -91,6 +91,11 @@ public class MonkeyKeyEvent extends MonkeyEvent { mKeyCode, mRepeatCount, mMetaState, mDeviceId, mScancode); } + @Override + public boolean isThrottlable() { + return (getAction() == KeyEvent.ACTION_UP); + } + @Override public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) { if (verbose > 1) { diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeyMotionEvent.java b/cmds/monkey/src/com/android/commands/monkey/MonkeyMotionEvent.java index 2657061be..81c64b44d 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeyMotionEvent.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeyMotionEvent.java @@ -20,6 +20,7 @@ import android.app.IActivityManager; import android.os.RemoteException; import android.os.SystemClock; import android.view.IWindowManager; +import android.view.KeyEvent; import android.view.MotionEvent; @@ -123,6 +124,11 @@ public class MonkeyMotionEvent extends MonkeyEvent { mXPrecision, mYPrecision, mDeviceId, mEdgeFlags); } + @Override + public boolean isThrottlable() { + return (getAction() == KeyEvent.ACTION_UP); + } + @Override public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) { diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java index 902d8e84c..5f9c10f03 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java @@ -31,7 +31,7 @@ import java.util.Random; /** * monkey event queue */ -public class MonkeySourceRandom implements MonkeyEventSource{ +public class MonkeySourceRandom implements MonkeyEventSource { /** Key events that move around the UI. */ private static final int[] NAV_KEYS = { KeyEvent.KEYCODE_DPAD_UP, KeyEvent.KEYCODE_DPAD_DOWN, @@ -168,7 +168,7 @@ public class MonkeySourceRandom implements MonkeyEventSource{ private float[] mFactors = new float[FACTORZ_COUNT]; private ArrayList mMainApps; private int mEventCount = 0; //total number of events generated so far - private LinkedList mQ = new LinkedList(); + private MonkeyEventQueue mQ; private Random mRandom; private int mVerbose = 0; private long mThrottle = 0; @@ -203,7 +203,7 @@ public class MonkeySourceRandom implements MonkeyEventSource{ mRandom = new SecureRandom(); mRandom.setSeed((seed == 0) ? -1 : seed); mMainApps = MainApps; - mThrottle = throttle; + mQ = new MonkeyEventQueue(throttle); } /** @@ -336,7 +336,6 @@ public class MonkeySourceRandom implements MonkeyEventSource{ downAt, MotionEvent.ACTION_UP, x, y, 0); e.setIntermediateNote(false); mQ.addLast(e); - addThrottle(); } /** @@ -387,7 +386,6 @@ public class MonkeySourceRandom implements MonkeyEventSource{ e.setIntermediateNote(false); mQ.addLast(e); } - addThrottle(); } /** @@ -420,13 +418,11 @@ public class MonkeySourceRandom implements MonkeyEventSource{ MonkeyActivityEvent e = new MonkeyActivityEvent(mMainApps.get( mRandom.nextInt(mMainApps.size()))); mQ.addLast(e); - addThrottle(); return; } else if (cls < mFactors[FACTOR_FLIP]) { MonkeyFlipEvent e = new MonkeyFlipEvent(mKeyboardOpen); mKeyboardOpen = !mKeyboardOpen; mQ.addLast(e); - addThrottle(); return; } else { lastKey = 1 + mRandom.nextInt(KeyEvent.getMaxKeyCode() - 1); @@ -437,8 +433,6 @@ public class MonkeySourceRandom implements MonkeyEventSource{ e = new MonkeyKeyEvent(KeyEvent.ACTION_UP, lastKey); mQ.addLast(e); - - addThrottle(); } public boolean validate() { @@ -472,8 +466,4 @@ public class MonkeySourceRandom implements MonkeyEventSource{ mQ.removeFirst(); return e; } - - private void addThrottle() { - mQ.addLast(new MonkeyThrottleEvent(MonkeyEvent.EVENT_TYPE_THROTTLE, mThrottle)); - } } diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java index aadda9f62..869af2cad 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java @@ -35,18 +35,19 @@ import java.util.StringTokenizer; * type= raw events * count= 10 * speed= 1.0 + * start data >> * captureDispatchPointer(5109520,5109520,0,230.75429,458.1814,0.20784314, * 0.06666667,0,0.0,0.0,65539,0) * captureDispatchKey(5113146,5113146,0,20,0,0,0,0) * captureDispatchFlip(true) * ... */ -public class MonkeySourceScript implements MonkeyEventSource{ +public class MonkeySourceScript implements MonkeyEventSource { private int mEventCountInScript = 0; //total number of events in the file private int mVerbose = 0; private double mSpeed = 1.0; private String mScriptFileName; - private LinkedList mQ = new LinkedList(); + private MonkeyEventQueue mQ; private static final String HEADER_TYPE = "type="; private static final String HEADER_COUNT = "count="; @@ -80,12 +81,14 @@ public class MonkeySourceScript implements MonkeyEventSource{ // a line at the end of the header private static final String STARTING_DATA_LINE = "start data >>"; private boolean mFileOpened = false; + FileInputStream mFStream; DataInputStream mInputStream; BufferedReader mBufferReader; - public MonkeySourceScript(String filename) { + public MonkeySourceScript(String filename, long throttle) { mScriptFileName = filename; + mQ = new MonkeyEventQueue(throttle); } /** diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeyThrottleEvent.java b/cmds/monkey/src/com/android/commands/monkey/MonkeyThrottleEvent.java index 3d7d48aa5..0b4e6b693 100644 --- a/cmds/monkey/src/com/android/commands/monkey/MonkeyThrottleEvent.java +++ b/cmds/monkey/src/com/android/commands/monkey/MonkeyThrottleEvent.java @@ -16,11 +16,10 @@ package com.android.commands.monkey; +import java.util.List; + import android.app.IActivityManager; -import android.os.RemoteException; -import android.os.SystemClock; import android.view.IWindowManager; -import android.view.MotionEvent; /** @@ -29,8 +28,8 @@ import android.view.MotionEvent; public class MonkeyThrottleEvent extends MonkeyEvent { private long mThrottle; - public MonkeyThrottleEvent(int type, long throttle) { - super(type); + public MonkeyThrottleEvent(long throttle) { + super(MonkeyEvent.EVENT_TYPE_THROTTLE); mThrottle = throttle; }