Improve handling of sessions with no name

This commit is contained in:
Matéo Duparc 2021-05-27 21:00:57 +02:00
parent c18bb1cb60
commit aa4ae9ee92
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
7 changed files with 180 additions and 174 deletions

View File

@ -28,7 +28,7 @@ class ChatActivity : ServiceBoundActivity() {
private lateinit var binding: ActivityChatBinding private lateinit var binding: ActivityChatBinding
private var sessionId = -1 private var sessionId = -1
private lateinit var sessionName: String private var sessionName: String? = null
private var avatar: ByteArray? = null private var avatar: ByteArray? = null
private lateinit var chatAdapter: ChatAdapter private lateinit var chatAdapter: ChatAdapter
private var lastLoadedMessageOffset = 0 private var lastLoadedMessageOffset = 0
@ -47,10 +47,6 @@ class ChatActivity : ServiceBoundActivity() {
sessionId = intent.getIntExtra("sessionId", -1) sessionId = intent.getIntExtra("sessionId", -1)
if (sessionId != -1) { if (sessionId != -1) {
intent.getStringExtra("sessionName")?.let { name ->
sessionName = name
binding.toolbar.avatar.setTextAvatar(name)
binding.toolbar.title.text = name
chatAdapter = ChatAdapter(this@ChatActivity, ::onClickSaveFile) chatAdapter = ChatAdapter(this@ChatActivity, ::onClickSaveFile)
binding.recyclerChat.apply { binding.recyclerChat.apply {
adapter = chatAdapter adapter = chatAdapter
@ -90,19 +86,25 @@ class ChatActivity : ServiceBoundActivity() {
filePicker.launch("*/*") filePicker.launch("*/*")
} }
serviceConnection = object : ServiceConnection { serviceConnection = object : ServiceConnection {
override fun onServiceConnected(name: ComponentName?, service: IBinder) { override fun onServiceConnected(componentName: ComponentName?, service: IBinder) {
val binder = service as AIRAService.AIRABinder val binder = service as AIRAService.AIRABinder
airaService = binder.getService() airaService = binder.getService()
chatAdapter.clear() chatAdapter.clear()
val contact = airaService.contacts[sessionId] val contact = airaService.contacts[sessionId]
if (contact == null) { val avatar = if (contact == null) {
sessionName = airaService.savedNames[sessionId]
airaService.savedAvatars[sessionId] airaService.savedAvatars[sessionId]
} else { } else {
sessionName = contact.name
contact.avatar contact.avatar
}?.let { }
AIRADatabase.loadAvatar(it)?.let { image -> binding.toolbar.title.text = sessionName ?: airaService.sessions[sessionId]!!.ip
avatar = image if (avatar == null) {
binding.toolbar.avatar.setTextAvatar(sessionName)
} else {
AIRADatabase.loadAvatar(avatar)?.let { image ->
this@ChatActivity.avatar = image
binding.toolbar.avatar.setImageAvatar(image) binding.toolbar.avatar.setImageAvatar(image)
} }
} }
@ -117,7 +119,7 @@ class ChatActivity : ServiceBoundActivity() {
} }
airaService.receiveFileTransfers[sessionId]?.let { airaService.receiveFileTransfers[sessionId]?.let {
if (it.shouldAsk) { if (it.shouldAsk) {
it.ask(this@ChatActivity, sessionName) it.ask(this@ChatActivity)
} }
} }
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount) binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount)
@ -147,6 +149,9 @@ class ChatActivity : ServiceBoundActivity() {
runOnUiThread { runOnUiThread {
sessionName = name sessionName = name
binding.toolbar.title.text = name binding.toolbar.title.text = name
if (avatar == null) {
binding.toolbar.avatar.setTextAvatar(name)
}
} }
} }
} }
@ -176,10 +181,10 @@ class ChatActivity : ServiceBoundActivity() {
false false
} }
} }
override fun onAskLargeFiles(sessionId: Int, name: String, filesReceiver: FilesReceiver): Boolean { override fun onAskLargeFiles(sessionId: Int, filesReceiver: FilesReceiver): Boolean {
return if (this@ChatActivity.sessionId == sessionId) { return if (this@ChatActivity.sessionId == sessionId) {
runOnUiThread { runOnUiThread {
filesReceiver.ask(this@ChatActivity, name) filesReceiver.ask(this@ChatActivity)
} }
true true
} else { } else {
@ -198,7 +203,6 @@ class ChatActivity : ServiceBoundActivity() {
} }
} }
} }
}
private fun displayIconTrustLevel(isContact: Boolean, isVerified: Boolean) { private fun displayIconTrustLevel(isContact: Boolean, isVerified: Boolean) {
when { when {
@ -251,10 +255,14 @@ class ChatActivity : ServiceBoundActivity() {
true true
} }
R.id.set_as_contact -> { R.id.set_as_contact -> {
if (airaService.setAsContact(sessionId, sessionName)) { if (sessionName == null) {
Toast.makeText(this, R.string.no_name_error, Toast.LENGTH_SHORT).show()
} else {
if (airaService.setAsContact(sessionId, sessionName!!)) {
invalidateOptionsMenu() invalidateOptionsMenu()
displayIconTrustLevel(true, false) displayIconTrustLevel(true, false)
} }
}
true true
} }
R.id.remove_contact -> { R.id.remove_contact -> {

View File

@ -71,9 +71,9 @@ class MainActivity : ServiceBoundActivity() {
return false return false
} }
override fun onAskLargeFiles(sessionId: Int, name: String, filesReceiver: FilesReceiver): Boolean { override fun onAskLargeFiles(sessionId: Int, filesReceiver: FilesReceiver): Boolean {
runOnUiThread { runOnUiThread {
filesReceiver.ask(this@MainActivity, name) filesReceiver.ask(this@MainActivity)
} }
return true return true
} }
@ -322,7 +322,6 @@ class MainActivity : ServiceBoundActivity() {
private fun launchChatActivity(session: Session) { private fun launchChatActivity(session: Session) {
startActivity(Intent(this, ChatActivity::class.java).apply { startActivity(Intent(this, ChatActivity::class.java).apply {
putExtra("sessionId", session.sessionId) putExtra("sessionId", session.sessionId)
putExtra("sessionName", airaService.getNameOf(session.sessionId))
}) })
} }

View File

@ -31,18 +31,16 @@ class SessionAdapter(private val activity: AppCompatActivity): BaseAdapter() {
val view: View = convertView ?: inflater.inflate(R.layout.adapter_session, parent, false) val view: View = convertView ?: inflater.inflate(R.layout.adapter_session, parent, false)
val currentSession = getItem(position) val currentSession = getItem(position)
view.findViewById<TextView>(R.id.text_name).apply { view.findViewById<TextView>(R.id.text_name).apply {
val avatarName = if (currentSession.name == null) { setTextColor(if (currentSession.name == null) {
text = currentSession.ip text = currentSession.ip
setTextColor(Color.RED) Color.RED
"?"
} else { } else {
text = currentSession.name text = currentSession.name
setTextColor(Color.WHITE) Color.WHITE
currentSession.name!! })
}
val avatar = view.findViewById<Avatar>(R.id.avatar) val avatar = view.findViewById<Avatar>(R.id.avatar)
if (currentSession.avatar == null) { if (currentSession.avatar == null) {
avatar.setTextAvatar(avatarName) avatar.setTextAvatar(currentSession.name)
} else { } else {
avatar.setImageAvatar(currentSession.avatar!!) avatar.setImageAvatar(currentSession.avatar!!)
} }

View File

@ -108,7 +108,7 @@ class AIRAService : Service() {
fun onNameTold(sessionId: Int, name: String) fun onNameTold(sessionId: Int, name: String)
fun onAvatarChanged(sessionId: Int, avatar: ByteArray?) fun onAvatarChanged(sessionId: Int, avatar: ByteArray?)
fun onNewMessage(sessionId: Int, data: ByteArray): Boolean fun onNewMessage(sessionId: Int, data: ByteArray): Boolean
fun onAskLargeFiles(sessionId: Int, name: String, filesReceiver: FilesReceiver): Boolean fun onAskLargeFiles(sessionId: Int, filesReceiver: FilesReceiver): Boolean
} }
fun connectTo(ip: String) { fun connectTo(ip: String) {
@ -188,7 +188,7 @@ class AIRAService : Service() {
return sessions.contains(sessionId) return sessions.contains(sessionId)
} }
fun getNameOf(sessionId: Int): String { private fun getNameOf(sessionId: Int): String {
return contacts[sessionId]?.name ?: savedNames[sessionId] ?: sessions[sessionId]!!.ip return contacts[sessionId]?.name ?: savedNames[sessionId] ?: sessions[sessionId]!!.ip
} }
@ -343,11 +343,10 @@ class AIRAService : Service() {
} }
private fun sendNotification(sessionId: Int, msgContent: ByteArray) { private fun sendNotification(sessionId: Int, msgContent: ByteArray) {
val name = getNameOf(sessionId)
val notificationBuilder = NotificationCompat.Builder(this, MESSAGES_NOTIFICATION_CHANNEL_ID) val notificationBuilder = NotificationCompat.Builder(this, MESSAGES_NOTIFICATION_CHANNEL_ID)
.setCategory(NotificationCompat.CATEGORY_MESSAGE) .setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setSmallIcon(R.drawable.ic_launcher) .setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(name) .setContentTitle(getNameOf(sessionId))
.setContentText( .setContentText(
if (msgContent[0] == Protocol.MESSAGE) { if (msgContent[0] == Protocol.MESSAGE) {
msgContent.decodeToString(1) msgContent.decodeToString(1)
@ -358,7 +357,6 @@ class AIRAService : Service() {
.setContentIntent( .setContentIntent(
PendingIntent.getActivity(this, 0, Intent(this, ChatActivity::class.java).apply { PendingIntent.getActivity(this, 0, Intent(this, ChatActivity::class.java).apply {
putExtra("sessionId", sessionId) putExtra("sessionId", sessionId)
putExtra("sessionName", name)
}, 0) }, 0)
) )
.setAutoCancel(true) .setAutoCancel(true)
@ -706,7 +704,7 @@ class AIRAService : Service() {
Protocol.ASK_LARGE_FILES -> { Protocol.ASK_LARGE_FILES -> {
if (!receiveFileTransfers.containsKey(sessionId) && !sendFileTransfers.containsKey(sessionId)) { if (!receiveFileTransfers.containsKey(sessionId) && !sendFileTransfers.containsKey(sessionId)) {
Protocol.parseAskFiles(buffer)?.let { files -> Protocol.parseAskFiles(buffer)?.let { files ->
val name = getNameOf(sessionId) val sessionName = getNameOf(sessionId)
val filesReceiver = FilesReceiver( val filesReceiver = FilesReceiver(
files, files,
{ filesReceiver -> { filesReceiver ->
@ -723,12 +721,12 @@ class AIRAService : Service() {
}, },
this, this,
notificationManager, notificationManager,
name sessionName
) )
receiveFileTransfers[sessionId] = filesReceiver receiveFileTransfers[sessionId] = filesReceiver
var shouldSendNotification = true var shouldSendNotification = true
if (!isAppInBackground) { if (!isAppInBackground) {
if (uiCallbacks?.onAskLargeFiles(sessionId, name, filesReceiver) == true) { if (uiCallbacks?.onAskLargeFiles(sessionId, filesReceiver) == true) {
shouldSendNotification = false shouldSendNotification = false
} }
} }
@ -737,12 +735,11 @@ class AIRAService : Service() {
.setCategory(NotificationCompat.CATEGORY_EVENT) .setCategory(NotificationCompat.CATEGORY_EVENT)
.setSmallIcon(R.drawable.ic_launcher) .setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.download_file_request)) .setContentTitle(getString(R.string.download_file_request))
.setContentText(getString(R.string.want_to_send_files, name)) .setContentText(getString(R.string.want_to_send_files, sessionName))
.setOngoing(true) //not cancelable .setOngoing(true) //not cancelable
.setContentIntent( .setContentIntent(
PendingIntent.getActivity(this, 0, Intent(this, ChatActivity::class.java).apply { PendingIntent.getActivity(this, 0, Intent(this, ChatActivity::class.java).apply {
putExtra("sessionId", sessionId) putExtra("sessionId", sessionId)
putExtra("sessionName", name)
}, 0) }, 0)
) )
.setDefaults(Notification.DEFAULT_ALL) .setDefaults(Notification.DEFAULT_ALL)

View File

@ -15,14 +15,14 @@ class FilesReceiver(
private val onAborted: (FilesReceiver) -> Unit, private val onAborted: (FilesReceiver) -> Unit,
context: Context, context: Context,
notificationManager: NotificationManagerCompat, notificationManager: NotificationManagerCompat,
sessionName: String private val sessionName: String
): FilesTransfer(context, notificationManager, sessionName) { ): FilesTransfer(context, notificationManager, sessionName) {
var shouldAsk = true var shouldAsk = true
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
fun ask(activity: AppCompatActivity, senderName: String) { fun ask(activity: AppCompatActivity) {
val dialogBinding = DialogAskFileBinding.inflate(activity.layoutInflater) val dialogBinding = DialogAskFileBinding.inflate(activity.layoutInflater)
dialogBinding.textTitle.text = activity.getString(R.string.want_to_send_files, senderName)+':' dialogBinding.textTitle.text = activity.getString(R.string.want_to_send_files, sessionName)+':'
val filesInfo = StringBuilder() val filesInfo = StringBuilder()
for (file in files) { for (file in files) {
filesInfo.appendLine(file.fileName+" ("+FileUtils.formatSize(file.fileSize)+')') filesInfo.appendLine(file.fileName+" ("+FileUtils.formatSize(file.fileSize)+')')

View File

@ -33,13 +33,16 @@ class Avatar @JvmOverloads constructor(
} }
} }
fun setTextAvatar(name: String) { fun setTextAvatar(name: String?) {
if (name.isNotEmpty()) { val char = if (name == null || name.isEmpty()) {
binding.textLetter.text = name[0].toString() '?'
} else {
name[0]
}
binding.textLetter.text = char.toString()
binding.imageAvatar.visibility = View.GONE binding.imageAvatar.visibility = View.GONE
binding.textAvatar.visibility = View.VISIBLE binding.textAvatar.visibility = View.VISIBLE
} }
}
fun setImageAvatar(avatar: ByteArray) { fun setImageAvatar(avatar: ByteArray) {
Glide.with(this).load(avatar).circleCrop().into(binding.imageAvatar) Glide.with(this).load(avatar).circleCrop().into(binding.imageAvatar)

View File

@ -93,6 +93,7 @@
<string name="remove">Remove</string> <string name="remove">Remove</string>
<string name="choose_avatar">Choose avatar</string> <string name="choose_avatar">Choose avatar</string>
<string name="message_hint">Send a message…</string> <string name="message_hint">Send a message…</string>
<string name="no_name_error">This session has no name !</string>
<!--accessibility strings--> <!--accessibility strings-->
<string name="send_file">Send file</string> <string name="send_file">Send file</string>