AI 143341: am: CL 143190 am: CL 142951 Change the way we handle monkey throttling. We now will only sleep after complete down/[move]/up sequences. This avoids unwanted long pressing. We will need to add code to explicitly long press at some point.
  Original author: emillar
  Merged from: //branches/cupcake/...
  Original author: android-build
  Merged from: //branches/donutburger/...

Automated import of CL 143341
diff --git a/cmds/monkey/src/com/android/commands/monkey/Monkey.java b/cmds/monkey/src/com/android/commands/monkey/Monkey.java
index f6ab19d..00fb40c 100644
--- a/cmds/monkey/src/com/android/commands/monkey/Monkey.java
+++ b/cmds/monkey/src/com/android/commands/monkey/Monkey.java
@@ -369,7 +369,7 @@
             if (mVerbose >= 2) {    // check seeding performance
                 System.out.println("// Seeded: " + mSeed);
             }
-            mEventSource = new MonkeySourceRandom(mSeed, mMainApps);
+            mEventSource = new MonkeySourceRandom(mSeed, mMainApps, mThrottle);
             mEventSource.setVerbose(mVerbose);
             //set any of the factors that has been set
             for (int i = 0; i < MonkeySourceRandom.FACTORZ_COUNT; i++) {
@@ -709,13 +709,6 @@
                 }
             }
 
-            try {
-                Thread.sleep(mThrottle);
-            } catch (InterruptedException e1) {
-                System.out.println("** Monkey interrupted in sleep.");
-                return i;
-            }
-
             // In this debugging mode, we never send any events.  This is primarily
             // here so you can manually test the package or category limits, while manually
             // exercising the system.
@@ -730,7 +723,10 @@
 
             MonkeyEvent ev = mEventSource.getNextEvent();
             if (ev != null) {
-                i++;
+                // We don't want to count throttling as an event.
+                if (!(ev instanceof MonkeyThrottleEvent)) {
+                    i++;
+                }
                 int injectCode = ev.injectEvent(mWm, mAm, mVerbose);
                 if (injectCode == MonkeyEvent.INJECT_FAIL) {
                     if (ev instanceof MonkeyKeyEvent) {
diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeyEvent.java b/cmds/monkey/src/com/android/commands/monkey/MonkeyEvent.java
index ff99f5f..7176073 100644
--- a/cmds/monkey/src/com/android/commands/monkey/MonkeyEvent.java
+++ b/cmds/monkey/src/com/android/commands/monkey/MonkeyEvent.java
@@ -29,6 +29,7 @@
     public static final int EVENT_TYPE_TRACKBALL = 2;
     public static final int EVENT_TYPE_ACTIVITY = 3;
     public static final int EVENT_TYPE_FLIP = 4; // Keyboard flip
+    public static final int EVENT_TYPE_THROTTLE = 5;
         
     public static final int INJECT_SUCCESS = 1;
     public static final int INJECT_FAIL = 0;
diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java
index 3dbb575..902d8e8 100644
--- a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java
+++ b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandom.java
@@ -171,6 +171,7 @@
     private LinkedList<MonkeyEvent> mQ = new LinkedList<MonkeyEvent>();
     private Random mRandom;    
     private int mVerbose = 0;
+    private long mThrottle = 0;
 
     private boolean mKeyboardOpen = false;
 
@@ -185,7 +186,7 @@
         return KEY_NAMES[keycode];
     }
     
-    public MonkeySourceRandom(long seed, ArrayList<ComponentName> MainApps) {
+    public MonkeySourceRandom(long seed, ArrayList<ComponentName> MainApps, long throttle) {
         // default values for random distributions
         // note, these are straight percentages, to match user input (cmd line args)
         // but they will be converted to 0..1 values before the main loop runs.
@@ -202,6 +203,7 @@
         mRandom = new SecureRandom();
         mRandom.setSeed((seed == 0) ? -1 : seed);
         mMainApps = MainApps;
+        mThrottle = throttle;
     }
 
     /**
@@ -334,6 +336,7 @@
                 downAt, MotionEvent.ACTION_UP, x, y, 0);        
         e.setIntermediateNote(false);        
         mQ.addLast(e);
+        addThrottle();
     }
   
     /**
@@ -384,6 +387,7 @@
             e.setIntermediateNote(false);        
             mQ.addLast(e);
         }        
+        addThrottle();
     }
     
     /** 
@@ -416,11 +420,13 @@
             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);
@@ -431,6 +437,8 @@
         
         e = new MonkeyKeyEvent(KeyEvent.ACTION_UP, lastKey);
         mQ.addLast(e);
+        
+        addThrottle();
     }
     
     public boolean validate() {
@@ -464,4 +472,8 @@
         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/MonkeyThrottleEvent.java b/cmds/monkey/src/com/android/commands/monkey/MonkeyThrottleEvent.java
new file mode 100644
index 0000000..3d7d48a
--- /dev/null
+++ b/cmds/monkey/src/com/android/commands/monkey/MonkeyThrottleEvent.java
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2009 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 android.app.IActivityManager;
+import android.os.RemoteException;
+import android.os.SystemClock;
+import android.view.IWindowManager;
+import android.view.MotionEvent;
+
+
+/**
+ * monkey throttle event
+ */
+public class MonkeyThrottleEvent extends MonkeyEvent {
+    private long mThrottle; 
+        
+    public MonkeyThrottleEvent(int type, long throttle) {
+        super(type);
+        mThrottle = throttle;
+    }  
+
+    @Override
+    public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
+
+        if (verbose > 1) {
+            System.out.println("Sleeping for " + mThrottle + " milliseconds");
+        }
+        try {
+            Thread.sleep(mThrottle);
+        } catch (InterruptedException e1) {
+            System.out.println("** Monkey interrupted in sleep.");
+            return MonkeyEvent.INJECT_FAIL;
+        }
+        
+        return MonkeyEvent.INJECT_SUCCESS;
+    }
+}