diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/ReadTextActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/ReadTextActivity.kt index 2d34ad78..da270d14 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/ReadTextActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/ReadTextActivity.kt @@ -1,7 +1,9 @@ package com.simplemobiletools.filemanager.pro.activities +import android.app.Activity import android.app.SearchManager import android.content.Context +import android.content.Intent import android.net.Uri import android.os.Bundle import android.print.PrintAttributes @@ -24,8 +26,11 @@ import com.simplemobiletools.filemanager.pro.extensions.config import com.simplemobiletools.filemanager.pro.extensions.openPath import kotlinx.android.synthetic.main.activity_read_text.* import java.io.File +import java.io.OutputStream class ReadTextActivity : SimpleActivity() { + private val SELECT_SAVE_FILE_INTENT = 1 + private var filePath = "" private var originalText = "" private var isSearchOpen = false @@ -40,15 +45,8 @@ class ReadTextActivity : SimpleActivity() { return } - handlePermission(PERMISSION_WRITE_STORAGE) { - if (it) { - read_text_view.onGlobalLayout { - checkIntent() - } - } else { - toast(R.string.no_storage_permissions) - finish() - } + read_text_view.onGlobalLayout { + checkIntent() } } @@ -69,6 +67,14 @@ class ReadTextActivity : SimpleActivity() { return true } + override fun onActivityResult(requestCode: Int, resultCode: Int, resultData: Intent?) { + super.onActivityResult(requestCode, resultCode, resultData) + if (requestCode == SELECT_SAVE_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) { + val outputStream = contentResolver.openOutputStream(resultData.data!!) + saveTextContent(outputStream) + } + } + private fun setupSearch(menu: Menu) { val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager searchMenuItem = menu.findItem(R.id.menu_search) @@ -111,16 +117,37 @@ class ReadTextActivity : SimpleActivity() { filePath = getRealPathFromURI(intent.data!!) ?: "" } - SaveAsDialog(this, filePath) { - getFileOutputStream(FileDirItem(it, it.getFilenameFromPath())) { - if (it != null) { - it.bufferedWriter().use { it.write(read_text_view.text.toString()) } - toast(R.string.file_saved) - hideKeyboard() - } else { - toast(R.string.unknown_error_occurred) + if (filePath.isEmpty()) { + SaveAsDialog(this, filePath, true) { path, filename -> + Intent(Intent.ACTION_CREATE_DOCUMENT).apply { + type = "text/plain" + putExtra(Intent.EXTRA_TITLE, filename) + addCategory(Intent.CATEGORY_OPENABLE) + + startActivityForResult(this, SELECT_SAVE_FILE_INTENT) } } + } else { + SaveAsDialog(this, filePath, false) { path, filename -> + handlePermission(PERMISSION_WRITE_STORAGE) { + if (it) { + val file = File(path) + getFileOutputStream(file.toFileDirItem(this), true) { + saveTextContent(it) + } + } + } + } + } + } + + private fun saveTextContent(outputStream: OutputStream?) { + if (outputStream != null) { + outputStream.bufferedWriter().use { it.write(read_text_view.text.toString()) } + toast(R.string.file_saved) + hideKeyboard() + } else { + toast(R.string.unknown_error_occurred) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/SaveAsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/SaveAsDialog.kt index 97fab1b5..9290dd71 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/SaveAsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/pro/dialogs/SaveAsDialog.kt @@ -8,7 +8,8 @@ import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.filemanager.pro.R import kotlinx.android.synthetic.main.dialog_save_as.view.* -class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val callback: (savePath: String) -> Unit) { +class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val hidePath: Boolean, + val callback: (path: String, filename: String) -> Unit) { init { if (path.isEmpty()) { @@ -30,10 +31,16 @@ class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val callb } save_as_name.setText(name) - save_as_path.setOnClickListener { - FilePickerDialog(activity, realPath, false, false, true, true) { - save_as_path.text = activity.humanizePath(it) - realPath = it + + if (hidePath) { + save_as_path_label.beGone() + save_as_path.beGone() + } else { + save_as_path.setOnClickListener { + FilePickerDialog(activity, realPath, false, false, true, true) { + save_as_path.text = activity.humanizePath(it) + realPath = it + } } } } @@ -64,14 +71,14 @@ class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val callb return@setOnClickListener } - if (activity.getDoesFilePathExist(newPath)) { + if (!hidePath && activity.getDoesFilePathExist(newPath)) { val title = String.format(activity.getString(R.string.file_already_exists_overwrite), newFilename) ConfirmationDialog(activity, title) { - callback(newPath) + callback(newPath, newFilename) dismiss() } } else { - callback(newPath) + callback(newPath, newFilename) dismiss() } }