mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-16 12:11:39 +08:00
android 下载进度发送事件发出和发送事件频率优化
This commit is contained in:
@@ -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<DownloadTaskParams, Void, Void> {
|
||||
class DownloadTask extends AsyncTask<DownloadTaskParams, long[], Void> {
|
||||
final int DOWNLOAD_CHUNK_SIZE = 4096;
|
||||
|
||||
Context context;
|
||||
@@ -91,11 +93,17 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
|
||||
|
||||
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<DownloadTaskParams, Void, Void> {
|
||||
}
|
||||
}
|
||||
|
||||
@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);
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user