Remove injection in dialog fragments

* Set result code in document activity
This commit is contained in:
Willi Ye 2021-02-03 18:41:46 +01:00 committed by ◱ Mark
parent a261ff8413
commit 696f0c7769
13 changed files with 44 additions and 59 deletions

View File

@ -26,7 +26,7 @@ object KeyReader {
* Reads keys file, trims and writes to internal app data storage, it makes sure file is properly formatted * 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 { 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)) if (!DocumentFile.isDocumentUri(context, uri))
return false return false

View File

@ -12,7 +12,7 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceGroup import androidx.preference.PreferenceGroup
import emu.skyline.databinding.SettingsActivityBinding import emu.skyline.databinding.SettingsActivityBinding
import emu.skyline.preference.ActivityResultDelegate import emu.skyline.preference.ActivityResultPreference
import emu.skyline.preference.DocumentActivity import emu.skyline.preference.DocumentActivity
class SettingsActivity : AppCompatActivity() { class SettingsActivity : AppCompatActivity() {
@ -56,7 +56,7 @@ class SettingsActivity : AppCompatActivity() {
private var requestCodeCounter = 0 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?) { fun delegateActivityResult(requestCode : Int, resultCode : Int, data : Intent?) {
preferenceScreen.delegateActivityResult(requestCode, resultCode, data) preferenceScreen.delegateActivityResult(requestCode, resultCode, data)
@ -74,7 +74,7 @@ class SettingsActivity : AppCompatActivity() {
for (i in 0 until preferenceCount) { for (i in 0 until preferenceCount) {
when (val pref = getPreference(i)) { when (val pref = getPreference(i)) {
is PreferenceGroup -> pref.assignActivityRequestCode() 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) { for (i in 0 until preferenceCount) {
when (val pref = getPreference(i)) { when (val pref = getPreference(i)) {
is PreferenceGroup -> pref.delegateActivityResult(requestCode, resultCode, data) is PreferenceGroup -> pref.delegateActivityResult(requestCode, resultCode, data)
is ActivityResultDelegate -> pref.onActivityResult(requestCode, resultCode, data) is ActivityResultPreference -> pref.onActivityResult(requestCode, resultCode, data)
} }
} }
} }

View File

@ -38,7 +38,7 @@ class ControllerGeneralViewItem(private val controllerId : Int, val type : Gener
} }
super.bind(holder, position) super.bind(holder, position)
holder.itemView.setOnClickListener { onClick.invoke(this, position) } holder.binding.root.setOnClickListener { onClick.invoke(this, position) }
} }
override fun areItemsTheSame(other : GenericListItem<ControllerItemBinding>) = other is ControllerGeneralViewItem && controllerId == other.controllerId override fun areItemsTheSame(other : GenericListItem<ControllerItemBinding>) = other is ControllerGeneralViewItem && controllerId == other.controllerId

View File

@ -41,7 +41,7 @@ class ControllerStickViewItem(private val controllerId : Int, val stick : StickI
super.bind(holder, position) super.bind(holder, position)
holder.itemView.setOnClickListener { onClick.invoke(this, position) } holder.binding.root.setOnClickListener { onClick.invoke(this, position) }
} }
override fun areItemsTheSame(other : GenericListItem<ControllerItemBinding>) = other is ControllerStickViewItem && controllerId == other.controllerId override fun areItemsTheSame(other : GenericListItem<ControllerItemBinding>) = other is ControllerStickViewItem && controllerId == other.controllerId

View File

@ -13,12 +13,11 @@ import android.view.*
import android.view.animation.LinearInterpolator import android.view.animation.LinearInterpolator
import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialogFragment import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.hilt.android.AndroidEntryPoint
import emu.skyline.R import emu.skyline.R
import emu.skyline.adapter.controller.ControllerButtonViewItem import emu.skyline.adapter.controller.ControllerButtonViewItem
import emu.skyline.databinding.ButtonDialogBinding import emu.skyline.databinding.ButtonDialogBinding
import emu.skyline.di.getInputManager
import emu.skyline.input.* import emu.skyline.input.*
import javax.inject.Inject
import kotlin.math.abs import kotlin.math.abs
/** /**
@ -26,12 +25,10 @@ import kotlin.math.abs
* *
* @param item This is used to hold the [ControllerButtonViewItem] between instances * @param item This is used to hold the [ControllerButtonViewItem] between instances
*/ */
@AndroidEntryPoint
class ButtonDialog @JvmOverloads constructor(private val item : ControllerButtonViewItem? = null) : BottomSheetDialogFragment() { class ButtonDialog @JvmOverloads constructor(private val item : ControllerButtonViewItem? = null) : BottomSheetDialogFragment() {
private lateinit var binding : ButtonDialogBinding private lateinit var binding : ButtonDialogBinding
@Inject private val inputManager by lazy { requireContext().getInputManager() }
lateinit var inputManager : InputManager
/** /**
* This inflates the layout of the dialog after initial view creation * This inflates the layout of the dialog after initial view creation

View File

@ -14,25 +14,21 @@ import android.view.*
import android.view.animation.LinearInterpolator import android.view.animation.LinearInterpolator
import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialogFragment import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dagger.hilt.android.AndroidEntryPoint
import emu.skyline.R import emu.skyline.R
import emu.skyline.adapter.controller.ControllerGeneralViewItem import emu.skyline.adapter.controller.ControllerGeneralViewItem
import emu.skyline.databinding.RumbleDialogBinding import emu.skyline.databinding.RumbleDialogBinding
import emu.skyline.di.getInputManager
import emu.skyline.input.ControllerActivity 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 * 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 * @param item This is used to hold the [ControllerGeneralViewItem] between instances
*/ */
@AndroidEntryPoint
class RumbleDialog @JvmOverloads constructor(val item : ControllerGeneralViewItem? = null) : BottomSheetDialogFragment() { class RumbleDialog @JvmOverloads constructor(val item : ControllerGeneralViewItem? = null) : BottomSheetDialogFragment() {
private lateinit var binding : RumbleDialogBinding private lateinit var binding : RumbleDialogBinding
@Inject private val inputManager by lazy { requireContext().getInputManager() }
lateinit var inputManager : InputManager
/** /**
* This inflates the layout of the dialog after initial view creation * This inflates the layout of the dialog after initial view creation

View File

@ -18,6 +18,7 @@ import dagger.hilt.android.AndroidEntryPoint
import emu.skyline.R import emu.skyline.R
import emu.skyline.adapter.controller.ControllerStickViewItem import emu.skyline.adapter.controller.ControllerStickViewItem
import emu.skyline.databinding.StickDialogBinding import emu.skyline.databinding.StickDialogBinding
import emu.skyline.di.getInputManager
import emu.skyline.input.* import emu.skyline.input.*
import emu.skyline.input.MotionHostEvent.Companion.axes import emu.skyline.input.MotionHostEvent.Companion.axes
import java.util.* import java.util.*
@ -30,7 +31,6 @@ import kotlin.math.max
* *
* @param item This is used to hold the [ControllerStickViewItem] between instances * @param item This is used to hold the [ControllerStickViewItem] between instances
*/ */
@AndroidEntryPoint
class StickDialog @JvmOverloads constructor(val item : ControllerStickViewItem? = null) : BottomSheetDialogFragment() { class StickDialog @JvmOverloads constructor(val item : ControllerStickViewItem? = null) : BottomSheetDialogFragment() {
/** /**
* This enumerates all of the stages this dialog can be in * 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 private var animationStop = false
@Inject private val inputManager by lazy { requireContext().getInputManager() }
lateinit var inputManager : InputManager
/** /**
* This inflates the layout of the dialog after initial view creation * This inflates the layout of the dialog after initial view creation

View File

@ -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?)
}

View File

@ -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?)
}

View File

@ -9,7 +9,6 @@ import android.app.Activity
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.util.AttributeSet import android.util.AttributeSet
import androidx.preference.Preference
import androidx.preference.Preference.SummaryProvider import androidx.preference.Preference.SummaryProvider
import emu.skyline.R import emu.skyline.R
import emu.skyline.di.getInputManager import emu.skyline.di.getInputManager
@ -18,14 +17,12 @@ import emu.skyline.input.ControllerActivity
/** /**
* This preference is used to launch [ControllerActivity] using a preference * 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 * The index of the controller this preference manages
*/ */
private var index = -1 private var index = -1
override var requestCode = 0
private val inputManager = context.getInputManager() private val inputManager = context.getInputManager()
init { init {

View File

@ -51,6 +51,7 @@ abstract class DocumentActivity : AppCompatActivity() {
.putString(keyName, uri.toString()) .putString(keyName, uri.toString())
.apply() .apply()
} }
setResult(resultCode)
finish() finish()
} }
} }

View File

@ -10,7 +10,6 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.util.AttributeSet import android.util.AttributeSet
import androidx.preference.Preference
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import com.google.android.material.snackbar.Snackbar import com.google.android.material.snackbar.Snackbar
import emu.skyline.KeyReader import emu.skyline.KeyReader
@ -20,14 +19,11 @@ import emu.skyline.SettingsActivity
/** /**
* Launches [FileActivity] and process the selected file for key import * 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 { class FilePreference @JvmOverloads constructor(context : Context?, attrs : AttributeSet? = null, defStyleAttr : Int = androidx.preference.R.attr.preferenceStyle) : ActivityResultPreference(context, attrs, defStyleAttr) {
override var requestCode = 0
override fun onClick() = (context as Activity).startActivityForResult(Intent(context, FileActivity::class.java).apply { putExtra(DocumentActivity.KEY_NAME, key) }, requestCode) 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?) { override fun onActivityResult(requestCode : Int, resultCode : Int, data : Intent?) {
if (this.requestCode == requestCode && requestCode == Activity.RESULT_OK) { if (this.requestCode == requestCode && resultCode == Activity.RESULT_OK && (key == KeyReader.KeyType.Prod.keyName || key == KeyReader.KeyType.Title.keyName)) {
if (key == "prod_keys" || key == "title_keys") {
val success = KeyReader.import( val success = KeyReader.import(
context, context,
Uri.parse(PreferenceManager.getDefaultSharedPreferences(context).getString(key, "")), Uri.parse(PreferenceManager.getDefaultSharedPreferences(context).getString(key, "")),
@ -36,5 +32,4 @@ class FilePreference @JvmOverloads constructor(context : Context?, attrs : Attri
Snackbar.make((context as SettingsActivity).binding.root, if (success) R.string.import_keys_success else R.string.import_keys_failed, Snackbar.LENGTH_LONG).show() Snackbar.make((context as SettingsActivity).binding.root, if (success) R.string.import_keys_success else R.string.import_keys_failed, Snackbar.LENGTH_LONG).show()
} }
} }
}
} }

View File

@ -10,16 +10,13 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.net.Uri import android.net.Uri
import android.util.AttributeSet import android.util.AttributeSet
import androidx.preference.Preference
import androidx.preference.Preference.SummaryProvider import androidx.preference.Preference.SummaryProvider
import androidx.preference.R import androidx.preference.R
/** /**
* This preference shows the decoded URI of it's preference and launches [DocumentActivity] * 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 { class FolderPreference @JvmOverloads constructor(context : Context?, attrs : AttributeSet? = null, defStyleAttr : Int = R.attr.preferenceStyle) : ActivityResultPreference(context, attrs, defStyleAttr) {
override var requestCode = 0
init { init {
summaryProvider = SummaryProvider<FolderPreference> { preference -> summaryProvider = SummaryProvider<FolderPreference> { preference ->
Uri.decode(preference.getPersistedString("")) Uri.decode(preference.getPersistedString(""))