Handling null Uri when selecting file & Prevent double large file transfer

This commit is contained in:
Matéo Duparc 2021-05-05 18:07:38 +02:00
parent 58804bef67
commit e7e3db60b4
Signed by: hardcoresushi
GPG Key ID: 007F84120107191E
4 changed files with 27 additions and 17 deletions

View File

@ -28,9 +28,7 @@
</intent-filter>
</receiver>
<activity
android:name=".ChatActivity"
android:windowSoftInputMode="adjustResize"/>
<activity android:name=".ChatActivity"/>
<activity android:name=".MainActivity">
<intent-filter android:label="@string/share_label">
<action android:name="android.intent.action.SEND"/>

View File

@ -23,6 +23,7 @@ import sushi.hardcore.aira.databinding.DialogFingerprintsBinding
import sushi.hardcore.aira.databinding.DialogInfoBinding
import sushi.hardcore.aira.utils.FileUtils
import sushi.hardcore.aira.utils.StringUtils
import java.io.FileNotFoundException
import java.util.*
class ChatActivity : AppCompatActivity() {
@ -39,6 +40,7 @@ class ChatActivity : AppCompatActivity() {
if (::airaService.isInitialized && uri != null) {
contentResolver.query(uri, null, null, null, null)?.let { cursor ->
if (cursor.moveToFirst()) {
try {
contentResolver.openInputStream(uri)?.let { inputStream ->
val fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME))
val fileSize = cursor.getLong(cursor.getColumnIndex(OpenableColumns.SIZE))
@ -49,6 +51,9 @@ class ChatActivity : AppCompatActivity() {
lastLoadedMessageOffset += 1
}
}
} catch (e: FileNotFoundException) {
Toast.makeText(this, e.localizedMessage, Toast.LENGTH_SHORT).show()
}
}
cursor.close()
}
@ -70,7 +75,9 @@ class ChatActivity : AppCompatActivity() {
chatAdapter = ChatAdapter(this@ChatActivity, ::onClickSaveFile)
binding.recyclerChat.apply {
adapter = chatAdapter
layoutManager = LinearLayoutManager(this@ChatActivity, LinearLayoutManager.VERTICAL, false)
layoutManager = LinearLayoutManager(this@ChatActivity, LinearLayoutManager.VERTICAL, false).apply {
stackFromEnd = true
}
addOnScrollListener(object : RecyclerView.OnScrollListener() {
fun loadMsgsIfNeeded(recyclerView: RecyclerView) {
if (!recyclerView.canScrollVertically(-1) && ::airaService.isInitialized) {

View File

@ -8,6 +8,7 @@ import android.net.nsd.NsdServiceInfo
import android.os.*
import android.os.Process.THREAD_PRIORITY_BACKGROUND
import android.util.Log
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
@ -16,7 +17,6 @@ import sushi.hardcore.aira.*
import java.io.IOException
import java.io.InputStream
import java.net.*
import java.nio.ByteBuffer
import java.nio.channels.*
class AIRAService : Service() {
@ -143,10 +143,14 @@ class AIRAService : Service() {
return msg
}
} else {
if (sendFileTransfers[sessionId] == null && receiveFileTransfers[sessionId] == null) {
val fileTransfer = SendFileTransfer(fileName, fileSize, inputStream)
sendFileTransfers[sessionId] = fileTransfer
createFileTransferNotification(sessionId, fileTransfer)
sendTo(sessionId, Protocol.askLargeFile(fileSize, fileName))
} else {
Toast.makeText(this, R.string.file_transfer_already_in_progress, Toast.LENGTH_SHORT).show()
}
}
return null
}

View File

@ -71,4 +71,5 @@
<string name="remove_contact">Remove contact</string>
<string name="details">Details</string>
<string name="your_addresses">Your IP addresses:</string>
<string name="file_transfer_already_in_progress">Another file transfer is already in progress</string>
</resources>