32
32
import java .io .File ;
33
33
import java .io .IOException ;
34
34
import java .util .ArrayList ;
35
+ import java .util .Date ;
35
36
import java .util .HashMap ;
36
37
import java .util .List ;
37
38
import java .util .Map ;
38
39
import java .util .zip .ZipEntry ;
39
40
import java .util .zip .ZipFile ;
40
41
41
42
public class CodePush {
42
-
43
43
private static boolean needToReportRollback = false ;
44
44
private static boolean isRunningBinaryVersion = false ;
45
45
private static boolean testConfigurationFlag = false ;
@@ -348,8 +348,9 @@ public void clearUpdates() {
348
348
}
349
349
350
350
private class CodePushNativeModule extends ReactContextBaseJavaModule {
351
-
351
+ private Date lastPausedDate = null ;
352
352
private LifecycleEventListener lifecycleEventListener = null ;
353
+ private int minimumBackgroundDuration = 0 ;
353
354
354
355
private void loadBundle () {
355
356
Intent intent = mainActivity .getIntent ();
@@ -491,7 +492,7 @@ protected Void doInBackground(Object... params) {
491
492
}
492
493
493
494
@ ReactMethod
494
- public void installUpdate (final ReadableMap updatePackage , final int installMode , final Promise promise ) {
495
+ public void installUpdate (final ReadableMap updatePackage , final int installMode , final int minimumBackgroundDuration , final Promise promise ) {
495
496
AsyncTask asyncTask = new AsyncTask () {
496
497
@ Override
497
498
protected Void doInBackground (Object ... params ) {
@@ -505,24 +506,39 @@ protected Void doInBackground(Object... params) {
505
506
savePendingUpdate (pendingHash , /* isLoading */ false );
506
507
}
507
508
508
- if (installMode == CodePushInstallMode .ON_NEXT_RESUME .getValue () &&
509
- lifecycleEventListener == null ) {
510
- // Ensure we do not add the listener twice.
511
- lifecycleEventListener = new LifecycleEventListener () {
512
- @ Override
513
- public void onHostResume () {
514
- loadBundle ();
515
- }
516
-
517
- @ Override
518
- public void onHostPause () {
519
- }
520
-
521
- @ Override
522
- public void onHostDestroy () {
523
- }
524
- };
525
- getReactApplicationContext ().addLifecycleEventListener (lifecycleEventListener );
509
+ if (installMode == CodePushInstallMode .ON_NEXT_RESUME .getValue ()) {
510
+ // Store the minimum duration on the native module as an instance
511
+ // variable instead of relying on a closure below, so that any
512
+ // subsequent resume-based installs could override it.
513
+ CodePushNativeModule .this .minimumBackgroundDuration = minimumBackgroundDuration ;
514
+
515
+ if (lifecycleEventListener == null ) {
516
+ // Ensure we do not add the listener twice.
517
+ lifecycleEventListener = new LifecycleEventListener () {
518
+ @ Override
519
+ public void onHostResume () {
520
+ // Determine how long the app was in the background and ensure
521
+ // that it meets the minimum duration amount of time.
522
+ int durationInBackground = (new Date () - lastPausedDate ) / 1000 ;
523
+ if (durationInBackground >= CodePushNativeModule .this .minimumBackgroundDuration ) {
524
+ loadBundle ();
525
+ }
526
+ }
527
+
528
+ @ Override
529
+ public void onHostPause () {
530
+ // Save the current time so that when the app is later
531
+ // resumed, we can detect how long it was in the background.
532
+ lastPausedDate = new Date ();
533
+ }
534
+
535
+ @ Override
536
+ public void onHostDestroy () {
537
+ }
538
+ };
539
+
540
+ getReactApplicationContext ().addLifecycleEventListener (lifecycleEventListener );
541
+ }
526
542
}
527
543
528
544
promise .resolve ("" );
0 commit comments