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

View File

@ -8,4 +8,32 @@ class History(val aor: String, val peerURI: String, val direction: String,
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
}
}
}

View File

@ -45,7 +45,7 @@ class HistoryActivity : AppCompatActivity() {
val dialogClickListener = DialogInterface.OnClickListener { _, which ->
when (which) {
DialogInterface.BUTTON_POSITIVE -> {
History.removeAt(posAtHistory[pos])
MainActivity.history.removeAt(posAtHistory[pos])
aorGenerateHistory(aor)
if (uaHistory.size == 0) {
val i = Intent()
@ -61,7 +61,7 @@ class HistoryActivity : AppCompatActivity() {
val builder = AlertDialog.Builder(this@HistoryActivity,
R.style.Theme_AppCompat)
builder.setMessage("Do you want to delete " +
History[pos].peerURI + "?")
MainActivity.history[pos].peerURI + "?")
.setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show()
true
@ -85,8 +85,8 @@ class HistoryActivity : AppCompatActivity() {
private fun aorGenerateHistory(aor: String) {
uaHistory.clear()
posAtHistory.clear()
for (i in History.indices.reversed()) {
val h = History[i]
for (i in MainActivity.history.indices.reversed()) {
val h = MainActivity.history[i]
if (h.aor == aor) {
var peer_uri = h.peerURI
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)
}
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
}
}
}
}
}

View File

@ -112,7 +112,7 @@ class MainActivity : AppCompatActivity() {
callee.hint = "Callee"
callButton.tag = "Call"
callButton.setImageResource(R.drawable.call)
if (HistoryActivity.aorHistory(aor) > 0) {
if (History.aorHistory(history, aor) > 0) {
historyButton.visibility = View.VISIBLE
} else {
historyButton.visibility = View.INVISIBLE
@ -214,8 +214,8 @@ class MainActivity : AppCompatActivity() {
val fis = FileInputStream(file)
val ois = ObjectInputStream(fis)
@SuppressWarnings("unchecked")
HistoryActivity.History = ois.readObject() as ArrayList<History>
Log.d("Baresip", "Restored History of " + HistoryActivity.History.size + " entries")
history = ois.readObject() as ArrayList<History>
Log.d("Baresip", "Restored History of ${history.size} entries")
ois.close()
fis.close()
} catch (e: Exception) {
@ -277,15 +277,22 @@ class MainActivity : AppCompatActivity() {
val aor = ua.aor
when (callButton.tag) {
"Call" -> {
val callee = (findViewById(R.id.callee) as EditText).text.toString()
if (callee.length > 0) {
var uri = EditContactsActivity.findContactURI(callee)
val calleeText = (findViewById(R.id.callee) as EditText).text.toString()
if (calleeText.length > 0) {
var uri = EditContactsActivity.findContactURI(calleeText)
if (!uri.startsWith("sip:")) uri = "sip:$uri"
if (!uri.contains("@")) {
val host = aor.substring(aor.indexOf("@") + 1)
uri = "$uri@$host"
}
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" -> {
@ -426,7 +433,7 @@ class MainActivity : AppCompatActivity() {
try {
val fos = FileOutputStream(file)
val oos = ObjectOutputStream(fos)
oos.writeObject(HistoryActivity.History)
oos.writeObject(history)
oos.close()
fos.close()
} catch (e: IOException) {
@ -477,7 +484,7 @@ class MainActivity : AppCompatActivity() {
}
if (resultCode == RESULT_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
}
}
@ -797,8 +804,7 @@ class MainActivity : AppCompatActivity() {
dtmf.addTextChangedListener(call.dtmfWatcher)
}
}
HistoryActivity.History.add(History(aor, call.peerURI, "out",
true))
history.add(History(aor, call.peerURI, "out", true))
call.hasHistory = true
} else {
Log.d("Baresip", "Inbound call $callp established")
@ -819,8 +825,7 @@ class MainActivity : AppCompatActivity() {
}
}
}
HistoryActivity.History.add(History(aor, call.peerURI, "in",
true))
history.add(History(aor, call.peerURI, "in", true))
call.hasHistory = true
}
am.mode = AudioManager.MODE_IN_COMMUNICATION
@ -828,8 +833,8 @@ class MainActivity : AppCompatActivity() {
// am.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
// (am.getStreamMaxVolume(STREAM_VOICE_CALL) / 2) + 1,
// AudioManager.STREAM_VOICE_CALL)
if (HistoryActivity.aorHistory(aor) > HISTORY_SIZE)
HistoryActivity.aorRemoveHistory(aor)
if (History.aorHistory(history, aor) > HISTORY_SIZE)
History.aorRemoveHistory(history, aor)
}
"call incoming" -> {
val peer_uri = call_peeruri(callp)
@ -953,13 +958,13 @@ class MainActivity : AppCompatActivity() {
for (i in inIndex until callsIn.size) {
this@MainActivity.addCallViews(ua, callsIn[i], (i + 1) * 10)
}
historyButton.visibility = View.VISIBLE
}
}
if (!call.hasHistory) {
if (HistoryActivity.aorHistory(aor) > HISTORY_SIZE)
HistoryActivity.aorRemoveHistory(aor)
HistoryActivity.History.add(History(aor, call.peerURI,
"in", false))
if (History.aorHistory(history, aor) > HISTORY_SIZE)
History.aorRemoveHistory(history, aor)
history.add(History(aor, call.peerURI,"in", false))
}
} else {
Log.d("Baresip", "Removing outgoing call $uap / $callp / " +
@ -978,17 +983,17 @@ class MainActivity : AppCompatActivity() {
securityButton.visibility = View.INVISIBLE
dtmf.removeTextChangedListener(call.dtmfWatcher)
dtmf.visibility = View.INVISIBLE
historyButton.visibility = View.VISIBLE
}
calls.removeAt(Call.index(calls, call, ""))
}
if (!call.hasHistory) {
if (HistoryActivity.aorHistory(aor) > HISTORY_SIZE)
HistoryActivity.aorRemoveHistory(aor)
HistoryActivity.History.add(History(aor, call.peerURI,
"out", false))
if (History.aorHistory(history, aor) > HISTORY_SIZE)
History.aorRemoveHistory(history, aor)
history.add(History(aor, call.peerURI, "out",
false))
}
}
historyButton.visibility = View.VISIBLE
if (calls.size == 0) am.mode = AudioManager.MODE_NORMAL
}
else -> Log.d("Baresip", "Unknown event '${ev[0]}'")
@ -1039,6 +1044,7 @@ class MainActivity : AppCompatActivity() {
var uas = ArrayList<UserAgent>()
internal var images = ArrayList<Int>()
internal var calls = ArrayList<Call>()
var history: ArrayList<History> = ArrayList()
var filesPath = ""
const val ACCOUNTS_CODE = 1