Replace deprecated ActivityResults with ResultLauncher
This commit is contained in:
@@ -8,6 +8,7 @@ import android.os.Bundle
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.view.View
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import com.github.gotify.R
|
||||
import com.github.gotify.SSLSettings
|
||||
@@ -35,11 +36,6 @@ import java.security.cert.X509Certificate
|
||||
import okhttp3.HttpUrl
|
||||
|
||||
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 settings: Settings
|
||||
|
||||
@@ -47,6 +43,27 @@ internal class LoginActivity : AppCompatActivity() {
|
||||
private var caCertContents: String? = null
|
||||
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?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
UncaughtExceptionHandler.registerCurrentThread()
|
||||
@@ -152,9 +169,8 @@ internal class LoginActivity : AppCompatActivity() {
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE)
|
||||
|
||||
try {
|
||||
startActivityForResult(
|
||||
Intent.createChooser(intent, getString(R.string.select_ca_file)),
|
||||
FILE_SELECT_CODE
|
||||
certificateDialogResultLauncher.launch(
|
||||
Intent.createChooser(intent, getString(R.string.select_ca_file))
|
||||
)
|
||||
} catch (e: ActivityNotFoundException) {
|
||||
// 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 {
|
||||
val ca = CertUtils.parseCertificate(content)
|
||||
return (ca as X509Certificate).subjectDN.name
|
||||
|
||||
Reference in New Issue
Block a user