Skip to content

Commit 463823e

Browse files
committed
simplify Open as Plain Text, move the helper function in Commons
1 parent 00edde4 commit 463823e

File tree

3 files changed

+8
-40
lines changed

3 files changed

+8
-40
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ ext {
4545
}
4646

4747
dependencies {
48-
implementation 'com.simplemobiletools:commons:4.4.8'
48+
implementation 'com.simplemobiletools:commons:4.4.9'
4949

5050
implementation files('../libs/RootTools.jar')
5151

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

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

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)) {
18+
fun Activity.tryOpenPathIntent(path: String, forceChooser: Boolean, openAsText: Boolean = false) {
19+
if (!forceChooser && path.endsWith(".apk", true)) {
5320
val uri = if (isNougatPlus()) {
5421
FileProvider.getUriForFile(this, "${BuildConfig.APPLICATION_ID}.provider", File(path))
5522
} else {
@@ -67,12 +34,13 @@ fun Activity.tryOpenPathIntent(path: String, forceChooser: Boolean, asText: Bool
6734
}
6835
}
6936
} else {
70-
openPath(path, forceChooser)
37+
openPath(path, forceChooser, openAsText)
7138
}
7239
}
7340

74-
fun Activity.openPath(path: String, forceChooser: Boolean) {
75-
openPathIntent(path, forceChooser, BuildConfig.APPLICATION_ID)
41+
fun Activity.openPath(path: String, forceChooser: Boolean, openAsText: Boolean = false) {
42+
val mimeType = if (openAsText) "text/plain" else ""
43+
openPathIntent(path, forceChooser, BuildConfig.APPLICATION_ID, mimeType)
7644
}
7745

7846
fun Activity.setAs(path: String) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<string name="go_to_home_folder">Ísť do domovského priečinka</string>
88
<string name="set_as_home_folder">Nastaviť ako domovský priečinok</string>
99
<string name="home_folder_updated">Domovský priečinok bol nastavený</string>
10-
<string name="open_as_text">Open as Plain Text</string>
10+
<string name="open_as_text">Otvoriť ako textový súbor</string>
1111
<string name="copy_path">Kopírovať cestu do schránky</string>
1212
<string name="path_copied">Cesta skopírovaná</string>
1313
<string name="select_audio_file">Prosím zvoľte audio súbor</string>

0 commit comments

Comments
 (0)