-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdevice_manipulations.py
138 lines (107 loc) · 4 KB
/
device_manipulations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
from py_adb.adb import ADB
from py_adb.files import Files
from py_adb.device_info import DeviceInfo
from py_adb.android_keyevent import AndroidKeyevent
from enum import Enum, auto
import time
class UnlockType(Enum):
SWIPE = auto()
class DeviceManipulations:
@staticmethod
def set_screen_brightness(dev_id: str, value):
"""
Method to change screen brightness on the device in range - 0 to 255
"""
if value > 255:
value = 255
if value < 0:
value = 0
command = "adb -s {dev_id} shell settings put system screen_brightness {value}".format(dev_id=dev_id, value=value)
ADB.exec_adb(command)
@staticmethod
def screenshot(dev_id: str, path_save: str):
"""
Make a screenshot
TODO: Test
"""
command = "adb -s {dev_id} shell screencap {path}".format(dev_id=dev_id, path=path_save)
ADB.exec_adb(command)
@staticmethod
def save_screenshot(dev_id: str, path_device: str, path_save: str, delete=False):
"""
Return path to saved screenshot file
:dev_id: Device ID
:path_device: Path to save on the device
:path_save: Path to save on the Desctop
:delete: - False (dafault) do not save file on the device
- True save file on the device
"""
DeviceManipulations.screenshot(dev_id, path_device)
Files.pull(dev_id, path_device, path_save)
if delete is True:
pass
@staticmethod
def open_notification_center(dev_id):
"""
Method perform horizontal swipe to open notification center
TODO: Add Check if notification center is open
:dev_id: Device ID
"""
size = DeviceInfo.get_display_size(dev_id)
x = int(size.get("width")) / 2
y2 = int(size.get("height")) / 2
DeviceManipulations.swipe(dev_id, x, 1, x, y2)
@staticmethod
def execute_keyevent(dev_id, android_keyevent):
"""
Method to execute Keyevent
:dev_id: Device ID
:android_keyevent: Android KeyEvent
"""
command = "adb -s {dev_id} shell input keyevent {keycode}".format(
dev_id=dev_id, keycode=android_keyevent.value.get("key_code"))
ADB.exec_adb(command)
@staticmethod
def lock_device(dev_id: str):
"""
Method to lock the device
:dev_id: Device ID
"""
DeviceManipulations.execute_keyevent(dev_id, AndroidKeyevent.POWER_BUTTON)
@staticmethod
def unlock_device(dev_id: str, unlock_type):
"""
Method to unlock the device
:dev_id: Device ID
"""
is_locked = DeviceInfo.is_locked(dev_id)
if is_locked:
DeviceManipulations.execute_keyevent(dev_id, AndroidKeyevent.WAKEUP)
if unlock_type.SWIPE:
display_sie = DeviceInfo.get_display_size(dev_id)
x = display_sie.get("width") / 2
y = display_sie.get("height")
DeviceManipulations.swipe(dev_id, x, y * 0.10, x, y * 0.80)
@staticmethod
def swipe(dev_id: str, x1: int, y1: int, x2: int, y2: int):
"""
Method for perform swipe by coordinates
"""
command = "adb -s {dev_id} shell input swipe {x1} {y1} {x2} {y2}".format(dev_id=dev_id, x1=x1, y1=y1, x2=x2, y2=y2)
ADB.exec_adb(command)
@staticmethod
def tap(dev_id: str, x: int, y: int):
"""
Method for perform tap by coordinates
"""
command = "adb -s {dev_id} shell input tap {x} {y}".format(dev_id=dev_id, x=x, y=y)
ADB.exec_adb(command)
@staticmethod
def send_text(dev_id: str, text: str):
"""
Method perform typing text
NOTE: Before use, make sure input text field in selected
"""
command = "adb -s {dev_id} shell input text {text}".format(
dev_id=dev_id, text=text.strip().replace(" ", "%s")) # %s - Space
ADB.exec_adb(command)