Skip to content

Commit 6d06cf0

Browse files
committed
add handling for one time file exporting with scoped storage
1 parent b956dd0 commit 6d06cf0

File tree

1 file changed

+34
-6
lines changed
  • app/src/main/kotlin/com/simplemobiletools/notes/pro/activities

1 file changed

+34
-6
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.simplemobiletools.notes.pro.activities
22

3+
import android.app.Activity
34
import android.content.Intent
45
import android.net.Uri
56
import android.os.Bundle
@@ -42,7 +43,9 @@ import java.nio.charset.Charset
4243
class MainActivity : SimpleActivity() {
4344
private val EXPORT_FILE_SYNC = 1
4445
private val EXPORT_FILE_NO_SYNC = 2
46+
4547
private val PICK_OPEN_FILE_INTENT = 1
48+
private val PICK_EXPORT_FILE_INTENT = 2
4649

4750
private lateinit var mCurrentNote: Note
4851
private var mNotes = ArrayList<Note>()
@@ -226,6 +229,8 @@ class MainActivity : SimpleActivity() {
226229
super.onActivityResult(requestCode, resultCode, resultData)
227230
if (requestCode == PICK_OPEN_FILE_INTENT && resultCode == RESULT_OK && resultData != null && resultData.data != null) {
228231
importUri(resultData.data!!)
232+
} else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null) {
233+
tryExportNoteValueToFile(resultData.dataString!!, getCurrentNoteText() ?: "", true)
229234
}
230235
}
231236

@@ -651,9 +656,15 @@ class MainActivity : SimpleActivity() {
651656
}
652657

653658
private fun tryExportAsFile() {
654-
handlePermission(PERMISSION_WRITE_STORAGE) {
655-
if (it) {
656-
exportAsFile()
659+
if (hasPermission(PERMISSION_WRITE_STORAGE)) {
660+
exportAsFile()
661+
} else {
662+
Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
663+
type = "text/*"
664+
putExtra(Intent.EXTRA_TITLE, "${mCurrentNote.title.removeSuffix(".txt")}.txt")
665+
addCategory(Intent.CATEGORY_OPENABLE)
666+
667+
startActivityForResult(this, PICK_EXPORT_FILE_INTENT)
657668
}
658669
}
659670
}
@@ -751,9 +762,13 @@ class MainActivity : SimpleActivity() {
751762
}
752763

753764
fun tryExportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
754-
handlePermission(PERMISSION_WRITE_STORAGE) {
755-
if (it) {
756-
exportNoteValueToFile(path, content, showSuccessToasts, callback)
765+
if (path.startsWith("content://")) {
766+
exportNoteValueToUri(Uri.parse(path), content)
767+
} else {
768+
handlePermission(PERMISSION_WRITE_STORAGE) {
769+
if (it) {
770+
exportNoteValueToFile(path, content, showSuccessToasts, callback)
771+
}
757772
}
758773
}
759774
}
@@ -791,6 +806,7 @@ class MainActivity : SimpleActivity() {
791806
file.printWriter().use { out ->
792807
out.write(content)
793808
}
809+
794810
if (showSuccessToasts) {
795811
noteExportedSuccessfully(path.getFilenameFromPath())
796812
}
@@ -802,6 +818,18 @@ class MainActivity : SimpleActivity() {
802818
}
803819
}
804820

821+
private fun exportNoteValueToUri(uri: Uri, content: String) {
822+
try {
823+
val outputStream = contentResolver.openOutputStream(uri)
824+
outputStream!!.bufferedWriter().use { out ->
825+
out.write(content)
826+
}
827+
noteExportedSuccessfully(mCurrentNote.title)
828+
} catch (e: Exception) {
829+
showErrorToast(e)
830+
}
831+
}
832+
805833
private fun noteExportedSuccessfully(title: String) {
806834
val message = String.format(getString(R.string.note_exported_successfully), title)
807835
toast(message)

0 commit comments

Comments
 (0)