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.view.View
|
||||||
import android.widget.*
|
import android.widget.*
|
||||||
import android.widget.AdapterView
|
import android.widget.AdapterView
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.appcompat.app.AppCompatDelegate
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
import com.tutpro.baresip.Utils.copyInputStreamToFile
|
import com.tutpro.baresip.Utils.copyInputStreamToFile
|
||||||
@@ -25,9 +26,6 @@ class ConfigActivity : AppCompatActivity() {
|
|||||||
private val READ_CERT_PERMISSION_CODE = 1
|
private val READ_CERT_PERMISSION_CODE = 1
|
||||||
private val READ_CA_PERMISSION_CODE = 2
|
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 binding: ActivityConfigBinding
|
||||||
private lateinit var autoStart: CheckBox
|
private lateinit var autoStart: CheckBox
|
||||||
private lateinit var listenAddr: EditText
|
private lateinit var listenAddr: EditText
|
||||||
@@ -88,6 +86,33 @@ class ConfigActivity : AppCompatActivity() {
|
|||||||
oldCertificateFile = Config.variable("sip_certificate").isNotEmpty()
|
oldCertificateFile = Config.variable("sip_certificate").isNotEmpty()
|
||||||
certificateFile.isChecked = oldCertificateFile
|
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 ->
|
certificateFile.setOnCheckedChangeListener { _, isChecked ->
|
||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
if (Build.VERSION.SDK_INT < 29) {
|
if (Build.VERSION.SDK_INT < 29) {
|
||||||
@@ -112,7 +137,7 @@ class ConfigActivity : AppCompatActivity() {
|
|||||||
save = true
|
save = true
|
||||||
restart = true
|
restart = true
|
||||||
} else {
|
} else {
|
||||||
Utils.selectInputFile(this, CERTIFICATE_CODE)
|
Utils.selectInputFile(certificateRequest)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Config.removeVariable("sip_certificate")
|
Config.removeVariable("sip_certificate")
|
||||||
@@ -130,6 +155,30 @@ class ConfigActivity : AppCompatActivity() {
|
|||||||
oldCAFile = Config.variable("sip_cafile").isNotEmpty()
|
oldCAFile = Config.variable("sip_cafile").isNotEmpty()
|
||||||
caFile.isChecked = oldCAFile
|
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 ->
|
caFile.setOnCheckedChangeListener { _, isChecked ->
|
||||||
if (isChecked) {
|
if (isChecked) {
|
||||||
if (Build.VERSION.SDK_INT < 29) {
|
if (Build.VERSION.SDK_INT < 29) {
|
||||||
@@ -154,7 +203,7 @@ class ConfigActivity : AppCompatActivity() {
|
|||||||
save = true
|
save = true
|
||||||
restart = true
|
restart = true
|
||||||
} else {
|
} else {
|
||||||
Utils.selectInputFile(this, CA_CERTIFICATES_CODE)
|
Utils.selectInputFile(certificatesRequest)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Config.removeVariable("sip_cafile")
|
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>,
|
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>,
|
||||||
grantResults: IntArray) {
|
grantResults: IntArray) {
|
||||||
|
|
||||||
|
|||||||
@@ -21,10 +21,10 @@ import android.text.Editable
|
|||||||
import android.text.TextWatcher
|
import android.text.TextWatcher
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.widget.TextView
|
import android.widget.TextView
|
||||||
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
import androidx.annotation.RequiresApi
|
import androidx.annotation.RequiresApi
|
||||||
import androidx.appcompat.app.AlertDialog
|
import androidx.appcompat.app.AlertDialog
|
||||||
import androidx.core.app.ActivityCompat
|
import androidx.core.app.ActivityCompat
|
||||||
import androidx.core.app.ActivityCompat.startActivityForResult
|
|
||||||
import androidx.core.content.ContextCompat
|
import androidx.core.content.ContextCompat
|
||||||
|
|
||||||
import java.io.*
|
import java.io.*
|
||||||
@@ -109,15 +109,15 @@ object Utils {
|
|||||||
var u = uri
|
var u = uri
|
||||||
if (uri.startsWith("<") && (uri.endsWith(">")))
|
if (uri.startsWith("<") && (uri.endsWith(">")))
|
||||||
u = uri.substring(1).substringBeforeLast(">")
|
u = uri.substring(1).substringBeforeLast(">")
|
||||||
if (u.contains("@")) {
|
return if (u.contains("@")) {
|
||||||
val user = uriUserPart(u)
|
val user = uriUserPart(u)
|
||||||
val host = uriHostPart(u)
|
val host = uriHostPart(u)
|
||||||
return if (isE164Number(user) || (host == domain))
|
if (isE164Number(user) || (host == domain))
|
||||||
user
|
user
|
||||||
else
|
else
|
||||||
"$user@$host"
|
"$user@$host"
|
||||||
} else {
|
} else {
|
||||||
return u
|
u
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -188,11 +188,11 @@ object Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun checkIpPort(ipPort: String): Boolean {
|
fun checkIpPort(ipPort: String): Boolean {
|
||||||
if (ipPort.startsWith("["))
|
return if (ipPort.startsWith("["))
|
||||||
return checkIpv6InBrackets(ipPort.substringBeforeLast(":")) &&
|
checkIpv6InBrackets(ipPort.substringBeforeLast(":")) &&
|
||||||
checkPort(ipPort.substringAfterLast(":"))
|
checkPort(ipPort.substringAfterLast(":"))
|
||||||
else
|
else
|
||||||
return checkIpV4(ipPort.substringBeforeLast(":")) &&
|
checkIpV4(ipPort.substringBeforeLast(":")) &&
|
||||||
checkPort(ipPort.substringAfterLast(":"))
|
checkPort(ipPort.substringAfterLast(":"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -232,10 +232,10 @@ object Utils {
|
|||||||
|
|
||||||
fun checkHostPortParams(hpp: String) : Boolean {
|
fun checkHostPortParams(hpp: String) : Boolean {
|
||||||
val restParams = hpp.split(";", limit = 2)
|
val restParams = hpp.split(";", limit = 2)
|
||||||
if (restParams.size == 1)
|
return if (restParams.size == 1)
|
||||||
return checkHostPort(restParams[0])
|
checkHostPort(restParams[0])
|
||||||
else
|
else
|
||||||
return checkHostPort(restParams[0]) && checkParams(restParams[1])
|
checkHostPort(restParams[0]) && checkParams(restParams[1])
|
||||||
}
|
}
|
||||||
|
|
||||||
fun checkSipUri(uri: String): Boolean {
|
fun checkSipUri(uri: String): Boolean {
|
||||||
@@ -282,14 +282,14 @@ object Utils {
|
|||||||
var servers = ""
|
var servers = ""
|
||||||
for (dnsServer in list) {
|
for (dnsServer in list) {
|
||||||
var address = dnsServer.hostAddress.removePrefix("/")
|
var address = dnsServer.hostAddress.removePrefix("/")
|
||||||
if (checkIpV4(address))
|
address = if (checkIpV4(address))
|
||||||
address = "${address}:53"
|
"${address}:53"
|
||||||
else
|
else
|
||||||
address = "[${address}]:53"
|
"[${address}]:53"
|
||||||
if (servers == "")
|
servers = if (servers == "")
|
||||||
servers = address
|
address
|
||||||
else
|
else
|
||||||
servers = "${servers},${address}"
|
"${servers},${address}"
|
||||||
}
|
}
|
||||||
return servers
|
return servers
|
||||||
}
|
}
|
||||||
@@ -343,10 +343,10 @@ object Utils {
|
|||||||
fun implode(list: List<String>, sep: String): String {
|
fun implode(list: List<String>, sep: String): String {
|
||||||
var res = ""
|
var res = ""
|
||||||
for (s in list) {
|
for (s in list) {
|
||||||
if (res == "")
|
res = if (res == "")
|
||||||
res = s
|
s
|
||||||
else
|
else
|
||||||
res = res + sep + s
|
res + sep + s
|
||||||
}
|
}
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
@@ -463,13 +463,13 @@ object Utils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(29)
|
@RequiresApi(29)
|
||||||
fun selectInputFile(activity: Activity, activityCode: Int) {
|
fun selectInputFile(request: ActivityResultLauncher<Intent>) {
|
||||||
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
|
||||||
addCategory(Intent.CATEGORY_OPENABLE)
|
addCategory(Intent.CATEGORY_OPENABLE)
|
||||||
type = "*/*"
|
type = "*/*"
|
||||||
putExtra(DocumentsContract.EXTRA_INITIAL_URI, MediaStore.Downloads.EXTERNAL_CONTENT_URI)
|
putExtra(DocumentsContract.EXTRA_INITIAL_URI, MediaStore.Downloads.EXTERNAL_CONTENT_URI)
|
||||||
}
|
}
|
||||||
startActivityForResult(activity, intent, activityCode, null)
|
request.launch(intent)
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(29)
|
@RequiresApi(29)
|
||||||
|
|||||||
Reference in New Issue
Block a user