adding debug screen to override features

- adds enum support with persistence via class names
This commit is contained in:
Adam Brown 2021-12-06 12:08:34 +00:00
parent 96295f6102
commit 2d74eb060c
11 changed files with 395 additions and 7 deletions

View File

@ -7,6 +7,7 @@
<activity android:name=".features.debug.DebugPermissionActivity" />
<activity android:name=".features.debug.settings.DebugPrivateSettingsActivity" />
<activity android:name=".features.debug.sas.DebugSasEmojiActivity" />
<activity android:name=".features.debug.features.DebugFeaturesSettingsActivity" />
</application>
</manifest>

View File

@ -34,6 +34,7 @@ import im.vector.app.core.utils.checkPermissions
import im.vector.app.core.utils.registerForPermissionsResult
import im.vector.app.core.utils.toast
import im.vector.app.databinding.ActivityDebugMenuBinding
import im.vector.app.features.debug.features.DebugFeaturesSettingsActivity
import im.vector.app.features.debug.sas.DebugSasEmojiActivity
import im.vector.app.features.debug.settings.DebugPrivateSettingsActivity
import im.vector.app.features.qrcode.QrCodeScannerActivity
@ -76,6 +77,7 @@ class DebugMenuActivity : VectorBaseActivity<ActivityDebugMenuBinding>() {
}
private fun setupViews() {
views.debugFeatures.setOnClickListener { startActivity(Intent(this, DebugFeaturesSettingsActivity::class.java)) }
views.debugPrivateSetting.setOnClickListener { openPrivateSettings() }
views.debugTestTextViewLink.setOnClickListener { testTextViewLink() }
views.debugOpenButtonStylesLight.setOnClickListener {

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2021 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 android.content.Context
import dagger.Binds
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import im.vector.app.features.DefaultVectorFeatures
import im.vector.app.features.VectorFeatures
import im.vector.app.features.debug.features.DebugVectorFeatures
@InstallIn(SingletonComponent::class)
@Module
interface FeaturesModule {
@Binds
fun bindNavigator(navigator: DebugVectorFeatures): VectorFeatures
companion object {
@Provides
fun providesDefaultVectorFeatures(): DefaultVectorFeatures {
return DefaultVectorFeatures()
}
@Provides
fun providesDebugVectorFeatures(context: Context, defaultVectorFeatures: DefaultVectorFeatures): DebugVectorFeatures {
return DebugVectorFeatures(context, defaultVectorFeatures)
}
}
}

View File

@ -0,0 +1,78 @@
/*
* Copyright (c) 2021 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.features
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import im.vector.app.core.extensions.cleanup
import im.vector.app.core.extensions.configureWith
import im.vector.app.databinding.FragmentGenericRecyclerBinding
import im.vector.app.features.DefaultVectorFeatures
import im.vector.app.features.themes.ActivityOtherThemes
import im.vector.app.features.themes.ThemeUtils
import javax.inject.Inject
@AndroidEntryPoint
class DebugFeaturesSettingsActivity : AppCompatActivity() {
@Inject lateinit var debugFeatures: DebugVectorFeatures
@Inject lateinit var defaultFeatures: DefaultVectorFeatures
private lateinit var views: FragmentGenericRecyclerBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ThemeUtils.setActivityTheme(this, ActivityOtherThemes.Default)
views = FragmentGenericRecyclerBinding.inflate(layoutInflater)
setContentView(views.root)
val controller = FeaturesController(object : EnumFeatureItem.Listener {
@Suppress("UNCHECKED_CAST")
override fun <T : Enum<T>> onOptionSelected(option: Any?, feature: Feature.EnumFeature<T>) {
debugFeatures.overrideEnum(option as? T, feature.type)
}
})
views.genericRecyclerView.configureWith(controller)
controller.setData(createState())
}
private fun createState(): FeaturesState {
return FeaturesState(listOf(
createEnumFeature(
label = "Login version",
selection = debugFeatures.loginVersion(),
default = defaultFeatures.loginVersion()
)
))
}
private inline fun <reified T : Enum<T>> createEnumFeature(label: String, selection: T, default: T): Feature {
return Feature.EnumFeature(
label = label,
selection = selection.takeIf { debugFeatures.hasEnumOverride(T::class) },
default = default,
options = enumValues<T>().toList(),
type = T::class
)
}
override fun onDestroy() {
views.genericRecyclerView.cleanup()
super.onDestroy()
}
}

View File

@ -0,0 +1,66 @@
/*
* Copyright (c) 2021 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.features
import android.content.Context
import android.content.SharedPreferences
import im.vector.app.features.DefaultVectorFeatures
import im.vector.app.features.VectorFeatures
import kotlin.reflect.KClass
class DebugVectorFeatures(
context: Context,
private val vectorFeatures: DefaultVectorFeatures
) : VectorFeatures {
private val featurePrefs = context.getSharedPreferences("debug-features", Context.MODE_PRIVATE)
override fun loginVersion(): VectorFeatures.LoginVersion {
return featurePrefs.readEnum<VectorFeatures.LoginVersion>() ?: vectorFeatures.loginVersion()
}
fun <T : Enum<T>> hasEnumOverride(type: KClass<T>): Boolean {
return featurePrefs.containsEnum(type)
}
fun <T : Enum<T>> overrideEnum(value: T?, type: KClass<T>) {
if (value == null) {
featurePrefs.removeEnum(type)
} else {
featurePrefs.putEnum(value, type)
}
}
}
private fun <T : Enum<T>> SharedPreferences.removeEnum(type: KClass<T>) {
edit().remove("enum-${type.simpleName}").apply()
}
private fun <T : Enum<T>> SharedPreferences.containsEnum(type: KClass<T>): Boolean {
return contains("enum-${type.simpleName}")
}
private inline fun <reified T : Enum<T>> SharedPreferences.readEnum(): T? {
val value = T::class.simpleName
return getString("enum-$value", null)?.let { enumValueOf<T>(it) }
}
private fun <T : Enum<T>> SharedPreferences.putEnum(value: T, type: KClass<T>) {
edit()
.putString("enum-${type.simpleName}", value.name)
.apply()
}

View File

@ -0,0 +1,78 @@
/*
* Copyright 2019 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.features
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.TextView
import com.airbnb.epoxy.EpoxyAttribute
import com.airbnb.epoxy.EpoxyModelClass
import im.vector.app.core.epoxy.VectorEpoxyHolder
import im.vector.app.core.epoxy.VectorEpoxyModel
@EpoxyModelClass(layout = im.vector.app.R.layout.item_feature)
abstract class EnumFeatureItem : VectorEpoxyModel<EnumFeatureItem.Holder>() {
@EpoxyAttribute
lateinit var feature: Feature.EnumFeature<*>
@EpoxyAttribute(EpoxyAttribute.Option.DoNotHash)
var listener: Listener? = null
override fun bind(holder: Holder) {
super.bind(holder)
holder.label.text = feature.label
holder.optionsSpinner.apply {
val arrayAdapter = ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item)
arrayAdapter.add("DEFAULT - ${feature.default.name}")
arrayAdapter.addAll(feature.options.map { it.name })
adapter = arrayAdapter
feature.selection?.let {
setSelection(feature.options.indexOf(it) + 1, false)
}
onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
when (position) {
0 -> listener?.onOptionSelected(option = null, feature)
else -> {
val option: Any = feature.options[position - 1]
listener?.onOptionSelected(option, feature)
}
}
}
override fun onNothingSelected(parent: AdapterView<*>?) {
// do nothing
}
}
}
}
class Holder : VectorEpoxyHolder() {
val label by bind<TextView>(im.vector.app.R.id.feature_label)
val optionsSpinner by bind<Spinner>(im.vector.app.R.id.feature_options)
}
interface Listener {
fun <T : Enum<T>> onOptionSelected(option: Any?, feature: Feature.EnumFeature<T>)
}
}

View File

@ -0,0 +1,52 @@
/*
* Copyright 2019 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.features
import com.airbnb.epoxy.TypedEpoxyController
import kotlin.reflect.KClass
data class FeaturesState(
val features: List<Feature>
)
sealed interface Feature {
data class EnumFeature<T : Enum<T>>(
val label: String,
val selection: T?,
val default: T,
val options: List<T>,
val type: KClass<T>
) : Feature
}
class FeaturesController(private val listener: EnumFeatureItem.Listener) : TypedEpoxyController<FeaturesState>() {
override fun buildModels(data: FeaturesState?) {
if (data == null) return
data.features.forEachIndexed { index, feature ->
when (feature) {
is Feature.EnumFeature<*> -> enumFeatureItem {
id(index)
feature(feature)
listener(this@FeaturesController.listener)
}
}
}
}
}

View File

@ -20,6 +20,12 @@
android:padding="@dimen/layout_horizontal_margin"
android:showDividers="middle">
<Button
android:id="@+id/debug_features"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Features" />
<Button
android:id="@+id/debug_private_setting"
android:layout_width="wrap_content"

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="4dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="4dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/feature_label"
style="@style/Widget.Vector.TextView.Subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="?vctr_content_primary"
tools:text="Login version" />
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/feature_options"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>

View File

@ -31,8 +31,6 @@ import im.vector.app.core.error.DefaultErrorFormatter
import im.vector.app.core.error.ErrorFormatter
import im.vector.app.core.time.Clock
import im.vector.app.core.time.DefaultClock
import im.vector.app.features.DefaultVectorFeatures
import im.vector.app.features.VectorFeatures
import im.vector.app.features.invite.AutoAcceptInvites
import im.vector.app.features.invite.CompileTimeAutoAcceptInvites
import im.vector.app.features.navigation.DefaultNavigator
@ -135,9 +133,4 @@ object VectorStaticModule {
fun providesCoroutineDispatchers(): CoroutineDispatchers {
return CoroutineDispatchers(io = Dispatchers.IO, computation = Dispatchers.Default)
}
@Provides
fun providesFeatures(): VectorFeatures {
return DefaultVectorFeatures()
}
}

View File

@ -0,0 +1,34 @@
/*
* Copyright (c) 2021 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.di
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import im.vector.app.features.DefaultVectorFeatures
import im.vector.app.features.VectorFeatures
@InstallIn(SingletonComponent::class)
@Module
object FeaturesModule {
@Provides
fun providesFeatures(): VectorFeatures {
return DefaultVectorFeatures()
}
}