2019-02-13 16:08:35 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2019-07-12 20:38:15 +02:00
|
|
|
* 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/. */
|
2019-02-13 16:08:35 +01:00
|
|
|
|
|
|
|
package org.mozilla.fenix
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.SharedPreferences
|
|
|
|
import androidx.preference.PreferenceManager
|
2019-03-22 22:19:10 +01:00
|
|
|
import com.facebook.flipper.android.AndroidFlipperClient
|
|
|
|
import com.facebook.flipper.android.utils.FlipperUtils
|
|
|
|
import com.facebook.flipper.plugins.inspector.DescriptorMapping
|
|
|
|
import com.facebook.flipper.plugins.inspector.InspectorFlipperPlugin
|
|
|
|
import com.facebook.flipper.plugins.leakcanary.LeakCanaryFlipperPlugin
|
|
|
|
import com.facebook.flipper.plugins.leakcanary.RecordLeakService
|
|
|
|
import com.facebook.flipper.plugins.sharedpreferences.SharedPreferencesFlipperPlugin
|
|
|
|
import com.facebook.soloader.SoLoader
|
2019-02-13 16:08:35 +01:00
|
|
|
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() {
|
|
|
|
|
2019-03-22 22:19:10 +01:00
|
|
|
override fun onCreate() {
|
|
|
|
SoLoader.init(this, false)
|
|
|
|
|
|
|
|
if (FlipperUtils.shouldEnableFlipper(this)) {
|
|
|
|
AndroidFlipperClient.getInstance(this).apply {
|
|
|
|
addPlugin(InspectorFlipperPlugin(this@DebugFenixApplication,
|
|
|
|
DescriptorMapping.withDefaults()))
|
|
|
|
addPlugin(LeakCanaryFlipperPlugin())
|
|
|
|
addPlugin(SharedPreferencesFlipperPlugin(this@DebugFenixApplication,
|
|
|
|
this@DebugFenixApplication.packageName + "_preferences"))
|
|
|
|
start()
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 07:23:28 +02:00
|
|
|
|
|
|
|
super.onCreate()
|
2019-03-22 22:19:10 +01:00
|
|
|
}
|
|
|
|
|
2019-02-13 16:08:35 +01:00
|
|
|
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)
|
2019-03-22 22:19:10 +01:00
|
|
|
.listenerServiceClass(RecordLeakService::class.java)
|
2019-02-13 16:08:35 +01:00
|
|
|
.heapDumper(heapDumper)
|
|
|
|
.buildAndInstall()
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun toggleLeakCanary(newValue: Boolean) {
|
|
|
|
heapDumper?.enabled = newValue
|
|
|
|
}
|
|
|
|
|
|
|
|
internal class ToggleableHeapDumper(
|
2019-02-13 22:18:03 +01:00
|
|
|
context: Context,
|
2019-02-13 16:08:35 +01:00
|
|
|
private val defaultDumper: HeapDumper
|
|
|
|
) : HeapDumper {
|
2019-02-15 17:29:02 +01:00
|
|
|
var prefs: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
|
|
|
|
var enabled = prefs.getBoolean(context.getPreferenceKey(pref_key_leakcanary), false)
|
2019-02-13 22:18:03 +01:00
|
|
|
override fun dumpHeap(): File? = if (enabled) defaultDumper.dumpHeap() else HeapDumper.RETRY_LATER
|
2019-02-13 16:08:35 +01:00
|
|
|
}
|
|
|
|
}
|