Replace deprecated ActivityResults with ResultLauncher

This commit is contained in:
Niko Diamadis
2023-07-18 21:54:41 +02:00
parent 70c7375abb
commit 69fc25571a

View File

@@ -8,6 +8,7 @@ import android.os.Bundle
import android.text.Editable import android.text.Editable
import android.text.TextWatcher import android.text.TextWatcher
import android.view.View import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import com.github.gotify.R import com.github.gotify.R
import com.github.gotify.SSLSettings import com.github.gotify.SSLSettings
@@ -35,11 +36,6 @@ import java.security.cert.X509Certificate
import okhttp3.HttpUrl import okhttp3.HttpUrl
internal class LoginActivity : AppCompatActivity() { internal class LoginActivity : AppCompatActivity() {
companion object {
// return value from startActivityForResult when choosing a certificate
private const val FILE_SELECT_CODE = 1
}
private lateinit var binding: ActivityLoginBinding private lateinit var binding: ActivityLoginBinding
private lateinit var settings: Settings private lateinit var settings: Settings
@@ -47,6 +43,27 @@ internal class LoginActivity : AppCompatActivity() {
private var caCertContents: String? = null private var caCertContents: String? = null
private lateinit var advancedDialog: AdvancedDialog private lateinit var advancedDialog: AdvancedDialog
private val certificateDialogResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
try {
require(result.resultCode == RESULT_OK) { "result was ${result.resultCode}" }
requireNotNull(result.data) { "file path was null" }
val uri = result.data!!.data ?: throw IllegalArgumentException("file path was null")
val fileStream = contentResolver.openInputStream(uri)
?: throw IllegalArgumentException("file path was invalid")
val content = Utils.readFileFromStream(fileStream)
val name = getNameOfCertContent(content)
// temporarily set the contents (don't store to settings until they decide to login)
caCertContents = content
advancedDialog.showRemoveCACertificate(name)
} catch (e: Exception) {
Utils.showSnackBar(this, getString(R.string.select_ca_failed, e.message))
}
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
UncaughtExceptionHandler.registerCurrentThread() UncaughtExceptionHandler.registerCurrentThread()
@@ -152,9 +169,8 @@ internal class LoginActivity : AppCompatActivity() {
intent.addCategory(Intent.CATEGORY_OPENABLE) intent.addCategory(Intent.CATEGORY_OPENABLE)
try { try {
startActivityForResult( certificateDialogResultLauncher.launch(
Intent.createChooser(intent, getString(R.string.select_ca_file)), Intent.createChooser(intent, getString(R.string.select_ca_file))
FILE_SELECT_CODE
) )
} catch (e: ActivityNotFoundException) { } catch (e: ActivityNotFoundException) {
// case for user not having a file browser installed // case for user not having a file browser installed
@@ -162,30 +178,6 @@ internal class LoginActivity : AppCompatActivity() {
} }
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
try {
if (requestCode == FILE_SELECT_CODE) {
require(resultCode == RESULT_OK) { "result was $resultCode" }
requireNotNull(data) { "file path was null" }
val uri = data.data ?: throw IllegalArgumentException("file path was null")
val fileStream = contentResolver.openInputStream(uri)
?: throw IllegalArgumentException("file path was invalid")
val content = Utils.readFileFromStream(fileStream)
val name = getNameOfCertContent(content)
// temporarily set the contents (don't store to settings until they decide to login)
caCertContents = content
advancedDialog.showRemoveCACertificate(name)
}
} catch (e: Exception) {
Utils.showSnackBar(this, getString(R.string.select_ca_failed, e.message))
}
}
private fun getNameOfCertContent(content: String): String { private fun getNameOfCertContent(content: String): String {
val ca = CertUtils.parseCertificate(content) val ca = CertUtils.parseCertificate(content)
return (ca as X509Certificate).subjectDN.name return (ca as X509Certificate).subjectDN.name