@novacraft-engineering/pulsar-code-push 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (147) hide show
  1. package/AlertAdapter.js +24 -0
  2. package/CodePush.js +671 -0
  3. package/CodePush.podspec +28 -0
  4. package/LICENSE +23 -0
  5. package/README.md +70 -0
  6. package/android/build.gradle +61 -0
  7. package/android/codepush.gradle +162 -0
  8. package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  9. package/android/gradle/wrapper/gradle-wrapper.properties +5 -0
  10. package/android/gradle.properties +20 -0
  11. package/android/gradlew +164 -0
  12. package/android/gradlew.bat +90 -0
  13. package/android/proguard-rules.pro +33 -0
  14. package/android/src/debug/AndroidManifest.xml +9 -0
  15. package/android/src/main/AndroidManifest.xml +5 -0
  16. package/android/src/main/java/com/microsoft/codepush/react/CodePush.java +444 -0
  17. package/android/src/main/java/com/microsoft/codepush/react/CodePushConstants.java +35 -0
  18. package/android/src/main/java/com/microsoft/codepush/react/CodePushDialog.java +102 -0
  19. package/android/src/main/java/com/microsoft/codepush/react/CodePushInstallMode.java +16 -0
  20. package/android/src/main/java/com/microsoft/codepush/react/CodePushInvalidPublicKeyException.java +12 -0
  21. package/android/src/main/java/com/microsoft/codepush/react/CodePushInvalidUpdateException.java +7 -0
  22. package/android/src/main/java/com/microsoft/codepush/react/CodePushMalformedDataException.java +12 -0
  23. package/android/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java +862 -0
  24. package/android/src/main/java/com/microsoft/codepush/react/CodePushNotInitializedException.java +12 -0
  25. package/android/src/main/java/com/microsoft/codepush/react/CodePushTelemetryManager.java +175 -0
  26. package/android/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java +12 -0
  27. package/android/src/main/java/com/microsoft/codepush/react/CodePushUpdateManager.java +383 -0
  28. package/android/src/main/java/com/microsoft/codepush/react/CodePushUpdateState.java +15 -0
  29. package/android/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java +275 -0
  30. package/android/src/main/java/com/microsoft/codepush/react/CodePushUtils.java +238 -0
  31. package/android/src/main/java/com/microsoft/codepush/react/DownloadProgress.java +30 -0
  32. package/android/src/main/java/com/microsoft/codepush/react/DownloadProgressCallback.java +5 -0
  33. package/android/src/main/java/com/microsoft/codepush/react/FileUtils.java +203 -0
  34. package/android/src/main/java/com/microsoft/codepush/react/ReactHostHolder.java +11 -0
  35. package/android/src/main/java/com/microsoft/codepush/react/ReactInstanceHolder.java +17 -0
  36. package/android/src/main/java/com/microsoft/codepush/react/SettingsManager.java +173 -0
  37. package/android/src/main/java/com/microsoft/codepush/react/TLSSocketFactory.java +72 -0
  38. package/ios/CodePush/Base64/Base64/MF_Base64Additions.h +34 -0
  39. package/ios/CodePush/Base64/Base64/MF_Base64Additions.m +252 -0
  40. package/ios/CodePush/Base64/README.md +47 -0
  41. package/ios/CodePush/CodePush.h +235 -0
  42. package/ios/CodePush/CodePush.m +1122 -0
  43. package/ios/CodePush/CodePushConfig.m +116 -0
  44. package/ios/CodePush/CodePushDownloadHandler.m +130 -0
  45. package/ios/CodePush/CodePushErrorUtils.m +20 -0
  46. package/ios/CodePush/CodePushPackage.m +602 -0
  47. package/ios/CodePush/CodePushTelemetryManager.m +175 -0
  48. package/ios/CodePush/CodePushUpdateUtils.m +376 -0
  49. package/ios/CodePush/CodePushUtils.m +9 -0
  50. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithm.h +69 -0
  51. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmFactory.h +16 -0
  52. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmFactory.m +51 -0
  53. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmNone.h +15 -0
  54. package/ios/CodePush/JWT/Core/Algorithms/Base/JWTAlgorithmNone.m +55 -0
  55. package/ios/CodePush/JWT/Core/Algorithms/ESFamily/JWTAlgorithmESBase.h +24 -0
  56. package/ios/CodePush/JWT/Core/Algorithms/ESFamily/JWTAlgorithmESBase.m +41 -0
  57. package/ios/CodePush/JWT/Core/Algorithms/HSFamily/JWTAlgorithmHSBase.h +28 -0
  58. package/ios/CodePush/JWT/Core/Algorithms/HSFamily/JWTAlgorithmHSBase.m +205 -0
  59. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolder.h +103 -0
  60. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolder.m +322 -0
  61. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolderChain.h +37 -0
  62. package/ios/CodePush/JWT/Core/Algorithms/Holders/JWTAlgorithmDataHolderChain.m +145 -0
  63. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTAlgorithmRSBase.h +35 -0
  64. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTAlgorithmRSBase.m +551 -0
  65. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/JWTRSAlgorithm.h +23 -0
  66. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKey.h +43 -0
  67. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKey.m +230 -0
  68. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKeyExtractor.h +31 -0
  69. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoKeyExtractor.m +113 -0
  70. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.h +38 -0
  71. package/ios/CodePush/JWT/Core/Algorithms/RSFamily/RSKeys/JWTCryptoSecurity.m +500 -0
  72. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaim.h +18 -0
  73. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaim.m +214 -0
  74. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSet.h +23 -0
  75. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSet.m +29 -0
  76. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetSerializer.h +19 -0
  77. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetSerializer.m +68 -0
  78. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetVerifier.h +18 -0
  79. package/ios/CodePush/JWT/Core/ClaimSet/JWTClaimsSetVerifier.m +72 -0
  80. package/ios/CodePush/JWT/Core/Coding/JWTCoding+ResultTypes.h +67 -0
  81. package/ios/CodePush/JWT/Core/Coding/JWTCoding+ResultTypes.m +111 -0
  82. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionOne.h +119 -0
  83. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionOne.m +307 -0
  84. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionThree.h +94 -0
  85. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionThree.m +619 -0
  86. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionTwo.h +164 -0
  87. package/ios/CodePush/JWT/Core/Coding/JWTCoding+VersionTwo.m +514 -0
  88. package/ios/CodePush/JWT/Core/Coding/JWTCoding.h +24 -0
  89. package/ios/CodePush/JWT/Core/Coding/JWTCoding.m +11 -0
  90. package/ios/CodePush/JWT/Core/FrameworkSupplement/JWT.h +52 -0
  91. package/ios/CodePush/JWT/Core/FrameworkSupplement/Map.modulemap +5 -0
  92. package/ios/CodePush/JWT/Core/Supplement/JWTBase64Coder.h +28 -0
  93. package/ios/CodePush/JWT/Core/Supplement/JWTBase64Coder.m +70 -0
  94. package/ios/CodePush/JWT/Core/Supplement/JWTDeprecations.h +22 -0
  95. package/ios/CodePush/JWT/Core/Supplement/JWTErrorDescription.h +34 -0
  96. package/ios/CodePush/JWT/Core/Supplement/JWTErrorDescription.m +73 -0
  97. package/ios/CodePush/JWT/LICENSE +19 -0
  98. package/ios/CodePush/JWT/README.md +489 -0
  99. package/ios/CodePush/RCTConvert+CodePushInstallMode.m +20 -0
  100. package/ios/CodePush/RCTConvert+CodePushUpdateState.m +20 -0
  101. package/ios/CodePush/SSZipArchive/Info.plist +26 -0
  102. package/ios/CodePush/SSZipArchive/README.md +1 -0
  103. package/ios/CodePush/SSZipArchive/SSZipArchive.h +178 -0
  104. package/ios/CodePush/SSZipArchive/SSZipArchive.m +1496 -0
  105. package/ios/CodePush/SSZipArchive/SSZipCommon.h +71 -0
  106. package/ios/CodePush/SSZipArchive/Supporting Files/PrivacyInfo.xcprivacy +23 -0
  107. package/ios/CodePush/SSZipArchive/include/ZipArchive.h +25 -0
  108. package/ios/CodePush/SSZipArchive/minizip/LICENSE +17 -0
  109. package/ios/CodePush/SSZipArchive/minizip/mz.h +273 -0
  110. package/ios/CodePush/SSZipArchive/minizip/mz_compat.c +1306 -0
  111. package/ios/CodePush/SSZipArchive/minizip/mz_compat.h +346 -0
  112. package/ios/CodePush/SSZipArchive/minizip/mz_crypt.c +187 -0
  113. package/ios/CodePush/SSZipArchive/minizip/mz_crypt.h +65 -0
  114. package/ios/CodePush/SSZipArchive/minizip/mz_crypt_apple.c +526 -0
  115. package/ios/CodePush/SSZipArchive/minizip/mz_os.c +348 -0
  116. package/ios/CodePush/SSZipArchive/minizip/mz_os.h +176 -0
  117. package/ios/CodePush/SSZipArchive/minizip/mz_os_posix.c +350 -0
  118. package/ios/CodePush/SSZipArchive/minizip/mz_strm.c +556 -0
  119. package/ios/CodePush/SSZipArchive/minizip/mz_strm.h +132 -0
  120. package/ios/CodePush/SSZipArchive/minizip/mz_strm_buf.c +383 -0
  121. package/ios/CodePush/SSZipArchive/minizip/mz_strm_buf.h +42 -0
  122. package/ios/CodePush/SSZipArchive/minizip/mz_strm_mem.c +269 -0
  123. package/ios/CodePush/SSZipArchive/minizip/mz_strm_mem.h +48 -0
  124. package/ios/CodePush/SSZipArchive/minizip/mz_strm_os.h +40 -0
  125. package/ios/CodePush/SSZipArchive/minizip/mz_strm_os_posix.c +203 -0
  126. package/ios/CodePush/SSZipArchive/minizip/mz_strm_pkcrypt.c +334 -0
  127. package/ios/CodePush/SSZipArchive/minizip/mz_strm_pkcrypt.h +46 -0
  128. package/ios/CodePush/SSZipArchive/minizip/mz_strm_split.c +429 -0
  129. package/ios/CodePush/SSZipArchive/minizip/mz_strm_split.h +43 -0
  130. package/ios/CodePush/SSZipArchive/minizip/mz_strm_wzaes.c +360 -0
  131. package/ios/CodePush/SSZipArchive/minizip/mz_strm_wzaes.h +46 -0
  132. package/ios/CodePush/SSZipArchive/minizip/mz_strm_zlib.c +389 -0
  133. package/ios/CodePush/SSZipArchive/minizip/mz_strm_zlib.h +43 -0
  134. package/ios/CodePush/SSZipArchive/minizip/mz_zip.c +2782 -0
  135. package/ios/CodePush/SSZipArchive/minizip/mz_zip.h +262 -0
  136. package/ios/CodePush/SSZipArchive/minizip/mz_zip_rw.c +1942 -0
  137. package/ios/CodePush/SSZipArchive/minizip/mz_zip_rw.h +285 -0
  138. package/ios/CodePush.xcodeproj/project.pbxproj +1052 -0
  139. package/ios/CodePush.xcodeproj/project.xcworkspace/contents.xcworkspacedata +7 -0
  140. package/ios/CodePush.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist +8 -0
  141. package/ios/PrivacyInfo.xcprivacy +31 -0
  142. package/logging.js +6 -0
  143. package/package-mixins.js +68 -0
  144. package/package.json +51 -0
  145. package/react-native.config.js +11 -0
  146. package/request-fetch-adapter.js +52 -0
  147. package/typings/react-native-code-push.d.ts +455 -0
