Repository navigation
Expand file tree
/
Copy pathdevutil.js
More file actions
161 lines (143 loc) · 4.17 KB
/
Copy pathdevutil.js
File metadata and controls
161 lines (143 loc) · 4.17 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
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
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.ensureUnusedPort = function(adb, serial, port) {
return adb.openTcp(serial, port)
.then(function(conn) {
conn.end()
throw new Error(util.format('Port "%d" should be unused', port))
})
.catch(closedError, function() {
return Promise.resolve(port)
})
}
devutil.waitForPort = function(adb, serial, port) {
return adb.openTcp(serial, port)
.then(function(conn) {
conn.port = port
return conn
})
.catch(closedError, function() {
return Promise.delay(100)
.then(function() {
return devutil.waitForPort(adb, serial, port)
})
})
}
devutil.waitForPortToFree = function(adb, serial, port) {
return adb.openTcp(serial, port)
.then(function(conn) {
var resolver = Promise.defer()
function endListener() {
resolver.resolve(port)
}
function errorListener(err) {
resolver.reject(err)
}
conn.on('end', endListener)
conn.on('error', errorListener)
return resolver.promise.finally(function() {
conn.removeListener('end', endListener)
conn.removeListener('error', errorListener)
conn.end()
})
})
.catch(closedError, function() {
return port
})
}
devutil.listPidsByComm = function(adb, serial, comm, bin) {
var users = {
shell: true
}
return adb.shell(serial, ['ps', comm])
.then(function(out) {
return new Promise(function(resolve) {
var header = false
, 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(+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']
, brand = properties['ro.product.brand']
, manufacturer = properties['ro.product.manufacturer']
, operator = properties['gsm.sim.operator.alpha'] ||
properties['gsm.operator.alpha']
, version = properties['ro.build.version.release']
, sdk = properties['ro.build.version.sdk']
, abi = properties['ro.product.cpu.abi']
, 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
}
}