Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2673 from paymand/long_press_keycode
Renamed keyevent to press_keycode and added long_press_keycode.
- Loading branch information
Showing
15 changed files
with
229 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
lib/devices/android/bootstrap/src/io/appium/android/bootstrap/handler/LongPressKeyCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package io.appium.android.bootstrap.handler; | ||
|
||
import io.appium.android.bootstrap.AndroidCommand; | ||
import io.appium.android.bootstrap.AndroidCommandResult; | ||
import io.appium.android.bootstrap.CommandHandler; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Hashtable; | ||
|
||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import android.os.SystemClock; | ||
import android.view.InputDevice; | ||
import android.view.InputEvent; | ||
import android.view.KeyCharacterMap; | ||
import android.view.KeyEvent; | ||
|
||
import com.android.uiautomator.common.ReflectionUtils; | ||
|
||
/** | ||
* This handler is used to LongPressKeyCode. | ||
* | ||
*/ | ||
public class LongPressKeyCode extends CommandHandler { | ||
public Integer keyCode; | ||
|
||
public Integer metaState; | ||
|
||
/* | ||
* @param command The {@link AndroidCommand} used for this handler. | ||
* | ||
* @return {@link AndroidCommandResult} | ||
* | ||
* @throws JSONException | ||
* | ||
* @see io.appium.android.bootstrap.CommandHandler#execute(io.appium.android. | ||
* bootstrap.AndroidCommand) | ||
*/ | ||
@Override | ||
public AndroidCommandResult execute(final AndroidCommand command) | ||
throws JSONException { | ||
try { | ||
final ReflectionUtils utils = new ReflectionUtils(); | ||
final Method injectEventSync = utils.getMethod("injectEventSync", | ||
InputEvent.class); | ||
final Hashtable<String, Object> params = command.params(); | ||
keyCode = (Integer) params.get("keycode"); | ||
metaState = params.get("metastate") != JSONObject.NULL ? (Integer) params | ||
.get("metastate") : 0; | ||
final long eventTime = SystemClock.uptimeMillis(); | ||
// Send an initial down event | ||
final KeyEvent downEvent = new KeyEvent(eventTime, eventTime, | ||
KeyEvent.ACTION_DOWN, keyCode, 0, metaState, | ||
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD); | ||
if ((Boolean) injectEventSync.invoke(utils.getController(), downEvent)) { | ||
// Send a repeat event. This will cause the FLAG_LONG_PRESS to be set. | ||
final KeyEvent repeatEvent = KeyEvent.changeTimeRepeat(downEvent, | ||
eventTime, 1); | ||
injectEventSync.invoke(utils.getController(), repeatEvent); | ||
// Finally, send the up event | ||
final KeyEvent upEvent = new KeyEvent(eventTime, eventTime, | ||
KeyEvent.ACTION_UP, keyCode, 0, metaState, | ||
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0, InputDevice.SOURCE_KEYBOARD); | ||
injectEventSync.invoke(utils.getController(), upEvent); | ||
} | ||
return getSuccessResult(true); | ||
} catch (final Exception e) { | ||
return getErrorResult(e.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.