From 82bd8a2e2ac311b160b1883190236b06ba37820c Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Mon, 17 Feb 2020 16:29:10 +0100 Subject: [PATCH] Ask for write permission before editing media --- .../riotx/core/utils/PermissionsTools.kt | 1 + .../preview/AttachmentsPreviewFragment.kt | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/vector/src/main/java/im/vector/riotx/core/utils/PermissionsTools.kt b/vector/src/main/java/im/vector/riotx/core/utils/PermissionsTools.kt index f8cdeb3de6..4790b26ad0 100644 --- a/vector/src/main/java/im/vector/riotx/core/utils/PermissionsTools.kt +++ b/vector/src/main/java/im/vector/riotx/core/utils/PermissionsTools.kt @@ -68,6 +68,7 @@ const val PERMISSION_REQUEST_CODE_CHANGE_AVATAR = 574 const val PERMISSION_REQUEST_CODE_DOWNLOAD_FILE = 575 const val PERMISSION_REQUEST_CODE_PICK_ATTACHMENT = 576 const val PERMISSION_REQUEST_CODE_INCOMING_URI = 577 +const val PERMISSION_REQUEST_CODE_PREVIEW_FRAGMENT = 578 /** * Log the used permissions statuses. diff --git a/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt b/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt index 25f3ebc2d9..60765c495d 100644 --- a/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/attachments/preview/AttachmentsPreviewFragment.kt @@ -45,8 +45,12 @@ import im.vector.riotx.core.extensions.cleanup import im.vector.riotx.core.platform.VectorBaseFragment import im.vector.riotx.core.resources.ColorProvider import im.vector.riotx.core.utils.OnSnapPositionChangeListener +import im.vector.riotx.core.utils.PERMISSIONS_FOR_WRITING_FILES +import im.vector.riotx.core.utils.PERMISSION_REQUEST_CODE_PREVIEW_FRAGMENT import im.vector.riotx.core.utils.SnapOnScrollListener +import im.vector.riotx.core.utils.allGranted import im.vector.riotx.core.utils.attachSnapHelperWithListener +import im.vector.riotx.core.utils.checkPermissions import kotlinx.android.parcel.Parcelize import kotlinx.android.synthetic.main.fragment_attachments_preview.* import timber.log.Timber @@ -178,7 +182,22 @@ class AttachmentsPreviewFragment @Inject constructor( viewModel.handle(AttachmentsPreviewAction.RemoveCurrentAttachment) } - private fun handleEditAction() = withState(viewModel) { + private fun handleEditAction() { + // check permissions + if (checkPermissions(PERMISSIONS_FOR_WRITING_FILES, this, PERMISSION_REQUEST_CODE_PREVIEW_FRAGMENT)) { + doHandleEditAction() + } + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + + if (requestCode == PERMISSION_REQUEST_CODE_PREVIEW_FRAGMENT && allGranted(grantResults)) { + doHandleEditAction() + } + } + + private fun doHandleEditAction() = withState(viewModel) { val currentAttachment = it.attachments.getOrNull(it.currentAttachmentIndex) ?: return@withState val destinationFile = File(requireContext().cacheDir, "${currentAttachment.name}_edited_image_${System.currentTimeMillis()}") UCrop.of(currentAttachment.queryUri.toUri(), destinationFile.toUri())