Support deprecation of getSerializableExtra

This commit is contained in:
Abandoned Cart 2023-03-09 15:29:38 -05:00 committed by Billy Laws
parent 6d582566f9
commit 6703a875f0
5 changed files with 20 additions and 4 deletions

View File

@ -25,6 +25,7 @@ import emu.skyline.data.AppItemTag
import emu.skyline.databinding.AppDialogBinding
import emu.skyline.loader.LoaderResult
import emu.skyline.settings.SettingsActivity
import emu.skyline.utils.serializable
/**
* This dialog is used to show extra game metadata and provide extra options such as pinning the game to the home screen
@ -46,7 +47,7 @@ class AppDialog : BottomSheetDialogFragment() {
private lateinit var binding : AppDialogBinding
private val item by lazy { requireArguments().getSerializable(AppItemTag)!! as AppItem }
private val item by lazy { requireArguments().serializable<AppItem>(AppItemTag)!! }
/**
* This inflates the layout of the dialog after initial view creation

View File

@ -37,6 +37,7 @@ import emu.skyline.settings.EmulationSettings
import emu.skyline.settings.NativeSettings
import emu.skyline.utils.ByteBufferSerializable
import emu.skyline.utils.GpuDriverHelper
import emu.skyline.utils.serializable
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.util.concurrent.FutureTask
@ -218,7 +219,7 @@ class EmulationActivity : AppCompatActivity(), SurfaceHolder.Callback, View.OnTo
* Populates the [item] member with data from the intent
*/
private fun populateAppItem() {
val intentItem = intent.getSerializableExtra(AppItemTag) as AppItem?
val intentItem = intent.serializable(AppItemTag) as AppItem?
if (intentItem != null) {
item = intentItem
return

View File

@ -29,6 +29,7 @@ import emu.skyline.settings.EmulationSettings
import emu.skyline.utils.GpuDriverHelper
import emu.skyline.utils.GpuDriverInstallResult
import emu.skyline.utils.WindowInsetsHelper
import emu.skyline.utils.serializable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@ -40,7 +41,7 @@ import kotlinx.coroutines.launch
class GpuDriverActivity : AppCompatActivity() {
private val binding by lazy { GpuDriverActivityBinding.inflate(layoutInflater) }
private val item by lazy { intent.extras?.getSerializable(AppItemTag) as AppItem? }
private val item by lazy { intent.extras?.serializable(AppItemTag) as AppItem? }
private val adapter = SelectableGenericAdapter(0)

View File

@ -17,6 +17,7 @@ import emu.skyline.preference.GpuDriverPreference
import emu.skyline.preference.IntegerListPreference
import emu.skyline.utils.GpuDriverHelper
import emu.skyline.utils.WindowInsetsHelper
import emu.skyline.utils.serializable
/**
* This fragment is used to display custom game preferences
@ -26,7 +27,7 @@ class GameSettingsFragment : PreferenceFragmentCompat() {
private const val DIALOG_FRAGMENT_TAG = "androidx.preference.PreferenceFragment.DIALOG"
}
private val item by lazy { requireArguments().getSerializable(AppItemTag)!! as AppItem }
private val item by lazy { requireArguments().serializable<AppItem>(AppItemTag)!! }
override fun onViewCreated(view : View, savedInstanceState : Bundle?) {
super.onViewCreated(view, savedInstanceState)

View File

@ -5,11 +5,23 @@
package emu.skyline.utils
import android.content.Intent
import android.os.Build
import android.os.Bundle
import java.io.File
import java.io.ObjectInputStream
import java.io.ObjectOutputStream
import java.io.Serializable
inline fun <reified T : Serializable> Bundle.serializable(key: String): T? = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializable(key, T::class.java)
else -> @Suppress("DEPRECATION") getSerializable(key) as? T
}
inline fun <reified T : Serializable> Intent.serializable(key: String): T? = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU -> getSerializableExtra(key, T::class.java)
else -> @Suppress("DEPRECATION") getSerializableExtra(key) as? T
}
fun <T : Serializable> T.toFile(file : File) {
ObjectOutputStream(file.outputStream()).use { it.writeObject(this) }
}