allow syncing note with content uris

This commit is contained in:
tibbi 2021-03-21 11:50:02 +01:00
parent 386767bd6f
commit 4f1bb56d00
5 changed files with 27 additions and 16 deletions

View File

@ -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)
}
}

View File

@ -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)

View File

@ -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()

View File

@ -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)
}
}
}

View File

@ -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 {