4545import java .util .ArrayList ;
4646import java .util .Date ;
4747import java .util .HashMap ;
48+ import java .util .LinkedList ;
4849import java .util .List ;
4950import java .util .Map ;
5051import java .util .Stack ;
@@ -59,6 +60,7 @@ public class Request {
5960 private static final long DEVELOPMENT_MIN_DELAY_MS = 100 ;
6061 private static final long DEVELOPMENT_MAX_DELAY_MS = 5000 ;
6162 private static final long PRODUCTION_DELAY = 60000 ;
63+ private Waiter waiter ;
6264 static final int MAX_EVENTS_PER_API_CALL ;
6365 static final String LEANPLUM = "__leanplum__" ;
6466 static final String UUID_KEY = "uuid" ;
@@ -164,6 +166,28 @@ public static void saveToken() {
164166 SharedPreferencesUtil .commitChanges (editor );
165167 }
166168
169+ private static class NoWaiter implements Waiter {
170+ @ Override
171+ public void beforeRead () {
172+ // No op.
173+ }
174+
175+ @ Override
176+ public void afterRead () {
177+ // No op.
178+ }
179+
180+ @ Override
181+ public void beforeWrite () {
182+ // No op.
183+ }
184+
185+ @ Override
186+ public void afterWrite () {
187+ // No op.
188+ }
189+ }
190+
167191 public static String appId () {
168192 return appId ;
169193 }
@@ -177,6 +201,10 @@ public static String userId() {
177201 }
178202
179203 public Request (String httpMethod , String apiMethod , Map <String , Object > params ) {
204+ this (httpMethod , apiMethod , params , new NoWaiter ());
205+ }
206+
207+ Request (String httpMethod , String apiMethod , Map <String , Object > params , Waiter waiter ) {
180208 this .httpMethod = httpMethod ;
181209 this .apiMethod = apiMethod ;
182210 this .params = params != null ? params : new HashMap <String , Object >();
@@ -187,6 +215,7 @@ public Request(String httpMethod, String apiMethod, Map<String, Object> params)
187215 // Make sure the Handler is initialized on the main thread.
188216 OsHandler .getInstance ();
189217 dataBaseIndex = -1 ;
218+ this .waiter = waiter ;
190219 }
191220
192221 public static Request get (String apiMethod , Map <String , Object > params ) {
@@ -231,28 +260,34 @@ private Map<String, Object> createArgsDictionary() {
231260 }
232261
233262 private void saveRequestForLater (Map <String , Object > args ) {
234- synchronized (Request .class ) {
235- Context context = Leanplum .getContext ();
236- SharedPreferences preferences = context .getSharedPreferences (
237- LEANPLUM , Context .MODE_PRIVATE );
238- SharedPreferences .Editor editor = preferences .edit ();
239- long count = LeanplumEventDataManager .getEventsCount ();
240- String uuid = preferences .getString (Constants .Defaults .UUID_KEY , null );
241- if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0 ) {
242- uuid = UUID .randomUUID ().toString ();
243- editor .putString (Constants .Defaults .UUID_KEY , uuid );
244- SharedPreferencesUtil .commitChanges (editor );
245- }
246- args .put (UUID_KEY , uuid );
247- LeanplumEventDataManager .insertEvent (JsonConverter .toJson (args ));
248-
249- dataBaseIndex = count ;
250- // Checks if here response and/or error callback for this request. We need to add callbacks to
251- // eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
252- // will handle error callback for this event.
253- if (response != null || error != null && !Util .isConnected ()) {
254- eventCallbackManager .addCallbacks (this , response , error );
263+ try {
264+ waiter .beforeWrite ();
265+ synchronized (Request .class ) {
266+ Context context = Leanplum .getContext ();
267+ SharedPreferences preferences = context .getSharedPreferences (
268+ LEANPLUM , Context .MODE_PRIVATE );
269+ SharedPreferences .Editor editor = preferences .edit ();
270+ long count = LeanplumEventDataManager .getEventsCount ();
271+ String uuid = preferences .getString (Constants .Defaults .UUID_KEY , null );
272+ if (uuid == null || count % MAX_EVENTS_PER_API_CALL == 0 ) {
273+ uuid = UUID .randomUUID ().toString ();
274+ editor .putString (Constants .Defaults .UUID_KEY , uuid );
275+ SharedPreferencesUtil .commitChanges (editor );
276+ }
277+ args .put (UUID_KEY , uuid );
278+ LeanplumEventDataManager .insertEvent (JsonConverter .toJson (args ));
279+
280+ dataBaseIndex = count ;
281+ // Checks if here response and/or error callback for this request. We need to add callbacks to
282+ // eventCallbackManager only if here was internet connection, otherwise triggerErrorCallback
283+ // will handle error callback for this event.
284+ if (response != null || error != null && !Util .isConnected ()) {
285+ eventCallbackManager .addCallbacks (this , response , error );
286+ }
287+ waiter .afterWrite ();
255288 }
289+ } catch (Throwable t ) {
290+ Util .handleException (t );
256291 }
257292 }
258293
@@ -454,13 +489,14 @@ private void sendNow() {
454489 return ;
455490 }
456491
457- this .sendEventually ();
492+ //use something like a callback interface and pass it through here
493+ this .sendEventually (); //write
458494
459495 Util .executeAsyncTask (true , new AsyncTask <Void , Void , Void >() {
460496 @ Override
461497 protected Void doInBackground (Void ... params ) {
462498 try {
463- sendRequests ();
499+ sendRequests (); // read
464500 } catch (Throwable t ) {
465501 Util .handleException (t );
466502 }
@@ -545,6 +581,7 @@ private RequestsWithEncoding getRequestsWithEncodedString() {
545581 }
546582
547583 private void sendRequests () {
584+ waiter .beforeRead ();
548585 RequestsWithEncoding requestsWithEncoding = getRequestsWithEncodedString ();
549586
550587 List <Map <String , Object >> unsentRequests = requestsWithEncoding .unsentRequests ;
@@ -611,6 +648,7 @@ private void sendRequests() {
611648 parseResponseBody (responseBody , requestsToSend , errorException , unsentRequests .size ());
612649 }
613650 }
651+ waiter .afterRead ();
614652 } catch (JSONException e ) {
615653 Log .e ("Error parsing JSON response: " + e .toString () + "\n " + Log .getStackTraceString (e ));
616654 deleteSentRequests (unsentRequests .size ());
0 commit comments