Ejemplo n.º 1
0
        // 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;
        }