Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit f47e65c

Browse files
committed
Adding Android implementation
1 parent 32792cc commit f47e65c

File tree

1 file changed

+37
-21
lines changed
  • android/app/src/main/java/com/microsoft/codepush/react

1 file changed

+37
-21
lines changed

android/app/src/main/java/com/microsoft/codepush/react/CodePush.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
import java.io.File;
3333
import java.io.IOException;
3434
import java.util.ArrayList;
35+
import java.util.Date;
3536
import java.util.HashMap;
3637
import java.util.List;
3738
import java.util.Map;
3839
import java.util.zip.ZipEntry;
3940
import java.util.zip.ZipFile;
4041

4142
public class CodePush {
42-
4343
private static boolean needToReportRollback = false;
4444
private static boolean isRunningBinaryVersion = false;
4545
private static boolean testConfigurationFlag = false;
@@ -348,8 +348,9 @@ public void clearUpdates() {
348348
}
349349

350350
private class CodePushNativeModule extends ReactContextBaseJavaModule {
351-
351+
private Date lastPausedDate = null;
352352
private LifecycleEventListener lifecycleEventListener = null;
353+
private int minimumBackgroundDuration = 0;
353354

354355
private void loadBundle() {
355356
Intent intent = mainActivity.getIntent();
@@ -491,7 +492,7 @@ protected Void doInBackground(Object... params) {
491492
}
492493

493494
@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) {
495496
AsyncTask asyncTask = new AsyncTask() {
496497
@Override
497498
protected Void doInBackground(Object... params) {
@@ -505,24 +506,39 @@ protected Void doInBackground(Object... params) {
505506
savePendingUpdate(pendingHash, /* isLoading */false);
506507
}
507508

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+
}
526542
}
527543

528544
promise.resolve("");

0 commit comments

Comments
 (0)