- Better names for some UserAgent, Call, and Account functions.

This commit is contained in:
Juha Heinanen
2020-05-05 17:15:58 +03:00
parent 67a81de7c3
commit 590e9caded
12 changed files with 49 additions and 60 deletions
@@ -192,28 +192,12 @@ class Account(val accp: String) {
return res return res
} }
fun find(accp: String): Account? { fun ofAor(aor: String): Account? {
for (ua in UserAgent.uas()) { for (ua in UserAgent.uas())
if (ua.account.accp == accp) return ua.account if (ua.account.aor == aor) return ua.account
}
return null return null
} }
fun findUa(aor: String): UserAgent? {
for (ua in UserAgent.uas()) {
if (ua.account.aor == aor) return ua
}
return null
}
fun exists(aor: String): Boolean {
for (ua in UserAgent.uas()) {
if (ua.account.aor.split(":")[1] == aor.split(":")[0])
return true
}
return false
}
fun checkDisplayName(dn: String): Boolean { fun checkDisplayName(dn: String): Boolean {
if (dn == "") return true if (dn == "") return true
val dnRegex = Regex("^([* .!%_`'~]|[+]|[-a-zA-Z0-9]){1,63}\$") val dnRegex = Regex("^([* .!%_`'~]|[+]|[-a-zA-Z0-9]){1,63}\$")
@@ -49,7 +49,7 @@ class AccountActivity : AppCompatActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
aor = intent.getStringExtra("aor")!! aor = intent.getStringExtra("aor")!!
ua = Account.findUa(aor)!! ua = UserAgent.ofAor(aor)!!
acc = ua.account acc = ua.account
uaIndex = UserAgent.findAorIndex(aor)!! uaIndex = UserAgent.findAorIndex(aor)!!
@@ -45,7 +45,7 @@ class AccountsActivity : AppCompatActivity() {
Log.d("Baresip", "Invalid Address of Record $aor") Log.d("Baresip", "Invalid Address of Record $aor")
Utils.alertView(this, getString(R.string.notice), Utils.alertView(this, getString(R.string.notice),
String.format(getString(R.string.invalid_aor), aor)) String.format(getString(R.string.invalid_aor), aor))
} else if (Account.exists(aor)) { } else if (Account.ofAor(aor) != null) {
Log.d("Baresip", "Account $aor already exists") Log.d("Baresip", "Account $aor already exists")
Utils.alertView(this, getString(R.string.notice), Utils.alertView(this, getString(R.string.notice),
String.format(getString(R.string.account_exists), aor.split(":")[0])) String.format(getString(R.string.account_exists), aor.split(":")[0]))
@@ -85,7 +85,7 @@ class AccountsActivity : AppCompatActivity() {
ACCOUNT_CODE -> { ACCOUNT_CODE -> {
if (resultCode == Activity.RESULT_OK) { if (resultCode == Activity.RESULT_OK) {
val aor = data!!.getStringExtra("aor")!! val aor = data!!.getStringExtra("aor")!!
val ua = Account.findUa(aor)!! val ua = UserAgent.ofAor(aor)!!
if (aorPasswords.containsKey(aor) && aorPasswords[aor] == "") if (aorPasswords.containsKey(aor) && aorPasswords[aor] == "")
askPassword(String.format(getString(R.string.account_password), askPassword(String.format(getString(R.string.account_password),
Utils.plainAor(aor)), ua) Utils.plainAor(aor)), ua)
@@ -337,7 +337,7 @@ class BaresipService: Service() {
"Call Reject" -> { "Call Reject" -> {
val callp = intent!!.getStringExtra("callp")!! val callp = intent!!.getStringExtra("callp")!!
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w(LOG_TAG, "onStartCommand did not find call $callp") Log.w(LOG_TAG, "onStartCommand did not find call $callp")
} else { } else {
@@ -354,7 +354,7 @@ class BaresipService: Service() {
"Transfer Show", "Transfer Accept" -> { "Transfer Show", "Transfer Accept" -> {
val uap = intent!!.getStringExtra("uap")!! val uap = intent!!.getStringExtra("uap")!!
val ua = UserAgent.find(uap) val ua = UserAgent.ofUap(uap)
if (ua == null) { if (ua == null) {
Log.w(LOG_TAG, "onStartCommand did not find ua $uap") Log.w(LOG_TAG, "onStartCommand did not find ua $uap")
} else { } else {
@@ -371,7 +371,7 @@ class BaresipService: Service() {
"Transfer Deny" -> { "Transfer Deny" -> {
val callp = intent!!.getStringExtra("callp")!! val callp = intent!!.getStringExtra("callp")!!
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) if (call == null)
Log.w(LOG_TAG, "onStartCommand did not find call $callp") Log.w(LOG_TAG, "onStartCommand did not find call $callp")
else else
@@ -392,7 +392,7 @@ class BaresipService: Service() {
"Message Save" -> { "Message Save" -> {
val uap = intent!!.getStringExtra("uap")!! val uap = intent!!.getStringExtra("uap")!!
val ua = UserAgent.find(uap) val ua = UserAgent.ofUap(uap)
if (ua == null) if (ua == null)
Log.w(LOG_TAG, "onStartCommand did not find UA $uap") Log.w(LOG_TAG, "onStartCommand did not find UA $uap")
else else
@@ -403,7 +403,7 @@ class BaresipService: Service() {
"Message Delete" -> { "Message Delete" -> {
val uap = intent!!.getStringExtra("uap")!! val uap = intent!!.getStringExtra("uap")!!
val ua = UserAgent.find(uap) val ua = UserAgent.ofUap(uap)
if (ua == null) if (ua == null)
Log.w(LOG_TAG, "onStartCommand did not find UA $uap") Log.w(LOG_TAG, "onStartCommand did not find UA $uap")
else else
@@ -480,7 +480,7 @@ class BaresipService: Service() {
@Keep @Keep
fun uaEvent(event: String, uap: String, callp: String) { fun uaEvent(event: String, uap: String, callp: String) {
if (!isServiceRunning) return if (!isServiceRunning) return
val ua = UserAgent.find(uap) val ua = UserAgent.ofUap(uap)
if (ua == null) { if (ua == null) {
Log.w(LOG_TAG, "uaEvent did not find ua $uap") Log.w(LOG_TAG, "uaEvent did not find ua $uap")
return return
@@ -637,7 +637,7 @@ class BaresipService: Service() {
} }
"call established" -> { "call established" -> {
nm.cancel(CALL_NOTIFICATION_ID) nm.cancel(CALL_NOTIFICATION_ID)
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w(LOG_TAG, "Call $callp that is established is not found") Log.w(LOG_TAG, "Call $callp that is established is not found")
return return
@@ -659,7 +659,7 @@ class BaresipService: Service() {
return return
} }
"call verified", "call secure" -> { "call verified", "call secure" -> {
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w("Baresip", "Call $callp that is verified is not found") Log.w("Baresip", "Call $callp that is verified is not found")
return return
@@ -674,7 +674,7 @@ class BaresipService: Service() {
return return
} }
"call transfer" -> { "call transfer" -> {
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w(LOG_TAG, "Call $callp to be transferred is not found") Log.w(LOG_TAG, "Call $callp to be transferred is not found")
return return
@@ -724,7 +724,7 @@ class BaresipService: Service() {
} }
"call closed" -> { "call closed" -> {
nm.cancel(CALL_NOTIFICATION_ID) nm.cancel(CALL_NOTIFICATION_ID)
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.d(LOG_TAG, "AoR $aor call $callp that is closed is not found") Log.d(LOG_TAG, "AoR $aor call $callp that is closed is not found")
return return
@@ -778,7 +778,7 @@ class BaresipService: Service() {
} catch (e: Exception) { } catch (e: Exception) {
Log.w(LOG_TAG, "UTF-8 decode failed") Log.w(LOG_TAG, "UTF-8 decode failed")
} }
val ua = UserAgent.find(uap) val ua = UserAgent.ofUap(uap)
if (ua == null) { if (ua == null) {
Log.w(LOG_TAG, "messageEvent did not find ua $uap") Log.w(LOG_TAG, "messageEvent did not find ua $uap")
return return
@@ -79,7 +79,7 @@ class Call(val callp: String, val ua: UserAgent, val peerURI: String, val dir: S
return result return result
} }
fun find(callp: String): Call? { fun ofCallp(callp: String): Call? {
for (c in BaresipService.calls) for (c in BaresipService.calls)
if (c.callp == callp) return c if (c.callp == callp) return c
return null return null
@@ -34,7 +34,7 @@ class CallsActivity : AppCompatActivity() {
aor = intent.getStringExtra("aor")!! aor = intent.getStringExtra("aor")!!
Utils.addActivity("calls,$aor") Utils.addActivity("calls,$aor")
val ua = Account.findUa(aor)!! val ua = UserAgent.ofAor(aor)!!
account = ua.account account = ua.account
val headerView = findViewById(R.id.account) as TextView val headerView = findViewById(R.id.account) as TextView
@@ -48,7 +48,7 @@ class ChatActivity : AppCompatActivity() {
Utils.addActivity("chat,$aor,$peerUri,$focus") Utils.addActivity("chat,$aor,$peerUri,$focus")
} }
val userAgent = Account.findUa(aor) val userAgent = UserAgent.ofAor(aor)
if (userAgent == null) { if (userAgent == null) {
Log.w("Baresip", "MessageActivity did not find ua of $aor") Log.w("Baresip", "MessageActivity did not find ua of $aor")
MainActivity.activityAor = aor MainActivity.activityAor = aor
@@ -155,7 +155,7 @@ class ChatsActivity: AppCompatActivity() {
Message.save() Message.save()
uaMessages.clear() uaMessages.clear()
clAdapter.notifyDataSetChanged() clAdapter.notifyDataSetChanged()
Account.findUa(aor)!!.account.unreadMessages = false UserAgent.ofAor(aor)!!.account.unreadMessages = false
dialog.dismiss() dialog.dismiss()
} }
deleteDialog.setNegativeButton(getText(R.string.cancel)) { dialog, _ -> deleteDialog.setNegativeButton(getText(R.string.cancel)) { dialog, _ ->
@@ -66,7 +66,7 @@ class ContactListAdapter(private val cxt: Context, private val rows: ArrayList<C
i.putExtra("action", "call") i.putExtra("action", "call")
else else
i.putExtra("action", "message") i.putExtra("action", "message")
val ua = Account.findUa(aor) val ua = UserAgent.ofAor(aor)
if (ua == null) { if (ua == null) {
Log.w("Baresip", "onClickListener did not find AoR $aor") Log.w("Baresip", "onClickListener did not find AoR $aor")
} else { } else {
@@ -472,7 +472,7 @@ class MainActivity : AppCompatActivity() {
return return
} }
val uap = intent.getStringExtra("uap")!! val uap = intent.getStringExtra("uap")!!
val ua = UserAgent.find(uap) val ua = UserAgent.ofUap(uap)
if (ua == null) { if (ua == null) {
Log.w("Baresip", "handleIntent 'call' did not find ua $uap") Log.w("Baresip", "handleIntent 'call' did not find ua $uap")
return return
@@ -484,7 +484,7 @@ class MainActivity : AppCompatActivity() {
} }
"call show", "call answer" -> { "call show", "call answer" -> {
val callp = intent.getStringExtra("callp")!! val callp = intent.getStringExtra("callp")!!
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w("Baresip", "handleIntent '$action' did not find call $callp") Log.w("Baresip", "handleIntent '$action' did not find call $callp")
return return
@@ -497,7 +497,7 @@ class MainActivity : AppCompatActivity() {
} }
"transfer show", "transfer accept" -> { "transfer show", "transfer accept" -> {
val callp = intent.getStringExtra("callp")!! val callp = intent.getStringExtra("callp")!!
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w("Baresip", "handleIntent '$action' did not find call $callp") Log.w("Baresip", "handleIntent '$action' did not find call $callp")
moveTaskToBack(true) moveTaskToBack(true)
@@ -509,7 +509,7 @@ class MainActivity : AppCompatActivity() {
} }
"message", "message show", "message reply" -> { "message", "message show", "message reply" -> {
val uap = intent.getStringExtra("uap")!! val uap = intent.getStringExtra("uap")!!
val ua = UserAgent.find(uap) val ua = UserAgent.ofUap(uap)
if (ua == null) { if (ua == null) {
Log.w("Baresip", "onNewIntent did not find ua $uap") Log.w("Baresip", "onNewIntent did not find ua $uap")
return return
@@ -609,7 +609,7 @@ class MainActivity : AppCompatActivity() {
return return
} }
val uap = params[0] val uap = params[0]
val ua = UserAgent.find(uap) val ua = UserAgent.ofUap(uap)
if (ua == null) { if (ua == null) {
Log.w("Baresip", "handleServiceEvent '$event' did not find ua $uap") Log.w("Baresip", "handleServiceEvent '$event' did not find ua $uap")
return return
@@ -652,7 +652,7 @@ class MainActivity : AppCompatActivity() {
Api.ua_hangup(uap, callp, 486, "Busy Here") Api.ua_hangup(uap, callp, 486, "Busy Here")
return return
} }
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w("Baresip", "Incoming call $callp not found") Log.w("Baresip", "Incoming call $callp not found")
return return
@@ -674,7 +674,7 @@ class MainActivity : AppCompatActivity() {
} }
"call established" -> { "call established" -> {
val callp = params[1] val callp = params[1]
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w("Baresip", "Established call $callp not found") Log.w("Baresip", "Established call $callp not found")
return return
@@ -688,7 +688,7 @@ class MainActivity : AppCompatActivity() {
} }
"call verify" -> { "call verify" -> {
val callp = params[1] val callp = params[1]
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w("Baresip", "Call $callp to be verified is not found") Log.w("Baresip", "Call $callp to be verified is not found")
return return
@@ -728,7 +728,7 @@ class MainActivity : AppCompatActivity() {
} }
"call verified", "call secure" -> { "call verified", "call secure" -> {
val callp = params[1] val callp = params[1]
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w("Baresip", "Call $callp that is verified is not found") Log.w("Baresip", "Call $callp that is verified is not found")
return return
@@ -745,7 +745,7 @@ class MainActivity : AppCompatActivity() {
} }
"call transfer", "transfer show" -> { "call transfer", "transfer show" -> {
val callp = params[1] val callp = params[1]
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w("Baresip", "Call $callp to be transferred is not found") Log.w("Baresip", "Call $callp to be transferred is not found")
return return
@@ -771,7 +771,7 @@ class MainActivity : AppCompatActivity() {
} }
"transfer accept" -> { "transfer accept" -> {
val callp = params[1] val callp = params[1]
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w("Baresip", "Call $callp to be transferred is not found") Log.w("Baresip", "Call $callp to be transferred is not found")
return return
@@ -928,7 +928,7 @@ class MainActivity : AppCompatActivity() {
uaAdapter.notifyDataSetChanged() uaAdapter.notifyDataSetChanged()
spinToAor(activityAor) spinToAor(activityAor)
if (aorSpinner.tag != "") if (aorSpinner.tag != "")
updateIcons(Account.findUa(aorSpinner.tag.toString())!!.account) updateIcons(Account.ofAor(aorSpinner.tag.toString())!!)
if (BaresipService.isServiceRunning) { if (BaresipService.isServiceRunning) {
baresipService.setAction("UpdateNotification") baresipService.setAction("UpdateNotification")
startService(baresipService) startService(baresipService)
@@ -937,7 +937,7 @@ class MainActivity : AppCompatActivity() {
ACCOUNT_CODE -> { ACCOUNT_CODE -> {
spinToAor(activityAor) spinToAor(activityAor)
val ua = Account.findUa(activityAor)!! val ua = UserAgent.ofAor(activityAor)!!
updateIcons(ua.account) updateIcons(ua.account)
if (resultCode == Activity.RESULT_OK) if (resultCode == Activity.RESULT_OK)
if (aorPasswords.containsKey(activityAor) && aorPasswords[activityAor] == "") if (aorPasswords.containsKey(activityAor) && aorPasswords[activityAor] == "")
@@ -972,7 +972,7 @@ class MainActivity : AppCompatActivity() {
CHATS_CODE, CHAT_CODE -> { CHATS_CODE, CHAT_CODE -> {
spinToAor(activityAor) spinToAor(activityAor)
updateIcons(Account.findUa(activityAor)!!.account) updateIcons(Account.ofAor(activityAor)!!)
} }
ABOUT_CODE -> { } ABOUT_CODE -> { }
@@ -35,6 +35,18 @@ class UserAgent(val uap: String) {
} }
} }
fun ofAor(aor: String): UserAgent? {
for (ua in BaresipService.uas)
if (ua.account.aor == aor) return ua
return null
}
fun ofUap(uap: String): UserAgent? {
for (ua in BaresipService.uas)
if (ua.uap == uap) return ua
return null
}
fun uaAlloc(uri: String): UserAgent? { fun uaAlloc(uri: String): UserAgent? {
val uap = Api.ua_alloc(uri) val uap = Api.ua_alloc(uri)
if (uap != "") return UserAgent(uap) if (uap != "") return UserAgent(uap)
@@ -42,13 +54,6 @@ class UserAgent(val uap: String) {
return null return null
} }
fun find(uap: String): UserAgent? {
for (ua in BaresipService.uas) {
if (ua.uap == uap) return ua
}
return null
}
fun findAorIndex(aor: String): Int? { fun findAorIndex(aor: String): Int? {
for (i in BaresipService.uas.indices) { for (i in BaresipService.uas.indices) {
if (BaresipService.uas[i].account.aor == aor) return i if (BaresipService.uas[i].account.aor == aor) return i
@@ -322,7 +322,7 @@ object Utils {
val text = sequence.subSequence(start, start + count).toString() val text = sequence.subSequence(start, start + count).toString()
if (text.length > 0) { if (text.length > 0) {
val digit = text[0] val digit = text[0]
val call = Call.find(callp) val call = Call.ofCallp(callp)
if (call == null) { if (call == null) {
Log.w("Baresip", "dtmfWatcher did not find call $callp") Log.w("Baresip", "dtmfWatcher did not find call $callp")
} else { } else {