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

Commit b665afd

Browse files
Merge pull request #416 from Microsoft/getConfig
[Android] Guarding against NPEs in getConfiguration
2 parents 59f6a25 + d26188f commit b665afd

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

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

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,30 @@
3232
import java.util.Map;
3333

3434
public class CodePushNativeModule extends ReactContextBaseJavaModule {
35+
private String mBinaryContentsHash = null;
36+
private String mClientUniqueId = null;
3537
private LifecycleEventListener mLifecycleEventListener = null;
3638
private int mMinimumBackgroundDuration = 0;
39+
3740
private CodePush mCodePush;
38-
private CodePushUpdateManager mUpdateManager;
39-
private CodePushTelemetryManager mTelemetryManager;
4041
private SettingsManager mSettingsManager;
42+
private CodePushTelemetryManager mTelemetryManager;
43+
private CodePushUpdateManager mUpdateManager;
4144

4245
private static final String REACT_APPLICATION_CLASS_NAME = "com.facebook.react.ReactApplication";
4346
private static final String REACT_NATIVE_HOST_CLASS_NAME = "com.facebook.react.ReactNativeHost";
4447

4548
public CodePushNativeModule(ReactApplicationContext reactContext, CodePush codePush, CodePushUpdateManager codePushUpdateManager, CodePushTelemetryManager codePushTelemetryManager, SettingsManager settingsManager) {
4649
super(reactContext);
50+
4751
mCodePush = codePush;
48-
mUpdateManager = codePushUpdateManager;
49-
mTelemetryManager = codePushTelemetryManager;
5052
mSettingsManager = settingsManager;
53+
mTelemetryManager = codePushTelemetryManager;
54+
mUpdateManager = codePushUpdateManager;
55+
56+
// Initialize module state while we have a reference to the current context.
57+
mBinaryContentsHash = CodePushUpdateUtils.getHashForBinaryContents(reactContext, mCodePush.isDebugMode());
58+
mClientUniqueId = Settings.Secure.getString(reactContext.getContentResolver(), Settings.Secure.ANDROID_ID);
5159
}
5260

5361
@Override
@@ -240,19 +248,15 @@ public void dispatchDownloadProgressEvent() {
240248

241249
@ReactMethod
242250
public void getConfiguration(Promise promise) {
243-
Activity currentActivity = getCurrentActivity();
244251
WritableNativeMap configMap = new WritableNativeMap();
245252
configMap.putString("appVersion", mCodePush.getAppVersion());
253+
configMap.putString("clientUniqueId", mClientUniqueId);
246254
configMap.putString("deploymentKey", mCodePush.getDeploymentKey());
247255
configMap.putString("serverUrl", mCodePush.getServerUrl());
248-
configMap.putString("clientUniqueId",
249-
Settings.Secure.getString(currentActivity.getContentResolver(),
250-
android.provider.Settings.Secure.ANDROID_ID));
251-
String binaryHash = CodePushUpdateUtils.getHashForBinaryContents(currentActivity, mCodePush.isDebugMode());
252-
if (binaryHash != null) {
253-
// binaryHash will be null if the React Native assets were not bundled into the APK
254-
// (e.g. in Debug builds)
255-
configMap.putString(CodePushConstants.PACKAGE_HASH_KEY, binaryHash);
256+
257+
// The binary hash may be null in debug builds
258+
if (mBinaryContentsHash != null) {
259+
configMap.putString(CodePushConstants.PACKAGE_HASH_KEY, mBinaryContentsHash);
256260
}
257261

258262
promise.resolve(configMap);

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package com.microsoft.codepush.react;
22

3-
import android.app.Activity;
3+
import android.content.Context;
44

55
import com.facebook.react.bridge.ReadableArray;
66
import com.facebook.react.bridge.WritableMap;
@@ -98,9 +98,9 @@ public static String findJSBundleInUpdateContents(String folderPath, String expe
9898
return null;
9999
}
100100

101-
public static String getHashForBinaryContents(Activity mainActivity, boolean isDebugMode) {
101+
public static String getHashForBinaryContents(Context context, boolean isDebugMode) {
102102
try {
103-
return CodePushUtils.getStringFromInputStream(mainActivity.getAssets().open(CodePushConstants.CODE_PUSH_HASH_FILE_NAME));
103+
return CodePushUtils.getStringFromInputStream(context.getAssets().open(CodePushConstants.CODE_PUSH_HASH_FILE_NAME));
104104
} catch (IOException e) {
105105
if (!isDebugMode) {
106106
// Only print this message in "Release" mode. In "Debug", we may not have the
@@ -128,4 +128,4 @@ public static void verifyHashForDiffUpdate(String folderPath, String expectedHas
128128
throw new CodePushInvalidUpdateException("The update contents failed the data integrity check.");
129129
}
130130
}
131-
}
131+
}

0 commit comments

Comments
 (0)