11package com.simplemobiletools.filemanager.pro.activities
22
3+ import android.app.Activity
34import android.app.SearchManager
45import android.content.Context
6+ import android.content.Intent
57import android.net.Uri
68import android.os.Bundle
79import android.print.PrintAttributes
@@ -24,8 +26,11 @@ import com.simplemobiletools.filemanager.pro.extensions.config
2426import com.simplemobiletools.filemanager.pro.extensions.openPath
2527import kotlinx.android.synthetic.main.activity_read_text.*
2628import java.io.File
29+ import java.io.OutputStream
2730
2831class 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 )
0 commit comments