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