From 3ff9ab1bc80cf90f59ef9f085ad0a0a1710fb896 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Wed, 10 Aug 2022 14:44:51 +0200 Subject: [PATCH] Interface for LeakDetector --- .../im/vector/app/features/debug/di/DebugModule.kt | 5 +++++ .../features/debug/leak/DebugMemoryLeaksViewModel.kt | 6 +++--- .../{LeakCanaryProxy.kt => LeakCanaryLeakDetector.kt} | 5 +++-- .../src/main/java/im/vector/app/VectorApplication.kt | 7 +++---- .../java/im/vector/app/core/debug/LeakDetector.kt} | 11 ++++------- .../release/java/im/vector/app/core/di/DebugModule.kt | 8 ++++++++ 6 files changed, 26 insertions(+), 16 deletions(-) rename vector/src/debug/java/im/vector/app/leakcanary/{LeakCanaryProxy.kt => LeakCanaryLeakDetector.kt} (83%) rename vector/src/{release/java/im/vector/app/leakcanary/LeakCanaryProxy.kt => main/java/im/vector/app/core/debug/LeakDetector.kt} (76%) diff --git a/vector/src/debug/java/im/vector/app/features/debug/di/DebugModule.kt b/vector/src/debug/java/im/vector/app/features/debug/di/DebugModule.kt index b15f6dce62..d409e56349 100644 --- a/vector/src/debug/java/im/vector/app/features/debug/di/DebugModule.kt +++ b/vector/src/debug/java/im/vector/app/features/debug/di/DebugModule.kt @@ -26,8 +26,10 @@ import dagger.hilt.components.SingletonComponent import im.vector.app.core.debug.DebugNavigator import im.vector.app.core.debug.DebugReceiver import im.vector.app.core.debug.FlipperProxy +import im.vector.app.core.debug.LeakDetector import im.vector.app.features.debug.DebugMenuActivity import im.vector.app.flipper.VectorFlipperProxy +import im.vector.app.leakcanary.LeakCanaryLeakDetector import im.vector.app.receivers.VectorDebugReceiver @InstallIn(SingletonComponent::class) @@ -49,4 +51,7 @@ abstract class DebugModule { @Binds abstract fun bindsFlipperProxy(flipperProxy: VectorFlipperProxy): FlipperProxy + + @Binds + abstract fun bindsLeakDetector(leakDetector: LeakCanaryLeakDetector): LeakDetector } diff --git a/vector/src/debug/java/im/vector/app/features/debug/leak/DebugMemoryLeaksViewModel.kt b/vector/src/debug/java/im/vector/app/features/debug/leak/DebugMemoryLeaksViewModel.kt index 617a6b2ecb..5432cb0888 100644 --- a/vector/src/debug/java/im/vector/app/features/debug/leak/DebugMemoryLeaksViewModel.kt +++ b/vector/src/debug/java/im/vector/app/features/debug/leak/DebugMemoryLeaksViewModel.kt @@ -20,18 +20,18 @@ import com.airbnb.mvrx.MavericksViewModelFactory import dagger.assisted.Assisted import dagger.assisted.AssistedFactory import dagger.assisted.AssistedInject +import im.vector.app.core.debug.LeakDetector import im.vector.app.core.di.MavericksAssistedViewModelFactory import im.vector.app.core.di.hiltMavericksViewModelFactory import im.vector.app.core.platform.EmptyViewEvents import im.vector.app.core.platform.VectorViewModel import im.vector.app.features.settings.VectorPreferences -import im.vector.app.leakcanary.LeakCanaryProxy import kotlinx.coroutines.launch class DebugMemoryLeaksViewModel @AssistedInject constructor( @Assisted initialState: DebugMemoryLeaksViewState, private val vectorPreferences: VectorPreferences, - private val leakCanaryProxy: LeakCanaryProxy, + private val leakDetector: LeakDetector, ) : VectorViewModel(initialState) { @AssistedFactory @@ -56,7 +56,7 @@ class DebugMemoryLeaksViewModel @AssistedInject constructor( private fun handleEnableMemoryLeaksAnalysis(action: DebugMemoryLeaksViewActions.EnableMemoryLeaksAnalysis) { viewModelScope.launch { vectorPreferences.enableMemoryLeakAnalysis(action.isEnabled) - leakCanaryProxy.enable(action.isEnabled) + leakDetector.enable(action.isEnabled) refreshStateFromPreferences() } } diff --git a/vector/src/debug/java/im/vector/app/leakcanary/LeakCanaryProxy.kt b/vector/src/debug/java/im/vector/app/leakcanary/LeakCanaryLeakDetector.kt similarity index 83% rename from vector/src/debug/java/im/vector/app/leakcanary/LeakCanaryProxy.kt rename to vector/src/debug/java/im/vector/app/leakcanary/LeakCanaryLeakDetector.kt index 3154c2c04b..15db71cd96 100644 --- a/vector/src/debug/java/im/vector/app/leakcanary/LeakCanaryProxy.kt +++ b/vector/src/debug/java/im/vector/app/leakcanary/LeakCanaryLeakDetector.kt @@ -16,11 +16,12 @@ package im.vector.app.leakcanary +import im.vector.app.core.debug.LeakDetector import leakcanary.LeakCanary import javax.inject.Inject -class LeakCanaryProxy @Inject constructor() { - fun enable(enable: Boolean) { +class LeakCanaryLeakDetector @Inject constructor() : LeakDetector { + override fun enable(enable: Boolean) { LeakCanary.config = LeakCanary.config.copy(dumpHeap = enable) } } diff --git a/vector/src/main/java/im/vector/app/VectorApplication.kt b/vector/src/main/java/im/vector/app/VectorApplication.kt index bbff8f35c8..5797e3efe8 100644 --- a/vector/src/main/java/im/vector/app/VectorApplication.kt +++ b/vector/src/main/java/im/vector/app/VectorApplication.kt @@ -42,6 +42,7 @@ import com.vanniktech.emoji.google.GoogleEmojiProvider import dagger.hilt.android.HiltAndroidApp import im.vector.app.config.Config import im.vector.app.core.debug.FlipperProxy +import im.vector.app.core.debug.LeakDetector import im.vector.app.core.di.ActiveSessionHolder import im.vector.app.core.pushers.FcmHelper import im.vector.app.core.resources.BuildMeta @@ -61,8 +62,6 @@ import im.vector.app.features.settings.VectorLocale import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.themes.ThemeUtils import im.vector.app.features.version.VersionProvider -import im.vector.app.leakcanary.LeakCanaryProxy -import im.vector.app.push.fcm.FcmHelper import org.jitsi.meet.sdk.log.JitsiMeetDefaultLogHandler import org.matrix.android.sdk.api.Matrix import org.matrix.android.sdk.api.auth.AuthenticationService @@ -104,7 +103,7 @@ class VectorApplication : @Inject lateinit var matrix: Matrix @Inject lateinit var fcmHelper: FcmHelper @Inject lateinit var buildMeta: BuildMeta - @Inject lateinit var leakCanaryProxy: LeakCanaryProxy + @Inject lateinit var leakDetector: LeakDetector // font thread handler private var fontThreadHandler: Handler? = null @@ -258,6 +257,6 @@ class VectorApplication : } private fun initMemoryLeakAnalysis() { - leakCanaryProxy.enable(vectorPreferences.isMemoryLeakAnalysisEnabled()) + leakDetector.enable(vectorPreferences.isMemoryLeakAnalysisEnabled()) } } diff --git a/vector/src/release/java/im/vector/app/leakcanary/LeakCanaryProxy.kt b/vector/src/main/java/im/vector/app/core/debug/LeakDetector.kt similarity index 76% rename from vector/src/release/java/im/vector/app/leakcanary/LeakCanaryProxy.kt rename to vector/src/main/java/im/vector/app/core/debug/LeakDetector.kt index 28c9f1c9d5..37a90a6c61 100644 --- a/vector/src/release/java/im/vector/app/leakcanary/LeakCanaryProxy.kt +++ b/vector/src/main/java/im/vector/app/core/debug/LeakDetector.kt @@ -14,14 +14,11 @@ * limitations under the License. */ -package im.vector.app.leakcanary - -import javax.inject.Inject +package im.vector.app.core.debug /** - * No op version. + * Used for memory leak analysis control. */ -@Suppress("UNUSED_PARAMETER") -class LeakCanaryProxy @Inject constructor() { - fun enable(enable: Boolean) {} +interface LeakDetector { + fun enable(enable: Boolean) } diff --git a/vector/src/release/java/im/vector/app/core/di/DebugModule.kt b/vector/src/release/java/im/vector/app/core/di/DebugModule.kt index bedbe97864..2a1e334a48 100644 --- a/vector/src/release/java/im/vector/app/core/di/DebugModule.kt +++ b/vector/src/release/java/im/vector/app/core/di/DebugModule.kt @@ -24,6 +24,7 @@ import dagger.hilt.components.SingletonComponent import im.vector.app.core.debug.DebugNavigator import im.vector.app.core.debug.DebugReceiver import im.vector.app.core.debug.FlipperProxy +import im.vector.app.core.debug.LeakDetector import okhttp3.Interceptor import org.matrix.android.sdk.api.Matrix @@ -57,4 +58,11 @@ object DebugModule { override fun networkInterceptor(): Interceptor? = null } + + @Provides + fun providesLeakDetector() = object : LeakDetector { + override fun enable(enable: Boolean) { + // no op + } + } }