Option to disable LeakCanary in debug builds

master
Koitharu 1 year ago
parent 498b9aed26
commit 914dd9670a
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -1,9 +1,12 @@
package org.koitharu.kotatsu package org.koitharu.kotatsu
import android.content.Context import android.content.Context
import android.content.SharedPreferences
import android.os.Build import android.os.Build
import android.os.StrictMode import android.os.StrictMode
import androidx.core.content.edit
import androidx.fragment.app.strictmode.FragmentStrictMode import androidx.fragment.app.strictmode.FragmentStrictMode
import leakcanary.LeakCanary
import org.koitharu.kotatsu.core.BaseApp import org.koitharu.kotatsu.core.BaseApp
import org.koitharu.kotatsu.local.data.LocalMangaRepository import org.koitharu.kotatsu.local.data.LocalMangaRepository
import org.koitharu.kotatsu.local.data.PagesCache import org.koitharu.kotatsu.local.data.PagesCache
@ -13,9 +16,23 @@ import org.koitharu.kotatsu.reader.ui.ReaderViewModel
class KotatsuApp : BaseApp() { class KotatsuApp : BaseApp() {
var isLeakCanaryEnabled: Boolean
get() = getDebugPreferences(this).getBoolean(KEY_LEAK_CANARY, true)
set(value) {
getDebugPreferences(this).edit { putBoolean(KEY_LEAK_CANARY, value) }
configureLeakCanary()
}
override fun attachBaseContext(base: Context) { override fun attachBaseContext(base: Context) {
super.attachBaseContext(base) super.attachBaseContext(base)
enableStrictMode() enableStrictMode()
configureLeakCanary()
}
private fun configureLeakCanary() {
LeakCanary.config = LeakCanary.config.copy(
dumpHeap = isLeakCanaryEnabled,
)
} }
private fun enableStrictMode() { private fun enableStrictMode() {
@ -55,7 +72,7 @@ class KotatsuApp : BaseApp() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && notifier != null) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && notifier != null) {
penaltyListener(notifier.executor, notifier) penaltyListener(notifier.executor, notifier)
} }
}.build() }.build(),
) )
FragmentStrictMode.defaultPolicy = FragmentStrictMode.Policy.Builder().apply { FragmentStrictMode.defaultPolicy = FragmentStrictMode.Policy.Builder().apply {
detectWrongFragmentContainer() detectWrongFragmentContainer()
@ -70,4 +87,13 @@ class KotatsuApp : BaseApp() {
} }
}.build() }.build()
} }
private companion object {
const val PREFS_DEBUG = "_debug"
const val KEY_LEAK_CANARY = "leak_canary"
fun getDebugPreferences(context: Context): SharedPreferences =
context.getSharedPreferences(PREFS_DEBUG, MODE_PRIVATE)
}
} }

@ -6,6 +6,7 @@ import android.view.MenuInflater
import android.view.MenuItem import android.view.MenuItem
import androidx.core.view.MenuProvider import androidx.core.view.MenuProvider
import leakcanary.LeakCanary import leakcanary.LeakCanary
import org.koitharu.kotatsu.KotatsuApp
import org.koitharu.kotatsu.R import org.koitharu.kotatsu.R
import org.koitharu.workinspector.WorkInspector import org.koitharu.workinspector.WorkInspector
@ -13,10 +14,18 @@ class SettingsMenuProvider(
private val context: Context, private val context: Context,
) : MenuProvider { ) : MenuProvider {
private val application: KotatsuApp
get() = context.applicationContext as KotatsuApp
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.opt_settings, menu) menuInflater.inflate(R.menu.opt_settings, menu)
} }
override fun onPrepareMenu(menu: Menu) {
super.onPrepareMenu(menu)
menu.findItem(R.id.action_leakcanary).isChecked = application.isLeakCanaryEnabled
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean = when (menuItem.itemId) { override fun onMenuItemSelected(menuItem: MenuItem): Boolean = when (menuItem.itemId) {
R.id.action_leaks -> { R.id.action_leaks -> {
context.startActivity(LeakCanary.newLeakDisplayActivityIntent()) context.startActivity(LeakCanary.newLeakDisplayActivityIntent())
@ -28,6 +37,13 @@ class SettingsMenuProvider(
true true
} }
R.id.action_leakcanary -> {
val checked = !menuItem.isChecked
menuItem.isChecked = checked
application.isLeakCanaryEnabled = checked
true
}
else -> false else -> false
} }
} }

@ -1,15 +1,23 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu <menu
xmlns:android="http://schemas.android.com/apk/res/android" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item <item
android:id="@id/action_leaks" android:id="@+id/action_leakcanary"
android:checkable="true"
android:title="LeakCanary"
app:showAsAction="never"
tools:ignore="HardcodedText" />
<item
android:id="@+id/action_leaks"
android:title="@string/leak_canary_display_activity_label" android:title="@string/leak_canary_display_activity_label"
app:showAsAction="never" /> app:showAsAction="never" />
<item <item
android:id="@id/action_works" android:id="@+id/action_works"
android:title="@string/wi_lib_name" android:title="@string/wi_lib_name"
app:showAsAction="never" /> app:showAsAction="never" />

@ -2,8 +2,6 @@
<resources> <resources>
<item name="toolbar" type="id" /> <item name="toolbar" type="id" />
<item name="container" type="id" /> <item name="container" type="id" />
<item name="action_leaks" type="id" />
<item name="action_works" type="id" />
<item name="action_tracker" type="id" /> <item name="action_tracker" type="id" />
<item name="fast_scroller" type="id" /> <item name="fast_scroller" type="id" />
<item name="group_branches" type="id" /> <item name="group_branches" type="id" />

Loading…
Cancel
Save