/
Adb.py
302 lines (253 loc) · 12.2 KB
/
Adb.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
from subprocess import Popen, CREATE_NEW_CONSOLE,PIPE,STDOUT
import subprocess
import time
from com.dtmilano.android.viewclient import ViewClient
class Adb:
"""Adb class"""
def __init__(self):
self.terminal='cmd'
self.command='adb shell'
self.command1=''
self.dict_call_divert_on={'airtel':'**21*',
'Vodafone IN':'**21*',
'aircel':'345'}
self.dict_call_divert_off={'airtel':'##21#',
'Vodafone IN':'##21#',
'aircel':'345'}
self.dict_call_wait_on={'airtel':'*43#',
'Vodafone IN':'*43#',
'aircel':'345'}
self.dict_call_wait_off={'airtel':'#43#',
'Vodafone IN':'#43#',
'aircel':'345'}
def start_shell(self):
print 'starting adb shell....'
self.command=self.terminal+' '+'/k' +' '+ self.command
process=Popen(self.command,creationflags=CREATE_NEW_CONSOLE)
def send_command(self):
print 'sending command....'
procId = subprocess.Popen('adb -s ZY2235CQ26 shell', stdin = subprocess.PIPE)
procId.communicate('am broadcast -a android.settings.AIRPLANE_MODE_SETTINGS\n input keyevent 19\n input keyevent 23\n input keyevent 4\n exit')
def Get_operator(self,udid):
print 'checking operator...'
device_udid=udid
print device_udid
startshell_command= 'adb -s'+' '+device_udid+' '+'shell' + ' getprop gsm.sim.operator.alpha'
print startshell_command
procId = subprocess.Popen(startshell_command, stdout = subprocess.PIPE)
#procId = subprocess.Popen(startshell_command, stdin = subprocess.PIPE, stdout=subprocess.PIPE).communicate('getprop gsm.sim.operator.alpha\n exit')[0]
procId=procId.communicate()[0]
procId= str(procId)
print procId
return procId.strip()
def Get_call_divert_on_number(self,mobile_no,udid):
print mobile_no
print udid
operator=self.Get_operator(udid)
print operator
if operator in self.dict_call_divert_on:
print operator +' is in the list'
number = self.dict_call_divert_on.get(operator)
print 'call divert on number is '+ number + mobile_no + '#'
return str(number + mobile_no + '#')
def Get_call_divert_off_number(self,mobile_no,udid):
print mobile_no
print udid
operator=self.Get_operator(udid)
print operator
if operator in self.dict_call_divert_off:
print operator +' is in the list'
number = self.dict_call_divert_off.get(operator)
print 'call divert off number is '+ number
return str(number)
def Get_call_wait_on_number(self,udid):
operator=self.Get_operator(udid)
print operator
if operator in self.dict_call_wait_on:
print operator +' is in the list'
number = self.dict_call_wait_on.get(operator)
print 'call wait on number is '+ number
return str(number)
def Get_call_wait_off_number(self,udid):
operator=self.Get_operator(udid)
print operator
if operator in self.dict_call_wait_off:
print operator +' is in the list'
number = self.dict_call_wait_off.get(operator)
print 'call wait off number is '+ number
return str(number)
def Open_call_forwarding_settings(self,udid):
device_udid=udid
print device_udid
start_shell_command= 'adb -s'+' '+device_udid+' '+'shell'+' '+'am start -n com.android.phone/.GsmUmtsCallForwardOptions'
procId = subprocess.Popen(start_shell_command, stdout = subprocess.PIPE)
time.sleep(12)
def Call(self,udid,number_to_dial):
procId = subprocess.Popen('adb -s ZY2235CQ26 shell', stdin = subprocess.PIPE)
command= str('am start -a android.intent.action.CALL -d tel:'+ number_to_dial)
procId.communicate(command)
def End_call(self,udid):
print 'ending call...'
device_udid=udid
command= 'adb -s'+' '+device_udid+' '+'shell input keyevent KEYCODE_ENDCALL'
print command
procId = subprocess.Popen(command)
def Answer_call(self,udid):
print 'answering call...'
device_udid=udid
command= 'adb -s'+' '+device_udid+' '+'shell input keyevent KEYCODE_CALL'
print command
procId = subprocess.Popen(command)
def reboot_device(self,deviceid):
print 'rebooting device...'
device_udid=deviceid
startshell_command= 'adb -s'+' '+device_udid+' '+'reboot'
procId = subprocess.Popen(startshell_command)
def switchoff_device(self,deviceid):
print 'shutdown device...'
device_udid=deviceid
startshell_command= 'adb -s'+' '+device_udid+' '+'reboot -p'
procId = subprocess.Popen(startshell_command)
def getting_list_of_devices(self):
print 'getting list of devices...'
import re
import string
startshell_command= 'adb devices'
procId = subprocess.Popen(startshell_command,stdout=subprocess.PIPE)
device_list=procId.communicate()[0]
print ''.join(i for i in device_list if i in string.ascii_letters+'0123456789')
def launch_airplane_mode_settings(self,deviceid):
print 'Launching airplane mode page.....'
print deviceid
device_udid=deviceid
startshell_command= 'adb -s'+' '+device_udid+' '+'shell am start -a android.settings.AIRPLANE_MODE_SETTINGS'
procId = subprocess.Popen(startshell_command)
def get_android_version_number(self,deviceid):
print 'getting the android version..........'
device_udid=deviceid
startshell_command= 'adb -s'+' '+device_udid+' '+'shell getprop ro.build.version.release'
procId = subprocess.Popen(startshell_command,stdout=subprocess.PIPE)
to_ver= procId.communicate()[0].strip().strip('\r\n')
to_ver=float(str(to_ver)[0:3])
print type(to_ver)
print to_ver
return to_ver
def Launch_wifi_settings(self,deviceid):
print 'Launching wifi settings.....'
print deviceid
device_udid=deviceid
command= 'adb -s'+' '+device_udid+' '+'shell am start -n com.android.settings/.wifi.WifiSettings'
procId = subprocess.Popen(command)
def Close_settings(self,deviceid):
print 'closing settings.....'
print deviceid
device_udid=deviceid
command= 'adb -s'+' '+device_udid+' '+'shell am force-stop com.android.settings'
procId = subprocess.Popen(command)
def Compose_SMS(self,udid,number,sms_body):
print 'creating sms...'
device_udid=udid
command= 'adb -s'+' '+device_udid+' '+'shell am start -a android.intent.action.SENDTO -d sms:'+number+' '+'--es sms_body '+"'"+sms_body+"'"+' --ez exit_on_sent true'
print command
procId = subprocess.Popen(command)
def Launch_Network_settings(self,udid):
print 'Launching network settings.....'
#switch from 3g to 4g or 2g
print udid
device_udid=udid
command= 'adb -s'+' '+device_udid+' '+'shell am start -n com.android.phone/.MobileNetworkSettings'
procId = subprocess.Popen(command)
def Bring_to_homescreen(self,udid):
print 'bringing phone to home screen...'
device_udid=udid
command= 'adb -s'+' '+device_udid+' '+'shell input keyevent KEYCODE_HOME'
print command
procId = subprocess.Popen(command)
def Check_airplane_mode_status(self,udid):
print 'checking airplane mode status...'
device_udid=udid
command= 'adb -s'+' '+device_udid+' '+'shell settings get global airplane_mode_on'
out = subprocess.Popen(command, stdout = subprocess.PIPE)
out=out.communicate()[0]
print out
return out.strip()
#returns 0 for off and 1 for on
def wake_device(self,udid):
print 'waking up device...'
device_udid=udid
command= 'adb -s'+' '+device_udid+' '+'shell input keyevent WAKEUP'
print command
procId = subprocess.Popen(command)
def unlock_device_old(self,udid):
print 'hello'+ udid
self.device_udid=udid
print self.device_udid
#device, serialno = ViewClient.connectToDeviceOrExit(5,self.device_udid)
device, serialno = ViewClient.connectToDeviceOrExit(serialno=self.device_udid)
if device.isLocked():
device.wake()
device.unlock()
else :
device.wake()
device.unlock()
def unlock_device(self,udid,pin):
pin= map(int, str(pin))
device, serialno = ViewClient.connectToDeviceOrExit()
vc = ViewClient(device=device, serialno=udid)
if device.isLocked():
print 'device with serial no '+udid+ ' is locked'
device.wake()
vc.dump()
device.shell('input swipe 357 1227 357 680')
vc.dump()
for item in pin:
item= str(item)
key = vc.findViewWithText(item)
if key:
key.touch()
else:
print 'key not found or locator is incorrect'
enter= vc.findViewById('com.android.systemui:id/key_enter')
if enter:
enter.touch()
else:
print 'key not found or locator is incorrect'
else:
print 'device with serial no '+udid+ ' is unlocked'
def Switch_network_mode(self,udid,mode): #dial to check *#*#4636#*#*
if mode == '3G':
mode_value = 0
elif mode == '2G':
mode_value = 1
elif mode == '4G':
mode_value = 2
else:
mode_value = 2
print 'switching network mode to '+str(mode_value)+ ' on '+udid
cmd1= 'adb -s'+' '+udid+' '+'shell settings put global preferred_network_mode '+str(mode_value)
cmd2= 'adb -s'+' '+udid+' '+'shell settings put global preferred_network_mode2 '+str(mode_value)
print cmd1
print cmd2
procId = subprocess.Popen(cmd1)
procId = subprocess.Popen(cmd2)
def Check_call_state(self,udid):
print 'checking call state...'
device_udid=udid
p1= Popen(['adb', '-s', device_udid, 'shell', 'dumpsys', 'telephony.registry'],stdout=PIPE)
p2 = Popen(["findstr", "mCallState"], stdin=p1.stdout, stdout=PIPE)
p1.stdout.close() # Allow p1 to receive a SIGPIPE if p2 exits.
output = p2.communicate()[0]
out= output.splitlines()[0] + '\n'
print out
if 'mCallState=0' in out:
print 'phone is idle'
return 'idle'
elif 'mCallState=1' in out:
print 'there is an incoming call'
return 'incoming'
elif 'mCallState=2' in out:
print 'there is an ongoing call'
return 'ongoing'
#test= Adb()
#udid= 'D1AGAD57A1407259'
#test.Check_call_state(udid)