am 4bff8ab0: Merge change 20840 into donut
Merge commit '4bff8ab0e9073ae537e8a6c4ee8240a6698384f8' * commit '4bff8ab0e9073ae537e8a6c4ee8240a6698384f8': Add "type" command to allow strings to be more easily typed.
This commit is contained in:
@@ -87,6 +87,11 @@ The press command is a shortcut for the key command. The keycode
|
|||||||
paramter works just like the key command and will automatically send
|
paramter works just like the key command and will automatically send
|
||||||
both the up and the down event.
|
both the up and the down event.
|
||||||
|
|
||||||
|
type string
|
||||||
|
|
||||||
|
This command will simulate a user typing the given string on the
|
||||||
|
keyboard by generating the proper KeyEvents.
|
||||||
|
|
||||||
OTHER NOTES
|
OTHER NOTES
|
||||||
|
|
||||||
There are some convenience features added to allow running without
|
There are some convenience features added to allow running without
|
||||||
|
|||||||
@@ -33,12 +33,19 @@ public class MonkeyKeyEvent extends MonkeyEvent {
|
|||||||
private int mDeviceId = -1;
|
private int mDeviceId = -1;
|
||||||
private long mEventTime = -1;
|
private long mEventTime = -1;
|
||||||
|
|
||||||
|
private KeyEvent keyEvent = null;
|
||||||
|
|
||||||
public MonkeyKeyEvent(int action, int keycode) {
|
public MonkeyKeyEvent(int action, int keycode) {
|
||||||
super(EVENT_TYPE_KEY);
|
super(EVENT_TYPE_KEY);
|
||||||
mAction = action;
|
mAction = action;
|
||||||
mKeyCode = keycode;
|
mKeyCode = keycode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public MonkeyKeyEvent(KeyEvent e) {
|
||||||
|
super(EVENT_TYPE_KEY);
|
||||||
|
keyEvent = e;
|
||||||
|
}
|
||||||
|
|
||||||
public MonkeyKeyEvent(long downTime, long eventTime, int action,
|
public MonkeyKeyEvent(long downTime, long eventTime, int action,
|
||||||
int code, int repeat, int metaState,
|
int code, int repeat, int metaState,
|
||||||
int device, int scancode) {
|
int device, int scancode) {
|
||||||
@@ -82,13 +89,16 @@ public class MonkeyKeyEvent extends MonkeyEvent {
|
|||||||
* @return the key event
|
* @return the key event
|
||||||
*/
|
*/
|
||||||
private KeyEvent getEvent() {
|
private KeyEvent getEvent() {
|
||||||
if (mDeviceId < 0) {
|
if (keyEvent == null) {
|
||||||
return new KeyEvent(mAction, mKeyCode);
|
if (mDeviceId < 0) {
|
||||||
|
keyEvent = new KeyEvent(mAction, mKeyCode);
|
||||||
|
} else {
|
||||||
|
// for scripts
|
||||||
|
keyEvent = new KeyEvent(mDownTime, mEventTime, mAction,
|
||||||
|
mKeyCode, mRepeatCount, mMetaState, mDeviceId, mScancode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
return keyEvent;
|
||||||
// for scripts
|
|
||||||
return new KeyEvent(mDownTime, mEventTime, mAction,
|
|
||||||
mKeyCode, mRepeatCount, mMetaState, mDeviceId, mScancode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import android.os.RemoteException;
|
|||||||
import android.os.ServiceManager;
|
import android.os.ServiceManager;
|
||||||
import android.os.SystemClock;
|
import android.os.SystemClock;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.view.KeyCharacterMap;
|
||||||
import android.view.KeyEvent;
|
import android.view.KeyEvent;
|
||||||
import android.view.MotionEvent;
|
import android.view.MotionEvent;
|
||||||
|
|
||||||
@@ -217,7 +218,7 @@ public class MonkeySourceNetwork implements MonkeyEventSource {
|
|||||||
try {
|
try {
|
||||||
sleep = Integer.parseInt(sleepStr);
|
sleep = Integer.parseInt(sleepStr);
|
||||||
} catch (NumberFormatException e) {
|
} catch (NumberFormatException e) {
|
||||||
Log.e(TAG, "Not a number: " + sleepStr, e);
|
Log.e(TAG, "Not a number: " + sleepStr, e);
|
||||||
}
|
}
|
||||||
return new MonkeyThrottleEvent(sleep);
|
return new MonkeyThrottleEvent(sleep);
|
||||||
}
|
}
|
||||||
@@ -225,6 +226,34 @@ public class MonkeySourceNetwork implements MonkeyEventSource {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Command to type a string
|
||||||
|
*/
|
||||||
|
private static class TypeCommand implements MonkeyCommand {
|
||||||
|
// wake
|
||||||
|
public MonkeyEvent translateCommand(List<String> command,
|
||||||
|
CommandQueue queue) {
|
||||||
|
if (command.size() == 2) {
|
||||||
|
String str = command.get(1);
|
||||||
|
|
||||||
|
char[] chars = str.toString().toCharArray();
|
||||||
|
|
||||||
|
// Convert the string to an array of KeyEvent's for
|
||||||
|
// the built in keymap.
|
||||||
|
KeyCharacterMap keyCharacterMap = KeyCharacterMap.
|
||||||
|
load(KeyCharacterMap.BUILT_IN_KEYBOARD);
|
||||||
|
KeyEvent[] events = keyCharacterMap.getEvents(chars);
|
||||||
|
|
||||||
|
// enqueue all the events we just got.
|
||||||
|
for (KeyEvent event : events) {
|
||||||
|
queue.enqueueEvent(new MonkeyKeyEvent(event));
|
||||||
|
}
|
||||||
|
return new MonkeyNoopEvent();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Command to wake the device up
|
* Command to wake the device up
|
||||||
*/
|
*/
|
||||||
@@ -325,6 +354,7 @@ public class MonkeySourceNetwork implements MonkeyEventSource {
|
|||||||
COMMAND_MAP.put("wake", new WakeCommand());
|
COMMAND_MAP.put("wake", new WakeCommand());
|
||||||
COMMAND_MAP.put("tap", new TapCommand());
|
COMMAND_MAP.put("tap", new TapCommand());
|
||||||
COMMAND_MAP.put("press", new PressCommand());
|
COMMAND_MAP.put("press", new PressCommand());
|
||||||
|
COMMAND_MAP.put("type", new TypeCommand());
|
||||||
}
|
}
|
||||||
|
|
||||||
// QUIT command
|
// QUIT command
|
||||||
@@ -400,6 +430,17 @@ public class MonkeySourceNetwork implements MonkeyEventSource {
|
|||||||
output = new PrintWriter(s.getOutputStream(), true);
|
output = new PrintWriter(s.getOutputStream(), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper function for commandLineSplit that replaces quoted
|
||||||
|
* charaters with their real values.
|
||||||
|
*
|
||||||
|
* @param input the string to do replacement on.
|
||||||
|
* @returns the results with the characters replaced.
|
||||||
|
*/
|
||||||
|
private static String replaceQuotedChars(String input) {
|
||||||
|
return input.replace("\\\"", "\"");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function splits the given line into String parts. It obey's quoted
|
* This function splits the given line into String parts. It obey's quoted
|
||||||
* strings and returns them as a single part.
|
* strings and returns them as a single part.
|
||||||
@@ -420,22 +461,22 @@ public class MonkeySourceNetwork implements MonkeyEventSource {
|
|||||||
String cur = tok.nextToken();
|
String cur = tok.nextToken();
|
||||||
if (!insideQuote && cur.startsWith("\"")) {
|
if (!insideQuote && cur.startsWith("\"")) {
|
||||||
// begin quote
|
// begin quote
|
||||||
quotedWord.append(cur);
|
quotedWord.append(replaceQuotedChars(cur));
|
||||||
insideQuote = true;
|
insideQuote = true;
|
||||||
} else if (insideQuote) {
|
} else if (insideQuote) {
|
||||||
// end quote
|
// end quote
|
||||||
if (cur.endsWith("\"")) {
|
if (cur.endsWith("\"")) {
|
||||||
insideQuote = false;
|
insideQuote = false;
|
||||||
quotedWord.append(cur);
|
quotedWord.append(" ").append(replaceQuotedChars(cur));
|
||||||
String word = quotedWord.toString();
|
String word = quotedWord.toString();
|
||||||
|
|
||||||
// trim off the quotes
|
// trim off the quotes
|
||||||
result.add(word.substring(1, word.length() - 1));
|
result.add(word.substring(1, word.length() - 1));
|
||||||
} else {
|
} else {
|
||||||
quotedWord.append(cur);
|
quotedWord.append(" ").append(replaceQuotedChars(cur));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
result.add(cur);
|
result.add(replaceQuotedChars(cur));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
Reference in New Issue
Block a user