提交 f2e8bb59 authored 作者: 吴强's avatar 吴强
......@@ -3,6 +3,7 @@ package com.bolan.android.modules.update;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Process;
import android.util.Log;
import com.bolan.android.modules.Utils;
......@@ -74,17 +75,21 @@ public class Updater extends ReactContextBaseJavaModule {
is = connection.getInputStream();
fos = new FileOutputStream(apkFile);
int count = 0;
int lastProgress = -1;
byte buf[] = new byte[1024 * 64];
WritableMap event;
do {
int numRead = is.read(buf);
count += numRead;
int progress = (int) (((float) count / length) * 100);
event = Arguments.createMap();
event.putInt("progress", progress);
event.putInt("current", count);
event.putInt("all", length);
emit("Updater/downloading", event);
if (progress != lastProgress) {
lastProgress = progress;
event = Arguments.createMap();
event.putInt("progress", progress);
event.putInt("current", count);
event.putInt("all", length);
emit("Updater/downloading", event);
}
if (numRead <= 0) {
event = Arguments.createMap();
event.putInt("progress", progress);
......@@ -107,7 +112,7 @@ public class Updater extends ReactContextBaseJavaModule {
return;
}
getCurrentActivity().startActivity(i);
android.os.Process.killProcess(android.os.Process.myPid());
Process.killProcess(Process.myPid());
promise.resolve(null);
} catch (MalformedURLException e) {
promise.reject("ERROR_UPDATE", "invalid url: " + sUrl);
......
......@@ -5,6 +5,7 @@ const { Updater } = NativeModules;
/**
* @typedef {Object} UpdateEvent 下载事件
* @property {string} status 事件类型,downloading或downloaded
* @property {number} progress 下载进度,0到100的整数
* @property {number} current 已下载字节数
* @property {number} all 总字节数
......@@ -15,39 +16,36 @@ const { Updater } = NativeModules;
* @param {UpdateEvent} event 下载事件
*/
/**
* @typedef {Object} CallbackHandle
*/
/**
* @member {Function} CallbackHandle~remove
*/
/**
*
* @param {UpdateCallback} cb 下载回调
* @return {CallbackHandle} 回调具柄,用以删除回调
*/
export const addUpdaterDownloadingCallback = (cb) => {
return DeviceEventEmitter.addListener('Updater/downloading', cb);
};
/**
*
* @param {UpdateCallback} cb 下载回调
* @return {CallbackHandle} 回调具柄,用以删除回调
*/
export const addUpdaterDownloadedCallback = (cb) => {
return DeviceEventEmitter.addListener('Updater/downloaded', cb);
};
/**
* 下载更新包并自动安装, 下载过程中会发送Updater/downloading事件,下载完成时会发送Updater/downloaded事件
* @param {string} url 更新地址
* @param {UpdateCallback} cb 下载回调
* @returns {Promise.<void>}
*/
export const update = async (url) => {
return Updater.update(url);
export const update = async (url, cb) => {
let handle1;
let handle2;
if (cb) {
handle1 = DeviceEventEmitter.addListener('Updater/downloading', (event) => {
cb({
...event,
status: 'downloading',
});
});
handle2 = DeviceEventEmitter.addListener('Updater/downloaded', (event) => {
cb({
...event,
status: 'downloaded',
});
});
}
await Updater.update(url);
if (handle1) {
handle1.remove();
}
if (handle2) {
handle2.remove();
}
};
/**
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论