Skip to content

Commit

Permalink
Add rollbar-android option to detect when the network is unavailable … (
Browse files Browse the repository at this point in the history
#276)

Add rollbar-android option to detect when the network is unavailable and suspend sending occurrences.
  • Loading branch information
diegov authored Oct 18, 2021
1 parent 3b46c20 commit abf7ce6
Show file tree
Hide file tree
Showing 11 changed files with 1,130 additions and 32 deletions.
1 change: 1 addition & 0 deletions examples/rollbar-android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ dependencies {
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:27.1.1'
implementation 'org.slf4j:slf4j-android:1.7.25'
testImplementation group: 'junit', name: 'junit', version: '4.12'
}
3 changes: 3 additions & 0 deletions examples/rollbar-android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.rollbar.example.android">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
tools:ignore="AllowBackup,GoogleAppIndexingWarning"
android:allowBackup="true"
Expand Down
123 changes: 110 additions & 13 deletions rollbar-android/src/main/java/com/rollbar/android/Rollbar.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.os.Bundle;

import android.util.Log;
import com.rollbar.android.notifier.sender.ConnectionAwareSenderFailureStrategy;
import com.rollbar.android.provider.ClientProvider;
import com.rollbar.notifier.config.ConfigProvider;
import com.rollbar.notifier.uncaughtexception.RollbarUncaughtExceptionHandler;
Expand All @@ -23,24 +24,34 @@
import com.rollbar.notifier.sender.queue.DiskQueue;
import com.rollbar.notifier.util.ObjectsUtils;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class Rollbar {
public class Rollbar implements Closeable {

private static final String ITEM_DIR_NAME = "rollbar-items";
private static final String ANDROID = "android";
private static final String DEFAULT_ENVIRONMENT = "production";

private static final int DEFAULT_ITEM_SCHEDULE_STARTUP_DELAY = 1;
private static final int DEFAULT_ITEM_SCHEDULE_DELAY = 15;
private static final boolean DEFAULT_REGISTER_EXCEPTION_HANDLER = true;
private static final boolean DEFAULT_INCLUDE_LOGCAT = false;
private static final ConfigProvider DEFAULT_CONFIG_PROVIDER = null;
private static final String DEFAULT_CAPTURE_IP = "full";
private static final int DEFAULT_MAX_LOGCAT_SIZE = -1;
private static final boolean DEFAULT_SUSPEND_WHEN_NETWORK_IS_UNAVAILABLE = false;

public static final String TAG = "Rollbar";
private static final String MANIFEST_ACCESS_TOKEN = ROLLBAR_NAMESPACE + ".ACCESS_TOKEN";

private final ConnectionAwareSenderFailureStrategy senderFailureStrategy;

private com.rollbar.notifier.Rollbar rollbar;
private static Rollbar notifier;

Expand Down Expand Up @@ -68,7 +79,21 @@ public static Rollbar init(Context context) {
* @return the managed instance of Rollbar.
*/
public static Rollbar init(Context context, String accessToken, String environment) {
return init(context, accessToken, environment, true);
return init(context, accessToken, environment, DEFAULT_REGISTER_EXCEPTION_HANDLER);
}

/**
* Initialize the singleton instance of Rollbar.
*
* @param context Android context to use.
* @param accessToken a Rollbar access token with at least post_client_item scope
* @param suspendWhenNetworkIsUnavailable if true, sending occurrences will be suspended while the network is unavailable
* @return the managed instance of Rollbar.
*/
public static Rollbar init(Context context, String accessToken,
boolean suspendWhenNetworkIsUnavailable) {
return init(context, accessToken, DEFAULT_ENVIRONMENT, DEFAULT_REGISTER_EXCEPTION_HANDLER,
DEFAULT_INCLUDE_LOGCAT, DEFAULT_CONFIG_PROVIDER, suspendWhenNetworkIsUnavailable);
}

/**
Expand All @@ -81,7 +106,7 @@ public static Rollbar init(Context context, String accessToken, String environme
* @return the managed instance of Rollbar.
*/
public static Rollbar init(Context context, String accessToken, String environment, boolean registerExceptionHandler) {
return init(context, accessToken, environment, registerExceptionHandler, false);
return init(context, accessToken, environment, registerExceptionHandler, DEFAULT_INCLUDE_LOGCAT);
}

/**
Expand All @@ -95,7 +120,7 @@ public static Rollbar init(Context context, String accessToken, String environme
* @return the managed instance of Rollbar.
*/
public static Rollbar init(Context context, String accessToken, String environment, boolean registerExceptionHandler, boolean includeLogcat) {
return init(context, accessToken, environment, registerExceptionHandler, includeLogcat, null);
return init(context, accessToken, environment, registerExceptionHandler, includeLogcat, DEFAULT_CONFIG_PROVIDER);
}

/**
Expand All @@ -110,14 +135,43 @@ public static Rollbar init(Context context, String accessToken, String environme
* @return the managed instance of Rollbar.
*/
public static Rollbar init(Context context, String accessToken, String environment, boolean registerExceptionHandler, boolean includeLogcat, ConfigProvider provider) {
return init(context, accessToken, environment, registerExceptionHandler, includeLogcat,
provider, DEFAULT_SUSPEND_WHEN_NETWORK_IS_UNAVAILABLE);
}

/**
* Initialize the singleton instance of Rollbar.
*
* @param context Android context to use.
* @param accessToken a Rollbar access token with at least post_client_item scope
* @param environment the environment to set for items
* @param registerExceptionHandler whether or not to handle uncaught exceptions.
* @param includeLogcat whether or not to include logcat output with items
* @param provider a configuration provider that can be used to customize the configuration further.
* @param suspendWhenNetworkIsUnavailable if true, sending occurrences will be suspended while the network is unavailable
* @return the managed instance of Rollbar.
*/
public static Rollbar init(Context context, String accessToken, String environment,
boolean registerExceptionHandler, boolean includeLogcat,
ConfigProvider provider, boolean suspendWhenNetworkIsUnavailable) {
if (isInit()) {
Log.w(TAG, "Rollbar.init() called when it was already initialized.");
// This is likely an activity that was destroyed and recreated, so we need to update it
notifier.updateContext(context);
} else {
notifier = new Rollbar(context, accessToken, environment, registerExceptionHandler, includeLogcat, provider);
notifier = new Rollbar(context, accessToken, environment, registerExceptionHandler,
includeLogcat, provider, DEFAULT_CAPTURE_IP, DEFAULT_MAX_LOGCAT_SIZE,
suspendWhenNetworkIsUnavailable);
}
return notifier;
}

private void updateContext(Context context) {
if (this.senderFailureStrategy != null) {
this.senderFailureStrategy.updateContext(context);
}
}

/**
* Initialize the singleton instance of Rollbar.
*
Expand All @@ -134,6 +188,18 @@ public static Rollbar init(Context context, ConfigProvider provider) {
return notifier;
}

@Override
public void close() throws IOException {
if (rollbar != null) {
try {
rollbar.close(false);
} catch (Exception e) {
throw new IOException(e);
}
rollbar = null;
}
}

/**
* Has the singleton instance of Rollbar been initialized already or not.
*
Expand Down Expand Up @@ -202,7 +268,7 @@ public Rollbar(Context context, String accessToken, String environment, boolean
* @param configProvider a configuration provider that can be used to customize the configuration further.
*/
public Rollbar(Context context, String accessToken, String environment, boolean registerExceptionHandler, boolean includeLogcat, ConfigProvider configProvider) {
this(context, accessToken, environment, registerExceptionHandler, includeLogcat, configProvider, "full");
this(context, accessToken, environment, registerExceptionHandler, includeLogcat, configProvider, DEFAULT_CAPTURE_IP);
}

/**
Expand All @@ -217,9 +283,11 @@ public Rollbar(Context context, String accessToken, String environment, boolean
* @param captureIp one of: full, anonymize, none. This determines how the remote ip is captured.
*/
public Rollbar(Context context, String accessToken, String environment, boolean registerExceptionHandler, boolean includeLogcat, ConfigProvider configProvider, String captureIp) {
this(context, accessToken, environment, registerExceptionHandler, includeLogcat, configProvider, captureIp, -1);
this(context, accessToken, environment, registerExceptionHandler, includeLogcat, configProvider,
captureIp, DEFAULT_MAX_LOGCAT_SIZE);
}


/**
* Construct a new Rollbar instance.
*
Expand All @@ -233,6 +301,27 @@ public Rollbar(Context context, String accessToken, String environment, boolean
* @param maxLogcatSize the maximum number of logcat lines to capture with items (ignored unless positive)
*/
public Rollbar(Context context, String accessToken, String environment, boolean registerExceptionHandler, boolean includeLogcat, ConfigProvider configProvider, String captureIp, int maxLogcatSize) {
this(context, accessToken, environment, registerExceptionHandler, includeLogcat, configProvider,
captureIp, maxLogcatSize, DEFAULT_SUSPEND_WHEN_NETWORK_IS_UNAVAILABLE);
}

/**
* Construct a new Rollbar instance.
*
* @param context Android context to use.
* @param accessToken a Rollbar access token with at least post_client_item scope
* @param environment the environment to set for items
* @param registerExceptionHandler whether or not to handle uncaught exceptions.
* @param includeLogcat whether or not to include logcat output with items
* @param configProvider a configuration provider that can be used to customize the configuration further.
* @param captureIp one of: full, anonymize, none. This determines how the remote ip is captured.
* @param maxLogcatSize the maximum number of logcat lines to capture with items (ignored unless positive)
* @param suspendWhenNetworkIsUnavailable if true, sending occurrences will be suspended while the network is unavailable
*/
public Rollbar(Context context, String accessToken, String environment,
boolean registerExceptionHandler, boolean includeLogcat,
ConfigProvider configProvider, String captureIp, int maxLogcatSize,
boolean suspendWhenNetworkIsUnavailable) {
if (accessToken == null) {
try {
accessToken = loadAccessTokenFromManifest(context);
Expand Down Expand Up @@ -271,12 +360,20 @@ public Rollbar(Context context, String accessToken, String environment, boolean
.accessToken(accessToken)
.build();

BufferedSender sender = new BufferedSender.Builder()
.queue(queue)
.sender(innerSender)
.initialFlushDelay(TimeUnit.SECONDS.toMillis(DEFAULT_ITEM_SCHEDULE_STARTUP_DELAY))
.flushFreq(TimeUnit.SECONDS.toMillis(DEFAULT_ITEM_SCHEDULE_DELAY))
.build();
BufferedSender.Builder senderBuilder = new BufferedSender.Builder()
.queue(queue)
.sender(innerSender)
.initialFlushDelay(TimeUnit.SECONDS.toMillis(DEFAULT_ITEM_SCHEDULE_STARTUP_DELAY))
.flushFreq(TimeUnit.SECONDS.toMillis(DEFAULT_ITEM_SCHEDULE_DELAY));

if (suspendWhenNetworkIsUnavailable) {
this.senderFailureStrategy = new ConnectionAwareSenderFailureStrategy(context);
senderBuilder.senderFailureStrategy(this.senderFailureStrategy);
} else {
this.senderFailureStrategy = null;
}

BufferedSender sender = senderBuilder.build();

ConfigBuilder defaultConfig = ConfigBuilder.withAccessToken(accessToken)
.client(clientProvider)
Expand Down
Loading

0 comments on commit abf7ce6

Please sign in to comment.