Skip to content

Commit

Permalink
Adapt code to the fix of QMobileAPI crash on image conversion to stream
Browse files Browse the repository at this point in the history
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
  • Loading branch information
e-marchand committed Jan 29, 2024
1 parent ebbc83a commit d5e0d6d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ interface ActionActivity {
)

fun uploadImage(
bodies: Map<String, RequestBody?>,
bodies: Map<String, Result<RequestBody>>,
tableName: String,
isFromAction: Boolean = false,
taskToSendIfOffline: ActionTask?,
onImageUploaded: (parameterName: String, receivedId: String) -> Unit,
onImageFailed: (parameterName: String, e: Throwable) -> Unit,
onAllUploadFinished: () -> Unit
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,8 @@ class ActionParametersFragment : BaseFragment(), ActionProvider, MenuProvider {
}

private fun uploadImages(proceed: () -> Unit) {
val bodies: Map<String, RequestBody?> = imagesToUpload.getBodies(activity)
val activity = this.activity ?: return
val bodies: Map<String, Result<RequestBody>> = imagesToUpload.getBodies(activity)

actionActivity.uploadImage(
bodies = bodies,
Expand All @@ -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<String, Any> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -700,11 +711,12 @@ class MainActivity :
}

override fun uploadImage(
bodies: Map<String, RequestBody?>,
bodies: Map<String, Result<RequestBody>>,
tableName: String,
isFromAction: Boolean,
taskToSendIfOffline: ActionTask?,
onImageUploaded: (parameterName: String, receivedId: String) -> Unit,
onImageFailed: (parameterName: String, throwable: Throwable) -> Unit,
onAllUploadFinished: () -> Unit
) {
queryNetwork(object : NetworkChecker {
Expand All @@ -714,6 +726,9 @@ class MainActivity :
onImageUploaded = { parameterName, receivedId ->
onImageUploaded(parameterName, receivedId)
},
onImageFailed = { parameterName, throwable ->
onImageFailed(parameterName, throwable)
},
onError = {
taskToSendIfOffline?.let { taskViewModel.insert(it) }
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d5e0d6d

Please sign in to comment.