Skip to content

Commit c6bec63

Browse files
authored
fix: Events deleted from local DB on failures when device is offline (#3038)
1 parent e017c7a commit c6bec63

File tree

1 file changed

+42
-6
lines changed
  • aws-android-sdk-pinpoint/src/main/java/com/amazonaws/mobileconnectors/pinpoint/internal/event

1 file changed

+42
-6
lines changed

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

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@
1515

1616
package com.amazonaws.mobileconnectors.pinpoint.internal.event;
1717

18+
import android.content.Context;
1819
import android.database.Cursor;
20+
import android.net.ConnectivityManager;
21+
import android.net.NetworkCapabilities;
22+
import android.net.NetworkInfo;
1923
import android.net.Uri;
2024

2125
import java.net.SocketException;
2226
import java.net.UnknownHostException;
2327
import java.util.ArrayList;
28+
import java.util.Collections;
2429
import java.util.Date;
2530
import java.util.HashMap;
2631
import java.util.List;
@@ -247,7 +252,11 @@ public void submitEvents() {
247252
submissionRunnableQueue.execute(new Runnable() {
248253
@Override
249254
public void run() {
250-
processEvents();
255+
if (isNetworkAvailable(pinpointContext.getApplicationContext())) {
256+
processEvents();
257+
} else {
258+
log.warn("Device is offline, skipping submitting events to Pinpoint");
259+
}
251260
}
252261
});
253262
}
@@ -256,7 +265,12 @@ public Future<List<AnalyticsEvent>> submitEventsWithResult() {
256265
return submissionRunnableQueue.submit(new Callable<List<AnalyticsEvent>>() {
257266
@Override
258267
public List<AnalyticsEvent> call() throws Exception {
259-
return processEvents();
268+
if (isNetworkAvailable(pinpointContext.getApplicationContext())) {
269+
return processEvents();
270+
} else {
271+
log.warn("Device is offline, skipping submitting events to Pinpoint");
272+
return Collections.emptyList();
273+
}
260274
}
261275
});
262276
}
@@ -451,7 +465,8 @@ private void submitEventsAndEndpoint(final JSONArray eventArray,
451465
// in the local database.
452466
// For all other client exceptions occurred during submit events,
453467
// log the exception and delete the events in the local database.
454-
if (isClientExceptionRetryable(amazonClientException)) {
468+
if (isClientExceptionRetryable(amazonClientException) ||
469+
isClientExceptionRetryable(amazonClientException.getCause())) {
455470
log.error("AmazonClientException: Unable to successfully deliver events to server. " +
456471
"Events will be saved, error likely recoverable." +
457472
amazonClientException.getMessage(), amazonClientException);
@@ -542,10 +557,13 @@ private boolean isRetryable(String responseCode) {
542557
return true;
543558
}
544559

545-
private boolean isClientExceptionRetryable(AmazonClientException amazonClientException) {
560+
private boolean isClientExceptionRetryable(Throwable amazonClientException) {
561+
if (!isNetworkAvailable(pinpointContext.getApplicationContext())) {
562+
return true;
563+
}
546564
return amazonClientException.getCause() != null &&
547-
(amazonClientException.getCause() instanceof UnknownHostException ||
548-
amazonClientException.getCause() instanceof SocketException);
565+
(amazonClientException.getCause() instanceof UnknownHostException ||
566+
amazonClientException.getCause() instanceof SocketException);
549567
}
550568

551569
/**
@@ -674,4 +692,22 @@ void buildEventPayload(AnalyticsEvent internalEvent,
674692
.withSession(session)
675693
.withTimestamp(DateUtils.formatISO8601Date(new Date(internalEvent.getEventTimestamp())));
676694
}
695+
696+
private boolean isNetworkAvailable(Context context) {
697+
ConnectivityManager connectivityManager =
698+
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
699+
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
700+
NetworkCapabilities capabilities =
701+
connectivityManager.getNetworkCapabilities(connectivityManager.getActiveNetwork());
702+
if (capabilities != null) {
703+
return capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) ||
704+
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) ||
705+
capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET);
706+
}
707+
} else {
708+
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
709+
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
710+
}
711+
return false;
712+
}
677713
}

0 commit comments

Comments
 (0)