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 | ||||
|  | ||||
| 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,16 +45,9 @@ class ReadTextActivity : SimpleActivity() { | ||||
|             return | ||||
|         } | ||||
|  | ||||
|         handlePermission(PERMISSION_WRITE_STORAGE) { | ||||
|             if (it) { | ||||
|         read_text_view.onGlobalLayout { | ||||
|             checkIntent() | ||||
|         } | ||||
|             } else { | ||||
|                 toast(R.string.no_storage_permissions) | ||||
|                 finish() | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     override fun onCreateOptionsMenu(menu: Menu): Boolean { | ||||
| @@ -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,18 +117,39 @@ 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()) } | ||||
|         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) | ||||
|         } | ||||
|     } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     private fun printText() { | ||||
|         try { | ||||
|   | ||||
| @@ -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,6 +31,11 @@ class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val callb | ||||
|             } | ||||
|  | ||||
|             save_as_name.setText(name) | ||||
|  | ||||
|             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) | ||||
| @@ -37,6 +43,7 @@ class SaveAsDialog(val activity: BaseSimpleActivity, var path: String, val callb | ||||
|                     } | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         AlertDialog.Builder(activity) | ||||
|                 .setPositiveButton(R.string.ok, null) | ||||
| @@ -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() | ||||
|                             } | ||||
|                         } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user