Fixed issue with picture

This commit is contained in:
Sylvain Berfini 2023-08-21 11:07:01 +02:00
parent cd3a4e0e63
commit 10bd90ab18
7 changed files with 56 additions and 21 deletions

View file

@ -114,6 +114,7 @@ class ContactsManager {
val address = account.params.identityAddress ?: continue
friend.address = address
friend.photo = "file:/storage/emulated/0/Android/data/org.linphone/files/Pictures/john.jpg" // TODO REMOVE
Log.i(
"$TAG Local contact created for account [${address.asString()}] and picture [${friend.photo}]"

View file

@ -64,7 +64,7 @@ class EditContactFragment : GenericFragment() {
if (uri != null) {
Log.i("$TAG Picture picked [$uri]")
// TODO FIXME: use a better file name
val localFileName = FileUtils.getFileStoragePath("temp", true)
val localFileName = FileUtils.getFileStoragePath("temp.jpg", true)
lifecycleScope.launch {
if (FileUtils.copyFile(uri, localFileName)) {
viewModel.picturePath.postValue(localFileName.absolutePath)

View file

@ -62,7 +62,7 @@ class NewContactFragment : GenericFragment() {
if (uri != null) {
Log.i("$TAG Picture picked [$uri]")
// TODO FIXME: use a better file name
val localFileName = FileUtils.getFileStoragePath("temp", true)
val localFileName = FileUtils.getFileStoragePath("temp.jpg", true)
lifecycleScope.launch {
if (FileUtils.copyFile(uri, localFileName)) {
viewModel.picturePath.postValue(localFileName.absolutePath)

View file

@ -29,6 +29,7 @@ import org.linphone.core.FriendList.Status
import org.linphone.core.tools.Log
import org.linphone.ui.main.contacts.model.NewOrEditNumberOrAddressModel
import org.linphone.utils.Event
import org.linphone.utils.FileUtils
class ContactNewOrEditViewModel() : ViewModel() {
companion object {
@ -123,10 +124,9 @@ class ContactNewOrEditViewModel() : ViewModel() {
vCard.familyName = lastName.value
vCard.givenName = firstName.value
// TODO FIXME : doesn't work for newly created contact
val picture = picturePath.value.orEmpty()
if (picture.isNotEmpty()) {
friend.photo = picture
friend.photo = FileUtils.getProperFilePath(picture)
}
}

View file

@ -37,9 +37,14 @@ class AccountProfileFragment : GenericFragment() {
private val pickMedia = registerForActivityResult(ActivityResultContracts.PickVisualMedia()) { uri ->
if (uri != null) {
Log.i("$TAG Picture picked [$uri]")
// TODO FIXME: use a better file name
val localFileName = FileUtils.getFileStoragePath("temp", true)
val identity = "john" // TODO FIXME
val localFileName = FileUtils.getFileStoragePath(
"$identity.jpg",
isImage = true,
overrideExisting = true
)
Log.i("$TAG Picture picked [$uri], will be stored as [${localFileName.absolutePath}]")
lifecycleScope.launch {
if (FileUtils.copyFile(uri, localFileName)) {
withContext(Dispatchers.Main) {
@ -94,7 +99,9 @@ class AccountProfileFragment : GenericFragment() {
if (found) {
startPostponedEnterTransition()
} else {
Log.e("$TAG Failed to find an account matching this identity address [$identity]")
Log.e(
"$TAG Failed to find an account matching this identity address [$identity]"
)
// TODO Error
goBack()
}

View file

@ -8,6 +8,7 @@ import org.linphone.LinphoneApplication.Companion.coreContext
import org.linphone.core.Account
import org.linphone.core.tools.Log
import org.linphone.utils.Event
import org.linphone.utils.FileUtils
class AccountProfileViewModel : ViewModel() {
companion object {
@ -72,8 +73,9 @@ class AccountProfileViewModel : ViewModel() {
@UiThread
fun setImage(file: File) {
val path = file.absolutePath
val path = FileUtils.getProperFilePath(file.absolutePath)
picturePath.value = path
coreContext.postOnCoreThread {
if (::account.isInitialized) {
val friend = coreContext.contactsManager.localFriends.find {
@ -82,6 +84,7 @@ class AccountProfileViewModel : ViewModel() {
} != null
}
if (friend != null) {
// TODO FIXME: photo must be set on Account not Friend, Friend will be re-created from Account right after this
friend.edit()
friend.photo = path
friend.done()

View file

@ -21,6 +21,7 @@ package org.linphone.utils
import android.net.Uri
import android.os.Environment
import androidx.annotation.AnyThread
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
@ -34,33 +35,54 @@ class FileUtils {
companion object {
const val TAG = "[File Utils]"
fun getFileStoragePath(fileName: String, isImage: Boolean = false): File {
@AnyThread
fun getProperFilePath(path: String): String {
if (path.startsWith("file:") || path.startsWith("content:")) {
return path
} else if (path.startsWith("/")) {
return "file:$path"
}
return "file:/$path"
}
@AnyThread
fun getFileStoragePath(
fileName: String,
isImage: Boolean = false,
overrideExisting: Boolean = false
): File {
val path = getFileStorageDir(isImage)
var file = File(path, fileName)
var prefix = 1
while (file.exists()) {
file = File(path, prefix.toString() + "_" + fileName)
Log.w("$TAG File with that name already exists, renamed to ${file.name}")
prefix += 1
if (!overrideExisting) {
var prefix = 1
while (file.exists()) {
file = File(path, prefix.toString() + "_" + fileName)
Log.w("$TAG File with that name already exists, renamed to ${file.name}")
prefix += 1
}
}
return file
}
fun getFileStorageCacheDir(fileName: String): File {
@AnyThread
fun getFileStorageCacheDir(fileName: String, overrideExisting: Boolean = false): File {
val path = coreContext.context.cacheDir
Log.i("$TAG Cache directory is: $path")
var file = File(path, fileName)
var prefix = 1
while (file.exists()) {
file = File(path, prefix.toString() + "_" + fileName)
Log.w("$TAG File with that name already exists, renamed to ${file.name}")
prefix += 1
if (!overrideExisting) {
var prefix = 1
while (file.exists()) {
file = File(path, prefix.toString() + "_" + fileName)
Log.w("$TAG File with that name already exists, renamed to ${file.name}")
prefix += 1
}
}
return file
}
@AnyThread
suspend fun copyFile(from: Uri, to: File): Boolean {
try {
withContext(Dispatchers.IO) {
@ -82,6 +104,7 @@ class FileUtils {
return false
}
@AnyThread
suspend fun dumpStringToFile(data: String, to: File): Boolean {
try {
withContext(Dispatchers.IO) {
@ -101,6 +124,7 @@ class FileUtils {
return false
}
@AnyThread
private fun getFileStorageDir(isPicture: Boolean = false): File {
var path: File? = null
if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {