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

Commit 7916856

Browse files
committed
fix rollback android
1 parent 3e29100 commit 7916856

File tree

11 files changed

+70
-85
lines changed

11 files changed

+70
-85
lines changed

Examples/CodePushDemoApp/crossplatformdemo.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ let CodePushDemoApp = React.createClass({
1818
let self = this;
1919
try {
2020
return await CodePush.sync(
21-
{
21+
{
2222
updateDialog: true,
2323
installMode: CodePush.InstallMode.ON_NEXT_RESUME
24-
},
24+
},
2525
(syncStatus) => {
2626
switch(syncStatus) {
27-
case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
27+
case CodePush.SyncStatus.CHECKING_FOR_UPDATE:
2828
self.setState({
2929
syncMessage: "Checking for update."
3030
});
@@ -80,36 +80,36 @@ let CodePushDemoApp = React.createClass({
8080
CodePush.log(error);
8181
}
8282
},
83-
83+
8484
componentDidMount() {
8585
CodePush.notifyApplicationReady();
8686
},
87-
87+
8888
getInitialState() {
8989
return { };
9090
},
91-
91+
9292
render() {
9393
let syncView, syncButton, progressView;
94-
94+
9595
if (this.state.syncMessage) {
9696
syncView = (
9797
<Text style={styles.messages}>{this.state.syncMessage}</Text>
9898
);
9999
} else {
100-
syncButton = (
100+
syncButton = (
101101
<Button style={{color: 'green'}} onPress={this.sync}>
102102
Start Sync!
103103
</Button>
104104
);
105105
}
106-
106+
107107
if (this.state.progress) {
108108
progressView = (
109109
<Text style={styles.messages}>{this.state.progress.receivedBytes} of {this.state.progress.totalBytes} bytes received</Text>
110110
);
111111
}
112-
112+
113113
return (
114114
<View style={styles.container}>
115115
<Text style={styles.welcome}>

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

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public CodePush(String deploymentKey, Activity mainActivity, boolean isDebugMode
9999
appVersion = pInfo.versionName;
100100
buildVersion = pInfo.versionCode;
101101
} catch (PackageManager.NameNotFoundException e) {
102-
throw new CodePushUnknownException("Unable to get package info for " + applicationContext.getPackageName(), e);
102+
CodePushUtils.logException("Unable to get package info for " + applicationContext.getPackageName(), e);
103103
}
104104

105105
if (currentInstance != null) {
@@ -109,6 +109,7 @@ public CodePush(String deploymentKey, Activity mainActivity, boolean isDebugMode
109109
currentInstance = this;
110110

111111
clearDebugCacheIfNeeded();
112+
initializeUpdateAfterRestart();
112113
}
113114

114115
private void clearDebugCacheIfNeeded() {
@@ -129,13 +130,14 @@ private long getBinaryResourcesModifiedTime() {
129130
ZipEntry classesDexEntry = applicationFile.getEntry(RESOURCES_BUNDLE);
130131
return classesDexEntry.getTime();
131132
} catch (PackageManager.NameNotFoundException | IOException e) {
132-
throw new CodePushUnknownException("Error in getting file information about compiled resources", e);
133+
CodePushUtils.logException("Error in getting file information about compiled resources", e);
134+
return -1;
133135
} finally {
134136
if (applicationFile != null) {
135137
try {
136138
applicationFile.close();
137139
} catch (IOException e) {
138-
throw new CodePushUnknownException("Error in closing application file.", e);
140+
CodePushUtils.logException("Error in closing application file.", e);
139141
}
140142
}
141143
}
@@ -193,7 +195,8 @@ public String getBundleUrlInternal(String assetsBundleFileName) {
193195
return binaryJsBundleUrl;
194196
}
195197
} catch (NumberFormatException e) {
196-
throw new CodePushUnknownException("Error in reading binary modified date from package metadata", e);
198+
CodePushUtils.logException("Error in closing application file.", e);
199+
return binaryJsBundleUrl;
197200
}
198201
}
199202

@@ -251,7 +254,7 @@ private void initializeUpdateAfterRestart() {
251254
}
252255
} catch (JSONException e) {
253256
// Should not happen.
254-
throw new CodePushUnknownException("Unable to read pending update metadata stored in SharedPreferences", e);
257+
CodePushUtils.logException("Unable to read pending update metadata stored in SharedPreferences", e);
255258
}
256259
}
257260
}
@@ -267,7 +270,7 @@ private boolean isFailedHash(String packageHash) {
267270
return true;
268271
}
269272
} catch (JSONException e) {
270-
throw new CodePushUnknownException("Unable to read failedUpdates data stored in SharedPreferences.", e);
273+
CodePushUtils.logException("Unable to read failedUpdates data stored in SharedPreferences.", e);
271274
}
272275
}
273276
}
@@ -284,7 +287,8 @@ private boolean isPendingUpdate(String packageHash) {
284287
(packageHash == null || pendingUpdate.getString(PENDING_UPDATE_HASH_KEY).equals(packageHash));
285288
}
286289
catch (JSONException e) {
287-
throw new CodePushUnknownException("Unable to read pending update metadata in isPendingUpdate.", e);
290+
CodePushUtils.logException("Unable to read pending update metadata in isPendingUpdate.", e);
291+
return false;
288292
}
289293
}
290294

@@ -316,8 +320,9 @@ private void saveFailedUpdate(ReadableMap failedPackage) {
316320
failedUpdates = new JSONArray(failedUpdatesString);
317321
} catch (JSONException e) {
318322
// Should not happen.
319-
throw new CodePushMalformedDataException("Unable to parse failed updates information " +
323+
CodePushUtils.logException("Unable to parse failed updates information " +
320324
failedUpdatesString + " stored in SharedPreferences", e);
325+
failedUpdates = new JSONArray();
321326
}
322327
}
323328

@@ -335,7 +340,7 @@ private void savePendingUpdate(String packageHash, boolean isLoading) {
335340
settings.edit().putString(PENDING_UPDATE_KEY, pendingUpdate.toString()).commit();
336341
} catch (JSONException e) {
337342
// Should not happen.
338-
throw new CodePushUnknownException("Unable to save pending update.", e);
343+
CodePushUtils.logException("Unable to save pending update.", e);
339344
}
340345
}
341346

@@ -382,11 +387,6 @@ public String getName() {
382387
return "CodePush";
383388
}
384389

385-
@Override
386-
public void initialize() {
387-
CodePush.this.initializeUpdateAfterRestart();
388-
}
389-
390390
private void loadBundleLegacy() {
391391
Intent intent = mainActivity.getIntent();
392392
mainActivity.finish();
@@ -418,6 +418,7 @@ private void loadBundle() {
418418
public void run() {
419419
try {
420420
recreateMethod.invoke(instanceManager);
421+
initializeUpdateAfterRestart();
421422
}
422423
catch (Exception e) {
423424
// The recreation method threw an unknown exception
@@ -558,7 +559,7 @@ protected Void doInBackground(Void... params) {
558559
return null;
559560
}
560561
} catch (JSONException e) {
561-
throw new CodePushUnknownException("Unable to read failed updates information stored in SharedPreferences.", e);
562+
CodePushUtils.logException("Unable to read failed updates information stored in SharedPreferences.", e);
562563
}
563564
}
564565
} else if (didUpdate) {
@@ -595,7 +596,8 @@ protected Void doInBackground(Void... params) {
595596

596597
String pendingHash = CodePushUtils.tryGetString(updatePackage, PACKAGE_HASH_KEY);
597598
if (pendingHash == null) {
598-
throw new CodePushUnknownException("Update package to be installed has no hash.");
599+
CodePushUtils.log("Update package to be installed has no hash.");
600+
return null;
599601
} else {
600602
savePendingUpdate(pendingHash, /* isLoading */false);
601603
}
@@ -687,7 +689,7 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl) {
687689
try {
688690
codePushPackage.downloadAndReplaceCurrentBundle(remoteBundleUrl, assetsBundleFileName);
689691
} catch (IOException e) {
690-
throw new CodePushUnknownException("Unable to replace current bundle", e);
692+
CodePushUtils.logException("Unable to replace current bundle", e);
691693
}
692694
}
693695
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ public void onClick(DialogInterface dialog, int which) {
3636
case DialogInterface.BUTTON_NEGATIVE:
3737
successCallback.invoke(1);
3838
break;
39-
default:
40-
throw new CodePushUnknownException("Unknown button ID pressed.");
4139
}
4240
}
4341
};

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,18 @@ public WritableMap getCurrentPackageInfo() {
7171
try {
7272
return CodePushUtils.getWritableMapFromFile(statusFilePath);
7373
} catch (IOException e) {
74-
throw new CodePushUnknownException("Error getting current package info" , e);
74+
// Should not happen.
75+
CodePushUtils.logException("Error getting current package info" , e);
76+
return new WritableNativeMap();
7577
}
7678
}
7779

7880
public void updateCurrentPackageInfo(ReadableMap packageInfo) {
7981
try {
8082
CodePushUtils.writeReadableMapToFile(packageInfo, getStatusFilePath());
8183
} catch (IOException e) {
82-
throw new CodePushUnknownException("Error updating current package info" , e);
84+
// Should not happen.
85+
CodePushUtils.logException("Error updating current package info" , e);
8386
}
8487
}
8588

@@ -100,6 +103,10 @@ public String getCurrentPackageBundlePath(String bundleFileName) {
100103
}
101104

102105
WritableMap currentPackage = getCurrentPackage();
106+
if (currentPackage == null) {
107+
return null;
108+
}
109+
103110
String relativeBundlePath = CodePushUtils.tryGetString(currentPackage, RELATIVE_BUNDLE_PATH_KEY);
104111
if (relativeBundlePath == null) {
105112
return CodePushUtils.appendPathComponent(packageFolder, bundleFileName);
@@ -205,20 +212,18 @@ public void downloadPackage(ReadableMap updatePackage, String expectedBundleFile
205212
}
206213

207214
if (totalBytes != receivedBytes) {
208-
throw new CodePushUnknownException("Received " + receivedBytes + " bytes, expected " + totalBytes);
215+
CodePushUtils.log("Received " + receivedBytes + " bytes, expected " + totalBytes);
209216
}
210217

211218
isZip = ByteBuffer.wrap(header).getInt() == 0x504b0304;
212-
} catch (MalformedURLException e) {
213-
throw new CodePushMalformedDataException(downloadUrlString, e);
214219
} finally {
215220
try {
216221
if (bout != null) bout.close();
217222
if (fos != null) fos.close();
218223
if (bin != null) bin.close();
219224
if (connection != null) connection.disconnect();
220225
} catch (IOException e) {
221-
throw new CodePushUnknownException("Error closing IO resources.", e);
226+
CodePushUtils.logException("Error closing IO resources.", e);
222227
}
223228
}
224229

@@ -262,9 +267,8 @@ public void downloadPackage(ReadableMap updatePackage, String expectedBundleFile
262267
try {
263268
updatePackageJSON.put(RELATIVE_BUNDLE_PATH_KEY, relativeBundlePath);
264269
} catch (JSONException e) {
265-
throw new CodePushUnknownException("Unable to set key " +
266-
RELATIVE_BUNDLE_PATH_KEY + " to value " + relativeBundlePath +
267-
" in update package.", e);
270+
CodePushUtils.logException("Unable to set key " + RELATIVE_BUNDLE_PATH_KEY +
271+
" to value " + relativeBundlePath + " in update package.", e);
268272
}
269273

270274
updatePackage = CodePushUtils.convertJsonObjectToWritable(updatePackageJSON);
@@ -327,16 +331,14 @@ public void downloadAndReplaceCurrentBundle(String remoteBundleUrl, String bundl
327331
while ((numBytesRead = bin.read(data, 0, DOWNLOAD_BUFFER_SIZE)) >= 0) {
328332
bout.write(data, 0, numBytesRead);
329333
}
330-
} catch (MalformedURLException e) {
331-
throw new CodePushMalformedDataException(remoteBundleUrl, e);
332334
} finally {
333335
try {
334336
if (bout != null) bout.close();
335337
if (fos != null) fos.close();
336338
if (bin != null) bin.close();
337339
if (connection != null) connection.disconnect();
338340
} catch (IOException e) {
339-
throw new CodePushUnknownException("Error closing IO resources.", e);
341+
CodePushUtils.logException("Error closing IO resources.", e);
340342
}
341343
}
342344
}

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private static void addContentsOfFolderToManifest(String folderPath, String path
3636
manifest.add(relativePath + ":" + computeHash(new FileInputStream(file)));
3737
} catch (FileNotFoundException e) {
3838
// Should not happen.
39-
throw new CodePushUnknownException("Unable to compute hash of update contents.", e);
39+
CodePushUtils.logException("Unable to compute hash of update contents.", e);
4040
}
4141
}
4242
}
@@ -52,7 +52,7 @@ private static String computeHash(InputStream dataStream) {
5252
while (digestInputStream.read(byteBuffer) != -1);
5353
} catch (NoSuchAlgorithmException | IOException e) {
5454
// Should not happen.
55-
throw new CodePushUnknownException("Unable to compute hash of update contents.", e);
55+
CodePushUtils.logException("Unable to compute hash of update contents.", e);
5656
} finally {
5757
try {
5858
if (digestInputStream != null) digestInputStream.close();

0 commit comments

Comments
 (0)