- 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

View File

@ -192,28 +192,12 @@ class Account(val accp: String) {
return res
}
fun find(accp: String): Account? {
for (ua in UserAgent.uas()) {
if (ua.account.accp == accp) return ua.account
}
fun ofAor(aor: String): Account? {
for (ua in UserAgent.uas())
if (ua.account.aor == aor) return ua.account
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 {
if (dn == "") return true
val dnRegex = Regex("^([* .!%_`'~]|[+]|[-a-zA-Z0-9]){1,63}\$")

View File

@ -49,7 +49,7 @@ class AccountActivity : AppCompatActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
aor = intent.getStringExtra("aor")!!
ua = Account.findUa(aor)!!
ua = UserAgent.ofAor(aor)!!
acc = ua.account
uaIndex = UserAgent.findAorIndex(aor)!!

View File

@ -45,7 +45,7 @@ class AccountsActivity : AppCompatActivity() {
Log.d("Baresip", "Invalid Address of Record $aor")
Utils.alertView(this, getString(R.string.notice),
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")
Utils.alertView(this, getString(R.string.notice),
String.format(getString(R.string.account_exists), aor.split(":")[0]))
@ -85,7 +85,7 @@ class AccountsActivity : AppCompatActivity() {
ACCOUNT_CODE -> {
if (resultCode == Activity.RESULT_OK) {
val aor = data!!.getStringExtra("aor")!!
val ua = Account.findUa(aor)!!
val ua = UserAgent.ofAor(aor)!!
if (aorPasswords.containsKey(aor) && aorPasswords[aor] == "")
askPassword(String.format(getString(R.string.account_password),
Utils.plainAor(aor)), ua)

View File

@ -337,7 +337,7 @@ class BaresipService: Service() {
"Call Reject" -> {
val callp = intent!!.getStringExtra("callp")!!
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w(LOG_TAG, "onStartCommand did not find call $callp")
} else {
@ -354,7 +354,7 @@ class BaresipService: Service() {
"Transfer Show", "Transfer Accept" -> {
val uap = intent!!.getStringExtra("uap")!!
val ua = UserAgent.find(uap)
val ua = UserAgent.ofUap(uap)
if (ua == null) {
Log.w(LOG_TAG, "onStartCommand did not find ua $uap")
} else {
@ -371,7 +371,7 @@ class BaresipService: Service() {
"Transfer Deny" -> {
val callp = intent!!.getStringExtra("callp")!!
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null)
Log.w(LOG_TAG, "onStartCommand did not find call $callp")
else
@ -392,7 +392,7 @@ class BaresipService: Service() {
"Message Save" -> {
val uap = intent!!.getStringExtra("uap")!!
val ua = UserAgent.find(uap)
val ua = UserAgent.ofUap(uap)
if (ua == null)
Log.w(LOG_TAG, "onStartCommand did not find UA $uap")
else
@ -403,7 +403,7 @@ class BaresipService: Service() {
"Message Delete" -> {
val uap = intent!!.getStringExtra("uap")!!
val ua = UserAgent.find(uap)
val ua = UserAgent.ofUap(uap)
if (ua == null)
Log.w(LOG_TAG, "onStartCommand did not find UA $uap")
else
@ -480,7 +480,7 @@ class BaresipService: Service() {
@Keep
fun uaEvent(event: String, uap: String, callp: String) {
if (!isServiceRunning) return
val ua = UserAgent.find(uap)
val ua = UserAgent.ofUap(uap)
if (ua == null) {
Log.w(LOG_TAG, "uaEvent did not find ua $uap")
return
@ -637,7 +637,7 @@ class BaresipService: Service() {
}
"call established" -> {
nm.cancel(CALL_NOTIFICATION_ID)
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w(LOG_TAG, "Call $callp that is established is not found")
return
@ -659,7 +659,7 @@ class BaresipService: Service() {
return
}
"call verified", "call secure" -> {
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w("Baresip", "Call $callp that is verified is not found")
return
@ -674,7 +674,7 @@ class BaresipService: Service() {
return
}
"call transfer" -> {
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w(LOG_TAG, "Call $callp to be transferred is not found")
return
@ -724,7 +724,7 @@ class BaresipService: Service() {
}
"call closed" -> {
nm.cancel(CALL_NOTIFICATION_ID)
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.d(LOG_TAG, "AoR $aor call $callp that is closed is not found")
return
@ -778,7 +778,7 @@ class BaresipService: Service() {
} catch (e: Exception) {
Log.w(LOG_TAG, "UTF-8 decode failed")
}
val ua = UserAgent.find(uap)
val ua = UserAgent.ofUap(uap)
if (ua == null) {
Log.w(LOG_TAG, "messageEvent did not find ua $uap")
return

View File

@ -79,7 +79,7 @@ class Call(val callp: String, val ua: UserAgent, val peerURI: String, val dir: S
return result
}
fun find(callp: String): Call? {
fun ofCallp(callp: String): Call? {
for (c in BaresipService.calls)
if (c.callp == callp) return c
return null

View File

@ -34,7 +34,7 @@ class CallsActivity : AppCompatActivity() {
aor = intent.getStringExtra("aor")!!
Utils.addActivity("calls,$aor")
val ua = Account.findUa(aor)!!
val ua = UserAgent.ofAor(aor)!!
account = ua.account
val headerView = findViewById(R.id.account) as TextView

View File

@ -48,7 +48,7 @@ class ChatActivity : AppCompatActivity() {
Utils.addActivity("chat,$aor,$peerUri,$focus")
}
val userAgent = Account.findUa(aor)
val userAgent = UserAgent.ofAor(aor)
if (userAgent == null) {
Log.w("Baresip", "MessageActivity did not find ua of $aor")
MainActivity.activityAor = aor

View File

@ -155,7 +155,7 @@ class ChatsActivity: AppCompatActivity() {
Message.save()
uaMessages.clear()
clAdapter.notifyDataSetChanged()
Account.findUa(aor)!!.account.unreadMessages = false
UserAgent.ofAor(aor)!!.account.unreadMessages = false
dialog.dismiss()
}
deleteDialog.setNegativeButton(getText(R.string.cancel)) { dialog, _ ->

View File

@ -66,7 +66,7 @@ class ContactListAdapter(private val cxt: Context, private val rows: ArrayList<C
i.putExtra("action", "call")
else
i.putExtra("action", "message")
val ua = Account.findUa(aor)
val ua = UserAgent.ofAor(aor)
if (ua == null) {
Log.w("Baresip", "onClickListener did not find AoR $aor")
} else {

View File

@ -472,7 +472,7 @@ class MainActivity : AppCompatActivity() {
return
}
val uap = intent.getStringExtra("uap")!!
val ua = UserAgent.find(uap)
val ua = UserAgent.ofUap(uap)
if (ua == null) {
Log.w("Baresip", "handleIntent 'call' did not find ua $uap")
return
@ -484,7 +484,7 @@ class MainActivity : AppCompatActivity() {
}
"call show", "call answer" -> {
val callp = intent.getStringExtra("callp")!!
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w("Baresip", "handleIntent '$action' did not find call $callp")
return
@ -497,7 +497,7 @@ class MainActivity : AppCompatActivity() {
}
"transfer show", "transfer accept" -> {
val callp = intent.getStringExtra("callp")!!
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w("Baresip", "handleIntent '$action' did not find call $callp")
moveTaskToBack(true)
@ -509,7 +509,7 @@ class MainActivity : AppCompatActivity() {
}
"message", "message show", "message reply" -> {
val uap = intent.getStringExtra("uap")!!
val ua = UserAgent.find(uap)
val ua = UserAgent.ofUap(uap)
if (ua == null) {
Log.w("Baresip", "onNewIntent did not find ua $uap")
return
@ -609,7 +609,7 @@ class MainActivity : AppCompatActivity() {
return
}
val uap = params[0]
val ua = UserAgent.find(uap)
val ua = UserAgent.ofUap(uap)
if (ua == null) {
Log.w("Baresip", "handleServiceEvent '$event' did not find ua $uap")
return
@ -652,7 +652,7 @@ class MainActivity : AppCompatActivity() {
Api.ua_hangup(uap, callp, 486, "Busy Here")
return
}
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w("Baresip", "Incoming call $callp not found")
return
@ -674,7 +674,7 @@ class MainActivity : AppCompatActivity() {
}
"call established" -> {
val callp = params[1]
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w("Baresip", "Established call $callp not found")
return
@ -688,7 +688,7 @@ class MainActivity : AppCompatActivity() {
}
"call verify" -> {
val callp = params[1]
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w("Baresip", "Call $callp to be verified is not found")
return
@ -728,7 +728,7 @@ class MainActivity : AppCompatActivity() {
}
"call verified", "call secure" -> {
val callp = params[1]
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w("Baresip", "Call $callp that is verified is not found")
return
@ -745,7 +745,7 @@ class MainActivity : AppCompatActivity() {
}
"call transfer", "transfer show" -> {
val callp = params[1]
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w("Baresip", "Call $callp to be transferred is not found")
return
@ -771,7 +771,7 @@ class MainActivity : AppCompatActivity() {
}
"transfer accept" -> {
val callp = params[1]
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w("Baresip", "Call $callp to be transferred is not found")
return
@ -928,7 +928,7 @@ class MainActivity : AppCompatActivity() {
uaAdapter.notifyDataSetChanged()
spinToAor(activityAor)
if (aorSpinner.tag != "")
updateIcons(Account.findUa(aorSpinner.tag.toString())!!.account)
updateIcons(Account.ofAor(aorSpinner.tag.toString())!!)
if (BaresipService.isServiceRunning) {
baresipService.setAction("UpdateNotification")
startService(baresipService)
@ -937,7 +937,7 @@ class MainActivity : AppCompatActivity() {
ACCOUNT_CODE -> {
spinToAor(activityAor)
val ua = Account.findUa(activityAor)!!
val ua = UserAgent.ofAor(activityAor)!!
updateIcons(ua.account)
if (resultCode == Activity.RESULT_OK)
if (aorPasswords.containsKey(activityAor) && aorPasswords[activityAor] == "")
@ -972,7 +972,7 @@ class MainActivity : AppCompatActivity() {
CHATS_CODE, CHAT_CODE -> {
spinToAor(activityAor)
updateIcons(Account.findUa(activityAor)!!.account)
updateIcons(Account.ofAor(activityAor)!!)
}
ABOUT_CODE -> { }

View File

@ -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? {
val uap = Api.ua_alloc(uri)
if (uap != "") return UserAgent(uap)
@ -42,13 +54,6 @@ class UserAgent(val uap: String) {
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? {
for (i in BaresipService.uas.indices) {
if (BaresipService.uas[i].account.aor == aor) return i

View File

@ -322,7 +322,7 @@ object Utils {
val text = sequence.subSequence(start, start + count).toString()
if (text.length > 0) {
val digit = text[0]
val call = Call.find(callp)
val call = Call.ofCallp(callp)
if (call == null) {
Log.w("Baresip", "dtmfWatcher did not find call $callp")
} else {