From e02cf61f2ff410b4accd1481ea1e0b564fb05ee9 Mon Sep 17 00:00:00 2001 From: Adam Brown Date: Mon, 8 Aug 2022 16:57:51 +0100 Subject: [PATCH] decoupling debug receiver from the variants by introducing vector layer interface --- .../app/features/debug/di/DebugModule.kt | 32 ++++++++++++++++ ...ebugReceiver.kt => VectorDebugReceiver.kt} | 12 +++++- .../vector/app/core/platform/DebugReceiver.kt | 24 ++++++++++++ .../app/core/platform/VectorBaseActivity.kt | 21 +++------- .../java/im/vector/app/core/di/DebugModule.kt | 38 +++++++++++++++++++ 5 files changed, 110 insertions(+), 17 deletions(-) create mode 100644 vector/src/debug/java/im/vector/app/features/debug/di/DebugModule.kt rename vector/src/debug/java/im/vector/app/receivers/{DebugReceiver.kt => VectorDebugReceiver.kt} (88%) create mode 100644 vector/src/main/java/im/vector/app/core/platform/DebugReceiver.kt create mode 100644 vector/src/release/java/im/vector/app/core/di/DebugModule.kt 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 new file mode 100644 index 0000000000..d68d6c743f --- /dev/null +++ b/vector/src/debug/java/im/vector/app/features/debug/di/DebugModule.kt @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.debug.di + +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import im.vector.app.core.platform.DebugReceiver +import im.vector.app.receivers.VectorDebugReceiver + +@InstallIn(SingletonComponent::class) +@Module +abstract class DebugModule { + + @Binds + abstract fun bindsDebugReceiver(receiver: VectorDebugReceiver): DebugReceiver +} diff --git a/vector/src/debug/java/im/vector/app/receivers/DebugReceiver.kt b/vector/src/debug/java/im/vector/app/receivers/VectorDebugReceiver.kt similarity index 88% rename from vector/src/debug/java/im/vector/app/receivers/DebugReceiver.kt rename to vector/src/debug/java/im/vector/app/receivers/VectorDebugReceiver.kt index 9ec475d6d3..9b547a433e 100644 --- a/vector/src/debug/java/im/vector/app/receivers/DebugReceiver.kt +++ b/vector/src/debug/java/im/vector/app/receivers/VectorDebugReceiver.kt @@ -23,13 +23,23 @@ import android.content.IntentFilter import android.content.SharedPreferences import androidx.core.content.edit import im.vector.app.core.di.DefaultSharedPreferences +import im.vector.app.core.platform.DebugReceiver import im.vector.app.core.utils.lsFiles import timber.log.Timber +import javax.inject.Inject /** * Receiver to handle some command from ADB */ -class DebugReceiver : BroadcastReceiver() { +class VectorDebugReceiver @Inject constructor() : BroadcastReceiver(), DebugReceiver { + + override fun register(context: Context) { + context.registerReceiver(this, getIntentFilter(context)) + } + + override fun unregister(context: Context) { + context.unregisterReceiver(this) + } override fun onReceive(context: Context, intent: Intent) { Timber.v("Received debug action: ${intent.action}") diff --git a/vector/src/main/java/im/vector/app/core/platform/DebugReceiver.kt b/vector/src/main/java/im/vector/app/core/platform/DebugReceiver.kt new file mode 100644 index 0000000000..aeb99f312d --- /dev/null +++ b/vector/src/main/java/im/vector/app/core/platform/DebugReceiver.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.core.platform + +import android.content.Context + +interface DebugReceiver { + fun register(context: Context) + fun unregister(context: Context) +} diff --git a/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt b/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt index 8a09b6bd46..e5c7ab9228 100644 --- a/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt +++ b/vector/src/main/java/im/vector/app/core/platform/VectorBaseActivity.kt @@ -91,7 +91,7 @@ import im.vector.app.features.settings.FontScalePreferencesImpl import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.themes.ActivityOtherThemes import im.vector.app.features.themes.ThemeUtils -import im.vector.app.receivers.DebugReceiver +import im.vector.app.receivers.VectorDebugReceiver import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import org.matrix.android.sdk.api.extensions.orFalse @@ -160,6 +160,8 @@ abstract class VectorBaseActivity : AppCompatActivity(), Maver @Inject lateinit var rageShake: RageShake @Inject lateinit var buildMeta: BuildMeta @Inject lateinit var fontScalePreferences: FontScalePreferences + // For debug only + @Inject lateinit var debugReceiver: VectorDebugReceiver @Inject lateinit var vectorFeatures: VectorFeatures @@ -176,9 +178,6 @@ abstract class VectorBaseActivity : AppCompatActivity(), Maver private var savedInstanceState: Bundle? = null - // For debug only - private var debugReceiver: DebugReceiver? = null - private val restorables = ArrayList() override fun attachBaseContext(base: Context) { @@ -418,13 +417,7 @@ abstract class VectorBaseActivity : AppCompatActivity(), Maver if (this !is BugReportActivity && vectorPreferences.useRageshake()) { rageShake.start() } - DebugReceiver - .getIntentFilter(this) - .takeIf { buildMeta.isDebug } - ?.let { - debugReceiver = DebugReceiver() - registerReceiver(debugReceiver, it) - } + debugReceiver.register(this) } private val postResumeScheduledActions = mutableListOf<() -> Unit>() @@ -454,11 +447,7 @@ abstract class VectorBaseActivity : AppCompatActivity(), Maver Timber.i("onPause Activity ${javaClass.simpleName}") rageShake.stop() - - debugReceiver?.let { - unregisterReceiver(debugReceiver) - debugReceiver = null - } + debugReceiver.unregister(this) } override fun onWindowFocusChanged(hasFocus: 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 new file mode 100644 index 0000000000..2899b14b7b --- /dev/null +++ b/vector/src/release/java/im/vector/app/core/di/DebugModule.kt @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package im.vector.app.features.debug.di + +import dagger.Binds +import dagger.Module +import dagger.hilt.InstallIn +import dagger.hilt.components.SingletonComponent +import im.vector.app.core.platform.DebugReceiver + +@InstallIn(SingletonComponent::class) +@Module +object DebugModule { + @Provides + fun providesDebugReceiver() = object: DebugReceiver { + override fun register(context: Context) { + // no op + } + + override fun unregister(context: Context) { + // no op + } + } +}