From a93875884c137b506bd287727dcf540a106d8dd0 Mon Sep 17 00:00:00 2001 From: nonghuaqiang <744519553@qq.com> Date: Tue, 3 Dec 2019 12:34:02 +0800 Subject: [PATCH] =?UTF-8?q?android=20=E4=B8=8B=E8=BD=BD=E8=BF=9B=E5=BA=A6?= =?UTF-8?q?=E5=8F=91=E9=80=81=E4=BA=8B=E4=BB=B6=E5=8F=91=E5=87=BA=E5=92=8C?= =?UTF-8?q?=E5=8F=91=E9=80=81=E4=BA=8B=E4=BB=B6=E9=A2=91=E7=8E=87=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/update/DownloadTask.java | 23 +++++++++++++++++-- .../modules/update/UpdateModule.java | 12 +++++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java b/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java index 76015d3..f66d4b3 100644 --- a/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java +++ b/android/src/main/java/cn/reactnative/modules/update/DownloadTask.java @@ -3,6 +3,8 @@ package cn.reactnative.modules.update; import android.content.Context; import android.os.AsyncTask; import android.util.Log; +import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.WritableMap; import okhttp3.OkHttpClient; import okhttp3.Request; @@ -31,11 +33,11 @@ import java.util.HashMap; import okio.BufferedSink; import okio.BufferedSource; import okio.Okio; - +import static cn.reactnative.modules.update.UpdateModule.sendEvent; /** * Created by tdzl2003 on 3/31/16. */ -class DownloadTask extends AsyncTask { +class DownloadTask extends AsyncTask { final int DOWNLOAD_CHUNK_SIZE = 4096; Context context; @@ -91,11 +93,17 @@ class DownloadTask extends AsyncTask { long bytesRead = 0; long totalRead = 0; + double lastProgressValue=0; while ((bytesRead = source.read(sink.buffer(), DOWNLOAD_CHUNK_SIZE)) != -1) { totalRead += bytesRead; if (UpdateContext.DEBUG) { Log.d("RNUpdate", "Progress " + totalRead + "/" + contentLength); } + double progress = Math.round(((double) totalRead * 100) / contentLength); + if ((progress != lastProgressValue) || (totalRead == contentLength)) { + lastProgressValue = progress; + publishProgress(new long[]{(long)progress,totalRead, contentLength}); + } } if (totalRead != contentLength) { throw new Error("Unexpected eof while reading ppk"); @@ -108,6 +116,17 @@ class DownloadTask extends AsyncTask { } } + @Override + protected void onProgressUpdate(long[]... values) { + super.onProgressUpdate(values); + WritableMap params = Arguments.createMap(); + params.putDouble("progress", (values[0][0])); + params.putDouble("totalRead", (values[0][1])); + params.putDouble("contentLength", (values[0][2])); + sendEvent("progress", params); + + } + byte[] buffer = new byte[1024]; private static native byte[] bsdiffPatch(byte[] origin, byte[] patch); diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java index 8f22789..0f64b06 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java @@ -9,11 +9,14 @@ import com.facebook.react.ReactApplication; import com.facebook.react.ReactInstanceManager; import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.bridge.JSBundleLoader; +import com.facebook.react.bridge.WritableMap; +import com.facebook.react.modules.core.DeviceEventManagerModule; import java.lang.reflect.Field; import java.lang.reflect.Method; @@ -25,10 +28,11 @@ import java.util.Map; */ public class UpdateModule extends ReactContextBaseJavaModule{ UpdateContext updateContext; - + public static ReactApplicationContext mContext; public UpdateModule(ReactApplicationContext reactContext, UpdateContext updateContext) { super(reactContext); this.updateContext = updateContext; + mContext=reactContext; } public UpdateModule(ReactApplicationContext reactContext) { @@ -170,4 +174,10 @@ public class UpdateModule extends ReactContextBaseJavaModule{ } }); } + + /* 发送事件*/ + public static void sendEvent(String eventName, WritableMap params) { + ((ReactContext) mContext).getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, + params); + } }