From 2eddfd29f31da8e0eb3f6ca63cb92bb804d3bc8e Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 11 Dec 2015 15:04:12 -0800 Subject: [PATCH 1/4] fix-date-comparison-bug --- .../microsoft/codepush/react/CodePush.java | 49 +++++++++++++------ .../codepush/react/CodePushUtils.java | 5 ++ 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java index fe311d9..a2cc43f 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java @@ -1,6 +1,6 @@ package com.microsoft.codepush.react; -import com.facebook.react.ReactPackage; +import com.facebook.react.*; import com.facebook.react.bridge.JavaScriptModule; import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.NativeModule; @@ -54,6 +54,7 @@ public class CodePush { private final String DOWNLOAD_PROGRESS_EVENT_NAME = "CodePushDownloadProgress"; private final String RESOURCES_BUNDLE = "resources.arsc"; private final String REACT_DEV_BUNDLE_CACHE_FILE_NAME = "ReactNativeDevBundle.js"; + private final String BINARY_MODIFIED_TIME_KEY = "binaryModifiedTime"; private CodePushPackage codePushPackage; private CodePushReactPackage codePushReactPackage; @@ -95,22 +96,31 @@ public class CodePush { return codePushReactPackage; } - public String getBundleUrl(String assetsBundleFileName) { - this.assetsBundleFileName = assetsBundleFileName; - String binaryJsBundleUrl = ASSETS_BUNDLE_PREFIX + assetsBundleFileName; - ZipFile applicationFile; - long binaryResourcesModifiedTime = -1; - + public long getBinaryResourcesModifiedTime() { ApplicationInfo ai = null; + ZipFile applicationFile = null; try { ai = applicationContext.getPackageManager().getApplicationInfo(applicationContext.getPackageName(), 0); applicationFile = new ZipFile(ai.sourceDir); ZipEntry classesDexEntry = applicationFile.getEntry(RESOURCES_BUNDLE); - binaryResourcesModifiedTime = classesDexEntry.getTime(); - applicationFile.close(); + return classesDexEntry.getTime(); } catch (PackageManager.NameNotFoundException | IOException e) { throw new CodePushUnknownException("Error in getting file information about compiled resources", e); + } finally { + if (applicationFile != null) { + try { + applicationFile.close(); + } catch (IOException e) { + throw new CodePushUnknownException("Error in closing application file.", e); + } + } } + } + + public String getBundleUrl(String assetsBundleFileName) { + this.assetsBundleFileName = assetsBundleFileName; + String binaryJsBundleUrl = ASSETS_BUNDLE_PREFIX + assetsBundleFileName; + long binaryResourcesModifiedTime = getBinaryResourcesModifiedTime(); try { String packageFilePath = codePushPackage.getCurrentPackageBundlePath(); @@ -119,15 +129,22 @@ public class CodePush { return binaryJsBundleUrl; } - File packageFile = new File(packageFilePath); - if (packageFile.lastModified() < binaryResourcesModifiedTime) { + ReadableMap packageMetadata = codePushPackage.getCurrentPackage(); + // May throw NumberFormatException. + Long binaryModifiedDateDuringPackageInstall = Long.parseLong(CodePushUtils.tryGetString(packageMetadata, BINARY_MODIFIED_TIME_KEY)); + if (binaryModifiedDateDuringPackageInstall == binaryResourcesModifiedTime) { + return packageFilePath; + } else { // The binary version is newer. + Log.d(CODE_PUSH_TAG, "Found a package installed via CodePush that was " + + "installed under a different binary version, so the JS bundle packaged " + + "in the binary will be used as the most current package."); return binaryJsBundleUrl; } - - return packageFilePath; } catch (IOException e) { throw new CodePushUnknownException("Error in getting current package bundle path", e); + } catch (NumberFormatException e) { + throw new CodePushUnknownException("Error in reading binary modified date from package metadata", e); } } @@ -235,9 +252,11 @@ public class CodePush { if (updateIsLoading) { // Pending update was initialized, but notifyApplicationReady was not called. // Therefore, deduce that it is a broken update and rollback. + Log.d(CODE_PUSH_TAG, "Update did not finish loading the last time, rolling back to a previous version."); rollbackPackage(); } else { // Clear the React dev bundle cache so that new updates can be loaded. + if (com.facebook.react.BuildConfig.DEBUG) clearReactDevBundleCache(); // Mark that we tried to initialize the new update, so that if it crashes, // we will know that we need to rollback when the app next starts. @@ -325,7 +344,9 @@ public class CodePush { @Override protected Void doInBackground(Object[] params) { try { - codePushPackage.downloadPackage(applicationContext, updatePackage, new DownloadProgressCallback() { + WritableMap mutableUpdatePackage = CodePushUtils.convertReadableMapToWritableMap(updatePackage); + mutableUpdatePackage.putString(BINARY_MODIFIED_TIME_KEY, "" + getBinaryResourcesModifiedTime()); + codePushPackage.downloadPackage(applicationContext, mutableUpdatePackage, new DownloadProgressCallback() { @Override public void call(DownloadProgress downloadProgress) { getReactApplicationContext() diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java index ae02351..757b149 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java @@ -248,6 +248,11 @@ public class CodePushUtils { return jsonArr; } + public static WritableMap convertReadableMapToWritableMap(ReadableMap map) { + JSONObject mapJSON = convertReadableToJsonObject(map); + return convertJsonObjectToWriteable(mapJSON); + } + public static String tryGetString(ReadableMap map, String key) { try { return map.getString(key); From c4d04155faf91eb5456d4310d930dec2e7b84470 Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 11 Dec 2015 15:05:09 -0800 Subject: [PATCH 2/4] undo import change --- .../src/main/java/com/microsoft/codepush/react/CodePush.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java index a2cc43f..16228a6 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java @@ -1,6 +1,6 @@ package com.microsoft.codepush.react; -import com.facebook.react.*; +import com.facebook.react.ReactPackage; import com.facebook.react.bridge.JavaScriptModule; import com.facebook.react.bridge.LifecycleEventListener; import com.facebook.react.bridge.NativeModule; From 9350fbea019901b0b68785270c1d2a1020703e0a Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 11 Dec 2015 16:14:41 -0800 Subject: [PATCH 3/4] change logging tag --- .../java/com/microsoft/codepush/react/CodePush.java | 13 ++++++------- .../com/microsoft/codepush/react/CodePushUtils.java | 8 ++++++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java index 16228a6..ce05ad6 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java @@ -50,7 +50,6 @@ public class CodePush { private final String PENDING_UPDATE_IS_LOADING_KEY = "isLoading"; private final String ASSETS_BUNDLE_PREFIX = "assets://"; private final String CODE_PUSH_PREFERENCES = "CodePush"; - private final String CODE_PUSH_TAG = "CodePush"; private final String DOWNLOAD_PROGRESS_EVENT_NAME = "CodePushDownloadProgress"; private final String RESOURCES_BUNDLE = "resources.arsc"; private final String REACT_DEV_BUNDLE_CACHE_FILE_NAME = "ReactNativeDevBundle.js"; @@ -136,9 +135,9 @@ public class CodePush { return packageFilePath; } else { // The binary version is newer. - Log.d(CODE_PUSH_TAG, "Found a package installed via CodePush that was " + - "installed under a different binary version, so the JS bundle packaged " + - "in the binary will be used as the most current package."); + CodePushUtils.log("Found a package installed via CodePush that was installed " + + "under a different binary version, so the JS bundle packaged in the " + + "binary will be used as the most current package."); return binaryJsBundleUrl; } } catch (IOException e) { @@ -237,8 +236,8 @@ public class CodePush { return pendingUpdate; } catch (JSONException e) { // Should not happen. - Log.e(CODE_PUSH_TAG, "Unable to parse pending update metadata " + - pendingUpdateString + " stored in SharedPreferences", e); + CodePushUtils.log("Unable to parse pending update metadata " + pendingUpdateString + + " stored in SharedPreferences"); return null; } } @@ -252,7 +251,7 @@ public class CodePush { if (updateIsLoading) { // Pending update was initialized, but notifyApplicationReady was not called. // Therefore, deduce that it is a broken update and rollback. - Log.d(CODE_PUSH_TAG, "Update did not finish loading the last time, rolling back to a previous version."); + CodePushUtils.log("Update did not finish loading the last time, rolling back to a previous version."); rollbackPackage(); } else { // Clear the React dev bundle cache so that new updates can be loaded. diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java index 757b149..80203dc 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java @@ -1,5 +1,7 @@ package com.microsoft.codepush.react; +import android.util.Log; + import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.NoSuchKeyException; import com.facebook.react.bridge.ReadableArray; @@ -23,6 +25,8 @@ import java.util.Iterator; public class CodePushUtils { + public static final String REACT_NATIVE_LOG_TAG = "ReactNative"; + public static String appendPathComponent(String basePath, String appendPathComponent) { return new File(basePath, appendPathComponent).getAbsolutePath(); } @@ -260,4 +264,8 @@ public class CodePushUtils { return null; } } + + public static void log(String message) { + Log.d(REACT_NATIVE_LOG_TAG, "[CodePush] " + message); + } } From 61f83fde0d9f80965db0584a60e2c7a10fb407cb Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Fri, 11 Dec 2015 17:07:23 -0800 Subject: [PATCH 4/4] change log message --- .../main/java/com/microsoft/codepush/react/CodePush.java | 7 +++---- .../java/com/microsoft/codepush/react/CodePushUtils.java | 4 ++++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java index ce05ad6..4104837 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePush.java @@ -125,6 +125,7 @@ public class CodePush { String packageFilePath = codePushPackage.getCurrentPackageBundlePath(); if (packageFilePath == null) { // There has not been any downloaded updates. + CodePushUtils.logBundleUrl(binaryJsBundleUrl); return binaryJsBundleUrl; } @@ -132,12 +133,11 @@ public class CodePush { // May throw NumberFormatException. Long binaryModifiedDateDuringPackageInstall = Long.parseLong(CodePushUtils.tryGetString(packageMetadata, BINARY_MODIFIED_TIME_KEY)); if (binaryModifiedDateDuringPackageInstall == binaryResourcesModifiedTime) { + CodePushUtils.logBundleUrl(packageFilePath); return packageFilePath; } else { // The binary version is newer. - CodePushUtils.log("Found a package installed via CodePush that was installed " + - "under a different binary version, so the JS bundle packaged in the " + - "binary will be used as the most current package."); + CodePushUtils.logBundleUrl(binaryJsBundleUrl); return binaryJsBundleUrl; } } catch (IOException e) { @@ -255,7 +255,6 @@ public class CodePush { rollbackPackage(); } else { // Clear the React dev bundle cache so that new updates can be loaded. - if (com.facebook.react.BuildConfig.DEBUG) clearReactDevBundleCache(); // Mark that we tried to initialize the new update, so that if it crashes, // we will know that we need to rollback when the app next starts. diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java index 80203dc..295fc37 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUtils.java @@ -268,4 +268,8 @@ public class CodePushUtils { public static void log(String message) { Log.d(REACT_NATIVE_LOG_TAG, "[CodePush] " + message); } + + public static void logBundleUrl(String path) { + log("Loading JS bundle from \"" + path + "\""); + } }