diff --git a/app/src/main/kotlin/com/tutpro/baresip/AboutActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/AboutActivity.kt index 94c4814c..d6910683 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/AboutActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/AboutActivity.kt @@ -12,23 +12,39 @@ import android.widget.TextView class AboutActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) setContentView(R.layout.activity_about) + + BaresipService.activities.add(0, "about") + val aboutText = findViewById(R.id.aboutText) val text = String.format(getString(R.string.about_text), BuildConfig.VERSION_NAME) aboutText.text = HtmlCompat.fromHtml(text, HtmlCompat.FROM_HTML_MODE_LEGACY) aboutText.setMovementMethod(LinkMovementMethod.getInstance()) + } override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { android.R.id.home -> { + BaresipService.activities.remove("about") val i = Intent() setResult(Activity.RESULT_CANCELED, i) finish() } } + return true } + + override fun onBackPressed() { + + BaresipService.activities.remove("about") + super.onBackPressed() + + } + } diff --git a/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt index 0617a065..972b3875 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/AccountActivity.kt @@ -35,10 +35,14 @@ class AccountActivity : AppCompatActivity() { private var uaIndex= -1 override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) setContentView(R.layout.activity_account) - acc = Account.find(intent.getStringExtra("accp"))!! + val accp = intent.getStringExtra("accp") + BaresipService.activities.add(0, "account,$accp") + + acc = Account.find(accp)!! aor = acc.aor uaIndex = UserAgent.findAorIndex(aor)!! ua = UserAgent.uas()[uaIndex] @@ -179,249 +183,265 @@ class AccountActivity : AppCompatActivity() { } override fun onCreateOptionsMenu(menu: Menu): Boolean { + super.onCreateOptionsMenu(menu) val inflater = menuInflater inflater.inflate(R.menu.check_icon, menu) return true + } override fun onOptionsItemSelected(item: MenuItem): Boolean { - if (item.itemId == R.id.checkIcon) { + when (item.itemId) { - val dn = displayName.text.toString().trim() - if (dn != acc.displayName) { - if (checkDisplayName(dn)) { - if (account_set_display_name(acc.accp, dn) == 0) { - acc.displayName = account_display_name(acc.accp); - Log.d("Baresip", "New display name is ${acc.displayName}") - save = true - } else { - Log.e("Baresip", "Setting of display name failed") - } - } else { - Utils.alertView(this, "Notice", "Invalid Display Name: $dn") - return false - } - } - - val au = authUser.text.toString().trim() - if (au != acc.authUser) { - if (checkAuthUser(au)) { - if (account_set_auth_user(acc.accp, au) == 0) { - acc.authUser = account_auth_user(acc.accp); - Log.d("Baresip", "New auth user is ${acc.authUser}") - save = true - } else { - Log.e("Baresip", "Setting of auth user failed") - } - } else { - Utils.alertView(this, "Notice", - "Invalid Authentication UserName: $au") - return false - } - } - - val ap = authPass.text.toString().trim() - if (ap != acc.authPass) { - if (Utils.checkPrintAscii(ap)) { - if (account_set_auth_pass(acc.accp, ap) == 0) { - acc.authPass = account_auth_pass(acc.accp) - // Log.d("Baresip", "New auth password is ${acc.authPass}") - save = true - } else { - Log.e("Baresip", "Setting of auth pass failed") - } - } else { - Utils.alertView(this, "Notice", - "Invalid Authentication Password: $ap") - return false - } - } - - val ob = ArrayList() - var uri: String - if (outbound1.text.toString().trim() != "") { - uri = outbound1.text.toString().trim().replace(" ", "") - if (!uri.startsWith("sip:")) uri = "sip:" + uri - ob.add(uri) - } - if (outbound2.text.toString().trim() != "") { - uri = outbound2.text.toString().trim().replace(" ", "") - if (!uri.startsWith("sip:")) uri = "sip:" + uri - ob.add(uri) - } - if (ob != acc.outbound) { - val outbound = ArrayList() - for (i in ob.indices) { - if ((ob[i] == "") || checkOutboundUri(ob[i])) { - if (account_set_outbound(acc.accp, ob[i], i) == 0) { - if (ob[i] != "") - outbound.add(account_outbound(acc.accp, i)) + R.id.checkIcon -> { + val dn = displayName.text.toString().trim() + if (dn != acc.displayName) { + if (checkDisplayName(dn)) { + if (account_set_display_name(acc.accp, dn) == 0) { + acc.displayName = account_display_name(acc.accp); + Log.d("Baresip", "New display name is ${acc.displayName}") + save = true } else { - Log.e("Baresip", "Setting of outbound proxy ${ob[i]} failed") - break + Log.e("Baresip", "Setting of display name failed") + } + } else { + Utils.alertView(this, "Notice", "Invalid Display Name: $dn") + return false + } + } + + val au = authUser.text.toString().trim() + if (au != acc.authUser) { + if (checkAuthUser(au)) { + if (account_set_auth_user(acc.accp, au) == 0) { + acc.authUser = account_auth_user(acc.accp); + Log.d("Baresip", "New auth user is ${acc.authUser}") + save = true + } else { + Log.e("Baresip", "Setting of auth user failed") } } else { Utils.alertView(this, "Notice", - "Invalid Proxy Server URI: ${ob[i]}") + "Invalid Authentication UserName: $au") return false } } - Log.d("Baresip", "New outbound proxies are ${outbound}") - acc.outbound = outbound - if (outbound.isEmpty()) - account_set_sipnat(acc.accp, "") - else - account_set_sipnat(acc.accp, "outbound") - save = true - } - var newRegint = -1 - if (regCheck.isChecked) { - if (acc.regint != 3600) newRegint = 3600 - } else { - if (acc.regint != 0) { - Api.ua_unregister(ua.uap) - UserAgent.updateStatus(ua, R.drawable.dot_yellow) - newRegint = 0 - } - } - if (newRegint != -1) - if (account_set_regint(acc.accp, newRegint) == 0) { - acc.regint = account_regint(acc.accp) - Log.d("Baresip", "New regint is ${acc.regint}") - save = true - } else { - Log.e("Baresip", "Setting of regint failed") - } - - if (mediaNat != acc.mediaNat) { - if (account_set_medianat(acc.accp, mediaNat) == 0) { - acc.mediaNat = account_medianat(acc.accp) - Log.d("Baresip", "New medianat is ${acc.mediaNat}") - save = true - } else { - Log.e("Baresip", "Setting of medianat failed") - } - } - - if (mediaNat != "") { - val newStunServer = stunServer.text.toString().trim() - if (acc.stunServer != newStunServer) { - if (!Utils.checkHostPort(newStunServer)) { - Utils.alertView(this, "Notice", - "Invalid STUN Server '$newStunServer'") - return false - } - var host = "" - var port = 0 - if (newStunServer != "") { - val hostPort = newStunServer.split(":") - host = hostPort[0] - if (hostPort.size == 2) port = hostPort[1].toInt() - } - if ((account_set_stun_host(acc.accp, host) == 0) && - (account_set_stun_port(acc.accp, port) == 0)) { - acc.stunServer = account_stun_host(acc.accp) - if (port != 0) - acc.stunServer += ":" + account_stun_port(acc.accp).toString() - Log.d("Baresip", "New StunServer is '${acc.stunServer}'") - save = true - } else { - Log.e("Baresip", "Setting of StunServer failed") - } - } - } - - val ac = ArrayList(LinkedHashSet(newCodecs.filter { it != "-" } as ArrayList)) - if (ac != acc.audioCodec) { - Log.d("Baresip", "New codecs ${newCodecs.filter { it != "-" }}") - val acParam = ";audio_codecs=" + Utils.implode(ac, ",") - if (account_set_audio_codecs(acc.accp, acParam) == 0) { - var i = 0 - while (true) { - val codec = account_audio_codec(acc.accp, i) - if (codec != "") { - Log.d("Baresip", "Found audio codec '$codec'") - i++ + val ap = authPass.text.toString().trim() + if (ap != acc.authPass) { + if (Utils.checkPrintAscii(ap)) { + if (account_set_auth_pass(acc.accp, ap) == 0) { + acc.authPass = account_auth_pass(acc.accp) + // Log.d("Baresip", "New auth password is ${acc.authPass}") + save = true } else { - break + Log.e("Baresip", "Setting of auth pass failed") + } + } else { + Utils.alertView(this, "Notice", + "Invalid Authentication Password: $ap") + return false + } + } + + val ob = ArrayList() + var uri: String + if (outbound1.text.toString().trim() != "") { + uri = outbound1.text.toString().trim().replace(" ", "") + if (!uri.startsWith("sip:")) uri = "sip:" + uri + ob.add(uri) + } + if (outbound2.text.toString().trim() != "") { + uri = outbound2.text.toString().trim().replace(" ", "") + if (!uri.startsWith("sip:")) uri = "sip:" + uri + ob.add(uri) + } + if (ob != acc.outbound) { + val outbound = ArrayList() + for (i in ob.indices) { + if ((ob[i] == "") || checkOutboundUri(ob[i])) { + if (account_set_outbound(acc.accp, ob[i], i) == 0) { + if (ob[i] != "") + outbound.add(account_outbound(acc.accp, i)) + } else { + Log.e("Baresip", "Setting of outbound proxy ${ob[i]} failed") + break + } + } else { + Utils.alertView(this, "Notice", + "Invalid Proxy Server URI: ${ob[i]}") + return false } } - acc.audioCodec = ac + Log.d("Baresip", "New outbound proxies are ${outbound}") + acc.outbound = outbound + if (outbound.isEmpty()) + account_set_sipnat(acc.accp, "") + else + account_set_sipnat(acc.accp, "outbound") save = true - } else { - Log.e("Baresip", "Setting of audio codecs '$acParam' failed") } - } - if (mediaEnc != acc.mediaEnc) { - if (account_set_mediaenc(acc.accp, mediaEnc) == 0) { - acc.mediaEnc = account_mediaenc(acc.accp) - Log.d("Baresip", "New mediaenc is ${acc.mediaEnc}") - save = true + var newRegint = -1 + if (regCheck.isChecked) { + if (acc.regint != 3600) newRegint = 3600 } else { - Log.e("Baresip", "Setting of mediaenc $mediaEnc failed") - } - } - - if (answerMode != acc.answerMode) { - acc.answerMode = answerMode - Log.d("Baresip", "New answermode is ${acc.answerMode}") - save = true - } - - var vmUri = voicemailUri.text.toString().trim() - if (vmUri != acc.vmUri) { - if (vmUri != "") { - if (!vmUri.startsWith("sip:")) vmUri = "sip:$vmUri" - if (!vmUri.contains("@")) vmUri = "$vmUri@${acc.host()}" - if (!Utils.checkSipUri(vmUri)) { - Utils.alertView(this, "Notice", - "Invalid Voicemail URI: $vmUri") - return false + if (acc.regint != 0) { + Api.ua_unregister(ua.uap) + UserAgent.updateStatus(ua, R.drawable.dot_yellow) + newRegint = 0 } - account_set_mwi(acc.accp, "yes") - } else { - account_set_mwi(acc.accp, "no") } - acc.vmUri = vmUri - save = true + if (newRegint != -1) + if (account_set_regint(acc.accp, newRegint) == 0) { + acc.regint = account_regint(acc.accp) + Log.d("Baresip", "New regint is ${acc.regint}") + save = true + } else { + Log.e("Baresip", "Setting of regint failed") + } + + if (mediaNat != acc.mediaNat) { + if (account_set_medianat(acc.accp, mediaNat) == 0) { + acc.mediaNat = account_medianat(acc.accp) + Log.d("Baresip", "New medianat is ${acc.mediaNat}") + save = true + } else { + Log.e("Baresip", "Setting of medianat failed") + } + } + + if (mediaNat != "") { + val newStunServer = stunServer.text.toString().trim() + if (acc.stunServer != newStunServer) { + if (!Utils.checkHostPort(newStunServer)) { + Utils.alertView(this, "Notice", + "Invalid STUN Server '$newStunServer'") + return false + } + var host = "" + var port = 0 + if (newStunServer != "") { + val hostPort = newStunServer.split(":") + host = hostPort[0] + if (hostPort.size == 2) port = hostPort[1].toInt() + } + if ((account_set_stun_host(acc.accp, host) == 0) && + (account_set_stun_port(acc.accp, port) == 0)) { + acc.stunServer = account_stun_host(acc.accp) + if (port != 0) + acc.stunServer += ":" + account_stun_port(acc.accp).toString() + Log.d("Baresip", "New StunServer is '${acc.stunServer}'") + save = true + } else { + Log.e("Baresip", "Setting of StunServer failed") + } + } + } + + val ac = ArrayList(LinkedHashSet(newCodecs.filter { it != "-" } as ArrayList)) + if (ac != acc.audioCodec) { + Log.d("Baresip", "New codecs ${newCodecs.filter { it != "-" }}") + val acParam = ";audio_codecs=" + Utils.implode(ac, ",") + if (account_set_audio_codecs(acc.accp, acParam) == 0) { + var i = 0 + while (true) { + val codec = account_audio_codec(acc.accp, i) + if (codec != "") { + Log.d("Baresip", "Found audio codec '$codec'") + i++ + } else { + break + } + } + acc.audioCodec = ac + save = true + } else { + Log.e("Baresip", "Setting of audio codecs '$acParam' failed") + } + } + + if (mediaEnc != acc.mediaEnc) { + if (account_set_mediaenc(acc.accp, mediaEnc) == 0) { + acc.mediaEnc = account_mediaenc(acc.accp) + Log.d("Baresip", "New mediaenc is ${acc.mediaEnc}") + save = true + } else { + Log.e("Baresip", "Setting of mediaenc $mediaEnc failed") + } + } + + if (answerMode != acc.answerMode) { + acc.answerMode = answerMode + Log.d("Baresip", "New answermode is ${acc.answerMode}") + save = true + } + + var vmUri = voicemailUri.text.toString().trim() + if (vmUri != acc.vmUri) { + if (vmUri != "") { + if (!vmUri.startsWith("sip:")) vmUri = "sip:$vmUri" + if (!vmUri.contains("@")) vmUri = "$vmUri@${acc.host()}" + if (!Utils.checkSipUri(vmUri)) { + Utils.alertView(this, "Notice", + "Invalid Voicemail URI: $vmUri") + return false + } + account_set_mwi(acc.accp, "yes") + } else { + account_set_mwi(acc.accp, "no") + } + acc.vmUri = vmUri + save = true + } + + if (defaultCheck.isChecked && (uaIndex > 0)) { + val uasTmp = BaresipService.uas[0] + val statusTmp = BaresipService.status[0] + BaresipService.uas[0] = BaresipService.uas[uaIndex] + BaresipService.status[0] = BaresipService.status[uaIndex] + BaresipService.uas[uaIndex] = uasTmp + BaresipService.status[uaIndex] = statusTmp + save = true + } + + if (save) { + AccountsActivity.saveAccounts() + if (Api.ua_update_account(ua.uap) != 0) + Log.e("Baresip", "Failed to update UA ${ua.uap} with AoR $aor") + } + + if (regCheck.isChecked) Api.ua_register(ua.uap) + + BaresipService.activities.removeAt(0) + val i = Intent() + setResult(Activity.RESULT_OK, i) + finish() + } - if (defaultCheck.isChecked && (uaIndex > 0)) { - val uasTmp = BaresipService.uas[0] - val statusTmp = BaresipService.status[0] - BaresipService.uas[0] = BaresipService.uas[uaIndex] - BaresipService.status[0] = BaresipService.status[uaIndex] - BaresipService.uas[uaIndex] = uasTmp - BaresipService.status[uaIndex] = statusTmp - save = true + android.R.id.home -> { + + BaresipService.activities.removeAt(0) + val i = Intent() + setResult(Activity.RESULT_OK, i) + finish() + } - if (save) { - AccountsActivity.saveAccounts() - if (Api.ua_update_account(ua.uap) != 0) - Log.e("Baresip", "Failed to update UA ${ua.uap} with AoR $aor") - } + } - if (regCheck.isChecked) Api.ua_register(ua.uap) + return true - val i = Intent() - setResult(Activity.RESULT_OK, i) - finish() - return true + } - } else if (item.itemId == android.R.id.home) { + override fun onBackPressed() { - Log.d("Baresip", "Back array was pressed at Account") - val i = Intent() - setResult(Activity.RESULT_OK, i) - finish() - return true + BaresipService.activities.removeAt(0) + super.onBackPressed() - } else return super.onOptionsItemSelected(item) } fun onClick(v: View) { diff --git a/app/src/main/kotlin/com/tutpro/baresip/AccountListAdapter.kt b/app/src/main/kotlin/com/tutpro/baresip/AccountListAdapter.kt index d5904bbe..d61f4d24 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/AccountListAdapter.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/AccountListAdapter.kt @@ -3,7 +3,6 @@ package com.tutpro.baresip import android.app.Activity import android.content.Context import android.content.Intent -import android.content.res.Resources import android.os.Bundle import android.support.v7.app.AlertDialog import android.view.LayoutInflater diff --git a/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt index d9542687..b3efe654 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/AccountsActivity.kt @@ -18,16 +18,19 @@ import java.util.ArrayList class AccountsActivity : AppCompatActivity() { internal lateinit var alAdapter: AccountListAdapter + internal var aor = "" internal var password = "" - internal var fromAorSpinner = false public override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) setContentView(R.layout.activity_accounts) filesPath = applicationContext.filesDir.absolutePath + BaresipService.activities.add(0, "accounts") + val listView = findViewById(R.id.accounts) as ListView generateAccounts() alAdapter = AccountListAdapter(this, accounts) @@ -35,7 +38,7 @@ class AccountsActivity : AppCompatActivity() { val addAccountButton = findViewById(R.id.addAccount) as ImageButton val newAorView = findViewById(R.id.newAor) as EditText - addAccountButton.setOnClickListener{ + addAccountButton.setOnClickListener { val aor = newAorView.text.toString().trim() if (!Utils.checkAor(aor)) { Log.d("Baresip", "Invalid Address of Record $aor") @@ -68,52 +71,54 @@ class AccountsActivity : AppCompatActivity() { } } } - - val accp = intent.getStringExtra("accp") - if (accp != "") { - fromAorSpinner = true - val i = Intent(this, AccountActivity::class.java) - val b = Bundle() - b.putString("accp", accp) - i.putExtras(b) - startActivityForResult(i, 1) - } - } - - override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - if (fromAorSpinner) { - val i = Intent() - setResult(Activity.RESULT_OK, i) - finish() - } } override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.accounts_menu, menu) return true + } override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.export_accounts -> { if (Utils.requestPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) askPassword(getString(R.string.encrypt_password)) } + R.id.import_accounts -> { if (Utils.requestPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE)) askPassword(getString(R.string.decrypt_password)) } + android.R.id.home -> { Log.d("Baresip", "Back array was pressed at Accounts") + BaresipService.activities.removeAt(0) val i = Intent() setResult(Activity.RESULT_OK, i) finish() } + } + return true + + } + + override fun onBackPressed() { + + BaresipService.activities.removeAt(0) + val i = Intent() + setResult(Activity.RESULT_OK, i) + finish() + super.onBackPressed() + } private fun askPassword(title: String) { @@ -187,6 +192,7 @@ class AccountsActivity : AppCompatActivity() { if (accounts == "") return false return Utils.putFileContents(File(filesPath + "/accounts"), accounts) } + } } diff --git a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt index dfac417d..acad39b9 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/BaresipService.kt @@ -928,6 +928,7 @@ class BaresipService: Service() { var messages = ArrayList() var contacts = ArrayList() var chatTexts: MutableMap = mutableMapOf() + var activities = mutableListOf() } diff --git a/app/src/main/kotlin/com/tutpro/baresip/CallsActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/CallsActivity.kt index d4d2ba92..095f4888 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/CallsActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/CallsActivity.kt @@ -19,13 +19,15 @@ import java.util.GregorianCalendar class CallsActivity : AppCompatActivity() { internal var uaHistory = ArrayList() + internal var aor = "" public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_calls) - val aor = intent.getStringExtra("aor")!! + aor = intent.getStringExtra("aor")!! + BaresipService.activities.add(0, "calls,$aor") val ua = Account.findUa(aor)!! val headerView = findViewById(R.id.account) as TextView @@ -122,22 +124,34 @@ class CallsActivity : AppCompatActivity() { } override fun onPause() { + CallHistory.save(applicationContext.filesDir.absolutePath) super.onPause() + } override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + android.R.id.home -> { - Log.d("Baresip", "Back array was pressed at Calls") + BaresipService.activities.removeAt(0) val i = Intent() setResult(Activity.RESULT_CANCELED, i) finish() } } + return true } + override fun onBackPressed() { + + BaresipService.activities.removeAt(0) + super.onBackPressed() + + } + private fun aorGenerateHistory(aor: String) { uaHistory.clear() for (i in CallHistory.history().indices.reversed()) { diff --git a/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt index f9bcab33..7ee31c45 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ChatActivity.kt @@ -44,10 +44,13 @@ class ChatActivity : AppCompatActivity() { val i = Intent() setResult(Activity.RESULT_CANCELED, i) finish() + return } else { ua = userAgent } + BaresipService.activities.add(0, "chat,$aor,$peerUri,$focus") + imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager this@ChatActivity.window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN) @@ -59,6 +62,7 @@ class ChatActivity : AppCompatActivity() { else chatPeer = chatPeer.substring(4) } + setTitle(String.format(getString(R.string.chat_with), chatPeer)) val headerView = findViewById(R.id.account) as TextView @@ -161,16 +165,20 @@ class ChatActivity : AppCompatActivity() { } override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.call_icon, menu) return true + } override fun onPause() { + if (newMessage.text.toString() != "") { Log.d("Baresip", "Saving newMessage ${newMessage.text} for $aor::$peerUri") BaresipService.chatTexts.put("$aor::$peerUri", newMessage.text.toString()) } super.onPause() + } /*override fun onStop() { @@ -179,8 +187,9 @@ class ChatActivity : AppCompatActivity() { }*/ override fun onResume() { + super.onResume() - Log.d("Baresip", "Chat resumed") + val chatText = BaresipService.chatTexts.get("$aor::$peerUri") if (chatText != null) { Log.d("Baresip", "Restoring newMessage ${newMessage.text} for $aor::$peerUri") @@ -192,15 +201,20 @@ class ChatActivity : AppCompatActivity() { chatMessages = uaPeerMessages(aor, peerUri) mlAdapter = MessageListAdapter(this, chatMessages) listView.adapter = mlAdapter + } override fun onDestroy() { + LocalBroadcastManager.getInstance(this).unregisterReceiver(messageResponseReceiver) super.onDestroy() + } override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + android.R.id.home -> { var save = false for (m in chatMessages) { @@ -211,9 +225,11 @@ class ChatActivity : AppCompatActivity() { } if (save) Message.saveMessages(applicationContext.filesDir.absolutePath) imm.hideSoftInputFromWindow(newMessage.windowToken, 0) + BaresipService.activities.removeAt(0) val i = Intent() setResult(Activity.RESULT_OK, i) } + R.id.callIcon -> { val intent = Intent(this, MainActivity::class.java) intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or @@ -223,9 +239,19 @@ class ChatActivity : AppCompatActivity() { intent.putExtra("peer", peerUri) startActivity(intent) } + } + finish() return true + + } + + override fun onBackPressed() { + + BaresipService.activities.removeAt(0) + super.onBackPressed() + } private fun uaPeerMessages(aor: String, peerUri: String): ArrayList { diff --git a/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt index 74665129..60b06f89 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ChatsActivity.kt @@ -13,18 +13,17 @@ import java.util.* class ChatsActivity: AppCompatActivity() { internal lateinit var uaMessages: ArrayList - internal lateinit var aor: String internal lateinit var listView: ListView internal lateinit var clAdapter: ChatListAdapter internal lateinit var peerUri: AutoCompleteTextView internal lateinit var plusButton: ImageButton + internal var aor = "" + public override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - Log.d("Baresip", "Chats created") - setContentView(R.layout.activity_chats) filesPath = applicationContext.filesDir.absolutePath @@ -33,6 +32,7 @@ class ChatsActivity: AppCompatActivity() { plusButton = findViewById(R.id.plusButton) as ImageButton aor = intent.extras!!.getString("aor")!! + BaresipService.activities.add(0, "chats,$aor") val headerView = findViewById(R.id.account) as TextView val headerText = "${getString(R.string.account)} ${aor.substringAfter(":")}" @@ -49,7 +49,7 @@ class ChatsActivity: AppCompatActivity() { b.putString("aor", aor) b.putString("peer", uaMessages[pos].peerUri) i.putExtras(b) - startActivityForResult(i, MainActivity.MESSAGE_CODE) + startActivityForResult(i, MainActivity.CHAT_CODE) } listView.onItemLongClickListener = AdapterView.OnItemLongClickListener { _, _, pos, _ -> @@ -125,57 +125,60 @@ class ChatsActivity: AppCompatActivity() { b.putString("aor", aor) b.putString("peer", uri) i.putExtras(b) - startActivityForResult(i, MainActivity.MESSAGE_CODE) + startActivityForResult(i, MainActivity.CHAT_CODE) } } } - val peer = intent.getStringExtra("peer") - if (peer != "") { - val i = Intent(this, ChatActivity::class.java) - val b = Bundle() - b.putString("aor", aor) - b.putString("peer", peer) - b.putBoolean("focus", intent.getBooleanExtra("focus", false)) - i.putExtras(b) - startActivityForResult(i, MainActivity.MESSAGE_CODE) - } - - } - - override fun onPause() { - super.onPause() - Log.d("Baresip", "Chats paused") } override fun onResume() { + super.onResume() - Log.d("Baresip", "Chats resumed") + clAdapter.clear() uaMessages = uaMessages(aor) clAdapter = ChatListAdapter(this, uaMessages) listView.adapter = clAdapter + } override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + android.R.id.home -> { - Log.d("Baresip", "Back array was pressed at Chats") + BaresipService.activities.removeAt(0) val i = Intent() setResult(Activity.RESULT_CANCELED, i) finish() } + } + return true } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { - if ((requestCode == MainActivity.MESSAGE_CODE) && (resultCode == Activity.RESULT_OK)) { - clAdapter.clear() - uaMessages = uaMessages(aor) - clAdapter = ChatListAdapter(this, uaMessages) - listView.adapter = clAdapter + + if (requestCode == MainActivity.CHAT_CODE) { + if (resultCode == Activity.RESULT_OK) { + clAdapter.clear() + uaMessages = uaMessages(aor) + clAdapter = ChatListAdapter(this, uaMessages) + listView.adapter = clAdapter + } } + + } + + override fun onBackPressed() { + + BaresipService.activities.removeAt(0) + val i = Intent() + setResult(Activity.RESULT_OK, i) + finish() + } private fun uaMessages(aor: String) : ArrayList { diff --git a/app/src/main/kotlin/com/tutpro/baresip/ConfigActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ConfigActivity.kt index 7a570777..0c51ff7a 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ConfigActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ConfigActivity.kt @@ -52,6 +52,8 @@ class ConfigActivity : AppCompatActivity() { super.onCreate(savedInstanceState) setContentView(R.layout.activity_config) + BaresipService.activities.add(0, "config") + autoStart = findViewById(R.id.AutoStart) as CheckBox val asCv = Config.variable("auto_start") oldAutoStart = if (asCv.size == 0) "no" else asCv[0] @@ -142,211 +144,228 @@ class ConfigActivity : AppCompatActivity() { } override fun onCreateOptionsMenu(menu: Menu): Boolean { + super.onCreateOptionsMenu(menu) + val inflater = menuInflater inflater.inflate(R.menu.check_icon, menu) return true + } override fun onOptionsItemSelected(item: MenuItem): Boolean { val intent = Intent(this, MainActivity::class.java) - if (item.itemId == R.id.checkIcon) { + when (item.itemId) { - var autoStartString = "no" - if (autoStart.isChecked) autoStartString = "yes" - if (oldAutoStart != autoStartString) { - Config.replace("auto_start", autoStartString) - save = true - restart = false - } + R.id.checkIcon -> { - val listenAddr = listenAddr.text.toString().trim() - if (listenAddr != oldListenAddr) { - if ((listenAddr != "") && !Utils.checkIpPort(listenAddr)) { - Utils.alertView(this, getString(R.string.notice), - "${getString(R.string.invalid_listen_address)}: $listenAddr") - return false + var autoStartString = "no" + if (autoStart.isChecked) autoStartString = "yes" + if (oldAutoStart != autoStartString) { + Config.replace("auto_start", autoStartString) + save = true + restart = false } - Config.remove("sip_listen") - if (listenAddr != "") Config.add("sip_listen", listenAddr) - save = true - restart = true - } - var preferIPv6String = "no" - if (preferIPv6.isChecked) preferIPv6String = "yes" - if (oldPreferIPv6 != preferIPv6String) { - Config.replace("net_prefer_ipv6", preferIPv6String) - save = true - restart = true - } - - val dnsServers = addMissingPorts(dnsServers.text.toString().trim().toLowerCase()) - if (dnsServers != oldDnsServers) { - if (!checkDnsServers(dnsServers)) { - Utils.alertView(this, getString(R.string.notice), - "${getString(R.string.invalid_dns_servers)}: $dnsServers") - return false - } - Config.remove("dyn_dns") - Config.remove("dns_server") - if (dnsServers.isNotEmpty()) { - for (server in dnsServers.split(",")) - Config.add("dns_server", server) - Config.add("dyn_dns", "no") - if (Api.net_use_nameserver(dnsServers) != 0) { - Utils.alertView(this, getString(R.string.error), - "${getString(R.string.failed_to_set_dns_servers)}: $dnsServers") + val listenAddr = listenAddr.text.toString().trim() + if (listenAddr != oldListenAddr) { + if ((listenAddr != "") && !Utils.checkIpPort(listenAddr)) { + Utils.alertView(this, getString(R.string.notice), + "${getString(R.string.invalid_listen_address)}: $listenAddr") return false } - } else { - Config.add("dyn_dns", "yes") - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager - Config.updateDnsServers(cm.getLinkProperties(cm.activeNetwork).getDnsServers()) + Config.remove("sip_listen") + if (listenAddr != "") Config.add("sip_listen", listenAddr) + save = true + restart = true + } + + var preferIPv6String = "no" + if (preferIPv6.isChecked) preferIPv6String = "yes" + if (oldPreferIPv6 != preferIPv6String) { + Config.replace("net_prefer_ipv6", preferIPv6String) + save = true + restart = true + } + + val dnsServers = addMissingPorts(dnsServers.text.toString().trim().toLowerCase()) + if (dnsServers != oldDnsServers) { + if (!checkDnsServers(dnsServers)) { + Utils.alertView(this, getString(R.string.notice), + "${getString(R.string.invalid_dns_servers)}: $dnsServers") + return false + } + Config.remove("dyn_dns") + Config.remove("dns_server") + if (dnsServers.isNotEmpty()) { + for (server in dnsServers.split(",")) + Config.add("dns_server", server) + Config.add("dyn_dns", "no") + if (Api.net_use_nameserver(dnsServers) != 0) { + Utils.alertView(this, getString(R.string.error), + "${getString(R.string.failed_to_set_dns_servers)}: $dnsServers") + return false + } } else { - restart = true + Config.add("dyn_dns", "yes") + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + val cm = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager + Config.updateDnsServers(cm.getLinkProperties(cm.activeNetwork).getDnsServers()) + } else { + restart = true + } } + Api.net_debug() + save = true } - Api.net_debug() - save = true - } - if (certificateFile.isChecked != oldCertificateFile) { - if (certificateFile.isChecked) { - if (!Utils.requestPermission(this, - android.Manifest.permission.READ_EXTERNAL_STORAGE)) - return false - val content = Utils.getFileContents(File(downloadsDir, "cert.pem")) - if (content == "Failed") { - Utils.alertView(this, getString(R.string.error), - getString(R.string.read_cert_error)) + if (certificateFile.isChecked != oldCertificateFile) { + if (certificateFile.isChecked) { + if (!Utils.requestPermission(this, + android.Manifest.permission.READ_EXTERNAL_STORAGE)) + return false + val content = Utils.getFileContents(File(downloadsDir, "cert.pem")) + if (content == "Failed") { + Utils.alertView(this, getString(R.string.error), + getString(R.string.read_cert_error)) + return false + } + Utils.putFileContents(File(BaresipService.filesPath + "/cert.pem"), + content) + Config.remove("sip_certificate") + Config.add("sip_certificate", BaresipService.filesPath + "/cert.pem") + } else { + Config.remove("sip_certificate") + } + save = true + restart = true + } + + if (caFile.isChecked != oldCAFile) { + if (caFile.isChecked) { + if (!Utils.requestPermission(this, + android.Manifest.permission.READ_EXTERNAL_STORAGE)) + return false + val content = Utils.getFileContents(File(downloadsDir, "ca_certs.crt")) + if (content == "Failed") { + Utils.alertView(this, getString(R.string.error), + getString(R.string.read_ca_certs_error)) + return false + } + Utils.putFileContents(File(BaresipService.filesPath + "/ca_certs.crt"), + content) + Config.remove("sip_cafile") + Config.add("sip_cafile", BaresipService.filesPath + "/ca_certs.crt") + } else { + Config.remove("sip_cafile") + } + save = true + restart = true + } + + if (aec.isChecked != oldAec) { + Config.remove("module webrtc_aec.so") + if (aec.isChecked) { + Config.add("module", "webrtc_aec.so") + if (Api.module_load("webrtc_aec.so") != 0) { + Utils.alertView(this, getString(R.string.error), + getString(R.string.failed_to_load_aec_module)) + return false + } + } else { + Api.module_unload("webrtc_aec.so") + } + save = true + } + + val opusBitRate = opusBitRate.text.toString().trim() + if (opusBitRate != oldOpusBitrate) { + if (!checkOpusBitRate(opusBitRate)) { + Utils.alertView(this, getString(R.string.notice), + "${getString(R.string.invalid_opus_bitrate)}: $opusBitRate.") return false } - Utils.putFileContents(File(BaresipService.filesPath + "/cert.pem"), - content) - Config.remove("sip_certificate") - Config.add("sip_certificate", BaresipService.filesPath + "/cert.pem") - } else { - Config.remove("sip_certificate") + Config.remove("opus_bitrate") + Config.add("opus_bitrate", opusBitRate) + save = true + restart = true } - save = true - restart = true - } - if (caFile.isChecked != oldCAFile) { - if (caFile.isChecked) { - if (!Utils.requestPermission(this, - android.Manifest.permission.READ_EXTERNAL_STORAGE)) - return false - val content = Utils.getFileContents(File(downloadsDir, "ca_certs.crt")) - if (content == "Failed") { - Utils.alertView(this, getString(R.string.error), - getString(R.string.read_ca_certs_error)) + val opusPacketLoss = opusPacketLoss.text.toString().trim() + if (opusPacketLoss != oldOpusPacketLoss) { + if (!checkOpusPacketLoss(opusPacketLoss)) { + Utils.alertView(this, getString(R.string.notice), + "${getString(R.string.invalid_opus_packet_loss)}: $opusPacketLoss") return false } - Utils.putFileContents(File(BaresipService.filesPath + "/ca_certs.crt"), - content) - Config.remove("sip_cafile") - Config.add("sip_cafile", BaresipService.filesPath + "/ca_certs.crt") - } else { - Config.remove("sip_cafile") - } - save = true - restart = true - } - - if (aec.isChecked != oldAec) { - Config.remove("module webrtc_aec.so") - if (aec.isChecked) { - Config.add("module", "webrtc_aec.so") - if (Api.module_load("webrtc_aec.so") != 0) { - Utils.alertView(this, getString(R.string.error), - getString(R.string.failed_to_load_aec_module)) - return false + Config.remove("opus_inbandfec") + Config.remove("opus_packet_loss") + if (opusPacketLoss != "0") { + Config.add("opus_inbandfec", "yes") + Config.add("opus_packet_loss", opusPacketLoss) } - } else { - Api.module_unload("webrtc_aec.so") + save = true + restart = true } - save = true - } - val opusBitRate = opusBitRate.text.toString().trim() - if (opusBitRate != oldOpusBitrate) { - if (!checkOpusBitRate(opusBitRate)) { - Utils.alertView(this, getString(R.string.notice), - "${getString(R.string.invalid_opus_bitrate)}: $opusBitRate.") - return false + var iceModeString = "full" + if (iceLite.isChecked) iceModeString = "lite" + if (oldIceMode != iceModeString) { + Config.replace("ice_mode", iceModeString) + save = true + restart = true } - Config.remove("opus_bitrate") - Config.add("opus_bitrate", opusBitRate) - save = true - restart = true - } - val opusPacketLoss = opusPacketLoss.text.toString().trim() - if (opusPacketLoss != oldOpusPacketLoss) { - if (!checkOpusPacketLoss(opusPacketLoss)) { - Utils.alertView(this, getString(R.string.notice), - "${getString(R.string.invalid_opus_packet_loss)}: $opusPacketLoss") - return false + if (BaresipService.callVolume != callVolume) { + BaresipService.callVolume = callVolume + Config.replace("call_volume", callVolume.toString()) + save = true } - Config.remove("opus_inbandfec") - Config.remove("opus_packet_loss") - if (opusPacketLoss != "0") { - Config.add("opus_inbandfec", "yes") - Config.add("opus_packet_loss", opusPacketLoss) + + var logLevelString = "2" + if (debug.isChecked) logLevelString = "0" + if (oldLogLevel != logLevelString) { + Config.replace("log_level", logLevelString) + Api.log_level_set(logLevelString.toInt()) + Log.logLevelSet(logLevelString.toInt()) + save = true } - save = true - restart = true + + if (reset.isChecked) { + Config.reset(this) + save = false + restart = true + } + + if (save) Config.save() + + intent.putExtra("restart", restart) + setResult(RESULT_OK, intent) + finish() + } - var iceModeString = "full" - if (iceLite.isChecked) iceModeString = "lite" - if (oldIceMode != iceModeString) { - Config.replace("ice_mode", iceModeString) - save = true - restart = true + android.R.id.home -> { + + Log.d("Baresip", "Back array was pressed at Config") + setResult(Activity.RESULT_CANCELED, intent) + finish() + } + } - if (BaresipService.callVolume != callVolume) { - BaresipService.callVolume = callVolume - Config.replace("call_volume", callVolume.toString()) - save = true - } + BaresipService.activities.removeAt(0) + return true - var logLevelString = "2" - if (debug.isChecked) logLevelString = "0" - if (oldLogLevel != logLevelString) { - Config.replace("log_level", logLevelString) - Api.log_level_set(logLevelString.toInt()) - Log.logLevelSet(logLevelString.toInt()) - save = true - } + } - if (reset.isChecked) { - Config.reset(this) - save = false - restart = true - } + override fun onBackPressed() { - if (save) Config.save() + BaresipService.activities.removeAt(0) + super.onBackPressed() - intent.putExtra("restart", restart ) - setResult(RESULT_OK, intent) - finish() - return true - - } else if (item.itemId == android.R.id.home) { - - Log.d("Baresip", "Back array was pressed at Config") - setResult(Activity.RESULT_CANCELED, intent) - finish() - return true - - } else return super.onOptionsItemSelected(item) } fun onClick(v: View) { diff --git a/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt index 334332c6..cb938e46 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ContactActivity.kt @@ -13,117 +13,136 @@ class ContactActivity : AppCompatActivity() { lateinit var nameView: EditText lateinit var uriView: EditText + internal var index = 0 + internal var new = false + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) setContentView(R.layout.activity_contact) nameView = findViewById(R.id.Name) as EditText uriView = findViewById(R.id.Uri) as EditText - val uri = intent.getStringExtra("uri") new = intent.getBooleanExtra("new", false) + val uOrI: String if (new) { setTitle(getString(R.string.new_contact)) nameView.setText("") nameView.hint = getString(R.string.contact_name) nameView.setSelection(nameView.text.length) + val uri = intent.getStringExtra("uri") if (uri == "") { uriView.setText("") uriView.hint = getString(R.string.sip_uri) } else { uriView.setText(uri) } + uOrI = uri } else { index = intent.getIntExtra("index", 0) val name = Contact.contacts()[index].name setTitle(name) nameView.setText(name) uriView.setText(Contact.contacts()[index].uri) + uOrI = index.toString() } + + BaresipService.activities.add(0, "contact,$new,$uOrI") + } override fun onCreateOptionsMenu(menu: Menu): Boolean { + super.onCreateOptionsMenu(menu) + val inflater = menuInflater inflater.inflate(R.menu.check_icon, menu) return true + } override fun onOptionsItemSelected(item: MenuItem): Boolean { val i = Intent(this, MainActivity::class.java) - if (item.itemId == R.id.checkIcon) { + when (item.itemId) { - val newName = nameView.text.toString().trim() - if (!Utils.checkName(newName)) { - Utils.alertView(this, getString(R.string.error), - String.format(getString(R.string.invalid_contact), newName)) - return false - } - var alert: Boolean - if (new) - alert = ContactsActivity.nameExists(newName, true) - else - alert = (Contact.contacts()[index].name != newName) && - ContactsActivity.nameExists(newName, false) - if (alert) { - Utils.alertView(this, getString(R.string.error), - String.format(getString(R.string.contact_already_exists), newName)) - return false - } + R.id.checkIcon -> { - var newUri = uriView.text.toString().trim() - if (!newUri.startsWith("sip:")) { - if (newUri.contains("@") || (BaresipService.uas.size != 1)) { - newUri = "sip:$newUri" - } else { - newUri = "sip:$newUri@${Utils.aorDomain(BaresipService.uas[0].account.aor)}" + val newName = nameView.text.toString().trim() + if (!Utils.checkName(newName)) { + Utils.alertView(this, getString(R.string.error), + String.format(getString(R.string.invalid_contact), newName)) + return false } - } - if (!Utils.checkSipUri(newUri)) { - Utils.alertView(this, getString(R.string.error), - String.format(getString(R.string.invalid_contact_uri), newUri)) - return false - } - - if (new) { - if (Contact.contacts().size >= Contact.CONTACTS_SIZE) { - Utils.alertView(this, getString(R.string.notice), - String.format(getString(R.string.contacts_exceeded), - Contact.CONTACTS_SIZE)) - return true - } else { - Contact.contacts().add(Contact(newName, newUri)) + val alert: Boolean + if (new) + alert = ContactsActivity.nameExists(newName, true) + else + alert = (Contact.contacts()[index].name != newName) && + ContactsActivity.nameExists(newName, false) + if (alert) { + Utils.alertView(this, getString(R.string.error), + String.format(getString(R.string.contact_already_exists), newName)) + return false } - } else { - Contact.contacts()[index].uri = newUri - Contact.contacts()[index].name = newName + + var newUri = uriView.text.toString().trim() + if (!newUri.startsWith("sip:")) { + if (newUri.contains("@") || (BaresipService.uas.size != 1)) { + newUri = "sip:$newUri" + } else { + newUri = "sip:$newUri@${Utils.aorDomain(BaresipService.uas[0].account.aor)}" + } + } + if (!Utils.checkSipUri(newUri)) { + Utils.alertView(this, getString(R.string.error), + String.format(getString(R.string.invalid_contact_uri), newUri)) + return false + } + + if (new) { + if (Contact.contacts().size >= Contact.CONTACTS_SIZE) { + Utils.alertView(this, getString(R.string.notice), + String.format(getString(R.string.contacts_exceeded), + Contact.CONTACTS_SIZE)) + BaresipService.activities.removeAt(0) + return true + } else { + Contact.contacts().add(Contact(newName, newUri)) + } + } else { + Contact.contacts()[index].uri = newUri + Contact.contacts()[index].name = newName + } + + Contact.contacts().sortBy { Contact -> Contact.name } + ContactsActivity.saveContacts(applicationContext.filesDir, "contacts") + + i.putExtra("name", newName) + setResult(Activity.RESULT_OK, i) + finish() } - Contact.contacts().sortBy { Contact -> Contact.name } - ContactsActivity.saveContacts(applicationContext.filesDir, "contacts") + android.R.id.home -> { - i.putExtra("name", newName) - setResult(Activity.RESULT_OK, i) - finish() - return true + setResult(Activity.RESULT_CANCELED, i) + finish() + } - } else if (item.itemId == android.R.id.home) { + } - Log.d("Baresip", "Back array was pressed at Contact") - setResult(Activity.RESULT_CANCELED, i) - finish() - return true + BaresipService.activities.removeAt(0) + return true - } else return super.onOptionsItemSelected(item) } - companion object { + override fun onBackPressed() { - internal var index = 0 - internal var new = false + BaresipService.activities.removeAt(0) + super.onBackPressed() } diff --git a/app/src/main/kotlin/com/tutpro/baresip/ContactListAdapter.kt b/app/src/main/kotlin/com/tutpro/baresip/ContactListAdapter.kt index 6383e9ff..ad273f25 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/ContactListAdapter.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/ContactListAdapter.kt @@ -43,6 +43,7 @@ class ContactListAdapter(private val cxt: Context, private val rows: ArrayList { if (saveContacts(dir, "contacts.bs")) Utils.alertView(this, "", @@ -64,6 +75,7 @@ class ContactsActivity : AppCompatActivity() { Utils.alertView(this,getString(R.string.error), getString(R.string.export_error)) } + R.id.import_contacts -> { if (restoreContacts(dir, "contacts.bs")) { Utils.alertView(this, "", @@ -74,21 +86,27 @@ class ContactsActivity : AppCompatActivity() { Utils.alertView(this,getString(R.string.error), getString(R.string.import_error)) } + android.R.id.home -> { Log.d("Baresip", "Back array was pressed at Contacts") + BaresipService.activities.removeAt(0) val i = Intent() setResult(Activity.RESULT_OK, i) finish() } } + return true + } override fun onBackPressed() { - Log.d("Baresip", "Back button was pressed at Contacts") + + BaresipService.activities.removeAt(0) val i = Intent() setResult(Activity.RESULT_OK, i) finish() + } companion object { diff --git a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt index 92669315..f4639729 100644 --- a/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt +++ b/app/src/main/kotlin/com/tutpro/baresip/MainActivity.kt @@ -167,19 +167,15 @@ class MainActivity : AppCompatActivity() { if (event.action == MotionEvent.ACTION_DOWN) { if (aorSpinner.selectedItemPosition == -1) { val i = Intent(this, AccountsActivity::class.java) - val b = Bundle() - b.putString("accp", "") - i.putExtras(b) startActivityForResult(i, ACCOUNTS_CODE) true } else { if ((event.x - view.left) < 100) { - val i = Intent(this, AccountsActivity::class.java) + val i = Intent(this, AccountActivity::class.java) val b = Bundle() - b.putString("accp", - UserAgent.uas()[aorSpinner.selectedItemPosition].account.accp) + b.putString("accp", UserAgent.uas()[aorSpinner.selectedItemPosition].account.accp) i.putExtras(b) - startActivityForResult(i, ACCOUNTS_CODE) + startActivityForResult(i, ACCOUNT_CODE) true } else { false @@ -407,10 +403,8 @@ class MainActivity : AppCompatActivity() { val i = Intent(this@MainActivity, ChatsActivity::class.java) val b = Bundle() b.putString("aor", UserAgent.uas()[aorSpinner.selectedItemPosition].account.aor) - b.putString("peer", resumeUri) - b.putBoolean("focus", resumeUri != "") i.putExtras(b) - startActivityForResult(i, MESSAGES_CODE) + startActivityForResult(i, CHATS_CODE) } } @@ -554,20 +548,25 @@ class MainActivity : AppCompatActivity() { "message show", "message reply" -> handleServiceEvent(resumeAction, arrayListOf(resumeUap, resumeUri)) else -> { - if (UserAgent.uas().size > 0) { - val currentPosition = aorSpinner.selectedItemPosition - uaAdapter = UaSpinnerAdapter(applicationContext, UserAgent.uas(), UserAgent.status()) - aorSpinner.adapter = uaAdapter - if (currentPosition == -1) { - if (Call.calls().size > 0) - spinToAor(Call.calls()[0].ua.account.aor) - else - aorSpinner.setSelection(0) - } else { - aorSpinner.setSelection(currentPosition) + if (!BaresipService.activities.isEmpty()) { + restoreActivities() + } else { + if (UserAgent.uas().size > 0) { + val currentPosition = aorSpinner.selectedItemPosition + uaAdapter = UaSpinnerAdapter(applicationContext, UserAgent.uas(), + UserAgent.status()) + aorSpinner.adapter = uaAdapter + if (currentPosition == -1) { + if (Call.calls().size > 0) + spinToAor(Call.calls()[0].ua.account.aor) + else + aorSpinner.setSelection(0) + } else { + aorSpinner.setSelection(currentPosition) + } + showCall(UserAgent.uas()[aorSpinner.selectedItemPosition]) + updateIcons(UserAgent.uas()[aorSpinner.selectedItemPosition].account) } - showCall(UserAgent.uas()[aorSpinner.selectedItemPosition]) - updateIcons(UserAgent.uas()[aorSpinner.selectedItemPosition].account) } } } @@ -576,9 +575,11 @@ class MainActivity : AppCompatActivity() { } override fun onPause() { - super.onPause() - // Log.d("Baresip", "Main paused") + Log.d("Baresip", "Main paused") + if ((BaresipService.activities.size == 0) || (BaresipService.activities[0] != "main")) + BaresipService.activities.add(0, "main") visible = false + super.onPause() } private fun handleServiceEvent(event: String, params: ArrayList) { @@ -809,6 +810,7 @@ class MainActivity : AppCompatActivity() { "${getString(R.string.call_closed)}: $param", Toast.LENGTH_LONG).show() } + if (!BaresipService.activities.isEmpty()) restoreActivities() } "message show", "message reply" -> { val peer = params[1] @@ -816,7 +818,7 @@ class MainActivity : AppCompatActivity() { if ((aorSpinner.selectedItemPosition == -1) || (ua != UserAgent.uas()[aorSpinner.selectedItemPosition])) aorSpinner.setSelection(account_index) - val i = Intent(applicationContext, ChatsActivity::class.java) + val i = Intent(applicationContext, ChatActivity::class.java) i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) val b = Bundle() b.putString("aor", aor) @@ -895,9 +897,6 @@ class MainActivity : AppCompatActivity() { } R.id.accounts -> { i = Intent(this, AccountsActivity::class.java) - val b = Bundle() - b.putString("accp", "") - i.putExtras(b) startActivityForResult(i, ACCOUNTS_CODE) return true } @@ -975,8 +974,6 @@ class MainActivity : AppCompatActivity() { (data!!.getBooleanExtra("restart", true))) Utils.alertView(this, getString(R.string.notice), getString(R.string.restart_needed)) - if (resultCode == RESULT_CANCELED) - Log.d("Baresip", "Config canceled") } CALLS_CODE -> { @@ -1182,6 +1179,82 @@ class MainActivity : AppCompatActivity() { messagesButton.setImageResource(R.drawable.messages) } + private fun restoreActivities() { + Log.d("Baresip", "Stack is ${BaresipService.activities.toString()}") + val activity = BaresipService.activities[0].split(",") + BaresipService.activities.removeAt(0) + when (activity[0]) { + "main" -> { + if ((Call.calls().size == 0) && (BaresipService.activities.size > 1)) + restoreActivities() + } + "config" -> { + val i = Intent(this, ConfigActivity::class.java) + startActivityForResult(i, CONFIG_CODE) + } + "accounts" -> { + val i = Intent(this, AccountsActivity::class.java) + startActivityForResult(i, ACCOUNTS_CODE) + } + "account" -> { + val i = Intent(this, AccountActivity::class.java) + val b = Bundle() + b.putString("accp", activity[1]) + i.putExtras(b) + startActivityForResult(i, ACCOUNT_CODE) + } + "about" -> { + val i = Intent(this, AboutActivity::class.java) + startActivityForResult(i, ABOUT_CODE) + } + "contacts" -> { + val i = Intent(this, ContactsActivity::class.java) + val b = Bundle() + b.putString("aor", activity[1]) + i.putExtras(b) + startActivityForResult(i, CONTACTS_CODE) + + } + "contact" -> { + val i = Intent(this, ContactActivity::class.java) + val b = Bundle() + if (activity[1] == "true") { + b.putBoolean("new", true) + b.putString("uri", activity[2]) + } else { + b.putBoolean("new", false) + b.putInt("index", activity[2].toInt()) + } + i.putExtras(b) + startActivityForResult(i, CONTACT_CODE) + } + "chats" -> { + val i = Intent(this, ChatsActivity::class.java) + val b = Bundle() + b.putString("aor", activity[1]) + i.putExtras(b) + startActivityForResult(i, CHATS_CODE) + } + "chat" -> { + val i = Intent(this, ChatActivity::class.java) + val b = Bundle() + b.putString("aor", activity[1]) + b.putString("peer", activity[2]) + b.putString("focus", activity[3]) + i.putExtras(b) + startActivityForResult(i, CHAT_CODE) + } + "calls" -> { + val i = Intent(this, CallsActivity::class.java) + val b = Bundle() + b.putString("aor", activity[1]) + i.putExtras(b) + startActivityForResult(i, CALLS_CODE) + } + } + return + } + companion object { var visible = false @@ -1198,8 +1271,8 @@ class MainActivity : AppCompatActivity() { const val ABOUT_CODE = 5 const val ACCOUNT_CODE = 6 const val CONTACT_CODE = 7 - const val MESSAGES_CODE = 8 - const val MESSAGE_CODE = 9 + const val CHATS_CODE = 8 + const val CHAT_CODE = 9 const val PERMISSION_REQUEST_CODE = 1 const val RESTART_REQUEST_CODE = 2