3
3
import android .content .Context ;
4
4
import android .content .SharedPreferences ;
5
5
6
+ import com .facebook .react .bridge .ReadableMap ;
6
7
import com .facebook .react .bridge .WritableMap ;
7
8
import com .facebook .react .bridge .WritableNativeMap ;
8
9
10
+ import org .json .JSONException ;
11
+ import org .json .JSONObject ;
12
+
9
13
public class CodePushTelemetryManager {
10
14
11
15
private Context applicationContext ;
16
+ private final String APP_VERSION_KEY = "appVersion" ;
12
17
private final String CODE_PUSH_PREFERENCES ;
13
18
private final String DEPLOYMENT_FAILED_STATUS = "DeploymentFailed" ;
14
19
private final String DEPLOYMENT_KEY_KEY = "deploymentKey" ;
15
20
private final String DEPLOYMENT_SUCCEEDED_STATUS = "DeploymentSucceeded" ;
16
21
private final String LABEL_KEY = "label" ;
17
22
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" ;
18
28
19
29
public CodePushTelemetryManager (Context applicationContext , String codePushPreferencesKey ) {
20
30
this .applicationContext = applicationContext ;
@@ -24,34 +34,51 @@ public CodePushTelemetryManager(Context applicationContext, String codePushPrefe
24
34
public WritableMap getBinaryUpdateReport (String appVersion ) {
25
35
String previousStatusReportIdentifier = this .getPreviousStatusReportIdentifier ();
26
36
if (previousStatusReportIdentifier == null ) {
27
- this .recordDeploymentStatusReported ( appVersion );
37
+ this .clearRetryStatusReport ( );
28
38
WritableNativeMap reportMap = new WritableNativeMap ();
29
- reportMap .putString ("appVersion" , appVersion );
39
+ reportMap .putString (APP_VERSION_KEY , appVersion );
30
40
return reportMap ;
31
41
} else if (!previousStatusReportIdentifier .equals (appVersion )) {
32
- this .recordDeploymentStatusReported ( appVersion );
42
+ this .clearRetryStatusReport ( );
33
43
WritableNativeMap reportMap = new WritableNativeMap ();
34
44
if (this .isStatusReportIdentifierCodePushLabel (previousStatusReportIdentifier )) {
35
45
String previousDeploymentKey = this .getDeploymentKeyFromStatusReportIdentifier (previousStatusReportIdentifier );
36
46
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 );
40
50
} else {
41
51
// 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 );
44
54
}
45
55
return reportMap ;
46
56
}
47
57
48
58
return null ;
49
59
}
50
60
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
+
51
78
public WritableMap getRollbackReport (WritableMap lastFailedPackage ) {
52
79
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 );
55
82
return reportMap ;
56
83
}
57
84
@@ -60,28 +87,28 @@ public WritableMap getUpdateReport(WritableMap currentPackage) {
60
87
String previousStatusReportIdentifier = this .getPreviousStatusReportIdentifier ();
61
88
if (currentPackageIdentifier != null ) {
62
89
if (previousStatusReportIdentifier == null ) {
63
- this .recordDeploymentStatusReported ( currentPackageIdentifier );
90
+ this .clearRetryStatusReport ( );
64
91
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 );
67
94
return reportMap ;
68
95
} else if (!previousStatusReportIdentifier .equals (currentPackageIdentifier )) {
69
- this .recordDeploymentStatusReported ( currentPackageIdentifier );
96
+ this .clearRetryStatusReport ( );
70
97
if (this .isStatusReportIdentifierCodePushLabel (previousStatusReportIdentifier )) {
71
98
String previousDeploymentKey = this .getDeploymentKeyFromStatusReportIdentifier (previousStatusReportIdentifier );
72
99
String previousLabel = this .getVersionLabelFromStatusReportIdentifier (previousStatusReportIdentifier );
73
100
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 );
78
105
return reportMap ;
79
106
} else {
80
107
// Previous status report was with a binary app version.
81
108
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 );
85
112
return reportMap ;
86
113
}
87
114
}
@@ -90,6 +117,26 @@ public WritableMap getUpdateReport(WritableMap currentPackage) {
90
117
return null ;
91
118
}
92
119
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
+
93
140
private String getDeploymentKeyFromStatusReportIdentifier (String statusReportIdentifier ) {
94
141
String [] parsedIdentifier = statusReportIdentifier .split (":" );
95
142
if (parsedIdentifier .length > 0 ) {
@@ -99,7 +146,7 @@ private String getDeploymentKeyFromStatusReportIdentifier(String statusReportIde
99
146
}
100
147
}
101
148
102
- private String getPackageStatusReportIdentifier (WritableMap updatePackage ) {
149
+ private String getPackageStatusReportIdentifier (ReadableMap updatePackage ) {
103
150
// Because deploymentKeys can be dynamically switched, we use a
104
151
// combination of the deploymentKey and label as the packageIdentifier.
105
152
String deploymentKey = CodePushUtils .tryGetString (updatePackage , DEPLOYMENT_KEY_KEY );
@@ -129,7 +176,7 @@ private boolean isStatusReportIdentifierCodePushLabel(String statusReportIdentif
129
176
return statusReportIdentifier != null && statusReportIdentifier .contains (":" );
130
177
}
131
178
132
- private void recordDeploymentStatusReported (String appVersionOrPackageIdentifier ) {
179
+ private void saveStatusReportedForIdentifier (String appVersionOrPackageIdentifier ) {
133
180
SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
134
181
settings .edit ().putString (LAST_DEPLOYMENT_REPORT_KEY , appVersionOrPackageIdentifier ).commit ();
135
182
}
0 commit comments