mirror of
				https://gitcode.com/gh_mirrors/re/react-native-pushy.git
				synced 2025-11-01 05:43:11 +08:00 
			
		
		
		
	fix android reload in bridge-less mode
This commit is contained in:
		| @@ -98,23 +98,20 @@ public class UpdateModuleImpl { | |||||||
|                 } |                 } | ||||||
|             }); |             }); | ||||||
|         }catch (Exception e){ |         }catch (Exception e){ | ||||||
|             promise.reject("执行报错:" + e.getMessage()); |             promise.reject("downloadPatchFromPpk failed: "+e.getMessage()); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static void reloadUpdate(UpdateContext updateContext, ReactApplicationContext mContext, ReadableMap options, Promise promise) { |     public static void reloadUpdate(UpdateContext updateContext, ReactApplicationContext mContext, ReadableMap options, Promise promise) { | ||||||
|         final String hash = options.getString("hash"); |         final String hash = options.getString("hash"); | ||||||
|  |  | ||||||
|         if (hash == null || hash.isEmpty()) { |  | ||||||
|             promise.reject("hash不能为空"); |  | ||||||
|             return; |  | ||||||
|         } |  | ||||||
|         UiThreadUtil.runOnUiThread(new Runnable() { |         UiThreadUtil.runOnUiThread(new Runnable() { | ||||||
|             @Override |             @Override | ||||||
|             public void run() { |             public void run() { | ||||||
|  |  | ||||||
|  |                 updateContext.switchVersion(hash); | ||||||
|  |                 final Context application = mContext.getApplicationContext(); | ||||||
|  |                 JSBundleLoader loader = JSBundleLoader.createFileLoader(UpdateContext.getBundleUrl(application)); | ||||||
|                 try { |                 try { | ||||||
|                     updateContext.switchVersion(hash); |  | ||||||
|                     final Context application = mContext.getApplicationContext(); |  | ||||||
|                     ReactInstanceManager instanceManager = updateContext.getCustomReactInstanceManager(); |                     ReactInstanceManager instanceManager = updateContext.getCustomReactInstanceManager(); | ||||||
|  |  | ||||||
|                     if (instanceManager == null) { |                     if (instanceManager == null) { | ||||||
| @@ -122,12 +119,10 @@ public class UpdateModuleImpl { | |||||||
|                     } |                     } | ||||||
|  |  | ||||||
|                     try { |                     try { | ||||||
|                         JSBundleLoader loader = JSBundleLoader.createFileLoader(UpdateContext.getBundleUrl(application)); |  | ||||||
|                         Field loadField = instanceManager.getClass().getDeclaredField("mBundleLoader"); |                         Field loadField = instanceManager.getClass().getDeclaredField("mBundleLoader"); | ||||||
|                         loadField.setAccessible(true); |                         loadField.setAccessible(true); | ||||||
|                         loadField.set(instanceManager, loader); |                         loadField.set(instanceManager, loader); | ||||||
|                     } catch (Throwable err) { |                     } catch (Throwable err) { | ||||||
|                         promise.reject("pushy:"+err.getMessage()); |  | ||||||
|                         Field jsBundleField = instanceManager.getClass().getDeclaredField("mJSBundleFile"); |                         Field jsBundleField = instanceManager.getClass().getDeclaredField("mJSBundleFile"); | ||||||
|                         jsBundleField.setAccessible(true); |                         jsBundleField.setAccessible(true); | ||||||
|                         jsBundleField.set(instanceManager, UpdateContext.getBundleUrl(application)); |                         jsBundleField.set(instanceManager, UpdateContext.getBundleUrl(application)); | ||||||
| @@ -137,31 +132,36 @@ public class UpdateModuleImpl { | |||||||
|                     promise.resolve(true); |                     promise.resolve(true); | ||||||
|  |  | ||||||
|                 } catch (Throwable err) { |                 } catch (Throwable err) { | ||||||
|                     promise.reject(err); |  | ||||||
|                     Log.e("pushy", "switchVersion failed ", err); |  | ||||||
|                     final Activity currentActivity = mContext.getCurrentActivity(); |                     final Activity currentActivity = mContext.getCurrentActivity(); | ||||||
|                     if (currentActivity == null) { |                     if (currentActivity == null) { | ||||||
|                         return; |                         return; | ||||||
|                     } |                     } | ||||||
|                     try { |                     try { | ||||||
|                         // Try to get getReactDelegate method using reflection |  | ||||||
|                         java.lang.reflect.Method getReactDelegateMethod =  |                         java.lang.reflect.Method getReactDelegateMethod =  | ||||||
|                             ReactActivity.class.getMethod("getReactDelegate"); |                             ReactActivity.class.getMethod("getReactDelegate"); | ||||||
|                         if (getReactDelegateMethod != null) { |  | ||||||
|                             ReactDelegate reactDelegate = (ReactDelegate)  |                         ReactDelegate reactDelegate = (ReactDelegate)  | ||||||
|                                 getReactDelegateMethod.invoke(currentActivity); |                             getReactDelegateMethod.invoke(currentActivity); | ||||||
|                              |  | ||||||
|                             // Try to get reload method using reflection |                         Field reactHostField = ReactDelegate.class.getDeclaredField("mReactHost"); | ||||||
|                             java.lang.reflect.Method reloadMethod =  |                         reactHostField.setAccessible(true); | ||||||
|                                 ReactDelegate.class.getMethod("reload"); |                         Object reactHost = reactHostField.get(reactDelegate); | ||||||
|                             if (reloadMethod != null) { |  | ||||||
|                                 reloadMethod.invoke(reactDelegate); |                         // Access the mReactHostDelegate field | ||||||
|                             } else { |                         Field reactHostDelegateField = reactHost.getClass().getDeclaredField("mReactHostDelegate"); | ||||||
|                                 throw new NoSuchMethodException(); |                         reactHostDelegateField.setAccessible(true); | ||||||
|                             } |                         Object reactHostDelegate = reactHostDelegateField.get(reactHost); | ||||||
|                         } else { |  | ||||||
|                             throw new NoSuchMethodException(); |                         // Modify the jsBundleLoader field | ||||||
|                         } |                         Field jsBundleLoaderField = reactHostDelegate.getClass().getDeclaredField("jsBundleLoader"); | ||||||
|  |                         jsBundleLoaderField.setAccessible(true); | ||||||
|  |                         jsBundleLoaderField.set(reactHostDelegate, loader); | ||||||
|  |                          | ||||||
|  |                         // Get the reload method with a String parameter | ||||||
|  |                         java.lang.reflect.Method reloadMethod = reactHost.getClass().getMethod("reload", String.class); | ||||||
|  |  | ||||||
|  |                         // Invoke the reload method with a reason | ||||||
|  |                         reloadMethod.invoke(reactHost, "react-native-update"); | ||||||
|                     } catch (Throwable e) { |                     } catch (Throwable e) { | ||||||
|                         currentActivity.runOnUiThread(new Runnable() { |                         currentActivity.runOnUiThread(new Runnable() { | ||||||
|                             @Override |                             @Override | ||||||
| @@ -171,72 +171,54 @@ public class UpdateModuleImpl { | |||||||
|                         }); |                         }); | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|  |                 promise.resolve(true); | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |  | ||||||
|     public static void setNeedUpdate(UpdateContext updateContext, ReadableMap options,Promise promise) { |     public static void setNeedUpdate(UpdateContext updateContext, ReadableMap options, Promise promise) { | ||||||
|         try { |         final String hash = options.getString("hash"); | ||||||
|             final String hash = options.getString("hash"); |         UiThreadUtil.runOnUiThread(new Runnable() { | ||||||
|             if(hash==null || hash.isEmpty()){ |             @Override | ||||||
|                 promise.reject("hash不能为空"); |             public void run() { | ||||||
|                 return; |                 try { | ||||||
|             } |                     updateContext.switchVersion(hash); | ||||||
|             UiThreadUtil.runOnUiThread(new Runnable() { |                     promise.resolve(true); | ||||||
|                 @Override |                 } catch (Throwable err) { | ||||||
|                 public void run() { |                     promise.reject("switchVersionLater failed: "+err.getMessage()); | ||||||
|                     try { |                     Log.e("pushy", "switchVersionLater failed", err); | ||||||
|                         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) { |     public static void markSuccess(UpdateContext updateContext, Promise promise) { | ||||||
|         try { |         UiThreadUtil.runOnUiThread(new Runnable() { | ||||||
|             UiThreadUtil.runOnUiThread(new Runnable() { |             @Override | ||||||
|                 @Override |             public void run() { | ||||||
|                 public void run() { |                 updateContext.markSuccess(); | ||||||
|                     updateContext.markSuccess(); |                 promise.resolve(true); | ||||||
|                     promise.resolve(true); |             } | ||||||
|                 } |         }); | ||||||
|             }); |  | ||||||
|         } catch (Exception e){ |  | ||||||
|             promise.reject("执行报错:"+e.getMessage()); |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static void setUuid(UpdateContext updateContext, String uuid, Promise promise) { |     public static void setUuid(UpdateContext updateContext, String uuid, Promise promise) { | ||||||
|         try { |         UiThreadUtil.runOnUiThread(new Runnable() { | ||||||
|             UiThreadUtil.runOnUiThread(new Runnable() { |             @Override | ||||||
|                 @Override |             public void run() { | ||||||
|                 public void run() { |                 updateContext.setKv("uuid", uuid); | ||||||
|                     updateContext.setKv("uuid", uuid); |                 promise.resolve(true); | ||||||
|                     promise.resolve(true); |             } | ||||||
|                 } |         }); | ||||||
|             }); |  | ||||||
|         } catch (Exception e){ |  | ||||||
|             promise.reject("执行报错:"+e.getMessage()); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public static boolean check(String json) { |     public static boolean check(String json) { | ||||||
|         ObjectMapper mapper = new ObjectMapper(); |         ObjectMapper mapper = new ObjectMapper(); | ||||||
|         try { |         try { | ||||||
|             mapper.readValue(json, Map.class); |             mapper.readValue(json, Map.class); | ||||||
|             System.out.println("String can be converted to Map"); |  | ||||||
|             return  true; |             return  true; | ||||||
|         } catch (IOException e) { |         } catch (IOException e) { | ||||||
|             System.out.println("String cannot be converted to Map"); |  | ||||||
|             return  false; |             return  false; | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| @@ -246,12 +228,12 @@ public class UpdateModuleImpl { | |||||||
|         UiThreadUtil.runOnUiThread(new Runnable() { |         UiThreadUtil.runOnUiThread(new Runnable() { | ||||||
|             @Override |             @Override | ||||||
|             public void run() { |             public void run() { | ||||||
|                 if(!check(info)){ |                 if (check(info)) { | ||||||
|                     updateContext.setKv("hash_" + hash, info); |  | ||||||
|                     promise.reject("校验报错:json字符串格式错误"); |  | ||||||
|                 }else { |  | ||||||
|                     updateContext.setKv("hash_" + hash, info); |                     updateContext.setKv("hash_" + hash, info); | ||||||
|                     promise.resolve(true); |                     promise.resolve(true); | ||||||
|  |                 } else { | ||||||
|  |                     updateContext.setKv("hash_" + hash, info); | ||||||
|  |                     promise.reject("setLocalHashInfo failed: invalid json string"); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         }); |         }); | ||||||
| @@ -262,7 +244,7 @@ public class UpdateModuleImpl { | |||||||
|         if (check(value)) { |         if (check(value)) { | ||||||
|             promise.resolve(value); |             promise.resolve(value); | ||||||
|         } else { |         } else { | ||||||
|             promise.reject("校验报错:json字符串格式错误"); |             promise.reject("getLocalHashInfo failed: invalid json string"); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -1,6 +1,6 @@ | |||||||
| { | { | ||||||
|   "name": "react-native-update", |   "name": "react-native-update", | ||||||
|   "version": "10.24.3", |   "version": "10.25.0", | ||||||
|   "description": "react-native hot update", |   "description": "react-native hot update", | ||||||
|   "main": "src/index", |   "main": "src/index", | ||||||
|   "scripts": { |   "scripts": { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 sunnylqm
					sunnylqm