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

Commit 303edd1

Browse files
committed
Android implementation
1 parent 3026c86 commit 303edd1

File tree

3 files changed

+65
-22
lines changed

3 files changed

+65
-22
lines changed

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

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,15 @@ public CodePushNativeModule(ReactApplicationContext reactContext) {
353353
@Override
354354
public Map<String, Object> getConstants() {
355355
final Map<String, Object> constants = new HashMap<>();
356+
356357
constants.put("codePushInstallModeImmediate", CodePushInstallMode.IMMEDIATE.getValue());
357358
constants.put("codePushInstallModeOnNextRestart", CodePushInstallMode.ON_NEXT_RESTART.getValue());
358359
constants.put("codePushInstallModeOnNextResume", CodePushInstallMode.ON_NEXT_RESUME.getValue());
360+
361+
constants.put("codePushUpdateStateRunning", CodePushUpdateState.RUNNING.getValue());
362+
constants.put("codePushUpdateStatePending", CodePushUpdateState.PENDING.getValue());
363+
constants.put("codePushUpdateStateLatest", CodePushUpdateState.LATEST.getValue());
364+
359365
return constants;
360366
}
361367

@@ -481,31 +487,53 @@ public void getConfiguration(Promise promise) {
481487

482488
promise.resolve(configMap);
483489
}
484-
490+
485491
@ReactMethod
486-
public void getCurrentPackage(final Promise promise) {
492+
public void getUpdateMetadata(final int updateState, final Promise promise) {
487493
AsyncTask<Void, Void, Void> asyncTask = new AsyncTask<Void, Void, Void>() {
488494
@Override
489495
protected Void doInBackground(Void... params) {
490496
WritableMap currentPackage = codePushPackage.getCurrentPackage();
497+
491498
if (currentPackage == null) {
492499
promise.resolve("");
493500
return null;
494501
}
495502

496-
if (isRunningBinaryVersion) {
497-
currentPackage.putBoolean("_isDebugOnly", true);
498-
}
499-
500-
Boolean isPendingUpdate = false;
503+
Boolean currentUpdateIsPending = false;
501504

502505
if (currentPackage.hasKey(PACKAGE_HASH_KEY)) {
503506
String currentHash = currentPackage.getString(PACKAGE_HASH_KEY);
504-
isPendingUpdate = CodePush.this.isPendingUpdate(currentHash);
507+
currentUpdateIsPending = CodePush.this.isPendingUpdate(currentHash);
505508
}
509+
510+
if (updateState == CodePushUpdateState.PENDING.getValue() && !currentUpdateIsPending) {
511+
// The caller wanted a pending update
512+
// but there isn't currently one.
513+
promise.resolve("");
514+
} else if (updateState == CodePushUpdateState.RUNNING.getValue() && currentUpdateIsPending) {
515+
// The caller wants the running update, but the current
516+
// one is pending, so we need to grab the previous.
517+
promise.resolve(codePushPackage.getPreviousPackage());
518+
} else {
519+
// The current package satisfies the request:
520+
// 1) Caller wanted a pending, and there is a pending update
521+
// 2) Caller wanted the running update, and there isn't a pending
522+
// 3) Calers wants the latest update, regardless if it's pending or not
523+
524+
if (isRunningBinaryVersion) {
525+
// This only matters in Debug builds. Since we do not clear "outdated" updates,
526+
// we need to indicate to the JS side that somehow we have a current update on
527+
// disk that is not actually running.
528+
currentPackage.putBoolean("_isDebugOnly", true);
529+
}
506530

507-
currentPackage.putBoolean("isPending", isPendingUpdate);
508-
promise.resolve(currentPackage);
531+
// To support differentiating pending vs. non-pending updates
532+
// when request an update state of LATEST, provide an isPending flag
533+
currentPackage.putBoolean("isPending", currentUpdateIsPending);
534+
promise.resolve(currentPackage);
535+
}
536+
509537
return null;
510538
}
511539
};

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.nio.ByteBuffer;
1919

2020
public class CodePushPackage {
21-
2221
private final String CODE_PUSH_FOLDER_PREFIX = "CodePush";
2322
private final String CURRENT_PACKAGE_KEY = "currentPackage";
2423
private final String DIFF_MANIFEST_FILE_NAME = "hotcodepush.json";
@@ -124,18 +123,21 @@ public String getPreviousPackageHash() {
124123
}
125124

126125
public WritableMap getCurrentPackage() {
127-
String folderPath = getCurrentPackageFolderPath();
128-
if (folderPath == null) {
126+
String packageHash = getCurrentPackageHash();
127+
if (packageHash == null) {
129128
return null;
130129
}
131-
132-
String packagePath = CodePushUtils.appendPathComponent(folderPath, PACKAGE_FILE_NAME);
133-
try {
134-
return CodePushUtils.getWritableMapFromFile(packagePath);
135-
} catch (IOException e) {
136-
// Should not happen unless the update metadata was somehow deleted.
130+
131+
return getPackage(packageHash);
132+
}
133+
134+
public WritableMap getPreviousPackage() {
135+
String packageHash = getPreviousPackageHash();
136+
if (packageHash == null) {
137137
return null;
138138
}
139+
140+
return getPackage(packageHash);
139141
}
140142

141143
public WritableMap getPackage(String packageHash) {
@@ -340,8 +342,6 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl, String bundl
340342
}
341343

342344
public void clearUpdates() {
343-
File statusFile = new File(getStatusFilePath());
344-
statusFile.delete();
345345
FileUtils.deleteDirectoryAtPath(getCodePushPath());
346346
}
347-
}
347+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.microsoft.codepush.react;
2+
3+
public enum CodePushUpdateState {
4+
RUNNING(0),
5+
PENDING(1),
6+
LATEST(2);
7+
8+
private final int value;
9+
CodePushUpdateState(int value) {
10+
this.value = value;
11+
}
12+
public int getValue() {
13+
return this.value;
14+
}
15+
}

0 commit comments

Comments
 (0)