Skip to content

Commit 4f1bb56

Browse files
committed
allow syncing note with content uris
1 parent 386767b commit 4f1bb56

File tree

5 files changed

+27
-16
lines changed

5 files changed

+27
-16
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ class MainActivity : SimpleActivity() {
239239
if (requestCode == PICK_OPEN_FILE_INTENT && resultCode == RESULT_OK && resultData != null && resultData.data != null) {
240240
importUri(resultData.data!!)
241241
} else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null && mNotes.isNotEmpty()) {
242-
tryExportNoteValueToFile(resultData.dataString!!, getCurrentNoteValue(), true)
242+
showExportFilePickUpdateDialog(resultData.dataString!!, getCurrentNoteValue())
243243
}
244244
}
245245

@@ -752,7 +752,7 @@ class MainActivity : SimpleActivity() {
752752
if (!filename.isAValidFilename()) {
753753
toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
754754
} else {
755-
val noteStoredValue = note.getNoteStoredValue() ?: ""
755+
val noteStoredValue = note.getNoteStoredValue(this) ?: ""
756756
tryExportNoteValueToFile(file.absolutePath, note.value, false) {
757757
if (syncFile) {
758758
note.path = file.absolutePath
@@ -786,7 +786,7 @@ class MainActivity : SimpleActivity() {
786786

787787
fun tryExportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
788788
if (path.startsWith("content://")) {
789-
exportNoteValueToUri(Uri.parse(path), content)
789+
exportNoteValueToUri(Uri.parse(path), content, callback)
790790
} else {
791791
handlePermission(PERMISSION_WRITE_STORAGE) {
792792
if (it) {
@@ -841,15 +841,17 @@ class MainActivity : SimpleActivity() {
841841
}
842842
}
843843

844-
private fun exportNoteValueToUri(uri: Uri, content: String) {
844+
private fun exportNoteValueToUri(uri: Uri, content: String, callback: ((success: Boolean) -> Unit)? = null) {
845845
try {
846846
val outputStream = contentResolver.openOutputStream(uri)
847847
outputStream!!.bufferedWriter().use { out ->
848848
out.write(content)
849849
}
850850
noteExportedSuccessfully(mCurrentNote.title)
851+
callback?.invoke(true)
851852
} catch (e: Exception) {
852853
showErrorToast(e)
854+
callback?.invoke(false)
853855
}
854856
}
855857

app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/WidgetAdapter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
5656
}
5757
} else {
5858
remoteView = RemoteViews(context.packageName, R.layout.widget_text_layout).apply {
59-
val noteText = note!!.getNoteStoredValue() ?: ""
59+
val noteText = note!!.getNoteStoredValue(context) ?: ""
6060
for (id in textIds) {
6161
setText(id, noteText)
6262
setTextColor(id, widgetTextColor)

app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/TextFragment.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class TextFragment : NoteFragment() {
119119
view.text_note_view.apply {
120120
typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT
121121

122-
val fileContents = note!!.getNoteStoredValue()
122+
val fileContents = note!!.getNoteStoredValue(context)
123123
if (fileContents == null) {
124124
(activity as MainActivity).deleteNote(false, note!!)
125125
return
@@ -193,7 +193,7 @@ class TextFragment : NoteFragment() {
193193
return
194194
}
195195

196-
if (note!!.path.isNotEmpty() && !File(note!!.path).exists()) {
196+
if (note!!.path.isNotEmpty() && !note!!.path.startsWith("content://") && !File(note!!.path).exists()) {
197197
return
198198
}
199199

@@ -202,15 +202,15 @@ class TextFragment : NoteFragment() {
202202
}
203203

204204
val newText = getCurrentNoteViewText()
205-
val oldText = note!!.getNoteStoredValue()
205+
val oldText = note!!.getNoteStoredValue(context!!)
206206
if (newText != null && (newText != oldText || force)) {
207207
note!!.value = newText
208208
saveNoteValue(note!!)
209209
context!!.updateWidgets()
210210
}
211211
}
212212

213-
fun hasUnsavedChanges() = getCurrentNoteViewText() != note!!.getNoteStoredValue()
213+
fun hasUnsavedChanges() = getCurrentNoteViewText() != note!!.getNoteStoredValue(context!!)
214214

215215
fun focusEditText() {
216216
view.text_note_view.requestFocus()

app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt

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

33
import android.content.Context
4+
import android.net.Uri
45
import android.os.Handler
56
import android.os.Looper
67
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
@@ -22,9 +23,11 @@ class NotesHelper(val context: Context) {
2223
val notes = context.notesDB.getNotes() as ArrayList<Note>
2324
val notesToDelete = ArrayList<Note>(notes.size)
2425
notes.forEach {
25-
if (it.path.isNotEmpty() && !File(it.path).exists()) {
26-
context.notesDB.deleteNote(it)
27-
notesToDelete.add(it)
26+
if (it.path.isNotEmpty()) {
27+
if (!it.path.startsWith("content://") && !File(it.path).exists()) {
28+
context.notesDB.deleteNote(it)
29+
notesToDelete.add(it)
30+
}
2831
}
2932
}
3033

app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.simplemobiletools.notes.pro.models
22

3+
import android.content.Context
4+
import android.net.Uri
35
import androidx.room.ColumnInfo
46
import androidx.room.Entity
57
import androidx.room.Index
68
import androidx.room.PrimaryKey
79
import java.io.File
8-
import java.io.FileNotFoundException
910

1011
@Entity(tableName = "notes", indices = [(Index(value = ["id"], unique = true))])
1112
data class Note(
@@ -15,11 +16,16 @@ data class Note(
1516
@ColumnInfo(name = "type") var type: Int,
1617
@ColumnInfo(name = "path") var path: String = "") {
1718

18-
fun getNoteStoredValue(): String? {
19+
fun getNoteStoredValue(context: Context): String? {
1920
return if (path.isNotEmpty()) {
2021
try {
21-
File(path).readText()
22-
} catch (e: FileNotFoundException) {
22+
if (path.startsWith("content://")) {
23+
val inputStream = context.contentResolver.openInputStream(Uri.parse(path))
24+
inputStream?.bufferedReader().use { it!!.readText() }
25+
} else {
26+
File(path).readText()
27+
}
28+
} catch (e: Exception) {
2329
null
2430
}
2531
} else {

0 commit comments

Comments
 (0)