From 7916856aa4fac5df97b760ff0f0489e4a8fa394d Mon Sep 17 00:00:00 2001 From: Geoffrey Goh Date: Mon, 25 Apr 2016 15:04:19 -0700 Subject: [PATCH] fix rollback android --- Examples/CodePushDemoApp/crossplatformdemo.js | 20 +++++------ .../microsoft/codepush/react/CodePush.java | 36 ++++++++++--------- .../codepush/react/CodePushDialog.java | 2 -- .../react/CodePushMalformedDataException.java | 12 ------- .../codepush/react/CodePushPackage.java | 26 +++++++------- .../react/CodePushUnknownException.java | 12 ------- .../codepush/react/CodePushUpdateUtils.java | 4 +-- .../codepush/react/CodePushUtils.java | 23 +++++++----- .../microsoft/codepush/react/FileUtils.java | 8 ++--- ios/CodePush/CodePushConfig.m | 10 +++--- request-fetch-adapter.js | 2 +- 11 files changed, 70 insertions(+), 85 deletions(-) delete mode 100644 android/app/src/main/java/com/microsoft/codepush/react/CodePushMalformedDataException.java delete mode 100644 android/app/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java diff --git a/Examples/CodePushDemoApp/crossplatformdemo.js b/Examples/CodePushDemoApp/crossplatformdemo.js index d941af0..7111d1c 100644 --- a/Examples/CodePushDemoApp/crossplatformdemo.js +++ b/Examples/CodePushDemoApp/crossplatformdemo.js @@ -18,13 +18,13 @@ let CodePushDemoApp = React.createClass({ let self = this; try { return await CodePush.sync( - { + { updateDialog: true, installMode: CodePush.InstallMode.ON_NEXT_RESUME - }, + }, (syncStatus) => { switch(syncStatus) { - case CodePush.SyncStatus.CHECKING_FOR_UPDATE: + case CodePush.SyncStatus.CHECKING_FOR_UPDATE: self.setState({ syncMessage: "Checking for update." }); @@ -80,36 +80,36 @@ let CodePushDemoApp = React.createClass({ CodePush.log(error); } }, - + componentDidMount() { CodePush.notifyApplicationReady(); }, - + getInitialState() { return { }; }, - + render() { let syncView, syncButton, progressView; - + if (this.state.syncMessage) { syncView = ( {this.state.syncMessage} ); } else { - syncButton = ( + syncButton = ( ); } - + if (this.state.progress) { progressView = ( {this.state.progress.receivedBytes} of {this.state.progress.totalBytes} bytes received ); } - + return ( 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 2b23fec..551d0c4 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 @@ -99,7 +99,7 @@ public class CodePush implements ReactPackage { appVersion = pInfo.versionName; buildVersion = pInfo.versionCode; } catch (PackageManager.NameNotFoundException e) { - throw new CodePushUnknownException("Unable to get package info for " + applicationContext.getPackageName(), e); + CodePushUtils.logException("Unable to get package info for " + applicationContext.getPackageName(), e); } if (currentInstance != null) { @@ -109,6 +109,7 @@ public class CodePush implements ReactPackage { currentInstance = this; clearDebugCacheIfNeeded(); + initializeUpdateAfterRestart(); } private void clearDebugCacheIfNeeded() { @@ -129,13 +130,14 @@ public class CodePush implements ReactPackage { ZipEntry classesDexEntry = applicationFile.getEntry(RESOURCES_BUNDLE); return classesDexEntry.getTime(); } catch (PackageManager.NameNotFoundException | IOException e) { - throw new CodePushUnknownException("Error in getting file information about compiled resources", e); + CodePushUtils.logException("Error in getting file information about compiled resources", e); + return -1; } finally { if (applicationFile != null) { try { applicationFile.close(); } catch (IOException e) { - throw new CodePushUnknownException("Error in closing application file.", e); + CodePushUtils.logException("Error in closing application file.", e); } } } @@ -193,7 +195,8 @@ public class CodePush implements ReactPackage { return binaryJsBundleUrl; } } catch (NumberFormatException e) { - throw new CodePushUnknownException("Error in reading binary modified date from package metadata", e); + CodePushUtils.logException("Error in closing application file.", e); + return binaryJsBundleUrl; } } @@ -251,7 +254,7 @@ public class CodePush implements ReactPackage { } } catch (JSONException e) { // Should not happen. - throw new CodePushUnknownException("Unable to read pending update metadata stored in SharedPreferences", e); + CodePushUtils.logException("Unable to read pending update metadata stored in SharedPreferences", e); } } } @@ -267,7 +270,7 @@ public class CodePush implements ReactPackage { return true; } } catch (JSONException e) { - throw new CodePushUnknownException("Unable to read failedUpdates data stored in SharedPreferences.", e); + CodePushUtils.logException("Unable to read failedUpdates data stored in SharedPreferences.", e); } } } @@ -284,7 +287,8 @@ public class CodePush implements ReactPackage { (packageHash == null || pendingUpdate.getString(PENDING_UPDATE_HASH_KEY).equals(packageHash)); } catch (JSONException e) { - throw new CodePushUnknownException("Unable to read pending update metadata in isPendingUpdate.", e); + CodePushUtils.logException("Unable to read pending update metadata in isPendingUpdate.", e); + return false; } } @@ -316,8 +320,9 @@ public class CodePush implements ReactPackage { failedUpdates = new JSONArray(failedUpdatesString); } catch (JSONException e) { // Should not happen. - throw new CodePushMalformedDataException("Unable to parse failed updates information " + + CodePushUtils.logException("Unable to parse failed updates information " + failedUpdatesString + " stored in SharedPreferences", e); + failedUpdates = new JSONArray(); } } @@ -335,7 +340,7 @@ public class CodePush implements ReactPackage { settings.edit().putString(PENDING_UPDATE_KEY, pendingUpdate.toString()).commit(); } catch (JSONException e) { // Should not happen. - throw new CodePushUnknownException("Unable to save pending update.", e); + CodePushUtils.logException("Unable to save pending update.", e); } } @@ -382,11 +387,6 @@ public class CodePush implements ReactPackage { return "CodePush"; } - @Override - public void initialize() { - CodePush.this.initializeUpdateAfterRestart(); - } - private void loadBundleLegacy() { Intent intent = mainActivity.getIntent(); mainActivity.finish(); @@ -418,6 +418,7 @@ public class CodePush implements ReactPackage { public void run() { try { recreateMethod.invoke(instanceManager); + initializeUpdateAfterRestart(); } catch (Exception e) { // The recreation method threw an unknown exception @@ -558,7 +559,7 @@ public class CodePush implements ReactPackage { return null; } } catch (JSONException e) { - throw new CodePushUnknownException("Unable to read failed updates information stored in SharedPreferences.", e); + CodePushUtils.logException("Unable to read failed updates information stored in SharedPreferences.", e); } } } else if (didUpdate) { @@ -595,7 +596,8 @@ public class CodePush implements ReactPackage { String pendingHash = CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY); if (pendingHash == null) { - throw new CodePushUnknownException("Update package to be installed has no hash."); + CodePushUtils.log("Update package to be installed has no hash."); + return null; } else { savePendingUpdate(pendingHash, /* isLoading */false); } @@ -687,7 +689,7 @@ public class CodePush implements ReactPackage { try { codePushPackage.downloadAndReplaceCurrentBundle(remoteBundleUrl, assetsBundleFileName); } catch (IOException e) { - throw new CodePushUnknownException("Unable to replace current bundle", e); + CodePushUtils.logException("Unable to replace current bundle", e); } } } diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java index e8d19fb..a4d8fa3 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushDialog.java @@ -36,8 +36,6 @@ public class CodePushDialog extends ReactContextBaseJavaModule{ case DialogInterface.BUTTON_NEGATIVE: successCallback.invoke(1); break; - default: - throw new CodePushUnknownException("Unknown button ID pressed."); } } }; diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushMalformedDataException.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushMalformedDataException.java deleted file mode 100644 index 604a9d7..0000000 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushMalformedDataException.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.microsoft.codepush.react; - -import java.net.MalformedURLException; - -public class CodePushMalformedDataException extends RuntimeException { - public CodePushMalformedDataException(String path, Throwable cause) { - super("Unable to parse contents of " + path + ", the file may be corrupted.", cause); - } - public CodePushMalformedDataException(String url, MalformedURLException cause) { - super("The package has an invalid downloadUrl: " + url, cause); - } -} diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java index 6fefb5e..9b7d0c7 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/CodePushPackage.java @@ -71,7 +71,9 @@ public class CodePushPackage { try { return CodePushUtils.getWritableMapFromFile(statusFilePath); } catch (IOException e) { - throw new CodePushUnknownException("Error getting current package info" , e); + // Should not happen. + CodePushUtils.logException("Error getting current package info" , e); + return new WritableNativeMap(); } } @@ -79,7 +81,8 @@ public class CodePushPackage { try { CodePushUtils.writeReadableMapToFile(packageInfo, getStatusFilePath()); } catch (IOException e) { - throw new CodePushUnknownException("Error updating current package info" , e); + // Should not happen. + CodePushUtils.logException("Error updating current package info" , e); } } @@ -100,6 +103,10 @@ public class CodePushPackage { } WritableMap currentPackage = getCurrentPackage(); + if (currentPackage == null) { + return null; + } + String relativeBundlePath = CodePushUtils.tryGetString(currentPackage, RELATIVE_BUNDLE_PATH_KEY); if (relativeBundlePath == null) { return CodePushUtils.appendPathComponent(packageFolder, bundleFileName); @@ -205,12 +212,10 @@ public class CodePushPackage { } if (totalBytes != receivedBytes) { - throw new CodePushUnknownException("Received " + receivedBytes + " bytes, expected " + totalBytes); + CodePushUtils.log("Received " + receivedBytes + " bytes, expected " + totalBytes); } isZip = ByteBuffer.wrap(header).getInt() == 0x504b0304; - } catch (MalformedURLException e) { - throw new CodePushMalformedDataException(downloadUrlString, e); } finally { try { if (bout != null) bout.close(); @@ -218,7 +223,7 @@ public class CodePushPackage { if (bin != null) bin.close(); if (connection != null) connection.disconnect(); } catch (IOException e) { - throw new CodePushUnknownException("Error closing IO resources.", e); + CodePushUtils.logException("Error closing IO resources.", e); } } @@ -262,9 +267,8 @@ public class CodePushPackage { try { updatePackageJSON.put(RELATIVE_BUNDLE_PATH_KEY, relativeBundlePath); } catch (JSONException e) { - throw new CodePushUnknownException("Unable to set key " + - RELATIVE_BUNDLE_PATH_KEY + " to value " + relativeBundlePath + - " in update package.", e); + CodePushUtils.logException("Unable to set key " + RELATIVE_BUNDLE_PATH_KEY + + " to value " + relativeBundlePath + " in update package.", e); } updatePackage = CodePushUtils.convertJsonObjectToWritable(updatePackageJSON); @@ -327,8 +331,6 @@ public class CodePushPackage { while ((numBytesRead = bin.read(data, 0, DOWNLOAD_BUFFER_SIZE)) >= 0) { bout.write(data, 0, numBytesRead); } - } catch (MalformedURLException e) { - throw new CodePushMalformedDataException(remoteBundleUrl, e); } finally { try { if (bout != null) bout.close(); @@ -336,7 +338,7 @@ public class CodePushPackage { if (bin != null) bin.close(); if (connection != null) connection.disconnect(); } catch (IOException e) { - throw new CodePushUnknownException("Error closing IO resources.", e); + CodePushUtils.logException("Error closing IO resources.", e); } } } diff --git a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java b/android/app/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java deleted file mode 100644 index cb99f6c..0000000 --- a/android/app/src/main/java/com/microsoft/codepush/react/CodePushUnknownException.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.microsoft.codepush.react; - -class CodePushUnknownException extends RuntimeException { - - public CodePushUnknownException(String message, Throwable cause) { - super(message, cause); - } - - public CodePushUnknownException(String message) { - super(message); - } -} \ No newline at end of file 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 ee6d78d..4e9c84c 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 @@ -36,7 +36,7 @@ public class CodePushUpdateUtils { manifest.add(relativePath + ":" + computeHash(new FileInputStream(file))); } catch (FileNotFoundException e) { // Should not happen. - throw new CodePushUnknownException("Unable to compute hash of update contents.", e); + CodePushUtils.logException("Unable to compute hash of update contents.", e); } } } @@ -52,7 +52,7 @@ public class CodePushUpdateUtils { while (digestInputStream.read(byteBuffer) != -1); } catch (NoSuchAlgorithmException | IOException e) { // Should not happen. - throw new CodePushUnknownException("Unable to compute hash of update contents.", e); + CodePushUtils.logException("Unable to compute hash of update contents.", e); } finally { try { if (digestInputStream != null) digestInputStream.close(); 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 3f64aeb..780f1db 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 @@ -38,7 +38,7 @@ public class CodePushUtils { obj = jsonArr.get(i); } catch (JSONException jsonException) { // Should not happen. - throw new CodePushUnknownException(i + " should be within bounds of array " + jsonArr.toString(), jsonException); + CodePushUtils.logException(i + " should be within bounds of array " + jsonArr.toString(), jsonException); } if (obj instanceof JSONObject) @@ -56,7 +56,7 @@ public class CodePushUtils { else if (obj == null) arr.pushNull(); else - throw new CodePushUnknownException("Unrecognized object: " + obj); + CodePushUtils.log("Unrecognized object: " + obj); } return arr; @@ -72,7 +72,7 @@ public class CodePushUtils { obj = jsonObj.get(key); } catch (JSONException jsonException) { // Should not happen. - throw new CodePushUnknownException("Key " + key + " should exist in " + jsonObj.toString() + ".", jsonException); + CodePushUtils.logException("Key " + key + " should exist in " + jsonObj.toString() + ".", jsonException); } if (obj instanceof JSONObject) @@ -90,7 +90,7 @@ public class CodePushUtils { else if (obj == null) map.putNull(key); else - throw new CodePushUnknownException("Unrecognized object: " + obj); + CodePushUtils.log("Unrecognized object: " + obj); } return map; @@ -124,7 +124,7 @@ public class CodePushUtils { try { jsonArr.put(number.doubleValue()); } catch (JSONException jsonException) { - throw new CodePushUnknownException("Unable to put value " + arr.getDouble(i) + " in JSONArray"); + CodePushUtils.log("Unable to put value " + arr.getDouble(i) + " in JSONArray"); } } break; @@ -167,10 +167,10 @@ public class CodePushUtils { jsonObj.put(key, null); break; default: - throw new CodePushUnknownException("Unrecognized type: " + type + " of key: " + key); + CodePushUtils.log("Unrecognized type: " + type + " of key: " + key); } } catch (JSONException jsonException) { - throw new CodePushUnknownException("Error setting key: " + key + " in JSONObject", jsonException); + CodePushUtils.logException("Error setting key: " + key + " in JSONObject", jsonException); } } @@ -202,7 +202,9 @@ public class CodePushUtils { JSONObject json = new JSONObject(content); return convertJsonObjectToWritable(json); } catch (JSONException jsonException) { - throw new CodePushMalformedDataException(filePath, jsonException); + // Should not happen + CodePushUtils.logException(filePath, jsonException); + return null; } } @@ -214,6 +216,11 @@ public class CodePushUtils { log("Loading JS bundle from \"" + path + "\""); } + public static void logException(String message, Exception e) { + log(message); + e.printStackTrace(); + } + public static String tryGetString(ReadableMap map, String key) { try { return map.getString(key); diff --git a/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java b/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java index fcc8cf5..fe9767b 100644 --- a/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java +++ b/android/app/src/main/java/com/microsoft/codepush/react/FileUtils.java @@ -47,7 +47,7 @@ public class FileUtils { if (fromBufferedStream != null) fromBufferedStream.close(); if (destStream != null) destStream.close(); } catch (IOException e) { - throw new CodePushUnknownException("Error closing IO resources.", e); + CodePushUtils.logException("Error closing IO resources.", e); } } } @@ -110,8 +110,8 @@ public class FileUtils { File newFilePath = new File(newFolderPath, newFileName); if (!fileToMove.renameTo(newFilePath)) { - throw new CodePushUnknownException("Unable to move file from " + - fileToMove.getAbsolutePath() + " to " + newFilePath.getAbsolutePath() + "."); + CodePushUtils.log("Unable to move file from " + fileToMove.getAbsolutePath() + + " to " + newFilePath.getAbsolutePath() + "."); } } @@ -185,7 +185,7 @@ public class FileUtils { if (bufferedStream != null) bufferedStream.close(); if (fileStream != null) fileStream.close(); } catch (IOException e) { - throw new CodePushUnknownException("Error closing IO resources.", e); + CodePushUtils.logException("Error closing IO resources.", e); } } } diff --git a/ios/CodePush/CodePushConfig.m b/ios/CodePush/CodePushConfig.m index 9ea11ff..b84b757 100644 --- a/ios/CodePush/CodePushConfig.m +++ b/ios/CodePush/CodePushConfig.m @@ -27,12 +27,12 @@ static NSString * const ServerURLConfigKey = @"serverUrl"; { self = [super init]; NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; - + NSString *appVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"]; NSString *buildVersion = [infoDictionary objectForKey:(NSString *)kCFBundleVersionKey]; NSString *deploymentKey = [infoDictionary objectForKey:@"CodePushDeploymentKey"]; NSString *serverURL = [infoDictionary objectForKey:@"CodePushServerURL"]; - + NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSString *clientUniqueId = [userDefaults stringForKey:ClientUniqueIDConfigKey]; if (clientUniqueId == nil) { @@ -40,11 +40,11 @@ static NSString * const ServerURLConfigKey = @"serverUrl"; [userDefaults setObject:clientUniqueId forKey:ClientUniqueIDConfigKey]; [userDefaults synchronize]; } - + if (!serverURL) { serverURL = @"https://codepush.azurewebsites.net/"; } - + _configDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys: appVersion,AppVersionConfigKey, buildVersion,BuildVdersionConfigKey, @@ -52,7 +52,7 @@ static NSString * const ServerURLConfigKey = @"serverUrl"; clientUniqueId,ClientUniqueIDConfigKey, deploymentKey,DeploymentKeyConfigKey, nil]; - + return self; } diff --git a/request-fetch-adapter.js b/request-fetch-adapter.js index eb61622..16220db 100644 --- a/request-fetch-adapter.js +++ b/request-fetch-adapter.js @@ -21,7 +21,7 @@ module.exports = { headers: headers, body: requestBody }); - + const statusCode = response.status; const body = await response.text(); callback(null, { statusCode, body });