From 8ad33ebc2dd454a31bb9e9789b9e0c9c1b330cce Mon Sep 17 00:00:00 2001 From: Jonathan Carter Date: Tue, 5 Jul 2016 17:50:39 -0700 Subject: [PATCH 1/4] Guarding against NPEs --- .../codepush/react/CodePushNativeModule.java | 24 ++++++++++++------- 1 file changed, 15 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 1e733a9..1079987 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 @@ -80,6 +80,8 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { } private void loadBundleLegacy(final Activity currentActivity) { + CodePushUtils.log("Legacy restart logic being used"); + mCodePush.invalidateCurrentInstance(); currentActivity.runOnUiThread(new Runnable() { @@ -240,19 +242,23 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { @ReactMethod public void getConfiguration(Promise promise) { - Activity currentActivity = getCurrentActivity(); WritableNativeMap configMap = new WritableNativeMap(); configMap.putString("appVersion", mCodePush.getAppVersion()); configMap.putString("deploymentKey", mCodePush.getDeploymentKey()); configMap.putString("serverUrl", mCodePush.getServerUrl()); - configMap.putString("clientUniqueId", - Settings.Secure.getString(currentActivity.getContentResolver(), - android.provider.Settings.Secure.ANDROID_ID)); - String binaryHash = CodePushUpdateUtils.getHashForBinaryContents(currentActivity, mCodePush.isDebugMode()); - if (binaryHash != null) { - // binaryHash will be null if the React Native assets were not bundled into the APK - // (e.g. in Debug builds) - configMap.putString(CodePushConstants.PACKAGE_HASH_KEY, binaryHash); + + Activity currentActivity = getCurrentActivity(); + if (currentActivity != null) { + configMap.putString("clientUniqueId", + Settings.Secure.getString(currentActivity.getContentResolver(), + android.provider.Settings.Secure.ANDROID_ID)); + + String binaryHash = CodePushUpdateUtils.getHashForBinaryContents(currentActivity, mCodePush.isDebugMode()); + if (binaryHash != null) { + // binaryHash will be null if the React Native assets were not bundled into the APK + // (e.g. in Debug builds) + configMap.putString(CodePushConstants.PACKAGE_HASH_KEY, binaryHash); + } } promise.resolve(configMap); From 3a351d4d05a9228cd31e31a1e60dfecddeefa787 Mon Sep 17 00:00:00 2001 From: Jonathan Carter Date: Tue, 5 Jul 2016 17:56:22 -0700 Subject: [PATCH 2/4] Removing extra logging --- .../java/com/microsoft/codepush/react/CodePushNativeModule.java | 2 -- 1 file changed, 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 1079987..e332dc6 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 @@ -80,8 +80,6 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { } private void loadBundleLegacy(final Activity currentActivity) { - CodePushUtils.log("Legacy restart logic being used"); - mCodePush.invalidateCurrentInstance(); currentActivity.runOnUiThread(new Runnable() { From 71eea1771f9ef0abb5642138ea381067d1e8a6d4 Mon Sep 17 00:00:00 2001 From: Jonathan Carter Date: Tue, 5 Jul 2016 18:19:57 -0700 Subject: [PATCH 3/4] Capture device ID --- .../codepush/react/CodePushNativeModule.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 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 e332dc6..a564e11 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 @@ -32,22 +32,27 @@ import java.util.HashMap; import java.util.Map; public class CodePushNativeModule extends ReactContextBaseJavaModule { + private String mClientUniqueId = null; private LifecycleEventListener mLifecycleEventListener = null; private int mMinimumBackgroundDuration = 0; + private CodePush mCodePush; - private CodePushUpdateManager mUpdateManager; - private CodePushTelemetryManager mTelemetryManager; private SettingsManager mSettingsManager; + private CodePushTelemetryManager mTelemetryManager; + private CodePushUpdateManager mUpdateManager; private static final String REACT_APPLICATION_CLASS_NAME = "com.facebook.react.ReactApplication"; private static final String REACT_NATIVE_HOST_CLASS_NAME = "com.facebook.react.ReactNativeHost"; public CodePushNativeModule(ReactApplicationContext reactContext, CodePush codePush, CodePushUpdateManager codePushUpdateManager, CodePushTelemetryManager codePushTelemetryManager, SettingsManager settingsManager) { super(reactContext); + mCodePush = codePush; - mUpdateManager = codePushUpdateManager; - mTelemetryManager = codePushTelemetryManager; mSettingsManager = settingsManager; + mTelemetryManager = codePushTelemetryManager; + mUpdateManager = codePushUpdateManager; + + mClientUniqueId = Settings.Secure.getString(reactContext.getContentResolver(), Settings.Secure.ANDROID_ID); } @Override @@ -242,15 +247,12 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { public void getConfiguration(Promise promise) { WritableNativeMap configMap = new WritableNativeMap(); configMap.putString("appVersion", mCodePush.getAppVersion()); + configMap.putString("clientUniqueId", mClientUniqueId); configMap.putString("deploymentKey", mCodePush.getDeploymentKey()); configMap.putString("serverUrl", mCodePush.getServerUrl()); Activity currentActivity = getCurrentActivity(); if (currentActivity != null) { - configMap.putString("clientUniqueId", - Settings.Secure.getString(currentActivity.getContentResolver(), - android.provider.Settings.Secure.ANDROID_ID)); - String binaryHash = CodePushUpdateUtils.getHashForBinaryContents(currentActivity, mCodePush.isDebugMode()); if (binaryHash != null) { // binaryHash will be null if the React Native assets were not bundled into the APK From d26188fb12d6898c372db918e919d5938f970768 Mon Sep 17 00:00:00 2001 From: Jonathan Carter Date: Tue, 5 Jul 2016 21:40:10 -0700 Subject: [PATCH 4/4] Pre-computing binary hash --- .../codepush/react/CodePushNativeModule.java | 14 ++++++-------- .../codepush/react/CodePushUpdateUtils.java | 8 ++++---- 2 files changed, 10 insertions(+), 12 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 a564e11..42644b4 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 @@ -32,6 +32,7 @@ import java.util.HashMap; import java.util.Map; public class CodePushNativeModule extends ReactContextBaseJavaModule { + private String mBinaryContentsHash = null; private String mClientUniqueId = null; private LifecycleEventListener mLifecycleEventListener = null; private int mMinimumBackgroundDuration = 0; @@ -52,6 +53,8 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { mTelemetryManager = codePushTelemetryManager; mUpdateManager = codePushUpdateManager; + // Initialize module state while we have a reference to the current context. + mBinaryContentsHash = CodePushUpdateUtils.getHashForBinaryContents(reactContext, mCodePush.isDebugMode()); mClientUniqueId = Settings.Secure.getString(reactContext.getContentResolver(), Settings.Secure.ANDROID_ID); } @@ -251,14 +254,9 @@ public class CodePushNativeModule extends ReactContextBaseJavaModule { configMap.putString("deploymentKey", mCodePush.getDeploymentKey()); configMap.putString("serverUrl", mCodePush.getServerUrl()); - Activity currentActivity = getCurrentActivity(); - if (currentActivity != null) { - String binaryHash = CodePushUpdateUtils.getHashForBinaryContents(currentActivity, mCodePush.isDebugMode()); - if (binaryHash != null) { - // binaryHash will be null if the React Native assets were not bundled into the APK - // (e.g. in Debug builds) - configMap.putString(CodePushConstants.PACKAGE_HASH_KEY, binaryHash); - } + // The binary hash may be null in debug builds + if (mBinaryContentsHash != null) { + configMap.putString(CodePushConstants.PACKAGE_HASH_KEY, mBinaryContentsHash); } promise.resolve(configMap); diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java index f83bf98..88ad4c7 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUpdateUtils.java @@ -1,6 +1,6 @@ package com.microsoft.codepush.react; -import android.app.Activity; +import android.content.Context; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.WritableMap; @@ -98,9 +98,9 @@ public class CodePushUpdateUtils { return null; } - public static String getHashForBinaryContents(Activity mainActivity, boolean isDebugMode) { + public static String getHashForBinaryContents(Context context, boolean isDebugMode) { try { - return CodePushUtils.getStringFromInputStream(mainActivity.getAssets().open(CodePushConstants.CODE_PUSH_HASH_FILE_NAME)); + return CodePushUtils.getStringFromInputStream(context.getAssets().open(CodePushConstants.CODE_PUSH_HASH_FILE_NAME)); } catch (IOException e) { if (!isDebugMode) { // Only print this message in "Release" mode. In "Debug", we may not have the @@ -128,4 +128,4 @@ public class CodePushUpdateUtils { throw new CodePushInvalidUpdateException("The update contents failed the data integrity check."); } } -} +} \ No newline at end of file