@@ -64,7 +64,7 @@ public class RequestOld implements Requesting {
6464 private static final long DEVELOPMENT_MIN_DELAY_MS = 100 ;
6565 private static final long DEVELOPMENT_MAX_DELAY_MS = 5000 ;
6666 private static final long PRODUCTION_DELAY = 60000 ;
67- private RequestSequenceRecorder requestSequenceRecorder ;
67+
6868 static final int MAX_EVENTS_PER_API_CALL ;
6969 static final String LEANPLUM = "__leanplum__" ;
7070 static final String UUID_KEY = "uuid" ;
@@ -174,28 +174,6 @@ public static void saveToken() {
174174 SharedPreferencesUtil .commitChanges (editor );
175175 }
176176
177- private static class NoRequestSequenceRecorder implements RequestSequenceRecorder {
178- @ Override
179- public void beforeRead () {
180- // No op.
181- }
182-
183- @ Override
184- public void afterRead () {
185- // No op.
186- }
187-
188- @ Override
189- public void beforeWrite () {
190- // No op.
191- }
192-
193- @ Override
194- public void afterWrite () {
195- // No op.
196- }
197- }
198-
199177 public static String appId () {
200178 return appId ;
201179 }
@@ -209,10 +187,6 @@ public static String userId() {
209187 }
210188
211189 public RequestOld (String httpMethod , String apiMethod , Map <String , Object > params ) {
212- this (httpMethod , apiMethod , params , new NoRequestSequenceRecorder ());
213- }
214-
215- RequestOld (String httpMethod , String apiMethod , Map <String , Object > params , RequestSequenceRecorder requestSequenceRecorder ) {
216190 this .httpMethod = httpMethod ;
217191 this .apiMethod = apiMethod ;
218192 this .params = params != null ? params : new HashMap <String , Object >();
@@ -223,7 +197,7 @@ public RequestOld(String httpMethod, String apiMethod, Map<String, Object> param
223197 // Make sure the Handler is initialized on the main thread.
224198 OsHandler .getInstance ();
225199 dataBaseIndex = -1 ;
226- this . requestSequenceRecorder = requestSequenceRecorder ;
200+
227201 this .requestId = UUID .randomUUID ().toString ();
228202 }
229203
@@ -257,7 +231,6 @@ public void onApiResponse(ApiResponseCallback apiResponse) {
257231 RequestOld .apiResponse = apiResponse ;
258232 }
259233
260- @ VisibleForTesting
261234 public Map <String , Object > createArgsDictionary () {
262235 Map <String , Object > args = new HashMap <>();
263236 args .put (Constants .Params .DEVICE_ID , deviceId );
@@ -274,43 +247,47 @@ public Map<String, Object> createArgsDictionary() {
274247 return args ;
275248 }
276249
277- private void saveRequestForLater (Map <String , Object > args ) {
278- try {
250+ /**
251+ * Saves requests into database.
252+ * Saving will be executed on background thread serially.
253+ * @param args json to save.
254+ */
255+ private void saveRequestForLater (final Map <String , Object > args ) {
256+ OperationQueue .sharedInstance ().addOperation (new Runnable () {
257+ @ Override
258+ public void run () {
259+ try {
260+ Context context = Leanplum .getContext ();
261+ if (context == null ) {
262+ return ;
263+ }
279264
280- Context context = Leanplum .getContext ();
281- if (context == null ) {
282- return ;
283- }
265+ SharedPreferences preferences = context .getSharedPreferences (LEANPLUM ,
266+ Context .MODE_PRIVATE );
267+ SharedPreferences .Editor editor = preferences .edit ();
268+ long count = LeanplumEventDataManager .sharedInstance ().getEventsCount ();
269+ String uuid = preferences .getString (Constants .Defaults .UUID_KEY , null );
270+ if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0 ) {
271+ uuid = UUID .randomUUID ().toString ();
272+ editor .putString (Constants .Defaults .UUID_KEY , uuid );
273+ SharedPreferencesUtil .commitChanges (editor );
274+ }
275+ args .put (UUID_KEY , uuid );
276+ LeanplumEventDataManager .sharedInstance ().insertEvent (JsonConverter .toJson (args ));
277+
278+ dataBaseIndex = count ;
279+ // Checks if here response and/or error callback for this request. We need to add callbacks to
280+ // eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
281+ // will handle error callback for this event.
282+ if (response != null || error != null && !Util .isConnected ()) {
283+ eventCallbackManager .addCallbacks (RequestOld .this , response , error );
284+ }
284285
285- requestSequenceRecorder .beforeWrite ();
286-
287- synchronized (RequestOld .class ) {
288- SharedPreferences preferences = context .getSharedPreferences (
289- LEANPLUM , Context .MODE_PRIVATE );
290- SharedPreferences .Editor editor = preferences .edit ();
291- long count = LeanplumEventDataManager .sharedInstance ().getEventsCount ();
292- String uuid = preferences .getString (Constants .Defaults .UUID_KEY , null );
293- if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0 ) {
294- uuid = UUID .randomUUID ().toString ();
295- editor .putString (Constants .Defaults .UUID_KEY , uuid );
296- SharedPreferencesUtil .commitChanges (editor );
297- }
298- args .put (UUID_KEY , uuid );
299- LeanplumEventDataManager .sharedInstance ().insertEvent (JsonConverter .toJson (args ));
300-
301- dataBaseIndex = count ;
302- // Checks if here response and/or error callback for this request. We need to add callbacks to
303- // eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
304- // will handle error callback for this event.
305- if (response != null || error != null && !Util .isConnected ()) {
306- eventCallbackManager .addCallbacks (this , response , error );
286+ } catch (Throwable t ) {
287+ Util .handleException (t );
307288 }
308289 }
309-
310- requestSequenceRecorder .afterWrite ();
311- } catch (Throwable t ) {
312- Util .handleException (t );
313- }
290+ });
314291 }
315292
316293 public void send () {
@@ -481,7 +458,6 @@ private void parseResponseBody(JSONObject responseBody, List<Map<String, Object>
481458 * @param errorMessage String of error from server response.
482459 * @return String of readable error message.
483460 */
484- @ NonNull
485461 private String getReadableErrorMessage (String errorMessage ) {
486462 if (errorMessage == null || errorMessage .length () == 0 ) {
487463 errorMessage = "API error" ;
@@ -607,12 +583,9 @@ private RequestsWithEncoding getRequestsWithEncodedString() {
607583
608584 private void sendRequests () {
609585 Leanplum .countAggregator ().sendAllCounts ();
610- requestSequenceRecorder .beforeRead ();
611586
612587 RequestsWithEncoding requestsWithEncoding = getRequestsWithEncodedString ();
613588
614- requestSequenceRecorder .afterRead ();
615-
616589 List <Map <String , Object >> unsentRequests = requestsWithEncoding .unsentRequests ;
617590 List <Map <String , Object >> requestsToSend = requestsWithEncoding .requestsToSend ;
618591 String jsonEncodedString = requestsWithEncoding .jsonEncodedString ;
0 commit comments