From 724d08507a807c625d500beef3bb2c6e2ee3648a Mon Sep 17 00:00:00 2001 From: PhilKes Date: Mon, 5 May 2025 18:25:36 +0200 Subject: [PATCH] Add intent-filter to open any text based file --- app/src/main/AndroidManifest.xml | 15 ++++++++ .../activity/note/EditActivity.kt | 37 +++++++++++++++---- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9332c2ec..f0053215 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -77,6 +77,21 @@ + + + + + + + + + + + + + + + diff --git a/app/src/main/java/com/philkes/notallyx/presentation/activity/note/EditActivity.kt b/app/src/main/java/com/philkes/notallyx/presentation/activity/note/EditActivity.kt index e11b3675..393b533c 100644 --- a/app/src/main/java/com/philkes/notallyx/presentation/activity/note/EditActivity.kt +++ b/app/src/main/java/com/philkes/notallyx/presentation/activity/note/EditActivity.kt @@ -188,14 +188,15 @@ abstract class EditActivity(private val type: Type) : if (persistedId == null || notallyModel.originalNote == null) { notallyModel.setState(id) } - if ( - notallyModel.isNewNote && - intent.action in setOf(Intent.ACTION_SEND, Intent.ACTION_SEND_MULTIPLE) - ) { - handleSharedNote() - } else if (notallyModel.isNewNote) { - intent.getStringExtra(EXTRA_DISPLAYED_LABEL)?.let { - notallyModel.setLabels(listOf(it)) + if (notallyModel.isNewNote) { + when (intent.action) { + Intent.ACTION_SEND, + Intent.ACTION_SEND_MULTIPLE -> handleSharedNote() + Intent.ACTION_VIEW -> handleViewNote() + else -> + intent.getStringExtra(EXTRA_DISPLAYED_LABEL)?.let { + notallyModel.setLabels(listOf(it)) + } } } @@ -756,6 +757,26 @@ abstract class EditActivity(private val type: Type) : } } + private fun handleViewNote() { + val text = + intent.data?.let { uri -> + contentResolver.openInputStream(uri)?.use { inputStream -> + inputStream.bufferedReader().readText() + } + ?: run { + showToast(R.string.cant_load_file) + null + } + } ?: intent.getStringExtra(Intent.EXTRA_TEXT) + val title = intent.getStringExtra(Intent.EXTRA_SUBJECT) + if (text != null) { + notallyModel.body = Editable.Factory.getInstance().newEditable(text) + } + if (title != null) { + notallyModel.title = title + } + } + @RequiresApi(24) override fun recordAudio() { val permission = Manifest.permission.RECORD_AUDIO