Merge "New SDK architecture. DO NOT MERGE." into gingerbread
diff --git a/cmds/monkey/src/com/android/commands/monkey/Monkey.java b/cmds/monkey/src/com/android/commands/monkey/Monkey.java
index ea93492..ecbc9a1 100644
--- a/cmds/monkey/src/com/android/commands/monkey/Monkey.java
+++ b/cmds/monkey/src/com/android/commands/monkey/Monkey.java
@@ -25,6 +25,7 @@
 import android.content.pm.ResolveInfo;
 import android.os.Build;
 import android.os.Debug;
+import android.os.Environment;
 import android.os.Process;
 import android.os.RemoteException;
 import android.os.ServiceManager;
@@ -34,13 +35,16 @@
 import android.view.IWindowManager;
 
 import java.io.BufferedReader;
+import java.io.BufferedWriter;
 import java.io.ByteArrayInputStream;
 import java.io.DataInputStream;
 import java.io.File;
 import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.io.Writer;
 import java.security.SecureRandom;
 import java.util.ArrayList;
 import java.util.HashSet;
@@ -179,6 +183,10 @@
     /** Device idle time. This is for the scripted monkey. **/
     long mDeviceSleepTime = 30000;
 
+    boolean mRandomizeScript = false;
+
+    boolean mScriptLog = false;
+
     /** a filename to the setup script (if any) */
     private String mSetupFileName = null;
 
@@ -369,6 +377,20 @@
         }
     }
 
+    // Write the numbe of iteration to the log
+    private void writeScriptLog(int count) {
+        // TO DO: Add the script file name to the log.
+        try {
+            Writer output = new BufferedWriter(new FileWriter(new File(
+                    Environment.getExternalStorageDirectory(), "scriptlog.txt"), true));
+            output.write("iteration: " + count + " time: "
+                    + MonkeyUtils.toCalendarTime(System.currentTimeMillis()) + "\n");
+            output.close();
+        } catch (IOException e) {
+            System.err.println(e.toString());
+        }
+    }
+
     /**
      * Command-line entry point.
      *
@@ -471,12 +493,14 @@
             mCountEvents = false;
         } else if (mScriptFileNames != null && mScriptFileNames.size() > 1) {
             if (mSetupFileName != null) {
-                mEventSource = new MonkeySourceRandomScript(mSetupFileName, mScriptFileNames,
-                        mThrottle, mRandomizeThrottle, mRandom, mProfileWaitTime, mDeviceSleepTime);
+                mEventSource = new MonkeySourceRandomScript(mSetupFileName, 
+                        mScriptFileNames, mThrottle, mRandomizeThrottle, mRandom,
+                        mProfileWaitTime, mDeviceSleepTime, mRandomizeScript);
                 mCount++;
             } else {
-                mEventSource = new MonkeySourceRandomScript(mScriptFileNames, mThrottle,
-                        mRandomizeThrottle, mRandom, mProfileWaitTime, mDeviceSleepTime);
+                mEventSource = new MonkeySourceRandomScript(mScriptFileNames,
+                        mThrottle, mRandomizeThrottle, mRandom, 
+                        mProfileWaitTime, mDeviceSleepTime, mRandomizeScript);
             }
             mEventSource.setVerbose(mVerbose);
             mCountEvents = false;
@@ -665,6 +689,10 @@
                 } else if (opt.equals("--device-sleep-time")) {
                     mDeviceSleepTime = nextOptionLong("Device sleep time" +
                                                       "(in milliseconds)");
+                } else if (opt.equals("--randomize-script")) {
+                     mRandomizeScript = true;
+                } else if (opt.equals("--script-log")) {
+                    mScriptLog = true;
                 } else if (opt.equals("-h")) {
                     showUsage();
                     return false;
@@ -886,6 +914,7 @@
 
         boolean systemCrashed = false;
 
+        // TO DO : The count should apply to each of the script file.
         while (!systemCrashed && cycleCounter < mCount) {
             synchronized (this) {
                 if (mRequestProcRank) {
@@ -958,11 +987,13 @@
                     eventCounter++;
                     if (mCountEvents) {
                         cycleCounter++;
+                        writeScriptLog(cycleCounter);
                     }
                 }
             } else {
                 if (!mCountEvents) {
                     cycleCounter++;
+                    writeScriptLog(cycleCounter);
                 } else {
                     // Event Source has signaled that we have no more events to process
                     break;
@@ -1137,7 +1168,10 @@
         usage.append("              [--throttle MILLISEC] [--randomize-throttle]\n");
         usage.append("              [--profile-wait MILLISEC]\n");
         usage.append("              [--device-sleep-time MILLISEC]\n");
+        usage.append("              [--randomize-script]\n");
+        usage.append("              [--script-log]\n");
         usage.append("              COUNT\n");
         System.err.println(usage.toString());
     }
 }
+
diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandomScript.java b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandomScript.java
index 5e759cd..ded7bfc 100644
--- a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandomScript.java
+++ b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceRandomScript.java
@@ -38,6 +38,10 @@
     /** The random number generator */
     private Random mRandom;
 
+    private boolean mRandomizeScript = false;
+
+    private int mScriptCount = 0;
+
     /**
      * Creates a MonkeySourceRandomScript instance with an additional setup script.
      *
@@ -49,7 +53,7 @@
      */
     public MonkeySourceRandomScript(String setupFileName, ArrayList<String> scriptFileNames,
             long throttle, boolean randomizeThrottle, Random random, long profileWaitTime,
-            long deviceSleepTime) {
+            long deviceSleepTime, boolean randomizeScript) {
         if (setupFileName != null) {
             mSetupSource = new MonkeySourceScript(random, setupFileName, throttle,
                     randomizeThrottle, profileWaitTime, deviceSleepTime);
@@ -62,6 +66,7 @@
         }
 
         mRandom = random;
+        mRandomizeScript = randomizeScript;
     }
 
     /**
@@ -73,9 +78,10 @@
      * @param random The random number generator.
      */
     public MonkeySourceRandomScript(ArrayList<String> scriptFileNames, long throttle,
-            boolean randomizeThrottle, Random random, long profileWaitTime, long deviceSleepTime) {
+            boolean randomizeThrottle, Random random, long profileWaitTime, long deviceSleepTime,
+            boolean randomizeScript) {
         this(null, scriptFileNames, throttle, randomizeThrottle, random, profileWaitTime,
-                deviceSleepTime);
+                deviceSleepTime, randomizeScript);
     }
 
     /**
@@ -91,8 +97,13 @@
             int numSources = mScriptSources.size();
             if (numSources == 1) {
                 mCurrentSource = mScriptSources.get(0);
-            } else if (numSources > 1) {
-                mCurrentSource = mScriptSources.get(mRandom.nextInt(numSources));
+            } else if (numSources > 1 ) {
+                if (mRandomizeScript) {
+                    mCurrentSource = mScriptSources.get(mRandom.nextInt(numSources));
+                } else {
+                    mCurrentSource = mScriptSources.get(mScriptCount % numSources);
+                    mScriptCount++;
+                }
             }
         }