mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-10-07 22:55:13 +08:00
working on android
This commit is contained in:
5
android/src/main/AndroidManifest.xml
Normal file
5
android/src/main/AndroidManifest.xml
Normal file
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="cn.reactnative.modules.update">
|
||||
|
||||
</manifest>
|
@@ -0,0 +1,115 @@
|
||||
package cn.reactnative.modules.update;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.util.Log;
|
||||
|
||||
import com.squareup.okhttp.OkHttpClient;
|
||||
import com.squareup.okhttp.Request;
|
||||
import com.squareup.okhttp.Response;
|
||||
import com.squareup.okhttp.ResponseBody;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipInputStream;
|
||||
|
||||
import okio.BufferedSink;
|
||||
import okio.BufferedSource;
|
||||
import okio.Okio;
|
||||
|
||||
/**
|
||||
* Created by tdzl2003 on 3/31/16.
|
||||
*/
|
||||
class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
|
||||
final int DOWNLOAD_CHUNK_SIZE = 4096;
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(DownloadTaskParams... params) {
|
||||
DownloadTaskParams param = params[0];
|
||||
try {
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
Request request = new Request.Builder().url(param.url)
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
ResponseBody body = response.body();
|
||||
long contentLength = body.contentLength();
|
||||
BufferedSource source = body.source();
|
||||
|
||||
if (param.zipFilePath.exists()) {
|
||||
param.zipFilePath.delete();
|
||||
}
|
||||
|
||||
BufferedSink sink = Okio.buffer(Okio.sink(param.zipFilePath));
|
||||
|
||||
if (UpdateContext.DEBUG) {
|
||||
Log.d("RNUpdate", "Downloading " + param.url);
|
||||
}
|
||||
|
||||
long bytesRead = 0;
|
||||
long totalRead = 0;
|
||||
while ((bytesRead = source.read(sink.buffer(), DOWNLOAD_CHUNK_SIZE)) != -1) {
|
||||
totalRead += bytesRead;
|
||||
if (UpdateContext.DEBUG) {
|
||||
Log.d("RNUpdate", "Progress " + totalRead + "/" + contentLength);
|
||||
}
|
||||
}
|
||||
if (totalRead != contentLength) {
|
||||
throw new Error("Unexpected eof while reading ppk");
|
||||
}
|
||||
sink.writeAll(source);
|
||||
sink.close();
|
||||
|
||||
if (UpdateContext.DEBUG) {
|
||||
Log.d("RNUpdate", "Download finished");
|
||||
}
|
||||
|
||||
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(param.zipFilePath)));
|
||||
ZipEntry ze;
|
||||
byte[] buffer = new byte[1024];
|
||||
int count;
|
||||
String filename;
|
||||
|
||||
param.unzipDirectory.mkdirs();
|
||||
|
||||
while ((ze = zis.getNextEntry()) != null)
|
||||
{
|
||||
String fn = ze.getName();
|
||||
File fmd = new File(param.unzipDirectory, fn);
|
||||
|
||||
if (UpdateContext.DEBUG) {
|
||||
Log.d("RNUpdate", "Unzipping " + fn);
|
||||
}
|
||||
|
||||
if (ze.isDirectory()) {
|
||||
fmd.mkdirs();
|
||||
continue;
|
||||
}
|
||||
|
||||
FileOutputStream fout = new FileOutputStream(fmd);
|
||||
|
||||
while ((count = zis.read(buffer)) != -1)
|
||||
{
|
||||
fout.write(buffer, 0, count);
|
||||
}
|
||||
|
||||
fout.close();
|
||||
zis.closeEntry();
|
||||
}
|
||||
|
||||
zis.close();
|
||||
|
||||
if (UpdateContext.DEBUG) {
|
||||
Log.d("RNUpdate", "Unzip finished");
|
||||
}
|
||||
|
||||
} catch (Throwable e) {
|
||||
param.listener.onDownloadFailed(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,14 @@
|
||||
package cn.reactnative.modules.update;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Created by tdzl2003 on 3/31/16.
|
||||
*/
|
||||
class DownloadTaskParams {
|
||||
String url;
|
||||
String hash;
|
||||
File zipFilePath;
|
||||
File unzipDirectory;
|
||||
UpdateContext.DownloadFileListener listener;
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
package cn.reactnative.modules.update;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/**
|
||||
* Created by tdzl2003 on 3/31/16.
|
||||
*/
|
||||
public class UpdateContext {
|
||||
private Context context;
|
||||
private File rootDir;
|
||||
|
||||
public static boolean DEBUG = false;
|
||||
|
||||
|
||||
private static UpdateContext currentInstance = null;
|
||||
|
||||
static UpdateContext instance() {
|
||||
return currentInstance;
|
||||
}
|
||||
|
||||
public UpdateContext(Context context) {
|
||||
this.context = context;
|
||||
|
||||
this.rootDir = new File(context.getFilesDir(), "_update");
|
||||
|
||||
if (!rootDir.exists()) {
|
||||
rootDir.mkdir();
|
||||
}
|
||||
}
|
||||
|
||||
public String getRootDir() {
|
||||
return rootDir.toString();
|
||||
}
|
||||
|
||||
public interface DownloadFileListener {
|
||||
void onDownloadCompleted();
|
||||
void onDownloadFailed(Throwable error);
|
||||
}
|
||||
|
||||
public void downloadFile(String url, String hashName, DownloadFileListener listener) {
|
||||
DownloadTaskParams params = new DownloadTaskParams();
|
||||
params.url = url;
|
||||
params.hash = hashName;
|
||||
params.listener = listener;
|
||||
params.zipFilePath = new File(rootDir, hashName + ".ppk");
|
||||
params.unzipDirectory = new File(rootDir, hashName);
|
||||
new DownloadTask().execute(params);
|
||||
}
|
||||
}
|
@@ -0,0 +1,51 @@
|
||||
package cn.reactnative.modules.update;
|
||||
|
||||
import com.facebook.react.bridge.Promise;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
||||
import com.facebook.react.bridge.ReactMethod;
|
||||
import com.facebook.react.bridge.ReadableMap;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by tdzl2003 on 3/31/16.
|
||||
*/
|
||||
public class UpdateModule extends ReactContextBaseJavaModule{
|
||||
UpdateContext updateContext;
|
||||
|
||||
public UpdateModule(ReactApplicationContext reactContext) {
|
||||
super(reactContext);
|
||||
this.updateContext = new UpdateContext(reactContext.getApplicationContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Object> getConstants() {
|
||||
final Map<String, Object> constants = new HashMap<>();
|
||||
constants.put("downloadRootDir", updateContext.getRootDir());
|
||||
return constants;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "RCTHotUpdate";
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void downloadUpdate(ReadableMap options, final Promise promise){
|
||||
String url = options.getString("updateUrl");
|
||||
String hash = options.getString("hashName");
|
||||
updateContext.downloadFile(url, hash, new UpdateContext.DownloadFileListener() {
|
||||
@Override
|
||||
public void onDownloadCompleted() {
|
||||
promise.resolve(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDownloadFailed(Throwable error) {
|
||||
promise.reject(error);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@@ -0,0 +1,34 @@
|
||||
package cn.reactnative.modules.update;
|
||||
|
||||
import com.facebook.react.ReactPackage;
|
||||
import com.facebook.react.bridge.JavaScriptModule;
|
||||
import com.facebook.react.bridge.NativeModule;
|
||||
import com.facebook.react.bridge.ReactApplicationContext;
|
||||
import com.facebook.react.uimanager.ViewManager;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by tdzl2003 on 3/31/16.
|
||||
*/
|
||||
public class UpdatePackage implements ReactPackage {
|
||||
|
||||
@Override
|
||||
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
|
||||
return Arrays.asList(new NativeModule[]{
|
||||
// Modules from third-party
|
||||
new UpdateModule(reactContext),
|
||||
});
|
||||
}
|
||||
|
||||
public List<Class<? extends JavaScriptModule>> createJSModules() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user