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) { if (requestCode == PICK_OPEN_FILE_INTENT && resultCode == RESULT_OK && resultData != null && resultData.data != null) {
importUri(resultData.data!!) importUri(resultData.data!!)
} else if (requestCode == PICK_EXPORT_FILE_INTENT && resultCode == Activity.RESULT_OK && resultData != null && resultData.data != null && mNotes.isNotEmpty()) { } 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()) { if (!filename.isAValidFilename()) {
toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename))) toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename)))
} else { } else {
val noteStoredValue = note.getNoteStoredValue() ?: "" val noteStoredValue = note.getNoteStoredValue(this) ?: ""
tryExportNoteValueToFile(file.absolutePath, note.value, false) { tryExportNoteValueToFile(file.absolutePath, note.value, false) {
if (syncFile) { if (syncFile) {
note.path = file.absolutePath note.path = file.absolutePath
@ -786,7 +786,7 @@ class MainActivity : SimpleActivity() {
fun tryExportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) { fun tryExportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
if (path.startsWith("content://")) { if (path.startsWith("content://")) {
exportNoteValueToUri(Uri.parse(path), content) exportNoteValueToUri(Uri.parse(path), content, callback)
} else { } else {
handlePermission(PERMISSION_WRITE_STORAGE) { handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) { 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 { try {
val outputStream = contentResolver.openOutputStream(uri) val outputStream = contentResolver.openOutputStream(uri)
outputStream!!.bufferedWriter().use { out -> outputStream!!.bufferedWriter().use { out ->
out.write(content) out.write(content)
} }
noteExportedSuccessfully(mCurrentNote.title) noteExportedSuccessfully(mCurrentNote.title)
callback?.invoke(true)
} catch (e: Exception) { } catch (e: Exception) {
showErrorToast(e) showErrorToast(e)
callback?.invoke(false)
} }
} }

View File

@ -56,7 +56,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
} }
} else { } else {
remoteView = RemoteViews(context.packageName, R.layout.widget_text_layout).apply { remoteView = RemoteViews(context.packageName, R.layout.widget_text_layout).apply {
val noteText = note!!.getNoteStoredValue() ?: "" val noteText = note!!.getNoteStoredValue(context) ?: ""
for (id in textIds) { for (id in textIds) {
setText(id, noteText) setText(id, noteText)
setTextColor(id, widgetTextColor) setTextColor(id, widgetTextColor)

View File

@ -119,7 +119,7 @@ class TextFragment : NoteFragment() {
view.text_note_view.apply { view.text_note_view.apply {
typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT
val fileContents = note!!.getNoteStoredValue() val fileContents = note!!.getNoteStoredValue(context)
if (fileContents == null) { if (fileContents == null) {
(activity as MainActivity).deleteNote(false, note!!) (activity as MainActivity).deleteNote(false, note!!)
return return
@ -193,7 +193,7 @@ class TextFragment : NoteFragment() {
return return
} }
if (note!!.path.isNotEmpty() && !File(note!!.path).exists()) { if (note!!.path.isNotEmpty() && !note!!.path.startsWith("content://") && !File(note!!.path).exists()) {
return return
} }
@ -202,7 +202,7 @@ class TextFragment : NoteFragment() {
} }
val newText = getCurrentNoteViewText() val newText = getCurrentNoteViewText()
val oldText = note!!.getNoteStoredValue() val oldText = note!!.getNoteStoredValue(context!!)
if (newText != null && (newText != oldText || force)) { if (newText != null && (newText != oldText || force)) {
note!!.value = newText note!!.value = newText
saveNoteValue(note!!) saveNoteValue(note!!)
@ -210,7 +210,7 @@ class TextFragment : NoteFragment() {
} }
} }
fun hasUnsavedChanges() = getCurrentNoteViewText() != note!!.getNoteStoredValue() fun hasUnsavedChanges() = getCurrentNoteViewText() != note!!.getNoteStoredValue(context!!)
fun focusEditText() { fun focusEditText() {
view.text_note_view.requestFocus() view.text_note_view.requestFocus()

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.notes.pro.helpers package com.simplemobiletools.notes.pro.helpers
import android.content.Context import android.content.Context
import android.net.Uri
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.helpers.ensureBackgroundThread
@ -22,9 +23,11 @@ class NotesHelper(val context: Context) {
val notes = context.notesDB.getNotes() as ArrayList<Note> val notes = context.notesDB.getNotes() as ArrayList<Note>
val notesToDelete = ArrayList<Note>(notes.size) val notesToDelete = ArrayList<Note>(notes.size)
notes.forEach { notes.forEach {
if (it.path.isNotEmpty() && !File(it.path).exists()) { if (it.path.isNotEmpty()) {
context.notesDB.deleteNote(it) if (!it.path.startsWith("content://") && !File(it.path).exists()) {
notesToDelete.add(it) context.notesDB.deleteNote(it)
notesToDelete.add(it)
}
} }
} }

View File

@ -1,11 +1,12 @@
package com.simplemobiletools.notes.pro.models package com.simplemobiletools.notes.pro.models
import android.content.Context
import android.net.Uri
import androidx.room.ColumnInfo import androidx.room.ColumnInfo
import androidx.room.Entity import androidx.room.Entity
import androidx.room.Index import androidx.room.Index
import androidx.room.PrimaryKey import androidx.room.PrimaryKey
import java.io.File import java.io.File
import java.io.FileNotFoundException
@Entity(tableName = "notes", indices = [(Index(value = ["id"], unique = true))]) @Entity(tableName = "notes", indices = [(Index(value = ["id"], unique = true))])
data class Note( data class Note(
@ -15,11 +16,16 @@ data class Note(
@ColumnInfo(name = "type") var type: Int, @ColumnInfo(name = "type") var type: Int,
@ColumnInfo(name = "path") var path: String = "") { @ColumnInfo(name = "path") var path: String = "") {
fun getNoteStoredValue(): String? { fun getNoteStoredValue(context: Context): String? {
return if (path.isNotEmpty()) { return if (path.isNotEmpty()) {
try { try {
File(path).readText() if (path.startsWith("content://")) {
} catch (e: FileNotFoundException) { val inputStream = context.contentResolver.openInputStream(Uri.parse(path))
inputStream?.bufferedReader().use { it!!.readText() }
} else {
File(path).readText()
}
} catch (e: Exception) {
null null
} }
} else { } else {