33import android .content .Context ;
44import android .content .SharedPreferences ;
55
6+ import com .facebook .react .bridge .ReadableMap ;
67import com .facebook .react .bridge .WritableMap ;
78import com .facebook .react .bridge .WritableNativeMap ;
89
10+ import org .json .JSONException ;
11+ import org .json .JSONObject ;
12+
913public class CodePushTelemetryManager {
1014
1115 private Context applicationContext ;
16+ private final String APP_VERSION_KEY = "appVersion" ;
1217 private final String CODE_PUSH_PREFERENCES ;
1318 private final String DEPLOYMENT_FAILED_STATUS = "DeploymentFailed" ;
1419 private final String DEPLOYMENT_KEY_KEY = "deploymentKey" ;
1520 private final String DEPLOYMENT_SUCCEEDED_STATUS = "DeploymentSucceeded" ;
1621 private final String LABEL_KEY = "label" ;
1722 private final String LAST_DEPLOYMENT_REPORT_KEY = "CODE_PUSH_LAST_DEPLOYMENT_REPORT" ;
23+ private final String PACKAGE_KEY = "package" ;
24+ private final String PREVIOUS_DEPLOYMENT_KEY_KEY = "previousDeploymentKey" ;
25+ private final String PREVIOUS_LABEL_OR_APP_VERSION_KEY = "previousLabelOrAppVersion" ;
26+ private final String RETRY_DEPLOYMENT_REPORT_KEY = "CODE_PUSH_RETRY_DEPLOYMENT_REPORT" ;
27+ private final String STATUS_KEY = "status" ;
1828
1929 public CodePushTelemetryManager (Context applicationContext , String codePushPreferencesKey ) {
2030 this .applicationContext = applicationContext ;
@@ -24,34 +34,51 @@ public CodePushTelemetryManager(Context applicationContext, String codePushPrefe
2434 public WritableMap getBinaryUpdateReport (String appVersion ) {
2535 String previousStatusReportIdentifier = this .getPreviousStatusReportIdentifier ();
2636 if (previousStatusReportIdentifier == null ) {
27- this .recordDeploymentStatusReported ( appVersion );
37+ this .clearRetryStatusReport ( );
2838 WritableNativeMap reportMap = new WritableNativeMap ();
29- reportMap .putString ("appVersion" , appVersion );
39+ reportMap .putString (APP_VERSION_KEY , appVersion );
3040 return reportMap ;
3141 } else if (!previousStatusReportIdentifier .equals (appVersion )) {
32- this .recordDeploymentStatusReported ( appVersion );
42+ this .clearRetryStatusReport ( );
3343 WritableNativeMap reportMap = new WritableNativeMap ();
3444 if (this .isStatusReportIdentifierCodePushLabel (previousStatusReportIdentifier )) {
3545 String previousDeploymentKey = this .getDeploymentKeyFromStatusReportIdentifier (previousStatusReportIdentifier );
3646 String previousLabel = this .getVersionLabelFromStatusReportIdentifier (previousStatusReportIdentifier );
37- reportMap .putString ("appVersion" , appVersion );
38- reportMap .putString ("previousDeploymentKey" , previousDeploymentKey );
39- reportMap .putString ("previousLabelOrAppVersion" , previousLabel );
47+ reportMap .putString (APP_VERSION_KEY , appVersion );
48+ reportMap .putString (PREVIOUS_DEPLOYMENT_KEY_KEY , previousDeploymentKey );
49+ reportMap .putString (PREVIOUS_LABEL_OR_APP_VERSION_KEY , previousLabel );
4050 } else {
4151 // Previous status report was with a binary app version.
42- reportMap .putString ("appVersion" , appVersion );
43- reportMap .putString ("previousLabelOrAppVersion" , previousStatusReportIdentifier );
52+ reportMap .putString (APP_VERSION_KEY , appVersion );
53+ reportMap .putString (PREVIOUS_LABEL_OR_APP_VERSION_KEY , previousStatusReportIdentifier );
4454 }
4555 return reportMap ;
4656 }
4757
4858 return null ;
4959 }
5060
61+ public WritableMap getRetryStatusReport () {
62+ SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
63+ String retryStatusReportString = settings .getString (RETRY_DEPLOYMENT_REPORT_KEY , null );
64+ if (retryStatusReportString != null ) {
65+ clearRetryStatusReport ();
66+ try {
67+ JSONObject retryStatusReport = new JSONObject (retryStatusReportString );
68+ return CodePushUtils .convertJsonObjectToWritable (retryStatusReport );
69+ } catch (JSONException e ) {
70+ e .printStackTrace ();
71+ return null ;
72+ }
73+ }
74+
75+ return null ;
76+ }
77+
5178 public WritableMap getRollbackReport (WritableMap lastFailedPackage ) {
5279 WritableNativeMap reportMap = new WritableNativeMap ();
53- reportMap .putMap ("package" , lastFailedPackage );
54- reportMap .putString ("status" , DEPLOYMENT_FAILED_STATUS );
80+ reportMap .putMap (PACKAGE_KEY , lastFailedPackage );
81+ reportMap .putString (STATUS_KEY , DEPLOYMENT_FAILED_STATUS );
5582 return reportMap ;
5683 }
5784
@@ -60,28 +87,28 @@ public WritableMap getUpdateReport(WritableMap currentPackage) {
6087 String previousStatusReportIdentifier = this .getPreviousStatusReportIdentifier ();
6188 if (currentPackageIdentifier != null ) {
6289 if (previousStatusReportIdentifier == null ) {
63- this .recordDeploymentStatusReported ( currentPackageIdentifier );
90+ this .clearRetryStatusReport ( );
6491 WritableNativeMap reportMap = new WritableNativeMap ();
65- reportMap .putMap ("package" , currentPackage );
66- reportMap .putString ("status" , DEPLOYMENT_SUCCEEDED_STATUS );
92+ reportMap .putMap (PACKAGE_KEY , currentPackage );
93+ reportMap .putString (STATUS_KEY , DEPLOYMENT_SUCCEEDED_STATUS );
6794 return reportMap ;
6895 } else if (!previousStatusReportIdentifier .equals (currentPackageIdentifier )) {
69- this .recordDeploymentStatusReported ( currentPackageIdentifier );
96+ this .clearRetryStatusReport ( );
7097 if (this .isStatusReportIdentifierCodePushLabel (previousStatusReportIdentifier )) {
7198 String previousDeploymentKey = this .getDeploymentKeyFromStatusReportIdentifier (previousStatusReportIdentifier );
7299 String previousLabel = this .getVersionLabelFromStatusReportIdentifier (previousStatusReportIdentifier );
73100 WritableNativeMap reportMap = new WritableNativeMap ();
74- reportMap .putMap ("package" , currentPackage );
75- reportMap .putString ("status" , DEPLOYMENT_SUCCEEDED_STATUS );
76- reportMap .putString ("previousDeploymentKey" , previousDeploymentKey );
77- reportMap .putString ("previousLabelOrAppVersion" , previousLabel );
101+ reportMap .putMap (PACKAGE_KEY , currentPackage );
102+ reportMap .putString (STATUS_KEY , DEPLOYMENT_SUCCEEDED_STATUS );
103+ reportMap .putString (PREVIOUS_DEPLOYMENT_KEY_KEY , previousDeploymentKey );
104+ reportMap .putString (PREVIOUS_LABEL_OR_APP_VERSION_KEY , previousLabel );
78105 return reportMap ;
79106 } else {
80107 // Previous status report was with a binary app version.
81108 WritableNativeMap reportMap = new WritableNativeMap ();
82- reportMap .putMap ("package" , currentPackage );
83- reportMap .putString ("status" , DEPLOYMENT_SUCCEEDED_STATUS );
84- reportMap .putString ("previousLabelOrAppVersion" , previousStatusReportIdentifier );
109+ reportMap .putMap (PACKAGE_KEY , currentPackage );
110+ reportMap .putString (STATUS_KEY , DEPLOYMENT_SUCCEEDED_STATUS );
111+ reportMap .putString (PREVIOUS_LABEL_OR_APP_VERSION_KEY , previousStatusReportIdentifier );
85112 return reportMap ;
86113 }
87114 }
@@ -90,6 +117,26 @@ public WritableMap getUpdateReport(WritableMap currentPackage) {
90117 return null ;
91118 }
92119
120+ public void recordStatusReported (ReadableMap statusReport ) {
121+ if (statusReport .hasKey (APP_VERSION_KEY )) {
122+ saveStatusReportedForIdentifier (statusReport .getString (APP_VERSION_KEY ));
123+ } else if (statusReport .hasKey (PACKAGE_KEY )) {
124+ String packageIdentifier = getPackageStatusReportIdentifier (statusReport .getMap (PACKAGE_KEY ));
125+ saveStatusReportedForIdentifier (packageIdentifier );
126+ }
127+ }
128+
129+ public void saveStatusReportForRetry (ReadableMap statusReport ) {
130+ SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
131+ JSONObject statusReportJSON = CodePushUtils .convertReadableToJsonObject (statusReport );
132+ settings .edit ().putString (RETRY_DEPLOYMENT_REPORT_KEY , statusReportJSON .toString ()).commit ();
133+ }
134+
135+ private void clearRetryStatusReport () {
136+ SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
137+ settings .edit ().remove (RETRY_DEPLOYMENT_REPORT_KEY ).commit ();
138+ }
139+
93140 private String getDeploymentKeyFromStatusReportIdentifier (String statusReportIdentifier ) {
94141 String [] parsedIdentifier = statusReportIdentifier .split (":" );
95142 if (parsedIdentifier .length > 0 ) {
@@ -99,7 +146,7 @@ private String getDeploymentKeyFromStatusReportIdentifier(String statusReportIde
99146 }
100147 }
101148
102- private String getPackageStatusReportIdentifier (WritableMap updatePackage ) {
149+ private String getPackageStatusReportIdentifier (ReadableMap updatePackage ) {
103150 // Because deploymentKeys can be dynamically switched, we use a
104151 // combination of the deploymentKey and label as the packageIdentifier.
105152 String deploymentKey = CodePushUtils .tryGetString (updatePackage , DEPLOYMENT_KEY_KEY );
@@ -129,7 +176,7 @@ private boolean isStatusReportIdentifierCodePushLabel(String statusReportIdentif
129176 return statusReportIdentifier != null && statusReportIdentifier .contains (":" );
130177 }
131178
132- private void recordDeploymentStatusReported (String appVersionOrPackageIdentifier ) {
179+ private void saveStatusReportedForIdentifier (String appVersionOrPackageIdentifier ) {
133180 SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
134181 settings .edit ().putString (LAST_DEPLOYMENT_REPORT_KEY , appVersionOrPackageIdentifier ).commit ();
135182 }
0 commit comments