Skip to content

Commit 2049946

Browse files
authored
feat: Add submitEvent API with callbacks (#2934)
1 parent 7ce3f17 commit 2049946

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

aws-android-sdk-pinpoint/src/main/java/com/amazonaws/mobileconnectors/pinpoint/analytics/AnalyticsClient.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@
1515

1616
package com.amazonaws.mobileconnectors.pinpoint.analytics;
1717

18+
import androidx.core.util.Consumer;
1819
import java.util.List;
1920
import java.util.Map;
2021
import java.util.Map.Entry;
2122
import java.util.concurrent.ConcurrentHashMap;
2223
import com.amazonaws.logging.Log;
2324
import com.amazonaws.logging.LogFactory;
25+
import java.util.concurrent.ExecutionException;
26+
import java.util.concurrent.Future;
2427
import org.json.JSONArray;
2528
import org.json.JSONException;
2629
import org.json.JSONObject;
@@ -169,6 +172,28 @@ public void submitEvents() {
169172
eventRecorder.submitEvents();
170173
}
171174

175+
/**
176+
* Submit all recorded events and returns all the successfully submitted events.
177+
* If the device is off line, this is a no-op. See
178+
* {@link PinpointConfiguration}
179+
* for customizing which Internet connection the SDK can submit on.
180+
*
181+
* @param onSuccess Callback to return successfully submitted events.
182+
* @param onError Callback to return error.
183+
*/
184+
public void submitEvents(
185+
Consumer<List<AnalyticsEvent>> onSuccess,
186+
Consumer<Exception> onError
187+
) {
188+
log.info("Submitting events.");
189+
try {
190+
Future<List<AnalyticsEvent>> result = eventRecorder.submitEventsWithResult();
191+
onSuccess.accept(result.get());
192+
} catch (InterruptedException | ExecutionException exception) {
193+
onError.accept(exception);
194+
}
195+
}
196+
172197
/**
173198
* Adds the specified attribute to all subsequently created events Note: The
174199
* maximum allowed attributes and metrics on a single event is 40. Attempts

aws-android-sdk-pinpoint/src/main/java/com/amazonaws/mobileconnectors/pinpoint/internal/event/EventRecorder.java

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@
2626
import java.util.List;
2727
import java.util.Locale;
2828
import java.util.Map;
29+
import java.util.concurrent.Callable;
2930
import java.util.concurrent.ExecutorService;
31+
import java.util.concurrent.Future;
3032
import java.util.concurrent.LinkedBlockingQueue;
3133
import java.util.concurrent.ThreadPoolExecutor;
3234
import java.util.concurrent.TimeUnit;
@@ -250,6 +252,15 @@ public void run() {
250252
});
251253
}
252254

255+
public Future<List<AnalyticsEvent>> submitEventsWithResult() {
256+
return submissionRunnableQueue.submit(new Callable<List<AnalyticsEvent>>() {
257+
@Override
258+
public List<AnalyticsEvent> call() throws Exception {
259+
return processEvents();
260+
}
261+
});
262+
}
263+
253264
/**
254265
* Reads events of maximum of KEY_MAX_SUBMISSION_SIZE size.
255266
* The default max request size is DEFAULT_MAX_SUBMISSION_SIZE.
@@ -302,18 +313,18 @@ public List<JSONObject> getAllEvents() {
302313
return events;
303314
}
304315

305-
void processEvents() {
316+
List<AnalyticsEvent> processEvents() {
306317
final long start = TimeUnit.NANOSECONDS.toMillis(System.nanoTime());
307318

308319
Cursor cursor = null;
309-
320+
List<AnalyticsEvent> result = new ArrayList<>();
310321
try {
311322
cursor = dbUtil.queryAllEvents();
312323

313324
if (!cursor.moveToFirst()) {
314325
// if the cursor is empty there is nothing to do.
315326
log.info("No events available to submit.");
316-
return;
327+
return result;
317328
}
318329

319330
int submissions = 0;
@@ -336,6 +347,8 @@ void processEvents() {
336347
submissions++;
337348
}
338349

350+
//Add all successfully submitted events to result
351+
result.addAll(getSuccessfullySyncedEvents(events, batchIdsAndSizeToDelete));
339352
// Delete events from the local database. At this point batchIdsAndSizeToDelete
340353
// reflects the set of events that can be deleted from the local database.
341354
for (Integer id : batchIdsAndSizeToDelete.keySet()) {
@@ -351,12 +364,28 @@ void processEvents() {
351364
} while (cursor.moveToNext());
352365

353366
log.info(String.format(Locale.US, "Time of attemptDelivery: %d",
354-
TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) - start));
367+
TimeUnit.NANOSECONDS.toMillis(System.nanoTime()) - start));
368+
} catch (JSONException jsonException) {
369+
log.error("Failed to parse to event object", jsonException);
355370
} finally {
356371
if (cursor != null) {
357372
cursor.close();
358373
}
359374
}
375+
log.info(String.format("Submitted %s events", result.size()));
376+
return result;
377+
}
378+
379+
private List<AnalyticsEvent> getSuccessfullySyncedEvents(JSONArray events,
380+
HashMap<Integer, Integer> batchIdsAndSizeToDelete)
381+
throws JSONException {
382+
List<AnalyticsEvent> result = new ArrayList<>();
383+
for (int i = 0; i<events.length(); i++) {
384+
if (batchIdsAndSizeToDelete.containsKey(events.getJSONObject(i).getInt(DATABASE_ID_KEY))) {
385+
result.add(AnalyticsEvent.translateToEvent(events.getJSONObject(i)));
386+
}
387+
}
388+
return result;
360389
}
361390

362391
private void submitEventsAndEndpoint(final JSONArray eventArray,

0 commit comments

Comments
 (0)