Skip to content

Commit

Permalink
LeakSentry can be disabled (#1332)
Browse files Browse the repository at this point in the history
LeakCanary has an enabled config option. Default is on in debug builds off in release builds.

Fixes #1327
  • Loading branch information
pyricau authored May 4, 2019
1 parent c28128b commit f74f609
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.util.concurrent.TimeUnit
object LeakSentry {

data class Config(
val enabled: Boolean = InternalLeakSentry.isDebuggableBuild,
val watchActivities: Boolean = true,
val watchFragments: Boolean = true,
val watchFragmentViews: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package leakcanary.internal

import android.app.Application
import android.content.pm.ApplicationInfo
import android.os.Handler
import android.os.Looper
import android.os.SystemClock
Expand All @@ -14,6 +15,10 @@ internal object InternalLeakSentry {

private val listener: LeakSentryListener

val isDebuggableBuild by lazy {
(application.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0
}

lateinit var application: Application

private val clock = object : Clock {
Expand All @@ -37,11 +42,11 @@ internal object InternalLeakSentry {
mainHandler.postDelayed(it, LeakSentry.config.watchDurationMillis)
}
val refWatcher = RefWatcher(
clock,
checkRetainedExecutor
) {
listener.onReferenceRetained()
}
clock = clock,
checkRetainedExecutor = checkRetainedExecutor,
onReferenceRetained = { listener.onReferenceRetained() },
isEnabled = { LeakSentry.config.enabled }
)

fun install(application: Application) {
CanaryLog.d("Installing LeakSentry")
Expand Down
13 changes: 10 additions & 3 deletions leakcanary-watcher/src/main/java/leakcanary/RefWatcher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ import java.util.concurrent.Executor
class RefWatcher constructor(
private val clock: Clock,
private val checkRetainedExecutor: Executor,
private val onReferenceRetained: () -> Unit
private val onReferenceRetained: () -> Unit,
/**
* Calls to [watch] will be ignored when [isEnabled] returns false
*/
private val isEnabled: () -> Boolean = { true }
) {

/**
Expand All @@ -44,13 +48,13 @@ class RefWatcher constructor(
val hasRetainedReferences: Boolean
@Synchronized get() {
removeWeaklyReachableReferences()
return !retainedReferences.isEmpty()
return retainedReferences.isNotEmpty()
}

val hasWatchedReferences: Boolean
@Synchronized get() {
removeWeaklyReachableReferences()
return !retainedReferences.isEmpty() || !watchedReferences.isEmpty()
return retainedReferences.isNotEmpty() || watchedReferences.isNotEmpty()
}

val retainedKeys: Set<String>
Expand All @@ -75,6 +79,9 @@ class RefWatcher constructor(
watchedReference: Any,
referenceName: String
) {
if (!isEnabled()) {
return
}
removeWeaklyReachableReferences()
val key = UUID.randomUUID()
.toString()
Expand Down

0 comments on commit f74f609

Please sign in to comment.