|
10 | 10 | import android.os.Bundle; |
11 | 11 |
|
12 | 12 | import android.util.Log; |
| 13 | + |
| 14 | +import com.rollbar.android.anr.AnrDetector; |
| 15 | +import com.rollbar.android.anr.AnrDetectorFactory; |
| 16 | +import com.rollbar.android.anr.AnrException; |
13 | 17 | import com.rollbar.android.notifier.sender.ConnectionAwareSenderFailureStrategy; |
14 | 18 | import com.rollbar.android.provider.ClientProvider; |
15 | 19 | import com.rollbar.api.payload.data.TelemetryType; |
| 20 | +import com.rollbar.api.payload.data.body.RollbarThread; |
16 | 21 | import com.rollbar.notifier.config.ConfigProvider; |
17 | 22 | import com.rollbar.notifier.uncaughtexception.RollbarUncaughtExceptionHandler; |
18 | 23 | import com.rollbar.android.provider.NotifierProvider; |
|
24 | 29 | import com.rollbar.notifier.sender.SyncSender; |
25 | 30 | import com.rollbar.notifier.sender.queue.DiskQueue; |
26 | 31 | import com.rollbar.notifier.util.ObjectsUtils; |
| 32 | +import com.rollbar.notifier.wrapper.RollbarThrowableWrapper; |
| 33 | +import com.rollbar.notifier.wrapper.ThrowableWrapper; |
| 34 | + |
| 35 | +import org.slf4j.LoggerFactory; |
27 | 36 |
|
28 | 37 | import java.io.Closeable; |
29 | 38 | import java.io.File; |
30 | 39 | import java.io.IOException; |
31 | 40 | import java.lang.Thread.UncaughtExceptionHandler; |
32 | 41 | import java.util.Collections; |
| 42 | +import java.util.HashMap; |
| 43 | +import java.util.List; |
33 | 44 | import java.util.Map; |
34 | 45 | import java.util.concurrent.TimeUnit; |
35 | 46 |
|
@@ -71,6 +82,28 @@ public static Rollbar init(Context context) { |
71 | 82 | return init(context, null, null); |
72 | 83 | } |
73 | 84 |
|
| 85 | + /** |
| 86 | + * Initialize the singleton instance of Rollbar. |
| 87 | + * Defaults to reading the access token from the manifest, handling uncaught exceptions, and setting |
| 88 | + * the environment to production. |
| 89 | + * |
| 90 | + * @param context Android context to use. |
| 91 | + * @param androidConfiguration configuration for Android features. |
| 92 | + * @return the managed instance of Rollbar. |
| 93 | + */ |
| 94 | + public static Rollbar init(Context context, AndroidConfiguration androidConfiguration) { |
| 95 | + return init( |
| 96 | + context, |
| 97 | + null, |
| 98 | + DEFAULT_ENVIRONMENT, |
| 99 | + DEFAULT_REGISTER_EXCEPTION_HANDLER, |
| 100 | + DEFAULT_INCLUDE_LOGCAT, |
| 101 | + DEFAULT_CONFIG_PROVIDER, |
| 102 | + DEFAULT_SUSPEND_WHEN_NETWORK_IS_UNAVAILABLE, |
| 103 | + androidConfiguration |
| 104 | + ); |
| 105 | + } |
| 106 | + |
74 | 107 | /** |
75 | 108 | * Initialize the singleton instance of Rollbar. |
76 | 109 | * |
@@ -155,18 +188,71 @@ public static Rollbar init(Context context, String accessToken, String environme |
155 | 188 | public static Rollbar init(Context context, String accessToken, String environment, |
156 | 189 | boolean registerExceptionHandler, boolean includeLogcat, |
157 | 190 | ConfigProvider provider, boolean suspendWhenNetworkIsUnavailable) { |
| 191 | + return init( |
| 192 | + context, |
| 193 | + accessToken, |
| 194 | + environment, |
| 195 | + registerExceptionHandler, |
| 196 | + includeLogcat, |
| 197 | + provider, |
| 198 | + suspendWhenNetworkIsUnavailable, |
| 199 | + makeDefaultAndroidConfiguration() |
| 200 | + ); |
| 201 | + } |
| 202 | + |
| 203 | + /** |
| 204 | + * Initialize the singleton instance of Rollbar. |
| 205 | + * |
| 206 | + * @param context Android context to use. |
| 207 | + * @param accessToken a Rollbar access token with at least post_client_item scope |
| 208 | + * @param environment the environment to set for items |
| 209 | + * @param registerExceptionHandler whether or not to handle uncaught exceptions. |
| 210 | + * @param includeLogcat whether or not to include logcat output with items |
| 211 | + * @param provider a configuration provider that can be used to customize the configuration further. |
| 212 | + * @param suspendWhenNetworkIsUnavailable if true, sending occurrences will be suspended while the network is unavailable |
| 213 | + * @param androidConfiguration configuration for Android features |
| 214 | + * @return the managed instance of Rollbar. |
| 215 | + */ |
| 216 | + public static Rollbar init( |
| 217 | + Context context, |
| 218 | + String accessToken, |
| 219 | + String environment, |
| 220 | + boolean registerExceptionHandler, |
| 221 | + boolean includeLogcat, |
| 222 | + ConfigProvider provider, |
| 223 | + boolean suspendWhenNetworkIsUnavailable, |
| 224 | + AndroidConfiguration androidConfiguration |
| 225 | + ) { |
158 | 226 | if (isInit()) { |
159 | 227 | Log.w(TAG, "Rollbar.init() called when it was already initialized."); |
160 | 228 | // This is likely an activity that was destroyed and recreated, so we need to update it |
161 | 229 | notifier.updateContext(context); |
162 | 230 | } else { |
163 | 231 | notifier = new Rollbar(context, accessToken, environment, registerExceptionHandler, |
164 | | - includeLogcat, provider, DEFAULT_CAPTURE_IP, DEFAULT_MAX_LOGCAT_SIZE, |
165 | | - suspendWhenNetworkIsUnavailable); |
| 232 | + includeLogcat, provider, DEFAULT_CAPTURE_IP, DEFAULT_MAX_LOGCAT_SIZE, |
| 233 | + suspendWhenNetworkIsUnavailable); |
| 234 | + if (androidConfiguration != null) { |
| 235 | + initAnrDetector(context, androidConfiguration); |
| 236 | + } |
166 | 237 | } |
| 238 | + |
167 | 239 | return notifier; |
168 | 240 | } |
169 | 241 |
|
| 242 | + private static void initAnrDetector( |
| 243 | + Context context, |
| 244 | + AndroidConfiguration androidConfiguration |
| 245 | + ) { |
| 246 | + AnrDetector anrDetector = AnrDetectorFactory.create( |
| 247 | + context, |
| 248 | + LoggerFactory.getLogger(AnrDetectorFactory.class), |
| 249 | + androidConfiguration.getAnrConfiguration(), |
| 250 | + error -> reportANR(error)); |
| 251 | + if (anrDetector != null) { |
| 252 | + anrDetector.init(); |
| 253 | + } |
| 254 | + } |
| 255 | + |
170 | 256 | private void updateContext(Context context) { |
171 | 257 | if (this.senderFailureStrategy != null) { |
172 | 258 | this.senderFailureStrategy.updateContext(context); |
@@ -836,6 +922,24 @@ public void log(Throwable error, Map<String, Object> custom, Level level) { |
836 | 922 | log(error, custom, null, level); |
837 | 923 | } |
838 | 924 |
|
| 925 | + /** |
| 926 | + * Record an error or message with extra data at the level specified. At least one of `error` or |
| 927 | + * `description` must be non-null. If error is null, `description` will be sent as a message. If |
| 928 | + * error is non-null, description will be sent as the description of the error. Custom data will |
| 929 | + * be attached to message if the error is null. Custom data will extend whatever {@link |
| 930 | + * Config#custom} returns. |
| 931 | + * |
| 932 | + * @param error the error (if any). |
| 933 | + * @param custom the custom data (if any). |
| 934 | + * @param description the description of the error, or the message to send. |
| 935 | + * @param level the level to send it at. |
| 936 | + * @param isUncaught whether this data comes from an uncaught exception. |
| 937 | + */ |
| 938 | + public void log(ThrowableWrapper error, Map<String, Object> custom, String description, |
| 939 | + Level level, boolean isUncaught) { |
| 940 | + rollbar.log(error, custom, description, level, isUncaught); |
| 941 | + } |
| 942 | + |
839 | 943 | /** |
840 | 944 | * Log an error at level specified. |
841 | 945 | * |
@@ -1086,4 +1190,22 @@ private static void ensureInit(Runnable runnable) { |
1086 | 1190 | } |
1087 | 1191 | } |
1088 | 1192 |
|
| 1193 | + private static void reportANR(AnrException error){ |
| 1194 | + List<RollbarThread> rollbarThreads = error.getThreads(); |
| 1195 | + |
| 1196 | + ThrowableWrapper throwableWrapper; |
| 1197 | + |
| 1198 | + if (rollbarThreads == null) { |
| 1199 | + throwableWrapper = new RollbarThrowableWrapper(error); |
| 1200 | + } else { |
| 1201 | + throwableWrapper = new RollbarThrowableWrapper(error, rollbarThreads); |
| 1202 | + } |
| 1203 | + |
| 1204 | + notifier.log(throwableWrapper, new HashMap<>(), "ANR", Level.CRITICAL, false); |
| 1205 | + } |
| 1206 | + |
| 1207 | + private static AndroidConfiguration makeDefaultAndroidConfiguration() { |
| 1208 | + return new AndroidConfiguration.Builder().build(); |
| 1209 | + } |
| 1210 | + |
1089 | 1211 | } |
0 commit comments