Use LiveData to observe registration status changes and message responses
This commit is contained in:
@@ -38,6 +38,7 @@ import android.media.MediaPlayer
|
||||
import android.telecom.TelecomManager
|
||||
import android.widget.Toast
|
||||
import android.os.Looper
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
|
||||
class BaresipService: Service() {
|
||||
|
||||
@@ -554,12 +555,13 @@ class BaresipService: Service() {
|
||||
else
|
||||
status[account_index] = R.drawable.dot_green
|
||||
updateStatusNotification()
|
||||
if (!isMainVisible)
|
||||
return
|
||||
registrationUpdate.postValue(System.currentTimeMillis())
|
||||
return
|
||||
}
|
||||
"registering failed" -> {
|
||||
status[account_index] = R.drawable.dot_red
|
||||
updateStatusNotification()
|
||||
registrationUpdate.postValue(System.currentTimeMillis())
|
||||
if (Utils.isVisible()) {
|
||||
val reason = if (ev.size > 1) {
|
||||
if (ev[1] == "Invalid argument") // Likely due to DNS lookup failure
|
||||
@@ -570,14 +572,13 @@ class BaresipService: Service() {
|
||||
""
|
||||
toast(String.format(getString(R.string.registering_failed), aor) + reason)
|
||||
}
|
||||
if (!isMainVisible)
|
||||
return
|
||||
return
|
||||
}
|
||||
"unregistering" -> {
|
||||
status[account_index] = R.drawable.dot_white
|
||||
updateStatusNotification()
|
||||
if (!isMainVisible)
|
||||
return
|
||||
registrationUpdate.postValue(System.currentTimeMillis())
|
||||
return
|
||||
}
|
||||
"call outgoing" -> {
|
||||
if (call!!.status == "transferring")
|
||||
@@ -970,15 +971,12 @@ class BaresipService: Service() {
|
||||
m.responseCode = responseCode
|
||||
m.responseReason = responseReason
|
||||
}
|
||||
messageUpdate.postValue(System.currentTimeMillis())
|
||||
break
|
||||
} else {
|
||||
if (m.timeStamp < timeStamp - 60000)
|
||||
break
|
||||
}
|
||||
if (Utils.isVisible()) {
|
||||
val intent = Intent("message response")
|
||||
intent.putExtra("response code", responseCode)
|
||||
intent.putExtra("response reason", responseReason)
|
||||
intent.putExtra("time", time)
|
||||
LocalBroadcastManager.getInstance(this).sendBroadcast(intent)
|
||||
}
|
||||
}
|
||||
|
||||
@Keep
|
||||
@@ -1442,6 +1440,8 @@ class BaresipService: Service() {
|
||||
val calls = ArrayList<Call>()
|
||||
var callHistory = ArrayList<NewCallHistory>()
|
||||
var messages = ArrayList<Message>()
|
||||
val messageUpdate = MutableLiveData<Long>()
|
||||
val registrationUpdate = MutableLiveData<Long>()
|
||||
val contacts = ArrayList<Contact>()
|
||||
val chatTexts: MutableMap<String, String> = mutableMapOf()
|
||||
val activities = mutableListOf<String>()
|
||||
@@ -1450,10 +1450,15 @@ class BaresipService: Service() {
|
||||
}
|
||||
|
||||
init {
|
||||
|
||||
messageUpdate.postValue(System.currentTimeMillis())
|
||||
registrationUpdate.postValue(System.currentTimeMillis())
|
||||
|
||||
if (!libraryLoaded) {
|
||||
Log.d(TAG, "Loading baresip library")
|
||||
System.loadLibrary("baresip")
|
||||
libraryLoaded = true
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import android.app.Activity
|
||||
import android.content.*
|
||||
import android.os.Bundle
|
||||
import android.os.SystemClock
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.view.Menu
|
||||
@@ -13,6 +12,7 @@ import android.view.View
|
||||
import android.view.WindowManager
|
||||
import android.view.inputmethod.InputMethodManager
|
||||
import android.widget.*
|
||||
import androidx.lifecycle.Observer
|
||||
import com.tutpro.baresip.databinding.ActivityChatBinding
|
||||
|
||||
class ChatActivity : AppCompatActivity() {
|
||||
@@ -27,7 +27,6 @@ class ChatActivity : AppCompatActivity() {
|
||||
private lateinit var aor: String
|
||||
private lateinit var peerUri: String
|
||||
private lateinit var ua: UserAgent
|
||||
private lateinit var messageResponseReceiver: BroadcastReceiver
|
||||
|
||||
private var focus = false
|
||||
private var lastCall: Long = 0
|
||||
@@ -75,8 +74,13 @@ class ChatActivity : AppCompatActivity() {
|
||||
headerView.text = headerText
|
||||
|
||||
listView = binding.messages
|
||||
|
||||
chatMessages = uaPeerMessages(aor, peerUri)
|
||||
mlAdapter = MessageListAdapter(this, chatMessages)
|
||||
|
||||
val messagesObserver = Observer<Long> { mlAdapter.notifyDataSetChanged() }
|
||||
BaresipService.messageUpdate.observe(this, messagesObserver)
|
||||
|
||||
listView.adapter = mlAdapter
|
||||
listView.isLongClickable = true
|
||||
val footerView = View(applicationContext)
|
||||
@@ -159,15 +163,6 @@ class ChatActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
messageResponseReceiver = object : BroadcastReceiver() {
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
mlAdapter.notifyDataSetChanged()
|
||||
}
|
||||
}
|
||||
|
||||
LocalBroadcastManager.getInstance(this).registerReceiver(messageResponseReceiver,
|
||||
IntentFilter("message response"))
|
||||
|
||||
ua.account.unreadMessages = false
|
||||
|
||||
}
|
||||
@@ -201,11 +196,6 @@ class ChatActivity : AppCompatActivity() {
|
||||
MainActivity.activityAor = aor
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(messageResponseReceiver)
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
|
||||
if ((BaresipService.activities.indexOf("chat,$aor,$peerUri,false") == -1) &&
|
||||
|
||||
@@ -31,6 +31,7 @@ import androidx.core.app.ActivityCompat
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.localbroadcastmanager.content.LocalBroadcastManager
|
||||
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
||||
import androidx.lifecycle.Observer
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import com.tutpro.baresip.Utils.showSnackBar
|
||||
import com.tutpro.baresip.databinding.ActivityMainBinding
|
||||
@@ -224,6 +225,9 @@ class MainActivity : AppCompatActivity() {
|
||||
}
|
||||
}
|
||||
|
||||
val registrationObserver = Observer<Long> { uaAdapter.notifyDataSetChanged() }
|
||||
BaresipService.registrationUpdate.observe(this, registrationObserver)
|
||||
|
||||
accountsRequest = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
|
||||
uaAdapter.notifyDataSetChanged()
|
||||
spinToAor(activityAor)
|
||||
@@ -943,9 +947,6 @@ class MainActivity : AppCompatActivity() {
|
||||
for (account_index in UserAgent.uas().indices) {
|
||||
if (UserAgent.uas()[account_index].account.aor == aor) {
|
||||
when (ev[0]) {
|
||||
"registered", "unregistering", "registering failed" -> {
|
||||
uaAdapter.notifyDataSetChanged()
|
||||
}
|
||||
"call rejected" -> {
|
||||
if (aor == aorSpinner.tag) {
|
||||
callsButton.setImageResource(R.drawable.calls_missed)
|
||||
|
||||
Reference in New Issue
Block a user