fix rollback android

This commit is contained in:
Geoffrey Goh
2016-04-25 15:04:19 -07:00
parent 3e29100c6c
commit 7916856aa4
11 changed files with 70 additions and 85 deletions

View File

@@ -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 = (
<Text style={styles.messages}>{this.state.syncMessage}</Text>
);
} else {
syncButton = (
syncButton = (
<Button style={{color: 'green'}} onPress={this.sync}>
Start Sync!
</Button>
);
}
if (this.state.progress) {
progressView = (
<Text style={styles.messages}>{this.state.progress.receivedBytes} of {this.state.progress.totalBytes} bytes received</Text>
);
}
return (
<View style={styles.container}>
<Text style={styles.welcome}>

View File

@@ -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);
}
}
}

View File

@@ -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.");
}
}
};

View File

@@ -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);
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}

View File

@@ -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();

View File

@@ -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);

View File

@@ -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);
}
}
}

View File

@@ -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;
}

View File

@@ -21,7 +21,7 @@ module.exports = {
headers: headers,
body: requestBody
});
const statusCode = response.status;
const body = await response.text();
callback(null, { statusCode, body });