mirror of
				https://github.com/SimpleMobileTools/Simple-File-Manager.git
				synced 2025-06-05 22:09:15 +02:00 
			
		
		
		
	use scoped storage at the text editor
This commit is contained in:
		| @@ -1,7 +1,9 @@ | |||||||
| package com.simplemobiletools.filemanager.pro.activities | package com.simplemobiletools.filemanager.pro.activities | ||||||
|  |  | ||||||
|  | import android.app.Activity | ||||||
| import android.app.SearchManager | import android.app.SearchManager | ||||||
| import android.content.Context | import android.content.Context | ||||||
|  | import android.content.Intent | ||||||
| import android.net.Uri | import android.net.Uri | ||||||
| import android.os.Bundle | import android.os.Bundle | ||||||
| import android.print.PrintAttributes | import android.print.PrintAttributes | ||||||
| @@ -24,8 +26,11 @@ import com.simplemobiletools.filemanager.pro.extensions.config | |||||||
| import com.simplemobiletools.filemanager.pro.extensions.openPath | import com.simplemobiletools.filemanager.pro.extensions.openPath | ||||||
| import kotlinx.android.synthetic.main.activity_read_text.* | import kotlinx.android.synthetic.main.activity_read_text.* | ||||||
| import java.io.File | import java.io.File | ||||||
|  | import java.io.OutputStream | ||||||
|  |  | ||||||
| class ReadTextActivity : SimpleActivity() { | class ReadTextActivity : SimpleActivity() { | ||||||
|  |     private val SELECT_SAVE_FILE_INTENT = 1 | ||||||
|  |  | ||||||
|     private var filePath = "" |     private var filePath = "" | ||||||
|     private var originalText = "" |     private var originalText = "" | ||||||
|     private var isSearchOpen = false |     private var isSearchOpen = false | ||||||
| @@ -40,16 +45,9 @@ class ReadTextActivity : SimpleActivity() { | |||||||
|             return |             return | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         handlePermission(PERMISSION_WRITE_STORAGE) { |  | ||||||
|             if (it) { |  | ||||||
|         read_text_view.onGlobalLayout { |         read_text_view.onGlobalLayout { | ||||||
|             checkIntent() |             checkIntent() | ||||||
|         } |         } | ||||||
|             } else { |  | ||||||
|                 toast(R.string.no_storage_permissions) |  | ||||||
|                 finish() |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     override fun onCreateOptionsMenu(menu: Menu): Boolean { |     override fun onCreateOptionsMenu(menu: Menu): Boolean { | ||||||
| @@ -69,6 +67,14 @@ class ReadTextActivity : SimpleActivity() { | |||||||
|         return true |         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) { |     private fun setupSearch(menu: Menu) { | ||||||
|         val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager |         val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager | ||||||
|         searchMenuItem = menu.findItem(R.id.menu_search) |         searchMenuItem = menu.findItem(R.id.menu_search) | ||||||
| @@ -111,18 +117,39 @@ class ReadTextActivity : SimpleActivity() { | |||||||
|             filePath = getRealPathFromURI(intent.data!!) ?: "" |             filePath = getRealPathFromURI(intent.data!!) ?: "" | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         SaveAsDialog(this, filePath) { |         if (filePath.isEmpty()) { | ||||||
|             getFileOutputStream(FileDirItem(it, it.getFilenameFromPath())) { |             SaveAsDialog(this, filePath, true) { path, filename -> | ||||||
|                 if (it != null) { |                 Intent(Intent.ACTION_CREATE_DOCUMENT).apply { | ||||||
|                     it.bufferedWriter().use { it.write(read_text_view.text.toString()) } |                     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) |             toast(R.string.file_saved) | ||||||
|             hideKeyboard() |             hideKeyboard() | ||||||
|         } else { |         } else { | ||||||
|             toast(R.string.unknown_error_occurred) |             toast(R.string.unknown_error_occurred) | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     private fun printText() { |     private fun printText() { | ||||||
|         try { |         try { | ||||||
|   | |||||||
| @@ -8,7 +8,8 @@ import com.simplemobiletools.commons.extensions.* | |||||||
| import com.simplemobiletools.filemanager.pro.R | import com.simplemobiletools.filemanager.pro.R | ||||||
| import kotlinx.android.synthetic.main.dialog_save_as.view.* | 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 { |     init { | ||||||
|         if (path.isEmpty()) { |         if (path.isEmpty()) { | ||||||
| @@ -30,6 +31,11 @@ class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val callb | |||||||
|             } |             } | ||||||
|  |  | ||||||
|             save_as_name.setText(name) |             save_as_name.setText(name) | ||||||
|  |  | ||||||
|  |             if (hidePath) { | ||||||
|  |                 save_as_path_label.beGone() | ||||||
|  |                 save_as_path.beGone() | ||||||
|  |             } else { | ||||||
|                 save_as_path.setOnClickListener { |                 save_as_path.setOnClickListener { | ||||||
|                     FilePickerDialog(activity, realPath, false, false, true, true) { |                     FilePickerDialog(activity, realPath, false, false, true, true) { | ||||||
|                         save_as_path.text = activity.humanizePath(it) |                         save_as_path.text = activity.humanizePath(it) | ||||||
| @@ -37,6 +43,7 @@ class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val callb | |||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|         AlertDialog.Builder(activity) |         AlertDialog.Builder(activity) | ||||||
|                 .setPositiveButton(R.string.ok, null) |                 .setPositiveButton(R.string.ok, null) | ||||||
| @@ -64,14 +71,14 @@ class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val callb | |||||||
|                                 return@setOnClickListener |                                 return@setOnClickListener | ||||||
|                             } |                             } | ||||||
|  |  | ||||||
|                             if (activity.getDoesFilePathExist(newPath)) { |                             if (!hidePath && activity.getDoesFilePathExist(newPath)) { | ||||||
|                                 val title = String.format(activity.getString(R.string.file_already_exists_overwrite), newFilename) |                                 val title = String.format(activity.getString(R.string.file_already_exists_overwrite), newFilename) | ||||||
|                                 ConfirmationDialog(activity, title) { |                                 ConfirmationDialog(activity, title) { | ||||||
|                                     callback(newPath) |                                     callback(newPath, newFilename) | ||||||
|                                     dismiss() |                                     dismiss() | ||||||
|                                 } |                                 } | ||||||
|                             } else { |                             } else { | ||||||
|                                 callback(newPath) |                                 callback(newPath, newFilename) | ||||||
|                                 dismiss() |                                 dismiss() | ||||||
|                             } |                             } | ||||||
|                         } |                         } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user