Skip to content

Commit 18a3504

Browse files
committed
fix(android): Keep the no-arg AppLifecycleIntegration constructor as deprecated
1 parent 7f86c34 commit 18a3504

3 files changed

Lines changed: 43 additions & 5 deletions

File tree

‎sentry-android-core/api/sentry-android-core.api‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ public final class io/sentry/android/core/AppComponentsBreadcrumbsIntegration :
180180
}
181181

182182
public final class io/sentry/android/core/AppLifecycleIntegration : io/sentry/Integration, java/io/Closeable {
183+
public fun <init> ()V
183184
public fun <init> (Landroid/content/Context;Lio/sentry/android/core/BuildInfoProvider;)V
184185
public fun close ()V
185186
public fun register (Lio/sentry/IScopes;Lio/sentry/SentryOptions;)V

‎sentry-android-core/src/main/java/io/sentry/android/core/AppLifecycleIntegration.java‎

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,22 @@ public final class AppLifecycleIntegration implements Integration, Closeable {
4343

4444
private @Nullable SentryAndroidOptions options;
4545

46-
private final @NotNull Context context;
47-
private final @NotNull BuildInfoProvider buildInfoProvider;
46+
// Both null when created through the deprecated no-arg constructor, which skips ANR warnings.
47+
private final @Nullable Context context;
48+
private final @Nullable BuildInfoProvider buildInfoProvider;
4849
private @Nullable Consumer<AnrWarningResult> anrWarningConsumer;
4950
private boolean isClosed;
5051

52+
/**
53+
* @deprecated Without a {@link Context} the integration cannot listen for ANR warnings. Use
54+
* {@link #AppLifecycleIntegration(Context, BuildInfoProvider)} instead.
55+
*/
56+
@Deprecated
57+
public AppLifecycleIntegration() {
58+
this.context = null;
59+
this.buildInfoProvider = null;
60+
}
61+
5162
public AppLifecycleIntegration(
5263
final @NotNull Context context, final @NotNull BuildInfoProvider buildInfoProvider) {
5364
this.context = context.getApplicationContext();
@@ -128,6 +139,13 @@ private void removeObservers() {
128139
*/
129140
private void scheduleRegisterAnrWarningListener(
130141
final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) {
142+
final @Nullable Context context = this.context;
143+
if (context == null || buildInfoProvider == null) {
144+
options
145+
.getLogger()
146+
.log(SentryLevel.DEBUG, "ANR warning listener needs a Context, not installed.");
147+
return;
148+
}
131149
if (buildInfoProvider.getSdkInfoVersion() < Build.VERSION_CODES.CINNAMON_BUN) {
132150
options
133151
.getLogger()
@@ -136,7 +154,9 @@ private void scheduleRegisterAnrWarningListener(
136154
}
137155

138156
try {
139-
options.getExecutorService().submit(() -> registerAnrWarningListener(scopes, options));
157+
options
158+
.getExecutorService()
159+
.submit(() -> registerAnrWarningListener(context, scopes, options));
140160
} catch (RejectedExecutionException e) {
141161
options
142162
.getLogger()
@@ -171,7 +191,9 @@ private void scheduleUnregisterAnrWarningListener() {
171191
// before this ever runs. The suppression is scoped so that register() stays lint-checked.
172192
@SuppressLint("NewApi")
173193
private void registerAnrWarningListener(
174-
final @NotNull IScopes scopes, final @NotNull SentryAndroidOptions options) {
194+
final @NotNull Context context,
195+
final @NotNull IScopes scopes,
196+
final @NotNull SentryAndroidOptions options) {
175197
final @Nullable ActivityManager activityManager =
176198
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
177199
if (activityManager == null) {
@@ -216,7 +238,8 @@ private void unregisterAnrWarningListener(final @NotNull SentryAndroidOptions op
216238
anrWarningConsumer = null;
217239
}
218240

219-
if (consumerRef == null) {
241+
final @Nullable Context context = this.context;
242+
if (consumerRef == null || context == null) {
220243
return;
221244
}
222245

‎sentry-android-core/src/test/java/io/sentry/android/core/AppLifecycleIntegrationTest.kt‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ class AppLifecycleIntegrationTest {
143143
verify(activityManager, never()).registerAnrWarningListener(any(), any())
144144
}
145145

146+
@Test
147+
@Config(sdk = [37])
148+
fun `the deprecated no-arg constructor installs the integration without the ANR warning listener`() {
149+
@Suppress("DEPRECATION") val sut = AppLifecycleIntegration()
150+
151+
sut.register(fixture.scopes, fixture.options)
152+
153+
assertNotNull(sut.watcher)
154+
155+
sut.close()
156+
157+
assertNull(sut.watcher)
158+
}
159+
146160
@Test
147161
@Config(sdk = [37])
148162
fun `ANR warning listener is not registered below API 37`() {

0 commit comments

Comments
 (0)