From 696f0c7769c35bed4a9b465d9aa9271cd4e364b8 Mon Sep 17 00:00:00 2001 From: Willi Ye Date: Wed, 3 Feb 2021 18:41:46 +0100 Subject: [PATCH] Remove injection in dialog fragments * Set result code in document activity --- app/src/main/java/emu/skyline/KeyReader.kt | 2 +- .../main/java/emu/skyline/SettingsActivity.kt | 8 +++---- .../controller/ControllerGeneralViewItem.kt | 2 +- .../controller/ControllerStickViewItem.kt | 2 +- .../emu/skyline/input/dialog/ButtonDialog.kt | 7 ++----- .../emu/skyline/input/dialog/RumbleDialog.kt | 8 ++----- .../emu/skyline/input/dialog/StickDialog.kt | 5 ++--- .../preference/ActivityResultDelegate.kt | 17 --------------- .../preference/ActivityResultPreference.kt | 20 ++++++++++++++++++ .../preference/ControllerPreference.kt | 5 +---- .../skyline/preference/DocumentActivity.kt | 1 + .../emu/skyline/preference/FilePreference.kt | 21 +++++++------------ .../skyline/preference/FolderPreference.kt | 5 +---- 13 files changed, 44 insertions(+), 59 deletions(-) delete mode 100644 app/src/main/java/emu/skyline/preference/ActivityResultDelegate.kt create mode 100644 app/src/main/java/emu/skyline/preference/ActivityResultPreference.kt diff --git a/app/src/main/java/emu/skyline/KeyReader.kt b/app/src/main/java/emu/skyline/KeyReader.kt index 4198dadc..ceeb20b7 100644 --- a/app/src/main/java/emu/skyline/KeyReader.kt +++ b/app/src/main/java/emu/skyline/KeyReader.kt @@ -26,7 +26,7 @@ object KeyReader { * Reads keys file, trims and writes to internal app data storage, it makes sure file is properly formatted */ fun import(context : Context, uri : Uri, keyType : KeyType) : Boolean { - Log.i(Tag, "Parsing ${keyType.name}") + Log.i(Tag, "Parsing ${keyType.name} $uri") if (!DocumentFile.isDocumentUri(context, uri)) return false diff --git a/app/src/main/java/emu/skyline/SettingsActivity.kt b/app/src/main/java/emu/skyline/SettingsActivity.kt index aa121b54..a497533b 100644 --- a/app/src/main/java/emu/skyline/SettingsActivity.kt +++ b/app/src/main/java/emu/skyline/SettingsActivity.kt @@ -12,7 +12,7 @@ import androidx.appcompat.app.AppCompatActivity import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceGroup import emu.skyline.databinding.SettingsActivityBinding -import emu.skyline.preference.ActivityResultDelegate +import emu.skyline.preference.ActivityResultPreference import emu.skyline.preference.DocumentActivity class SettingsActivity : AppCompatActivity() { @@ -56,7 +56,7 @@ class SettingsActivity : AppCompatActivity() { private var requestCodeCounter = 0 /** - * Delegates activity result to all preferences which implement [ActivityResultDelegate] + * Delegates activity result to all preferences which implement [ActivityResultPreference] */ fun delegateActivityResult(requestCode : Int, resultCode : Int, data : Intent?) { preferenceScreen.delegateActivityResult(requestCode, resultCode, data) @@ -74,7 +74,7 @@ class SettingsActivity : AppCompatActivity() { for (i in 0 until preferenceCount) { when (val pref = getPreference(i)) { is PreferenceGroup -> pref.assignActivityRequestCode() - is ActivityResultDelegate -> pref.requestCode = requestCodeCounter++ + is ActivityResultPreference -> pref.requestCode = requestCodeCounter++ } } } @@ -83,7 +83,7 @@ class SettingsActivity : AppCompatActivity() { for (i in 0 until preferenceCount) { when (val pref = getPreference(i)) { is PreferenceGroup -> pref.delegateActivityResult(requestCode, resultCode, data) - is ActivityResultDelegate -> pref.onActivityResult(requestCode, resultCode, data) + is ActivityResultPreference -> pref.onActivityResult(requestCode, resultCode, data) } } } diff --git a/app/src/main/java/emu/skyline/adapter/controller/ControllerGeneralViewItem.kt b/app/src/main/java/emu/skyline/adapter/controller/ControllerGeneralViewItem.kt index 491b40c0..08c83144 100644 --- a/app/src/main/java/emu/skyline/adapter/controller/ControllerGeneralViewItem.kt +++ b/app/src/main/java/emu/skyline/adapter/controller/ControllerGeneralViewItem.kt @@ -38,7 +38,7 @@ class ControllerGeneralViewItem(private val controllerId : Int, val type : Gener } super.bind(holder, position) - holder.itemView.setOnClickListener { onClick.invoke(this, position) } + holder.binding.root.setOnClickListener { onClick.invoke(this, position) } } override fun areItemsTheSame(other : GenericListItem) = other is ControllerGeneralViewItem && controllerId == other.controllerId diff --git a/app/src/main/java/emu/skyline/adapter/controller/ControllerStickViewItem.kt b/app/src/main/java/emu/skyline/adapter/controller/ControllerStickViewItem.kt index 5cb75e03..67638742 100644 --- a/app/src/main/java/emu/skyline/adapter/controller/ControllerStickViewItem.kt +++ b/app/src/main/java/emu/skyline/adapter/controller/ControllerStickViewItem.kt @@ -41,7 +41,7 @@ class ControllerStickViewItem(private val controllerId : Int, val stick : StickI super.bind(holder, position) - holder.itemView.setOnClickListener { onClick.invoke(this, position) } + holder.binding.root.setOnClickListener { onClick.invoke(this, position) } } override fun areItemsTheSame(other : GenericListItem) = other is ControllerStickViewItem && controllerId == other.controllerId diff --git a/app/src/main/java/emu/skyline/input/dialog/ButtonDialog.kt b/app/src/main/java/emu/skyline/input/dialog/ButtonDialog.kt index 7358aa74..e2bece55 100644 --- a/app/src/main/java/emu/skyline/input/dialog/ButtonDialog.kt +++ b/app/src/main/java/emu/skyline/input/dialog/ButtonDialog.kt @@ -13,12 +13,11 @@ import android.view.* import android.view.animation.LinearInterpolator import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetDialogFragment -import dagger.hilt.android.AndroidEntryPoint import emu.skyline.R import emu.skyline.adapter.controller.ControllerButtonViewItem import emu.skyline.databinding.ButtonDialogBinding +import emu.skyline.di.getInputManager import emu.skyline.input.* -import javax.inject.Inject import kotlin.math.abs /** @@ -26,12 +25,10 @@ import kotlin.math.abs * * @param item This is used to hold the [ControllerButtonViewItem] between instances */ -@AndroidEntryPoint class ButtonDialog @JvmOverloads constructor(private val item : ControllerButtonViewItem? = null) : BottomSheetDialogFragment() { private lateinit var binding : ButtonDialogBinding - @Inject - lateinit var inputManager : InputManager + private val inputManager by lazy { requireContext().getInputManager() } /** * This inflates the layout of the dialog after initial view creation diff --git a/app/src/main/java/emu/skyline/input/dialog/RumbleDialog.kt b/app/src/main/java/emu/skyline/input/dialog/RumbleDialog.kt index fc2404fc..e68dc611 100644 --- a/app/src/main/java/emu/skyline/input/dialog/RumbleDialog.kt +++ b/app/src/main/java/emu/skyline/input/dialog/RumbleDialog.kt @@ -14,25 +14,21 @@ import android.view.* import android.view.animation.LinearInterpolator import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetDialogFragment -import dagger.hilt.android.AndroidEntryPoint import emu.skyline.R import emu.skyline.adapter.controller.ControllerGeneralViewItem import emu.skyline.databinding.RumbleDialogBinding +import emu.skyline.di.getInputManager import emu.skyline.input.ControllerActivity -import emu.skyline.input.InputManager -import javax.inject.Inject /** * This dialog is used to set a device to pass on any rumble/force feedback data onto * * @param item This is used to hold the [ControllerGeneralViewItem] between instances */ -@AndroidEntryPoint class RumbleDialog @JvmOverloads constructor(val item : ControllerGeneralViewItem? = null) : BottomSheetDialogFragment() { private lateinit var binding : RumbleDialogBinding - @Inject - lateinit var inputManager : InputManager + private val inputManager by lazy { requireContext().getInputManager() } /** * This inflates the layout of the dialog after initial view creation diff --git a/app/src/main/java/emu/skyline/input/dialog/StickDialog.kt b/app/src/main/java/emu/skyline/input/dialog/StickDialog.kt index 0cda9168..055c97a8 100644 --- a/app/src/main/java/emu/skyline/input/dialog/StickDialog.kt +++ b/app/src/main/java/emu/skyline/input/dialog/StickDialog.kt @@ -18,6 +18,7 @@ import dagger.hilt.android.AndroidEntryPoint import emu.skyline.R import emu.skyline.adapter.controller.ControllerStickViewItem import emu.skyline.databinding.StickDialogBinding +import emu.skyline.di.getInputManager import emu.skyline.input.* import emu.skyline.input.MotionHostEvent.Companion.axes import java.util.* @@ -30,7 +31,6 @@ import kotlin.math.max * * @param item This is used to hold the [ControllerStickViewItem] between instances */ -@AndroidEntryPoint class StickDialog @JvmOverloads constructor(val item : ControllerStickViewItem? = null) : BottomSheetDialogFragment() { /** * This enumerates all of the stages this dialog can be in @@ -66,8 +66,7 @@ class StickDialog @JvmOverloads constructor(val item : ControllerStickViewItem? */ private var animationStop = false - @Inject - lateinit var inputManager : InputManager + private val inputManager by lazy { requireContext().getInputManager() } /** * This inflates the layout of the dialog after initial view creation diff --git a/app/src/main/java/emu/skyline/preference/ActivityResultDelegate.kt b/app/src/main/java/emu/skyline/preference/ActivityResultDelegate.kt deleted file mode 100644 index 735b1d6c..00000000 --- a/app/src/main/java/emu/skyline/preference/ActivityResultDelegate.kt +++ /dev/null @@ -1,17 +0,0 @@ -/* - * SPDX-License-Identifier: MPL-2.0 - * Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/) - */ - -package emu.skyline.preference - -import android.content.Intent - -/** - * Some preferences need results from activities, this delegates the results to them - */ -interface ActivityResultDelegate { - var requestCode : Int - - fun onActivityResult(requestCode : Int, resultCode : Int, data : Intent?) -} diff --git a/app/src/main/java/emu/skyline/preference/ActivityResultPreference.kt b/app/src/main/java/emu/skyline/preference/ActivityResultPreference.kt new file mode 100644 index 00000000..d1a8cc29 --- /dev/null +++ b/app/src/main/java/emu/skyline/preference/ActivityResultPreference.kt @@ -0,0 +1,20 @@ +/* + * SPDX-License-Identifier: MPL-2.0 + * Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/) + */ + +package emu.skyline.preference + +import android.content.Context +import android.content.Intent +import android.util.AttributeSet +import androidx.preference.Preference + +/** + * Some preferences need results from activities, this delegates the results to them + */ +abstract class ActivityResultPreference @JvmOverloads constructor(context : Context?, attrs : AttributeSet? = null, defStyleAttr : Int = 0) : Preference(context, attrs, defStyleAttr) { + var requestCode = 0 + + abstract fun onActivityResult(requestCode : Int, resultCode : Int, data : Intent?) +} diff --git a/app/src/main/java/emu/skyline/preference/ControllerPreference.kt b/app/src/main/java/emu/skyline/preference/ControllerPreference.kt index ef36ac84..abba6367 100644 --- a/app/src/main/java/emu/skyline/preference/ControllerPreference.kt +++ b/app/src/main/java/emu/skyline/preference/ControllerPreference.kt @@ -9,7 +9,6 @@ import android.app.Activity import android.content.Context import android.content.Intent import android.util.AttributeSet -import androidx.preference.Preference import androidx.preference.Preference.SummaryProvider import emu.skyline.R import emu.skyline.di.getInputManager @@ -18,14 +17,12 @@ import emu.skyline.input.ControllerActivity /** * This preference is used to launch [ControllerActivity] using a preference */ -class ControllerPreference @JvmOverloads constructor(context : Context, attrs : AttributeSet? = null, defStyleAttr : Int = R.attr.preferenceStyle) : Preference(context, attrs, defStyleAttr), ActivityResultDelegate { +class ControllerPreference @JvmOverloads constructor(context : Context, attrs : AttributeSet? = null, defStyleAttr : Int = R.attr.preferenceStyle) : ActivityResultPreference(context, attrs, defStyleAttr) { /** * The index of the controller this preference manages */ private var index = -1 - override var requestCode = 0 - private val inputManager = context.getInputManager() init { diff --git a/app/src/main/java/emu/skyline/preference/DocumentActivity.kt b/app/src/main/java/emu/skyline/preference/DocumentActivity.kt index cbaf2680..cd8cb21f 100644 --- a/app/src/main/java/emu/skyline/preference/DocumentActivity.kt +++ b/app/src/main/java/emu/skyline/preference/DocumentActivity.kt @@ -51,6 +51,7 @@ abstract class DocumentActivity : AppCompatActivity() { .putString(keyName, uri.toString()) .apply() } + setResult(resultCode) finish() } } diff --git a/app/src/main/java/emu/skyline/preference/FilePreference.kt b/app/src/main/java/emu/skyline/preference/FilePreference.kt index 02af3c68..8d8176cf 100644 --- a/app/src/main/java/emu/skyline/preference/FilePreference.kt +++ b/app/src/main/java/emu/skyline/preference/FilePreference.kt @@ -10,7 +10,6 @@ import android.content.Context import android.content.Intent import android.net.Uri import android.util.AttributeSet -import androidx.preference.Preference import androidx.preference.PreferenceManager import com.google.android.material.snackbar.Snackbar import emu.skyline.KeyReader @@ -20,21 +19,17 @@ import emu.skyline.SettingsActivity /** * Launches [FileActivity] and process the selected file for key import */ -class FilePreference @JvmOverloads constructor(context : Context?, attrs : AttributeSet? = null, defStyleAttr : Int = androidx.preference.R.attr.preferenceStyle) : Preference(context, attrs, defStyleAttr), ActivityResultDelegate { - override var requestCode = 0 - +class FilePreference @JvmOverloads constructor(context : Context?, attrs : AttributeSet? = null, defStyleAttr : Int = androidx.preference.R.attr.preferenceStyle) : ActivityResultPreference(context, attrs, defStyleAttr) { override fun onClick() = (context as Activity).startActivityForResult(Intent(context, FileActivity::class.java).apply { putExtra(DocumentActivity.KEY_NAME, key) }, requestCode) override fun onActivityResult(requestCode : Int, resultCode : Int, data : Intent?) { - if (this.requestCode == requestCode && requestCode == Activity.RESULT_OK) { - if (key == "prod_keys" || key == "title_keys") { - val success = KeyReader.import( - context, - Uri.parse(PreferenceManager.getDefaultSharedPreferences(context).getString(key, "")), - KeyReader.KeyType.parse(key) - ) - Snackbar.make((context as SettingsActivity).binding.root, if (success) R.string.import_keys_success else R.string.import_keys_failed, Snackbar.LENGTH_LONG).show() - } + if (this.requestCode == requestCode && resultCode == Activity.RESULT_OK && (key == KeyReader.KeyType.Prod.keyName || key == KeyReader.KeyType.Title.keyName)) { + val success = KeyReader.import( + context, + Uri.parse(PreferenceManager.getDefaultSharedPreferences(context).getString(key, "")), + KeyReader.KeyType.parse(key) + ) + Snackbar.make((context as SettingsActivity).binding.root, if (success) R.string.import_keys_success else R.string.import_keys_failed, Snackbar.LENGTH_LONG).show() } } } diff --git a/app/src/main/java/emu/skyline/preference/FolderPreference.kt b/app/src/main/java/emu/skyline/preference/FolderPreference.kt index 1c468ed3..1af46377 100644 --- a/app/src/main/java/emu/skyline/preference/FolderPreference.kt +++ b/app/src/main/java/emu/skyline/preference/FolderPreference.kt @@ -10,16 +10,13 @@ import android.content.Context import android.content.Intent import android.net.Uri import android.util.AttributeSet -import androidx.preference.Preference import androidx.preference.Preference.SummaryProvider import androidx.preference.R /** * This preference shows the decoded URI of it's preference and launches [DocumentActivity] */ -class FolderPreference @JvmOverloads constructor(context : Context?, attrs : AttributeSet? = null, defStyleAttr : Int = R.attr.preferenceStyle) : Preference(context, attrs, defStyleAttr), ActivityResultDelegate { - override var requestCode = 0 - +class FolderPreference @JvmOverloads constructor(context : Context?, attrs : AttributeSet? = null, defStyleAttr : Int = R.attr.preferenceStyle) : ActivityResultPreference(context, attrs, defStyleAttr) { init { summaryProvider = SummaryProvider { preference -> Uri.decode(preference.getPersistedString(""))