Skip to content

Commit b489fcc

Browse files
committed
AdbClient: Added isLocked() method to check if screen is locked
- Version 4.6.0 - Code reformatted - Ignored setuptools files
1 parent eba326e commit b489fcc

3 files changed

Lines changed: 30 additions & 13 deletions

File tree

AndroidViewClient/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
bin/*
22
tmp/*
3+
build/*
4+
dist/*
5+
*.egg-info

AndroidViewClient/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from setuptools import setup, find_packages
44

55
setup(name='androidviewclient',
6-
version='4.5.3',
6+
version='4.6.0',
77
description='''AndroidViewClient is a 100% pure python tool that
88
simplifies test script creation providing higher level operations and the ability of
99
obtaining the tree of Views present at any given moment on the device or emulator screen.

AndroidViewClient/src/com/dtmilano/android/adb/adbclient.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
@author: Diego Torres Milano
1818
'''
1919

20-
__version__ = '4.5.3'
20+
__version__ = '4.6.0'
2121

2222
import sys
2323
import warnings
@@ -93,7 +93,7 @@ def __init__(self, serialno, hostname=HOSTNAME, port=PORT, settransport=True, re
9393
@staticmethod
9494
def setAlarm(timeout):
9595
osName = platform.system()
96-
if osName.startswith('Windows'): # alarm is not implemented in Windows
96+
if osName.startswith('Windows'): # alarm is not implemented in Windows
9797
return
9898
if DEBUG:
9999
print >> sys.stderr, "setAlarm(%d)" % timeout
@@ -153,7 +153,7 @@ def __receive(self, nob=None):
153153
recv = bytearray()
154154
nr = 0
155155
while nr < nob:
156-
chunk = self.socket.recv(min((nob-nr), 4096))
156+
chunk = self.socket.recv(min((nob - nr), 4096))
157157
recv.extend(chunk)
158158
nr += len(chunk)
159159
if DEBUG:
@@ -190,7 +190,7 @@ def checkVersion(self, reconnect=True):
190190
print >> sys.stderr, "checkVersion(reconnect=%s)" % reconnect
191191
self.__send('host:version', reconnect=False)
192192
version = self.socket.recv(8)
193-
VERSION='0004001f'
193+
VERSION = '0004001f'
194194
if version != VERSION:
195195
raise RuntimeError("ERROR: Incorrect ADB server version %s (expecting %s)" % (version, VERSION))
196196
if reconnect:
@@ -254,9 +254,9 @@ def shell(self, cmd=None):
254254
return out
255255
else:
256256
self.__send('shell:')
257-
#sin = self.socket.makefile("rw")
258-
#sout = self.socket.makefile("r")
259-
#return (sin, sin)
257+
# sin = self.socket.makefile("rw")
258+
# sout = self.socket.makefile("r")
259+
# return (sin, sin)
260260
sout = adbClient.socket.makefile("r")
261261
return sout
262262

@@ -306,7 +306,7 @@ def getProperty(self, key, strip=True):
306306
def press(self, name, eventType=DOWN_AND_UP):
307307
cmd = 'input keyevent %s' % name
308308
if DEBUG:
309-
print >>sys.stderr, "press(%s)" % cmd
309+
print >> sys.stderr, "press(%s)" % cmd
310310
self.shell(cmd)
311311

312312
def startActivity(self, component=None, flags=None, uri=None):
@@ -320,7 +320,7 @@ def startActivity(self, component=None, flags=None, uri=None):
320320
if DEBUG:
321321
print >> sys.stderr, "Starting activity: %s" % cmd
322322
out = self.shell(cmd)
323-
if re.search(r"(Error type)|(Error: )|(Cannot find 'App')", out, re.IGNORECASE|re.MULTILINE):
323+
if re.search(r"(Error type)|(Error: )|(Cannot find 'App')", out, re.IGNORECASE | re.MULTILINE):
324324
raise RuntimeError(out)
325325

326326
def takeSnapshot(self, reconnect=False):
@@ -334,7 +334,7 @@ def takeSnapshot(self, reconnect=False):
334334
raise Exception("You have to install PIL to use takeSnapshot()")
335335
self.__send('framebuffer:', checkok=True, reconnect=False)
336336
import struct
337-
#case 1: // version
337+
# case 1: // version
338338
# return 12; // bpp, size, width, height, 4*(length, offset)
339339
received = self.__receive(1 * 4 + 12 * 4)
340340
(version, bpp, size, width, height, roffset, rlen, boffset, blen, goffset, glen, aoffset, alen) = struct.unpack('<' + 'L' * 13, received)
@@ -374,14 +374,28 @@ def drag(self, (x0, y0), (x1, y1), duration, steps):
374374
elif version <= 17:
375375
self.shell('input swipe %d %d %d %d' % (x0, y0, x1, y1))
376376
else:
377-
self.shell('input swipe %d %d %d %d %d' % (x0, y0, x1, y1, duration*1000))
377+
self.shell('input swipe %d %d %d %d %d' % (x0, y0, x1, y1, duration * 1000))
378378

379379
def type(self, text):
380380
self.shell(u'input text "%s"' % text)
381381

382382
def wake(self):
383383
self.shell('input keyevent 26')
384384

385+
def isLocked(self):
386+
'''
387+
Checks if the device screen is locked.
388+
389+
@return True if the device screen is locked
390+
'''
391+
392+
lockScreenRE = re.compile('mShowingLockscreen=(true|false)')
393+
m = lockScreenRE.search(self.shell('(dumpsys window policy'))
394+
if m:
395+
return (m.group(1) == 'true')
396+
raise RuntimeError("Couldn't determine screen lock state")
397+
398+
385399
@staticmethod
386400
def percentSame(image1, image2):
387401
'''
@@ -437,7 +451,7 @@ def sameAs(image1, image2, percent=1.0):
437451
if cmd == 'exit':
438452
break
439453
adbClient.socket.__send(cmd + "\r\n")
440-
sout.readline(4096) # eat first line, which is the command
454+
sout.readline(4096) # eat first line, which is the command
441455
while True:
442456
line = sout.readline(4096)
443457
if prompt.match(line):

0 commit comments

Comments
 (0)