simplify Open as Plain Text, move the helper function in Commons

This commit is contained in:
tibbi 2018-07-11 11:27:54 +02:00
parent 00edde45b5
commit 463823ec3e
3 changed files with 8 additions and 40 deletions

View file

@ -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')

View file

@ -15,41 +15,8 @@ fun Activity.sharePaths(paths: ArrayList<String>) {
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) {

View file

@ -7,7 +7,7 @@
<string name="go_to_home_folder">Ísť do domovského priečinka</string>
<string name="set_as_home_folder">Nastaviť ako domovský priečinok</string>
<string name="home_folder_updated">Domovský priečinok bol nastavený</string>
<string name="open_as_text">Open as Plain Text</string>
<string name="open_as_text">Otvoriť ako textový súbor</string>
<string name="copy_path">Kopírovať cestu do schránky</string>
<string name="path_copied">Cesta skopírovaná</string>
<string name="select_audio_file">Prosím zvoľte audio súbor</string>