mirror of
https://github.com/SimpleMobileTools/Simple-File-Manager.git
synced 2025-06-05 22:09:15 +02:00
allow saving files in the file editor
This commit is contained in:
@ -37,7 +37,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile 'com.simplemobiletools:commons:2.31.0'
|
compile 'com.simplemobiletools:commons:2.31.1'
|
||||||
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'
|
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'
|
||||||
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|
||||||
|
@ -3,14 +3,20 @@ package com.simplemobiletools.filemanager.activities
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
|
import com.simplemobiletools.commons.extensions.getFileOutputStream
|
||||||
|
import com.simplemobiletools.commons.extensions.getRealPathFromURI
|
||||||
|
import com.simplemobiletools.commons.extensions.hideKeyboard
|
||||||
import com.simplemobiletools.commons.extensions.toast
|
import com.simplemobiletools.commons.extensions.toast
|
||||||
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
|
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
|
||||||
import com.simplemobiletools.filemanager.R
|
import com.simplemobiletools.filemanager.R
|
||||||
|
import com.simplemobiletools.filemanager.dialogs.SaveAsDialog
|
||||||
import com.simplemobiletools.filemanager.extensions.config
|
import com.simplemobiletools.filemanager.extensions.config
|
||||||
import kotlinx.android.synthetic.main.activity_read_text.*
|
import kotlinx.android.synthetic.main.activity_read_text.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
class ReadTextActivity : SimpleActivity() {
|
class ReadTextActivity : SimpleActivity() {
|
||||||
|
var filePath = ""
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
setContentView(R.layout.activity_read_text)
|
setContentView(R.layout.activity_read_text)
|
||||||
@ -39,13 +45,28 @@ class ReadTextActivity : SimpleActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun saveText() {
|
private fun saveText() {
|
||||||
|
if (filePath.isEmpty()) {
|
||||||
|
filePath = getRealPathFromURI(intent.data) ?: ""
|
||||||
|
}
|
||||||
|
|
||||||
|
SaveAsDialog(this, filePath) {
|
||||||
|
getFileOutputStream(File(it)) {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun checkIntent() {
|
private fun checkIntent() {
|
||||||
read_text_view.setTextColor(config.textColor)
|
read_text_view.setTextColor(config.textColor)
|
||||||
val uri = intent.data
|
val uri = intent.data
|
||||||
val text = if (uri.scheme == "file") {
|
val text = if (uri.scheme == "file") {
|
||||||
|
filePath = uri.path
|
||||||
File(uri.path).readText()
|
File(uri.path).readText()
|
||||||
} else {
|
} else {
|
||||||
contentResolver.openInputStream(uri).bufferedReader().use { it.readText() }
|
contentResolver.openInputStream(uri).bufferedReader().use { it.readText() }
|
||||||
|
@ -0,0 +1,83 @@
|
|||||||
|
package com.simplemobiletools.filemanager.dialogs
|
||||||
|
|
||||||
|
import android.support.v7.app.AlertDialog
|
||||||
|
import android.view.LayoutInflater
|
||||||
|
import android.view.WindowManager
|
||||||
|
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
|
||||||
|
import com.simplemobiletools.commons.dialogs.FilePickerDialog
|
||||||
|
import com.simplemobiletools.commons.extensions.*
|
||||||
|
import com.simplemobiletools.filemanager.R
|
||||||
|
import com.simplemobiletools.filemanager.activities.SimpleActivity
|
||||||
|
import kotlinx.android.synthetic.main.dialog_save_as.view.*
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
class SaveAsDialog(val activity: SimpleActivity, var path: String, val callback: (savePath: String) -> Unit) {
|
||||||
|
|
||||||
|
init {
|
||||||
|
if (path.isEmpty()) {
|
||||||
|
path = "${activity.internalStoragePath}/${System.currentTimeMillis()}.txt"
|
||||||
|
}
|
||||||
|
|
||||||
|
var realPath = File(path).parent.trimEnd('/')
|
||||||
|
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_save_as, null).apply {
|
||||||
|
save_as_path.text = activity.humanizePath(realPath)
|
||||||
|
|
||||||
|
val fullName = path.getFilenameFromPath()
|
||||||
|
val dotAt = fullName.lastIndexOf(".")
|
||||||
|
var name = fullName
|
||||||
|
|
||||||
|
if (dotAt > 0) {
|
||||||
|
name = fullName.substring(0, dotAt)
|
||||||
|
val extension = fullName.substring(dotAt + 1)
|
||||||
|
save_as_extension.setText(extension)
|
||||||
|
}
|
||||||
|
|
||||||
|
save_as_name.setText(name)
|
||||||
|
save_as_path.setOnClickListener {
|
||||||
|
FilePickerDialog(activity, realPath, false, false, true) {
|
||||||
|
save_as_path.text = activity.humanizePath(it)
|
||||||
|
realPath = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AlertDialog.Builder(activity)
|
||||||
|
.setPositiveButton(R.string.ok, null)
|
||||||
|
.setNegativeButton(R.string.cancel, null)
|
||||||
|
.create().apply {
|
||||||
|
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
|
||||||
|
activity.setupDialogStuff(view, this, R.string.save_as)
|
||||||
|
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
|
||||||
|
val filename = view.save_as_name.value
|
||||||
|
val extension = view.save_as_extension.value
|
||||||
|
|
||||||
|
if (filename.isEmpty()) {
|
||||||
|
activity.toast(R.string.filename_cannot_be_empty)
|
||||||
|
return@setOnClickListener
|
||||||
|
}
|
||||||
|
|
||||||
|
if (extension.isEmpty()) {
|
||||||
|
activity.toast(R.string.extension_cannot_be_empty)
|
||||||
|
return@setOnClickListener
|
||||||
|
}
|
||||||
|
|
||||||
|
val newFile = File(realPath, "$filename.$extension")
|
||||||
|
if (!newFile.name.isAValidFilename()) {
|
||||||
|
activity.toast(R.string.filename_invalid_characters)
|
||||||
|
return@setOnClickListener
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newFile.exists()) {
|
||||||
|
val title = String.format(activity.getString(R.string.file_already_exists_overwrite), newFile.name)
|
||||||
|
ConfirmationDialog(activity, title) {
|
||||||
|
callback(newFile.absolutePath)
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
callback(newFile.absolutePath)
|
||||||
|
dismiss()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
48
app/src/main/res/layout/dialog_save_as.xml
Normal file
48
app/src/main/res/layout/dialog_save_as.xml
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout
|
||||||
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/save_as_holder"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:padding="@dimen/activity_margin">
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyTextView
|
||||||
|
android:id="@+id/save_as_path_label"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/path"
|
||||||
|
android:textSize="@dimen/smaller_text_size"/>
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyTextView
|
||||||
|
android:id="@+id/save_as_path"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="@dimen/activity_margin"
|
||||||
|
android:layout_marginLeft="@dimen/activity_margin"
|
||||||
|
android:paddingRight="@dimen/small_margin"
|
||||||
|
android:paddingTop="@dimen/small_margin"/>
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyEditText
|
||||||
|
android:id="@+id/save_as_name"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="@dimen/activity_margin"
|
||||||
|
android:singleLine="true"
|
||||||
|
android:textCursorDrawable="@null"/>
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyTextView
|
||||||
|
android:id="@+id/save_as_extension_label"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:text="@string/extension"/>
|
||||||
|
|
||||||
|
<com.simplemobiletools.commons.views.MyEditText
|
||||||
|
android:id="@+id/save_as_extension"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginBottom="@dimen/activity_margin"
|
||||||
|
android:singleLine="true"
|
||||||
|
android:textCursorDrawable="@null"/>
|
||||||
|
|
||||||
|
</LinearLayout>
|
Reference in New Issue
Block a user