1
0
mirror of https://gitcode.com/gh_mirrors/re/react-native-pushy.git synced 2025-09-18 01:26:10 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
tdzl2003
2016-04-04 23:02:28 +08:00
parent b9ea739a44
commit 063c14ab45
10 changed files with 394 additions and 346 deletions

View File

@@ -9,6 +9,7 @@ import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.ResponseBody;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
@@ -212,45 +213,37 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
copyFilesWithBlacklist("", from, to, blackList);
}
private void doDownload(DownloadTaskParams param) {
try {
downloadFile(param.url, param.zipFilePath);
private void doDownload(DownloadTaskParams param) throws IOException {
downloadFile(param.url, param.zipFilePath);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(param.zipFilePath)));
ZipEntry ze;
String filename;
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(param.zipFilePath)));
ZipEntry ze;
String filename;
removeDirectory(param.unzipDirectory);
param.unzipDirectory.mkdirs();
removeDirectory(param.unzipDirectory);
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;
}
unzipToFile(zis, fmd);
}
zis.close();
while ((ze = zis.getNextEntry()) != null)
{
String fn = ze.getName();
File fmd = new File(param.unzipDirectory, fn);
if (UpdateContext.DEBUG) {
Log.d("RNUpdate", "Unzip finished");
Log.d("RNUpdate", "Unzipping " + fn);
}
} catch (Throwable e) {
if (UpdateContext.DEBUG) {
e.printStackTrace();
if (ze.isDirectory()) {
fmd.mkdirs();
continue;
}
param.listener.onDownloadFailed(e);
unzipToFile(zis, fmd);
}
zis.close();
if (UpdateContext.DEBUG) {
Log.d("RNUpdate", "Unzip finished");
}
}
@@ -268,162 +261,161 @@ class DownloadTask extends AsyncTask<DownloadTaskParams, Void, Void> {
}
}
private void doPatchFromApk(DownloadTaskParams param) {
try {
downloadFile(param.url, param.zipFilePath);
private void doPatchFromApk(DownloadTaskParams param) throws IOException, JSONException {
downloadFile(param.url, param.zipFilePath);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(param.zipFilePath)));
ZipEntry ze;
int count;
String filename;
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(param.zipFilePath)));
ZipEntry ze;
int count;
String filename;
removeDirectory(param.unzipDirectory);
param.unzipDirectory.mkdirs();
removeDirectory(param.unzipDirectory);
param.unzipDirectory.mkdirs();
while ((ze = zis.getNextEntry()) != null)
{
String fn = ze.getName();
while ((ze = zis.getNextEntry()) != null)
{
String fn = ze.getName();
if (fn.equals("__diff.json")) {
// copy files from assets
byte[] bytes = readBytes(zis);
String json = new String(bytes, "UTF-8");
JSONObject obj = (JSONObject)new JSONTokener(json).nextValue();
if (fn.equals("__diff.json")) {
// copy files from assets
byte[] bytes = readBytes(zis);
String json = new String(bytes, "UTF-8");
JSONObject obj = (JSONObject)new JSONTokener(json).nextValue();
JSONObject copies = obj.getJSONObject("copies");
Iterator<?> keys = copies.keys();
while( keys.hasNext() ) {
String to = (String)keys.next();
String from = copies.getString(to);
if (from.isEmpty()) {
from = to;
}
copyFromResource(from, new File(param.unzipDirectory, to));
JSONObject copies = obj.getJSONObject("copies");
Iterator<?> keys = copies.keys();
while( keys.hasNext() ) {
String to = (String)keys.next();
String from = copies.getString(to);
if (from.isEmpty()) {
from = to;
}
continue;
copyFromResource(from, new File(param.unzipDirectory, to));
}
if (fn.equals("index.bundlejs.patch")) {
// do bsdiff patch
byte[] patched = bsdiffPatch(readOriginBundle(), readBytes(zis));
FileOutputStream fout = new FileOutputStream(new File(param.unzipDirectory, "index.bundlejs"));
fout.write(patched);
fout.close();
continue;
}
File fmd = new File(param.unzipDirectory, fn);
if (UpdateContext.DEBUG) {
Log.d("RNUpdate", "Unzipping " + fn);
}
if (ze.isDirectory()) {
fmd.mkdirs();
continue;
}
unzipToFile(zis, fmd);
continue;
}
if (fn.equals("index.bundlejs.patch")) {
// do bsdiff patch
byte[] patched = bsdiffPatch(readOriginBundle(), readBytes(zis));
zis.close();
FileOutputStream fout = new FileOutputStream(new File(param.unzipDirectory, "index.bundlejs"));
fout.write(patched);
fout.close();
continue;
}
File fmd = new File(param.unzipDirectory, fn);
if (UpdateContext.DEBUG) {
Log.d("RNUpdate", "Unzip finished");
Log.d("RNUpdate", "Unzipping " + fn);
}
} catch (Throwable e) {
if (UpdateContext.DEBUG) {
e.printStackTrace();
if (ze.isDirectory()) {
fmd.mkdirs();
continue;
}
param.listener.onDownloadFailed(e);
unzipToFile(zis, fmd);
}
zis.close();
if (UpdateContext.DEBUG) {
Log.d("RNUpdate", "Unzip finished");
}
}
private void doPatchFromPpk(DownloadTaskParams param) {
try {
downloadFile(param.url, param.zipFilePath);
private void doPatchFromPpk(DownloadTaskParams param) throws IOException, JSONException {
downloadFile(param.url, param.zipFilePath);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(param.zipFilePath)));
ZipEntry ze;
int count;
String filename;
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(param.zipFilePath)));
ZipEntry ze;
int count;
String filename;
removeDirectory(param.unzipDirectory);
param.unzipDirectory.mkdirs();
removeDirectory(param.unzipDirectory);
param.unzipDirectory.mkdirs();
while ((ze = zis.getNextEntry()) != null)
{
String fn = ze.getName();
while ((ze = zis.getNextEntry()) != null)
{
String fn = ze.getName();
if (fn.equals("__diff.json")) {
// copy files from assets
byte[] bytes = readBytes(zis);
String json = new String(bytes, "UTF-8");
JSONObject obj = (JSONObject)new JSONTokener(json).nextValue();
if (fn.equals("__diff.json")) {
// copy files from assets
byte[] bytes = readBytes(zis);
String json = new String(bytes, "UTF-8");
JSONObject obj = (JSONObject)new JSONTokener(json).nextValue();
JSONObject copies = obj.getJSONObject("copies");
Iterator<?> keys = copies.keys();
while( keys.hasNext() ) {
String to = (String)keys.next();
String from = copies.getString(to);
if (from.isEmpty()) {
from = to;
}
copyFile(new File(param.originDirectory, from), new File(param.unzipDirectory, to));
JSONObject copies = obj.getJSONObject("copies");
Iterator<?> keys = copies.keys();
while( keys.hasNext() ) {
String to = (String)keys.next();
String from = copies.getString(to);
if (from.isEmpty()) {
from = to;
}
JSONObject blackList = obj.getJSONObject("deletes");
copyFilesWithBlacklist(param.originDirectory, param.unzipDirectory, blackList);
continue;
copyFile(new File(param.originDirectory, from), new File(param.unzipDirectory, to));
}
if (fn.equals("index.bundlejs.patch")) {
// do bsdiff patch
byte[] patched = bsdiffPatch(readFile(new File(param.originDirectory, "index.bundlejs")), readBytes(zis));
FileOutputStream fout = new FileOutputStream(new File(param.unzipDirectory, "index.bundlejs"));
fout.write(patched);
fout.close();
continue;
}
File fmd = new File(param.unzipDirectory, fn);
if (UpdateContext.DEBUG) {
Log.d("RNUpdate", "Unzipping " + fn);
}
if (ze.isDirectory()) {
fmd.mkdirs();
continue;
}
unzipToFile(zis, fmd);
JSONObject blackList = obj.getJSONObject("deletes");
copyFilesWithBlacklist(param.originDirectory, param.unzipDirectory, blackList);
continue;
}
if (fn.equals("index.bundlejs.patch")) {
// do bsdiff patch
byte[] patched = bsdiffPatch(readFile(new File(param.originDirectory, "index.bundlejs")), readBytes(zis));
zis.close();
FileOutputStream fout = new FileOutputStream(new File(param.unzipDirectory, "index.bundlejs"));
fout.write(patched);
fout.close();
continue;
}
File fmd = new File(param.unzipDirectory, fn);
if (UpdateContext.DEBUG) {
Log.d("RNUpdate", "Unzip finished");
Log.d("RNUpdate", "Unzipping " + fn);
}
} catch (Throwable e) {
if (UpdateContext.DEBUG) {
e.printStackTrace();
if (ze.isDirectory()) {
fmd.mkdirs();
continue;
}
param.listener.onDownloadFailed(e);
unzipToFile(zis, fmd);
}
zis.close();
if (UpdateContext.DEBUG) {
Log.d("RNUpdate", "Unzip finished");
}
}
private void doCleanUp(DownloadTaskParams param) {
}
@Override
protected Void doInBackground(DownloadTaskParams... params) {
switch (params[0].type) {
case DownloadTaskParams.TASK_TYPE_FULL_DOWNLOAD:
doDownload(params[0]);
break;
case DownloadTaskParams.TASK_TYPE_PATCH_FROM_APK:
doPatchFromApk(params[0]);
break;
case DownloadTaskParams.TASK_TYPE_PATCH_FROM_PPK:
doPatchFromPpk(params[0]);
break;
try {
switch (params[0].type) {
case DownloadTaskParams.TASK_TYPE_FULL_DOWNLOAD:
doDownload(params[0]);
break;
case DownloadTaskParams.TASK_TYPE_PATCH_FROM_APK:
doPatchFromApk(params[0]);
break;
case DownloadTaskParams.TASK_TYPE_PATCH_FROM_PPK:
doPatchFromPpk(params[0]);
break;
case DownloadTaskParams.TASK_TYPE_CLEARUP:
doCleanUp(params[0]);
break;
}
params[0].listener.onDownloadCompleted();
} catch (Throwable e) {
if (UpdateContext.DEBUG) {
e.printStackTrace();
}
params[0].listener.onDownloadFailed(e);
}
return null;
}

View File

@@ -12,6 +12,8 @@ class DownloadTaskParams {
static final int TASK_TYPE_PATCH_FROM_APK = 2;
static final int TASK_TYPE_PATCH_FROM_PPK = 3;
static final int TASK_TYPE_CLEARUP = 0; //Keep hash & originHash
int type;
String url;
String hash;

View File

@@ -1,8 +1,12 @@
package cn.reactnative.modules.update;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import java.io.File;
import java.net.URL;
/**
* Created by tdzl2003 on 3/31/16.
@@ -13,13 +17,6 @@ public class UpdateContext {
public static boolean DEBUG = false;
private static UpdateContext currentInstance = null;
static UpdateContext instance() {
return currentInstance;
}
public UpdateContext(Context context) {
this.context = context;
@@ -28,12 +25,26 @@ public class UpdateContext {
if (!rootDir.exists()) {
rootDir.mkdir();
}
this.sp = context.getSharedPreferences("update", Context.MODE_PRIVATE);
}
public String getRootDir() {
return rootDir.toString();
}
public String getPackageVersion() {
PackageManager pm = context.getPackageManager();
PackageInfo pi = null;
try {
pi = pm.getPackageInfo(context.getPackageName(), 0);
return pi.versionName;
} catch( PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
public interface DownloadFileListener {
void onDownloadCompleted();
void onDownloadFailed(Throwable error);
@@ -73,4 +84,102 @@ public class UpdateContext {
params.originDirectory = new File(rootDir, originHashName);
new DownloadTask(context).execute(params);
}
private SharedPreferences sp;
public void switchVersion(String hashName) {
if (!new File(rootDir, hashName).exists()) {
throw new Error("Hash name not found, must download first.");
}
String lastVersion = getCurrentVersion();
SharedPreferences.Editor editor = sp.edit();
editor.putString("currentVersion", hashName);
if (lastVersion != null) {
editor.putString("lastVersion", hashName);
}
editor.putBoolean("firstTime", true);
editor.putBoolean("shouldRollback", false);
editor.putBoolean("rolledBack", false);
editor.apply();
}
public String getCurrentVersion() {
return sp.getString("currentVersion", null);
}
public boolean isFirstTime() {
return sp.getBoolean("firstTime", false);
}
public boolean isRolledBack() {
return sp.getBoolean("rolledBack", false);
}
public void clearFirstTimeMark() {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("firstTime", false);
editor.putBoolean("shouldRollback", false);
editor.apply();
this.clearUp();
}
public static String getBundleUrl(Context context) {
return new UpdateContext(context.getApplicationContext()).getBundleUrl();
}
public static String getBundleUrl(Context context, String defaultAssetsUrl) {
return new UpdateContext(context.getApplicationContext()).getBundleUrl(defaultAssetsUrl);
}
public String getBundleUrl() {
return getBundleUrl("assets://index.android.bundle");
}
public String getBundleUrl(String defaultAssetsUrl) {
// Test should rollback.
if (sp.getBoolean("shouldRollback", false)) {
this.rollBack();
}
String currentVersion = getCurrentVersion();
if (currentVersion == null) {
return defaultAssetsUrl;
}
if (sp.getBoolean("firstTime", false)) {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("shouldRollback", true);
editor.apply();
}
return new File(rootDir, currentVersion+"/index.bundlejs").toURI().toString();
}
private void rollBack() {
String lastVersion = sp.getString("lastVersion", null);
if (lastVersion == null) {
throw new Error("This should never happen");
}
SharedPreferences.Editor editor = sp.edit();
editor.putString("currentVersion", lastVersion);
editor.putBoolean("shouldRollback", false);
editor.putBoolean("firstTime", false);
editor.putBoolean("rolledBack", true);
editor.apply();
}
private void clearUp() {
DownloadTaskParams params = new DownloadTaskParams();
params.type = DownloadTaskParams.TASK_TYPE_CLEARUP;
params.hash = sp.getString("currentVersion", null);
params.originHash = sp.getString("lastVersion", null);;
params.listener = new DownloadFileListener() {
@Override
public void onDownloadCompleted() {
}
@Override
public void onDownloadFailed(Throwable error) {
}
};
new DownloadTask(context).execute(params);
}
}

View File

@@ -1,10 +1,14 @@
package cn.reactnative.modules.update;
import android.app.Activity;
import android.content.Intent;
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 com.facebook.react.bridge.UiThreadUtil;
import java.util.HashMap;
import java.util.Map;
@@ -15,15 +19,23 @@ import java.util.Map;
public class UpdateModule extends ReactContextBaseJavaModule{
UpdateContext updateContext;
public UpdateModule(ReactApplicationContext reactContext) {
public UpdateModule(ReactApplicationContext reactContext, UpdateContext updateContext) {
super(reactContext);
this.updateContext = new UpdateContext(reactContext.getApplicationContext());
this.updateContext = updateContext;
}
public UpdateModule(ReactApplicationContext reactContext) {
this(reactContext, new UpdateContext(reactContext.getApplicationContext()));
}
@Override
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("downloadRootDir", updateContext.getRootDir());
constants.put("packageVersion", updateContext.getPackageVersion());
constants.put("currentVersion", updateContext.getCurrentVersion());
constants.put("firstTime", updateContext.isFirstTime());
constants.put("rolledBack", updateContext.isRolledBack());
return constants;
}
@@ -50,7 +62,7 @@ public class UpdateModule extends ReactContextBaseJavaModule{
}
@ReactMethod
public void downloadPatchFromApk(ReadableMap options, final Promise promise){
public void downloadPatchFromPackage(ReadableMap options, final Promise promise){
String url = options.getString("updateUrl");
String hash = options.getString("hashName");
updateContext.downloadPatchFromApk(url, hash, new UpdateContext.DownloadFileListener() {
@@ -83,4 +95,32 @@ public class UpdateModule extends ReactContextBaseJavaModule{
}
});
}
@ReactMethod
public void reloadUpdate(ReadableMap options) {
final String hash = options.getString("hashName");
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
updateContext.switchVersion(hash);
Activity activity = getCurrentActivity();
Intent intent = activity.getIntent();
activity.finish();
activity.startActivity(intent);
}
});
}
@ReactMethod
public void setNeedUpdate(ReadableMap options) {
final String hash = options.getString("hashName");
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
updateContext.switchVersion(hash);
}
});
}
}