Avoid warnings in ConfigActivity and Utils
This commit is contained in:
@ -12,6 +12,7 @@ import android.view.MenuItem
|
||||
import android.view.View
|
||||
import android.widget.*
|
||||
import android.widget.AdapterView
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatDelegate
|
||||
import com.tutpro.baresip.Utils.copyInputStreamToFile
|
||||
@ -25,9 +26,6 @@ class ConfigActivity : AppCompatActivity() {
|
||||
private val READ_CERT_PERMISSION_CODE = 1
|
||||
private val READ_CA_PERMISSION_CODE = 2
|
||||
|
||||
private val CERTIFICATE_CODE = 1
|
||||
private val CA_CERTIFICATES_CODE = 2
|
||||
|
||||
private lateinit var binding: ActivityConfigBinding
|
||||
private lateinit var autoStart: CheckBox
|
||||
private lateinit var listenAddr: EditText
|
||||
@ -88,6 +86,33 @@ class ConfigActivity : AppCompatActivity() {
|
||||
oldCertificateFile = Config.variable("sip_certificate").isNotEmpty()
|
||||
certificateFile.isChecked = oldCertificateFile
|
||||
|
||||
val certificateRequest =
|
||||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
||||
if (it.resultCode == RESULT_OK) {
|
||||
it.data?.data?.also { uri ->
|
||||
try {
|
||||
val inputStream = applicationContext.contentResolver.openInputStream(uri)
|
||||
as FileInputStream
|
||||
File(BaresipService.filesPath + "/cert.pem")
|
||||
.copyInputStreamToFile(inputStream)
|
||||
inputStream.close()
|
||||
Config.removeVariable("sip_certificate")
|
||||
Config.addLine("sip_certificate ${BaresipService.filesPath}/cert.pem")
|
||||
save = true
|
||||
restart = true
|
||||
} catch (e: Error) {
|
||||
Utils.alertView(
|
||||
this, getString(R.string.error),
|
||||
getString(R.string.read_cert_error)
|
||||
)
|
||||
certificateFile.isChecked = false
|
||||
}
|
||||
}
|
||||
} else {
|
||||
certificateFile.isChecked = false
|
||||
}
|
||||
}
|
||||
|
||||
certificateFile.setOnCheckedChangeListener { _, isChecked ->
|
||||
if (isChecked) {
|
||||
if (Build.VERSION.SDK_INT < 29) {
|
||||
@ -112,7 +137,7 @@ class ConfigActivity : AppCompatActivity() {
|
||||
save = true
|
||||
restart = true
|
||||
} else {
|
||||
Utils.selectInputFile(this, CERTIFICATE_CODE)
|
||||
Utils.selectInputFile(certificateRequest)
|
||||
}
|
||||
} else {
|
||||
Config.removeVariable("sip_certificate")
|
||||
@ -130,6 +155,30 @@ class ConfigActivity : AppCompatActivity() {
|
||||
oldCAFile = Config.variable("sip_cafile").isNotEmpty()
|
||||
caFile.isChecked = oldCAFile
|
||||
|
||||
val certificatesRequest =
|
||||
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
||||
if (it.resultCode == Activity.RESULT_OK)
|
||||
it.data?.data?.also { uri ->
|
||||
try {
|
||||
val inputStream = applicationContext.contentResolver.openInputStream(uri)
|
||||
as FileInputStream
|
||||
File(BaresipService.filesPath + "/ca_certs.crt")
|
||||
.copyInputStreamToFile(inputStream)
|
||||
inputStream.close()
|
||||
Config.removeVariable("sip_cafile")
|
||||
Config.addLine("sip_cafile ${BaresipService.filesPath}/ca_certs.crt")
|
||||
save = true
|
||||
restart = true
|
||||
} catch (e: Error) {
|
||||
Utils.alertView(this, getString(R.string.error),
|
||||
getString(R.string.read_ca_certs_error))
|
||||
caFile.isChecked = false
|
||||
}
|
||||
}
|
||||
else
|
||||
caFile.isChecked = false
|
||||
}
|
||||
|
||||
caFile.setOnCheckedChangeListener { _, isChecked ->
|
||||
if (isChecked) {
|
||||
if (Build.VERSION.SDK_INT < 29) {
|
||||
@ -154,7 +203,7 @@ class ConfigActivity : AppCompatActivity() {
|
||||
save = true
|
||||
restart = true
|
||||
} else {
|
||||
Utils.selectInputFile(this, CA_CERTIFICATES_CODE)
|
||||
Utils.selectInputFile(certificatesRequest)
|
||||
}
|
||||
} else {
|
||||
Config.removeVariable("sip_cafile")
|
||||
@ -366,61 +415,6 @@ class ConfigActivity : AppCompatActivity() {
|
||||
|
||||
}
|
||||
|
||||
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
||||
|
||||
super.onActivityResult(requestCode, resultCode, data)
|
||||
|
||||
when (requestCode) {
|
||||
|
||||
CERTIFICATE_CODE -> {
|
||||
if (resultCode == Activity.RESULT_OK)
|
||||
data?.data?.also {
|
||||
try {
|
||||
val inputStream = applicationContext.contentResolver.openInputStream(it)
|
||||
as FileInputStream
|
||||
File(BaresipService.filesPath + "/cert.pem")
|
||||
.copyInputStreamToFile(inputStream)
|
||||
inputStream.close()
|
||||
Config.removeVariable("sip_certificate")
|
||||
Config.addLine("sip_certificate ${BaresipService.filesPath}/cert.pem")
|
||||
save = true
|
||||
restart = true
|
||||
} catch (e: Error) {
|
||||
Utils.alertView(this, getString(R.string.error),
|
||||
getString(R.string.read_cert_error))
|
||||
certificateFile.isChecked = false
|
||||
}
|
||||
}
|
||||
else
|
||||
certificateFile.isChecked = false
|
||||
}
|
||||
|
||||
CA_CERTIFICATES_CODE -> {
|
||||
if (resultCode == Activity.RESULT_OK)
|
||||
data?.data?.also {
|
||||
try {
|
||||
val inputStream = applicationContext.contentResolver.openInputStream(it)
|
||||
as FileInputStream
|
||||
File(BaresipService.filesPath + "/ca_certs.crt")
|
||||
.copyInputStreamToFile(inputStream)
|
||||
inputStream.close()
|
||||
Config.removeVariable("sip_cafile")
|
||||
Config.addLine("sip_cafile ${BaresipService.filesPath}/ca_certs.crt")
|
||||
save = true
|
||||
restart = true
|
||||
} catch (e: Error) {
|
||||
Utils.alertView(this, getString(R.string.error),
|
||||
getString(R.string.read_ca_certs_error))
|
||||
caFile.isChecked = false
|
||||
}
|
||||
}
|
||||
else
|
||||
caFile.isChecked = false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
|
||||
grantResults: IntArray) {
|
||||
|
||||
|
||||
@ -21,10 +21,10 @@ import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.view.View
|
||||
import android.widget.TextView
|
||||
import androidx.activity.result.ActivityResultLauncher
|
||||
import androidx.annotation.RequiresApi
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.core.app.ActivityCompat
|
||||
import androidx.core.app.ActivityCompat.startActivityForResult
|
||||
import androidx.core.content.ContextCompat
|
||||
|
||||
import java.io.*
|
||||
@ -109,15 +109,15 @@ object Utils {
|
||||
var u = uri
|
||||
if (uri.startsWith("<") && (uri.endsWith(">")))
|
||||
u = uri.substring(1).substringBeforeLast(">")
|
||||
if (u.contains("@")) {
|
||||
return if (u.contains("@")) {
|
||||
val user = uriUserPart(u)
|
||||
val host = uriHostPart(u)
|
||||
return if (isE164Number(user) || (host == domain))
|
||||
if (isE164Number(user) || (host == domain))
|
||||
user
|
||||
else
|
||||
"$user@$host"
|
||||
} else {
|
||||
return u
|
||||
u
|
||||
}
|
||||
}
|
||||
|
||||
@ -188,11 +188,11 @@ object Utils {
|
||||
}
|
||||
|
||||
fun checkIpPort(ipPort: String): Boolean {
|
||||
if (ipPort.startsWith("["))
|
||||
return checkIpv6InBrackets(ipPort.substringBeforeLast(":")) &&
|
||||
return if (ipPort.startsWith("["))
|
||||
checkIpv6InBrackets(ipPort.substringBeforeLast(":")) &&
|
||||
checkPort(ipPort.substringAfterLast(":"))
|
||||
else
|
||||
return checkIpV4(ipPort.substringBeforeLast(":")) &&
|
||||
checkIpV4(ipPort.substringBeforeLast(":")) &&
|
||||
checkPort(ipPort.substringAfterLast(":"))
|
||||
}
|
||||
|
||||
@ -232,10 +232,10 @@ object Utils {
|
||||
|
||||
fun checkHostPortParams(hpp: String) : Boolean {
|
||||
val restParams = hpp.split(";", limit = 2)
|
||||
if (restParams.size == 1)
|
||||
return checkHostPort(restParams[0])
|
||||
return if (restParams.size == 1)
|
||||
checkHostPort(restParams[0])
|
||||
else
|
||||
return checkHostPort(restParams[0]) && checkParams(restParams[1])
|
||||
checkHostPort(restParams[0]) && checkParams(restParams[1])
|
||||
}
|
||||
|
||||
fun checkSipUri(uri: String): Boolean {
|
||||
@ -282,14 +282,14 @@ object Utils {
|
||||
var servers = ""
|
||||
for (dnsServer in list) {
|
||||
var address = dnsServer.hostAddress.removePrefix("/")
|
||||
if (checkIpV4(address))
|
||||
address = "${address}:53"
|
||||
address = if (checkIpV4(address))
|
||||
"${address}:53"
|
||||
else
|
||||
address = "[${address}]:53"
|
||||
if (servers == "")
|
||||
servers = address
|
||||
"[${address}]:53"
|
||||
servers = if (servers == "")
|
||||
address
|
||||
else
|
||||
servers = "${servers},${address}"
|
||||
"${servers},${address}"
|
||||
}
|
||||
return servers
|
||||
}
|
||||
@ -343,10 +343,10 @@ object Utils {
|
||||
fun implode(list: List<String>, sep: String): String {
|
||||
var res = ""
|
||||
for (s in list) {
|
||||
if (res == "")
|
||||
res = s
|
||||
res = if (res == "")
|
||||
s
|
||||
else
|
||||
res = res + sep + s
|
||||
res + sep + s
|
||||
}
|
||||
return res
|
||||
}
|
||||
@ -463,13 +463,13 @@ object Utils {
|
||||
}
|
||||
|
||||
@RequiresApi(29)
|
||||
fun selectInputFile(activity: Activity, activityCode: Int) {
|
||||
fun selectInputFile(request: ActivityResultLauncher<Intent>) {
|
||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
||||
addCategory(Intent.CATEGORY_OPENABLE)
|
||||
type = "*/*"
|
||||
putExtra(DocumentsContract.EXTRA_INITIAL_URI, MediaStore.Downloads.EXTERNAL_CONTENT_URI)
|
||||
}
|
||||
startActivityForResult(activity, intent, activityCode, null)
|
||||
request.launch(intent)
|
||||
}
|
||||
|
||||
@RequiresApi(29)
|
||||
|
||||
Reference in New Issue
Block a user