From d3a8e2df41e863af5f2172def1d5aa4581b24968 Mon Sep 17 00:00:00 2001 From: Juha Heinanen Date: Sat, 20 Apr 2019 13:17:50 +0300 Subject: [PATCH] - Fixed call, message, and call transfer actions when initiated from notification. - Moved save and restore messages functions to Message class. - Coding style edits. --- .../com/tutpro/baresip/BaresipService.kt | 25 ++++---- .../com/tutpro/baresip/CallsActivity.kt | 3 +- .../kotlin/com/tutpro/baresip/ChatActivity.kt | 2 +- .../com/tutpro/baresip/ChatsActivity.kt | 51 ++--------------- .../kotlin/com/tutpro/baresip/MainActivity.kt | 57 ++++++++++--------- .../main/kotlin/com/tutpro/baresip/Message.kt | 36 ++++++++++-- .../com/tutpro/baresip/MessageListAdapter.kt | 4 +- 7 files changed, 81 insertions(+), 97 deletions(-) diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index 805bdd2b..e747adff 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -26,8 +26,6 @@ import java.io.InputStream import java.util.* import android.content.Intent - - class BaresipService: Service() { private val LOG_TAG = "Baresip Service" @@ -173,7 +171,8 @@ class BaresipService: Service() { "Call Show", "Call Answer" -> { val newIntent = Intent(this, MainActivity::class.java) - newIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP + newIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or + Intent.FLAG_ACTIVITY_NEW_TASK newIntent.putExtra("action", action.toLowerCase()) newIntent.putExtra("callp", intent!!.getStringExtra("callp")) startActivity(newIntent) @@ -201,7 +200,8 @@ class BaresipService: Service() { Log.w(LOG_TAG, "onStartCommand did not find ua $uap") } else { val newIntent = Intent(this, MainActivity::class.java) - newIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP + newIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or + Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK newIntent.putExtra("action", action.toLowerCase()) newIntent.putExtra("callp", intent.getStringExtra("callp")) newIntent.putExtra("uri", intent.getStringExtra("uri")) @@ -222,10 +222,11 @@ class BaresipService: Service() { "Message Show", "Message Reply" -> { val newIntent = Intent(this, MainActivity::class.java) - newIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP + newIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP or + Intent.FLAG_ACTIVITY_NEW_TASK newIntent.putExtra("action", action.toLowerCase()) newIntent.putExtra("uap", intent!!.getStringExtra("uap")) - newIntent.putExtra("time", intent.getStringExtra("time")) + newIntent.putExtra("peer", intent.getStringExtra("peer")) startActivity(newIntent) nm.cancel(BaresipService.MESSAGE_NOTIFICATION_ID) } @@ -533,20 +534,20 @@ class BaresipService: Service() { } catch (e: Exception) { Log.w(LOG_TAG, "UTF-8 decode failed") } - Log.d(LOG_TAG, "Message event for $uap from $peer") val ua = UserAgent.find(uap) if (ua == null) { Log.w(LOG_TAG, "messageEvent did not find ua $uap") return } val timeStamp = System.currentTimeMillis().toString() + Log.d(LOG_TAG, "Message event for $uap from $peer at $timeStamp") Message.add(Message(ua.account.aor, peer, text, timeStamp.toLong(), R.drawable.arrow_down_green, 0, "", true)) if (!Utils.isVisible()) { val intent = Intent(this, BaresipService::class.java) intent.action = "Message Show" intent.putExtra("uap", uap) - .putExtra("time", timeStamp) + .putExtra("peer", peer) val pi = PendingIntent.getService(this, MESSAGE_REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT) val nb = NotificationCompat.Builder(this, HIGH_CHANNEL_ID) @@ -567,19 +568,19 @@ class BaresipService: Service() { val replyIntent = Intent(this, BaresipService::class.java) replyIntent.action = "Message Reply" replyIntent.putExtra("uap", uap) - .putExtra("time", timeStamp) + .putExtra("peer", peer) val replyPendingIntent = PendingIntent.getService(this, REPLY_REQ_CODE, replyIntent, PendingIntent.FLAG_UPDATE_CURRENT) val saveIntent = Intent(this, BaresipService::class.java) saveIntent.action = "Message Save" saveIntent.putExtra("uap", uap) - .putExtra("time", timeStamp) + .putExtra("time", timeStamp) val savePendingIntent = PendingIntent.getService(this, SAVE_REQ_CODE, saveIntent, PendingIntent.FLAG_UPDATE_CURRENT) val deleteIntent = Intent(this, BaresipService::class.java) deleteIntent.action = "Message Delete" deleteIntent.putExtra("uap", uap) - .putExtra("time", timeStamp) + .putExtra("time", timeStamp) val deletePendingIntent = PendingIntent.getService(this, DELETE_REQ_CODE, deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT) nb.addAction(R.drawable.ic_stat, "Reply", replyPendingIntent) @@ -591,7 +592,7 @@ class BaresipService: Service() { val intent = Intent("service event") intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP intent.putExtra("event", "message show") - intent.putExtra("params", arrayListOf(uap, timeStamp)) + intent.putExtra("params", arrayListOf(uap, peer)) LocalBroadcastManager.getInstance(this).sendBroadcast(intent) } diff --git a/app/src/main/kotlin/com/tutpro/baresip/CallsActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/CallsActivity.kt index ea55ff18..e64bd93e 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/CallsActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/CallsActivity.kt @@ -47,8 +47,7 @@ class CallsActivity : AppCompatActivity() { when (which) { DialogInterface.BUTTON_NEUTRAL, DialogInterface.BUTTON_NEGATIVE -> { val i = Intent(this@CallsActivity, MainActivity::class.java) - i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or - Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP) + i.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP if (which == DialogInterface.BUTTON_NEUTRAL) i.putExtra("action", "call") else diff --git a/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt index 2736e563..66c9f697 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt @@ -166,7 +166,7 @@ class ChatActivity : AppCompatActivity() { Log.d("Baresip", "Saving newMessage ${newMessage.text} for $aor::$peerUri") BaresipService.chatTexts.put("$aor::$peerUri", newMessage.text.toString()) } - ChatsActivity.saveMessages(applicationContext.filesDir.absolutePath) + Message.saveMessages(applicationContext.filesDir.absolutePath) super.onPause() } diff --git a/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt index 24a66ac0..a976beda 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt @@ -8,12 +8,6 @@ import android.support.v7.app.AppCompatActivity import android.view.MenuItem import android.widget.* -import java.io.File -import java.io.FileInputStream -import java.io.FileOutputStream -import java.io.IOException -import java.io.ObjectInputStream -import java.io.ObjectOutputStream import java.util.* class ChatsActivity: AppCompatActivity() { @@ -78,7 +72,7 @@ class ChatsActivity: AppCompatActivity() { else clAdapter.remove(m) clAdapter.notifyDataSetChanged() - Message.messages(msgs) + BaresipService.messages = msgs uaMessages = uaMessages(aor) } DialogInterface.BUTTON_POSITIVE -> { @@ -148,7 +142,7 @@ class ChatsActivity: AppCompatActivity() { } override fun onPause() { - saveMessages(applicationContext.filesDir.absolutePath) + Message.saveMessages(applicationContext.filesDir.absolutePath) super.onPause() } @@ -206,20 +200,12 @@ class ChatsActivity: AppCompatActivity() { var filesPath = "" - fun findUaMessage(aor: String, timeStamp: Long): Message? { - for (i in Message.messages().indices.reversed()) - if ((Message.messages()[i].aor == aor) && - (Message.messages()[i].timeStamp == timeStamp)) - return Message.messages()[i] - return null - } - fun saveUaMessage(aor: String, time: Long, path: String) { for (i in Message.messages().indices.reversed()) if ((Message.messages()[i].aor == aor) && (Message.messages()[i].timeStamp == time)) { Message.messages()[i].new = false - saveMessages(path) + Message.saveMessages(path) return } } @@ -229,39 +215,10 @@ class ChatsActivity: AppCompatActivity() { if ((Message.messages()[i].aor == aor) && (Message.messages()[i].timeStamp == time)) { Message.messages().removeAt(i) - saveMessages(path) + Message.saveMessages(path) return } } - fun saveMessages(path: String = filesPath) { - val file = File(path, "messages") - try { - val fos = FileOutputStream(file) - val oos = ObjectOutputStream(fos) - oos.writeObject(Message.messages()) - oos.close() - fos.close() - } catch (e: IOException) { - Log.e("Baresip", "OutputStream exception: " + e.toString()) - e.printStackTrace() - } - } - - fun restoreMessages(path: String = filesPath) { - val file = File(path, "messages") - if (file.exists()) { - try { - val fis = FileInputStream(file) - val ois = ObjectInputStream(fis) - Message.messages(ois.readObject() as ArrayList) - ois.close() - fis.close() - } catch (e: Exception) { - Log.e("Baresip", "InputStream exception: - " + e.toString()) - } - } - } - } } diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index 6c652a87..3dd932d0 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -55,8 +55,8 @@ class MainActivity : AppCompatActivity() { super.onCreate(savedInstanceState) - Log.d("Baresip", "Main created with action " + - intent.getStringExtra("action")) + val intentAction = intent.getStringExtra("action") + Log.d("Baresip", "Main created with action '$intentAction'") if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) { setShowWhenLocked(true) @@ -102,9 +102,9 @@ class MainActivity : AppCompatActivity() { IntentFilter("service event")) stopState = "initial" - quitTimer = object: CountDownTimer(5000, 1000) { + quitTimer = object : CountDownTimer(5000, 1000) { override fun onTick(millisUntilFinished: Long) { - Log.d("Baresip", "Seconds remaining: ${millisUntilFinished/1000}") + Log.d("Baresip", "Seconds remaining: ${millisUntilFinished / 1000}") } override fun onFinish() { when (stopState) { @@ -191,11 +191,11 @@ class MainActivity : AppCompatActivity() { } callUri.setAdapter(ArrayAdapter(this, android.R.layout.select_dialog_item, - Contact.contacts().map{Contact -> Contact.name})) + Contact.contacts().map { Contact -> Contact.name })) callUri.threshold = 2 - callUri.setOnFocusChangeListener { view, b -> - if (b) imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT) - } + callUri.setOnFocusChangeListener { view, b -> + if (b) imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT) + } securityButton.setOnClickListener { when (securityButton.tag) { @@ -249,7 +249,7 @@ class MainActivity : AppCompatActivity() { } } if (!Utils.checkSipUri(uri)) - Utils.alertView(this,"Notice","Invalid SIP URI '$uri'") + Utils.alertView(this, "Notice", "Invalid SIP URI '$uri'") else call(ua, uri, "outgoing") } else { @@ -276,7 +276,7 @@ class MainActivity : AppCompatActivity() { answerButton.setOnClickListener { val ua = UserAgent.uas()[aorSpinner.selectedItemPosition] val aor = ua.account.aor - val callp = Call.uaCalls(ua,"in")[0].callp + val callp = Call.uaCalls(ua, "in")[0].callp Log.d("Baresip", "AoR $aor answering call $callp from ${callUri.text}") answerButton.isEnabled = false Api.ua_answer(ua.uap, callp) @@ -285,7 +285,7 @@ class MainActivity : AppCompatActivity() { rejectButton.setOnClickListener { val ua = UserAgent.uas()[aorSpinner.selectedItemPosition] val aor = ua.account.aor - val callp = Call.uaCalls(ua,"in")[0].callp + val callp = Call.uaCalls(ua, "in")[0].callp Log.d("Baresip", "AoR $aor rejecting call $callp from ${callUri.text}") rejectButton.isEnabled = false Api.ua_hangup(ua.uap, callp, 486, "Rejected") @@ -294,7 +294,7 @@ class MainActivity : AppCompatActivity() { holdButton.setOnClickListener { val ua = UserAgent.uas()[aorSpinner.selectedItemPosition] val aor = ua.account.aor - val call = Call.uaCalls(ua,"")[0] + val call = Call.uaCalls(ua, "")[0] if (call.onhold) { Log.d("Baresip", "AoR $aor resuming call ${call.callp} with ${callUri.text}") Api.call_unhold(call.callp) @@ -310,7 +310,7 @@ class MainActivity : AppCompatActivity() { infoButton.setOnClickListener { val ua = UserAgent.uas()[aorSpinner.selectedItemPosition] - val calls = Call.uaCalls(ua,"") + val calls = Call.uaCalls(ua, "") if (calls.size > 0) { val status = Api.call_status(calls[0].callp) val codecs = Api.call_audio_codecs(calls[0].callp) @@ -371,20 +371,22 @@ class MainActivity : AppCompatActivity() { startActivityForResult(i, CONTACTS_CODE) } - ChatsActivity.restoreMessages(applicationContext.filesDir.absolutePath) + if (intentAction == null) + Message.restoreMessages(applicationContext.filesDir.absolutePath) messagesButton.setOnClickListener { if (aorSpinner.selectedItemPosition >= 0) { val i = Intent(this@MainActivity, ChatsActivity::class.java) val b = Bundle() b.putString("aor", UserAgent.uas()[aorSpinner.selectedItemPosition].account.aor) - b.putString("peer", "") - b.putBoolean("focus", false) + b.putString("peer", resumeUri) + b.putBoolean("focus", resumeUri != "") i.putExtras(b) startActivityForResult(i, MESSAGES_CODE) } } - CallHistory.restore(applicationContext.filesDir.absolutePath) + if (intentAction == null) + CallHistory.restore(applicationContext.filesDir.absolutePath) callsButton.setOnClickListener { if (aorSpinner.selectedItemPosition >= 0) { val i = Intent(this@MainActivity, CallsActivity::class.java) @@ -477,7 +479,7 @@ class MainActivity : AppCompatActivity() { resumeCall = call resumeUri = intent.getStringExtra("uri") } - "message show", "message reply" -> { + "message", "message show", "message reply" -> { val uap = intent.getStringExtra("uap") val ua = UserAgent.find(uap) if (ua == null) { @@ -488,7 +490,7 @@ class MainActivity : AppCompatActivity() { spinToAor(ua.account.aor) resumeAction = action resumeUap = uap - resumeTime = intent.getStringExtra("time") + resumeUri = intent.getStringExtra("peer") } } } @@ -514,8 +516,11 @@ class MainActivity : AppCompatActivity() { "transfer show", "transfer accept" -> handleServiceEvent("$resumeAction,$resumeUri", arrayListOf(resumeCall!!.ua.uap, resumeCall!!.callp)) + "message" -> { + messagesButton.performClick() + } "message show", "message reply" -> - handleServiceEvent(resumeAction, arrayListOf(resumeUap, resumeTime)) + handleServiceEvent(resumeAction, arrayListOf(resumeUap, resumeUri)) else -> { if ((aorSpinner.selectedItemPosition == -1) && (UserAgent.uas().size > 0)) aorSpinner.setSelection(0) @@ -524,6 +529,7 @@ class MainActivity : AppCompatActivity() { } } resumeAction = "" + resumeUri = "" } override fun onPause() { @@ -769,13 +775,8 @@ class MainActivity : AppCompatActivity() { } } "message show", "message reply" -> { - val timeStamp = params[1].toLong() - val msg = ChatsActivity.findUaMessage(ua.account.aor, timeStamp) - if (msg == null) { - Log.w("Baresip", "Message ${ua.uap}/$timeStamp is not found") - return - } - Log.d("Baresip", "Message for $aor from ${msg.peerUri}") + val peer = params[1] + Log.d("Baresip", "Message for $aor from $peer") if ((aorSpinner.selectedItemPosition == -1) || (ua != UserAgent.uas()[aorSpinner.selectedItemPosition])) aorSpinner.setSelection(account_index) @@ -783,7 +784,7 @@ class MainActivity : AppCompatActivity() { i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) val b = Bundle() b.putString("aor", aor) - b.putString("peer", msg.peerUri) + b.putString("peer", peer) b.putBoolean("focus", ev[0] == "message reply") i.putExtras(b) startActivity(i) diff --git a/app/src/main/kotlin/com/tutpro/baresip/Message.kt b/app/src/main/kotlin/com/tutpro/baresip/Message.kt index 1cacdb7b..e1c3ab74 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/Message.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/Message.kt @@ -15,10 +15,6 @@ class Message(val aor: String, val peerUri: String, val message: String, val tim return BaresipService.messages } - fun messages(messages: ArrayList) { - BaresipService.messages = messages - } - fun add(message: Message) { BaresipService.messages.add(message) var count = 0 @@ -35,5 +31,37 @@ class Message(val aor: String, val peerUri: String, val message: String, val tim BaresipService.messages.removeAt(firstIndex) } + fun saveMessages(path: String) { + val file = File(path, "messages") + try { + val fos = FileOutputStream(file) + val oos = ObjectOutputStream(fos) + oos.writeObject(BaresipService.messages) + oos.close() + fos.close() + Log.d("Baresip", "Saved ${BaresipService.messages.size} messages") + } catch (e: IOException) { + Log.e("Baresip", "OutputStream exception: " + e.toString()) + e.printStackTrace() + } + } + + fun restoreMessages(path: String) { + val file = File(path, "messages") + if (file.exists()) { + try { + val fis = FileInputStream(file) + val ois = ObjectInputStream(fis) + BaresipService.messages = ois.readObject() as ArrayList + ois.close() + fis.close() + Log.d("Baresip", "Restored ${BaresipService.messages.size} messages") + } catch (e: Exception) { + Log.e("Baresip", "InputStream exception: - " + e.toString()) + } + } + } + + } } diff --git a/app/src/main/kotlin/com/tutpro/baresip/MessageListAdapter.kt b/app/src/main/kotlin/com/tutpro/baresip/MessageListAdapter.kt index 74d94cb2..856c5927 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MessageListAdapter.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MessageListAdapter.kt @@ -63,10 +63,8 @@ class MessageListAdapter(private val cxt: Context, private val rows: ArrayList