@@ -61,7 +61,7 @@ public class CodePush {
61
61
private final String PENDING_UPDATE_IS_LOADING_KEY = "isLoading" ;
62
62
private final String PENDING_UPDATE_KEY = "CODE_PUSH_PENDING_UPDATE" ;
63
63
private final String RESOURCES_BUNDLE = "resources.arsc" ;
64
- private final String STATUS_REPORTS_KEY = "CODE_PUSH_STATUS_REPORTS " ;
64
+ private final String LAST_DEPLOYMENT_REPORT_KEY = "CODE_PUSH_LAST_DEPLOYMENT_REPORT " ;
65
65
66
66
// This needs to be kept in sync with https://github.com/facebook/react-native/blob/master/ReactAndroid/src/main/java/com/facebook/react/devsupport/DevSupportManager.java#L78
67
67
private final String REACT_DEV_BUNDLE_CACHE_FILE_NAME = "ReactNativeDevBundle.js" ;
@@ -153,10 +153,10 @@ public String getBundleUrl(String assetsBundleFileName) {
153
153
binaryModifiedDateDuringPackageInstall = Long .parseLong (binaryModifiedDateDuringPackageInstallString );
154
154
}
155
155
156
- String pacakgeAppVersion = CodePushUtils .tryGetString (packageMetadata , "appVersion" );
156
+ String packageAppVersion = CodePushUtils .tryGetString (packageMetadata , "appVersion" );
157
157
if (binaryModifiedDateDuringPackageInstall != null &&
158
158
binaryModifiedDateDuringPackageInstall == binaryResourcesModifiedTime &&
159
- (this .isUsingTestConfiguration () || this .appVersion .equals (pacakgeAppVersion ))) {
159
+ (this .isUsingTestConfiguration () || this .appVersion .equals (packageAppVersion ))) {
160
160
CodePushUtils .logBundleUrl (packageFilePath );
161
161
return packageFilePath ;
162
162
} else {
@@ -169,8 +169,6 @@ public String getBundleUrl(String assetsBundleFileName) {
169
169
CodePushUtils .logBundleUrl (binaryJsBundleUrl );
170
170
return binaryJsBundleUrl ;
171
171
}
172
- } catch (IOException e ) {
173
- throw new CodePushUnknownException ("Error in getting current package bundle path" , e );
174
172
} catch (NumberFormatException e ) {
175
173
throw new CodePushUnknownException ("Error in reading binary modified date from package metadata" , e );
176
174
}
@@ -263,17 +261,11 @@ private void initializeUpdateAfterRestart() {
263
261
264
262
private boolean isDeploymentStatusNotYetReported (String appVersionOrPackageIdentifier ) {
265
263
SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
266
- String sentStatusReportsString = settings .getString (STATUS_REPORTS_KEY , null );
267
- if (sentStatusReportsString == null ) {
264
+ String lastDeploymentReportIdentifier = settings .getString (LAST_DEPLOYMENT_REPORT_KEY , null );
265
+ if (lastDeploymentReportIdentifier == null ) {
268
266
return true ;
269
267
} else {
270
- try {
271
- JSONObject sentStatusReports = new JSONObject (sentStatusReportsString );
272
- return !sentStatusReports .has (appVersionOrPackageIdentifier );
273
- } catch (JSONException e ) {
274
- throw new CodePushUnknownException ("Unable to parse sent status reports information " +
275
- sentStatusReportsString + " stored in SharedPreferences." , e );
276
- }
268
+ return !lastDeploymentReportIdentifier .equals (appVersionOrPackageIdentifier );
277
269
}
278
270
}
279
271
@@ -311,22 +303,9 @@ private boolean isPendingUpdate(String packageHash) {
311
303
}
312
304
}
313
305
314
- private void recordDeploymentStatusReported (String appVersionOrPackageIdentifier , String status ) {
306
+ private void recordDeploymentStatusReported (String appVersionOrPackageIdentifier ) {
315
307
SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
316
- String sentStatusReportsString = settings .getString (STATUS_REPORTS_KEY , null );
317
- JSONObject sentStatusReports ;
318
- try {
319
- if (sentStatusReportsString == null ) {
320
- sentStatusReports = new JSONObject ();
321
- } else {
322
- sentStatusReports = new JSONObject (sentStatusReportsString );
323
- }
324
-
325
- sentStatusReports .put (appVersionOrPackageIdentifier , status );
326
- settings .edit ().putString (STATUS_REPORTS_KEY , sentStatusReports .toString ()).commit ();
327
- } catch (JSONException e ) {
328
- throw new CodePushUnknownException ("Unable to save new entry in SharedPreferences under " + STATUS_REPORTS_KEY + "." , e );
329
- }
308
+ settings .edit ().putString (LAST_DEPLOYMENT_REPORT_KEY , appVersionOrPackageIdentifier ).commit ();
330
309
}
331
310
332
311
private void removeFailedUpdates () {
@@ -340,19 +319,9 @@ private void removePendingUpdate() {
340
319
}
341
320
342
321
private void rollbackPackage () {
343
- try {
344
- WritableMap failedPackage = codePushPackage .getCurrentPackage ();
345
- saveFailedUpdate (failedPackage );
346
- } catch (IOException e ) {
347
- throw new CodePushUnknownException ("Attempted a rollback without having a current downloaded package" , e );
348
- }
349
-
350
- try {
351
- codePushPackage .rollbackPackage ();
352
- } catch (IOException e ) {
353
- throw new CodePushUnknownException ("Error in rolling back package" , e );
354
- }
355
-
322
+ WritableMap failedPackage = codePushPackage .getCurrentPackage ();
323
+ saveFailedUpdate (failedPackage );
324
+ codePushPackage .rollbackPackage ();
356
325
removePendingUpdate ();
357
326
}
358
327
@@ -464,23 +433,17 @@ public void getCurrentPackage(final Promise promise) {
464
433
AsyncTask asyncTask = new AsyncTask () {
465
434
@ Override
466
435
protected Void doInBackground (Object ... params ) {
467
- try {
468
- WritableMap currentPackage = codePushPackage .getCurrentPackage ();
436
+ WritableMap currentPackage = codePushPackage .getCurrentPackage ();
469
437
470
- Boolean isPendingUpdate = false ;
438
+ Boolean isPendingUpdate = false ;
471
439
472
- if (currentPackage .hasKey (codePushPackage .PACKAGE_HASH_KEY )) {
473
- String currentHash = currentPackage .getString (codePushPackage .PACKAGE_HASH_KEY );
474
- isPendingUpdate = CodePush .this .isPendingUpdate (currentHash );
475
- }
476
-
477
- currentPackage .putBoolean ("isPending" , isPendingUpdate );
478
- promise .resolve (currentPackage );
479
- } catch (IOException e ) {
480
- e .printStackTrace ();
481
- promise .reject (e .getMessage ());
440
+ if (currentPackage .hasKey (codePushPackage .PACKAGE_HASH_KEY )) {
441
+ String currentHash = currentPackage .getString (codePushPackage .PACKAGE_HASH_KEY );
442
+ isPendingUpdate = CodePush .this .isPendingUpdate (currentHash );
482
443
}
483
-
444
+
445
+ currentPackage .putBoolean ("isPending" , isPendingUpdate );
446
+ promise .resolve (currentPackage );
484
447
return null ;
485
448
}
486
449
};
@@ -490,16 +453,16 @@ protected Void doInBackground(Object... params) {
490
453
491
454
@ ReactMethod
492
455
public void getNewStatusReport (Promise promise ) {
493
- // Check if there was a rollback that was not yet reported
494
456
if (didRollback ) {
457
+ // Check if there was a rollback that was not yet reported
495
458
JSONArray failedUpdates = getFailedUpdates ();
496
459
if (failedUpdates != null && failedUpdates .length () > 0 ) {
497
460
try {
498
461
JSONObject lastFailedPackageJSON = failedUpdates .getJSONObject (failedUpdates .length () - 1 );
499
462
WritableMap lastFailedPackage = CodePushUtils .convertJsonObjectToWriteable (lastFailedPackageJSON );
500
463
String lastFailedPackageIdentifier = getPackageStatusReportIdentifier (lastFailedPackage );
501
464
if (lastFailedPackage != null && isDeploymentStatusNotYetReported (lastFailedPackageIdentifier )) {
502
- recordDeploymentStatusReported (lastFailedPackageIdentifier , DEPLOYMENT_FAILED_STATUS );
465
+ recordDeploymentStatusReported (lastFailedPackageIdentifier );
503
466
WritableNativeMap reportMap = new WritableNativeMap ();
504
467
reportMap .putMap ("package" , lastFailedPackage );
505
468
reportMap .putString ("status" , DEPLOYMENT_FAILED_STATUS );
@@ -510,36 +473,34 @@ public void getNewStatusReport(Promise promise) {
510
473
throw new CodePushUnknownException ("Unable to read failed updates information stored in SharedPreferences." , e );
511
474
}
512
475
}
513
- }
514
-
515
- // Check if the current CodePush package has been reported
516
- if (didUpdate ) {
517
- try {
518
- WritableMap currentPackage = codePushPackage .getCurrentPackage ();
519
- if (currentPackage != null ) {
520
- String currentPackageIdentifier = getPackageStatusReportIdentifier (currentPackage );
521
- if (currentPackageIdentifier != null && isDeploymentStatusNotYetReported (currentPackageIdentifier )) {
522
- recordDeploymentStatusReported (currentPackageIdentifier , DEPLOYMENT_SUCCEEDED_STATUS );
523
- WritableNativeMap reportMap = new WritableNativeMap ();
524
- reportMap .putMap ("package" , currentPackage );
525
- reportMap .putString ("status" , DEPLOYMENT_SUCCEEDED_STATUS );
526
- promise .resolve (reportMap );
527
- return ;
528
- }
476
+ } else if (didUpdate ) {
477
+ // Check if the current CodePush package has been reported
478
+ WritableMap currentPackage = codePushPackage .getCurrentPackage ();
479
+ if (currentPackage != null ) {
480
+ String currentPackageIdentifier = getPackageStatusReportIdentifier (currentPackage );
481
+ if (currentPackageIdentifier != null && isDeploymentStatusNotYetReported (currentPackageIdentifier )) {
482
+ recordDeploymentStatusReported (currentPackageIdentifier );
483
+ WritableNativeMap reportMap = new WritableNativeMap ();
484
+ reportMap .putMap ("package" , currentPackage );
485
+ reportMap .putString ("status" , DEPLOYMENT_SUCCEEDED_STATUS );
486
+ promise .resolve (reportMap );
487
+ return ;
488
+ }
489
+ }
490
+ } else {
491
+ String currentPackageHash = null ;
492
+ currentPackageHash = codePushPackage .getCurrentPackageHash ();
493
+ if (currentPackageHash == null ) {
494
+ // Check if the current appVersion has been reported.
495
+ String binaryIdentifier = "" + getBinaryResourcesModifiedTime ();
496
+ if (isDeploymentStatusNotYetReported (binaryIdentifier )) {
497
+ recordDeploymentStatusReported (binaryIdentifier );
498
+ WritableNativeMap reportMap = new WritableNativeMap ();
499
+ reportMap .putString ("appVersion" , appVersion );
500
+ promise .resolve (reportMap );
501
+ return ;
529
502
}
530
- } catch (IOException e ) {
531
- // If didUpdate is true, there should be a current package, so this should not happen.
532
- throw new CodePushUnknownException ("Error getting current package after an update." , e );
533
503
}
534
- }
535
-
536
- // Check if the current appVersion has been reported.
537
- if (isDeploymentStatusNotYetReported (appVersion )) {
538
- recordDeploymentStatusReported (appVersion , DEPLOYMENT_SUCCEEDED_STATUS );
539
- WritableNativeMap reportMap = new WritableNativeMap ();
540
- reportMap .putString ("appVersion" , appVersion );
541
- promise .resolve (reportMap );
542
- return ;
543
504
}
544
505
545
506
promise .resolve ("" );
@@ -600,16 +561,11 @@ public void isFailedUpdate(String packageHash, Promise promise) {
600
561
601
562
@ ReactMethod
602
563
public void isFirstRun (String packageHash , Promise promise ) {
603
- try {
604
- boolean isFirstRun = didUpdate
605
- && packageHash != null
606
- && packageHash .length () > 0
607
- && packageHash .equals (codePushPackage .getCurrentPackageHash ());
608
- promise .resolve (isFirstRun );
609
- } catch (IOException e ) {
610
- e .printStackTrace ();
611
- promise .reject (e .getMessage ());
612
- }
564
+ boolean isFirstRun = didUpdate
565
+ && packageHash != null
566
+ && packageHash .length () > 0
567
+ && packageHash .equals (codePushPackage .getCurrentPackageHash ());
568
+ promise .resolve (isFirstRun );
613
569
}
614
570
615
571
@ ReactMethod
0 commit comments