Skip to content

Commit 374c0b8

Browse files
committed
Add: hold_duration for drag method
1 parent 223ab7b commit 374c0b8

6 files changed

Lines changed: 53 additions & 12 deletions

File tree

module/device/control.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,27 +148,30 @@ def swipe_vector(self, vector, box=(123, 159, 1175, 628), random_range=(0, 0, 0,
148148
self.swipe(p1, p2, duration=duration, name=name, distance_check=distance_check)
149149

150150
def drag(self, p1, p2, segments=1, shake=(0, 15), point_random=(-10, -10, 10, 10), shake_random=(-5, -5, 5, 5),
151-
swipe_duration=0.25, shake_duration=0.1, name='DRAG'):
151+
swipe_duration=0.25, shake_duration=0.1, hold_duration=0.0, name='DRAG'):
152152
self.handle_control_check(name)
153153
p1, p2 = ensure_int(p1, p2)
154154
logger.info(
155155
'Drag %s -> %s' % (point2str(*p1), point2str(*p2))
156156
)
157157
method = self.config.Emulator_ControlMethod
158158
if method == 'minitouch':
159-
self.drag_minitouch(p1, p2, point_random=point_random)
159+
self.drag_minitouch(p1, p2, point_random=point_random, hold_duration=hold_duration)
160160
elif method == 'uiautomator2':
161161
self.drag_uiautomator2(
162162
p1, p2, segments=segments, shake=shake, point_random=point_random, shake_random=shake_random,
163-
swipe_duration=swipe_duration, shake_duration=shake_duration)
163+
swipe_duration=swipe_duration, shake_duration=shake_duration, hold_duration=hold_duration)
164164
elif method == 'scrcpy':
165-
self.drag_scrcpy(p1, p2, point_random=point_random)
165+
self.drag_scrcpy(p1, p2, point_random=point_random, hold_duration=hold_duration)
166166
elif method == 'MaaTouch':
167-
self.drag_maatouch(p1, p2, point_random=point_random)
167+
self.drag_maatouch(p1, p2, point_random=point_random, hold_duration=hold_duration)
168168
elif method == 'nemu_ipc':
169-
self.drag_nemu_ipc(p1, p2, point_random=point_random)
169+
self.drag_nemu_ipc(p1, p2, point_random=point_random, hold_duration=hold_duration)
170170
else:
171171
logger.warning(f'Control method {method} does not support drag well, '
172172
f'falling back to ADB swipe may cause unexpected behaviour')
173173
self.swipe_adb(p1, p2, duration=ensure_time(swipe_duration * 2))
174+
hold_duration = ensure_time(hold_duration)
175+
if hold_duration > 0:
176+
self.sleep(hold_duration)
174177
self.click(Button(area=(), color=(), button=area_offset(point_random, p2), name=name), False)

module/device/method/maatouch.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def swipe_maatouch(self, p1, p2):
367367
builder.send_sync()
368368

369369
@retry
370-
def drag_maatouch(self, p1, p2, point_random=(-10, -10, 10, 10)):
370+
def drag_maatouch(self, p1, p2, point_random=(-10, -10, 10, 10), hold_duration=0.0):
371371
p1 = np.array(p1) - random_rectangle_point(point_random)
372372
p2 = np.array(p2) - random_rectangle_point(point_random)
373373
points = insert_swipe(p0=p1, p3=p2, speed=20)
@@ -384,6 +384,12 @@ def drag_maatouch(self, p1, p2, point_random=(-10, -10, 10, 10)):
384384
builder.move(*p2).commit().wait(140)
385385
builder.send_sync()
386386

387+
hold_duration = ensure_time(hold_duration) - 0.28
388+
hold_ms = int(max(0.0, hold_duration) * 1000)
389+
if hold_ms > 0:
390+
builder.move(*p2).commit().wait(hold_ms)
391+
builder.send_sync()
392+
387393
builder.up().commit()
388394
builder.send_sync()
389395

module/device/method/minitouch.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ def swipe_minitouch(self, p1, p2):
694694
builder.send()
695695

696696
@retry
697-
def drag_minitouch(self, p1, p2, point_random=(-10, -10, 10, 10)):
697+
def drag_minitouch(self, p1, p2, point_random=(-10, -10, 10, 10), hold_duration=0.0):
698698
p1 = np.array(p1) - random_rectangle_point(point_random)
699699
p2 = np.array(p2) - random_rectangle_point(point_random)
700700
points = insert_swipe(p0=p1, p3=p2, speed=20)
@@ -711,5 +711,11 @@ def drag_minitouch(self, p1, p2, point_random=(-10, -10, 10, 10)):
711711
builder.move(*p2).commit().wait(140)
712712
builder.send()
713713

714+
hold_duration = ensure_time(hold_duration) - 0.28
715+
hold_ms = int(max(0.0, hold_duration) * 1000)
716+
if hold_ms > 0:
717+
builder.move(*p2).commit().wait(hold_ms)
718+
builder.send()
719+
714720
builder.up().commit()
715721
builder.send()

