From 538db291ec2cae0450bb98c6d69e46ca97b6678e Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Mon, 11 Jul 2016 15:57:04 -0700 Subject: [PATCH 1/2] Add resume listener for immediate installs, no-op loadBundle if currentActivity is null --- .../codepush/react/CodePushNativeModule.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java index 42644b4..eb873ac 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java @@ -102,7 +102,11 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { mCodePush.clearDebugCacheIfNeeded(); final Activity currentActivity = getCurrentActivity(); - if (!ReactActivity.class.isInstance(currentActivity)) { + if (currentActivity == null) { + // The currentActivity can be null if it is backgrounded / destroyed, so we simply + // no-op to prevent any null pointer exceptions. + return; + } else if (!ReactActivity.class.isInstance(currentActivity)) { // Our preferred reload logic relies on the user's Activity inheriting // from the core ReactActivity class, so if it doesn't, we fallback // early to our legacy behavior. @@ -379,7 +383,12 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { mSettingsManager.savePendingUpdate(pendingHash, /* isLoading */false); } - if (installMode == CodePushInstallMode.ON_NEXT_RESUME.getValue()) { + if (installMode == CodePushInstallMode.ON_NEXT_RESUME.getValue() || + // We also add the resume listener if the installMode is IMMEDIATE, because + // if the current activity is backgrounded, we want to reload the bundle when + // it comes back into the foreground. + installMode == CodePushInstallMode.IMMEDIATE.getValue()) { + // Store the minimum duration on the native module as an instance // variable instead of relying on a closure below, so that any // subsequent resume-based installs could override it. From 3ce96e94335d9b81e17c267c835d9e87404f37d7 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Mon, 11 Jul 2016 16:51:27 -0700 Subject: [PATCH 2/2] ignore minimumBackgroundDuration for immediate installs --- .../codepush/react/CodePushNativeModule.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java index eb873ac..3a31f1e 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushNativeModule.java @@ -388,7 +388,7 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { // if the current activity is backgrounded, we want to reload the bundle when // it comes back into the foreground. installMode == CodePushInstallMode.IMMEDIATE.getValue()) { - + // Store the minimum duration on the native module as an instance // variable instead of relying on a closure below, so that any // subsequent resume-based installs could override it. @@ -401,15 +401,19 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { @Override public void onHostResume() { - // Determine how long the app was in the background and ensure - // that it meets the minimum duration amount of time. - long durationInBackground = 0; - if (lastPausedDate != null) { - durationInBackground = (new Date().getTime() - lastPausedDate.getTime()) / 1000; - } - - if (durationInBackground >= CodePushNativeModule.this.mMinimumBackgroundDuration) { + if (installMode == CodePushInstallMode.IMMEDIATE.getValue()) { loadBundle(); + } else { + // Determine how long the app was in the background and ensure + // that it meets the minimum duration amount of time. + long durationInBackground = 0; + if (lastPausedDate != null) { + durationInBackground = (new Date().getTime() - lastPausedDate.getTime()) / 1000; + } + + if (durationInBackground >= CodePushNativeModule.this.mMinimumBackgroundDuration) { + loadBundle(); + } } }