Skip to content

Commit

Permalink
add adbkit-init
Browse files Browse the repository at this point in the history
  • Loading branch information
codeskyblue committed Feb 24, 2018
1 parent 974cad7 commit d31d2e0
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ ChangeLog
.vscode/
report/
*.apk
node_modules/
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -905,9 +905,13 @@ $ curl -d '{"jsonrpc":"2.0","method":"deviceInfo","id":1}' 127.0.0.1:9008/jsonrp

   如果报错,可能是缺少某个设备组件没有安装,使用下面的命令重新初始化 `python -m uiautomator2 init --reinstall`

## 尝鲜功能
## 实验室功能
### 远程查看
手机`python -m uiautomator2 init`之后,浏览器输入 <device_ip:7912>,会发现一个远程控制功能,延迟非常低噢。^_^

### 手机USB连接后,自动调用init命令
[adbkit-init](examples/adbkit-init)

# 项目历史
项目重构自 <https://github.com/openatx/atx-uiautomator>

Expand Down
23 changes: 23 additions & 0 deletions examples/adbkit-init/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# adbkit-init
run `python -m uiautomator2 init` once android device plugin.

## Installation
```bash
npm install .
```

## Usage
```bash
node main.js --server $SERVER_ADDR
```

How it works.

Use adbkit to trace device. And the following command will call when device plugin

```bash
python -m uiautomator2 init --server $SERVER_ADDR
```

## LICENSE
MIT
57 changes: 57 additions & 0 deletions examples/adbkit-init/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict'

var Promise = require('bluebird')
var adb = require('adbkit')
var client = adb.createClient()
var util = require('util')
const { spawn } = require("child_process")
var argv = require('minimist')(process.argv.slice(2))

const serverAddr = argv.server; // Usage: node main.js --server $SERVER_ADDR

function initDevice(device) {
if (device.type != 'device') {
return
}
client.shell(device.id, 'am start -a android.intent.action.VIEW -d http://www.stackoverflow.com')
.then(adb.util.readAll)
.then(function(output) {
var args = ["-m", "uiautomator2", "init", "--serial", device.id]
if (serverAddr) {
args.push("--server", serverAddr);
}
const child = spawn("python", args);
child.stdout.on("data", data => {
process.stdout.write(data)
})
child.stderr.on("data", data => {
process.stderr.write(data)
})
child.on('close', code => {
util.log(`child process exited with code ${code}`);
});
})
}

util.log("tracking device")
if (serverAddr) {
util.log("server %s", serverAddr)
}

client.trackDevices()
.then(function(tracker) {
tracker.on('add', function(device) {
util.log("Device %s(%s) was plugged in", device.id, device.type)
initDevice(device)
})
tracker.on('remove', function(device) {
util.log('Device %s was unplugged', device.id)
})
tracker.on("change", function(device) {
util.log('Device %s was changed to %s', device.id, device.type)
initDevice(device)
})
tracker.on('end', function() {
util.log('Tracking stopped')
})
})
15 changes: 15 additions & 0 deletions examples/adbkit-init/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "adbkit-init",
"version": "1.0.0",
"description": "run `python -m uiautomator2 init` once android device plugin.",
"main": "main.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "MIT",
"dependencies": {
"adbkit": "^2.11.0",
"minimist": "^1.2.0"
}
}
42 changes: 22 additions & 20 deletions uiautomator2/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def launch_and_check(self):


class MyFire(object):
def init(self, server=None, apk_version=__apk_version__, agent_version=__atx_agent_version__, verbose=False, reinstall=False, proxy=None):
def init(self, server=None, apk_version=__apk_version__, agent_version=__atx_agent_version__, verbose=False, reinstall=False, proxy=None, serial=None):
if verbose:
log.setLevel(logging.DEBUG)
if server:
Expand All @@ -231,26 +231,28 @@ def init(self, server=None, apk_version=__apk_version__, agent_version=__atx_age
os.environ['HTTP_PROXY'] = proxy
os.environ['HTTPS_PROXY'] = proxy

output = subprocess.check_output(['adb', 'devices'])
pattern = re.compile(r'(?P<serial>[^\s]+)\t(?P<status>device|offline)')
matches = pattern.findall(output.decode())
for m in matches:
serial, status = m[0], m[1]
if status == 'offline':
log.warn("device(%s) is offline, skip", serial)
continue

log.info("Device(%s) initialing ...", serial)
ins = Installer(serial)
ins.server_addr = server
ins.install_minicap()
ins.install_minitouch()
ins.install_uiautomator_apk(apk_version, reinstall)
ins.install_atx_agent(agent_version, reinstall)
ins.launch_and_check()
if len(matches) == 0:
log.warn("No avaliable android devices detected. See details from `adb devices`")
if not serial:
output = subprocess.check_output(['adb', 'devices'])
pattern = re.compile(r'(?P<serial>[^\s]+)\t(?P<status>device|offline)')
matches = pattern.findall(output.decode())
valid_serials = [m[0] for m in matches if m[1] == 'device']
if len(valid_serials) == 0:
log.warning("No avaliable android devices detected.")
return
if len(valid_serials) > 1:
log.warning("More then 1 device detected, you must specify android serial")
return
serial = valid_serials[0]

log.info("Device(%s) initialing ...", serial)
ins = Installer(serial)
ins.server_addr = server
ins.install_minicap()
ins.install_minitouch()
ins.install_uiautomator_apk(apk_version, reinstall)
ins.install_atx_agent(agent_version, reinstall)
ins.launch_and_check()

def clear_cache(self):
log.info("clear cache dir: %s", appdir)
shutil.rmtree(appdir, ignore_errors=True)
Expand Down
3 changes: 2 additions & 1 deletion uiautomator2/version.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# coding: utf-8
#

__apk_version__ = '1.0.9'
__apk_version__ = '1.0.10'
# 1.0.10 fix service not started bug
# 1.0.9 fix apk version code and version name
# ERR: 1.0.8 bad version number. show ip on notification
# ERR: 1.0.7 bad version number. new input method, some bug fix
Expand Down

0 comments on commit d31d2e0

Please sign in to comment.