From d5e0d6d02832c0f8122b1a0919ae81a8544365a8 Mon Sep 17 00:00:00 2001 From: "eric.marchand" Date: Mon, 29 Jan 2024 15:43:23 +0100 Subject: [PATCH] Adapt code to the fix of QMobileAPI crash on image conversion to stream Even if failing to upload image set metadata to avoid sending wrong text data to server fix #12 Take some uri permission to avoid security exception on images --- .../com/qmobile/qmobileui/ActionActivity.kt | 3 ++- .../ActionParametersFragment.kt | 14 +++++++---- .../activity/mainactivity/MainActivity.kt | 23 ++++++++++++++++++- 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/qmobileui/src/main/java/com/qmobile/qmobileui/ActionActivity.kt b/qmobileui/src/main/java/com/qmobile/qmobileui/ActionActivity.kt index c8c949ff..1e6f2a50 100644 --- a/qmobileui/src/main/java/com/qmobile/qmobileui/ActionActivity.kt +++ b/qmobileui/src/main/java/com/qmobile/qmobileui/ActionActivity.kt @@ -32,11 +32,12 @@ interface ActionActivity { ) fun uploadImage( - bodies: Map, + bodies: Map>, tableName: String, isFromAction: Boolean = false, taskToSendIfOffline: ActionTask?, onImageUploaded: (parameterName: String, receivedId: String) -> Unit, + onImageFailed: (parameterName: String, e: Throwable) -> Unit, onAllUploadFinished: () -> Unit ) diff --git a/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/ActionParametersFragment.kt b/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/ActionParametersFragment.kt index 5d3ff3d6..0a23e784 100644 --- a/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/ActionParametersFragment.kt +++ b/qmobileui/src/main/java/com/qmobile/qmobileui/action/actionparameters/ActionParametersFragment.kt @@ -540,7 +540,8 @@ class ActionParametersFragment : BaseFragment(), ActionProvider, MenuProvider { } private fun uploadImages(proceed: () -> Unit) { - val bodies: Map = imagesToUpload.getBodies(activity) + val activity = this.activity ?: return + val bodies: Map> = imagesToUpload.getBodies(activity) actionActivity.uploadImage( bodies = bodies, @@ -550,10 +551,15 @@ class ActionParametersFragment : BaseFragment(), ActionProvider, MenuProvider { onImageUploaded = { parameterName, receivedId -> paramsToSubmit[parameterName] = receivedId metaDataToSubmit[parameterName] = UploadHelper.UPLOADED_METADATA_STRING + }, + onImageFailed = { parameterName, throwable -> + //paramsToSubmit[parameterName] = receivedId + metaDataToSubmit[parameterName] = UploadHelper.UPLOADED_METADATA_STRING + }, + onAllUploadFinished = { + proceed() } - ) { - proceed() - } + ) } override fun getActionContent(actionUUID: String, itemId: String?): MutableMap { diff --git a/qmobileui/src/main/java/com/qmobile/qmobileui/activity/mainactivity/MainActivity.kt b/qmobileui/src/main/java/com/qmobile/qmobileui/activity/mainactivity/MainActivity.kt index 28a46243..be3b232c 100644 --- a/qmobileui/src/main/java/com/qmobile/qmobileui/activity/mainactivity/MainActivity.kt +++ b/qmobileui/src/main/java/com/qmobile/qmobileui/activity/mainactivity/MainActivity.kt @@ -8,15 +8,18 @@ package com.qmobile.qmobileui.activity.mainactivity import android.Manifest import android.annotation.SuppressLint +import android.app.Activity import android.content.Context import android.content.Intent import android.net.ConnectivityManager +import android.net.Uri import android.os.Build import android.os.Bundle import android.view.Menu import android.view.MenuItem import android.view.View import androidx.activity.addCallback +import androidx.annotation.CallSuper import androidx.appcompat.view.menu.MenuBuilder import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen import androidx.core.view.ViewCompat @@ -142,6 +145,14 @@ class MainActivity : ?.fragments ?.first() + override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { + super.onActivityResult(requestCode, resultCode, data) + + if (resultCode == Activity.RESULT_OK) { + val selectedUri: Uri = data?.data ?: return + contentResolver.takePersistableUriPermission(selectedUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) + } + } override fun onCreate(savedInstanceState: Bundle?) { installSplashScreen() super.onCreate(savedInstanceState) @@ -700,11 +711,12 @@ class MainActivity : } override fun uploadImage( - bodies: Map, + bodies: Map>, tableName: String, isFromAction: Boolean, taskToSendIfOffline: ActionTask?, onImageUploaded: (parameterName: String, receivedId: String) -> Unit, + onImageFailed: (parameterName: String, throwable: Throwable) -> Unit, onAllUploadFinished: () -> Unit ) { queryNetwork(object : NetworkChecker { @@ -714,6 +726,9 @@ class MainActivity : onImageUploaded = { parameterName, receivedId -> onImageUploaded(parameterName, receivedId) }, + onImageFailed = { parameterName, throwable -> + onImageFailed(parameterName, throwable) + }, onError = { taskToSendIfOffline?.let { taskViewModel.insert(it) } } @@ -938,6 +953,12 @@ class MainActivity : UPLOADED_METADATA_STRING ) }, + onImageFailed = { parameterName, throwable -> + pendingTask.actionInfo.metaDataToSubmit?.set( + parameterName, + UPLOADED_METADATA_STRING + ) + }, onAllUploadFinished = { sendAction(pendingTask, pendingTask.actionInfo.tableName) { // Nothing to do