From 564224b5ce83c2c939278a4c389aa9b32d356d52 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ricau Date: Thu, 10 Nov 2022 11:42:11 -0800 Subject: [PATCH] Add support for disabling LeakCanary notifications Fixes #2394 Fixes #2398 --- .../src/main/java/leakcanary/LeakCanary.kt | 18 +++++++++++++++++- .../java/leakcanary/internal/Notifications.kt | 4 +++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/leakcanary-android-core/src/main/java/leakcanary/LeakCanary.kt b/leakcanary-android-core/src/main/java/leakcanary/LeakCanary.kt index b7965f6089..96a27ef6bc 100644 --- a/leakcanary-android-core/src/main/java/leakcanary/LeakCanary.kt +++ b/leakcanary-android-core/src/main/java/leakcanary/LeakCanary.kt @@ -208,6 +208,15 @@ object LeakCanary { } ), + /** + * Whether to show LeakCanary notifications. When [showNotifications] is true, LeakCanary + * will only display notifications if the app is in foreground and is not an instant, TV or + * Wear app. + * + * Defaults to true. + */ + val showNotifications: Boolean = true, + /** * Deprecated: This is a no-op, set a custom [leakingObjectFinder] instead. */ @@ -256,6 +265,7 @@ object LeakCanary { private var heapDumper = config.heapDumper private var eventListeners = config.eventListeners private var useExperimentalLeakFinders = config.useExperimentalLeakFinders + private var showNotifications = config.showNotifications /** @see [LeakCanary.Config.dumpHeap] */ fun dumpHeap(dumpHeap: Boolean) = @@ -315,6 +325,11 @@ object LeakCanary { fun useExperimentalLeakFinders(useExperimentalLeakFinders: Boolean) = apply { this.useExperimentalLeakFinders = useExperimentalLeakFinders } + /** @see [LeakCanary.Config.showNotifications] */ + fun showNotifications(showNotifications: Boolean) = + apply { this.showNotifications = showNotifications } + + fun build() = config.copy( dumpHeap = dumpHeap, dumpHeapWhenDebugging = dumpHeapWhenDebugging, @@ -329,7 +344,8 @@ object LeakCanary { leakingObjectFinder = leakingObjectFinder, heapDumper = heapDumper, eventListeners = eventListeners, - useExperimentalLeakFinders = useExperimentalLeakFinders + useExperimentalLeakFinders = useExperimentalLeakFinders, + showNotifications = showNotifications, ) } } diff --git a/leakcanary-android-core/src/main/java/leakcanary/internal/Notifications.kt b/leakcanary-android-core/src/main/java/leakcanary/internal/Notifications.kt index 41edceebc9..ef4e0f9334 100644 --- a/leakcanary-android-core/src/main/java/leakcanary/internal/Notifications.kt +++ b/leakcanary-android-core/src/main/java/leakcanary/internal/Notifications.kt @@ -24,6 +24,7 @@ import android.os.Build.VERSION.SDK_INT import android.os.Build.VERSION_CODES.JELLY_BEAN import android.os.Build.VERSION_CODES.O import com.squareup.leakcanary.core.R +import leakcanary.LeakCanary import leakcanary.internal.InternalLeakCanary.FormFactor.MOBILE internal object Notifications { @@ -34,7 +35,8 @@ internal object Notifications { // Watch devices: not sure, but probably not a good idea anyway? val canShowNotification: Boolean get() = InternalLeakCanary.formFactor == MOBILE && - (!InternalLeakCanary.isInstantApp || InternalLeakCanary.applicationVisible) + (!InternalLeakCanary.isInstantApp || InternalLeakCanary.applicationVisible) && + LeakCanary.config.showNotifications @Suppress("LongParameterList") fun showNotification(