From 463823ec3e9cdfd0ef888cfab34eec5fa6915589 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 11 Jul 2018 11:27:54 +0200 Subject: [PATCH] simplify Open as Plain Text, move the helper function in Commons --- app/build.gradle | 2 +- .../filemanager/extensions/Activity.kt | 44 +++---------------- app/src/main/res/values-sk/strings.xml | 2 +- 3 files changed, 8 insertions(+), 40 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b2320df0..cd2c5321 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -45,7 +45,7 @@ ext { } dependencies { - implementation 'com.simplemobiletools:commons:4.4.8' + implementation 'com.simplemobiletools:commons:4.4.9' implementation files('../libs/RootTools.jar') diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/Activity.kt index 16fef83d..fd379f3f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/Activity.kt @@ -15,41 +15,8 @@ fun Activity.sharePaths(paths: ArrayList) { sharePathsIntent(paths, BuildConfig.APPLICATION_ID) } -fun Activity.tryOpenPathIntent(path: String, forceChooser: Boolean, asText: Boolean = false) { - if (asText) { - - //TODO: Improve - - val uri = if (isNougatPlus()) { - FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", File(path)) - } else { - Uri.fromFile(File(path)) - } - - Intent().apply { - action = Intent.ACTION_VIEW - - val mimeType = "text/plain" - setDataAndType(uri, mimeType) - - addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - - if (resolveActivity(packageManager) != null) { - val chooser = Intent.createChooser(this, getString(R.string.open_with)) - try { - startActivity(if (forceChooser) chooser else this) - } catch (e: NullPointerException) { - showErrorToast(e) - } - } else { - if (!tryGenericMimeType(this, mimeType, uri)) { - toast(R.string.no_app_found) - } - } - - } - } - else if (!forceChooser && path.endsWith(".apk", true)) { +fun Activity.tryOpenPathIntent(path: String, forceChooser: Boolean, openAsText: Boolean = false) { + if (!forceChooser && path.endsWith(".apk", true)) { val uri = if (isNougatPlus()) { FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", File(path)) } else { @@ -67,12 +34,13 @@ fun Activity.tryOpenPathIntent(path: String, forceChooser: Boolean, asText: Bool } } } else { - openPath(path, forceChooser) + openPath(path, forceChooser, openAsText) } } -fun Activity.openPath(path: String, forceChooser: Boolean) { - openPathIntent(path, forceChooser, BuildConfig.APPLICATION_ID) +fun Activity.openPath(path: String, forceChooser: Boolean, openAsText: Boolean = false) { + val mimeType = if (openAsText) "text/plain" else "" + openPathIntent(path, forceChooser, BuildConfig.APPLICATION_ID, mimeType) } fun Activity.setAs(path: String) { diff --git a/app/src/main/res/values-sk/strings.xml b/app/src/main/res/values-sk/strings.xml index 6d51a2fd..9e039f74 100644 --- a/app/src/main/res/values-sk/strings.xml +++ b/app/src/main/res/values-sk/strings.xml @@ -7,7 +7,7 @@ Ísť do domovského priečinka Nastaviť ako domovský priečinok Domovský priečinok bol nastavený - Open as Plain Text + Otvoriť ako textový súbor Kopírovať cestu do schránky Cesta skopírovaná Prosím zvoľte audio súbor