# TouchLongClick
~~~
package io.appium.android.bootstrap.handler;
import android.os.SystemClock;
import com.android.uiautomator.common.ReflectionUtils;
import com.android.uiautomator.core.UiObjectNotFoundException;
import io.appium.android.bootstrap.Logger;
import java.lang.reflect.Method;
/**
* This handler is used to long click elements in the Android UI.
*
*/
public class TouchLongClick extends TouchEvent {
/*
* UiAutomator has a broken longClick, so we'll try to implement it using the
* touchDown / touchUp events.
*/
private boolean correctLongClick(final int x, final int y, final int duration) {
try {
/*
* bridge.getClass() returns ShellUiAutomatorBridge on API 18/19 so use
* the super class.
*/
final ReflectionUtils utils = new ReflectionUtils();
final Method touchDown = utils.getControllerMethod("touchDown", int.class,
int.class);
final Method touchUp = utils.getControllerMethod("touchUp", int.class, int.class);
if ((Boolean) touchDown.invoke(utils.getController(), x, y)) {
SystemClock.sleep(duration);
if ((Boolean) touchUp.invoke(utils.getController(), x, y)) {
return true;
}
}
return false;
} catch (final Exception e) {
Logger.debug("Problem invoking correct long click: " + e);
return false;
}
}
@Override
protected boolean executeTouchEvent() throws UiObjectNotFoundException {
final Object paramDuration = params.get("duration");
int duration = 2000; // two seconds
if (paramDuration != null) {
duration = Integer.parseInt(paramDuration.toString());
}
printEventDebugLine("TouchLongClick", duration);
if (correctLongClick(clickX, clickY, duration)) {
return true;
}
// if correctLongClick failed and we have an element
// then uiautomator's longClick is used as a fallback.
if (isElement) {
Logger.debug("Falling back to broken longClick");
return el.longClick();
}
return false;
}
}
~~~
TouchLongClick類繼承于TouchEvent,而TouchEvent繼承于CommandHandler.調用TouchEvent的execute的方法中,調用了executeTouchEvent方法,所以我們來看上面的executeTouchEvent就好了,執行長點擊事件,在uiautomator里有UiObject.longClick()方法,但是寫過case的人知道,有時候這個方法達不到我們的需求,但是我們是自己了反射調用TouchDown和TouchUp兩個個方法,而在appium里幫你解決了,它自己就幫你做到了這一點,如果你傳入到是控件對象,那無可厚非,還是調用UiObject.longClick方法,如果你想根據坐標,時間在點擊的話,那么就調用currectLongClick這個appium給你封裝好的方法。
~~~
final ReflectionUtils utils = new ReflectionUtils();
final Method touchDown = utils.getControllerMethod("touchDown", int.class,
int.class);
final Method touchUp = utils.getControllerMethod("touchUp", int.class, int.class);
~~~
通過反射得到uiautomator里的沒有公開的類,從而我們想要的方法touchDown和touchUp.
~~~
public ReflectionUtils() throws IllegalArgumentException,
IllegalAccessException, SecurityException, NoSuchFieldException {
final UiDevice device = UiDevice.getInstance();
final Object bridge = enableField(device.getClass(), "mUiAutomationBridge")
.get(device);
if (API_18) {
controller = enableField(bridge.getClass().getSuperclass(),
"mInteractionController").get(bridge);
} else {
controller = enableField(bridge.getClass(), "mInteractionController")
.get(bridge);
}
}
~~~
因為uiautomator api的改動,在api18以上的版本中,mInteractionController是存在于UiAutomationBridge的父類中的變量,而在18以下的版本中它是存在于本類中的。所以反射時會有一點點小小點差異,但總的來說都是要獲得InteractionController這個類,因為這個類里面存在有我們要但touch類但方法。最后我們就能輕松調用鼠標的TouchUp和TouchDown他們啦。然后再加上時間,長按就實現啦。
# TouchUp
~~~
package io.appium.android.bootstrap.handler;
import com.android.uiautomator.common.ReflectionUtils;
import com.android.uiautomator.core.UiObjectNotFoundException;
import io.appium.android.bootstrap.Logger;
import java.lang.reflect.Method;
/**
* This handler is used to perform a touchDown event on an element in the
* Android UI.
*
*/
public class TouchDown extends TouchEvent {
@Override
protected boolean executeTouchEvent() throws UiObjectNotFoundException {
printEventDebugLine("TouchDown");
try {
final ReflectionUtils utils = new ReflectionUtils();
final Method touchDown = utils.getControllerMethod("touchDown", int.class,
int.class);
return (Boolean) touchDown.invoke(utils.getController(), clickX, clickY);
} catch (final Exception e) {
Logger.debug("Problem invoking touchDown: " + e);
return false;
}
}
}
~~~
有了上面的分析,對TouchUp和TouchDown還有TouchMove的分析就不用再多說了,都是反射的原理
# TouchDown
類同
# TouchMove
類同
- 前言
- appium框架之bootstrap
- bootstrap之Click事件
- bootstrap之WaitForIdle&&Clear
- bootstrap之Orientation
- bootstrap之Swipe
- bootstrap之Flick
- bootstrap之Drag
- bootstrap之Pinch
- bootstrap之鼠標操作
- bootstrap之文本框的操作
- bootstrap之GetName&&GetAttribute&&GetDeviceSize&&GetSize&&GetLocation&&GetDataDir
- bootstrap之ScrollTo
- bootstrap之Wake&&PressBack&&TakeScreenshot&&OpenNotification
- bootstrap之PressKeyCode&&LongPressKeyCode
- bootstrap之DumpWindowHierarchy
- bootstrap之UpdateStrings
- bootstrap之MultiPointerGesture