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 01ca16bf35..03cf248b79 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 @@ -66,6 +66,7 @@ const val PERMISSION_REQUEST_CODE_AUDIO_CALL = 571 const val PERMISSION_REQUEST_CODE_VIDEO_CALL = 572 const val PERMISSION_REQUEST_CODE_EXPORT_KEYS = 573 const val PERMISSION_REQUEST_CODE_CHANGE_AVATAR = 574 +const val PERMISSION_REQUEST_CODE_DOWNLOAD_FILE = 575 /** * Log the used permissions statuses. diff --git a/vector/src/main/java/im/vector/riotx/features/crypto/keysbackup/setup/KeysBackupSetupActivity.kt b/vector/src/main/java/im/vector/riotx/features/crypto/keysbackup/setup/KeysBackupSetupActivity.kt index 77780016b9..0677224f51 100644 --- a/vector/src/main/java/im/vector/riotx/features/crypto/keysbackup/setup/KeysBackupSetupActivity.kt +++ b/vector/src/main/java/im/vector/riotx/features/crypto/keysbackup/setup/KeysBackupSetupActivity.kt @@ -27,7 +27,7 @@ import im.vector.riotx.R import im.vector.riotx.core.dialogs.ExportKeysDialog import im.vector.riotx.core.extensions.observeEvent import im.vector.riotx.core.platform.SimpleFragmentActivity -import im.vector.riotx.core.utils.toast +import im.vector.riotx.core.utils.* import im.vector.riotx.features.crypto.keys.KeysExporter class KeysBackupSetupActivity : SimpleFragmentActivity() { @@ -132,39 +132,48 @@ class KeysBackupSetupActivity : SimpleFragmentActivity() { } private fun exportKeysManually() { - ExportKeysDialog().show(this, object : ExportKeysDialog.ExportKeyDialogListener { - override fun onPassphrase(passphrase: String) { - showWaitingView() + if (checkPermissions(PERMISSIONS_FOR_WRITING_FILES, this, PERMISSION_REQUEST_CODE_EXPORT_KEYS)) { + ExportKeysDialog().show(this, object : ExportKeysDialog.ExportKeyDialogListener { + override fun onPassphrase(passphrase: String) { + showWaitingView() - KeysExporter(session) - .export(this@KeysBackupSetupActivity, - passphrase, - object : MatrixCallback { + KeysExporter(session) + .export(this@KeysBackupSetupActivity, + passphrase, + object : MatrixCallback { - override fun onSuccess(data: String) { - hideWaitingView() + override fun onSuccess(data: String) { + hideWaitingView() - AlertDialog.Builder(this@KeysBackupSetupActivity) - .setMessage(getString(R.string.encryption_export_saved_as, data)) - .setCancelable(false) - .setPositiveButton(R.string.ok) { dialog, which -> - val resultIntent = Intent() - resultIntent.putExtra(MANUAL_EXPORT, true) - setResult(RESULT_OK, resultIntent) - finish() - } - .show() - } + AlertDialog.Builder(this@KeysBackupSetupActivity) + .setMessage(getString(R.string.encryption_export_saved_as, data)) + .setCancelable(false) + .setPositiveButton(R.string.ok) { dialog, which -> + val resultIntent = Intent() + resultIntent.putExtra(MANUAL_EXPORT, true) + setResult(RESULT_OK, resultIntent) + finish() + } + .show() + } - override fun onFailure(failure: Throwable) { - toast(failure.localizedMessage) - hideWaitingView() - } - }) - } - }) + override fun onFailure(failure: Throwable) { + toast(failure.localizedMessage) + hideWaitingView() + } + }) + } + }) + } } + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + if (allGranted(grantResults)) { + if (requestCode == PERMISSION_REQUEST_CODE_EXPORT_KEYS) { + exportKeysManually() + } + } + } override fun onBackPressed() { if (viewModel.shouldPromptOnBack) { diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt index ed137f24c8..1439a1e9dc 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailFragment.kt @@ -621,7 +621,26 @@ class RoomDetailFragment : } override fun onFileMessageClicked(eventId: String, messageFileContent: MessageFileContent) { - roomDetailViewModel.process(RoomDetailActions.DownloadFile(eventId, messageFileContent)) + val action = RoomDetailActions.DownloadFile(eventId, messageFileContent) + // We need WRITE_EXTERNAL permission + if (checkPermissions(PERMISSIONS_FOR_WRITING_FILES, this, PERMISSION_REQUEST_CODE_DOWNLOAD_FILE)) { + roomDetailViewModel.process(action) + } else { + roomDetailViewModel.pendingAction = action + } + } + + override fun onRequestPermissionsResult(requestCode: Int, permissions: Array, grantResults: IntArray) { + if (allGranted(grantResults)) { + if (requestCode == PERMISSION_REQUEST_CODE_DOWNLOAD_FILE) { + val action = roomDetailViewModel.pendingAction + + if (action != null) { + roomDetailViewModel.pendingAction = null + roomDetailViewModel.process(action) + } + } + } } override fun onAudioMessageClicked(messageAudioContent: MessageAudioContent) { diff --git a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt index 69c35b380b..9e0fda9146 100644 --- a/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt +++ b/vector/src/main/java/im/vector/riotx/features/home/room/detail/RoomDetailViewModel.kt @@ -75,6 +75,9 @@ class RoomDetailViewModel @AssistedInject constructor(@Assisted initialState: Ro } private var timeline = room.createTimeline(eventId, allowedTypes) + // Slot to keep a pending action during permission request + var pendingAction: RoomDetailActions? = null + @AssistedInject.Factory interface Factory { fun create(initialState: RoomDetailViewState): RoomDetailViewModel