@@ -0,0 +1,862 @@
1
+ package com.microsoft.codepush.react;
2
+
3
+ import android.app.Activity;
4
+ import android.content.SharedPreferences;
5
+ import android.os.AsyncTask;
6
+ import android.os.Handler;
7
+ import android.os.Looper;
8
+ import android.view.View;
9
+ import android.view.Choreographer;
10
+
11
+ import androidx.annotation.OptIn;
12
+
13
+ import com.facebook.react.ReactApplication;
14
+ import com.facebook.react.ReactHost;
15
+ import com.facebook.react.ReactInstanceManager;
16
+ import com.facebook.react.ReactRootView;
17
+ import com.facebook.react.bridge.Arguments;
18
+ import com.facebook.react.bridge.BaseJavaModule;
19
+ import com.facebook.react.bridge.JSBundleLoader;
20
+ import com.facebook.react.bridge.LifecycleEventListener;
21
+ import com.facebook.react.bridge.Promise;
22
+ import com.facebook.react.bridge.ReactApplicationContext;
23
+ import com.facebook.react.bridge.ReactMethod;
24
+ import com.facebook.react.bridge.ReadableMap;
25
+ import com.facebook.react.bridge.WritableMap;
26
+ import com.facebook.react.common.annotations.UnstableReactNativeAPI;
27
+ import com.facebook.react.devsupport.interfaces.DevSupportManager;
28
+ import com.facebook.react.modules.core.DeviceEventManagerModule;
29
+ import com.facebook.react.modules.core.ReactChoreographer;
30
+ import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
31
+ import com.facebook.react.runtime.ReactHostDelegate;
32
+ import com.facebook.react.runtime.ReactHostImpl;
33
+
34
+ import org.json.JSONArray;
35
+ import org.json.JSONException;
36
+ import org.json.JSONObject;
37
+
38
+ import java.io.IOException;
39
+ import java.lang.reflect.Field;
40
+ import java.lang.reflect.Method;
41
+ import java.util.ArrayList;
42
+ import java.util.Date;
43
+ import java.util.HashMap;
44
+ import java.util.List;
45
+ import java.util.Map;
46
+ import java.util.UUID;
47
+
48
+ @OptIn(markerClass = UnstableReactNativeAPI.class)
49
+ public class CodePushNativeModule extends BaseJavaModule {
50
+ private String mBinaryContentsHash = null;
51
+ private String mClientUniqueId = null;
52
+ private LifecycleEventListener mLifecycleEventListener = null;
53
+ private int mMinimumBackgroundDuration = 0;
54
+
55
+ private CodePush mCodePush;
56
+ private SettingsManager mSettingsManager;
57
+ private CodePushTelemetryManager mTelemetryManager;
58
+ private CodePushUpdateManager mUpdateManager;
59
+
60
+ private boolean _allowed = true;
61
+ private boolean _restartInProgress = false;
62
+ private ArrayList<Boolean> _restartQueue = new ArrayList<>();
63
+
64
+ public CodePushNativeModule(ReactApplicationContext reactContext, CodePush codePush, CodePushUpdateManager codePushUpdateManager, CodePushTelemetryManager codePushTelemetryManager, SettingsManager settingsManager) {
65
+ super(reactContext);
66
+
67
+ mCodePush = codePush;
68
+ mSettingsManager = settingsManager;
69
+ mTelemetryManager = codePushTelemetryManager;
70
+ mUpdateManager = codePushUpdateManager;
71
+
72
+ // Initialize module state while we have a reference to the current context.
73
+ mBinaryContentsHash = CodePushUpdateUtils.getHashForBinaryContents(reactContext, mCodePush.isDebugMode());
74
+
75
+ SharedPreferences preferences = codePush.getContext().getSharedPreferences(CodePushConstants.CODE_PUSH_PREFERENCES, 0);
76
+ mClientUniqueId = preferences.getString(CodePushConstants.CLIENT_UNIQUE_ID_KEY, null);
77
+ if (mClientUniqueId == null) {
78
+ mClientUniqueId = UUID.randomUUID().toString();
79
+ preferences.edit().putString(CodePushConstants.CLIENT_UNIQUE_ID_KEY, mClientUniqueId).apply();
80
+ }
81
+ }
82
+
83
+ @Override
84
+ public Map<String, Object> getConstants() {
85
+ final Map<String, Object> constants = new HashMap<>();
86
+
87
+ constants.put("codePushInstallModeImmediate", CodePushInstallMode.IMMEDIATE.getValue());
88
+ constants.put("codePushInstallModeOnNextRestart", CodePushInstallMode.ON_NEXT_RESTART.getValue());
89
+ constants.put("codePushInstallModeOnNextResume", CodePushInstallMode.ON_NEXT_RESUME.getValue());
90
+ constants.put("codePushInstallModeOnNextSuspend", CodePushInstallMode.ON_NEXT_SUSPEND.getValue());
91
+
92
+ constants.put("codePushUpdateStateRunning", CodePushUpdateState.RUNNING.getValue());
93
+ constants.put("codePushUpdateStatePending", CodePushUpdateState.PENDING.getValue());
94
+ constants.put("codePushUpdateStateLatest", CodePushUpdateState.LATEST.getValue());
95
+
96
+ return constants;
97
+ }
98
+
99
+ @Override
100
+ public String getName() {
101
+ return "CodePush";
102
+ }
103
+
104
+ private void loadBundleLegacy() {
105
+ final Activity currentActivity = getReactApplicationContext().getCurrentActivity();
106
+ if (currentActivity == null) {
107
+ // The currentActivity can be null if it is backgrounded / destroyed, so we simply
108
+ // no-op to prevent any null pointer exceptions.
109
+ return;
110
+ }
111
+ mCodePush.invalidateCurrentInstance();
112
+
113
+ currentActivity.runOnUiThread(new Runnable() {
114
+ @Override
115
+ public void run() {
116
+ currentActivity.recreate();
117
+ }
118
+ });
119
+ }
120
+
121
+ // Use reflection to find and set the appropriate fields on ReactInstanceManager. See #556 for a proposal for a less brittle way
122
+ // to approach this.
123
+ private void setJSBundle(ReactInstanceManager instanceManager, String latestJSBundleFile) throws IllegalAccessException {
124
+ try {
125
+ JSBundleLoader latestJSBundleLoader;
126
+ if (latestJSBundleFile.toLowerCase().startsWith("assets://")) {
127
+ latestJSBundleLoader = JSBundleLoader.createAssetLoader(getReactApplicationContext(), latestJSBundleFile, false);
128
+ } else {
129
+ latestJSBundleLoader = JSBundleLoader.createFileLoader(latestJSBundleFile);
130
+ }
131
+
132
+ Field bundleLoaderField = instanceManager.getClass().getDeclaredField("mBundleLoader");
133
+ bundleLoaderField.setAccessible(true);
134
+ bundleLoaderField.set(instanceManager, latestJSBundleLoader);
135
+ } catch (Exception e) {
136
+ CodePushUtils.log("Unable to set JSBundle of ReactInstanceManager - CodePush may not support this version of React Native");
137
+ throw new IllegalAccessException("Could not setJSBundle");
138
+ }
139
+ }
140
+
141
+ // Use reflection to find and set the appropriate fields on ReactHostDelegate. See #556 for a proposal for a less brittle way
142
+ // to approach this.
143
+ private void setJSBundle(ReactHostDelegate reactHostDelegate, String latestJSBundleFile) throws IllegalAccessException {
144
+ try {
145
+ JSBundleLoader latestJSBundleLoader;
146
+ if (latestJSBundleFile.toLowerCase().startsWith("assets://")) {
147
+ latestJSBundleLoader = JSBundleLoader.createAssetLoader(getReactApplicationContext(), latestJSBundleFile, false);
148
+ } else {
149
+ latestJSBundleLoader = JSBundleLoader.createFileLoader(latestJSBundleFile);
150
+ }
151
+
152
+ Field bundleLoaderField = reactHostDelegate.getClass().getDeclaredField("jsBundleLoader");
153
+ bundleLoaderField.setAccessible(true);
154
+ bundleLoaderField.set(reactHostDelegate, latestJSBundleLoader);
155
+ }
156
+ catch (NoSuchFieldException noSuchFileFound) {
157
+ // Ignore this error for Expo
158
+ }
159
+ catch (Exception e) {
160
+ CodePushUtils.log("Unable to set JSBundle of ReactHostDelegate - CodePush may not support this version of React Native");
161
+ throw new IllegalAccessException("Could not setJSBundle");
162
+ }
163
+ }
164
+
165
+ private void loadBundle() {
166
+ clearLifecycleEventListener();
167
+
168
+ // ReactNative core components are changed on new architecture.
169
+ if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
170
+ try {
171
+ DevSupportManager devSupportManager = null;
172
+ ReactHost reactHost = resolveReactHost();
173
+ if (reactHost != null) {
174
+ devSupportManager = reactHost.getDevSupportManager();
175
+ }
176
+ boolean isLiveReloadEnabled = isLiveReloadEnabled(devSupportManager);
177
+
178
+ mCodePush.clearDebugCacheIfNeeded(isLiveReloadEnabled);
179
+ } catch(Exception e) {
180
+ // If we got error in out reflection we should clear debug cache anyway.
181
+ mCodePush.clearDebugCacheIfNeeded(false);
182
+ }
183
+
184
+ try {
185
+ // #1) Get the ReactHost instance, which is what includes the
186
+ // logic to reload the current React context.
187
+ final ReactHost reactHost = resolveReactHost();
188
+ if (reactHost == null) {
189
+ loadBundleLegacy();
190
+ return;
191
+ }
192
+
193
+ String latestJSBundleFile = mCodePush.getJSBundleFileInternal(mCodePush.getAssetsBundleFileName());
194
+
195
+ try {
196
+ if (reactHost instanceof ReactHostImpl) {
197
+ ReactHostDelegate delegate = getReactHostDelegate((ReactHostImpl) reactHost);
198
+ if (delegate != null) {
199
+ // #2) Update the locally stored JS bundle file path
200
+ setJSBundle(delegate, latestJSBundleFile);
201
+ }
202
+ }
203
+ } catch (Exception e) {
204
+ CodePushUtils.log("Exception setJSBundle: " + e.getMessage());
205
+ }
206
+
207
+ // #3) Get the context creation method
208
+ try {
209
+ reactHost.reload("CodePush triggers reload");
210
+ mCodePush.initializeUpdateAfterRestart();
211
+ } catch (Exception e) {
212
+ // The recreation method threw an unknown exception
213
+ // so just simply fallback to restarting the Activity (if it exists)
214
+ loadBundleLegacy();
215
+ }
216
+
217
+ } catch (Exception e) {
218
+ // Our reflection logic failed somewhere
219
+ // so fall back to restarting the Activity (if it exists)
220
+ CodePushUtils.log("Failed to load the bundle, falling back to restarting the Activity (if it exists). " + e.getMessage());
221
+ loadBundleLegacy();
222
+ }
223
+
224
+ } else {
225
+
226
+ try {
227
+ DevSupportManager devSupportManager = null;
228
+ ReactInstanceManager reactInstanceManager = resolveInstanceManager();
229
+ if (reactInstanceManager != null) {
230
+ devSupportManager = reactInstanceManager.getDevSupportManager();
231
+ }
232
+ boolean isLiveReloadEnabled = isLiveReloadEnabled(devSupportManager);
233
+
234
+ mCodePush.clearDebugCacheIfNeeded(isLiveReloadEnabled);
235
+ } catch(Exception e) {
236
+ // If we got error in out reflection we should clear debug cache anyway.
237
+ mCodePush.clearDebugCacheIfNeeded(false);
238
+ }
239
+
240
+ try {
241
+ // #1) Get the ReactInstanceManager instance, which is what includes the
242
+ // logic to reload the current React context.
243
+ final ReactInstanceManager instanceManager = resolveInstanceManager();
244
+ if (instanceManager == null) {
245
+ return;
246
+ }
247
+
248
+ String latestJSBundleFile = mCodePush.getJSBundleFileInternal(mCodePush.getAssetsBundleFileName());
249
+
250
+ // #2) Update the locally stored JS bundle file path
251
+ setJSBundle(instanceManager, latestJSBundleFile);
252
+
253
+ // #3) Get the context creation method and fire it on the UI thread (which RN enforces)
254
+ new Handler(Looper.getMainLooper()).post(new Runnable() {
255
+ @Override
256
+ public void run() {
257
+ try {
258
+ // We don't need to resetReactRootViews anymore
259
+ // due the issue https://github.com/facebook/react-native/issues/14533
260
+ // has been fixed in RN 0.46.0
261
+ //resetReactRootViews(instanceManager);
262
+
263
+ instanceManager.recreateReactContextInBackground();
264
+ mCodePush.initializeUpdateAfterRestart();
265
+ } catch (Exception e) {
266
+ // The recreation method threw an unknown exception
267
+ // so just simply fallback to restarting the Activity (if it exists)
268
+ loadBundleLegacy();
269
+ }
270
+ }
271
+ });
272
+
273
+ } catch (Exception e) {
274
+ // Our reflection logic failed somewhere
275
+ // so fall back to restarting the Activity (if it exists)
276
+ CodePushUtils.log("Failed to load the bundle, falling back to restarting the Activity (if it exists). " + e.getMessage());
277
+ loadBundleLegacy();
278
+ }
279
+
280
+ }
281
+ }
282
+
283
+ private boolean isLiveReloadEnabled(DevSupportManager devSupportManager) {
284
+ if (devSupportManager != null) {
285
+ DeveloperSettings devSettings = devSupportManager.getDevSettings();
286
+ Method[] methods = devSettings.getClass().getMethods();
287
+ for (Method m : methods) {
288
+ if (m.getName().equals("isReloadOnJSChangeEnabled")) {
289
+ try {
290
+ return (boolean) m.invoke(devSettings);
291
+ } catch (Exception x) {
292
+ return false;
293
+ }
294
+ }
295
+ }
296
+ }
297
+
298
+ return false;
299
+ }
300
+
301
+ // This workaround has been implemented in order to fix https://github.com/facebook/react-native/issues/14533
302
+ // resetReactRootViews allows to call recreateReactContextInBackground without any exceptions
303
+ // This fix also relates to https://github.com/microsoft/react-native-code-push/issues/878
304
+ private void resetReactRootViews(ReactInstanceManager instanceManager) throws NoSuchFieldException, IllegalAccessException {
305
+ Field mAttachedRootViewsField = instanceManager.getClass().getDeclaredField("mAttachedRootViews");
306
+ mAttachedRootViewsField.setAccessible(true);
307
+ List<ReactRootView> mAttachedRootViews = (List<ReactRootView>)mAttachedRootViewsField.get(instanceManager);
308
+ for (ReactRootView reactRootView : mAttachedRootViews) {
309
+ reactRootView.removeAllViews();
310
+ reactRootView.setId(View.NO_ID);
311
+ }
312
+ mAttachedRootViewsField.set(instanceManager, mAttachedRootViews);
313
+ }
314
+
315
+ private void clearLifecycleEventListener() {
316
+ // Remove LifecycleEventListener to prevent infinite restart loop
317
+ if (mLifecycleEventListener != null) {
318
+ getReactApplicationContext().removeLifecycleEventListener(mLifecycleEventListener);
319
+ mLifecycleEventListener = null;
320
+ }
321
+ }
322
+
323
+ // Use reflection to find the ReactInstanceManager. See #556 for a proposal for a less brittle way to approach this.
324
+ private ReactInstanceManager resolveInstanceManager() throws NoSuchFieldException, IllegalAccessException {
325
+ ReactInstanceManager instanceManager = CodePush.getReactInstanceManager();
326
+ if (instanceManager != null) {
327
+ return instanceManager;
328
+ }
329
+
330
+ final Activity currentActivity = getReactApplicationContext().getCurrentActivity();
331
+ if (currentActivity == null) {
332
+ return null;
333
+ }
334
+
335
+ ReactApplication reactApplication = (ReactApplication) currentActivity.getApplication();
336
+ instanceManager = reactApplication.getReactNativeHost().getReactInstanceManager();
337
+
338
+ return instanceManager;
339
+ }
340
+
341
+ private ReactHost resolveReactHost() throws NoSuchFieldException, IllegalAccessException {
342
+ ReactHost reactHost = CodePush.getReactHost();
343
+ if (reactHost != null) {
344
+ return reactHost;
345
+ }
346
+
347
+ final Activity currentActivity = getReactApplicationContext().getCurrentActivity();
348
+ if (currentActivity == null) {
349
+ return null;
350
+ }
351
+
352
+ ReactApplication reactApplication = (ReactApplication) currentActivity.getApplication();
353
+ return reactApplication.getReactHost();
354
+ }
355
+
356
+ private void restartAppInternal(boolean onlyIfUpdateIsPending) {
357
+ if (this._restartInProgress) {
358
+ CodePushUtils.log("Restart request queued until the current restart is completed");
359
+ this._restartQueue.add(onlyIfUpdateIsPending);
360
+ return;
361
+ } else if (!this._allowed) {
362
+ CodePushUtils.log("Restart request queued until restarts are re-allowed");
363
+ this._restartQueue.add(onlyIfUpdateIsPending);
364
+ return;
365
+ }
366
+
367
+ this._restartInProgress = true;
368
+ if (!onlyIfUpdateIsPending || mSettingsManager.isPendingUpdate(null)) {
369
+ loadBundle();
370
+ CodePushUtils.log("Restarting app");
371
+ return;
372
+ }
373
+
374
+ this._restartInProgress = false;
375
+ if (this._restartQueue.size() > 0) {
376
+ boolean buf = this._restartQueue.get(0);
377
+ this._restartQueue.remove(0);
378
+ this.restartAppInternal(buf);
379
+ }
380
+ }
381
+
382
+ @ReactMethod
383
+ public void allow(Promise promise) {
384
+ CodePushUtils.log("Re-allowing restarts");
385
+ this._allowed = true;
386
+
387
+ if (_restartQueue.size() > 0) {
388
+ CodePushUtils.log("Executing pending restart");
389
+ boolean buf = this._restartQueue.get(0);
390
+ this._restartQueue.remove(0);
391
+ this.restartAppInternal(buf);
392
+ }
393
+
394
+ promise.resolve(null);
395
+ return;
396
+ }
397
+
398
+ @ReactMethod
399
+ public void clearPendingRestart(Promise promise) {
400
+ this._restartQueue.clear();
401
+ promise.resolve(null);
402
+ return;
403
+ }
404
+
405
+ @ReactMethod
406
+ public void disallow(Promise promise) {
407
+ CodePushUtils.log("Disallowing restarts");
408
+ this._allowed = false;
409
+ promise.resolve(null);
410
+ return;
411
+ }
412
+
413
+ @ReactMethod
414
+ public void restartApp(boolean onlyIfUpdateIsPending, Promise promise) {
415
+ try {
416
+ restartAppInternal(onlyIfUpdateIsPending);
417
+ promise.resolve(null);
418
+ } catch(CodePushUnknownException e) {
419
+ CodePushUtils.log(e);
420
+ promise.reject(e);
421
+ }
422
+ }
423
+
424
+ @ReactMethod
425
+ public void downloadUpdate(final ReadableMap updatePackage, final boolean notifyProgress, final Promise promise) {
426
+ AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
427
+ @Override
428
+ protected Void doInBackground(Void... params) {
429
+ try {
430
+ JSONObject mutableUpdatePackage = CodePushUtils.convertReadableToJsonObject(updatePackage);
431
+ CodePushUtils.setJSONValueForKey(mutableUpdatePackage, CodePushConstants.BINARY_MODIFIED_TIME_KEY, "" + mCodePush.getBinaryResourcesModifiedTime());
432
+ mUpdateManager.downloadPackage(mutableUpdatePackage, mCodePush.getAssetsBundleFileName(), new DownloadProgressCallback() {
433
+ private boolean hasScheduledNextFrame = false;
434
+ private DownloadProgress latestDownloadProgress = null;
435
+
436
+ @Override
437
+ public void call(DownloadProgress downloadProgress) {
438
+ if (!notifyProgress) {
439
+ return;
440
+ }
441
+
442
+ latestDownloadProgress = downloadProgress;
443
+ // If the download is completed, synchronously send the last event.
444
+ if (latestDownloadProgress.isCompleted()) {
445
+ dispatchDownloadProgressEvent();
446
+ return;
447
+ }
448
+
449
+ if (hasScheduledNextFrame) {
450
+ return;
451
+ }
452
+
453
+ hasScheduledNextFrame = true;
454
+ getReactApplicationContext().runOnUiQueueThread(new Runnable() {
455
+ @Override
456
+ public void run() {
457
+ ReactChoreographer.getInstance().postFrameCallback(ReactChoreographer.CallbackType.TIMERS_EVENTS, new Choreographer.FrameCallback() {
458
+ @Override
459
+ public void doFrame(long frameTimeNanos) {
460
+ if (!latestDownloadProgress.isCompleted()) {
461
+ dispatchDownloadProgressEvent();
462
+ }
463
+
464
+ hasScheduledNextFrame = false;
465
+ }
466
+ });
467
+ }
468
+ });
469
+ }
470
+
471
+ public void dispatchDownloadProgressEvent() {
472
+ getReactApplicationContext()
473
+ .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
474
+ .emit(CodePushConstants.DOWNLOAD_PROGRESS_EVENT_NAME, latestDownloadProgress.createWritableMap());
475
+ }
476
+ }, mCodePush.getPublicKey());
477
+
478
+ JSONObject newPackage = mUpdateManager.getPackage(CodePushUtils.tryGetString(updatePackage, CodePushConstants.PACKAGE_HASH_KEY));
479
+ promise.resolve(CodePushUtils.convertJsonObjectToWritable(newPackage));
480
+ } catch (CodePushInvalidUpdateException e) {
481
+ CodePushUtils.log(e);
482
+ mSettingsManager.saveFailedUpdate(CodePushUtils.convertReadableToJsonObject(updatePackage));
483
+ promise.reject(e);
484
+ } catch (IOException | CodePushUnknownException e) {
485
+ CodePushUtils.log(e);
486
+ promise.reject(e);
487
+ }
488
+
489
+ return null;
490
+ }
491
+ };
492
+
493
+ asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
494
+ }
495
+
496
+ @ReactMethod
497
+ public void getConfiguration(Promise promise) {
498
+ try {
499
+ WritableMap configMap = Arguments.createMap();
500
+ configMap.putString("appVersion", mCodePush.getAppVersion());
501
+ configMap.putString("clientUniqueId", mClientUniqueId);
502
+ configMap.putString("deploymentKey", mCodePush.getDeploymentKey());
503
+ configMap.putString("serverUrl", mCodePush.getServerUrl());
504
+
505
+ // The binary hash may be null in debug builds
506
+ if (mBinaryContentsHash != null) {
507
+ configMap.putString(CodePushConstants.PACKAGE_HASH_KEY, mBinaryContentsHash);
508
+ }
509
+
510
+ promise.resolve(configMap);
511
+ } catch(CodePushUnknownException e) {
512
+ CodePushUtils.log(e);
513
+ promise.reject(e);
514
+ }
515
+ }
516
+
517
+ @ReactMethod
518
+ public void getUpdateMetadata(final int updateState, final Promise promise) {
519
+ AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
520
+ @Override
521
+ protected Void doInBackground(Void... params) {
522
+ try {
523
+ JSONObject currentPackage = mUpdateManager.getCurrentPackage();
524
+
525
+ if (currentPackage == null) {
526
+ promise.resolve(null);
527
+ return null;
528
+ }
529
+
530
+ Boolean currentUpdateIsPending = false;
531
+
532
+ if (currentPackage.has(CodePushConstants.PACKAGE_HASH_KEY)) {
533
+ String currentHash = currentPackage.optString(CodePushConstants.PACKAGE_HASH_KEY, null);
534
+ currentUpdateIsPending = mSettingsManager.isPendingUpdate(currentHash);
535
+ }
536
+
537
+ if (updateState == CodePushUpdateState.PENDING.getValue() && !currentUpdateIsPending) {
538
+ // The caller wanted a pending update
539
+ // but there isn't currently one.
540
+ promise.resolve(null);
541
+ } else if (updateState == CodePushUpdateState.RUNNING.getValue() && currentUpdateIsPending) {
542
+ // The caller wants the running update, but the current
543
+ // one is pending, so we need to grab the previous.
544
+ JSONObject previousPackage = mUpdateManager.getPreviousPackage();
545
+
546
+ if (previousPackage == null) {
547
+ promise.resolve(null);
548
+ return null;
549
+ }
550
+
551
+ promise.resolve(CodePushUtils.convertJsonObjectToWritable(previousPackage));
552
+ } else {
553
+ // The current package satisfies the request:
554
+ // 1) Caller wanted a pending, and there is a pending update
555
+ // 2) Caller wanted the running update, and there isn't a pending
556
+ // 3) Caller wants the latest update, regardless if it's pending or not
557
+ if (mCodePush.isRunningBinaryVersion()) {
558
+ // This only matters in Debug builds. Since we do not clear "outdated" updates,
559
+ // we need to indicate to the JS side that somehow we have a current update on
560
+ // disk that is not actually running.
561
+ CodePushUtils.setJSONValueForKey(currentPackage, "_isDebugOnly", true);
562
+ }
563
+
564
+ // Enable differentiating pending vs. non-pending updates
565
+ CodePushUtils.setJSONValueForKey(currentPackage, "isPending", currentUpdateIsPending);
566
+ promise.resolve(CodePushUtils.convertJsonObjectToWritable(currentPackage));
567
+ }
568
+ } catch (CodePushMalformedDataException e) {
569
+ // We need to recover the app in case 'codepush.json' is corrupted
570
+ CodePushUtils.log(e.getMessage());
571
+ clearUpdates();
572
+ promise.resolve(null);
573
+ } catch(CodePushUnknownException e) {
574
+ CodePushUtils.log(e);
575
+ promise.reject(e);
576
+ }
577
+
578
+ return null;
579
+ }
580
+ };
581
+
582
+ asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
583
+ }
584
+
585
+ @ReactMethod
586
+ public void getNewStatusReport(final Promise promise) {
587
+ AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
588
+ @Override
589
+ protected Void doInBackground(Void... params) {
590
+ try {
591
+ if (mCodePush.needToReportRollback()) {
592
+ mCodePush.setNeedToReportRollback(false);
593
+ JSONArray failedUpdates = mSettingsManager.getFailedUpdates();
594
+ if (failedUpdates != null && failedUpdates.length() > 0) {
595
+ try {
596
+ JSONObject lastFailedPackageJSON = failedUpdates.getJSONObject(failedUpdates.length() - 1);
597
+ WritableMap lastFailedPackage = CodePushUtils.convertJsonObjectToWritable(lastFailedPackageJSON);
598
+ WritableMap failedStatusReport = mTelemetryManager.getRollbackReport(lastFailedPackage);
599
+ if (failedStatusReport != null) {
600
+ promise.resolve(failedStatusReport);
601
+ return null;
602
+ }
603
+ } catch (JSONException e) {
604
+ throw new CodePushUnknownException("Unable to read failed updates information stored in SharedPreferences.", e);
605
+ }
606
+ }
607
+ } else if (mCodePush.didUpdate()) {
608
+ JSONObject currentPackage = mUpdateManager.getCurrentPackage();
609
+ if (currentPackage != null) {
610
+ WritableMap newPackageStatusReport = mTelemetryManager.getUpdateReport(CodePushUtils.convertJsonObjectToWritable(currentPackage));
611
+ if (newPackageStatusReport != null) {
612
+ promise.resolve(newPackageStatusReport);
613
+ return null;
614
+ }
615
+ }
616
+ } else if (mCodePush.isRunningBinaryVersion()) {
617
+ WritableMap newAppVersionStatusReport = mTelemetryManager.getBinaryUpdateReport(mCodePush.getAppVersion());
618
+ if (newAppVersionStatusReport != null) {
619
+ promise.resolve(newAppVersionStatusReport);
620
+ return null;
621
+ }
622
+ } else {
623
+ WritableMap retryStatusReport = mTelemetryManager.getRetryStatusReport();
624
+ if (retryStatusReport != null) {
625
+ promise.resolve(retryStatusReport);
626
+ return null;
627
+ }
628
+ }
629
+
630
+ promise.resolve("");
631
+ } catch(CodePushUnknownException e) {
632
+ CodePushUtils.log(e);
633
+ promise.reject(e);
634
+ }
635
+ return null;
636
+ }
637
+ };
638
+
639
+ asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
640
+ }
641
+
642
+ @ReactMethod
643
+ public void installUpdate(final ReadableMap updatePackage, final int installMode, final int minimumBackgroundDuration, final Promise promise) {
644
+ AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
645
+ @Override
646
+ protected Void doInBackground(Void... params) {
647
+ try {
648
+ mUpdateManager.installPackage(CodePushUtils.convertReadableToJsonObject(updatePackage), mSettingsManager.isPendingUpdate(null));
649
+
650
+ String pendingHash = CodePushUtils.tryGetString(updatePackage, CodePushConstants.PACKAGE_HASH_KEY);
651
+ if (pendingHash == null) {
652
+ throw new CodePushUnknownException("Update package to be installed has no hash.");
653
+ } else {
654
+ mSettingsManager.savePendingUpdate(pendingHash, /* isLoading */false);
655
+ }
656
+
657
+ if (installMode == CodePushInstallMode.ON_NEXT_RESUME.getValue() ||
658
+ // We also add the resume listener if the installMode is IMMEDIATE, because
659
+ // if the current activity is backgrounded, we want to reload the bundle when
660
+ // it comes back into the foreground.
661
+ installMode == CodePushInstallMode.IMMEDIATE.getValue() ||
662
+ installMode == CodePushInstallMode.ON_NEXT_SUSPEND.getValue()) {
663
+
664
+ // Store the minimum duration on the native module as an instance
665
+ // variable instead of relying on a closure below, so that any
666
+ // subsequent resume-based installs could override it.
667
+ CodePushNativeModule.this.mMinimumBackgroundDuration = minimumBackgroundDuration;
668
+
669
+ if (mLifecycleEventListener == null) {
670
+ // Ensure we do not add the listener twice.
671
+ mLifecycleEventListener = new LifecycleEventListener() {
672
+ private Date lastPausedDate = null;
673
+ private Handler appSuspendHandler = new Handler(Looper.getMainLooper());
674
+ private Runnable loadBundleRunnable = new Runnable() {
675
+ @Override
676
+ public void run() {
677
+ CodePushUtils.log("Loading bundle on suspend");
678
+ restartAppInternal(false);
679
+ }
680
+ };
681
+
682
+ @Override
683
+ public void onHostResume() {
684
+ appSuspendHandler.removeCallbacks(loadBundleRunnable);
685
+ // As of RN 36, the resume handler fires immediately if the app is in
686
+ // the foreground, so explicitly wait for it to be backgrounded first
687
+ if (lastPausedDate != null) {
688
+ long durationInBackground = (new Date().getTime() - lastPausedDate.getTime()) / 1000;
689
+ if (installMode == CodePushInstallMode.IMMEDIATE.getValue()
690
+ || durationInBackground >= CodePushNativeModule.this.mMinimumBackgroundDuration) {
691
+ CodePushUtils.log("Loading bundle on resume");
692
+ restartAppInternal(false);
693
+ }
694
+ }
695
+ }
696
+
697
+ @Override
698
+ public void onHostPause() {
699
+ // Save the current time so that when the app is later
700
+ // resumed, we can detect how long it was in the background.
701
+ lastPausedDate = new Date();
702
+
703
+ if (installMode == CodePushInstallMode.ON_NEXT_SUSPEND.getValue() && mSettingsManager.isPendingUpdate(null)) {
704
+ appSuspendHandler.postDelayed(loadBundleRunnable, minimumBackgroundDuration * 1000);
705
+ }
706
+ }
707
+
708
+ @Override
709
+ public void onHostDestroy() {
710
+ }
711
+ };
712
+
713
+ getReactApplicationContext().addLifecycleEventListener(mLifecycleEventListener);
714
+ }
715
+ }
716
+
717
+ promise.resolve("");
718
+ } catch(CodePushUnknownException e) {
719
+ CodePushUtils.log(e);
720
+ promise.reject(e);
721
+ }
722
+
723
+ return null;
724
+ }
725
+ };
726
+
727
+ asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
728
+ }
729
+
730
+ @ReactMethod
731
+ public void isFailedUpdate(String packageHash, Promise promise) {
732
+ try {
733
+ promise.resolve(mSettingsManager.isFailedHash(packageHash));
734
+ } catch (CodePushUnknownException e) {
735
+ CodePushUtils.log(e);
736
+ promise.reject(e);
737
+ }
738
+ }
739
+
740
+ @ReactMethod
741
+ public void getLatestRollbackInfo(Promise promise) {
742
+ try {
743
+ JSONObject latestRollbackInfo = mSettingsManager.getLatestRollbackInfo();
744
+ if (latestRollbackInfo != null) {
745
+ promise.resolve(CodePushUtils.convertJsonObjectToWritable(latestRollbackInfo));
746
+ } else {
747
+ promise.resolve(null);
748
+ }
749
+ } catch (CodePushUnknownException e) {
750
+ CodePushUtils.log(e);
751
+ promise.reject(e);
752
+ }
753
+ }
754
+
755
+ @ReactMethod
756
+ public void setLatestRollbackInfo(String packageHash, Promise promise) {
757
+ try {
758
+ mSettingsManager.setLatestRollbackInfo(packageHash);
759
+ promise.resolve(null);
760
+ } catch (CodePushUnknownException e) {
761
+ CodePushUtils.log(e);
762
+ promise.reject(e);
763
+ }
764
+ }
765
+
766
+ @ReactMethod
767
+ public void isFirstRun(String packageHash, Promise promise) {
768
+ try {
769
+ boolean isFirstRun = mCodePush.didUpdate()
770
+ && packageHash != null
771
+ && packageHash.length() > 0
772
+ && packageHash.equals(mUpdateManager.getCurrentPackageHash());
773
+ promise.resolve(isFirstRun);
774
+ } catch(CodePushUnknownException e) {
775
+ CodePushUtils.log(e);
776
+ promise.reject(e);
777
+ }
778
+ }
779
+
780
+ @ReactMethod
781
+ public void notifyApplicationReady(Promise promise) {
782
+ try {
783
+ mSettingsManager.removePendingUpdate();
784
+ promise.resolve("");
785
+ } catch(CodePushUnknownException e) {
786
+ CodePushUtils.log(e);
787
+ promise.reject(e);
788
+ }
789
+ }
790
+
791
+ @ReactMethod
792
+ public void recordStatusReported(ReadableMap statusReport) {
793
+ try {
794
+ mTelemetryManager.recordStatusReported(statusReport);
795
+ } catch(CodePushUnknownException e) {
796
+ CodePushUtils.log(e);
797
+ }
798
+ }
799
+
800
+ @ReactMethod
801
+ public void saveStatusReportForRetry(ReadableMap statusReport) {
802
+ try {
803
+ mTelemetryManager.saveStatusReportForRetry(statusReport);
804
+ } catch(CodePushUnknownException e) {
805
+ CodePushUtils.log(e);
806
+ }
807
+ }
808
+
809
+ @ReactMethod
810
+ // Replaces the current bundle with the one downloaded from removeBundleUrl.
811
+ // It is only to be used during tests. No-ops if the test configuration flag is not set.
812
+ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl) {
813
+ try {
814
+ if (mCodePush.isUsingTestConfiguration()) {
815
+ try {
816
+ mUpdateManager.downloadAndReplaceCurrentBundle(remoteBundleUrl, mCodePush.getAssetsBundleFileName());
817
+ } catch (IOException e) {
818
+ throw new CodePushUnknownException("Unable to replace current bundle", e);
819
+ }
820
+ }
821
+ } catch(CodePushUnknownException | CodePushMalformedDataException e) {
822
+ CodePushUtils.log(e);
823
+ }
824
+ }
825
+
826
+ /**
827
+ * This method clears CodePush's downloaded updates.
828
+ * It is needed to switch to a different deployment if the current deployment is more recent.
829
+ * Note: we don’t recommend to use this method in scenarios other than that (CodePush will call
830
+ * this method automatically when needed in other cases) as it could lead to unpredictable
831
+ * behavior.
832
+ */
833
+ @ReactMethod
834
+ public void clearUpdates() {
835
+ CodePushUtils.log("Clearing updates.");
836
+ mCodePush.clearUpdates();
837
+ }
838
+
839
+ @ReactMethod
840
+ public void addListener(String eventName) {
841
+ // Set up any upstream listeners or background tasks as necessary
842
+ }
843
+
844
+ @ReactMethod
845
+ public void removeListeners(Integer count) {
846
+ // Remove upstream listeners, stop unnecessary background tasks
847
+ }
848
+
849
+ public ReactHostDelegate getReactHostDelegate(ReactHostImpl reactHostImpl) {
850
+ try {
851
+ Class<?> clazz = reactHostImpl.getClass();
852
+ Field field = clazz.getDeclaredField("reactHostDelegate");
853
+ field.setAccessible(true);
854
+
855
+ // Get the value of the field for the provided instance
856
+ return (ReactHostDelegate) field.get(reactHostImpl);
857
+ } catch (NoSuchFieldException | IllegalAccessException e) {
858
+ e.printStackTrace();
859
+ return null;
860
+ }
861
+ }
862
+ }