Skip to content

Commit

Permalink
Simplify up/down actions balance calculation for W3C actions (appium…
Browse files Browse the repository at this point in the history
  • Loading branch information
Mykola Mokhnach authored and rajdeepv committed Jan 13, 2019
1 parent b4eb9b8 commit aa2d039
Showing 1 changed file with 33 additions and 46 deletions.
79 changes: 33 additions & 46 deletions app/src/main/java/io/appium/uiautomator2/handler/W3CActions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.appium.uiautomator2.handler;

import android.os.SystemClock;
import android.util.Log;
import android.util.LongSparseArray;
import android.view.KeyCharacterMap;
import android.view.KeyEvent;
Expand Down Expand Up @@ -54,7 +53,6 @@
import static io.appium.uiautomator2.utils.w3c.ActionsHelpers.toolTypeToInputSource;

public class W3CActions extends SafeRequestHandler {
private static final String TAG = W3CActions.class.getSimpleName();
private static final KeyCharacterMap kcm = KeyCharacterMap.load(KeyCharacterMap.VIRTUAL_KEYBOARD);

private static final List<Integer> HOVERING_ACTIONS = Arrays.asList(
Expand All @@ -75,7 +73,7 @@ private static PointerProperties[] filterPointerProperties(
result.add(eventParams.properties);
}
}
return result.toArray(new PointerProperties[result.size()]);
return result.toArray(new PointerProperties[0]);
}

private static PointerCoords[] filterPointerCoordinates(
Expand All @@ -88,7 +86,7 @@ private static PointerCoords[] filterPointerCoordinates(
result.add(eventParams.coordinates);
}
}
return result.toArray(new PointerCoords[result.size()]);
return result.toArray(new PointerCoords[0]);
}

