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-01-26 19:45:18 +01:00
|
|
|
import android.widget.Toast
|
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
|
|
import androidx.appcompat.app.AlertDialog
|
2021-05-05 20:54:25 +02:00
|
|
|
import androidx.core.view.updatePadding
|
2021-01-26 19:45:18 +01:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import sushi.hardcore.aira.adapters.ChatAdapter
|
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
|
|
|
|
private lateinit var sessionName: String
|
|
|
|
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-05-21 14:55:37 +02:00
|
|
|
airaService.sendFilesFromUris(sessionId, uris)
|
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) {
|
|
|
|
intent.getStringExtra("sessionName")?.let { name ->
|
|
|
|
sessionName = name
|
2021-05-21 14:55:37 +02:00
|
|
|
binding.toolbar.textAvatar.setLetterFrom(name)
|
|
|
|
binding.toolbar.title.text = name
|
2021-01-26 19:45:18 +01:00
|
|
|
chatAdapter = ChatAdapter(this@ChatActivity, ::onClickSaveFile)
|
|
|
|
binding.recyclerChat.apply {
|
|
|
|
adapter = chatAdapter
|
2021-05-05 18:07:38 +02:00
|
|
|
layoutManager = LinearLayoutManager(this@ChatActivity, LinearLayoutManager.VERTICAL, false).apply {
|
|
|
|
stackFromEnd = true
|
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
|
|
|
fun loadMsgsIfNeeded(recyclerView: RecyclerView) {
|
2021-05-06 22:57:47 +02:00
|
|
|
if (!recyclerView.canScrollVertically(-1) && isServiceInitialized()) {
|
2021-01-26 19:45:18 +01:00
|
|
|
airaService.contacts[sessionId]?.let { contact ->
|
|
|
|
loadMsgs(contact.uuid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
|
|
|
loadMsgsIfNeeded(recyclerView)
|
|
|
|
}
|
|
|
|
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
|
|
|
|
loadMsgsIfNeeded(recyclerView)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-05-23 20:20:23 +02:00
|
|
|
binding.toolbar.toolbar.setOnClickListener {
|
|
|
|
showSessionInfo()
|
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
binding.buttonSend.setOnClickListener {
|
|
|
|
val msg = binding.editMessage.text.toString()
|
|
|
|
airaService.sendTo(sessionId, Protocol.newMessage(msg))
|
|
|
|
binding.editMessage.text.clear()
|
|
|
|
chatAdapter.newMessage(ChatItem(true, Protocol.newMessage(msg)))
|
|
|
|
if (airaService.contacts.contains(sessionId)) {
|
|
|
|
lastLoadedMessageOffset += 1
|
|
|
|
}
|
|
|
|
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount)
|
|
|
|
}
|
|
|
|
binding.buttonAttach.setOnClickListener {
|
|
|
|
filePicker.launch("*/*")
|
|
|
|
}
|
|
|
|
serviceConnection = object : ServiceConnection {
|
|
|
|
override fun onServiceConnected(name: ComponentName?, service: IBinder) {
|
|
|
|
val binder = service as AIRAService.AIRABinder
|
|
|
|
airaService = binder.getService()
|
2021-01-26 19:45:18 +01:00
|
|
|
|
2021-05-06 22:57:47 +02:00
|
|
|
chatAdapter.clear()
|
|
|
|
airaService.contacts[sessionId]?.let { contact ->
|
|
|
|
displayIconTrustLevel(true, contact.verified)
|
|
|
|
loadMsgs(contact.uuid)
|
|
|
|
}
|
|
|
|
airaService.savedMsgs[sessionId]?.let {
|
|
|
|
for (chatItem in it.asReversed()) {
|
|
|
|
chatAdapter.newLoadedMessage(chatItem)
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
}
|
|
|
|
airaService.receiveFileTransfers[sessionId]?.let {
|
|
|
|
if (it.shouldAsk) {
|
|
|
|
it.ask(this@ChatActivity, sessionName)
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
}
|
|
|
|
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount)
|
|
|
|
val showBottomPanel = {
|
2021-05-21 14:55:37 +02:00
|
|
|
binding.bottomPanel.visibility = View.VISIBLE
|
2021-05-06 22:57:47 +02:00
|
|
|
}
|
|
|
|
airaService.uiCallbacks = object : AIRAService.UiCallbacks {
|
|
|
|
override fun onNewSession(sessionId: Int, ip: String) {
|
|
|
|
if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
runOnUiThread {
|
|
|
|
showBottomPanel()
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
override fun onSessionDisconnect(sessionId: Int) {
|
|
|
|
if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
runOnUiThread {
|
2021-05-21 14:55:37 +02:00
|
|
|
val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
|
|
|
|
inputManager.hideSoftInputFromWindow(binding.editMessage.windowToken, 0)
|
|
|
|
binding.bottomPanel.visibility = View.GONE
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
}
|
|
|
|
override fun onNameTold(sessionId: Int, name: String) {
|
|
|
|
if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
runOnUiThread {
|
|
|
|
sessionName = name
|
|
|
|
title = name
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
}
|
|
|
|
override fun onNewMessage(sessionId: Int, data: ByteArray): Boolean {
|
|
|
|
return if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
runOnUiThread {
|
|
|
|
chatAdapter.newMessage(ChatItem(false, data))
|
|
|
|
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount)
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
if (airaService.contacts.contains(sessionId)) {
|
|
|
|
lastLoadedMessageOffset += 1
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
2021-05-11 16:18:01 +02:00
|
|
|
!airaService.isAppInBackground
|
2021-05-06 22:57:47 +02:00
|
|
|
} else {
|
|
|
|
false
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
|
2021-05-06 22:57:47 +02:00
|
|
|
override fun onAskLargeFiles(sessionId: Int, name: String, filesReceiver: FilesReceiver): Boolean {
|
|
|
|
return if (this@ChatActivity.sessionId == sessionId) {
|
|
|
|
runOnUiThread {
|
|
|
|
filesReceiver.ask(this@ChatActivity, name)
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-06 22:57:47 +02:00
|
|
|
airaService.isAppInBackground = false
|
|
|
|
if (airaService.isOnline(sessionId)) {
|
|
|
|
showBottomPanel()
|
|
|
|
binding.recyclerChat.updatePadding(bottom = 0)
|
|
|
|
}
|
|
|
|
airaService.setSeen(sessionId, true)
|
|
|
|
}
|
|
|
|
override fun onServiceDisconnected(name: ComponentName?) {}
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-06 22:57:47 +02:00
|
|
|
private fun displayIconTrustLevel(isContact: Boolean, isVerified: Boolean) {
|
2021-01-26 19:45:18 +01:00
|
|
|
when {
|
|
|
|
isVerified -> {
|
|
|
|
binding.imageTrustLevel.setImageResource(R.drawable.ic_verified)
|
|
|
|
}
|
|
|
|
isContact -> {
|
|
|
|
binding.imageTrustLevel.setImageDrawable(null)
|
|
|
|
}
|
|
|
|
else -> {
|
|
|
|
binding.imageTrustLevel.setImageResource(R.drawable.ic_warning)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onCreateOptionsMenu(menu: Menu): Boolean {
|
|
|
|
menuInflater.inflate(R.menu.chat_activity, menu)
|
|
|
|
val contact = airaService.contacts[sessionId]
|
2021-05-11 16:18:01 +02:00
|
|
|
menu.findItem(R.id.delete_conversation).isVisible = contact != null
|
|
|
|
menu.findItem(R.id.set_as_contact).isVisible = contact == null
|
|
|
|
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-11 16:18:01 +02:00
|
|
|
menu.findItem(R.id.refresh_name).isEnabled = airaService.isOnline(sessionId)
|
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 -> {
|
|
|
|
if (airaService.setAsContact(sessionId, sessionName)) {
|
|
|
|
invalidateOptionsMenu()
|
|
|
|
displayIconTrustLevel(true, false)
|
|
|
|
}
|
|
|
|
true
|
|
|
|
}
|
|
|
|
R.id.remove_contact -> {
|
|
|
|
AlertDialog.Builder(this)
|
|
|
|
.setTitle(R.string.warning)
|
|
|
|
.setMessage(R.string.ask_remove_contact)
|
|
|
|
.setPositiveButton(R.string.delete) { _, _ ->
|
|
|
|
if (airaService.removeContact(sessionId)) {
|
|
|
|
invalidateOptionsMenu()
|
|
|
|
displayIconTrustLevel(false, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.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-01-26 19:45:18 +01:00
|
|
|
AlertDialog.Builder(this)
|
|
|
|
.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 -> {
|
|
|
|
AlertDialog.Builder(this)
|
|
|
|
.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-11 16:18:01 +02:00
|
|
|
R.id.refresh_name -> {
|
|
|
|
airaService.sendTo(sessionId, Protocol.askName())
|
|
|
|
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-05-08 19:54:10 +02:00
|
|
|
override fun onPause() {
|
|
|
|
super.onPause()
|
|
|
|
lastLoadedMessageOffset = 0
|
|
|
|
}
|
|
|
|
|
2021-01-26 19:45:18 +01:00
|
|
|
private fun onClickSaveFile(fileName: String, rawUuid: ByteArray) {
|
2021-05-08 19:54:10 +02:00
|
|
|
val buffer = AIRADatabase.loadFile(rawUuid)
|
|
|
|
if (buffer == null) {
|
|
|
|
Toast.makeText(this, R.string.loadFile_failed, Toast.LENGTH_SHORT).show()
|
|
|
|
} else {
|
|
|
|
FileUtils.openFileForDownload(this, fileName)?.apply {
|
|
|
|
write(buffer)
|
|
|
|
close()
|
2021-01-26 19:45:18 +01:00
|
|
|
Toast.makeText(this@ChatActivity, R.string.file_saved, Toast.LENGTH_SHORT).show()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-23 20:20:23 +02:00
|
|
|
|
|
|
|
private fun showSessionInfo() {
|
|
|
|
val contact = airaService.contacts[sessionId]
|
|
|
|
val session = airaService.sessions[sessionId]
|
|
|
|
val publicKey = contact?.publicKey ?: session?.peerPublicKey
|
|
|
|
val dialogBinding = DialogInfoBinding.inflate(layoutInflater)
|
|
|
|
dialogBinding.textAvatar.setLetterFrom(sessionName)
|
|
|
|
dialogBinding.textFingerprint.text = StringUtils.beautifyFingerprint(generateFingerprint(publicKey!!))
|
|
|
|
if (session == null) {
|
|
|
|
dialogBinding.onlineFields.visibility = View.GONE
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
R.string.no
|
|
|
|
} else {
|
|
|
|
dialogBinding.textIsVerified.text = getString(if (contact.verified) {
|
|
|
|
R.string.yes
|
|
|
|
} else {
|
|
|
|
R.string.no
|
|
|
|
})
|
|
|
|
R.string.yes
|
|
|
|
})
|
|
|
|
AlertDialog.Builder(this)
|
|
|
|
.setTitle(sessionName)
|
|
|
|
.setView(dialogBinding.root)
|
|
|
|
.setPositiveButton(R.string.ok, null)
|
|
|
|
.show()
|
|
|
|
}
|
2021-01-26 19:45:18 +01:00
|
|
|
}
|