@revopush/react-native-code-push 0.0.1

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