Skip to content

Commit

Permalink
Add support for disabling LeakCanary notifications
Browse files Browse the repository at this point in the history
Fixes #2394
Fixes #2398
  • Loading branch information
pyricau committed Nov 10, 2022
1 parent e306e5c commit 49a6371
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
18 changes: 17 additions & 1 deletion leakcanary-android-core/src/main/java/leakcanary/LeakCanary.kt
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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) =
Expand Down Expand Up @@ -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,
Expand All @@ -329,7 +344,8 @@ object LeakCanary {
leakingObjectFinder = leakingObjectFinder,
heapDumper = heapDumper,
eventListeners = eventListeners,
useExperimentalLeakFinders = useExperimentalLeakFinders
useExperimentalLeakFinders = useExperimentalLeakFinders,
showNotifications = showNotifications,
)
}
}
Expand Down
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down
Expand Up @@ -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<View>()
Expand All @@ -28,6 +29,7 @@ open class ExampleApplication : Application() {

override fun onCreate() {
super.onCreate()
LeakCanary.config = LeakCanary.config.copy(showNotifications = false)
enabledStrictMode()
}

Expand Down

0 comments on commit 49a6371

Please sign in to comment.