2525import android .content .SharedPreferences ;
2626import android .os .AsyncTask ;
2727import android .os .Build ;
28-
29- import androidx .annotation .MainThread ;
30- import androidx .annotation .NonNull ;
31- import androidx .annotation .VisibleForTesting ;
32-
33- import android .os .Looper ;
3428import android .text .TextUtils ;
3529
3630import com .leanplum .Leanplum ;
@@ -66,7 +60,7 @@ public class RequestOld implements Requesting {
6660 private static final long DEVELOPMENT_MIN_DELAY_MS = 100 ;
6761 private static final long DEVELOPMENT_MAX_DELAY_MS = 5000 ;
6862 private static final long PRODUCTION_DELAY = 60000 ;
69- private RequestSequenceRecorder requestSequenceRecorder ;
63+
7064 static final int MAX_EVENTS_PER_API_CALL ;
7165 static final String LEANPLUM = "__leanplum__" ;
7266 static final String UUID_KEY = "uuid" ;
@@ -176,28 +170,6 @@ public static void saveToken() {
176170 SharedPreferencesUtil .commitChanges (editor );
177171 }
178172
179- private static class NoRequestSequenceRecorder implements RequestSequenceRecorder {
180- @ Override
181- public void beforeRead () {
182- // No op.
183- }
184-
185- @ Override
186- public void afterRead () {
187- // No op.
188- }
189-
190- @ Override
191- public void beforeWrite () {
192- // No op.
193- }
194-
195- @ Override
196- public void afterWrite () {
197- // No op.
198- }
199- }
200-
201173 public static String appId () {
202174 return appId ;
203175 }
@@ -211,10 +183,6 @@ public static String userId() {
211183 }
212184
213185 public RequestOld (String httpMethod , String apiMethod , Map <String , Object > params ) {
214- this (httpMethod , apiMethod , params , new NoRequestSequenceRecorder ());
215- }
216-
217- RequestOld (String httpMethod , String apiMethod , Map <String , Object > params , RequestSequenceRecorder requestSequenceRecorder ) {
218186 this .httpMethod = httpMethod ;
219187 this .apiMethod = apiMethod ;
220188 this .params = params != null ? params : new HashMap <String , Object >();
@@ -225,7 +193,7 @@ public RequestOld(String httpMethod, String apiMethod, Map<String, Object> param
225193 // Make sure the Handler is initialized on the main thread.
226194 OsHandler .getInstance ();
227195 dataBaseIndex = -1 ;
228- this . requestSequenceRecorder = requestSequenceRecorder ;
196+
229197 this .requestId = UUID .randomUUID ().toString ();
230198 }
231199
@@ -259,7 +227,6 @@ public void onApiResponse(ApiResponseCallback apiResponse) {
259227 RequestOld .apiResponse = apiResponse ;
260228 }
261229
262- @ VisibleForTesting
263230 public Map <String , Object > createArgsDictionary () {
264231 Map <String , Object > args = new HashMap <>();
265232 args .put (Constants .Params .DEVICE_ID , deviceId );
@@ -276,43 +243,47 @@ public Map<String, Object> createArgsDictionary() {
276243 return args ;
277244 }
278245
279- private void saveRequestForLater (Map <String , Object > args ) {
280- try {
246+ /**
247+ * Saves requests into database.
248+ * Saving will be executed on background thread serially.
249+ * @param args json to save.
250+ */
251+ private void saveRequestForLater (final Map <String , Object > args ) {
252+ OperationQueue .sharedInstance ().addOperation (new Runnable () {
253+ @ Override
254+ public void run () {
255+ try {
256+ Context context = Leanplum .getContext ();
257+ if (context == null ) {
258+ return ;
259+ }
281260
282- Context context = Leanplum .getContext ();
283- if (context == null ) {
284- return ;
285- }
261+ SharedPreferences preferences = context .getSharedPreferences (LEANPLUM ,
262+ Context .MODE_PRIVATE );
263+ SharedPreferences .Editor editor = preferences .edit ();
264+ long count = LeanplumEventDataManager .sharedInstance ().getEventsCount ();
265+ String uuid = preferences .getString (Constants .Defaults .UUID_KEY , null );
266+ if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0 ) {
267+ uuid = UUID .randomUUID ().toString ();
268+ editor .putString (Constants .Defaults .UUID_KEY , uuid );
269+ SharedPreferencesUtil .commitChanges (editor );
270+ }
271+ args .put (UUID_KEY , uuid );
272+ LeanplumEventDataManager .sharedInstance ().insertEvent (JsonConverter .toJson (args ));
273+
274+ dataBaseIndex = count ;
275+ // Checks if here response and/or error callback for this request. We need to add callbacks to
276+ // eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
277+ // will handle error callback for this event.
278+ if (response != null || error != null && !Util .isConnected ()) {
279+ eventCallbackManager .addCallbacks (RequestOld .this , response , error );
280+ }
286281
287- requestSequenceRecorder .beforeWrite ();
288-
289- synchronized (RequestOld .class ) {
290- SharedPreferences preferences = context .getSharedPreferences (
291- LEANPLUM , Context .MODE_PRIVATE );
292- SharedPreferences .Editor editor = preferences .edit ();
293- long count = LeanplumEventDataManager .sharedInstance ().getEventsCount ();
294- String uuid = preferences .getString (Constants .Defaults .UUID_KEY , null );
295- if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0 ) {
296- uuid = UUID .randomUUID ().toString ();
297- editor .putString (Constants .Defaults .UUID_KEY , uuid );
298- SharedPreferencesUtil .commitChanges (editor );
299- }
300- args .put (UUID_KEY , uuid );
301- LeanplumEventDataManager .sharedInstance ().insertEvent (JsonConverter .toJson (args ));
302-
303- dataBaseIndex = count ;
304- // Checks if here response and/or error callback for this request. We need to add callbacks to
305- // eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
306- // will handle error callback for this event.
307- if (response != null || error != null && !Util .isConnected ()) {
308- eventCallbackManager .addCallbacks (this , response , error );
282+ } catch (Throwable t ) {
283+ Util .handleException (t );
309284 }
310285 }
311-
312- requestSequenceRecorder .afterWrite ();
313- } catch (Throwable t ) {
314- Util .handleException (t );
315- }
286+ });
316287 }
317288
318289 public void send () {
@@ -483,7 +454,6 @@ private void parseResponseBody(JSONObject responseBody, List<Map<String, Object>
483454 * @param errorMessage String of error from server response.
484455 * @return String of readable error message.
485456 */
486- @ NonNull
487457 private String getReadableErrorMessage (String errorMessage ) {
488458 if (errorMessage == null || errorMessage .length () == 0 ) {
489459 errorMessage = "API error" ;
@@ -609,12 +579,9 @@ private RequestsWithEncoding getRequestsWithEncodedString() {
609579
610580 private void sendRequests () {
611581 Leanplum .countAggregator ().sendAllCounts ();
612- requestSequenceRecorder .beforeRead ();
613582
614583 RequestsWithEncoding requestsWithEncoding = getRequestsWithEncodedString ();
615584
616- requestSequenceRecorder .afterRead ();
617-
618585 List <Map <String , Object >> unsentRequests = requestsWithEncoding .unsentRequests ;
619586 List <Map <String , Object >> requestsToSend = requestsWithEncoding .requestsToSend ;
620587 String jsonEncodedString = requestsWithEncoding .jsonEncodedString ;
0 commit comments