Skip to content

Commit 580ec20

Browse files
committed
add "Open As Text" item
1 parent f81fb06 commit 580ec20

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

app/src/main/kotlin/com/simplemobiletools/filemanager/adapters/ItemsAdapter.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<FileD
8888
R.id.cab_copy_path -> copyPath()
8989
R.id.cab_set_as -> setAs()
9090
R.id.cab_open_with -> openWith()
91+
R.id.cab_open_as_text -> openAsText()
9192
R.id.cab_copy_to -> copyMoveTo(true)
9293
R.id.cab_move_to -> copyMoveTo(false)
9394
R.id.cab_compress -> compressSelection()
@@ -190,6 +191,10 @@ class ItemsAdapter(activity: SimpleActivity, var fileDirItems: MutableList<FileD
190191
activity.tryOpenPathIntent(getSelectedMedia().first().path, true)
191192
}
192193

194+
private fun openAsText() {
195+
activity.tryOpenPathIntent(getSelectedMedia().first().path, false, true)
196+
}
197+
193198
private fun copyMoveTo(isCopyOperation: Boolean) {
194199
val files = ArrayList<FileDirItem>()
195200
selectedPositions.forEach {

app/src/main/kotlin/com/simplemobiletools/filemanager/extensions/Activity.kt

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,41 @@ fun Activity.sharePaths(paths: ArrayList<String>) {
1515
sharePathsIntent(paths, BuildConfig.APPLICATION_ID)
1616
}
1717

18-
fun Activity.tryOpenPathIntent(path: String, forceChooser: Boolean) {
19-
if (!forceChooser && path.endsWith(".apk", true)) {
18+
fun Activity.tryOpenPathIntent(path: String, forceChooser: Boolean, asText: Boolean = false) {
19+
if (asText) {
20+
21+
//TODO: Improve
22+
23+
val uri = if (isNougatPlus()) {
24+
FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", File(path))
25+
} else {
26+
Uri.fromFile(File(path))
27+
}
28+
29+
Intent().apply {
30+
action = Intent.ACTION_VIEW
31+
32+
val mimeType = "text/plain"
33+
setDataAndType(uri, mimeType)
34+
35+
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
36+
37+
if (resolveActivity(packageManager) != null) {
38+
val chooser = Intent.createChooser(this, getString(R.string.open_with))
39+
try {
40+
startActivity(if (forceChooser) chooser else this)
41+
} catch (e: NullPointerException) {
42+
showErrorToast(e)
43+
}
44+
} else {
45+
if (!tryGenericMimeType(this, mimeType, uri)) {
46+
toast(R.string.no_app_found)
47+
}
48+
}
49+
50+
}
51+
}
52+
else if (!forceChooser && path.endsWith(".apk", true)) {
2053
val uri = if (isNougatPlus()) {
2154
FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", File(path))
2255
} else {

app/src/main/res/menu/cab.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
android:id="@+id/cab_open_with"
3434
android:title="@string/open_with"
3535
app:showAsAction="never"/>
36+
<item
37+
android:id="@+id/cab_open_as_text"
38+
android:title="@string/open_as_text"
39+
app:showAsAction="never"/>
3640
<item
3741
android:id="@+id/cab_copy_to"
3842
android:title="@string/copy_to"

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<string name="set_as_home_folder">Set as home folder</string>
99
<string name="home_folder_updated">Home folder updated</string>
1010
<string name="copy_path">Copy path to clipboard</string>
11+
<string name="open_as_text">Open as Plain Text</string>
1112
<string name="path_copied">Path copied</string>
1213
<string name="select_audio_file">Please select an audio file</string>
1314
<string name="search_folder">Search folder</string>

0 commit comments

Comments
 (0)