/**
Expand Down Expand Up @@ -154,12 +152,13 @@ private boolean injectKeyEvent(KeyInputEventParams eventParam, long startTimesta
boolean result = true;
for (KeyEvent event : events) {
if (event.getAction() == keyAction) {
Logger.debug(String.format("Generating KeyEvent for keyAction '%s', keyCode: '%s', metaState: '%s'",
keyAction, keyCode, metaKeysToState(depressedMetaKeys)));
result &= injectEventSync(new KeyEvent(startTimestamp + eventParam.startDelta,
final KeyEvent keyEvent = new KeyEvent(startTimestamp + eventParam.startDelta,
SystemClock.uptimeMillis(), keyAction, event.getKeyCode(), 0,
event.getMetaState() | metaKeysToState(depressedMetaKeys),
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0));
KeyCharacterMap.VIRTUAL_KEYBOARD, 0, 0);
result &= injectEventSync(keyEvent);
Logger.debug(String.format("[%s (%s)] Synthesized: %s", startTimestamp, result ? "success" : "fail",
keyEvent.toString()));
}
}
return result;
Expand Down Expand Up @@ -194,7 +193,6 @@ private boolean executeActions(final JSONArray actions) throws JSONException {
boolean result = true;
final Set<Integer> depressedMetaKeys = new HashSet<>();
final long startTimestamp = SystemClock.uptimeMillis();
final LongSparseArray<Integer> motionEventsBalanceByInputSource = new LongSparseArray<>();
for (final Long currentTimeDelta : allDeltas) {
final List<InputEventParams> eventParams = inputEventsMapping.get(currentTimeDelta);
final LongSparseArray<List<MotionInputEventParams>> motionParamsByInputSource = new LongSparseArray<>();
Expand All @@ -203,9 +201,9 @@ private boolean executeActions(final JSONArray actions) throws JSONException {
result &= injectKeyEvent((KeyInputEventParams) eventParam, startTimestamp, depressedMetaKeys);
} else if (eventParam instanceof MotionInputEventParams) {
final int inputSource = toolTypeToInputSource(((MotionInputEventParams) eventParam).properties.toolType);
final List<MotionInputEventParams> events = (motionParamsByInputSource.get(inputSource) == null) ?
new ArrayList<MotionInputEventParams>() :
motionParamsByInputSource.get(inputSource);
final List<MotionInputEventParams> events = (motionParamsByInputSource.get(inputSource) == null)
? new ArrayList<MotionInputEventParams>()
: motionParamsByInputSource.get(inputSource);
events.add((MotionInputEventParams) eventParam);
motionParamsByInputSource.put(inputSource, events);
}
Expand All @@ -221,61 +219,50 @@ private boolean executeActions(final JSONArray actions) throws JSONException {

for (final MotionInputEventParams motionEventParams : motionEventsParams) {
final int actionCode = motionEventParams.actionCode;
Integer upDownBalance = motionEventsBalanceByInputSource.get(inputSource);
if (upDownBalance == null) {
upDownBalance = 0;
}
MotionEvent synthesizedEvent = null;
switch (actionCode) {
case MotionEvent.ACTION_DOWN: {
++upDownBalance;
motionEventsBalanceByInputSource.put(inputSource, upDownBalance);
final int action = upDownBalance == 1 ? MotionEvent.ACTION_DOWN :
getPointerAction(MotionEvent.ACTION_POINTER_DOWN, upDownBalance - 1);
result &= injectEventSync(MotionEvent.obtain(startTimestamp + motionEventParams.startDelta,
SystemClock.uptimeMillis(), action,
action == MotionEvent.ACTION_DOWN ? 1 : upDownBalance, nonHoveringProps, nonHoveringCoords,
final int action = nonHoveringProps.length <= 1
? MotionEvent.ACTION_DOWN
: getPointerAction(MotionEvent.ACTION_POINTER_DOWN, nonHoveringProps.length - 1);
synthesizedEvent = MotionEvent.obtain(startTimestamp + motionEventParams.startDelta,
SystemClock.uptimeMillis(), action, nonHoveringProps.length, nonHoveringProps, nonHoveringCoords,
metaKeysToState(depressedMetaKeys), motionEventParams.button,
1, 1, 0, 0, inputSource, 0));
Log.d(TAG, String.format("Generated MotionEvent for action '%s'", action));
1, 1, 0, 0, inputSource, 0);
}
break;
case MotionEvent.ACTION_UP: {
if (upDownBalance <= 0) {
// ignore unbalanced pointer up actions
break;
}
motionEventsBalanceByInputSource.put(inputSource, upDownBalance);
final int action = upDownBalance <= 1 ? MotionEvent.ACTION_UP :
getPointerAction(MotionEvent.ACTION_POINTER_UP, upDownBalance - 1);
result &= injectEventSync(MotionEvent.obtain(startTimestamp + motionEventParams.startDelta,
SystemClock.uptimeMillis(), action, action == MotionEvent.ACTION_UP ? 1 : upDownBalance,
final int action = nonHoveringProps.length <= 1
? MotionEvent.ACTION_UP
: getPointerAction(MotionEvent.ACTION_POINTER_UP, nonHoveringProps.length - 1);
synthesizedEvent = MotionEvent.obtain(startTimestamp + motionEventParams.startDelta,
SystemClock.uptimeMillis(), action, nonHoveringProps.length,
nonHoveringProps, nonHoveringCoords, metaKeysToState(depressedMetaKeys), motionEventParams.button,
1, 1, 0, 0, inputSource, 0));
if (upDownBalance > 0) {
--upDownBalance;
}
Log.d(TAG, String.format("Generated MotionEvent for action '%s'", action));
1, 1, 0, 0, inputSource, 0);
}
break;
case MotionEvent.ACTION_MOVE: {
result &= injectEventSync(MotionEvent.obtain(startTimestamp + motionEventParams.startDelta,
SystemClock.uptimeMillis(), actionCode, upDownBalance,
synthesizedEvent = MotionEvent.obtain(startTimestamp + motionEventParams.startDelta,
SystemClock.uptimeMillis(), actionCode, nonHoveringProps.length,
nonHoveringProps, nonHoveringCoords, metaKeysToState(depressedMetaKeys),
motionEventParams.button, 1, 1, 0, 0, inputSource, 0)
);
motionEventParams.button, 1, 1, 0, 0, inputSource, 0);
}
break;
case MotionEvent.ACTION_HOVER_ENTER:
case MotionEvent.ACTION_HOVER_EXIT:
case MotionEvent.ACTION_HOVER_MOVE: {
result &= injectEventSync(MotionEvent.obtain(startTimestamp + motionEventParams.startDelta,
synthesizedEvent = MotionEvent.obtain(startTimestamp + motionEventParams.startDelta,
SystemClock.uptimeMillis(), actionCode, hoveringProps.length,
hoveringProps, hoveringCoords, metaKeysToState(depressedMetaKeys),
0, 1, 1, 0, 0, inputSource, 0)
);
0, 1, 1, 0, 0, inputSource, 0);
}
break;
} // switch
if (synthesizedEvent != null) {
result &= injectEventSync(synthesizedEvent);
Logger.debug(String.format("[%s (%s)] Synthesized %s", currentTimeDelta, result ? "success" : "fail",
synthesizedEvent.toString()));
}
} // motionEventParams : motionEventsParams
} // for i < motionParamsByInputSource.size()
SystemClock.sleep(currentTimeDelta - recentTimeDelta);
Expand Down

0 comments on commit aa2d039

Please sign in to comment.