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