-
-
Notifications
You must be signed in to change notification settings - Fork 426
/
ManifestMetadataReader.java
420 lines (352 loc) · 16.2 KB
/
ManifestMetadataReader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package io.sentry.android.core;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import io.sentry.ILogger;
import io.sentry.SentryLevel;
import io.sentry.protocol.SdkVersion;
import io.sentry.util.Objects;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/** Class responsible for reading values from manifest and setting them to the options */
final class ManifestMetadataReader {
static final String DSN = "io.sentry.dsn";
static final String DEBUG = "io.sentry.debug";
static final String DEBUG_LEVEL = "io.sentry.debug.level";
static final String SAMPLE_RATE = "io.sentry.sample-rate";
static final String ANR_ENABLE = "io.sentry.anr.enable";
static final String ANR_REPORT_DEBUG = "io.sentry.anr.report-debug";
static final String ANR_TIMEOUT_INTERVAL_MILLIS = "io.sentry.anr.timeout-interval-millis";
static final String AUTO_INIT = "io.sentry.auto-init";
static final String NDK_ENABLE = "io.sentry.ndk.enable";
static final String NDK_SCOPE_SYNC_ENABLE = "io.sentry.ndk.scope-sync.enable";
static final String RELEASE = "io.sentry.release";
static final String ENVIRONMENT = "io.sentry.environment";
static final String SDK_NAME = "io.sentry.sdk.name";
static final String SDK_VERSION = "io.sentry.sdk.version";
// TODO: remove on 6.x in favor of SESSION_AUTO_TRACKING_ENABLE
static final String SESSION_TRACKING_ENABLE = "io.sentry.session-tracking.enable";
static final String AUTO_SESSION_TRACKING_ENABLE = "io.sentry.auto-session-tracking.enable";
static final String SESSION_TRACKING_TIMEOUT_INTERVAL_MILLIS =
"io.sentry.session-tracking.timeout-interval-millis";
static final String BREADCRUMBS_ACTIVITY_LIFECYCLE_ENABLE =
"io.sentry.breadcrumbs.activity-lifecycle";
static final String BREADCRUMBS_APP_LIFECYCLE_ENABLE = "io.sentry.breadcrumbs.app-lifecycle";
static final String BREADCRUMBS_SYSTEM_EVENTS_ENABLE = "io.sentry.breadcrumbs.system-events";
static final String BREADCRUMBS_APP_COMPONENTS_ENABLE = "io.sentry.breadcrumbs.app-components";
static final String BREADCRUMBS_USER_INTERACTION_ENABLE =
"io.sentry.breadcrumbs.user-interaction";
static final String UNCAUGHT_EXCEPTION_HANDLER_ENABLE =
"io.sentry.uncaught-exception-handler.enable";
static final String TRACES_SAMPLE_RATE = "io.sentry.traces.sample-rate";
static final String TRACES_ACTIVITY_ENABLE = "io.sentry.traces.activity.enable";
static final String TRACES_ACTIVITY_AUTO_FINISH_ENABLE =
"io.sentry.traces.activity.auto-finish.enable";
static final String TRACES_UI_ENABLE = "io.sentry.traces.user-interaction.enable";
static final String TRACES_PROFILING_ENABLE = "io.sentry.traces.profiling.enable";
static final String PROFILES_SAMPLE_RATE = "io.sentry.traces.profiling.sample-rate";
@ApiStatus.Experimental static final String TRACE_SAMPLING = "io.sentry.traces.trace-sampling";
// TODO: remove in favor of TRACE_PROPAGATION_TARGETS
@Deprecated static final String TRACING_ORIGINS = "io.sentry.traces.tracing-origins";
static final String TRACE_PROPAGATION_TARGETS = "io.sentry.traces.trace-propagation-targets";
static final String ATTACH_THREADS = "io.sentry.attach-threads";
static final String PROGUARD_UUID = "io.sentry.proguard-uuid";
static final String IDLE_TIMEOUT = "io.sentry.traces.idle-timeout";
static final String ATTACH_SCREENSHOT = "io.sentry.attach-screenshot";
static final String CLIENT_REPORTS_ENABLE = "io.sentry.send-client-reports";
static final String COLLECT_ADDITIONAL_CONTEXT = "io.sentry.additional-context";
static final String SEND_DEFAULT_PII = "io.sentry.send-default-pii";
/** ManifestMetadataReader ctor */
private ManifestMetadataReader() {}
/**
* Reads configurations from Manifest and sets it to the options
*
* @param context the application context
* @param options the SentryAndroidOptions
*/
@SuppressWarnings("deprecation")
static void applyMetadata(
final @NotNull Context context,
final @NotNull SentryAndroidOptions options,
final @NotNull BuildInfoProvider buildInfoProvider) {
Objects.requireNonNull(context, "The application context is required.");
Objects.requireNonNull(options, "The options object is required.");
try {
final Bundle metadata = getMetadata(context, options.getLogger(), buildInfoProvider);
final ILogger logger = options.getLogger();
if (metadata != null) {
options.setDebug(readBool(metadata, logger, DEBUG, options.isDebug()));
if (options.isDebug()) {
final String level =
readString(
metadata,
logger,
DEBUG_LEVEL,
options.getDiagnosticLevel().name().toLowerCase(Locale.ROOT));
if (level != null) {
options.setDiagnosticLevel(SentryLevel.valueOf(level.toUpperCase(Locale.ROOT)));
}
}
options.setAnrEnabled(readBool(metadata, logger, ANR_ENABLE, options.isAnrEnabled()));
// deprecated
final boolean enableSessionTracking =
readBool(
metadata, logger, SESSION_TRACKING_ENABLE, options.isEnableAutoSessionTracking());
// use enableAutoSessionTracking as fallback
options.setEnableAutoSessionTracking(
readBool(metadata, logger, AUTO_SESSION_TRACKING_ENABLE, enableSessionTracking));
if (options.getSampleRate() == null) {
final Double sampleRate = readDouble(metadata, logger, SAMPLE_RATE);
if (sampleRate != -1) {
options.setSampleRate(sampleRate);
}
}
options.setAnrReportInDebug(
readBool(metadata, logger, ANR_REPORT_DEBUG, options.isAnrReportInDebug()));
options.setAnrTimeoutIntervalMillis(
readLong(
metadata,
logger,
ANR_TIMEOUT_INTERVAL_MILLIS,
options.getAnrTimeoutIntervalMillis()));
final String dsn = readString(metadata, logger, DSN, options.getDsn());
if (dsn == null) {
options
.getLogger()
.log(SentryLevel.FATAL, "DSN is required. Use empty string to disable SDK.");
} else if (dsn.isEmpty()) {
options.getLogger().log(SentryLevel.DEBUG, "DSN is empty, disabling sentry-android");
}
options.setDsn(dsn);
options.setEnableNdk(readBool(metadata, logger, NDK_ENABLE, options.isEnableNdk()));
options.setEnableScopeSync(
readBool(metadata, logger, NDK_SCOPE_SYNC_ENABLE, options.isEnableScopeSync()));
options.setRelease(readString(metadata, logger, RELEASE, options.getRelease()));
options.setEnvironment(readString(metadata, logger, ENVIRONMENT, options.getEnvironment()));
options.setSessionTrackingIntervalMillis(
readLong(
metadata,
logger,
SESSION_TRACKING_TIMEOUT_INTERVAL_MILLIS,
options.getSessionTrackingIntervalMillis()));
options.setEnableActivityLifecycleBreadcrumbs(
readBool(
metadata,
logger,
BREADCRUMBS_ACTIVITY_LIFECYCLE_ENABLE,
options.isEnableActivityLifecycleBreadcrumbs()));
options.setEnableAppLifecycleBreadcrumbs(
readBool(
metadata,
logger,
BREADCRUMBS_APP_LIFECYCLE_ENABLE,
options.isEnableAppComponentBreadcrumbs()));
options.setEnableSystemEventBreadcrumbs(
readBool(
metadata,
logger,
BREADCRUMBS_SYSTEM_EVENTS_ENABLE,
options.isEnableSystemEventBreadcrumbs()));
options.setEnableAppComponentBreadcrumbs(
readBool(
metadata,
logger,
BREADCRUMBS_APP_COMPONENTS_ENABLE,
options.isEnableAppComponentBreadcrumbs()));
options.setEnableUserInteractionBreadcrumbs(
readBool(
metadata,
logger,
BREADCRUMBS_USER_INTERACTION_ENABLE,
options.isEnableUserInteractionBreadcrumbs()));
options.setEnableUncaughtExceptionHandler(
readBool(
metadata,
logger,
UNCAUGHT_EXCEPTION_HANDLER_ENABLE,
options.isEnableUncaughtExceptionHandler()));
options.setAttachThreads(
readBool(metadata, logger, ATTACH_THREADS, options.isAttachThreads()));
options.setAttachScreenshot(
readBool(metadata, logger, ATTACH_SCREENSHOT, options.isAttachScreenshot()));
options.setSendClientReports(
readBool(metadata, logger, CLIENT_REPORTS_ENABLE, options.isSendClientReports()));
options.setCollectAdditionalContext(
readBool(
metadata,
logger,
COLLECT_ADDITIONAL_CONTEXT,
options.isCollectAdditionalContext()));
if (options.getTracesSampleRate() == null) {
final Double tracesSampleRate = readDouble(metadata, logger, TRACES_SAMPLE_RATE);
if (tracesSampleRate != -1) {
options.setTracesSampleRate(tracesSampleRate);
}
}
options.setTraceSampling(
readBool(metadata, logger, TRACE_SAMPLING, options.isTraceSampling()));
options.setEnableAutoActivityLifecycleTracing(
readBool(
metadata,
logger,
TRACES_ACTIVITY_ENABLE,
options.isEnableAutoActivityLifecycleTracing()));
options.setEnableActivityLifecycleTracingAutoFinish(
readBool(
metadata,
logger,
TRACES_ACTIVITY_AUTO_FINISH_ENABLE,
options.isEnableActivityLifecycleTracingAutoFinish()));
options.setProfilingEnabled(
readBool(metadata, logger, TRACES_PROFILING_ENABLE, options.isProfilingEnabled()));
if (options.getProfilesSampleRate() == null) {
final Double profilesSampleRate = readDouble(metadata, logger, PROFILES_SAMPLE_RATE);
if (profilesSampleRate != -1) {
options.setProfilesSampleRate(profilesSampleRate);
}
}
options.setEnableUserInteractionTracing(
readBool(metadata, logger, TRACES_UI_ENABLE, options.isEnableUserInteractionTracing()));
final long idleTimeout = readLong(metadata, logger, IDLE_TIMEOUT, -1);
if (idleTimeout != -1) {
options.setIdleTimeout(idleTimeout);
}
@Nullable
List<String> tracePropagationTargets =
readList(metadata, logger, TRACE_PROPAGATION_TARGETS);
// TODO remove once TRACING_ORIGINS have been removed
if (!metadata.containsKey(TRACE_PROPAGATION_TARGETS)
&& (tracePropagationTargets == null || tracePropagationTargets.isEmpty())) {
tracePropagationTargets = readList(metadata, logger, TRACING_ORIGINS);
}
if ((metadata.containsKey(TRACE_PROPAGATION_TARGETS)
|| metadata.containsKey(TRACING_ORIGINS))
&& tracePropagationTargets == null) {
options.setTracePropagationTargets(Collections.emptyList());
} else if (tracePropagationTargets != null) {
options.setTracePropagationTargets(tracePropagationTargets);
}
options.setProguardUuid(
readString(metadata, logger, PROGUARD_UUID, options.getProguardUuid()));
SdkVersion sdkInfo = options.getSdkVersion();
if (sdkInfo == null) {
// Is already set by the Options constructor, let's use an empty default otherwise.
sdkInfo = new SdkVersion("", "");
}
sdkInfo.setName(readStringNotNull(metadata, logger, SDK_NAME, sdkInfo.getName()));
sdkInfo.setVersion(readStringNotNull(metadata, logger, SDK_VERSION, sdkInfo.getVersion()));
options.setSdkVersion(sdkInfo);
options.setSendDefaultPii(
readBool(metadata, logger, SEND_DEFAULT_PII, options.isSendDefaultPii()));
}
options
.getLogger()
.log(SentryLevel.INFO, "Retrieving configuration from AndroidManifest.xml");
} catch (Throwable e) {
options
.getLogger()
.log(
SentryLevel.ERROR, "Failed to read configuration from android manifest metadata.", e);
}
}
private static boolean readBool(
final @NotNull Bundle metadata,
final @NotNull ILogger logger,
final @NotNull String key,
final boolean defaultValue) {
final boolean value = metadata.getBoolean(key, defaultValue);
logger.log(SentryLevel.DEBUG, "%s read: %s", key, value);
return value;
}
private static @Nullable String readString(
final @NotNull Bundle metadata,
final @NotNull ILogger logger,
final @NotNull String key,
final @Nullable String defaultValue) {
final String value = metadata.getString(key, defaultValue);
logger.log(SentryLevel.DEBUG, "%s read: %s", key, value);
return value;
}
private static @NotNull String readStringNotNull(
final @NotNull Bundle metadata,
final @NotNull ILogger logger,
final @NotNull String key,
final @NotNull String defaultValue) {
final String value = metadata.getString(key, defaultValue);
logger.log(SentryLevel.DEBUG, "%s read: %s", key, value);
return value;
}
private static @Nullable List<String> readList(
final @NotNull Bundle metadata, final @NotNull ILogger logger, final @NotNull String key) {
final String value = metadata.getString(key);
logger.log(SentryLevel.DEBUG, "%s read: %s", key, value);
if (value != null) {
return Arrays.asList(value.split(",", -1));
} else {
return null;
}
}
private static @NotNull Double readDouble(
final @NotNull Bundle metadata, final @NotNull ILogger logger, final @NotNull String key) {
// manifest meta-data only reads float
final Double value = ((Float) metadata.getFloat(key, -1)).doubleValue();
logger.log(SentryLevel.DEBUG, "%s read: %s", key, value);
return value;
}
private static long readLong(
final @NotNull Bundle metadata,
final @NotNull ILogger logger,
final @NotNull String key,
final long defaultValue) {
// manifest meta-data only reads int if the value is not big enough
final long value = metadata.getInt(key, (int) defaultValue);
logger.log(SentryLevel.DEBUG, "%s read: %s", key, value);
return value;
}
/**
* Checks if auto init is enabled or disabled
*
* @param context the application context
* @param logger the Logger interface
* @return true if auto init is enabled or false otherwise
*/
static boolean isAutoInit(final @NotNull Context context, final @NotNull ILogger logger) {
Objects.requireNonNull(context, "The application context is required.");
boolean autoInit = true;
try {
final Bundle metadata = getMetadata(context, logger, null);
if (metadata != null) {
autoInit = readBool(metadata, logger, AUTO_INIT, true);
}
logger.log(SentryLevel.INFO, "Retrieving auto-init from AndroidManifest.xml");
} catch (Throwable e) {
logger.log(SentryLevel.ERROR, "Failed to read auto-init from android manifest metadata.", e);
}
return autoInit;
}
/**
* Returns the Bundle attached from the given Context
*
* @param context the application context
* @return the Bundle attached to the PackageManager
* @throws PackageManager.NameNotFoundException if the package name is non-existent
*/
private static @Nullable Bundle getMetadata(
final @NotNull Context context,
final @NotNull ILogger logger,
final @Nullable BuildInfoProvider buildInfoProvider)
throws PackageManager.NameNotFoundException {
final ApplicationInfo app =
ContextUtils.getApplicationInfo(
context,
PackageManager.GET_META_DATA,
buildInfoProvider != null ? buildInfoProvider : new BuildInfoProvider(logger));
return app.metaData;
}
}