Skip to content

Commit 24ae124

Browse files
committed
allow saving files in the file editor
1 parent cb4b63a commit 24ae124

File tree

4 files changed

+153
-1
lines changed

4 files changed

+153
-1
lines changed

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ android {
3737
}
3838

3939
dependencies {
40-
compile 'com.simplemobiletools:commons:2.31.0'
40+
compile 'com.simplemobiletools:commons:2.31.1'
4141
compile 'com.bignerdranch.android:recyclerview-multiselect:0.2'
4242
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
4343

app/src/main/kotlin/com/simplemobiletools/filemanager/activities/ReadTextActivity.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ package com.simplemobiletools.filemanager.activities
33
import android.os.Bundle
44
import android.view.Menu
55
import android.view.MenuItem
6+
import com.simplemobiletools.commons.extensions.getFileOutputStream
7+
import com.simplemobiletools.commons.extensions.getRealPathFromURI
8+
import com.simplemobiletools.commons.extensions.hideKeyboard
69
import com.simplemobiletools.commons.extensions.toast
710
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE
811
import com.simplemobiletools.filemanager.R
12+
import com.simplemobiletools.filemanager.dialogs.SaveAsDialog
913
import com.simplemobiletools.filemanager.extensions.config
1014
import kotlinx.android.synthetic.main.activity_read_text.*
1115
import java.io.File
1216

1317
class ReadTextActivity : SimpleActivity() {
18+
var filePath = ""
19+
1420
override fun onCreate(savedInstanceState: Bundle?) {
1521
super.onCreate(savedInstanceState)
1622
setContentView(R.layout.activity_read_text)
@@ -39,13 +45,28 @@ class ReadTextActivity : SimpleActivity() {
3945
}
4046

4147
private fun saveText() {
48+
if (filePath.isEmpty()) {
49+
filePath = getRealPathFromURI(intent.data) ?: ""
50+
}
4251

52+
SaveAsDialog(this, filePath) {
53+
getFileOutputStream(File(it)) {
54+
if (it != null) {
55+
it.bufferedWriter().use { it.write(read_text_view.text.toString()) }
56+
toast(R.string.file_saved)
57+
hideKeyboard()
58+
} else {
59+
toast(R.string.unknown_error_occurred)
60+
}
61+
}
62+
}
4363
}
4464

4565
private fun checkIntent() {
4666
read_text_view.setTextColor(config.textColor)
4767
val uri = intent.data
4868
val text = if (uri.scheme == "file") {
69+
filePath = uri.path
4970
File(uri.path).readText()
5071
} else {
5172
contentResolver.openInputStream(uri).bufferedReader().use { it.readText() }
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.simplemobiletools.filemanager.dialogs
2+
3+
import android.support.v7.app.AlertDialog
4+
import android.view.LayoutInflater
5+
import android.view.WindowManager
6+
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
7+
import com.simplemobiletools.commons.dialogs.FilePickerDialog
8+
import com.simplemobiletools.commons.extensions.*
9+
import com.simplemobiletools.filemanager.R
10+
import com.simplemobiletools.filemanager.activities.SimpleActivity
11+
import kotlinx.android.synthetic.main.dialog_save_as.view.*
12+
import java.io.File
13+
14+
class SaveAsDialog(val activity: SimpleActivity, var path: String, val callback: (savePath: String) -> Unit) {
15+
16+
init {
17+
if (path.isEmpty()) {
18+
path = "${activity.internalStoragePath}/${System.currentTimeMillis()}.txt"
19+
}
20+
21+
var realPath = File(path).parent.trimEnd('/')
22+
val view = LayoutInflater.from(activity).inflate(R.layout.dialog_save_as, null).apply {
23+
save_as_path.text = activity.humanizePath(realPath)
24+
25+
val fullName = path.getFilenameFromPath()
26+
val dotAt = fullName.lastIndexOf(".")
27+
var name = fullName
28+
29+
if (dotAt > 0) {
30+
name = fullName.substring(0, dotAt)
31+
val extension = fullName.substring(dotAt + 1)
32+
save_as_extension.setText(extension)
33+
}
34+
35+
save_as_name.setText(name)
36+
save_as_path.setOnClickListener {
37+
FilePickerDialog(activity, realPath, false, false, true) {
38+
save_as_path.text = activity.humanizePath(it)
39+
realPath = it
40+
}
41+
}
42+
}
43+
44+
AlertDialog.Builder(activity)
45+
.setPositiveButton(R.string.ok, null)
46+
.setNegativeButton(R.string.cancel, null)
47+
.create().apply {
48+
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
49+
activity.setupDialogStuff(view, this, R.string.save_as)
50+
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
51+
val filename = view.save_as_name.value
52+
val extension = view.save_as_extension.value
53+
54+
if (filename.isEmpty()) {
55+
activity.toast(R.string.filename_cannot_be_empty)
56+
return@setOnClickListener
57+
}
58+
59+
if (extension.isEmpty()) {
60+
activity.toast(R.string.extension_cannot_be_empty)
61+
return@setOnClickListener
62+
}
63+
64+
val newFile = File(realPath, "$filename.$extension")
65+
if (!newFile.name.isAValidFilename()) {
66+
activity.toast(R.string.filename_invalid_characters)
67+
return@setOnClickListener
68+
}
69+
70+
if (newFile.exists()) {
71+
val title = String.format(activity.getString(R.string.file_already_exists_overwrite), newFile.name)
72+
ConfirmationDialog(activity, title) {
73+
callback(newFile.absolutePath)
74+
dismiss()
75+
}
76+
} else {
77+
callback(newFile.absolutePath)
78+
dismiss()
79+
}
80+
})
81+
}
82+
}
83+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:id="@+id/save_as_holder"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
android:padding="@dimen/activity_margin">
9+
10+
<com.simplemobiletools.commons.views.MyTextView
11+
android:id="@+id/save_as_path_label"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:text="@string/path"
15+
android:textSize="@dimen/smaller_text_size"/>
16+
17+
<com.simplemobiletools.commons.views.MyTextView
18+
android:id="@+id/save_as_path"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:layout_marginBottom="@dimen/activity_margin"
22+
android:layout_marginLeft="@dimen/activity_margin"
23+
android:paddingRight="@dimen/small_margin"
24+
android:paddingTop="@dimen/small_margin"/>
25+
26+
<com.simplemobiletools.commons.views.MyEditText
27+
android:id="@+id/save_as_name"
28+
android:layout_width="match_parent"
29+
android:layout_height="wrap_content"
30+
android:layout_marginBottom="@dimen/activity_margin"
31+
android:singleLine="true"
32+
android:textCursorDrawable="@null"/>
33+
34+
<com.simplemobiletools.commons.views.MyTextView
35+
android:id="@+id/save_as_extension_label"
36+
android:layout_width="wrap_content"
37+
android:layout_height="wrap_content"
38+
android:text="@string/extension"/>
39+
40+
<com.simplemobiletools.commons.views.MyEditText
41+
android:id="@+id/save_as_extension"
42+
android:layout_width="match_parent"
43+
android:layout_height="wrap_content"
44+
android:layout_marginBottom="@dimen/activity_margin"
45+
android:singleLine="true"
46+
android:textCursorDrawable="@null"/>
47+
48+
</LinearLayout>

0 commit comments

Comments
 (0)