Improve handling of sessions with no name
This commit is contained in:
parent
c18bb1cb60
commit
aa4ae9ee92
@ -28,7 +28,7 @@ class ChatActivity : ServiceBoundActivity() {
|
||||
|
||||
private lateinit var binding: ActivityChatBinding
|
||||
private var sessionId = -1
|
||||
private lateinit var sessionName: String
|
||||
private var sessionName: String? = null
|
||||
private var avatar: ByteArray? = null
|
||||
private lateinit var chatAdapter: ChatAdapter
|
||||
private var lastLoadedMessageOffset = 0
|
||||
@ -47,10 +47,6 @@ class ChatActivity : ServiceBoundActivity() {
|
||||
|
||||
sessionId = intent.getIntExtra("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)
|
||||
binding.recyclerChat.apply {
|
||||
adapter = chatAdapter
|
||||
@ -90,19 +86,25 @@ class ChatActivity : ServiceBoundActivity() {
|
||||
filePicker.launch("*/*")
|
||||
}
|
||||
serviceConnection = object : ServiceConnection {
|
||||
override fun onServiceConnected(name: ComponentName?, service: IBinder) {
|
||||
override fun onServiceConnected(componentName: ComponentName?, service: IBinder) {
|
||||
val binder = service as AIRAService.AIRABinder
|
||||
airaService = binder.getService()
|
||||
|
||||
chatAdapter.clear()
|
||||
val contact = airaService.contacts[sessionId]
|
||||
if (contact == null) {
|
||||
val avatar = if (contact == null) {
|
||||
sessionName = airaService.savedNames[sessionId]
|
||||
airaService.savedAvatars[sessionId]
|
||||
} else {
|
||||
sessionName = contact.name
|
||||
contact.avatar
|
||||
}?.let {
|
||||
AIRADatabase.loadAvatar(it)?.let { image ->
|
||||
avatar = image
|
||||
}
|
||||
binding.toolbar.title.text = sessionName ?: airaService.sessions[sessionId]!!.ip
|
||||
if (avatar == null) {
|
||||
binding.toolbar.avatar.setTextAvatar(sessionName)
|
||||
} else {
|
||||
AIRADatabase.loadAvatar(avatar)?.let { image ->
|
||||
this@ChatActivity.avatar = image
|
||||
binding.toolbar.avatar.setImageAvatar(image)
|
||||
}
|
||||
}
|
||||
@ -117,7 +119,7 @@ class ChatActivity : ServiceBoundActivity() {
|
||||
}
|
||||
airaService.receiveFileTransfers[sessionId]?.let {
|
||||
if (it.shouldAsk) {
|
||||
it.ask(this@ChatActivity, sessionName)
|
||||
it.ask(this@ChatActivity)
|
||||
}
|
||||
}
|
||||
binding.recyclerChat.smoothScrollToPosition(chatAdapter.itemCount)
|
||||
@ -147,6 +149,9 @@ class ChatActivity : ServiceBoundActivity() {
|
||||
runOnUiThread {
|
||||
sessionName = name
|
||||
binding.toolbar.title.text = name
|
||||
if (avatar == null) {
|
||||
binding.toolbar.avatar.setTextAvatar(name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -176,10 +181,10 @@ class ChatActivity : ServiceBoundActivity() {
|
||||
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) {
|
||||
runOnUiThread {
|
||||
filesReceiver.ask(this@ChatActivity, name)
|
||||
filesReceiver.ask(this@ChatActivity)
|
||||
}
|
||||
true
|
||||
} else {
|
||||
@ -198,7 +203,6 @@ class ChatActivity : ServiceBoundActivity() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun displayIconTrustLevel(isContact: Boolean, isVerified: Boolean) {
|
||||
when {
|
||||
@ -251,10 +255,14 @@ class ChatActivity : ServiceBoundActivity() {
|
||||
true
|
||||
}
|
||||
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()
|
||||
displayIconTrustLevel(true, false)
|
||||
}
|
||||
}
|
||||
true
|
||||
}
|
||||
R.id.remove_contact -> {
|
||||
|
@ -71,9 +71,9 @@ class MainActivity : ServiceBoundActivity() {
|
||||
return false
|
||||
}
|
||||
|
||||
override fun onAskLargeFiles(sessionId: Int, name: String, filesReceiver: FilesReceiver): Boolean {
|
||||
override fun onAskLargeFiles(sessionId: Int, filesReceiver: FilesReceiver): Boolean {
|
||||
runOnUiThread {
|
||||
filesReceiver.ask(this@MainActivity, name)
|
||||
filesReceiver.ask(this@MainActivity)
|
||||
}
|
||||
return true
|
||||
}
|
||||
@ -322,7 +322,6 @@ class MainActivity : ServiceBoundActivity() {
|
||||
private fun launchChatActivity(session: Session) {
|
||||
startActivity(Intent(this, ChatActivity::class.java).apply {
|
||||
putExtra("sessionId", session.sessionId)
|
||||
putExtra("sessionName", airaService.getNameOf(session.sessionId))
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -31,18 +31,16 @@ class SessionAdapter(private val activity: AppCompatActivity): BaseAdapter() {
|
||||
val view: View = convertView ?: inflater.inflate(R.layout.adapter_session, parent, false)
|
||||
val currentSession = getItem(position)
|
||||
view.findViewById<TextView>(R.id.text_name).apply {
|
||||
val avatarName = if (currentSession.name == null) {
|
||||
setTextColor(if (currentSession.name == null) {
|
||||
text = currentSession.ip
|
||||
setTextColor(Color.RED)
|
||||
"?"
|
||||
Color.RED
|
||||
} else {
|
||||
text = currentSession.name
|
||||
setTextColor(Color.WHITE)
|
||||
currentSession.name!!
|
||||
}
|
||||
Color.WHITE
|
||||
})
|
||||
val avatar = view.findViewById<Avatar>(R.id.avatar)
|
||||
if (currentSession.avatar == null) {
|
||||
avatar.setTextAvatar(avatarName)
|
||||
avatar.setTextAvatar(currentSession.name)
|
||||
} else {
|
||||
avatar.setImageAvatar(currentSession.avatar!!)
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ class AIRAService : Service() {
|
||||
fun onNameTold(sessionId: Int, name: String)
|
||||
fun onAvatarChanged(sessionId: Int, avatar: ByteArray?)
|
||||
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) {
|
||||
@ -188,7 +188,7 @@ class AIRAService : Service() {
|
||||
return sessions.contains(sessionId)
|
||||
}
|
||||
|
||||
fun getNameOf(sessionId: Int): String {
|
||||
private fun getNameOf(sessionId: Int): String {
|
||||
return contacts[sessionId]?.name ?: savedNames[sessionId] ?: sessions[sessionId]!!.ip
|
||||
}
|
||||
|
||||
@ -343,11 +343,10 @@ class AIRAService : Service() {
|
||||
}
|
||||
|
||||
private fun sendNotification(sessionId: Int, msgContent: ByteArray) {
|
||||
val name = getNameOf(sessionId)
|
||||
val notificationBuilder = NotificationCompat.Builder(this, MESSAGES_NOTIFICATION_CHANNEL_ID)
|
||||
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
|
||||
.setSmallIcon(R.drawable.ic_launcher)
|
||||
.setContentTitle(name)
|
||||
.setContentTitle(getNameOf(sessionId))
|
||||
.setContentText(
|
||||
if (msgContent[0] == Protocol.MESSAGE) {
|
||||
msgContent.decodeToString(1)
|
||||
@ -358,7 +357,6 @@ class AIRAService : Service() {
|
||||
.setContentIntent(
|
||||
PendingIntent.getActivity(this, 0, Intent(this, ChatActivity::class.java).apply {
|
||||
putExtra("sessionId", sessionId)
|
||||
putExtra("sessionName", name)
|
||||
}, 0)
|
||||
)
|
||||
.setAutoCancel(true)
|
||||
@ -706,7 +704,7 @@ class AIRAService : Service() {
|
||||
Protocol.ASK_LARGE_FILES -> {
|
||||
if (!receiveFileTransfers.containsKey(sessionId) && !sendFileTransfers.containsKey(sessionId)) {
|
||||
Protocol.parseAskFiles(buffer)?.let { files ->
|
||||
val name = getNameOf(sessionId)
|
||||
val sessionName = getNameOf(sessionId)
|
||||
val filesReceiver = FilesReceiver(
|
||||
files,
|
||||
{ filesReceiver ->
|
||||
@ -723,12 +721,12 @@ class AIRAService : Service() {
|
||||
},
|
||||
this,
|
||||
notificationManager,
|
||||
name
|
||||
sessionName
|
||||
)
|
||||
receiveFileTransfers[sessionId] = filesReceiver
|
||||
var shouldSendNotification = true
|
||||
if (!isAppInBackground) {
|
||||
if (uiCallbacks?.onAskLargeFiles(sessionId, name, filesReceiver) == true) {
|
||||
if (uiCallbacks?.onAskLargeFiles(sessionId, filesReceiver) == true) {
|
||||
shouldSendNotification = false
|
||||
}
|
||||
}
|
||||
@ -737,12 +735,11 @@ class AIRAService : Service() {
|
||||
.setCategory(NotificationCompat.CATEGORY_EVENT)
|
||||
.setSmallIcon(R.drawable.ic_launcher)
|
||||
.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
|
||||
.setContentIntent(
|
||||
PendingIntent.getActivity(this, 0, Intent(this, ChatActivity::class.java).apply {
|
||||
putExtra("sessionId", sessionId)
|
||||
putExtra("sessionName", name)
|
||||
}, 0)
|
||||
)
|
||||
.setDefaults(Notification.DEFAULT_ALL)
|
||||
|
@ -15,14 +15,14 @@ class FilesReceiver(
|
||||
private val onAborted: (FilesReceiver) -> Unit,
|
||||
context: Context,
|
||||
notificationManager: NotificationManagerCompat,
|
||||
sessionName: String
|
||||
private val sessionName: String
|
||||
): FilesTransfer(context, notificationManager, sessionName) {
|
||||
var shouldAsk = true
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
fun ask(activity: AppCompatActivity, senderName: String) {
|
||||
fun ask(activity: AppCompatActivity) {
|
||||
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()
|
||||
for (file in files) {
|
||||
filesInfo.appendLine(file.fileName+" ("+FileUtils.formatSize(file.fileSize)+')')
|
||||
|
@ -33,13 +33,16 @@ class Avatar @JvmOverloads constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun setTextAvatar(name: String) {
|
||||
if (name.isNotEmpty()) {
|
||||
binding.textLetter.text = name[0].toString()
|
||||
fun setTextAvatar(name: String?) {
|
||||
val char = if (name == null || name.isEmpty()) {
|
||||
'?'
|
||||
} else {
|
||||
name[0]
|
||||
}
|
||||
binding.textLetter.text = char.toString()
|
||||
binding.imageAvatar.visibility = View.GONE
|
||||
binding.textAvatar.visibility = View.VISIBLE
|
||||
}
|
||||
}
|
||||
|
||||
fun setImageAvatar(avatar: ByteArray) {
|
||||
Glide.with(this).load(avatar).circleCrop().into(binding.imageAvatar)
|
||||
|
@ -93,6 +93,7 @@
|
||||
<string name="remove">Remove</string>
|
||||
<string name="choose_avatar">Choose avatar</string>
|
||||
<string name="message_hint">Send a message…</string>
|
||||
<string name="no_name_error">This session has no name !</string>
|
||||
|
||||
<!--accessibility strings-->
|
||||
<string name="send_file">Send file</string>
|
||||
|
Loading…
Reference in New Issue
Block a user