From 3c9205bd9eb814ca032bdadf2101ebfd8dee8e12 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Sun, 13 Mar 2022 14:33:49 +0200 Subject: [PATCH] Simplified implementation of UA spinner --- .../com/tutpro/baresip/AccountActivity.kt | 6 ++-- .../com/tutpro/baresip/AccountsActivity.kt | 2 +- .../com/tutpro/baresip/BaresipService.kt | 30 +++++++++---------- .../kotlin/com/tutpro/baresip/MainActivity.kt | 2 +- .../com/tutpro/baresip/UaSpinnerAdapter.kt | 7 ++--- .../kotlin/com/tutpro/baresip/UserAgent.kt | 14 ++------- 6 files changed, 23 insertions(+), 38 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt index f3225358..f98d5ae9 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt @@ -447,12 +447,12 @@ class AccountActivity : AppCompatActivity() { if (regCheck.isChecked) { if (acc.regint != 3600) { newRegint = 3600 - ua.updateStatus(R.drawable.dot_yellow) + ua.status = R.drawable.dot_yellow } } else { if (acc.regint != 0) { Api.ua_unregister(ua.uap) - ua.updateStatus(R.drawable.dot_white) + ua.status = R.drawable.dot_white newRegint = 0 } } @@ -592,9 +592,7 @@ class AccountActivity : AppCompatActivity() { if (defaultCheck.isChecked && (uaIndex > 0)) { BaresipService.uas.add(0, BaresipService.uas[uaIndex]) - BaresipService.status.add(0, BaresipService.status[uaIndex]) BaresipService.uas.removeAt(uaIndex + 1) - BaresipService.status.removeAt(uaIndex + 1) save = true } diff --git a/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt index e32a52a7..ea52c189 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt @@ -73,7 +73,7 @@ class AccountsActivity : AppCompatActivity() { newAorView.setText("") newAorView.hint = getString(R.string.user_domain) newAorView.clearFocus() - ua.add(R.drawable.dot_white) + ua.add() generateAccounts() alAdapter.notifyDataSetChanged() saveAccounts() diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index c94336b5..ea763dfe 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -533,18 +533,18 @@ class BaresipService: Service() { @Keep fun uaAdd(uap: Long) { val ua = UserAgent(uap) - Log.d(TAG, "uaAdd ${ua.account.aor} at BaresipService") - uas.add(ua) - if (Api.ua_isregistered(uap)) { + ua.status = if (Api.ua_isregistered(uap)) { Log.d(TAG, "Ua ${ua.account.aor} is registered") - status.add(R.drawable.dot_green) + R.drawable.dot_green } else { Log.d(TAG, "Ua ${ua.account.aor} is NOT registered") if (ua.account.regint == 0) - status.add(R.drawable.dot_white) + R.drawable.dot_white else - status.add(R.drawable.dot_yellow) + R.drawable.dot_yellow } + Log.d(TAG, "uaAdd ${ua.account.aor} at BaresipService") + uas.add(ua) } @SuppressLint("UnspecifiedImmutableFlag") @@ -579,17 +579,17 @@ class BaresipService: Service() { return } "registered" -> { - if (ua.account.regint == 0) - status[account_index] = R.drawable.dot_white + ua.status = if (ua.account.regint == 0) + R.drawable.dot_white else - status[account_index] = R.drawable.dot_green + R.drawable.dot_green updateStatusNotification() if (isMainVisible) registrationUpdate.postValue(System.currentTimeMillis()) return } "registering failed" -> { - status[account_index] = R.drawable.dot_red + ua.status = R.drawable.dot_red updateStatusNotification() if (isMainVisible) registrationUpdate.postValue(System.currentTimeMillis()) @@ -606,7 +606,7 @@ class BaresipService: Service() { return } "unregistering" -> { - status[account_index] = R.drawable.dot_white + ua.status = R.drawable.dot_white updateStatusNotification() if (isMainVisible) registrationUpdate.postValue(System.currentTimeMillis()) @@ -1113,14 +1113,14 @@ class BaresipService: Service() { val contentView = RemoteViews(packageName, R.layout.status_notification) for (i: Int in 0..5) { val resID = resources.getIdentifier("status$i", "id", packageName) - if (i < status.size) { - contentView.setImageViewResource(resID, status[i]) + if (i < uas.size) { + contentView.setImageViewResource(resID, uas[i].status) contentView.setViewVisibility(resID, View.VISIBLE) } else { contentView.setViewVisibility(resID, View.INVISIBLE) } } - if (status.size > 4) + if (uas.size > 4) contentView.setViewVisibility(R.id.etc, View.VISIBLE) else contentView.setViewVisibility(R.id.etc, View.INVISIBLE) @@ -1447,7 +1447,6 @@ class BaresipService: Service() { stopRinging() stopMediaPlayer() uas.clear() - status.clear() callHistory.clear() messages.clear() if (this::nm.isInitialized) @@ -1483,7 +1482,6 @@ class BaresipService: Service() { var isMicMuted = false val uas = ArrayList() - val status = ArrayList() val calls = ArrayList() var callHistory = ArrayList() var messages = ArrayList() diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index ffd73730..887f14dd 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -197,7 +197,7 @@ class MainActivity : AppCompatActivity() { } } - uaAdapter = UaSpinnerAdapter(applicationContext, BaresipService.uas, BaresipService.status) + uaAdapter = UaSpinnerAdapter(applicationContext, BaresipService.uas) aorSpinner.adapter = uaAdapter aorSpinner.setSelection(-1) aorSpinner.tag = "" diff --git a/app/src/main/kotlin/com/tutpro/baresip/UaSpinnerAdapter.kt b/app/src/main/kotlin/com/tutpro/baresip/UaSpinnerAdapter.kt index 4631d1bf..f2e57945 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/UaSpinnerAdapter.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/UaSpinnerAdapter.kt @@ -9,9 +9,8 @@ import android.widget.ArrayAdapter import android.widget.ImageView import android.widget.TextView -class UaSpinnerAdapter(cxt: Context, private val uas: ArrayList, - private val images: ArrayList) : - ArrayAdapter(cxt, android.R.layout.simple_spinner_item, images) { +class UaSpinnerAdapter(cxt: Context, private val uas: ArrayList) : + ArrayAdapter(cxt, android.R.layout.simple_spinner_item, uas) { private val layoutInflater = LayoutInflater.from(context) @@ -47,7 +46,7 @@ class UaSpinnerAdapter(cxt: Context, private val uas: ArrayList, viewHolder.textView.textSize = 17f if (UserAgent.uas().size > 1 && ua.calls().isNotEmpty()) viewHolder.textView.setTypeface(null, Typeface.BOLD) - viewHolder.imageView.setImageResource(images[position]) + viewHolder.imageView.setImageResource(ua.status) return rowView } diff --git a/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt b/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt index b86c9b78..e3fb73e9 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/UserAgent.kt @@ -3,20 +3,14 @@ package com.tutpro.baresip class UserAgent(val uap: Long) { val account = Account(Api.ua_account(uap)) + var status = R.drawable.dot_white - fun add(status: Int) { + fun add() { BaresipService.uas.add(this) - BaresipService.status.add(status) } fun remove() { - val index = BaresipService.uas.indexOf(this) BaresipService.uas.remove(this) - BaresipService.status.removeAt(index) - } - - fun updateStatus(status: Int) { - BaresipService.status[BaresipService.uas.indexOf(this)] = status } fun calls(dir: String = ""): ArrayList { @@ -39,10 +33,6 @@ class UserAgent(val uap: Long) { return BaresipService.uas } - fun status(): ArrayList { - return BaresipService.status - } - fun ofAor(aor: String): UserAgent? { for (ua in BaresipService.uas) if (ua.account.aor == aor) return ua