From 94340a49d7ab05372d956ff49ddf4953d54870ba Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 11 Feb 2020 12:24:26 +0100 Subject: [PATCH] Ask for permission --- .../features/share/IncomingShareFragment.kt | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/vector/src/main/java/im/vector/riotx/features/share/IncomingShareFragment.kt b/vector/src/main/java/im/vector/riotx/features/share/IncomingShareFragment.kt index a107f3f698..2a2dfee6be 100644 --- a/vector/src/main/java/im/vector/riotx/features/share/IncomingShareFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/share/IncomingShareFragment.kt @@ -21,6 +21,7 @@ import android.content.Intent import android.os.Bundle import android.view.View import android.widget.Toast +import androidx.annotation.StringRes import androidx.appcompat.app.AlertDialog import androidx.appcompat.widget.SearchView import androidx.core.view.isVisible @@ -35,6 +36,10 @@ import im.vector.riotx.core.extensions.cleanup import im.vector.riotx.core.extensions.configureWith import im.vector.riotx.core.extensions.exhaustive import im.vector.riotx.core.platform.VectorBaseFragment +import im.vector.riotx.core.utils.PERMISSIONS_FOR_WRITING_FILES +import im.vector.riotx.core.utils.PERMISSION_REQUEST_CODE_PICK_ATTACHMENT +import im.vector.riotx.core.utils.allGranted +import im.vector.riotx.core.utils.checkPermissions import im.vector.riotx.features.attachments.AttachmentsHelper import im.vector.riotx.features.login.LoginActivity import kotlinx.android.synthetic.main.fragment_incoming_share.* @@ -72,10 +77,10 @@ class IncomingShareFragment @Inject constructor( isShareManaged = handleTextShare(intent) } if (!isShareManaged) { - cannotManageShare() + cannotManageShare(R.string.error_handling_incoming_share) } } else { - cannotManageShare() + cannotManageShare(R.string.error_handling_incoming_share) } incomingShareSearchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener { @@ -98,6 +103,22 @@ class IncomingShareFragment @Inject constructor( } } + override fun onResume() { + super.onResume() + + // We need the read file permission + checkPermissions(PERMISSIONS_FOR_WRITING_FILES, this, PERMISSION_REQUEST_CODE_PICK_ATTACHMENT) + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults) + + if (requestCode == PERMISSION_REQUEST_CODE_PICK_ATTACHMENT && !allGranted(grantResults)) { + // Permission is mandatory + cannotManageShare(R.string.missing_permissions_error) + } + } + private fun handleShareToRoom(event: IncomingShareViewEvents.ShareToRoom) { if (event.showAlert) { showConfirmationDialog(event.roomSummary, event.sharedData) @@ -127,11 +148,11 @@ class IncomingShareFragment @Inject constructor( } override fun onAttachmentsProcessFailed() { - cannotManageShare() + cannotManageShare(R.string.error_handling_incoming_share) } - private fun cannotManageShare() { - Toast.makeText(requireContext(), R.string.error_handling_incoming_share, Toast.LENGTH_LONG).show() + private fun cannotManageShare(@StringRes messageResId: Int) { + Toast.makeText(requireContext(), messageResId, Toast.LENGTH_LONG).show() requireActivity().finish() }