module/device/method/nemu_ipc.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def swipe_nemu_ipc(self, p1, p2):
622622
self.nemu_ipc.up()
623623
self.sleep(0.050)
624624

625-
def drag_nemu_ipc(self, p1, p2, point_random=(-10, -10, 10, 10)):
625+
def drag_nemu_ipc(self, p1, p2, point_random=(-10, -10, 10, 10), hold_duration=0.0):
626626
p1 = np.array(p1) - random_rectangle_point(point_random)
627627
p2 = np.array(p2) - random_rectangle_point(point_random)
628628
points = insert_swipe(p0=p1, p3=p2, speed=20)
@@ -636,5 +636,10 @@ def drag_nemu_ipc(self, p1, p2, point_random=(-10, -10, 10, 10)):
636636
self.nemu_ipc.down(*p2)
637637
self.sleep(0.140)
638638

639+
hold_duration = ensure_time(hold_duration) - 0.28
640+
if hold_duration > 0:
641+
self.nemu_ipc.down(*p2)
642+
self.sleep(hold_duration)
643+
639644
self.nemu_ipc.up()
640645
self.sleep(0.050)

module/device/method/scrcpy/scrcpy.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from adbutils.errors import AdbError, AdbTimeout
77

88
import module.device.method.scrcpy.const as const
9-
from module.base.utils import random_rectangle_point
9+
from module.base.utils import ensure_time, random_rectangle_point
1010
from module.device.method.minitouch import insert_swipe
1111
from module.device.method.scrcpy.core import ScrcpyCore, ScrcpyError
1212
from module.device.method.uiautomator_2 import Uiautomator2
@@ -143,7 +143,7 @@ def swipe_scrcpy(self, p1, p2):
143143
self.sleep(0.05)
144144

145145
@retry
146-
def drag_scrcpy(self, p1, p2, point_random=(-10, -10, 10, 10)):
146+
def drag_scrcpy(self, p1, p2, point_random=(-10, -10, 10, 10), hold_duration=0.0):
147147
self.scrcpy_ensure_running()
148148

149149
with self._scrcpy_control_socket_lock:
@@ -162,5 +162,17 @@ def drag_scrcpy(self, p1, p2, point_random=(-10, -10, 10, 10)):
162162
self._scrcpy_control.touch(*p2, const.ACTION_MOVE)
163163
self.sleep(0.002)
164164

165+
hold_duration = ensure_time(hold_duration) - 0.28
166+
if hold_duration > 0:
167+
step = 0.002
168+
repeats = int(hold_duration // step)
169+
for _ in range(repeats):
170+
self._scrcpy_control.touch(*p2, const.ACTION_MOVE)
171+
self.sleep(step)
172+
remainder = hold_duration - (repeats * step)
173+
if remainder > 0:
174+
self._scrcpy_control.touch(*p2, const.ACTION_MOVE)
175+
self.sleep(remainder)
176+
165177
self._scrcpy_control.touch(*p2, const.ACTION_UP)
166178
self.sleep(0.05)

module/device/method/uiautomator_2.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ def _drag_along(self, path):
187187
self.sleep(second)
188188

189189
def drag_uiautomator2(self, p1, p2, segments=1, shake=(0, 15), point_random=(-10, -10, 10, 10),
190-
shake_random=(-5, -5, 5, 5), swipe_duration=0.25, shake_duration=0.1):
190+
shake_random=(-5, -5, 5, 5), swipe_duration=0.25, shake_duration=0.1,
191+
hold_duration=0.0):
191192
"""Drag and shake, like:
192193
/\
193194
+-----------+ + +
@@ -204,6 +205,7 @@ def drag_uiautomator2(self, p1, p2, segments=1, shake=(0, 15), point_random=(-10
204205
shake_random: Add random to shake array.
205206
swipe_duration: Duration between way points.
206207
shake_duration: Duration between shake points.
208+
hold_duration: Hold time before release.
207209
"""
208210
p1 = np.array(p1) - random_rectangle_point(point_random)
209211
p2 = np.array(p2) - random_rectangle_point(point_random)
@@ -213,6 +215,13 @@ def drag_uiautomator2(self, p1, p2, segments=1, shake=(0, 15), point_random=(-10
213215
(*p2 - shake - random_rectangle_point(shake_random), shake_duration),
214216
(*p2, shake_duration)
215217
]
218+
internal_hold = ensure_time(shake_duration) * 3
219+
hold_duration = ensure_time(hold_duration) - internal_hold
220+
if hold_duration > 0:
221+
path += [
222+
(*p2, hold_duration),
223+
(*p2, 0),
224+
]
216225
path = [(int(x), int(y), d) for x, y, d in path]
217226
self._drag_along(path)
218227

0 commit comments

Comments
 (0)