2021-01-26 19:45:18 +01:00
|
|
|
package sushi.hardcore.aira
|
|
|
|
|
2021-05-05 20:54:25 +02:00
|
|
|
import android.content.ComponentName
|
2021-05-21 14:55:37 +02:00
|
|
|
import android.content.Context
|
2021-05-05 20:54:25 +02:00
|
|
|
import android.content.ServiceConnection
|
2021-01-26 19:45:18 +01:00
|
|
|
import android.os.Bundle
|
|
|
|
import android.os.IBinder
|
|
|
|
import android.view.Menu
|
|
|
|
import android.view.MenuItem
|
|
|
|
import android.view.View
|
2021-05-21 14:55:37 +02:00
|
|
|
import android.view.inputmethod.InputMethodManager
|
2021-05-31 11:42:42 +02:00
|
|
|
import android.widget.ImageView
|
2021-01-26 19:45:18 +01:00
|
|
|
import android.widget.Toast
|
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
|
|
import androidx.appcompat.app.AlertDialog
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import sushi.hardcore.aira.adapters.ChatAdapter
|
2021-07-31 22:07:32 +02:00
|
|
|
import sushi.hardcore.aira.adapters.FuckRecyclerView
|
2021-05-06 22:57:47 +02:00
|
|
|
import sushi.hardcore.aira.background_service.*
|
2021-01-26 19:45:18 +01:00
|
|
|
import sushi.hardcore.aira.databinding.ActivityChatBinding
|
2021-05-04 20:01:06 +02:00
|
|
|
import sushi.hardcore.aira.databinding.DialogFingerprintsBinding
|
|
|
|
import sushi.hardcore.aira.databinding.DialogInfoBinding
|
2021-01-26 19:45:18 +01:00
|
|
|
import sushi.hardcore.aira.utils.FileUtils
|
|
|
|
import sushi.hardcore.aira.utils.StringUtils
|
|
|
|
|
2021-05-06 22:57:47 +02:00
|
|
|
class ChatActivity : ServiceBoundActivity() {
|
2021-01-26 19:45:18 +01:00
|
|
|
private external fun generateFingerprint(publicKey: ByteArray): String
|
|
|
|
|
|
|
|
private lateinit var binding: ActivityChatBinding
|
|
|
|
private var sessionId = -1
|
2021-05-27 21:00:57 +02:00
|
|
|
private var sessionName: String? = null
|
2021-05-27 20:15:03 +02:00
|
|
|
private var avatar: ByteArray? = null
|
2021-01-26 19:45:18 +01:00
|
|
|
private lateinit var chatAdapter: ChatAdapter
|
|
|
|
private var lastLoadedMessageOffset = 0
|
2021-05-06 22:57:47 +02:00
|
|
|
private val filePicker = registerForActivityResult(ActivityResultContracts.GetMultipleContents()) { uris ->
|
|
|
|
if (isServiceInitialized() && uris.size > 0) {
|
2021-07-31 22:07:32 +02:00
|
|
|
airaService.sendFilesFromUris(sessionId, uris) { buffer ->
|
|
|
|
chatAdapter.newMessage(ChatItem(true, 0, buffer))
|
|
|
|
scrollToBottom()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private val uiCallbacks = object : AIRAService.UiCallbacks {
|
|
|
|
override fun onConnectFailed(ip: String, errorMsg: String?) {}
|
|
|
|
override fun onNewSession(sessionId: Int, ip: String) {
|
|
|
|
if (this@ChatActivity.sessionId == sessionId) {
|
2021-08-02 22:03:37 +02:00
|
|
|
val contact = airaService.contacts[sessionId]
|
2021-08-03 21:18:14 +02:00
|
|
|
val hasPendingMsgs = airaService.pendingMsgs[sessionId]?.size ?: 0 > 0
|
2021-07-31 22:07:32 +02:00
|
|
|
runOnUiThread {
|
|
|
|
if (contact == null) {
|
|
|
|
binding.bottomPanel.visibility = View.VISIBLE
|
|
|
|
} else {
|
|
|
|
binding.offlineWarning.visibility = View.GONE
|
2021-08-03 21:18:14 +02:00
|
|
|
if (hasPendingMsgs) {
|
2021-07-31 22:07:32 +02:00
|
|
|
binding.sendingPendingMsgsIndicator.visibility = View.VISIBLE
|
2021-08-02 22:03:37 +02:00
|
|
|
chatAdapter.removePendingMessages()
|
2021-07-31 22:07:32 +02:00
|
|
|
scrollToBottom()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
invalidateOptionsMenu()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onSessionDisconnect(sessionId: Int) {
|
|
|
|
if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
runOnUiThread {
|
|
|
|
if (airaService.isContact(sessionId)) {
|
|
|
|
binding.offlineWarning.visibility = View.VISIBLE
|
|
|
|
} else {
|
|
|
|
hideBottomPanel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onNameTold(sessionId: Int, name: String) {
|
|
|
|
if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
runOnUiThread {
|
|
|
|
sessionName = name
|
|
|
|
binding.toolbar.title.text = name
|
|
|
|
if (avatar == null) {
|
|
|
|
binding.toolbar.avatar.setTextAvatar(name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onAvatarChanged(sessionId: Int, avatar: ByteArray?) {
|
|
|
|
if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
runOnUiThread {
|
|
|
|
this@ChatActivity.avatar = avatar
|
|
|
|
if (avatar == null) {
|
|
|
|
binding.toolbar.avatar.setTextAvatar(sessionName)
|
|
|
|
} else {
|
|
|
|
binding.toolbar.avatar.setImageAvatar(avatar)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onSent(sessionId: Int, timestamp: Long, buffer: ByteArray) {
|
|
|
|
if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
if (airaService.isContact(sessionId)) {
|
|
|
|
lastLoadedMessageOffset += 1
|
|
|
|
}
|
|
|
|
runOnUiThread {
|
|
|
|
chatAdapter.newMessage(ChatItem(true, timestamp, buffer))
|
|
|
|
scrollToBottom()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onPendingMessagesSent(sessionId: Int) {
|
|
|
|
if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
runOnUiThread {
|
|
|
|
binding.sendingPendingMsgsIndicator.visibility = View.GONE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onNewMessage(sessionId: Int, timestamp: Long, data: ByteArray): Boolean {
|
|
|
|
return if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
runOnUiThread {
|
|
|
|
chatAdapter.newMessage(ChatItem(false, timestamp, data))
|
|
|
|
scrollToBottom()
|
|
|
|
}
|
|
|
|
if (airaService.isContact(sessionId)) {
|
|
|
|
lastLoadedMessageOffset += 1
|
|
|
|
}
|
|
|
|
!airaService.isAppInBackground
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onAskLargeFiles(sessionId: Int, filesReceiver: FilesReceiver): Boolean {
|
|
|
|
return if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
runOnUiThread {
|
|
|
|
filesReceiver.ask(this@ChatActivity, sessionName ?: airaService.sessions[sessionId]!!.ip)
|
|
|
|
}
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
binding = ActivityChatBinding.inflate(layoutInflater)
|
|
|
|
setContentView(binding.root)
|
2021-05-23 20:20:23 +02:00
|
|
|
setSupportActionBar(binding.toolbar.toolbar)
|
|
|
|
supportActionBar?.setDisplayHomeAsUpEnabled(true)
|
2021-01-26 19:45:18 +01:00
|
|
|
|
|
|
|
sessionId = intent.getIntExtra("sessionId", -1)
|
|
|
|
if (sessionId != -1) {
|
2021-05-27 21:00:57 +02:00
|
|
|
chatAdapter = ChatAdapter(this@ChatActivity, ::onClickSaveFile)
|
|
|
|
binding.recyclerChat.apply {
|
|
|
|
adapter = chatAdapter
|
2021-07-31 22:07:32 +02:00
|
|
|
layoutManager = FuckRecyclerView(this@ChatActivity).apply {
|
2021-05-27 21:00:57 +02:00
|
|
|
stackFromEnd = true
|
|
|
|
}
|
|
|
|
addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
|
|
|
fun loadMsgsIfNeeded(recyclerView: RecyclerView) {
|
|
|
|
if (!recyclerView.canScrollVertically(-1) && isServiceInitialized()) {
|
|
|
|
airaService.contacts[sessionId]?.let { contact ->
|
|
|
|
loadMsgs(contact.uuid)
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
}
|
2021-05-27 21:00:57 +02:00
|
|
|
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
|
|
|
loadMsgsIfNeeded(recyclerView)
|
|
|
|
}
|
|
|
|
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
|
|
|
|
loadMsgsIfNeeded(recyclerView)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
binding.toolbar.toolbar.setOnClickListener {
|
|
|
|
showSessionInfo()
|
|
|
|
}
|
|
|
|
binding.buttonSend.setOnClickListener {
|
|
|
|
val msg = binding.editMessage.text.toString()
|
|
|
|
binding.editMessage.text.clear()
|
2021-07-31 22:07:32 +02:00
|
|
|
if (!airaService.sendOrAddToPending(sessionId, Protocol.newMessage(msg))) {
|
|
|
|
chatAdapter.newMessage(ChatItem(true, 0, Protocol.newMessage(msg)))
|
|
|
|
scrollToBottom()
|
2021-05-06 22:57:47 +02:00
|
|
|
}
|
2021-05-27 21:00:57 +02:00
|
|
|
}
|
|
|
|
binding.buttonAttach.setOnClickListener {
|
|
|
|
filePicker.launch("*/*")
|
|
|
|
}
|
|
|
|
serviceConnection = object : ServiceConnection {
|
|
|
|
override fun onServiceConnected(componentName: ComponentName?, service: IBinder) {
|
|
|
|
val binder = service as AIRAService.AIRABinder
|
|
|
|
airaService = binder.getService()
|
2021-01-26 19:45:18 +01:00
|
|
|
|
2021-06-04 12:14:21 +02:00
|
|
|
val session = airaService.sessions[sessionId]
|
2021-05-27 21:00:57 +02:00
|
|
|
val contact = airaService.contacts[sessionId]
|
2021-06-04 12:14:21 +02:00
|
|
|
if (session == null && contact == null) { //may happen when resuming activity after session disconnect
|
2021-07-31 22:07:32 +02:00
|
|
|
hideBottomPanel()
|
2021-05-27 21:00:57 +02:00
|
|
|
} else {
|
2021-06-04 12:14:21 +02:00
|
|
|
val avatar = if (contact == null) {
|
|
|
|
displayIconTrustLevel(false, false)
|
|
|
|
sessionName = airaService.savedNames[sessionId]
|
|
|
|
airaService.savedAvatars[sessionId]
|
|
|
|
} else {
|
|
|
|
displayIconTrustLevel(true, contact.verified)
|
|
|
|
sessionName = contact.name
|
|
|
|
contact.avatar
|
2021-05-27 20:15:03 +02:00
|
|
|
}
|
2021-06-04 12:14:21 +02:00
|
|
|
val ipName = sessionName ?: airaService.sessions[sessionId]!!.ip
|
|
|
|
binding.toolbar.title.text = ipName
|
|
|
|
if (avatar == null) {
|
|
|
|
binding.toolbar.avatar.setTextAvatar(sessionName)
|
|
|
|
} else {
|
|
|
|
AIRADatabase.loadAvatar(avatar)?.let { image ->
|
|
|
|
this@ChatActivity.avatar = image
|
|
|
|
binding.toolbar.avatar.setImageAvatar(image)
|
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
}
|
2021-08-02 22:03:37 +02:00
|
|
|
chatAdapter.clear()
|
|
|
|
lastLoadedMessageOffset = 0
|
|
|
|
if (contact != null) {
|
|
|
|
loadMsgs(contact.uuid)
|
|
|
|
}
|
|
|
|
airaService.savedMsgs[sessionId]?.let {
|
|
|
|
for (msg in it.asReversed()) {
|
|
|
|
chatAdapter.newLoadedMessage(ChatItem(msg.outgoing, msg.timestamp, msg.data))
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 21:18:14 +02:00
|
|
|
var hasPendingMsgs = false
|
2021-07-31 22:07:32 +02:00
|
|
|
airaService.pendingMsgs[sessionId]?.let {
|
2021-08-03 21:18:14 +02:00
|
|
|
if (it.size > 0) {
|
|
|
|
hasPendingMsgs = true
|
|
|
|
for (msg in it) {
|
|
|
|
if (msg[0] == Protocol.MESSAGE ||msg[0] == Protocol.FILE) {
|
|
|
|
chatAdapter.newMessage(ChatItem(true, 0, msg))
|
|
|
|
}
|
2021-07-31 22:07:32 +02:00
|
|
|
}
|
2021-06-04 12:14:21 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-31 22:07:32 +02:00
|
|
|
if (chatAdapter.itemCount > 0) {
|
|
|
|
scrollToBottom()
|
|
|
|
}
|
2021-06-04 12:14:21 +02:00
|
|
|
airaService.receiveFileTransfers[sessionId]?.let {
|
|
|
|
if (it.shouldAsk) {
|
|
|
|
it.ask(this@ChatActivity, ipName)
|
|
|
|
}
|
|
|
|
}
|
2021-08-03 21:18:14 +02:00
|
|
|
binding.sendingPendingMsgsIndicator.visibility = if (session == null) {
|
2021-07-31 22:07:32 +02:00
|
|
|
if (contact == null) {
|
|
|
|
hideBottomPanel()
|
2021-05-27 21:00:57 +02:00
|
|
|
} else {
|
2021-07-31 22:07:32 +02:00
|
|
|
binding.offlineWarning.visibility = View.VISIBLE
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
2021-08-03 21:18:14 +02:00
|
|
|
View.GONE
|
|
|
|
} else {
|
|
|
|
binding.offlineWarning.visibility = View.GONE
|
|
|
|
if (hasPendingMsgs) {
|
|
|
|
View.VISIBLE
|
|
|
|
} else {
|
|
|
|
View.GONE
|
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
2021-07-31 22:07:32 +02:00
|
|
|
airaService.setSeen(sessionId, true)
|
2021-05-06 22:57:47 +02:00
|
|
|
}
|
2021-07-31 22:07:32 +02:00
|
|
|
airaService.uiCallbacks = uiCallbacks
|
2021-05-27 21:00:57 +02:00
|
|
|
airaService.isAppInBackground = false
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
2021-05-27 21:00:57 +02:00
|
|
|
override fun onServiceDisconnected(name: ComponentName?) {}
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 12:14:21 +02:00
|
|
|
|
2021-07-31 22:07:32 +02:00
|
|
|
private fun hideBottomPanel() {
|
2021-06-04 12:14:21 +02:00
|
|
|
val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
|
|
|
inputManager.hideSoftInputFromWindow(binding.editMessage.windowToken, 0)
|
|
|
|
binding.bottomPanel.visibility = View.GONE
|
|
|
|
invalidateOptionsMenu()
|
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
|
2021-05-06 22:57:47 +02:00
|
|
|
private fun displayIconTrustLevel(isContact: Boolean, isVerified: Boolean) {
|
2021-05-31 11:42:42 +02:00
|
|
|
val setResource = fun (imageView: ImageView, resource: Int?) {
|
|
|
|
imageView.apply {
|
|
|
|
visibility = if (resource == null) {
|
|
|
|
View.GONE
|
|
|
|
} else {
|
|
|
|
setImageResource(resource)
|
|
|
|
View.VISIBLE
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
when {
|
|
|
|
isVerified -> {
|
2021-05-31 11:42:42 +02:00
|
|
|
setResource(binding.toolbar.toolbarImageTrustLevel, R.drawable.ic_verified)
|
|
|
|
setResource(binding.bottomImageTrustLevel, R.drawable.ic_verified)
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
isContact -> {
|
2021-05-31 11:42:42 +02:00
|
|
|
setResource(binding.toolbar.toolbarImageTrustLevel, null)
|
|
|
|
setResource(binding.bottomImageTrustLevel, null)
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
else -> {
|
2021-05-31 11:42:42 +02:00
|
|
|
setResource(binding.toolbar.toolbarImageTrustLevel, R.drawable.ic_warning)
|
|
|
|
setResource(binding.bottomImageTrustLevel, R.drawable.ic_warning)
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 22:57:47 +02:00
|
|
|
private fun loadMsgs(contactUuid: String) {
|
2021-01-26 19:45:18 +01:00
|
|
|
AIRADatabase.loadMsgs(contactUuid, lastLoadedMessageOffset, Constants.MSG_LOADING_COUNT)?.let {
|
|
|
|
for (chatItem in it.asReversed()) {
|
|
|
|
chatAdapter.newLoadedMessage(chatItem)
|
|
|
|
}
|
|
|
|
lastLoadedMessageOffset += it.size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 22:07:32 +02:00
|
|
|
private fun scrollToBottom() {
|
2021-08-14 21:44:52 +02:00
|
|
|
val target = chatAdapter.itemCount-1
|
|
|
|
if (target >= 0) {
|
|
|
|
binding.recyclerChat.smoothScrollToPosition(target)
|
|
|
|
}
|
2021-07-31 22:07:32 +02:00
|
|
|
}
|
|
|
|
|
2021-01-26 19:45:18 +01:00
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
|
|
menuInflater.inflate(R.menu.chat_activity, menu)
|
|
|
|
val contact = airaService.contacts[sessionId]
|
2021-05-27 20:15:03 +02:00
|
|
|
val isOnline = airaService.isOnline(sessionId)
|
2021-05-11 16:18:01 +02:00
|
|
|
menu.findItem(R.id.delete_conversation).isVisible = contact != null
|
2021-05-27 20:15:03 +02:00
|
|
|
menu.findItem(R.id.set_as_contact).isVisible = contact == null && isOnline
|
2021-05-11 16:18:01 +02:00
|
|
|
menu.findItem(R.id.remove_contact).isVisible = contact != null
|
|
|
|
if (contact == null) {
|
2021-01-26 19:45:18 +01:00
|
|
|
menu.findItem(R.id.verify).isVisible = false
|
|
|
|
} else {
|
|
|
|
menu.findItem(R.id.verify).isVisible = !contact.verified
|
|
|
|
}
|
2021-05-27 20:15:03 +02:00
|
|
|
menu.findItem(R.id.refresh_profile).isEnabled = isOnline
|
|
|
|
menu.findItem(R.id.session_info).isVisible = isOnline || contact != null
|
2021-01-26 19:45:18 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
return when (item.itemId) {
|
|
|
|
android.R.id.home -> {
|
|
|
|
finish()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.session_info -> {
|
2021-05-23 20:20:23 +02:00
|
|
|
showSessionInfo()
|
2021-01-26 19:45:18 +01:00
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.set_as_contact -> {
|
2021-05-27 21:00:57 +02:00
|
|
|
if (sessionName == null) {
|
|
|
|
Toast.makeText(this, R.string.no_name_error, Toast.LENGTH_SHORT).show()
|
|
|
|
} else {
|
|
|
|
if (airaService.setAsContact(sessionId, sessionName!!)) {
|
|
|
|
invalidateOptionsMenu()
|
|
|
|
displayIconTrustLevel(true, false)
|
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.remove_contact -> {
|
2021-05-27 20:15:03 +02:00
|
|
|
AlertDialog.Builder(this, R.style.CustomAlertDialog)
|
2021-01-26 19:45:18 +01:00
|
|
|
.setTitle(R.string.warning)
|
|
|
|
.setMessage(R.string.ask_remove_contact)
|
|
|
|
.setPositiveButton(R.string.delete) { _, _ ->
|
|
|
|
if (airaService.removeContact(sessionId)) {
|
|
|
|
invalidateOptionsMenu()
|
|
|
|
displayIconTrustLevel(false, false)
|
2021-08-03 21:18:14 +02:00
|
|
|
if (!airaService.isOnline(sessionId)) {
|
|
|
|
hideBottomPanel()
|
|
|
|
binding.offlineWarning.visibility = View.GONE
|
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.show()
|
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.verify -> {
|
|
|
|
airaService.contacts[sessionId]?.let { contact ->
|
|
|
|
val localFingerprint = StringUtils.beautifyFingerprint(AIRADatabase.getIdentityFingerprint())
|
|
|
|
val peerFingerprint = StringUtils.beautifyFingerprint(generateFingerprint(contact.publicKey))
|
2021-05-04 20:01:06 +02:00
|
|
|
val dialogBinding = DialogFingerprintsBinding.inflate(layoutInflater)
|
|
|
|
dialogBinding.textLocalFingerprint.text = localFingerprint
|
|
|
|
dialogBinding.textPeerFingerprint.text = peerFingerprint
|
2021-05-27 20:15:03 +02:00
|
|
|
AlertDialog.Builder(this, R.style.CustomAlertDialog)
|
2021-01-26 19:45:18 +01:00
|
|
|
.setTitle(R.string.verifying_contact)
|
2021-05-04 20:01:06 +02:00
|
|
|
.setView(dialogBinding.root)
|
2021-01-26 19:45:18 +01:00
|
|
|
.setPositiveButton(R.string.they_match) { _, _ ->
|
|
|
|
if (airaService.setVerified(sessionId)) {
|
|
|
|
invalidateOptionsMenu()
|
|
|
|
displayIconTrustLevel(true, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.delete_conversation -> {
|
2021-05-27 20:15:03 +02:00
|
|
|
AlertDialog.Builder(this, R.style.CustomAlertDialog)
|
2021-01-26 19:45:18 +01:00
|
|
|
.setTitle(R.string.warning)
|
|
|
|
.setMessage(R.string.ask_delete_conversation)
|
|
|
|
.setPositiveButton(R.string.delete) { _, _ ->
|
|
|
|
if (airaService.deleteConversation(sessionId)) {
|
|
|
|
chatAdapter.clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.show()
|
|
|
|
true
|
|
|
|
}
|
2021-05-27 20:15:03 +02:00
|
|
|
R.id.refresh_profile -> {
|
2021-07-31 22:07:32 +02:00
|
|
|
airaService.sendOrAddToPending(sessionId, Protocol.askProfileInfo())
|
2021-05-11 16:18:01 +02:00
|
|
|
true
|
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
else -> super.onOptionsItemSelected(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-08 19:54:10 +02:00
|
|
|
override fun onResume() {
|
|
|
|
super.onResume()
|
2021-05-06 22:57:47 +02:00
|
|
|
if (isServiceInitialized()) {
|
2021-01-26 19:45:18 +01:00
|
|
|
airaService.setSeen(sessionId, true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 22:07:32 +02:00
|
|
|
private fun onClickSaveFile(fileName: String, fileContent: ByteArray) {
|
|
|
|
val file = FileUtils.openFileForDownload(this, fileName)
|
|
|
|
file.outputStream?.apply {
|
|
|
|
write(fileContent)
|
|
|
|
close()
|
|
|
|
Toast.makeText(this@ChatActivity, getString(R.string.file_saved, file.fileName), Toast.LENGTH_SHORT).show()
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-23 20:20:23 +02:00
|
|
|
|
|
|
|
private fun showSessionInfo() {
|
|
|
|
val contact = airaService.contacts[sessionId]
|
|
|
|
val session = airaService.sessions[sessionId]
|
2021-05-27 20:15:03 +02:00
|
|
|
(contact?.publicKey ?: session?.peerPublicKey)?.let { publicKey -> //can be null if disconnected and not a contact
|
|
|
|
val dialogBinding = DialogInfoBinding.inflate(layoutInflater)
|
|
|
|
if (avatar == null) {
|
|
|
|
dialogBinding.avatar.setTextAvatar(sessionName)
|
2021-05-23 20:20:23 +02:00
|
|
|
} else {
|
2021-05-27 20:15:03 +02:00
|
|
|
dialogBinding.avatar.setImageAvatar(avatar!!)
|
|
|
|
}
|
|
|
|
dialogBinding.textFingerprint.text = StringUtils.beautifyFingerprint(generateFingerprint(publicKey))
|
|
|
|
if (session == null) {
|
|
|
|
dialogBinding.onlineFields.visibility = View.GONE
|
2021-05-23 20:20:23 +02:00
|
|
|
} else {
|
2021-05-27 20:15:03 +02:00
|
|
|
dialogBinding.textIp.text = session.ip
|
|
|
|
dialogBinding.textOutgoing.text = getString(if (session.outgoing) {
|
|
|
|
R.string.outgoing
|
|
|
|
} else {
|
|
|
|
R.string.incoming
|
|
|
|
})
|
|
|
|
}
|
|
|
|
dialogBinding.textIsContact.text = getString(if (contact == null) {
|
|
|
|
dialogBinding.fieldIsVerified.visibility = View.GONE
|
2021-05-23 20:20:23 +02:00
|
|
|
R.string.no
|
2021-05-27 20:15:03 +02:00
|
|
|
} else {
|
|
|
|
dialogBinding.textIsVerified.text = getString(if (contact.verified) {
|
|
|
|
R.string.yes
|
|
|
|
} else {
|
|
|
|
R.string.no
|
|
|
|
})
|
|
|
|
R.string.yes
|
2021-05-23 20:20:23 +02:00
|
|
|
})
|
2021-05-27 20:15:03 +02:00
|
|
|
AlertDialog.Builder(this, R.style.CustomAlertDialog)
|
|
|
|
.setTitle(sessionName)
|
|
|
|
.setView(dialogBinding.root)
|
|
|
|
.setPositiveButton(R.string.ok, null)
|
|
|
|
.show()
|
|
|
|
}
|
2021-05-23 20:20:23 +02:00
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|