From 914dd9670a7cda294e9fc92817b16fae59bda49e Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sun, 19 Jan 2025 13:48:55 +0200 Subject: [PATCH] Option to disable LeakCanary in debug builds --- .../kotlin/org/koitharu/kotatsu/KotatsuApp.kt | 28 ++++++++++++++++++- .../kotatsu/settings/SettingsMenuProvider.kt | 16 +++++++++++ app/src/debug/res/menu/opt_settings.xml | 14 ++++++++-- app/src/main/res/values/ids.xml | 2 -- 4 files changed, 54 insertions(+), 6 deletions(-) diff --git a/app/src/debug/kotlin/org/koitharu/kotatsu/KotatsuApp.kt b/app/src/debug/kotlin/org/koitharu/kotatsu/KotatsuApp.kt index 1e9dd8a44..bd5169044 100644 --- a/app/src/debug/kotlin/org/koitharu/kotatsu/KotatsuApp.kt +++ b/app/src/debug/kotlin/org/koitharu/kotatsu/KotatsuApp.kt @@ -1,9 +1,12 @@ package org.koitharu.kotatsu import android.content.Context +import android.content.SharedPreferences import android.os.Build import android.os.StrictMode +import androidx.core.content.edit import androidx.fragment.app.strictmode.FragmentStrictMode +import leakcanary.LeakCanary import org.koitharu.kotatsu.core.BaseApp import org.koitharu.kotatsu.local.data.LocalMangaRepository import org.koitharu.kotatsu.local.data.PagesCache @@ -13,9 +16,23 @@ import org.koitharu.kotatsu.reader.ui.ReaderViewModel 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) { super.attachBaseContext(base) enableStrictMode() + configureLeakCanary() + } + + private fun configureLeakCanary() { + LeakCanary.config = LeakCanary.config.copy( + dumpHeap = isLeakCanaryEnabled, + ) } private fun enableStrictMode() { @@ -55,7 +72,7 @@ class KotatsuApp : BaseApp() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P && notifier != null) { penaltyListener(notifier.executor, notifier) } - }.build() + }.build(), ) FragmentStrictMode.defaultPolicy = FragmentStrictMode.Policy.Builder().apply { detectWrongFragmentContainer() @@ -70,4 +87,13 @@ class KotatsuApp : BaseApp() { } }.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) + } } diff --git a/app/src/debug/kotlin/org/koitharu/kotatsu/settings/SettingsMenuProvider.kt b/app/src/debug/kotlin/org/koitharu/kotatsu/settings/SettingsMenuProvider.kt index 519db2319..63bf3ef15 100644 --- a/app/src/debug/kotlin/org/koitharu/kotatsu/settings/SettingsMenuProvider.kt +++ b/app/src/debug/kotlin/org/koitharu/kotatsu/settings/SettingsMenuProvider.kt @@ -6,6 +6,7 @@ import android.view.MenuInflater import android.view.MenuItem import androidx.core.view.MenuProvider import leakcanary.LeakCanary +import org.koitharu.kotatsu.KotatsuApp import org.koitharu.kotatsu.R import org.koitharu.workinspector.WorkInspector @@ -13,10 +14,18 @@ class SettingsMenuProvider( private val context: Context, ) : MenuProvider { + private val application: KotatsuApp + get() = context.applicationContext as KotatsuApp + override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) { 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) { R.id.action_leaks -> { context.startActivity(LeakCanary.newLeakDisplayActivityIntent()) @@ -28,6 +37,13 @@ class SettingsMenuProvider( true } + R.id.action_leakcanary -> { + val checked = !menuItem.isChecked + menuItem.isChecked = checked + application.isLeakCanaryEnabled = checked + true + } + else -> false } } diff --git a/app/src/debug/res/menu/opt_settings.xml b/app/src/debug/res/menu/opt_settings.xml index b7bb4a5ac..8f3a0a518 100644 --- a/app/src/debug/res/menu/opt_settings.xml +++ b/app/src/debug/res/menu/opt_settings.xml @@ -1,15 +1,23 @@ + xmlns:app="http://schemas.android.com/apk/res-auto" + xmlns:tools="http://schemas.android.com/tools"> + + diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml index 582c3fc28..e8504f092 100644 --- a/app/src/main/res/values/ids.xml +++ b/app/src/main/res/values/ids.xml @@ -2,8 +2,6 @@ - -