From 4f1bb56d00f9f72e0c436926d9847a2f0929de74 Mon Sep 17 00:00:00 2001
From: tibbi <tibor@kaputa.sk>
Date: Sun, 21 Mar 2021 11:50:02 +0100
Subject: [PATCH] allow syncing note with content uris

---
 .../notes/pro/activities/MainActivity.kt           | 10 ++++++----
 .../notes/pro/adapters/WidgetAdapter.kt            |  2 +-
 .../notes/pro/fragments/TextFragment.kt            |  8 ++++----
 .../notes/pro/helpers/NotesHelper.kt               |  9 ++++++---
 .../com/simplemobiletools/notes/pro/models/Note.kt | 14 ++++++++++----
 5 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt
index 683beb0a..5c87b998 100644
--- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt
@@ -239,7 +239,7 @@ class MainActivity : SimpleActivity() {
         if (requestCode == PICK_OPEN_FILE_INTENT && resultCode == RESULT_OK && resultData != null && resultData.data != null) {
             importUri(resultData.data!!)
         } else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null && mNotes.isNotEmpty()) {
-            tryExportNoteValueToFile(resultData.dataString!!, getCurrentNoteValue(), true)
+            showExportFilePickUpdateDialog(resultData.dataString!!, getCurrentNoteValue())
         }
     }
 
@@ -752,7 +752,7 @@ class MainActivity : SimpleActivity() {
                         if (!filename.isAValidFilename()) {
                             toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
                         } else {
-                            val noteStoredValue = note.getNoteStoredValue() ?: ""
+                            val noteStoredValue = note.getNoteStoredValue(this) ?: ""
                             tryExportNoteValueToFile(file.absolutePath, note.value, false) {
                                 if (syncFile) {
                                     note.path = file.absolutePath
@@ -786,7 +786,7 @@ class MainActivity : SimpleActivity() {
 
     fun tryExportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
         if (path.startsWith("content://")) {
-            exportNoteValueToUri(Uri.parse(path), content)
+            exportNoteValueToUri(Uri.parse(path), content, callback)
         } else {
             handlePermission(PERMISSION_WRITE_STORAGE) {
                 if (it) {
@@ -841,15 +841,17 @@ class MainActivity : SimpleActivity() {
         }
     }
 
-    private fun exportNoteValueToUri(uri: Uri, content: String) {
+    private fun exportNoteValueToUri(uri: Uri, content: String, callback: ((success: Boolean) -> Unit)? = null) {
         try {
             val outputStream = contentResolver.openOutputStream(uri)
             outputStream!!.bufferedWriter().use { out ->
                 out.write(content)
             }
             noteExportedSuccessfully(mCurrentNote.title)
+            callback?.invoke(true)
         } catch (e: Exception) {
             showErrorToast(e)
+            callback?.invoke(false)
         }
     }
 
diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/WidgetAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/WidgetAdapter.kt
index 550422ee..0c0064d7 100644
--- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/WidgetAdapter.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/WidgetAdapter.kt
@@ -56,7 +56,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
             }
         } else {
             remoteView = RemoteViews(context.packageName, R.layout.widget_text_layout).apply {
-                val noteText = note!!.getNoteStoredValue() ?: ""
+                val noteText = note!!.getNoteStoredValue(context) ?: ""
                 for (id in textIds) {
                     setText(id, noteText)
                     setTextColor(id, widgetTextColor)
diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt
index 34cbb14e..efa161f4 100644
--- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt
@@ -119,7 +119,7 @@ class TextFragment : NoteFragment() {
         view.text_note_view.apply {
             typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT
 
-            val fileContents = note!!.getNoteStoredValue()
+            val fileContents = note!!.getNoteStoredValue(context)
             if (fileContents == null) {
                 (activity as MainActivity).deleteNote(false, note!!)
                 return
@@ -193,7 +193,7 @@ class TextFragment : NoteFragment() {
             return
         }
 
-        if (note!!.path.isNotEmpty() && !File(note!!.path).exists()) {
+        if (note!!.path.isNotEmpty() && !note!!.path.startsWith("content://") && !File(note!!.path).exists()) {
             return
         }
 
@@ -202,7 +202,7 @@ class TextFragment : NoteFragment() {
         }
 
         val newText = getCurrentNoteViewText()
-        val oldText = note!!.getNoteStoredValue()
+        val oldText = note!!.getNoteStoredValue(context!!)
         if (newText != null && (newText != oldText || force)) {
             note!!.value = newText
             saveNoteValue(note!!)
@@ -210,7 +210,7 @@ class TextFragment : NoteFragment() {
         }
     }
 
-    fun hasUnsavedChanges() = getCurrentNoteViewText() != note!!.getNoteStoredValue()
+    fun hasUnsavedChanges() = getCurrentNoteViewText() != note!!.getNoteStoredValue(context!!)
 
     fun focusEditText() {
         view.text_note_view.requestFocus()
diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt
index aa280bf4..bd200bbe 100644
--- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt
@@ -1,6 +1,7 @@
 package com.simplemobiletools.notes.pro.helpers
 
 import android.content.Context
+import android.net.Uri
 import android.os.Handler
 import android.os.Looper
 import com.simplemobiletools.commons.helpers.ensureBackgroundThread
@@ -22,9 +23,11 @@ class NotesHelper(val context: Context) {
             val notes = context.notesDB.getNotes() as ArrayList<Note>
             val notesToDelete = ArrayList<Note>(notes.size)
             notes.forEach {
-                if (it.path.isNotEmpty() && !File(it.path).exists()) {
-                    context.notesDB.deleteNote(it)
-                    notesToDelete.add(it)
+                if (it.path.isNotEmpty()) {
+                    if (!it.path.startsWith("content://") && !File(it.path).exists()) {
+                        context.notesDB.deleteNote(it)
+                        notesToDelete.add(it)
+                    }
                 }
             }
 
diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt
index 8e6964b9..70c55a80 100644
--- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt
+++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt
@@ -1,11 +1,12 @@
 package com.simplemobiletools.notes.pro.models
 
+import android.content.Context
+import android.net.Uri
 import androidx.room.ColumnInfo
 import androidx.room.Entity
 import androidx.room.Index
 import androidx.room.PrimaryKey
 import java.io.File
-import java.io.FileNotFoundException
 
 @Entity(tableName = "notes", indices = [(Index(value = ["id"], unique = true))])
 data class Note(
@@ -15,11 +16,16 @@ data class Note(
     @ColumnInfo(name = "type") var type: Int,
     @ColumnInfo(name = "path") var path: String = "") {
 
-    fun getNoteStoredValue(): String? {
+    fun getNoteStoredValue(context: Context): String? {
         return if (path.isNotEmpty()) {
             try {
-                File(path).readText()
-            } catch (e: FileNotFoundException) {
+                if (path.startsWith("content://")) {
+                    val inputStream = context.contentResolver.openInputStream(Uri.parse(path))
+                    inputStream?.bufferedReader().use { it!!.readText() }
+                } else {
+                    File(path).readText()
+                }
+            } catch (e: Exception) {
                 null
             }
         } else {