[fenix] Fixes https://github.com/mozilla-mobile/fenix/issues/401: Resolve known leak and install LeakCanary
parent
16db6131e3
commit
b3e1b4bc0a
@ -0,0 +1,10 @@
|
|||||||
|
<manifest
|
||||||
|
package="org.mozilla.fenix"
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
xmlns:tools="http://schemas.android.com/tools">
|
||||||
|
|
||||||
|
<application
|
||||||
|
tools:replace="android:name"
|
||||||
|
android:name="org.mozilla.fenix.DebugFenixApplication"/>
|
||||||
|
|
||||||
|
</manifest>
|
@ -0,0 +1,43 @@
|
|||||||
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||||
|
License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||||
|
file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
|
package org.mozilla.fenix
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
|
import com.squareup.leakcanary.AndroidHeapDumper
|
||||||
|
import com.squareup.leakcanary.HeapDumper
|
||||||
|
import com.squareup.leakcanary.LeakCanary
|
||||||
|
import com.squareup.leakcanary.internal.LeakCanaryInternals
|
||||||
|
import org.mozilla.fenix.R.string.pref_key_leakcanary
|
||||||
|
import org.mozilla.fenix.ext.getPreferenceKey
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class DebugFenixApplication : FenixApplication() {
|
||||||
|
|
||||||
|
private var heapDumper: ToggleableHeapDumper? = null
|
||||||
|
|
||||||
|
override fun setupLeakCanary() {
|
||||||
|
val leakDirectoryProvider = LeakCanaryInternals.getLeakDirectoryProvider(this)
|
||||||
|
val defaultDumper = AndroidHeapDumper(this, leakDirectoryProvider)
|
||||||
|
heapDumper = ToggleableHeapDumper(this, defaultDumper)
|
||||||
|
LeakCanary.refWatcher(this)
|
||||||
|
.heapDumper(heapDumper)
|
||||||
|
.buildAndInstall()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun toggleLeakCanary(newValue: Boolean) {
|
||||||
|
heapDumper?.enabled = newValue
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class ToggleableHeapDumper(
|
||||||
|
private val context: Context,
|
||||||
|
private val defaultDumper: HeapDumper
|
||||||
|
) : HeapDumper {
|
||||||
|
var prefs: SharedPreferences? = PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
|
var enabled = prefs?.getBoolean(context.getPreferenceKey(pref_key_leakcanary), true) ?: true
|
||||||
|
override fun dumpHeap(): File = if (enabled) defaultDumper.dumpHeap() else HeapDumper.RETRY_LATER
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue