Skip to content

Commit abf7ce6

Browse files
authored
Add rollbar-android option to detect when the network is unavailable … (#276)
Add rollbar-android option to detect when the network is unavailable and suspend sending occurrences.
1 parent 3b46c20 commit abf7ce6

File tree

11 files changed

+1130
-32
lines changed

11 files changed

+1130
-32
lines changed

examples/rollbar-android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ dependencies {
4040
implementation 'com.android.support:appcompat-v7:27.1.1'
4141
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
4242
implementation 'com.android.support:design:27.1.1'
43+
implementation 'org.slf4j:slf4j-android:1.7.25'
4344
testImplementation group: 'junit', name: 'junit', version: '4.12'
4445
}

examples/rollbar-android/src/main/AndroidManifest.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
package="com.rollbar.example.android">
55

6+
<uses-permission android:name="android.permission.INTERNET" />
7+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
8+
69
<application
710
tools:ignore="AllowBackup,GoogleAppIndexingWarning"
811
android:allowBackup="true"

rollbar-android/src/main/java/com/rollbar/android/Rollbar.java

Lines changed: 110 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import android.os.Bundle;
1111

1212
import android.util.Log;
13+
import com.rollbar.android.notifier.sender.ConnectionAwareSenderFailureStrategy;
1314
import com.rollbar.android.provider.ClientProvider;
1415
import com.rollbar.notifier.config.ConfigProvider;
1516
import com.rollbar.notifier.uncaughtexception.RollbarUncaughtExceptionHandler;
@@ -23,24 +24,34 @@
2324
import com.rollbar.notifier.sender.queue.DiskQueue;
2425
import com.rollbar.notifier.util.ObjectsUtils;
2526

27+
import java.io.Closeable;
2628
import java.io.File;
29+
import java.io.IOException;
2730
import java.lang.Thread.UncaughtExceptionHandler;
2831
import java.util.Collections;
2932
import java.util.Map;
3033
import java.util.concurrent.TimeUnit;
3134

32-
public class Rollbar {
35+
public class Rollbar implements Closeable {
3336

3437
private static final String ITEM_DIR_NAME = "rollbar-items";
3538
private static final String ANDROID = "android";
3639
private static final String DEFAULT_ENVIRONMENT = "production";
3740

3841
private static final int DEFAULT_ITEM_SCHEDULE_STARTUP_DELAY = 1;
3942
private static final int DEFAULT_ITEM_SCHEDULE_DELAY = 15;
43+
private static final boolean DEFAULT_REGISTER_EXCEPTION_HANDLER = true;
44+
private static final boolean DEFAULT_INCLUDE_LOGCAT = false;
45+
private static final ConfigProvider DEFAULT_CONFIG_PROVIDER = null;
46+
private static final String DEFAULT_CAPTURE_IP = "full";
47+
private static final int DEFAULT_MAX_LOGCAT_SIZE = -1;
48+
private static final boolean DEFAULT_SUSPEND_WHEN_NETWORK_IS_UNAVAILABLE = false;
4049

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

53+
private final ConnectionAwareSenderFailureStrategy senderFailureStrategy;
54+
4455
private com.rollbar.notifier.Rollbar rollbar;
4556
private static Rollbar notifier;
4657

@@ -68,7 +79,21 @@ public static Rollbar init(Context context) {
6879
* @return the managed instance of Rollbar.
6980
*/
7081
public static Rollbar init(Context context, String accessToken, String environment) {
71-
return init(context, accessToken, environment, true);
82+
return init(context, accessToken, environment, DEFAULT_REGISTER_EXCEPTION_HANDLER);
83+
}
84+
85+
/**
86+
* Initialize the singleton instance of Rollbar.
87+
*
88+
* @param context Android context to use.
89+
* @param accessToken a Rollbar access token with at least post_client_item scope
90+
* @param suspendWhenNetworkIsUnavailable if true, sending occurrences will be suspended while the network is unavailable
91+
* @return the managed instance of Rollbar.
92+
*/
93+
public static Rollbar init(Context context, String accessToken,
94+
boolean suspendWhenNetworkIsUnavailable) {
95+
return init(context, accessToken, DEFAULT_ENVIRONMENT, DEFAULT_REGISTER_EXCEPTION_HANDLER,
96+
DEFAULT_INCLUDE_LOGCAT, DEFAULT_CONFIG_PROVIDER, suspendWhenNetworkIsUnavailable);
7297
}
7398

7499
/**
@@ -81,7 +106,7 @@ public static Rollbar init(Context context, String accessToken, String environme
81106
* @return the managed instance of Rollbar.
82107
*/
83108
public static Rollbar init(Context context, String accessToken, String environment, boolean registerExceptionHandler) {
84-
return init(context, accessToken, environment, registerExceptionHandler, false);
109+
return init(context, accessToken, environment, registerExceptionHandler, DEFAULT_INCLUDE_LOGCAT);
85110
}
86111

87112
/**
@@ -95,7 +120,7 @@ public static Rollbar init(Context context, String accessToken, String environme
95120
* @return the managed instance of Rollbar.
96121
*/
97122
public static Rollbar init(Context context, String accessToken, String environment, boolean registerExceptionHandler, boolean includeLogcat) {
98-
return init(context, accessToken, environment, registerExceptionHandler, includeLogcat, null);
123+
return init(context, accessToken, environment, registerExceptionHandler, includeLogcat, DEFAULT_CONFIG_PROVIDER);
99124
}
100125

101126
/**
@@ -110,14 +135,43 @@ public static Rollbar init(Context context, String accessToken, String environme
110135
* @return the managed instance of Rollbar.
111136
*/
112137
public static Rollbar init(Context context, String accessToken, String environment, boolean registerExceptionHandler, boolean includeLogcat, ConfigProvider provider) {
138+
return init(context, accessToken, environment, registerExceptionHandler, includeLogcat,
139+
provider, DEFAULT_SUSPEND_WHEN_NETWORK_IS_UNAVAILABLE);
140+
}
141+
142+
/**
143+
* Initialize the singleton instance of Rollbar.
144+
*
145+
* @param context Android context to use.
146+
* @param accessToken a Rollbar access token with at least post_client_item scope
147+
* @param environment the environment to set for items
148+
* @param registerExceptionHandler whether or not to handle uncaught exceptions.
149+
* @param includeLogcat whether or not to include logcat output with items
150+
* @param provider a configuration provider that can be used to customize the configuration further.
151+
* @param suspendWhenNetworkIsUnavailable if true, sending occurrences will be suspended while the network is unavailable
152+
* @return the managed instance of Rollbar.
153+
*/
154+
public static Rollbar init(Context context, String accessToken, String environment,
155+
boolean registerExceptionHandler, boolean includeLogcat,
156+
ConfigProvider provider, boolean suspendWhenNetworkIsUnavailable) {
113157
if (isInit()) {
114158
Log.w(TAG, "Rollbar.init() called when it was already initialized.");
159+
// This is likely an activity that was destroyed and recreated, so we need to update it
160+
notifier.updateContext(context);
115161
} else {
116-
notifier = new Rollbar(context, accessToken, environment, registerExceptionHandler, includeLogcat, provider);
162+
notifier = new Rollbar(context, accessToken, environment, registerExceptionHandler,
163+
includeLogcat, provider, DEFAULT_CAPTURE_IP, DEFAULT_MAX_LOGCAT_SIZE,
164+
suspendWhenNetworkIsUnavailable);
117165
}
118166
return notifier;
119167
}
120168

169+
private void updateContext(Context context) {
170+
if (this.senderFailureStrategy != null) {
171+
this.senderFailureStrategy.updateContext(context);
172+
}
173+
}
174+
121175
/**
122176
* Initialize the singleton instance of Rollbar.
123177
*
@@ -134,6 +188,18 @@ public static Rollbar init(Context context, ConfigProvider provider) {
134188
return notifier;
135189
}
136190

191+
@Override
192+
public void close() throws IOException {
193+
if (rollbar != null) {
194+
try {
195+
rollbar.close(false);
196+
} catch (Exception e) {
197+
throw new IOException(e);
198+
}
199+
rollbar = null;
200+
}
201+
}
202+
137203
/**
138204
* Has the singleton instance of Rollbar been initialized already or not.
139205
*
@@ -202,7 +268,7 @@ public Rollbar(Context context, String accessToken, String environment, boolean
202268
* @param configProvider a configuration provider that can be used to customize the configuration further.
203269
*/
204270
public Rollbar(Context context, String accessToken, String environment, boolean registerExceptionHandler, boolean includeLogcat, ConfigProvider configProvider) {
205-
this(context, accessToken, environment, registerExceptionHandler, includeLogcat, configProvider, "full");
271+
this(context, accessToken, environment, registerExceptionHandler, includeLogcat, configProvider, DEFAULT_CAPTURE_IP);
206272
}
207273

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

290+
223291
/**
224292
* Construct a new Rollbar instance.
225293
*
@@ -233,6 +301,27 @@ public Rollbar(Context context, String accessToken, String environment, boolean
233301
* @param maxLogcatSize the maximum number of logcat lines to capture with items (ignored unless positive)
234302
*/
235303
public Rollbar(Context context, String accessToken, String environment, boolean registerExceptionHandler, boolean includeLogcat, ConfigProvider configProvider, String captureIp, int maxLogcatSize) {
304+
this(context, accessToken, environment, registerExceptionHandler, includeLogcat, configProvider,
305+
captureIp, maxLogcatSize, DEFAULT_SUSPEND_WHEN_NETWORK_IS_UNAVAILABLE);
306+
}
307+
308+
/**
309+
* Construct a new Rollbar instance.
310+
*
311+
* @param context Android context to use.
312+
* @param accessToken a Rollbar access token with at least post_client_item scope
313+
* @param environment the environment to set for items
314+
* @param registerExceptionHandler whether or not to handle uncaught exceptions.
315+
* @param includeLogcat whether or not to include logcat output with items
316+
* @param configProvider a configuration provider that can be used to customize the configuration further.
317+
* @param captureIp one of: full, anonymize, none. This determines how the remote ip is captured.
318+
* @param maxLogcatSize the maximum number of logcat lines to capture with items (ignored unless positive)
319+
* @param suspendWhenNetworkIsUnavailable if true, sending occurrences will be suspended while the network is unavailable
320+
*/
321+
public Rollbar(Context context, String accessToken, String environment,
322+
boolean registerExceptionHandler, boolean includeLogcat,
323+
ConfigProvider configProvider, String captureIp, int maxLogcatSize,
324+
boolean suspendWhenNetworkIsUnavailable) {
236325
if (accessToken == null) {
237326
try {
238327
accessToken = loadAccessTokenFromManifest(context);
@@ -271,12 +360,20 @@ public Rollbar(Context context, String accessToken, String environment, boolean
271360
.accessToken(accessToken)
272361
.build();
273362

274-
BufferedSender sender = new BufferedSender.Builder()
275-
.queue(queue)
276-
.sender(innerSender)
277-
.initialFlushDelay(TimeUnit.SECONDS.toMillis(DEFAULT_ITEM_SCHEDULE_STARTUP_DELAY))
278-
.flushFreq(TimeUnit.SECONDS.toMillis(DEFAULT_ITEM_SCHEDULE_DELAY))
279-
.build();
363+
BufferedSender.Builder senderBuilder = new BufferedSender.Builder()
364+
.queue(queue)
365+
.sender(innerSender)
366+
.initialFlushDelay(TimeUnit.SECONDS.toMillis(DEFAULT_ITEM_SCHEDULE_STARTUP_DELAY))
367+
.flushFreq(TimeUnit.SECONDS.toMillis(DEFAULT_ITEM_SCHEDULE_DELAY));
368+
369+
if (suspendWhenNetworkIsUnavailable) {
370+
this.senderFailureStrategy = new ConnectionAwareSenderFailureStrategy(context);
371+
senderBuilder.senderFailureStrategy(this.senderFailureStrategy);
372+
} else {
373+
this.senderFailureStrategy = null;
374+
}
375+
376+
BufferedSender sender = senderBuilder.build();
280377

281378
ConfigBuilder defaultConfig = ConfigBuilder.withAccessToken(accessToken)
282379
.client(clientProvider)

0 commit comments

Comments
 (0)