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 ;
@@ -23,71 +33,103 @@ public CodePushTelemetryManager(Context applicationContext, String codePushPrefe
2333
2434 public WritableMap getBinaryUpdateReport (String appVersion ) {
2535 String previousStatusReportIdentifier = this .getPreviousStatusReportIdentifier ();
36+ WritableNativeMap reportMap = null ;
2637 if (previousStatusReportIdentifier == null ) {
27- this .recordDeploymentStatusReported (appVersion );
28- WritableNativeMap reportMap = new WritableNativeMap ();
29- reportMap .putString ("appVersion" , appVersion );
30- return reportMap ;
38+ this .clearRetryStatusReport ();
39+ reportMap = new WritableNativeMap ();
40+ reportMap .putString (APP_VERSION_KEY , appVersion );
3141 } else if (!previousStatusReportIdentifier .equals (appVersion )) {
32- this .recordDeploymentStatusReported ( appVersion );
33- WritableNativeMap reportMap = new WritableNativeMap ();
42+ this .clearRetryStatusReport ( );
43+ 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 );
54+ }
55+ }
56+
57+ return reportMap ;
58+ }
59+
60+ public WritableMap getRetryStatusReport () {
61+ SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
62+ String retryStatusReportString = settings .getString (RETRY_DEPLOYMENT_REPORT_KEY , null );
63+ if (retryStatusReportString != null ) {
64+ clearRetryStatusReport ();
65+ try {
66+ JSONObject retryStatusReport = new JSONObject (retryStatusReportString );
67+ return CodePushUtils .convertJsonObjectToWritable (retryStatusReport );
68+ } catch (JSONException e ) {
69+ e .printStackTrace ();
4470 }
45- return reportMap ;
4671 }
4772
4873 return null ;
4974 }
5075
5176 public WritableMap getRollbackReport (WritableMap lastFailedPackage ) {
5277 WritableNativeMap reportMap = new WritableNativeMap ();
53- reportMap .putMap ("package" , lastFailedPackage );
54- reportMap .putString ("status" , DEPLOYMENT_FAILED_STATUS );
78+ reportMap .putMap (PACKAGE_KEY , lastFailedPackage );
79+ reportMap .putString (STATUS_KEY , DEPLOYMENT_FAILED_STATUS );
5580 return reportMap ;
5681 }
5782
5883 public WritableMap getUpdateReport (WritableMap currentPackage ) {
5984 String currentPackageIdentifier = this .getPackageStatusReportIdentifier (currentPackage );
6085 String previousStatusReportIdentifier = this .getPreviousStatusReportIdentifier ();
86+ WritableNativeMap reportMap = null ;
6187 if (currentPackageIdentifier != null ) {
6288 if (previousStatusReportIdentifier == null ) {
63- this .recordDeploymentStatusReported (currentPackageIdentifier );
64- WritableNativeMap reportMap = new WritableNativeMap ();
65- reportMap .putMap ("package" , currentPackage );
66- reportMap .putString ("status" , DEPLOYMENT_SUCCEEDED_STATUS );
67- return reportMap ;
89+ this .clearRetryStatusReport ();
90+ reportMap = new WritableNativeMap ();
91+ reportMap .putMap (PACKAGE_KEY , currentPackage );
92+ reportMap .putString (STATUS_KEY , DEPLOYMENT_SUCCEEDED_STATUS );
6893 } else if (!previousStatusReportIdentifier .equals (currentPackageIdentifier )) {
69- this .recordDeploymentStatusReported (currentPackageIdentifier );
94+ this .clearRetryStatusReport ();
95+ reportMap = new WritableNativeMap ();
7096 if (this .isStatusReportIdentifierCodePushLabel (previousStatusReportIdentifier )) {
7197 String previousDeploymentKey = this .getDeploymentKeyFromStatusReportIdentifier (previousStatusReportIdentifier );
7298 String previousLabel = this .getVersionLabelFromStatusReportIdentifier (previousStatusReportIdentifier );
73- 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 );
78- return reportMap ;
99+ reportMap .putMap (PACKAGE_KEY , currentPackage );
100+ reportMap .putString (STATUS_KEY , DEPLOYMENT_SUCCEEDED_STATUS );
101+ reportMap .putString (PREVIOUS_DEPLOYMENT_KEY_KEY , previousDeploymentKey );
102+ reportMap .putString (PREVIOUS_LABEL_OR_APP_VERSION_KEY , previousLabel );
79103 } else {
80104 // Previous status report was with a binary app version.
81- WritableNativeMap reportMap = new WritableNativeMap ();
82- reportMap .putMap ("package" , currentPackage );
83- reportMap .putString ("status" , DEPLOYMENT_SUCCEEDED_STATUS );
84- reportMap .putString ("previousLabelOrAppVersion" , previousStatusReportIdentifier );
85- return reportMap ;
105+ reportMap .putMap (PACKAGE_KEY , currentPackage );
106+ reportMap .putString (STATUS_KEY , DEPLOYMENT_SUCCEEDED_STATUS );
107+ reportMap .putString (PREVIOUS_LABEL_OR_APP_VERSION_KEY , previousStatusReportIdentifier );
86108 }
87109 }
88110 }
89111
90- return null ;
112+ return reportMap ;
113+ }
114+
115+ public void recordStatusReported (ReadableMap statusReport ) {
116+ if (statusReport .hasKey (APP_VERSION_KEY )) {
117+ saveStatusReportedForIdentifier (statusReport .getString (APP_VERSION_KEY ));
118+ } else if (statusReport .hasKey (PACKAGE_KEY )) {
119+ String packageIdentifier = getPackageStatusReportIdentifier (statusReport .getMap (PACKAGE_KEY ));
120+ saveStatusReportedForIdentifier (packageIdentifier );
121+ }
122+ }
123+
124+ public void saveStatusReportForRetry (ReadableMap statusReport ) {
125+ SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
126+ JSONObject statusReportJSON = CodePushUtils .convertReadableToJsonObject (statusReport );
127+ settings .edit ().putString (RETRY_DEPLOYMENT_REPORT_KEY , statusReportJSON .toString ()).commit ();
128+ }
129+
130+ private void clearRetryStatusReport () {
131+ SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
132+ settings .edit ().remove (RETRY_DEPLOYMENT_REPORT_KEY ).commit ();
91133 }
92134
93135 private String getDeploymentKeyFromStatusReportIdentifier (String statusReportIdentifier ) {
@@ -99,7 +141,7 @@ private String getDeploymentKeyFromStatusReportIdentifier(String statusReportIde
99141 }
100142 }
101143
102- private String getPackageStatusReportIdentifier (WritableMap updatePackage ) {
144+ private String getPackageStatusReportIdentifier (ReadableMap updatePackage ) {
103145 // Because deploymentKeys can be dynamically switched, we use a
104146 // combination of the deploymentKey and label as the packageIdentifier.
105147 String deploymentKey = CodePushUtils .tryGetString (updatePackage , DEPLOYMENT_KEY_KEY );
@@ -129,7 +171,7 @@ private boolean isStatusReportIdentifierCodePushLabel(String statusReportIdentif
129171 return statusReportIdentifier != null && statusReportIdentifier .contains (":" );
130172 }
131173
132- private void recordDeploymentStatusReported (String appVersionOrPackageIdentifier ) {
174+ private void saveStatusReportedForIdentifier (String appVersionOrPackageIdentifier ) {
133175 SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
134176 settings .edit ().putString (LAST_DEPLOYMENT_REPORT_KEY , appVersionOrPackageIdentifier ).commit ();
135177 }
0 commit comments