click on call button when callee is empty set callee to latest call peer

moved history activity companion object to history class\
moved history to main activity
This commit is contained in:
Juha Heinanen
2018-05-26 11:10:07 +03:00
parent b5616d5d70
commit bd2ed2334d
3 changed files with 61 additions and 50 deletions
@@ -8,4 +8,32 @@ class History(val aor: String, val peerURI: String, val direction: String,
val time: GregorianCalendar = GregorianCalendar() val time: GregorianCalendar = GregorianCalendar()
companion object {
fun aorHistory(history: ArrayList<History>, aor: String): Int {
var size = 0;
for (h in history) {
if (h.aor == aor) size++
}
return size
}
fun aorRemoveHistory(history: ArrayList<History>, aor: String) {
for (h in history) {
if (h.aor == aor) {
history.remove(h)
return
}
}
}
fun aorLatestHistory(history: ArrayList<History>, aor: String): History? {
for (h in history.reversed())
if (h.aor == aor) return h
return null
}
}
} }
@@ -45,7 +45,7 @@ class HistoryActivity : AppCompatActivity() {
val dialogClickListener = DialogInterface.OnClickListener { _, which -> val dialogClickListener = DialogInterface.OnClickListener { _, which ->
when (which) { when (which) {
DialogInterface.BUTTON_POSITIVE -> { DialogInterface.BUTTON_POSITIVE -> {
History.removeAt(posAtHistory[pos]) MainActivity.history.removeAt(posAtHistory[pos])
aorGenerateHistory(aor) aorGenerateHistory(aor)
if (uaHistory.size == 0) { if (uaHistory.size == 0) {
val i = Intent() val i = Intent()
@@ -61,7 +61,7 @@ class HistoryActivity : AppCompatActivity() {
val builder = AlertDialog.Builder(this@HistoryActivity, val builder = AlertDialog.Builder(this@HistoryActivity,
R.style.Theme_AppCompat) R.style.Theme_AppCompat)
builder.setMessage("Do you want to delete " + builder.setMessage("Do you want to delete " +
History[pos].peerURI + "?") MainActivity.history[pos].peerURI + "?")
.setPositiveButton("Yes", dialogClickListener) .setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show() .setNegativeButton("No", dialogClickListener).show()
true true
@@ -85,8 +85,8 @@ class HistoryActivity : AppCompatActivity() {
private fun aorGenerateHistory(aor: String) { private fun aorGenerateHistory(aor: String) {
uaHistory.clear() uaHistory.clear()
posAtHistory.clear() posAtHistory.clear()
for (i in History.indices.reversed()) { for (i in MainActivity.history.indices.reversed()) {
val h = History[i] val h = MainActivity.history[i]
if (h.aor == aor) { if (h.aor == aor) {
var peer_uri = h.peerURI var peer_uri = h.peerURI
if (Utils.uriHostPart(peer_uri) == Utils.uriHostPart(aor)) { if (Utils.uriHostPart(peer_uri) == Utils.uriHostPart(aor)) {
@@ -125,27 +125,4 @@ class HistoryActivity : AppCompatActivity() {
now.get(Calendar.DAY_OF_MONTH) == time.get(Calendar.DAY_OF_MONTH) now.get(Calendar.DAY_OF_MONTH) == time.get(Calendar.DAY_OF_MONTH)
} }
companion object {
var History: ArrayList<History> = ArrayList()
fun aorHistory(aor: String): Int {
var size = 0;
for (h in History) {
if (h.aor == aor) size++
}
return size
}
fun aorRemoveHistory(aor: String) {
for (h in History) {
if (h.aor == aor) {
History.remove(h)
return
}
}
}
}
} }
@@ -112,7 +112,7 @@ class MainActivity : AppCompatActivity() {
callee.hint = "Callee" callee.hint = "Callee"
callButton.tag = "Call" callButton.tag = "Call"
callButton.setImageResource(R.drawable.call) callButton.setImageResource(R.drawable.call)
if (HistoryActivity.aorHistory(aor) > 0) { if (History.aorHistory(history, aor) > 0) {
historyButton.visibility = View.VISIBLE historyButton.visibility = View.VISIBLE
} else { } else {
historyButton.visibility = View.INVISIBLE historyButton.visibility = View.INVISIBLE
@@ -214,8 +214,8 @@ class MainActivity : AppCompatActivity() {
val fis = FileInputStream(file) val fis = FileInputStream(file)
val ois = ObjectInputStream(fis) val ois = ObjectInputStream(fis)
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
HistoryActivity.History = ois.readObject() as ArrayList<History> history = ois.readObject() as ArrayList<History>
Log.d("Baresip", "Restored History of " + HistoryActivity.History.size + " entries") Log.d("Baresip", "Restored History of ${history.size} entries")
ois.close() ois.close()
fis.close() fis.close()
} catch (e: Exception) { } catch (e: Exception) {
@@ -277,15 +277,22 @@ class MainActivity : AppCompatActivity() {
val aor = ua.aor val aor = ua.aor
when (callButton.tag) { when (callButton.tag) {
"Call" -> { "Call" -> {
val callee = (findViewById(R.id.callee) as EditText).text.toString() val calleeText = (findViewById(R.id.callee) as EditText).text.toString()
if (callee.length > 0) { if (calleeText.length > 0) {
var uri = EditContactsActivity.findContactURI(callee) var uri = EditContactsActivity.findContactURI(calleeText)
if (!uri.startsWith("sip:")) uri = "sip:$uri" if (!uri.startsWith("sip:")) uri = "sip:$uri"
if (!uri.contains("@")) { if (!uri.contains("@")) {
val host = aor.substring(aor.indexOf("@") + 1) val host = aor.substring(aor.indexOf("@") + 1)
uri = "$uri@$host" uri = "$uri@$host"
} }
call(ua, uri) call(ua, uri)
} else {
val latest = History.aorLatestHistory(history, aor)
if (latest != null)
if (Utils.uriHostPart(latest.peerURI) == Utils.uriHostPart(aor))
callee.setText(Utils.uriUserPart(latest.peerURI))
else
callee.setText(latest.peerURI)
} }
} }
"Cancel" -> { "Cancel" -> {
@@ -426,7 +433,7 @@ class MainActivity : AppCompatActivity() {
try { try {
val fos = FileOutputStream(file) val fos = FileOutputStream(file)
val oos = ObjectOutputStream(fos) val oos = ObjectOutputStream(fos)
oos.writeObject(HistoryActivity.History) oos.writeObject(history)
oos.close() oos.close()
fos.close() fos.close()
} catch (e: IOException) { } catch (e: IOException) {
@@ -477,7 +484,7 @@ class MainActivity : AppCompatActivity() {
} }
if (resultCode == RESULT_CANCELED) { if (resultCode == RESULT_CANCELED) {
Log.d("Baresip", "History canceled") Log.d("Baresip", "History canceled")
if (HistoryActivity.aorHistory(uas[aorSpinner.selectedItemPosition].aor) == 0) { if (History.aorHistory(history, uas[aorSpinner.selectedItemPosition].aor) == 0) {
holdButton.visibility = View.INVISIBLE holdButton.visibility = View.INVISIBLE
} }
} }
@@ -797,8 +804,7 @@ class MainActivity : AppCompatActivity() {
dtmf.addTextChangedListener(call.dtmfWatcher) dtmf.addTextChangedListener(call.dtmfWatcher)
} }
} }
HistoryActivity.History.add(History(aor, call.peerURI, "out", history.add(History(aor, call.peerURI, "out", true))
true))
call.hasHistory = true call.hasHistory = true
} else { } else {
Log.d("Baresip", "Inbound call $callp established") Log.d("Baresip", "Inbound call $callp established")
@@ -819,8 +825,7 @@ class MainActivity : AppCompatActivity() {
} }
} }
} }
HistoryActivity.History.add(History(aor, call.peerURI, "in", history.add(History(aor, call.peerURI, "in", true))
true))
call.hasHistory = true call.hasHistory = true
} }
am.mode = AudioManager.MODE_IN_COMMUNICATION am.mode = AudioManager.MODE_IN_COMMUNICATION
@@ -828,8 +833,8 @@ class MainActivity : AppCompatActivity() {
// am.setStreamVolume(AudioManager.STREAM_VOICE_CALL, // am.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
// (am.getStreamMaxVolume(STREAM_VOICE_CALL) / 2) + 1, // (am.getStreamMaxVolume(STREAM_VOICE_CALL) / 2) + 1,
// AudioManager.STREAM_VOICE_CALL) // AudioManager.STREAM_VOICE_CALL)
if (HistoryActivity.aorHistory(aor) > HISTORY_SIZE) if (History.aorHistory(history, aor) > HISTORY_SIZE)
HistoryActivity.aorRemoveHistory(aor) History.aorRemoveHistory(history, aor)
} }
"call incoming" -> { "call incoming" -> {
val peer_uri = call_peeruri(callp) val peer_uri = call_peeruri(callp)
@@ -953,13 +958,13 @@ class MainActivity : AppCompatActivity() {
for (i in inIndex until callsIn.size) { for (i in inIndex until callsIn.size) {
this@MainActivity.addCallViews(ua, callsIn[i], (i + 1) * 10) this@MainActivity.addCallViews(ua, callsIn[i], (i + 1) * 10)
} }
historyButton.visibility = View.VISIBLE
} }
} }
if (!call.hasHistory) { if (!call.hasHistory) {
if (HistoryActivity.aorHistory(aor) > HISTORY_SIZE) if (History.aorHistory(history, aor) > HISTORY_SIZE)
HistoryActivity.aorRemoveHistory(aor) History.aorRemoveHistory(history, aor)
HistoryActivity.History.add(History(aor, call.peerURI, history.add(History(aor, call.peerURI,"in", false))
"in", false))
} }
} else { } else {
Log.d("Baresip", "Removing outgoing call $uap / $callp / " + Log.d("Baresip", "Removing outgoing call $uap / $callp / " +
@@ -978,17 +983,17 @@ class MainActivity : AppCompatActivity() {
securityButton.visibility = View.INVISIBLE securityButton.visibility = View.INVISIBLE
dtmf.removeTextChangedListener(call.dtmfWatcher) dtmf.removeTextChangedListener(call.dtmfWatcher)
dtmf.visibility = View.INVISIBLE dtmf.visibility = View.INVISIBLE
historyButton.visibility = View.VISIBLE
} }
calls.removeAt(Call.index(calls, call, "")) calls.removeAt(Call.index(calls, call, ""))
} }
if (!call.hasHistory) { if (!call.hasHistory) {
if (HistoryActivity.aorHistory(aor) > HISTORY_SIZE) if (History.aorHistory(history, aor) > HISTORY_SIZE)
HistoryActivity.aorRemoveHistory(aor) History.aorRemoveHistory(history, aor)
HistoryActivity.History.add(History(aor, call.peerURI, history.add(History(aor, call.peerURI, "out",
"out", false)) false))
} }
} }
historyButton.visibility = View.VISIBLE
if (calls.size == 0) am.mode = AudioManager.MODE_NORMAL if (calls.size == 0) am.mode = AudioManager.MODE_NORMAL
} }
else -> Log.d("Baresip", "Unknown event '${ev[0]}'") else -> Log.d("Baresip", "Unknown event '${ev[0]}'")
@@ -1039,6 +1044,7 @@ class MainActivity : AppCompatActivity() {
var uas = ArrayList<UserAgent>() var uas = ArrayList<UserAgent>()
internal var images = ArrayList<Int>() internal var images = ArrayList<Int>()
internal var calls = ArrayList<Call>() internal var calls = ArrayList<Call>()
var history: ArrayList<History> = ArrayList()
var filesPath = "" var filesPath = ""
const val ACCOUNTS_CODE = 1 const val ACCOUNTS_CODE = 1