add "Open As Text" item

This commit is contained in:
Himura Kazuto 2018-06-26 13:20:13 +03:00
parent f81fb06d41
commit 580ec209a2
4 changed files with 45 additions and 2 deletions

View File

@ -88,6 +88,7 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<FileD
R.id.cab_copy_path -> copyPath()
R.id.cab_set_as -> setAs()
R.id.cab_open_with -> openWith()
R.id.cab_open_as_text -> openAsText()
R.id.cab_copy_to -> copyMoveTo(true)
R.id.cab_move_to -> copyMoveTo(false)
R.id.cab_compress -> compressSelection()
@ -190,6 +191,10 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<FileD
activity.tryOpenPathIntent(getSelectedMedia().first().path, true)
}
private fun openAsText() {
activity.tryOpenPathIntent(getSelectedMedia().first().path, false, true)
}
private fun copyMoveTo(isCopyOperation: Boolean) {
val files = ArrayList<FileDirItem>()
selectedPositions.forEach {

View File

@ -15,8 +15,41 @@ fun Activity.sharePaths(paths: ArrayList<String>) {
sharePathsIntent(paths, BuildConfig.APPLICATION_ID)
}
fun Activity.tryOpenPathIntent(path: String, forceChooser: Boolean) {
if (!forceChooser && path.endsWith(".apk", true)) {
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)) {
val uri = if (isNougatPlus()) {
FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", File(path))
} else {

View File

@ -33,6 +33,10 @@
android:id="@+id/cab_open_with"
android:title="@string/open_with"
app:showAsAction="never"/>
<item
android:id="@+id/cab_open_as_text"
android:title="@string/open_as_text"
app:showAsAction="never"/>
<item
android:id="@+id/cab_copy_to"
android:title="@string/copy_to"

View File

@ -8,6 +8,7 @@
<string name="set_as_home_folder">Set as home folder</string>
<string name="home_folder_updated">Home folder updated</string>
<string name="copy_path">Copy path to clipboard</string>
<string name="open_as_text">Open as Plain Text</string>
<string name="path_copied">Path copied</string>
<string name="select_audio_file">Please select an audio file</string>
<string name="search_folder">Search folder</string>