1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

Record local hash info

This commit is contained in:
sunnylqm 2021-10-04 23:19:22 +08:00
parent d32d395a2a
commit bf1701031b
5 changed files with 54 additions and 25 deletions

View File

@ -15,7 +15,6 @@ import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
@ -32,9 +31,8 @@ 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, long[], Void> {
final int DOWNLOAD_CHUNK_SIZE = 4096;

View File

@ -17,9 +17,6 @@ import java.util.concurrent.Executors;
import java.io.File;
/**
* Created by tdzl2003 on 3/31/16.
*/
public class UpdateContext {
private Context context;
private File rootDir;
@ -72,10 +69,6 @@ public class UpdateContext {
return context.getString(R.string.pushy_build_time);
}
public String getUuid() {
return sp.getString("uuid", null);
}
public Map getBlockUpdate() {
return new HashMap<String, Object>() {{
put("until", sp.getInt("blockUntil", 0));
@ -163,12 +156,16 @@ public class UpdateContext {
editor.apply();
}
public void setUuid(String uuid) {
public void setKv(String key, String value) {
SharedPreferences.Editor editor = sp.edit();
editor.putString("uuid", uuid);
editor.putString(key, value);
editor.apply();
}
public String getKv(String key) {
return sp.getString(key, null);
}
public void setBlockUpdate(int until, String reason) {
SharedPreferences.Editor editor = sp.edit();
editor.putInt("blockUntil", until);
@ -191,7 +188,11 @@ public class UpdateContext {
public void markSuccess() {
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("firstTimeOk", true);
editor.remove("lastVersion");
String lastVersion = sp.getString("lastVersion", null);
if (lastVersion != null) {
editor.remove("lastVersion");
editor.remove("hash_" + lastVersion);
}
editor.apply();
this.cleanUp();

View File

@ -22,15 +22,11 @@ import com.facebook.react.modules.core.DeviceEventManagerModule;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import static android.support.v4.content.FileProvider.getUriForFile;
/**
* Created by tdzl2003 on 3/31/16.
*/
public class UpdateModule extends ReactContextBaseJavaModule {
UpdateContext updateContext;
public static ReactApplicationContext mContext;
@ -64,7 +60,7 @@ public class UpdateModule extends ReactContextBaseJavaModule {
updateContext.clearRollbackMark();
}
constants.put("blockUpdate", updateContext.getBlockUpdate());
constants.put("uuid", updateContext.getUuid());
constants.put("uuid", updateContext.getKv("uuid"));
return constants;
}
@ -264,11 +260,27 @@ public class UpdateModule extends ReactContextBaseJavaModule {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
updateContext.setUuid(uuid);
updateContext.setKv("uuid", uuid);
}
});
}
@ReactMethod
public void setLocalHashInfo(final String hash, final String info) {
UiThreadUtil.runOnUiThread(new Runnable() {
@Override
public void run() {
updateContext.setKv("hash_" + hash, info);
}
});
}
@ReactMethod
public void getLocalHashInfo(final String hash, final Promise promise) {
promise.resolve(updateContext.getKv("hash_" + hash));
}
/* 发送事件*/
public static void sendEvent(String eventName, WritableMap params) {
((ReactContext) mContext).getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName,

6
lib/index.d.ts vendored
View File

@ -72,6 +72,12 @@ export function setCustomEndpoints({
backupQueryUrl?: string;
}): void;
export function getCurrentVersionInfo(): Promise<{
name?: string;
description?: string;
metaInfo?: string;
}>;
interface ProgressData {
hash: string;
received: number;

View File

@ -21,11 +21,6 @@ if (!Pushy) {
throw new Error('react-native-update模块无法加载请对照安装文档检查配置。');
}
// TODO: save and export current version info
// name
// description
// metaInfo
export const downloadRootDir = Pushy.downloadRootDir;
export const packageVersion = Pushy.packageVersion;
export const currentVersion = Pushy.currentVersion;
@ -41,6 +36,18 @@ if (Platform.OS === 'android' && !Pushy.isUsingBundleUrl) {
);
}
function setLocalHashInfo(hash, info) {
Pushy.setLocalHashInfo(hash, JSON.stringify(info));
}
async function getLocalHashInfo(hash) {
return JSON.parse(await Pushy.getLocalHashInfo(hash));
}
export async function getCurrentVersionInfo() {
return currentVersion ? getLocalHashInfo(currentVersion) : {};
}
const eventEmitter = new NativeEventEmitter(Pushy);
if (!uuid) {
@ -219,6 +226,11 @@ export async function downloadUpdate(options, eventListeners) {
hash: options.hash,
});
}
setLocalHashInfo(options.hash, {
name: options.name,
description: options.description,
metaInfo: options.metaInfo,
});
progressHandler && progressHandler.remove();
downloadedHash = options.hash;
return options.hash;