mirror of
				https://gitcode.com/gh_mirrors/re/react-native-pushy.git
				synced 2025-10-31 13:23:12 +08:00 
			
		
		
		
	feat: project init
This commit is contained in:
		| @@ -58,6 +58,7 @@ repositories { | ||||
|  | ||||
| dependencies { | ||||
|     implementation 'com.facebook.react:react-native:+' | ||||
|     implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.0' | ||||
| } | ||||
| if (isNewArchitectureEnabled()) { | ||||
|     react { | ||||
|   | ||||
| @@ -10,8 +10,19 @@ import com.facebook.react.bridge.Promise; | ||||
| import com.facebook.react.bridge.ReactApplicationContext; | ||||
| import com.facebook.react.bridge.ReadableMap; | ||||
| import com.facebook.react.bridge.UiThreadUtil; | ||||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||||
|  | ||||
| import org.json.JSONObject; | ||||
|  | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.lang.reflect.Field; | ||||
| import java.util.HashMap; | ||||
| import java.util.Map; | ||||
| import java.util.Optional; | ||||
| import java.util.regex.Matcher; | ||||
| import java.util.regex.Pattern; | ||||
|  | ||||
| public class UpdateModuleImpl { | ||||
|  | ||||
|     public static final String NAME = "Pushy"; | ||||
| @@ -72,27 +83,35 @@ public class UpdateModuleImpl { | ||||
|     } | ||||
|  | ||||
|     public static void downloadPatchFromPpk(UpdateContext updateContext, ReadableMap options, Promise promise) { | ||||
|         String url = options.getString("updateUrl"); | ||||
|         String hash = options.getString("hash"); | ||||
|         try { | ||||
|             String url = options.getString("updateUrl"); | ||||
|             String hash = options.getString("hash"); | ||||
|  | ||||
|         String originHash = options.getString("originHash"); | ||||
|             String originHash = options.getString("originHash"); | ||||
|  | ||||
|         updateContext.downloadPatchFromPpk(url, hash, originHash, new UpdateContext.DownloadFileListener() { | ||||
|             @Override | ||||
|             public void onDownloadCompleted(DownloadTaskParams params) { | ||||
|                 promise.resolve(null); | ||||
|             } | ||||
|             updateContext.downloadPatchFromPpk(url, hash, originHash, new UpdateContext.DownloadFileListener() { | ||||
|                 @Override | ||||
|                 public void onDownloadCompleted(DownloadTaskParams params) { | ||||
|                     promise.resolve(null); | ||||
|                 } | ||||
|  | ||||
|             @Override | ||||
|             public void onDownloadFailed(Throwable error) { | ||||
|                 promise.reject(error); | ||||
|             } | ||||
|         }); | ||||
|                 @Override | ||||
|                 public void onDownloadFailed(Throwable error) { | ||||
|                     promise.reject(error); | ||||
|                 } | ||||
|             }); | ||||
|         }catch (Exception e){ | ||||
|             promise.reject("执行报错:"+e.getMessage()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static void reloadUpdate(UpdateContext updateContext, ReactApplicationContext mContext, ReadableMap options) { | ||||
|     public static void reloadUpdate(UpdateContext updateContext, ReactApplicationContext mContext, ReadableMap options,Promise promise) { | ||||
|         final String hash = options.getString("hash"); | ||||
|  | ||||
|         if(hash==null || hash.isEmpty()){ | ||||
|             promise.reject("hash不能为空"); | ||||
|             return; | ||||
|         } | ||||
|         UiThreadUtil.runOnUiThread(new Runnable() { | ||||
|             @Override | ||||
|             public void run() { | ||||
| @@ -112,6 +131,7 @@ public class UpdateModuleImpl { | ||||
|                         loadField.setAccessible(true); | ||||
|                         loadField.set(instanceManager, loader); | ||||
|                     } catch (Throwable err) { | ||||
|                         promise.reject("pushy:"+err.getMessage()); | ||||
|                         Field jsBundleField = instanceManager.getClass().getDeclaredField("mJSBundleFile"); | ||||
|                         jsBundleField.setAccessible(true); | ||||
|                         jsBundleField.set(instanceManager, UpdateContext.getBundleUrl(application)); | ||||
| @@ -119,11 +139,14 @@ public class UpdateModuleImpl { | ||||
|  | ||||
|                     try { | ||||
|                         instanceManager.recreateReactContextInBackground(); | ||||
|                         promise.resolve(true); | ||||
|                     } catch (Throwable err) { | ||||
|                         promise.reject("pushy:"+err.getMessage()); | ||||
|                         activity.recreate(); | ||||
|                     } | ||||
|  | ||||
|                 } catch (Throwable err) { | ||||
|                     promise.reject("pushy:switchVersion failed"+err.getMessage()); | ||||
|                     Log.e("pushy", "switchVersion failed", err); | ||||
|                 } | ||||
|             } | ||||
| @@ -131,61 +154,112 @@ public class UpdateModuleImpl { | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public static void setNeedUpdate(UpdateContext updateContext, ReadableMap options) { | ||||
|         final String hash = options.getString("hash"); | ||||
|     public static void setNeedUpdate(UpdateContext updateContext, ReadableMap options,Promise promise) { | ||||
|         try { | ||||
|             final String hash = options.getString("hash"); | ||||
|             if(hash==null || hash.isEmpty()){ | ||||
|                 promise.reject("hash不能为空"); | ||||
|                 return; | ||||
|             } | ||||
|             UiThreadUtil.runOnUiThread(new Runnable() { | ||||
|                 @Override | ||||
|                 public void run() { | ||||
|                     try { | ||||
|                         updateContext.switchVersion(hash); | ||||
|                         promise.resolve(true); | ||||
|                     } catch (Throwable err) { | ||||
|                         promise.reject("switchVersionLater failed:"+err.getMessage()); | ||||
|                         Log.e("pushy", "switchVersionLater failed", err); | ||||
|                     } | ||||
|                 } | ||||
|             }); | ||||
|         }catch (Exception e){ | ||||
|             promise.reject("执行报错:"+e.getMessage()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static void markSuccess(UpdateContext updateContext,Promise promise) { | ||||
|         try { | ||||
|             UiThreadUtil.runOnUiThread(new Runnable() { | ||||
|                 @Override | ||||
|                 public void run() { | ||||
|                     updateContext.markSuccess(); | ||||
|                     promise.resolve(true); | ||||
|                 } | ||||
|             }); | ||||
|         }catch (Exception e){ | ||||
|             promise.reject("执行报错:"+e.getMessage()); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public static void setBlockUpdate(UpdateContext updateContext, ReadableMap options,Promise promise) { | ||||
|         try { | ||||
|             final int until = options.getInt("until"); | ||||
|             final String reason = options.getString("reason"); | ||||
|             UiThreadUtil.runOnUiThread(new Runnable() { | ||||
|                 @Override | ||||
|                 public void run() { | ||||
|                     updateContext.setBlockUpdate(until, reason); | ||||
|                 } | ||||
|             }); | ||||
|             promise.resolve(true); | ||||
|         }catch (Exception e){ | ||||
|             promise.reject("执行报错:"+e.getMessage()); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
|     public static void setUuid(UpdateContext updateContext, String uuid, Promise promise) { | ||||
|         try { | ||||
|             UiThreadUtil.runOnUiThread(new Runnable() { | ||||
|                 @Override | ||||
|                 public void run() { | ||||
|                     updateContext.setKv("uuid", uuid); | ||||
|                     promise.resolve(true); | ||||
|                 } | ||||
|             }); | ||||
|         }catch (Exception e){ | ||||
|             promise.reject("执行报错:"+e.getMessage()); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
|     public static boolean check(String json) { | ||||
|         ObjectMapper mapper = new ObjectMapper(); | ||||
|         try { | ||||
|             mapper.readValue(json, Map.class); | ||||
|             System.out.println("String can be converted to Map"); | ||||
|             return  true; | ||||
|         } catch (IOException e) { | ||||
|             System.out.println("String cannot be converted to Map"); | ||||
|             return  false; | ||||
|         } | ||||
|     } | ||||
|  | ||||
|  | ||||
|     public static void setLocalHashInfo(UpdateContext updateContext, final String hash, final String info, Promise promise) { | ||||
|         UiThreadUtil.runOnUiThread(new Runnable() { | ||||
|             @Override | ||||
|             public void run() { | ||||
|                 try { | ||||
|                     updateContext.switchVersion(hash); | ||||
|                 } catch (Throwable err) { | ||||
|                     Log.e("pushy", "switchVersionLater failed", err); | ||||
|                 if(!check(info)){ | ||||
|                     updateContext.setKv("hash_" + hash, info); | ||||
|                     promise.reject("校验报错:json字符串格式错误"); | ||||
|                 }else { | ||||
|                     updateContext.setKv("hash_" + hash, info); | ||||
|                     promise.resolve(true); | ||||
|                 } | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     public static void markSuccess(UpdateContext updateContext) { | ||||
|         UiThreadUtil.runOnUiThread(new Runnable() { | ||||
|             @Override | ||||
|             public void run() { | ||||
|                 updateContext.markSuccess(); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     public static void setBlockUpdate(UpdateContext updateContext, ReadableMap options) { | ||||
|         final int until = options.getInt("until"); | ||||
|         final String reason = options.getString("reason"); | ||||
|         UiThreadUtil.runOnUiThread(new Runnable() { | ||||
|             @Override | ||||
|             public void run() { | ||||
|                 updateContext.setBlockUpdate(until, reason); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     public static void setUuid(UpdateContext updateContext, String uuid) { | ||||
|         UiThreadUtil.runOnUiThread(new Runnable() { | ||||
|             @Override | ||||
|             public void run() { | ||||
|                 updateContext.setKv("uuid", uuid); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     public static void setLocalHashInfo(UpdateContext updateContext, final String hash, final String info) { | ||||
|         UiThreadUtil.runOnUiThread(new Runnable() { | ||||
|             @Override | ||||
|             public void run() { | ||||
|                 updateContext.setKv("hash_" + hash, info); | ||||
|             } | ||||
|         }); | ||||
|     } | ||||
|  | ||||
|     public static void getLocalHashInfo(UpdateContext updateContext, final String hash, Promise promise) { | ||||
|         promise.resolve(updateContext.getKv("hash_" + hash)); | ||||
|         String value = updateContext.getKv("hash_" + hash); | ||||
|         if(check(value)){ | ||||
|             promise.resolve(value); | ||||
|         }else { | ||||
|             promise.reject("校验报错:json字符串格式错误"); | ||||
|         } | ||||
|  | ||||
|     } | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -94,33 +94,33 @@ public class UpdateModule extends NativeUpdateSpec { | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void reloadUpdate(ReadableMap options) { | ||||
|         UpdateModuleImpl.reloadUpdate(updateContext, mContext, options); | ||||
|     public void reloadUpdate(ReadableMap options,Promise promise) { | ||||
|         UpdateModuleImpl.reloadUpdate(updateContext, mContext, options,promise); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void setNeedUpdate(ReadableMap options) { | ||||
|         UpdateModuleImpl.setNeedUpdate(updateContext, options); | ||||
|     public void setNeedUpdate(ReadableMap options,Promise promise) { | ||||
|         UpdateModuleImpl.setNeedUpdate(updateContext, options,promise); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void markSuccess() { | ||||
|         UpdateModuleImpl.markSuccess(updateContext); | ||||
|     public void markSuccess(Promise promise) { | ||||
|         UpdateModuleImpl.markSuccess(updateContext,promise); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void setBlockUpdate(ReadableMap options) { | ||||
|         UpdateModuleImpl.setBlockUpdate(updateContext,options); | ||||
|     public void setBlockUpdate(ReadableMap options,Promise promise) { | ||||
|         UpdateModuleImpl.setBlockUpdate(updateContext,options,promise); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void setUuid(final String uuid) { | ||||
|         UpdateModuleImpl.setUuid(updateContext,uuid); | ||||
|     public void setUuid(final String uuid, Promise promise) { | ||||
|         UpdateModuleImpl.setUuid(updateContext,uuid,promise); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void setLocalHashInfo(final String hash, final String info) { | ||||
|         UpdateModuleImpl.setLocalHashInfo(updateContext,hash,info); | ||||
|     public void setLocalHashInfo(final String hash, final String info, final Promise promise) { | ||||
|         UpdateModuleImpl.setLocalHashInfo(updateContext,hash,info,promise); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 steven
					steven