// auto backup function on save public static void backupNote(Note note) { doPushNote(note); }
// actually pushes a note to sdcard, with optional subdirectory (e.g. backup) private static int doPushNote(Note note) { Note rnote = new Note(); try { File path = new File(Tomdroid.NOTES_PATH); if (!path.Exists()) path.Mkdir(); TLog.i(TAG, "Path {0} Exists: {1}", path, path.Exists()); // Check a second time, if not the most likely cause is the volume doesn't exist if(!path.Exists()) { TLog.w(TAG, "Couldn't create {0}", path); return NO_SD_CARD; } path = new File(Tomdroid.NOTES_PATH + "/"+note.getGuid() + ".note"); note.createDate = note.getLastChangeDate().Format3339(false); note.cursorPos = 0; note.width = 0; note.height = 0; note.X = -1; note.Y = -1; if (path.Exists()) { // update existing note // Try reading the file first string contents = ""; try { char[] buffer = new char[0x1000]; contents = readFile(path,buffer); } catch (IOException e) { e.PrintStackTrace(); TLog.w(TAG, "Something went wrong trying to read the note"); return PARSING_FAILED; } try { // Parsing // XML // Get a SAXParser from the SAXPArserFactory SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParser sp = spf.newSAXParser(); // Get the XMLReader of the SAXParser we created XMLReader xr = sp.getXMLReader(); // Create a new ContentHandler, send it this note to fill and apply it to the XML-Reader NoteHandler xmlHandler = new NoteHandler(rnote); xr.setContentHandler(xmlHandler); // Create the proper input source StringReader sr = new StringReader(contents); InputSource inputSource = new InputSource(sr); TLog.d(TAG, "parsing note. filename: {0}", path.Name()); xr.parse(inputSource); // TODO wrap and throw a new exception here } catch (Exception e) { e.PrintStackTrace(); if(e as TimeFormatException) TLog.e(TAG, "Problem parsing the note's date and time"); return PARSING_FAILED; } note.createDate = rnote.createDate; note.cursorPos = rnote.cursorPos; note.width = rnote.width; note.height = rnote.height; note.X = rnote.X; note.Y = rnote.Y; note.setTags(rnote.getTags()); } string xmlOutput = note.getXmlFileString(); path.CreateNewFile(); FileOutputStream fOut = new FileOutputStream(path); OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); myOutWriter.Append(xmlOutput); myOutWriter.Close(); fOut.Close(); } catch (Exception e) { TLog.e(TAG, "push to sd card didn't work"); return NOTE_PUSH_ERROR; } return NOTE_PUSHED; }
// this function is a shell to allow backup function to push as well but send a different message... may not be necessary any more... private void pushNote(Note note) { TLog.v(TAG, "pushing note to sdcard"); int message = doPushNote(note); SendMessage(message); }
public void addPushable(Note note) { this.pushableNotes.Add(note); }
/** * Insert last note in the content provider. * * @param note The note to insert. */ protected void insertNote(Note note) { NoteManager.putNote(this.activity, note); SendMessage(INCREMENT_PROGRESS ); }
public NoteHandler(Note note) { this.note = note; }
public void addDeleteable(Note note) { this.deleteableNotes.Add(note); }
// this function removes a "deleted" tag public static void undeleteNote(Activity activity, Note note) { note.removeTag("system:deleted"); Time now = new Time(); now.SetToNow(); note.setLastChangeDate(now); putNote(activity,note); }
// puts a note in the content provider // return uri public static Uri putNote(Activity activity, Note note) { // verify if the note is already in the content provider // TODO make the Query prettier (use querybuilder) Uri notes = Tomdroid.CONTENT_URI; string[] whereArgs = new string[1]; whereArgs[0] = note.getGuid(); // The note identifier is the guid ContentResolver cr = activity.ContentResolver; ICursor managedCursor = cr.Query(notes, LIST_PROJECTION, Note.GUID + "= ?", whereArgs, null); activity.StartManagingCursor(managedCursor); string title = note.getTitle(); string xmlContent = note.getXmlContent(); string plainContent = stringConverter.encode(Html.FromHtml(title + "\n" + xmlContent).ToString()); // Preparing the values to be either inserted or updated // depending on the result of the previous Query ContentValues values = new ContentValues(); values.Put(Note.TITLE, title); values.Put(Note.FILE, note.getFileName()); values.Put(Note.GUID, note.getGuid().ToString()); // Notice that we store the date in UTC because sqlite doesn't handle RFC3339 timezone information values.Put(Note.MODIFIED_DATE, note.getLastChangeDate().Format3339(false)); values.Put(Note.NOTE_CONTENT, xmlContent); values.Put(Note.NOTE_CONTENT_PLAIN, plainContent); values.Put(Note.TAGS, note.getTags()); Uri uri = null; if (managedCursor == null || managedCursor.Count == 0) { // This note is not in the database yet we need to insert it TLog.v(TAG, "A new note has been detected (not yet in db)"); uri = cr.Insert(Tomdroid.CONTENT_URI, values); TLog.v(TAG, "Note inserted in content provider. ID: {0} TITLE:{1} GUID:{2}", uri, note.getTitle(), note.getGuid()); } else { TLog.v(TAG, "A local note has been detected (already in db)"); cr.Update(Tomdroid.CONTENT_URI, values, Note.GUID+" = ?", whereArgs); uri = Uri.Parse(Tomdroid.CONTENT_URI+"/"+getNoteIdByGUID(activity, note.getGuid())); TLog.v(TAG, "Note updated in content provider: TITLE:{0} GUID:{1} TAGS:{2}", note.getTitle(), note.getGuid(), note.getTags()); } managedCursor.Close(); note = getNote(activity, uri); return uri; }
// gets a note from the content provider, based on guid public static Note getNoteByGuid(Activity activity, string guid) { Uri notes = Tomdroid.CONTENT_URI; string[] whereArgs = new string[1]; whereArgs[0] = guid; // The note identifier is the guid ContentResolver cr = activity.ContentResolver; ICursor cursor = cr.Query(notes, FULL_PROJECTION, Note.GUID + "= ?", whereArgs, null); activity.StartManagingCursor(cursor); if (cursor == null || cursor.Count == 0) { cursor.Close(); return null; } else { cursor.MoveToFirst(); string noteContent = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.NOTE_CONTENT)); string noteTitle = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.TITLE)); string noteChangeDate = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.MODIFIED_DATE)); string noteTags = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.TAGS)); string noteGUID = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.GUID)); int noteDbid = cursor.GetInt(cursor.GetColumnIndexOrThrow(Note.ID)); Note note = new Note(); note.setTitle(noteTitle); note.setXmlContent(stripTitleFromContent(noteContent, noteTitle)); note.setLastChangeDate(noteChangeDate); note.addTag(noteTags); note.setGuid(noteGUID); note.setDbId(noteDbid); cursor.Close(); return note; } }
// gets a note from the content provider public static Note getNote(Activity activity, Uri uri) { Note note = null; // can we find a matching note? ICursor cursor = activity.ManagedQuery(uri, FULL_PROJECTION, null, null, null); // cursor must not be null and must return more than 0 entry if (!(cursor == null || cursor.Count == 0)) { // create the note from the cursor cursor.MoveToFirst(); string noteContent = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.NOTE_CONTENT)); string noteTitle = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.TITLE)); string noteChangeDate = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.MODIFIED_DATE)); string noteTags = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.TAGS)); string noteGUID = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.GUID)); int noteDbid = cursor.GetInt(cursor.GetColumnIndexOrThrow(Note.ID)); note = new Note(); note.setTitle(noteTitle); note.setXmlContent(stripTitleFromContent(noteContent, noteTitle)); note.setLastChangeDate(noteChangeDate); note.setTags(noteTags); note.setGuid(noteGUID); note.setDbId(noteDbid); } cursor.Close(); return note; }
// this function gets all non-deleted notes as notes in an array public static Note[] getAllNotesAsNotes(Activity activity, bool includeNotebookTemplates) { Uri uri = Tomdroid.CONTENT_URI; string where = "("+Note.TAGS + " NOT LIKE '%" + "system:deleted" + "%')"; string orderBy; if (!includeNotebookTemplates) { where += " AND (" + Note.TAGS + " NOT LIKE '%" + "system:template" + "%')"; } orderBy = Note.MODIFIED_DATE + " DESC"; ICursor cursor = activity.ManagedQuery(uri, FULL_PROJECTION, where, null, orderBy); if (cursor == null || cursor.Count == 0) { TLog.d(TAG, "no notes in cursor"); return null; } TLog.d(TAG, "{0} notes in cursor",cursor.Count); Note[] notes = new Note[cursor.Count]; cursor.MoveToFirst(); int key = 0; while(!cursor.IsAfterLast) { string noteContent = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.NOTE_CONTENT)); string noteTitle = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.TITLE)); string noteChangeDate = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.MODIFIED_DATE)); string noteTags = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.TAGS)); string noteGUID = cursor.GetString(cursor.GetColumnIndexOrThrow(Note.GUID)); int noteDbid = cursor.GetInt(cursor.GetColumnIndexOrThrow(Note.ID)); Note note = new Note(); note.setTitle(noteTitle); note.setXmlContent(stripTitleFromContent(noteContent, noteTitle)); note.setLastChangeDate(noteChangeDate); note.addTag(noteTags); note.setGuid(noteGUID); note.setDbId(noteDbid); notes[key++] = note; cursor.MoveToNext(); } cursor.Close(); return notes; }