Skip to content

Commit d0808bb

Browse files
committed
use scoped storage at the text editor
1 parent c85f883 commit d0808bb

File tree

2 files changed

+59
-25
lines changed

2 files changed

+59
-25
lines changed

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/ReadTextActivity.kt

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.simplemobiletools.filemanager.pro.activities
22

3+
import android.app.Activity
34
import android.app.SearchManager
45
import android.content.Context
6+
import android.content.Intent
57
import android.net.Uri
68
import android.os.Bundle
79
import android.print.PrintAttributes
@@ -24,8 +26,11 @@ import com.simplemobiletools.filemanager.pro.extensions.config
2426
import com.simplemobiletools.filemanager.pro.extensions.openPath
2527
import kotlinx.android.synthetic.main.activity_read_text.*
2628
import java.io.File
29+
import java.io.OutputStream
2730

2831
class ReadTextActivity : SimpleActivity() {
32+
private val SELECT_SAVE_FILE_INTENT = 1
33+
2934
private var filePath = ""
3035
private var originalText = ""
3136
private var isSearchOpen = false
@@ -40,15 +45,8 @@ class ReadTextActivity : SimpleActivity() {
4045
return
4146
}
4247

43-
handlePermission(PERMISSION_WRITE_STORAGE) {
44-
if (it) {
45-
read_text_view.onGlobalLayout {
46-
checkIntent()
47-
}
48-
} else {
49-
toast(R.string.no_storage_permissions)
50-
finish()
51-
}
48+
read_text_view.onGlobalLayout {
49+
checkIntent()
5250
}
5351
}
5452

@@ -69,6 +67,14 @@ class ReadTextActivity : SimpleActivity() {
6967
return true
7068
}
7169

70+
override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) {
71+
super.onActivityResult(requestCode, resultCode, resultData)
72+
if (requestCode == SELECT_SAVE_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
73+
val outputStream = contentResolver.openOutputStream(resultData.data!!)
74+
saveTextContent(outputStream)
75+
}
76+
}
77+
7278
private fun setupSearch(menu: Menu) {
7379
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
7480
searchMenuItem = menu.findItem(R.id.menu_search)
@@ -111,19 +117,40 @@ class ReadTextActivity : SimpleActivity() {
111117
filePath = getRealPathFromURI(intent.data!!) ?: ""
112118
}
113119

114-
SaveAsDialog(this, filePath) {
115-
getFileOutputStream(FileDirItem(it, it.getFilenameFromPath())) {
116-
if (it != null) {
117-
it.bufferedWriter().use { it.write(read_text_view.text.toString()) }
118-
toast(R.string.file_saved)
119-
hideKeyboard()
120-
} else {
121-
toast(R.string.unknown_error_occurred)
120+
if (filePath.isEmpty()) {
121+
SaveAsDialog(this, filePath, true) { path, filename ->
122+
Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
123+
type = "text/plain"
124+
putExtra(Intent.EXTRA_TITLE, filename)
125+
addCategory(Intent.CATEGORY_OPENABLE)
126+
127+
startActivityForResult(this, SELECT_SAVE_FILE_INTENT)
128+
}
129+
}
130+
} else {
131+
SaveAsDialog(this, filePath, false) { path, filename ->
132+
handlePermission(PERMISSION_WRITE_STORAGE) {
133+
if (it) {
134+
val file = File(path)
135+
getFileOutputStream(file.toFileDirItem(this), true) {
136+
saveTextContent(it)
137+
}
138+
}
122139
}
123140
}
124141
}
125142
}
126143

144+
private fun saveTextContent(outputStream: OutputStream?) {
145+
if (outputStream != null) {
146+
outputStream.bufferedWriter().use { it.write(read_text_view.text.toString()) }
147+
toast(R.string.file_saved)
148+
hideKeyboard()
149+
} else {
150+
toast(R.string.unknown_error_occurred)
151+
}
152+
}
153+
127154
private fun printText() {
128155
try {
129156
val webView = WebView(this)

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/SaveAsDialog.kt

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import com.simplemobiletools.commons.extensions.*
88
import com.simplemobiletools.filemanager.pro.R
99
import kotlinx.android.synthetic.main.dialog_save_as.view.*
1010

11-
class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val callback: (savePath: String) -> Unit) {
11+
class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val hidePath: Boolean,
12+
val callback: (path: String, filename: String) -> Unit) {
1213

1314
init {
1415
if (path.isEmpty()) {
@@ -30,10 +31,16 @@ class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val callb
3031
}
3132

3233
save_as_name.setText(name)
33-
save_as_path.setOnClickListener {
34-
FilePickerDialog(activity, realPath, false, false, true, true) {
35-
save_as_path.text = activity.humanizePath(it)
36-
realPath = it
34+
35+
if (hidePath) {
36+
save_as_path_label.beGone()
37+
save_as_path.beGone()
38+
} else {
39+
save_as_path.setOnClickListener {
40+
FilePickerDialog(activity, realPath, false, false, true, true) {
41+
save_as_path.text = activity.humanizePath(it)
42+
realPath = it
43+
}
3744
}
3845
}
3946
}
@@ -64,14 +71,14 @@ class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val callb
6471
return@setOnClickListener
6572
}
6673

67-
if (activity.getDoesFilePathExist(newPath)) {
74+
if (!hidePath && activity.getDoesFilePathExist(newPath)) {
6875
val title = String.format(activity.getString(R.string.file_already_exists_overwrite), newFilename)
6976
ConfirmationDialog(activity, title) {
70-
callback(newPath)
77+
callback(newPath, newFilename)
7178
dismiss()
7279
}
7380
} else {
74-
callback(newPath)
81+
callback(newPath, newFilename)
7582
dismiss()
7683
}
7784
}

0 commit comments

Comments
 (0)