Poor UI for installation progress.

This commit is contained in:
Simo Kinnunen
2014-03-26 21:13:48 +09:00
parent 16bb47930d
commit 1880d52075
6 changed files with 98 additions and 87 deletions

View File

@@ -16,36 +16,6 @@ module.exports = syrup.serial()
.define(function(options, adb, router, push) {
var log = logger.createLogger('device:plugins:install')
function fetchResource(options) {
var resolver = Promise.defer()
function responseListener(res) {
if (res.statusCode === 200) {
resolver.resolve(res)
}
else {
resolver.reject(new Error(util.format(
'Resource "%s" returned HTTP %d'
, options.url
, res.statusCode
)))
}
}
function errorListener(err) {
resolver.reject(err)
}
var req = request(options)
.on('response', responseListener)
.on('error', errorListener)
return resolver.promise.finally(function() {
req.removeListener('response', responseListener)
req.removeListener('error', errorListener)
})
}
router.on(wire.InstallMessage, function(channel, message) {
log.info('Installing "%s"', message.url)
@@ -63,48 +33,64 @@ module.exports = syrup.serial()
])
}
sendProgress('Fetching app from server')
fetchResource({url: message.url})
.then(function(res) {
var contentLength = parseInt(res.headers['content-length'], 10)
var source = new stream.Readable().wrap(res)
var target = '/data/local/tmp/_app.apk'
sendProgress('Pushing app to the device', 0)
return adb.push(options.serial, source, target)
.then(function(transfer) {
var resolver = Promise.defer()
function progressListener(stats) {
if (contentLength) {
sendProgress(
'Pushing app to the device'
, stats.bytesTransferred / contentLength
)
}
}
function errorListener(err) {
resolver.reject(err)
}
function endListener() {
resolver.resolve(target)
}
transfer.on('progress', progressListener)
transfer.on('error', errorListener)
transfer.on('end', endListener)
return resolver.promise.finally(function() {
transfer.removeListener('progress', progressListener)
transfer.removeListener('error', errorListener)
transfer.removeListener('end', endListener)
})
})
function pushApp() {
var req = request({
url: message.url
})
// We need to catch the Content-Length on the fly or we risk
// losing some of the initial chunks.
var contentLength = null
req.on('response', function(res) {
contentLength = parseInt(res.headers['content-length'], 10)
})
var source = new stream.Readable().wrap(req)
var target = '/data/local/tmp/_app.apk'
return adb.push(options.serial, source, target)
.then(function(transfer) {
var resolver = Promise.defer()
function progressListener(stats) {
if (contentLength) {
// Progress 0% to 70%
sendProgress(
'Pushing app to the device'
, 70 * Math.max(0, Math.min(
70
, stats.bytesTransferred / contentLength
))
)
}
}
function errorListener(err) {
resolver.reject(err)
}
function endListener() {
resolver.resolve(target)
}
transfer.on('progress', progressListener)
transfer.on('error', errorListener)
transfer.on('end', endListener)
return resolver.promise.finally(function() {
transfer.removeListener('progress', progressListener)
transfer.removeListener('error', errorListener)
transfer.removeListener('end', endListener)
})
})
}
// Progress 0%
sendProgress('Pushing app to the device', 0)
pushApp()
.then(function(apk) {
sendProgress('Installing app')
// Progress 80%
sendProgress('Installing app', 80)
return adb.installRemote(options.serial, apk)
})
.then(function() {
@@ -114,7 +100,8 @@ module.exports = syrup.serial()
, message.launchActivity.action
, message.launchActivity.component
)
sendProgress('Launching activity')
// Progress 90%
sendProgress('Launching activity', 90)
return adb.startActivity(options.serial, message.launchActivity)
}
})
@@ -125,6 +112,7 @@ module.exports = syrup.serial()
options.serial
, seq++
, true
, 'Installation complete'
))
])
})