@algocare/react-native-code-push 9.0.0-beta.3

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