mirror of
https://invent.kde.org/network/kdeconnect-android.git
synced 2025-12-12 20:35:58 +01:00
Settings: Replace startActivityForResult with registerForActivityResult
We have to do this everywhere eventually, since startActivityForResult and onActivityResult are deprecated.
This commit is contained in:
@@ -22,6 +22,7 @@ import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.EditText
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.preference.EditTextPreference
|
||||
import androidx.preference.ListPreference
|
||||
@@ -168,21 +169,26 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
||||
}
|
||||
}
|
||||
|
||||
val activityLauncherWithDevicesByIpRefresh = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
||||
updateDevicesByIpSummary()
|
||||
}
|
||||
|
||||
private fun trustedNetworkPref(context: Context) = Preference(context).apply {
|
||||
isPersistent = false
|
||||
setTitle(R.string.trusted_networks)
|
||||
setSummary(R.string.trusted_networks_desc)
|
||||
onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
||||
startActivityForResult(Intent(context, TrustedNetworksActivity::class.java), REQUEST_REFRESH_NETWORKS)
|
||||
activityLauncherWithNetworkRefresh.launch(Intent(context, TrustedNetworksActivity::class.java))
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
private val REQUEST_REFRESH_DEVICES_BY_IP = 1
|
||||
private val REQUEST_REFRESH_NETWORKS = 2
|
||||
|
||||
private lateinit var devicesByIpPref : Preference
|
||||
|
||||
val activityLauncherWithNetworkRefresh = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
||||
BackgroundService.instance?.onNetworkChange(null)
|
||||
}
|
||||
|
||||
/** Opens activity to configure device by IP when clicked */
|
||||
private fun devicesByIpPref(context: Context) = Preference(context).apply {
|
||||
devicesByIpPref = this
|
||||
@@ -190,19 +196,11 @@ class SettingsFragment : PreferenceFragmentCompat() {
|
||||
setTitle(R.string.custom_device_list)
|
||||
updateDevicesByIpSummary()
|
||||
onPreferenceClickListener = Preference.OnPreferenceClickListener {
|
||||
startActivityForResult(Intent(context, CustomDevicesActivity::class.java), REQUEST_REFRESH_DEVICES_BY_IP)
|
||||
activityLauncherWithDevicesByIpRefresh.launch(Intent(context, CustomDevicesActivity::class.java))
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
when (requestCode) {
|
||||
REQUEST_REFRESH_DEVICES_BY_IP -> updateDevicesByIpSummary()
|
||||
REQUEST_REFRESH_NETWORKS -> BackgroundService.instance?.onNetworkChange(null)
|
||||
else -> super.onActivityResult(requestCode, resultCode, data)
|
||||
}
|
||||
}
|
||||
|
||||
private fun updateDevicesByIpSummary() {
|
||||
devicesByIpPref.setSummary(getString(
|
||||
R.string.custom_devices_settings_summary,
|
||||
|
||||
Reference in New Issue
Block a user