2020-03-27 20:36:02 +01:00
|
|
|
/*
|
2020-04-19 23:04:05 +02:00
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2020-03-27 20:36:02 +01:00
|
|
|
* Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/)
|
|
|
|
*/
|
|
|
|
|
2019-12-02 14:39:08 +01:00
|
|
|
package emu.skyline
|
|
|
|
|
|
|
|
import android.content.Intent
|
|
|
|
import android.os.Bundle
|
2020-05-28 07:39:36 +02:00
|
|
|
import android.view.KeyEvent
|
2019-12-02 14:39:08 +01:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2020-05-28 21:27:25 +02:00
|
|
|
import androidx.preference.Preference
|
2019-12-02 14:39:08 +01:00
|
|
|
import androidx.preference.PreferenceFragmentCompat
|
2020-05-28 21:27:25 +02:00
|
|
|
import emu.skyline.input.InputManager
|
|
|
|
import emu.skyline.preference.ControllerPreference
|
2020-04-12 22:29:19 +02:00
|
|
|
import kotlinx.android.synthetic.main.titlebar.*
|
2019-12-02 14:39:08 +01:00
|
|
|
|
|
|
|
class SettingsActivity : AppCompatActivity() {
|
2020-04-12 22:29:19 +02:00
|
|
|
/**
|
|
|
|
* This is the instance of [PreferenceFragment] that is shown inside [R.id.settings]
|
|
|
|
*/
|
2020-08-16 17:45:46 +02:00
|
|
|
private val preferenceFragment = PreferenceFragment()
|
2019-12-02 14:39:08 +01:00
|
|
|
|
2020-04-12 22:29:19 +02:00
|
|
|
/**
|
2020-05-28 21:27:25 +02:00
|
|
|
* This is an instance of [InputManager] used by [ControllerPreference]
|
|
|
|
*/
|
|
|
|
lateinit var inputManager : InputManager
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The key of the element to force a refresh when [onActivityResult] is called
|
|
|
|
*/
|
|
|
|
var refreshKey : String? = null
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This initializes all of the elements in the activity and displays the settings fragment
|
2020-04-12 22:29:19 +02:00
|
|
|
*/
|
2020-04-24 13:39:13 +02:00
|
|
|
override fun onCreate(savedInstanceState : Bundle?) {
|
2019-12-02 14:39:08 +01:00
|
|
|
super.onCreate(savedInstanceState)
|
2020-04-12 22:29:19 +02:00
|
|
|
|
2020-05-28 21:27:25 +02:00
|
|
|
inputManager = InputManager(this)
|
|
|
|
|
2019-12-02 14:39:08 +01:00
|
|
|
setContentView(R.layout.settings_activity)
|
2020-04-12 22:29:19 +02:00
|
|
|
|
|
|
|
setSupportActionBar(toolbar)
|
|
|
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
|
|
|
|
2019-12-02 14:39:08 +01:00
|
|
|
supportFragmentManager
|
|
|
|
.beginTransaction()
|
|
|
|
.replace(R.id.settings, preferenceFragment)
|
|
|
|
.commit()
|
|
|
|
}
|
|
|
|
|
2020-04-12 22:29:19 +02:00
|
|
|
/**
|
2020-05-28 21:27:25 +02:00
|
|
|
* This is used to refresh the preferences after [emu.skyline.preference.FolderActivity] or [emu.skyline.input.ControllerActivity] has returned
|
2020-04-12 22:29:19 +02:00
|
|
|
*/
|
2020-04-24 13:39:13 +02:00
|
|
|
public override fun onActivityResult(requestCode : Int, resultCode : Int, data : Intent?) {
|
2019-12-02 14:39:08 +01:00
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
2020-05-28 21:27:25 +02:00
|
|
|
|
2020-08-20 20:31:32 +02:00
|
|
|
refreshKey?.let {
|
2020-05-28 21:27:25 +02:00
|
|
|
inputManager.syncObjects()
|
|
|
|
preferenceFragment.refreshPreference(refreshKey!!)
|
|
|
|
|
|
|
|
refreshKey = null
|
|
|
|
}
|
2019-12-02 14:39:08 +01:00
|
|
|
}
|
|
|
|
|
2020-04-12 22:29:19 +02:00
|
|
|
/**
|
|
|
|
* This fragment is used to display all of the preferences and handle refreshing the preferences
|
|
|
|
*/
|
2019-12-02 14:39:08 +01:00
|
|
|
class PreferenceFragment : PreferenceFragmentCompat() {
|
2020-04-12 22:29:19 +02:00
|
|
|
/**
|
2020-05-28 21:27:25 +02:00
|
|
|
* This forces refreshing a certain preference by indirectly calling [Preference.notifyChanged]
|
2020-04-12 22:29:19 +02:00
|
|
|
*/
|
2020-05-28 21:27:25 +02:00
|
|
|
fun refreshPreference(key : String) {
|
|
|
|
val preference = preferenceManager.findPreference<Preference>(key)!!
|
|
|
|
preference.isSelectable = !preference.isSelectable
|
|
|
|
preference.isSelectable = !preference.isSelectable
|
2019-12-02 14:39:08 +01:00
|
|
|
}
|
|
|
|
|
2020-04-12 22:29:19 +02:00
|
|
|
/**
|
|
|
|
* This constructs the preferences from [R.xml.preferences]
|
|
|
|
*/
|
2020-04-24 13:39:13 +02:00
|
|
|
override fun onCreatePreferences(savedInstanceState : Bundle?, rootKey : String?) {
|
2019-12-02 14:39:08 +01:00
|
|
|
setPreferencesFromResource(R.xml.preferences, rootKey)
|
|
|
|
}
|
|
|
|
}
|
2020-05-28 07:39:36 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This handles on calling [onBackPressed] when [KeyEvent.KEYCODE_BUTTON_B] is lifted
|
|
|
|
*/
|
|
|
|
override fun onKeyUp(keyCode : Int, event : KeyEvent?) : Boolean {
|
|
|
|
if (keyCode == KeyEvent.KEYCODE_BUTTON_B) {
|
|
|
|
onBackPressed()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return super.onKeyUp(keyCode, event)
|
|
|
|
}
|
2019-12-02 14:39:08 +01:00
|
|
|
}
|