Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Fixes

- Reduce main thread work on init ([#3036](https://github.com/getsentry/sentry-java/pull/3036))
- Move Integrations registration to background on init ([#3043](https://github.com/getsentry/sentry-java/pull/3043))
- Fix SIGSEV, SIGABRT and SIGBUS crashes happening after/around the August Google Play System update, see [#2955](https://github.com/getsentry/sentry-java/issues/2955) for more details (fix provided by Native SDK bump)
- Ensure DSN uses http/https protocol ([#3044](https://github.com/getsentry/sentry-java/pull/3044))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
public final class AnrIntegration implements Integration, Closeable {

private final @NotNull Context context;
private boolean isClosed = false;
private final @NotNull Object startLock = new Object();

public AnrIntegration(final @NotNull Context context) {
this.context = context;
Expand Down Expand Up @@ -55,27 +57,51 @@ private void register(final @NotNull IHub hub, final @NotNull SentryAndroidOptio
.log(SentryLevel.DEBUG, "AnrIntegration enabled: %s", options.isAnrEnabled());

if (options.isAnrEnabled()) {
synchronized (watchDogLock) {
if (anrWatchDog == null) {
options
.getLogger()
.log(
SentryLevel.DEBUG,
"ANR timeout in milliseconds: %d",
options.getAnrTimeoutIntervalMillis());

anrWatchDog =
new ANRWatchDog(
options.getAnrTimeoutIntervalMillis(),
options.isAnrReportInDebug(),
error -> reportANR(hub, options, error),
options.getLogger(),
context);
anrWatchDog.start();

options.getLogger().log(SentryLevel.DEBUG, "AnrIntegration installed.");
addIntegrationToSdkVersion();
}
addIntegrationToSdkVersion();
try {
options
.getExecutorService()
.submit(
() -> {
synchronized (startLock) {
if (!isClosed) {
startAnrWatchdog(hub, options);
}
}
});
} catch (Throwable e) {
options
.getLogger()
.log(
SentryLevel.DEBUG,
"Failed to start AnrIntegration on executor thread. Starting on the calling thread.",
e);
startAnrWatchdog(hub, options);
}
}
}

private void startAnrWatchdog(
final @NotNull IHub hub, final @NotNull SentryAndroidOptions options) {
synchronized (watchDogLock) {
if (anrWatchDog == null) {
options
.getLogger()
.log(
SentryLevel.DEBUG,
"ANR timeout in milliseconds: %d",
options.getAnrTimeoutIntervalMillis());

anrWatchDog =
new ANRWatchDog(
options.getAnrTimeoutIntervalMillis(),
options.isAnrReportInDebug(),
error -> reportANR(hub, options, error),
options.getLogger(),
context);
anrWatchDog.start();

options.getLogger().log(SentryLevel.DEBUG, "AnrIntegration installed.");
}
}
}
Expand Down Expand Up @@ -126,6 +152,9 @@ ANRWatchDog getANRWatchDog() {

@Override
public void close() throws IOException {
synchronized (startLock) {
isClosed = true;
}
synchronized (watchDogLock) {
if (anrWatchDog != null) {
anrWatchDog.interrupt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ public final class PhoneStateBreadcrumbsIntegration implements Integration, Clos
private @Nullable SentryAndroidOptions options;
@TestOnly @Nullable PhoneStateChangeListener listener;
private @Nullable TelephonyManager telephonyManager;
private boolean isClosed = false;
private final @NotNull Object startLock = new Object();

public PhoneStateBreadcrumbsIntegration(final @NotNull Context context) {
this.context = Objects.requireNonNull(context, "Context is required");
}

@SuppressWarnings("deprecation")
@Override
public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) {
Objects.requireNonNull(hub, "Hub is required");
Expand All @@ -46,28 +47,56 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio

if (this.options.isEnableSystemEventBreadcrumbs()
&& Permissions.hasPermission(context, READ_PHONE_STATE)) {
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null) {
try {
listener = new PhoneStateChangeListener(hub);
telephonyManager.listen(listener, android.telephony.PhoneStateListener.LISTEN_CALL_STATE);

options.getLogger().log(SentryLevel.DEBUG, "PhoneStateBreadcrumbsIntegration installed.");
addIntegrationToSdkVersion();
} catch (Throwable e) {
this.options
.getLogger()
.log(SentryLevel.INFO, e, "TelephonyManager is not available or ready to use.");
}
} else {
this.options.getLogger().log(SentryLevel.INFO, "TelephonyManager is not available");
try {
options
.getExecutorService()
.submit(
() -> {
synchronized (startLock) {
if (!isClosed) {
startTelephonyListener(hub, options);
}
}
});
} catch (Throwable e) {
options
.getLogger()
.log(
SentryLevel.DEBUG,
"Failed to start PhoneStateBreadcrumbsIntegration on executor thread. Starting on the calling thread.",
e);
startTelephonyListener(hub, options);
}
}
}

@SuppressWarnings("deprecation")
private void startTelephonyListener(
final @NotNull IHub hub, final @NotNull SentryOptions options) {
telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null) {
try {
listener = new PhoneStateChangeListener(hub);
telephonyManager.listen(listener, android.telephony.PhoneStateListener.LISTEN_CALL_STATE);

options.getLogger().log(SentryLevel.DEBUG, "PhoneStateBreadcrumbsIntegration installed.");
addIntegrationToSdkVersion();
} catch (Throwable e) {
options
.getLogger()
.log(SentryLevel.INFO, e, "TelephonyManager is not available or ready to use.");
}
} else {
options.getLogger().log(SentryLevel.INFO, "TelephonyManager is not available");
}
}

@SuppressWarnings("deprecation")
@Override
public void close() throws IOException {
synchronized (startLock) {
isClosed = true;
}
if (telephonyManager != null && listener != null) {
telephonyManager.listen(listener, android.telephony.PhoneStateListener.LISTEN_NONE);
listener = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public final class SystemEventsBreadcrumbsIntegration implements Integration, Cl
private @Nullable SentryAndroidOptions options;

private final @NotNull List<String> actions;
private boolean isClosed = false;
private final @NotNull Object startLock = new Object();

public SystemEventsBreadcrumbsIntegration(final @NotNull Context context) {
this(context, getDefaultActions());
Expand Down Expand Up @@ -92,27 +94,50 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
this.options.isEnableSystemEventBreadcrumbs());

if (this.options.isEnableSystemEventBreadcrumbs()) {
receiver = new SystemEventsBroadcastReceiver(hub, this.options.getLogger());
final IntentFilter filter = new IntentFilter();
for (String item : actions) {
filter.addAction(item);
}

try {
// registerReceiver can throw SecurityException but it's not documented in the official docs
ContextUtils.registerReceiver(context, options, receiver, filter);
this.options
.getLogger()
.log(SentryLevel.DEBUG, "SystemEventsBreadcrumbsIntegration installed.");
addIntegrationToSdkVersion();
options
.getExecutorService()
.submit(
() -> {
synchronized (startLock) {
if (!isClosed) {
startSystemEventsReceiver(hub, (SentryAndroidOptions) options);
}
}
});
} catch (Throwable e) {
this.options.setEnableSystemEventBreadcrumbs(false);
this.options
options
.getLogger()
.log(SentryLevel.ERROR, "Failed to initialize SystemEventsBreadcrumbsIntegration.", e);
.log(
SentryLevel.DEBUG,
"Failed to start SystemEventsBreadcrumbsIntegration on executor thread. Starting on the calling thread.",
e);
startSystemEventsReceiver(hub, (SentryAndroidOptions) options);
}
}
}

private void startSystemEventsReceiver(
final @NotNull IHub hub, final @NotNull SentryAndroidOptions options) {
receiver = new SystemEventsBroadcastReceiver(hub, options.getLogger());
final IntentFilter filter = new IntentFilter();
for (String item : actions) {
filter.addAction(item);
}
try {
// registerReceiver can throw SecurityException but it's not documented in the official docs
ContextUtils.registerReceiver(context, options, receiver, filter);
options.getLogger().log(SentryLevel.DEBUG, "SystemEventsBreadcrumbsIntegration installed.");
addIntegrationToSdkVersion();
} catch (Throwable e) {
options.setEnableSystemEventBreadcrumbs(false);
options
.getLogger()
.log(SentryLevel.ERROR, "Failed to initialize SystemEventsBreadcrumbsIntegration.", e);
}
}

@SuppressWarnings("deprecation")
private static @NotNull List<String> getDefaultActions() {
final List<String> actions = new ArrayList<>();
Expand Down Expand Up @@ -164,6 +189,9 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio

@Override
public void close() throws IOException {
synchronized (startLock) {
isClosed = true;
}
if (receiver != null) {
context.unregisterReceiver(receiver);
receiver = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ public final class TempSensorBreadcrumbsIntegration
private @Nullable SentryAndroidOptions options;

@TestOnly @Nullable SensorManager sensorManager;
private boolean isClosed = false;
private final @NotNull Object startLock = new Object();

public TempSensorBreadcrumbsIntegration(final @NotNull Context context) {
this.context = Objects.requireNonNull(context, "Context is required");
}

@SuppressWarnings("deprecation")
@Override
public void register(final @NotNull IHub hub, final @NotNull SentryOptions options) {
this.hub = Objects.requireNonNull(hub, "Hub is required");
Expand All @@ -51,34 +52,57 @@ public void register(final @NotNull IHub hub, final @NotNull SentryOptions optio
this.options.isEnableSystemEventBreadcrumbs());

if (this.options.isEnableSystemEventBreadcrumbs()) {

try {
sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
if (sensorManager != null) {
final Sensor defaultSensor =
sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
if (defaultSensor != null) {
sensorManager.registerListener(this, defaultSensor, SensorManager.SENSOR_DELAY_NORMAL);

options
.getLogger()
.log(SentryLevel.DEBUG, "TempSensorBreadcrumbsIntegration installed.");
addIntegrationToSdkVersion();
} else {
this.options
.getLogger()
.log(SentryLevel.INFO, "TYPE_AMBIENT_TEMPERATURE is not available.");
}
options
.getExecutorService()
.submit(
() -> {
synchronized (startLock) {
if (!isClosed) {
startSensorListener(options);
}
}
});
} catch (Throwable e) {
options
.getLogger()
.log(
SentryLevel.DEBUG,
"Failed to start TempSensorBreadcrumbsIntegration on executor thread. Starting on the calling thread.",
e);
startSensorListener(options);
}
}
}

private void startSensorListener(final @NotNull SentryOptions options) {
try {
sensorManager = (SensorManager) context.getSystemService(SENSOR_SERVICE);
if (sensorManager != null) {
final Sensor defaultSensor =
sensorManager.getDefaultSensor(Sensor.TYPE_AMBIENT_TEMPERATURE);
if (defaultSensor != null) {
sensorManager.registerListener(this, defaultSensor, SensorManager.SENSOR_DELAY_NORMAL);

options.getLogger().log(SentryLevel.DEBUG, "TempSensorBreadcrumbsIntegration installed.");
addIntegrationToSdkVersion();
} else {
this.options.getLogger().log(SentryLevel.INFO, "SENSOR_SERVICE is not available.");
options.getLogger().log(SentryLevel.INFO, "TYPE_AMBIENT_TEMPERATURE is not available.");
}
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Failed to init. the SENSOR_SERVICE.");
} else {
options.getLogger().log(SentryLevel.INFO, "SENSOR_SERVICE is not available.");
}
} catch (Throwable e) {
options.getLogger().log(SentryLevel.ERROR, e, "Failed to init. the SENSOR_SERVICE.");
}
}

@Override
public void close() throws IOException {
synchronized (startLock) {
isClosed = true;
}
if (sensorManager != null) {
sensorManager.unregisterListener(this);
sensorManager = null;
Expand Down
Loading