def get_setting(self): """ get device setting via adb """ db_name = "/data/data/com.android.providers.settings/databases/settings.db" system_settings = {} out = adb.shell("sqlite3 %s \"select * from %s\"" % (db_name, "system")) out_lines = out.splitlines() for line in out.splitlines(): segs = line.split("|") if len(segs) != 3: continue system_settings[segs[1]] = segs[2] secure_settings = {} out = adb.shell("sqlite3 %s \"select * from %s\"" % (db_name, "secure")) out_lines = out.splitlines() for line in out_lines: segs = line.split("|") if len(segs) != 3: continue secure_settings[segs[1]] = segs[2] self.settings["system"] = system_settings self.settings["secure"] = secure_settings return self.settings
def dump(self, window = -1, sleep = 1): if sleep > 0: time.sleep(sleep) version = adb.getSDKVersion() if version >= 23: pathname = '/storage/self' filename = 'window_dump.xml' cmd = 'sh /system/bin/uiautomator dump --compressed %s/%s >/dev/null' % (pathname, filename) adb.shell(cmd) cmd = 'cat %s/%s' % (pathname, filename) received = adb.shell(cmd) else: if version >= 18: adb.shell('sh /system/bin/uiautomator dump --compressed window_dump.xml') received = adb.shell('cat window_dump.xml') else: adb.shell('sh /system/bin/uiautomator dump window_dump.xml') received = adb.shell('cat window_dump.xml') if received: received = unicode(received, encoding = 'utf-8', errors = 'replace') if not received: raise RuntimeError('ERROR: Empty UiAutomator dump was received') self.setViewsFromUiAutomatorDump(received) return self.views
def change_settings(self, table_name, name, value): """ dangerous method, by calling this, change settings.db in device be very careful for sql injection :param table_name: table name to work on, usually it is system or secure :param name: settings name to set :param value: settings value to set """ db_name = "/data/data/com.android.providers.settings/databases/settings.db" adb.shell("sqlite3 %s \"update %s set value='%s' where name='%s'" % (db_name, table_name, value, name)) return True
def send_intent(self, intent): """ send an intent to device via am (ActivityManager) :param intent: instance of Intent :return: """ assert intent is not None cmd = intent.get_cmd() return adb.shell(cmd)
def start_activity_via_monkey(self, package): """ use monkey to start activity """ cmd = "monkey" if package: cmd += " -p %s" % package out = adb.shell(cmd) if re.search(r"(Error)|(Cannot find 'App')", out, re.IGNORECASE | re.MULTILINE): raise RuntimeError(out)
def get_package_path(self, package_name): """ get installation path of a package (app) :param package_name: :return: package path of app in device """ dat = adb.shell("pm path %s" % package_name) package_path_RE = re.compile("^package:(.+)$") m = package_path_RE.match(dat) if m: path = m.group(1) return path.strip() return None
def send(self, device): data = adb.shell(self.intent) return True
def __dumpWindowsInformation(self, debug=False): self.windows = {} self.currentFocus = None dww = adb.shell('dumpsys window windows') lines = dww.splitlines() widRE = re.compile('^ *Window #%s Window\{%s (u\d+ )?%s?.*\}:' % (_nd('num'), _nh('winId'), _ns('activity', greedy=True))) currentFocusRE = re.compile('^ mCurrentFocus=Window\{%s .*' % _nh('winId')) viewVisibilityRE = re.compile(' mViewVisibility=0x%s ' % _nh('visibility')) # This is for 4.0.4 API-15 containingFrameRE = re.compile('^ *mContainingFrame=\[%s,%s\]\[%s,%s\] mParentFrame=\[%s,%s\]\[%s,%s\]' % (_nd('cx'), _nd('cy'), _nd('cw'), _nd('ch'), _nd('px'), _nd('py'), _nd('pw'), _nd('ph'))) contentFrameRE = re.compile('^ *mContentFrame=\[%s,%s\]\[%s,%s\] mVisibleFrame=\[%s,%s\]\[%s,%s\]' % (_nd('x'), _nd('y'), _nd('w'), _nd('h'), _nd('vx'), _nd('vy'), _nd('vx1'), _nd('vy1'))) # This is for 4.1 API-16 framesRE = re.compile('^ *Frames: containing=\[%s,%s\]\[%s,%s\] parent=\[%s,%s\]\[%s,%s\]' % (_nd('cx'), _nd('cy'), _nd('cw'), _nd('ch'), _nd('px'), _nd('py'), _nd('pw'), _nd('ph'))) contentRE = re.compile('^ *content=\[%s,%s\]\[%s,%s\] visible=\[%s,%s\]\[%s,%s\]' % (_nd('x'), _nd('y'), _nd('w'), _nd('h'), _nd('vx'), _nd('vy'), _nd('vx1'), _nd('vy1'))) policyVisibilityRE = re.compile('mPolicyVisibility=%s ' % _ns('policyVisibility', greedy=True)) for l in range(len(lines)): m = widRE.search(lines[l]) if m: num = int(m.group('num')) winId = m.group('winId') activity = m.group('activity') wvx = 0 wvy = 0 wvw = 0 wvh = 0 px = 0 py = 0 visibility = -1 policyVisibility = 0x0 for l2 in range(l+1, len(lines)): m = widRE.search(lines[l2]) if m: l += (l2-1) break m = viewVisibilityRE.search(lines[l2]) if m: visibility = int(m.group('visibility')) if self.build[VERSION_SDK_PROPERTY] >= 17: m = framesRE.search(lines[l2]) if m: px, py = obtainPxPy(m) m = contentRE.search(lines[l2+2]) if m: wvx, wvy = obtainVxVy(m) wvw, wvh = obtainVwVh(m) elif self.build[VERSION_SDK_PROPERTY] >= 16: m = framesRE.search(lines[l2]) if m: px, py = self.__obtainPxPy(m) m = contentRE.search(lines[l2+1]) if m: # FIXME: the information provided by 'dumpsys window windows' in 4.2.1 (API 16) # when there's a system dialog may not be correct and causes the View coordinates # be offset by this amount, see # https://github.com/dtmilano/AndroidViewClient/issues/29 wvx, wvy = self.__obtainVxVy(m) wvw, wvh = self.__obtainVwVh(m) elif self.build[VERSION_SDK_PROPERTY] == 15: m = containingFrameRE.search(lines[l2]) if m: px, py = self.__obtainPxPy(m) m = contentFrameRE.search(lines[l2+1]) if m: wvx, wvy = self.__obtainVxVy(m) wvw, wvh = self.__obtainVwVh(m) elif self.build[VERSION_SDK_PROPERTY] == 10: m = containingFrameRE.search(lines[l2]) if m: px, py = self.__obtainPxPy(m) m = contentFrameRE.search(lines[l2+1]) if m: wvx, wvy = self.__obtainVxVy(m) wvw, wvh = self.__obtainVwVh(m) else: warnings.warn("Unsupported Android version %d" % self.build[VERSION_SDK_PROPERTY]) #print >> sys.stderr, "Searching policyVisibility in", lines[l2] m = policyVisibilityRE.search(lines[l2]) if m: policyVisibility = 0x0 if m.group('policyVisibility') == 'true' else 0x8 self.windows[winId] = Window(num, winId, activity, wvx, wvy, wvw, wvh, px, py, visibility + policyVisibility) else: m = currentFocusRE.search(lines[l]) if m: self.currentFocus = m.group('winId') if self.windowId and self.windowId in self.windows and self.windows[self.windowId].visibility == 0: w = self.windows[self.windowId] return (w.wvx, w.wvy) elif self.currentFocus in self.windows and self.windows[self.currentFocus].visibility == 0: w = self.windows[self.currentFocus] return (w.wvx, w.wvy) else: return (0,0)