add comments and remove native in-mem caching

This commit is contained in:
Geoffrey Goh
2016-02-26 11:40:27 -08:00
parent c1187dbffb
commit 1c74ab3903
5 changed files with 49 additions and 63 deletions

View File

@@ -70,7 +70,7 @@ async function checkForUpdate(deploymentKey = null) {
* because we want to avoid having to install diff updates against the binary's
* version, which we can't do yet on Android.
*/
if (!update || update.updateAppVersion || localPackage && (update.packageHash === localPackage.packageHash) || localPackage && config.packageHash === localPackage.packageHash) {
if (!update || update.updateAppVersion || localPackage && (update.packageHash === localPackage.packageHash) || config.packageHash === update.packageHash) {
if (update && update.updateAppVersion) {
log("An update is available but it is targeting a newer binary version than you are currently running.");
}

View File

@@ -404,6 +404,8 @@ public class CodePush {
android.provider.Settings.Secure.ANDROID_ID));
String binaryHash = CodePushUpdateUtils.getHashForBinaryContents(mainActivity, 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(PACKAGE_HASH_KEY, binaryHash);
}

View File

@@ -24,10 +24,6 @@ public class CodePushUpdateUtils {
private static final String CODE_PUSH_HASH_FILE_NAME = "CodePushHash.json";
// These variables are used to cache the hash of the binary contents in memory.
private static String binaryHash = null;
private static boolean didLoadBinaryHash = false;
private static void addContentsOfFolderToManifest(String folderPath, String pathPrefix, ArrayList<String> manifest) {
File folder = new File(folderPath);
File[] folderFiles = folder.listFiles();
@@ -110,22 +106,17 @@ public class CodePushUpdateUtils {
}
public static String getHashForBinaryContents(Activity mainActivity, boolean isDebugMode) {
if (!didLoadBinaryHash) {
didLoadBinaryHash = true;
try {
binaryHash = CodePushUtils.getStringFromInputStream(mainActivity.getAssets().open(CODE_PUSH_HASH_FILE_NAME));
} catch (IOException e) {
if (!isDebugMode) {
// Only print this message in "Release" mode. In "Debug", we may not have the
// hash if the build skips bundling the files.
CodePushUtils.log("Unable to get the hash of the binary's bundled resources - \"codepush.gradle\" may have not been added to the build definition.");
}
return null;
try {
return CodePushUtils.getStringFromInputStream(mainActivity.getAssets().open(CODE_PUSH_HASH_FILE_NAME));
} catch (IOException e) {
if (!isDebugMode) {
// Only print this message in "Release" mode. In "Debug", we may not have the
// hash if the build skips bundling the files.
CodePushUtils.log("Unable to get the hash of the binary's bundled resources - \"codepush.gradle\" may have not been added to the build definition.");
}
}
return binaryHash;
return null;
}
}
public static void verifyHashForDiffUpdate(String folderPath, String expectedHash) {

View File

@@ -447,6 +447,9 @@ RCT_EXPORT_METHOD(getConfiguration:(RCTPromiseResolveBlock)resolve
}
if (binaryHash == nil) {
// The hash was not generated either due to a previous unknown error or the fact that
// the React Native assets were not bundled in the binary (e.g. during dev/simulator)
// builds.
resolve(configuration);
return;
}

View File

@@ -7,10 +7,6 @@ NSString * const AssetsFolderName = @"assets";
NSString * const BinaryHashKey = @"CodePushBinaryHash";
NSString * const ManifestFolderPrefix = @"CodePush";
// These variables are used to cache the hash of the binary contents in memory.
static NSString *binaryHash = nil;
static BOOL didLoadBinaryHash = NO;
+ (void)addContentsOfFolderToManifest:(NSString *)folderPath
pathPrefix:(NSString *)pathPrefix
manifest:(NSMutableArray *)manifest
@@ -162,49 +158,43 @@ static BOOL didLoadBinaryHash = NO;
+ (NSString *)getHashForBinaryContents:(NSURL *)binaryBundleUrl
error:(NSError **)error
{
if (!didLoadBinaryHash) {
didLoadBinaryHash = YES;
// Get the cached hash from user preferences if it exists.
NSString *binaryModifiedDate = [self modifiedDateStringOfFileAtURL:binaryBundleUrl];
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *binaryHashDictionary = [preferences objectForKey:BinaryHashKey];
if (binaryHashDictionary != nil) {
binaryHash = [binaryHashDictionary objectForKey:binaryModifiedDate];
if (binaryHash == nil) {
[preferences removeObjectForKey:BinaryHashKey];
[preferences synchronize];
} else {
return binaryHash;
}
// Get the cached hash from user preferences if it exists.
NSString *binaryModifiedDate = [self modifiedDateStringOfFileAtURL:binaryBundleUrl];
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *binaryHashDictionary = [preferences objectForKey:BinaryHashKey];
NSString *binaryHash = nil;
if (binaryHashDictionary != nil) {
binaryHash = [binaryHashDictionary objectForKey:binaryModifiedDate];
if (binaryHash == nil) {
[preferences removeObjectForKey:BinaryHashKey];
[preferences synchronize];
} else {
return binaryHash;
}
binaryHashDictionary = [NSMutableDictionary dictionary];
NSString *assetsPath = [CodePushPackage getBinaryAssetsPath];
NSMutableArray *manifest = [NSMutableArray array];
[self addContentsOfFolderToManifest:assetsPath
pathPrefix:[NSString stringWithFormat:@"%@/%@", [self getManifestFolderPrefix], @"assets"]
manifest:manifest
error:error];
if (*error) {
return nil;
}
NSData *jsBundleContents = [NSData dataWithContentsOfURL:binaryBundleUrl];
NSString *jsBundleContentsHash = [self computeHashForData:jsBundleContents];
[manifest addObject:[[NSString stringWithFormat:@"%@/%@:", [self getManifestFolderPrefix], [binaryBundleUrl lastPathComponent]] stringByAppendingString:jsBundleContentsHash]];
binaryHash = [self computeFinalHashFromManifest:manifest error:error];
// Cache the hash in user preferences. This assumes that the modified date for the
// JS bundle changes every time a new bundle is generated by the packager.
[binaryHashDictionary setObject:binaryHash forKey:binaryModifiedDate];
[preferences setObject:binaryHashDictionary forKey:BinaryHashKey];
[preferences synchronize];
return binaryHash;
}
// Use the cached hash in memory.
binaryHashDictionary = [NSMutableDictionary dictionary];
NSString *assetsPath = [CodePushPackage getBinaryAssetsPath];
NSMutableArray *manifest = [NSMutableArray array];
[self addContentsOfFolderToManifest:assetsPath
pathPrefix:[NSString stringWithFormat:@"%@/%@", [self getManifestFolderPrefix], @"assets"]
manifest:manifest
error:error];
if (*error) {
return nil;
}
NSData *jsBundleContents = [NSData dataWithContentsOfURL:binaryBundleUrl];
NSString *jsBundleContentsHash = [self computeHashForData:jsBundleContents];
[manifest addObject:[[NSString stringWithFormat:@"%@/%@:", [self getManifestFolderPrefix], [binaryBundleUrl lastPathComponent]] stringByAppendingString:jsBundleContentsHash]];
binaryHash = [self computeFinalHashFromManifest:manifest error:error];
// Cache the hash in user preferences. This assumes that the modified date for the
// JS bundle changes every time a new bundle is generated by the packager.
[binaryHashDictionary setObject:binaryHash forKey:binaryModifiedDate];
[preferences setObject:binaryHashDictionary forKey:BinaryHashKey];
[preferences synchronize];
return binaryHash;
}