Repository navigation
Expand file tree
/
Copy pathdevutil.js
More file actions
134 lines (121 loc) · 3.59 KB
/
Copy pathdevutil.js
File metadata and controls
134 lines (121 loc) · 3.59 KB
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
var util = require('util')
var split = require('split')
var Promise = require('bluebird')
var devutil = module.exports = Object.create(null)
function closedError(err) {
return err.message.indexOf('closed') !== -1
}
devutil.ensureUnusedLocalSocket = function(adb, serial, sock) {
return adb.openLocal(serial, sock)
.then(function(conn) {
conn.end()
throw new Error(util.format('Local socket "%s" should be unused', sock))
})
.catch(closedError, function() {
return Promise.resolve(sock)
})
}
devutil.waitForLocalSocket = function(adb, serial, sock) {
return adb.openLocal(serial, sock)
.then(function(conn) {
conn.sock = sock
return conn
})
.catch(closedError, function() {
return Promise.delay(100)
.then(function() {
return devutil.waitForLocalSocket(adb, serial, sock)
})
})
}
devutil.listPidsByComm = function(adb, serial, comm, bin) {
var users = {
shell: true
}
return adb.shell(serial, ['ps'])
.then(function(out) {
return new Promise(function(resolve) {
var header = true
var pids = []
out.pipe(split())
.on('data', function(chunk) {
if (header) {
header = false
}
else {
var cols = chunk.toString().split(/\s+/)
if (cols.pop() === bin && users[cols[0]]) {
pids.push(Number(cols[1]))
}
}
})
.on('end', function() {
resolve(pids)
})
})
})
}
devutil.waitForProcsToDie = function(adb, serial, comm, bin) {
return devutil.listPidsByComm(adb, serial, comm, bin)
.then(function(pids) {
if (pids.length) {
return Promise.delay(100)
.then(function() {
return devutil.waitForProcsToDie(adb, serial, comm, bin)
})
}
})
}
devutil.killProcsByComm = function(adb, serial, comm, bin, mode) {
return devutil.listPidsByComm(adb, serial, comm, bin, mode)
.then(function(pids) {
if (!pids.length) {
return Promise.resolve()
}
return adb.shell(serial, ['kill', mode || -15].concat(pids))
.then(function(out) {
return new Promise(function(resolve) {
out.on('end', resolve)
})
})
.then(function() {
return devutil.waitForProcsToDie(adb, serial, comm, bin)
})
.timeout(2000)
.catch(function() {
return devutil.killProcsByComm(adb, serial, comm, bin, -9)
})
})
}
devutil.makeIdentity = function(serial, properties) {
var model = properties['ro.product.model']
var brand = properties['ro.product.brand']
var manufacturer = properties['ro.product.manufacturer']
var operator = properties['gsm.sim.operator.alpha'] ||
properties['gsm.operator.alpha']
var version = properties['ro.build.version.release']
var sdk = properties['ro.build.version.sdk']
var abi = properties['ro.product.cpu.abi']
var product = properties['ro.product.name']
// Remove brand prefix for consistency
if (model.substr(0, brand.length) === brand) {
model = model.substr(brand.length)
}
// Remove manufacturer prefix for consistency
if (model.substr(0, manufacturer.length) === manufacturer) {
model = model.substr(manufacturer.length)
}
// Clean up remaining model name
// model = model.replace(/[_ ]/g, '')
return {
serial: serial
, platform: 'Android'
, manufacturer: manufacturer.toUpperCase()
, operator: operator || null
, model: model
, version: version
, abi: abi
, sdk: sdk
, product: product
}
}