android app files folder

This commit is contained in:
JRoberts
2022-04-15 23:56:57 +04:00
parent 6f0ecbb540
commit 279688f5e7
5 changed files with 37 additions and 12 deletions
@@ -5,6 +5,7 @@ import android.net.LocalServerSocket
import android.util.Log
import androidx.lifecycle.*
import chat.simplex.app.model.*
import chat.simplex.app.views.helpers.getFilesDirectory
import chat.simplex.app.views.helpers.withApi
import java.io.BufferedReader
import java.io.InputStreamReader
@@ -27,7 +28,7 @@ external fun chatRecvMsg(ctrl: ChatCtrl) : String
class SimplexApp: Application(), LifecycleEventObserver {
val chatController: ChatController by lazy {
val ctrl = chatInit(applicationContext.filesDir.toString())
val ctrl = chatInit(getFilesDirectory(applicationContext))
ChatController(ctrl, ntfManager, applicationContext)
}
@@ -39,6 +39,7 @@ open class ChatController(private val ctrl: ChatCtrl, private val ntfManager: Nt
Log.d(TAG, "user: $user")
try {
apiStartChat()
apiSetFilesFolder(getAppFilesDirectory(appContext))
chatModel.userAddress.value = apiGetUserAddress()
chatModel.userSMPServers.value = getUserSMPServers()
val chats = apiGetChats()
@@ -132,6 +133,12 @@ open class ChatController(private val ctrl: ChatCtrl, private val ntfManager: Nt
throw Error("failed starting chat: ${r.responseType} ${r.details}")
}
suspend fun apiSetFilesFolder(filesFolder: String) {
val r = sendCmd(CC.SetFilesFolder(filesFolder))
if (r is CR.CmdOk) return
throw Error("failed to set files folder: ${r.responseType} ${r.details}")
}
suspend fun apiGetChats(): List<Chat> {
val r = sendCmd(CC.ApiGetChats())
if (r is CR.ApiChats ) return r.chats
@@ -480,6 +487,7 @@ sealed class CC {
class ShowActiveUser: CC()
class CreateActiveUser(val profile: Profile): CC()
class StartChat: CC()
class SetFilesFolder(val filesFolder: String): CC()
class ApiGetChats: CC()
class ApiGetChat(val type: ChatType, val id: Long): CC()
class ApiSendMessage(val type: ChatType, val id: Long, val file: String?, val quotedItemId: Long?, val mc: MsgContent): CC()
@@ -504,6 +512,7 @@ sealed class CC {
is ShowActiveUser -> "/u"
is CreateActiveUser -> "/u ${profile.displayName} ${profile.fullName}"
is StartChat -> "/_start"
is SetFilesFolder -> "/_files_folder $filesFolder"
is ApiGetChats -> "/_get chats"
is ApiGetChat -> "/_get chat ${chatRef(type, id)} count=100"
is ApiSendMessage -> when {
@@ -535,6 +544,7 @@ sealed class CC {
is ShowActiveUser -> "showActiveUser"
is CreateActiveUser -> "createActiveUser"
is StartChat -> "startChat"
is SetFilesFolder -> "setFilesFolder"
is ApiGetChats -> "apiGetChats"
is ApiGetChat -> "apiGetChat"
is ApiSendMessage -> "apiSendMessage"
@@ -147,13 +147,13 @@ fun ChatView(chatModel: ChatModel) {
fun saveImage(context: Context, image: Bitmap): String {
val imageResized = base64ToBitmap(resizeImageToDataSize(image, 160000))
val imageName = "image_${System.currentTimeMillis()}.jpg"
val file = File(context.filesDir, imageName)
val fileToSave = "image_${System.currentTimeMillis()}.jpg"
val file = File(getAppFilesDirectory(context) + "/" + fileToSave)
val output = FileOutputStream(file)
imageResized.compress(Bitmap.CompressFormat.JPEG, 100, output)
output.flush()
output.close()
return file.absolutePath
return fileToSave
}
@Composable
@@ -12,20 +12,25 @@ import androidx.core.content.FileProvider
import chat.simplex.app.BuildConfig
import chat.simplex.app.model.CIFile
import chat.simplex.app.views.helpers.base64ToBitmap
import chat.simplex.app.views.helpers.getAppFilesDirectory
import java.io.*
@Composable
fun ChatItemImageView(image: String, file: CIFile?) {
Column {
val imageBitmap = if (
file?.filePath != null &&
File(file.filePath).exists() &&
file.stored // TODO more advanced approach would be to send progressive jpeg and only check for filepath
) {
val imageBitmap = if (file?.filePath != null) {
val context = LocalContext.current
try {
getBitmapFromUri(context, file.filePath)
} catch (e: Exception) {
val filePath = getAppFilesDirectory(context) + "/" + file.filePath
if (
File(filePath).exists() &&
file.stored // TODO more advanced approach would be to send progressive jpeg and only check for filepath
) {
try {
getBitmapFromUri(context, filePath)
} catch (e: Exception) {
base64ToBitmap(image)
}
} else {
base64ToBitmap(image)
}
} else {
@@ -1,5 +1,6 @@
package chat.simplex.app.views.helpers
import android.content.Context
import android.graphics.Rect
import android.view.ViewTreeObserver
import androidx.compose.runtime.*
@@ -38,3 +39,11 @@ fun getKeyboardState(): State<KeyboardState> {
return keyboardState
}
fun getFilesDirectory(context: Context): String {
return context.filesDir.toString()
}
fun getAppFilesDirectory(context: Context): String {
return getFilesDirectory(context) + "/app_files"
}