41
41
42
42
public class CodePush {
43
43
44
- private boolean resumablePendingUpdateAvailable = false ;
45
44
private boolean didUpdate = false ;
46
45
private Timer timer ;
47
46
private boolean usingTestFolder = false ;
@@ -87,7 +86,7 @@ public CodePush(String deploymentKey, Activity mainActivity) {
87
86
throw new CodePushUnknownException ("Unable to get package info for " + applicationContext .getPackageName (), e );
88
87
}
89
88
90
- checkForPendingUpdate ( /*needsRestart*/ false );
89
+ handleInitIfPendingUpdate ( );
91
90
}
92
91
93
92
public ReactPackage getReactPackage () {
@@ -144,52 +143,6 @@ private void cancelRollbackTimer() {
144
143
}
145
144
}
146
145
147
- private void checkForPendingUpdate (boolean needsRestart ) {
148
- SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
149
- String pendingUpdateString = settings .getString (PENDING_UPDATE_KEY , null );
150
-
151
- if (pendingUpdateString != null ) {
152
- try {
153
- JSONObject pendingUpdateJSON = new JSONObject (pendingUpdateString );
154
- String pendingHash = pendingUpdateJSON .getString (PENDING_UPDATE_HASH_KEY );
155
- String currentHash = codePushPackage .getCurrentPackageHash ();
156
- if (!pendingHash .equals (currentHash )) {
157
- throw new CodePushUnknownException ("Pending hash " + pendingHash +
158
- " and current hash " + currentHash + " are different" );
159
- }
160
-
161
- int rollbackTimeout = pendingUpdateJSON .getInt (PENDING_UPDATE_ROLLBACK_TIMEOUT_KEY );
162
- initializeUpdateWithRollbackTimeout (rollbackTimeout , needsRestart );
163
- settings .edit ().remove (PENDING_UPDATE_KEY ).commit ();
164
- } catch (JSONException e ) {
165
- // Should not happen.
166
- throw new CodePushUnknownException ("Unable to parse pending update metadata " +
167
- pendingUpdateString + " stored in SharedPreferences" , e );
168
- } catch (IOException e ) {
169
- // There is no current package hash.
170
- throw new CodePushUnknownException ("Should not register a pending update without a saving a current package" , e );
171
- }
172
- }
173
- }
174
-
175
- private void checkForPendingUpdateDuringResume () {
176
- if (resumablePendingUpdateAvailable ) {
177
- checkForPendingUpdate (/*needsRestart*/ true );
178
- }
179
- }
180
-
181
- private void initializeUpdateWithRollbackTimeout (int rollbackTimeout , boolean needsRestart ) {
182
- didUpdate = true ;
183
-
184
- if (needsRestart ) {
185
- codePushNativeModule .loadBundle ();
186
- }
187
-
188
- if (0 != rollbackTimeout ) {
189
- startRollbackTimer (rollbackTimeout );
190
- }
191
- }
192
-
193
146
private boolean isFailedHash (String packageHash ) {
194
147
SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
195
148
String failedUpdatesString = settings .getString (FAILED_UPDATES_KEY , null );
@@ -277,6 +230,28 @@ public void run() {
277
230
}, timeout );
278
231
}
279
232
233
+ private void handleInitIfPendingUpdate () {
234
+ SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
235
+ String pendingUpdateString = settings .getString (PENDING_UPDATE_KEY , null );
236
+
237
+ if (pendingUpdateString != null ) {
238
+ try {
239
+ didUpdate = true ;
240
+ JSONObject pendingUpdateJSON = new JSONObject (pendingUpdateString );
241
+ int rollbackTimeout = pendingUpdateJSON .getInt (PENDING_UPDATE_ROLLBACK_TIMEOUT_KEY );
242
+ if (0 != rollbackTimeout ) {
243
+ startRollbackTimer (rollbackTimeout );
244
+ }
245
+
246
+ settings .edit ().remove (PENDING_UPDATE_KEY ).commit ();
247
+ } catch (JSONException e ) {
248
+ // Should not happen.
249
+ throw new CodePushUnknownException ("Unable to parse pending update metadata " +
250
+ pendingUpdateString + " stored in SharedPreferences" , e );
251
+ }
252
+ }
253
+ }
254
+
280
255
private class CodePushNativeModule extends ReactContextBaseJavaModule {
281
256
282
257
private void loadBundle () {
@@ -289,15 +264,33 @@ private void loadBundle() {
289
264
public void installUpdate (ReadableMap updatePackage , int rollbackTimeout , int installMode , Promise promise ) {
290
265
try {
291
266
codePushPackage .installPackage (updatePackage );
267
+
268
+ String pendingHash = CodePushUtils .tryGetString (updatePackage , codePushPackage .PACKAGE_HASH_KEY );
269
+ if (pendingHash == null ) {
270
+ throw new CodePushUnknownException ("Update package to be installed has no hash." );
271
+ } else {
272
+ savePendingUpdate (pendingHash , rollbackTimeout );
273
+ }
274
+
292
275
if (installMode != CodePushInstallMode .IMMEDIATE .getValue ()) {
293
- resumablePendingUpdateAvailable = installMode == CodePushInstallMode .ON_NEXT_RESUME .getValue ();
294
- String pendingHash = CodePushUtils .tryGetString (updatePackage , codePushPackage .PACKAGE_HASH_KEY );
295
- if (pendingHash == null ) {
296
- throw new CodePushUnknownException ("Update package to be installed has no hash." );
297
- } else {
298
- savePendingUpdate (pendingHash , rollbackTimeout );
299
- }
276
+ restartPendingUpdate ();
277
+ } else if (installMode == CodePushInstallMode .ON_NEXT_RESUME .getValue ()) {
278
+ getReactApplicationContext ().addLifecycleEventListener (new LifecycleEventListener () {
279
+ @ Override
280
+ public void onHostResume () {
281
+ restartPendingUpdate ();
282
+ }
283
+
284
+ @ Override
285
+ public void onHostPause () {
286
+ }
287
+
288
+ @ Override
289
+ public void onHostDestroy () {
290
+ }
291
+ });
300
292
}
293
+
301
294
promise .resolve ("" );
302
295
} catch (IOException e ) {
303
296
e .printStackTrace ();
@@ -377,12 +370,34 @@ public void setUsingTestFolder(boolean shouldUseTestFolder) {
377
370
378
371
@ ReactMethod
379
372
public void restartImmediateUpdate (int rollbackTimeout ) {
380
- initializeUpdateWithRollbackTimeout ( rollbackTimeout , /*needsRestart*/ true );
373
+ loadBundle ( );
381
374
}
382
375
383
376
@ ReactMethod
384
377
public void restartPendingUpdate () {
385
- checkForPendingUpdate (/*needsRestart*/ true );
378
+ SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
379
+ String pendingUpdateString = settings .getString (PENDING_UPDATE_KEY , null );
380
+
381
+ if (pendingUpdateString != null ) {
382
+ try {
383
+ JSONObject pendingUpdateJSON = new JSONObject (pendingUpdateString );
384
+ String pendingHash = pendingUpdateJSON .getString (PENDING_UPDATE_HASH_KEY );
385
+ String currentHash = codePushPackage .getCurrentPackageHash ();
386
+ if (!pendingHash .equals (currentHash )) {
387
+ throw new CodePushUnknownException ("Pending hash " + pendingHash +
388
+ " and current hash " + currentHash + " are different" );
389
+ }
390
+
391
+ loadBundle ();
392
+ } catch (JSONException e ) {
393
+ // Should not happen.
394
+ throw new CodePushUnknownException ("Unable to parse pending update metadata " +
395
+ pendingUpdateString + " stored in SharedPreferences" , e );
396
+ } catch (IOException e ) {
397
+ // There is no current package hash.
398
+ throw new CodePushUnknownException ("Should not register a pending update without a saving a current package" , e );
399
+ }
400
+ }
386
401
}
387
402
388
403
@ Override
@@ -414,21 +429,6 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactAppli
414
429
nativeModules .add (CodePush .this .codePushNativeModule );
415
430
nativeModules .add (dialogModule );
416
431
417
- reactApplicationContext .addLifecycleEventListener (new LifecycleEventListener () {
418
- @ Override
419
- public void onHostResume () {
420
- CodePush .this .checkForPendingUpdateDuringResume ();
421
- }
422
-
423
- @ Override
424
- public void onHostPause () {
425
- }
426
-
427
- @ Override
428
- public void onHostDestroy () {
429
- }
430
- });
431
-
432
432
return nativeModules ;
433
433
}
434
434
0 commit comments