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 ;
@@ -23,71 +33,103 @@ public CodePushTelemetryManager(Context applicationContext, String codePushPrefe
23
33
24
34
public WritableMap getBinaryUpdateReport (String appVersion ) {
25
35
String previousStatusReportIdentifier = this .getPreviousStatusReportIdentifier ();
36
+ WritableNativeMap reportMap = null ;
26
37
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 );
31
41
} else if (!previousStatusReportIdentifier .equals (appVersion )) {
32
- this .recordDeploymentStatusReported ( appVersion );
33
- WritableNativeMap reportMap = new WritableNativeMap ();
42
+ this .clearRetryStatusReport ( );
43
+ 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 );
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 ();
44
70
}
45
- return reportMap ;
46
71
}
47
72
48
73
return null ;
49
74
}
50
75
51
76
public WritableMap getRollbackReport (WritableMap lastFailedPackage ) {
52
77
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 );
55
80
return reportMap ;
56
81
}
57
82
58
83
public WritableMap getUpdateReport (WritableMap currentPackage ) {
59
84
String currentPackageIdentifier = this .getPackageStatusReportIdentifier (currentPackage );
60
85
String previousStatusReportIdentifier = this .getPreviousStatusReportIdentifier ();
86
+ WritableNativeMap reportMap = null ;
61
87
if (currentPackageIdentifier != null ) {
62
88
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 );
68
93
} else if (!previousStatusReportIdentifier .equals (currentPackageIdentifier )) {
69
- this .recordDeploymentStatusReported (currentPackageIdentifier );
94
+ this .clearRetryStatusReport ();
95
+ reportMap = new WritableNativeMap ();
70
96
if (this .isStatusReportIdentifierCodePushLabel (previousStatusReportIdentifier )) {
71
97
String previousDeploymentKey = this .getDeploymentKeyFromStatusReportIdentifier (previousStatusReportIdentifier );
72
98
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 );
79
103
} else {
80
104
// 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 );
86
108
}
87
109
}
88
110
}
89
111
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 ();
91
133
}
92
134
93
135
private String getDeploymentKeyFromStatusReportIdentifier (String statusReportIdentifier ) {
@@ -99,7 +141,7 @@ private String getDeploymentKeyFromStatusReportIdentifier(String statusReportIde
99
141
}
100
142
}
101
143
102
- private String getPackageStatusReportIdentifier (WritableMap updatePackage ) {
144
+ private String getPackageStatusReportIdentifier (ReadableMap updatePackage ) {
103
145
// Because deploymentKeys can be dynamically switched, we use a
104
146
// combination of the deploymentKey and label as the packageIdentifier.
105
147
String deploymentKey = CodePushUtils .tryGetString (updatePackage , DEPLOYMENT_KEY_KEY );
@@ -129,7 +171,7 @@ private boolean isStatusReportIdentifierCodePushLabel(String statusReportIdentif
129
171
return statusReportIdentifier != null && statusReportIdentifier .contains (":" );
130
172
}
131
173
132
- private void recordDeploymentStatusReported (String appVersionOrPackageIdentifier ) {
174
+ private void saveStatusReportedForIdentifier (String appVersionOrPackageIdentifier ) {
133
175
SharedPreferences settings = applicationContext .getSharedPreferences (CODE_PUSH_PREFERENCES , 0 );
134
176
settings .edit ().putString (LAST_DEPLOYMENT_REPORT_KEY , appVersionOrPackageIdentifier ).commit ();
135
177
}
0 commit comments