Add the monkey event which allows the scripted monkey to execute the shell command.

Change-Id: Id5548fc42fc5c7bee6879fffc580e134a75572ab
diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeyCommandEvent.java b/cmds/monkey/src/com/android/commands/monkey/MonkeyCommandEvent.java
new file mode 100644
index 0000000..2122cce
--- /dev/null
+++ b/cmds/monkey/src/com/android/commands/monkey/MonkeyCommandEvent.java
@@ -0,0 +1,51 @@
+/*
+ * Copyright (C) 2010 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.view.IWindowManager;
+import android.os.Build;
+
+
+/**
+ * Events for running the shell command.
+ */
+public class MonkeyCommandEvent extends MonkeyEvent {
+
+    private String mCmd;
+
+    public MonkeyCommandEvent(String cmd) {
+        super(EVENT_TYPE_ACTIVITY);
+        mCmd = cmd;
+    }
+
+    @Override
+    public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
+        if (mCmd != null) {
+            //Execute the shell command
+            try {
+                java.lang.Process p = Runtime.getRuntime().exec(mCmd);
+                int status = p.waitFor();
+                System.err.println("// Shell command " + mCmd + " status was " + status);
+            } catch (Exception e) {
+                System.err.println("// Exception from " + mCmd + ":");
+                System.err.println(e.toString());
+            }
+        }
+        return MonkeyEvent.INJECT_SUCCESS;
+    }
+}
diff --git a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java
index a1d1f29..09d247d 100644
--- a/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java
+++ b/cmds/monkey/src/com/android/commands/monkey/MonkeySourceScript.java
@@ -102,6 +102,8 @@
 
     private static final String EVENT_KEYWORD_WRITEPOWERLOG = "WriteLog";
 
+    private static final String EVENT_KEYWORD_RUNCMD = "RunCmd";
+
     // a line at the end of the header
     private static final String STARTING_DATA_LINE = "start data >>";
 
@@ -355,6 +357,13 @@
             MonkeyPowerEvent e = new MonkeyPowerEvent();
             mQ.addLast(e);
         }
+
+      //Run the shell command
+        if (s.indexOf(EVENT_KEYWORD_RUNCMD) >= 0 && args.length == 1) {
+            String cmd = args[0];
+            MonkeyCommandEvent e = new MonkeyCommandEvent(cmd);
+            mQ.addLast(e);
+        }
     }
 
     /**