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( diff --git a/leakcanary-android-sample/src/main/java/com/example/leakcanary/ExampleApplication.kt b/leakcanary-android-sample/src/main/java/com/example/leakcanary/ExampleApplication.kt index 06799a801d..4030fd1423 100644 --- a/leakcanary-android-sample/src/main/java/com/example/leakcanary/ExampleApplication.kt +++ b/leakcanary-android-sample/src/main/java/com/example/leakcanary/ExampleApplication.kt @@ -20,6 +20,7 @@ import android.app.Dialog import android.app.Service import android.os.StrictMode import android.view.View +import leakcanary.LeakCanary open class ExampleApplication : Application() { val leakedViews = mutableListOf() @@ -28,6 +29,7 @@ open class ExampleApplication : Application() { override fun onCreate() { super.onCreate() + LeakCanary.config = LeakCanary.config.copy(showNotifications = false) enabledStrictMode() }