diff --git a/src/net/micode/notes/model/Note.java b/src/net/micode/notes/model/Note.java index c4e2e62..6706cf6 100644 --- a/src/net/micode/notes/model/Note.java +++ b/src/net/micode/notes/model/Note.java @@ -15,7 +15,6 @@ */ package net.micode.notes.model; - import android.content.ContentProviderOperation; import android.content.ContentProviderResult; import android.content.ContentUris; @@ -34,22 +33,16 @@ import net.micode.notes.data.Notes.TextNote; import java.util.ArrayList; -/** - * 笔记数据管理类,处理笔记的创建、更新和同步 - */ + public class Note { - // 笔记差异值(待更新的笔记内容) private ContentValues mNoteDiffValues; - // 笔记数据 private NoteData mNoteData; - // 日志标签 private static final String TAG = "Note"; - /** - * 创建新笔记并返回ID + * Create a new note id for adding a new note to databases */ public static synchronized long getNewNoteId(Context context, long folderId) { - // 在数据库中创建新笔记 + // Create a new note in the database ContentValues values = new ContentValues(); long createdTime = System.currentTimeMillis(); values.put(NoteColumns.CREATED_DATE, createdTime); @@ -72,68 +65,41 @@ public class Note { return noteId; } - /** - * 构造函数 - */ public Note() { mNoteDiffValues = new ContentValues(); mNoteData = new NoteData(); } - /** - * 设置笔记属性值 - */ public void setNoteValue(String key, String value) { mNoteDiffValues.put(key, value); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); } - /** - * 设置文本笔记数据 - */ public void setTextData(String key, String value) { mNoteData.setTextData(key, value); } - /** - * 设置文本笔记数据ID - */ public void setTextDataId(long id) { mNoteData.setTextDataId(id); } - /** - * 获取文本笔记数据ID - */ public long getTextDataId() { return mNoteData.mTextDataId; } - /** - * 设置通话记录数据ID - */ public void setCallDataId(long id) { mNoteData.setCallDataId(id); } - /** - * 设置通话记录数据 - */ public void setCallData(String key, String value) { mNoteData.setCallData(key, value); } - /** - * 检查笔记是否有本地修改 - */ public boolean isLocalModified() { return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); } - /** - * 将笔记同步到数据库 - */ public boolean syncNote(Context context, long noteId) { if (noteId <= 0) { throw new IllegalArgumentException("Wrong note id:" + noteId); @@ -143,16 +109,19 @@ public class Note { return true; } - // 更新笔记基本信息 + /** + * In theory, once data changed, the note should be updated on {@link NoteColumns#LOCAL_MODIFIED} and + * {@link NoteColumns#MODIFIED_DATE}. For data safety, though update note fails, we also update the + * note data info + */ if (context.getContentResolver().update( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), mNoteDiffValues, null, null) == 0) { Log.e(TAG, "Update note error, should not happen"); - // 即使更新失败也继续处理数据 + // Do not return, fall through } mNoteDiffValues.clear(); - // 更新笔记详细数据 if (mNoteData.isLocalModified() && (mNoteData.pushIntoContentResolver(context, noteId) == null)) { return false; @@ -161,24 +130,17 @@ public class Note { return true; } - /** - * 笔记数据内部类,处理笔记详细内容 - */ private class NoteData { - // 文本数据ID private long mTextDataId; - // 文本数据值 + private ContentValues mTextDataValues; - // 通话记录数据ID + private long mCallDataId; - // 通话记录数据值 + private ContentValues mCallDataValues; - // 日志标签 + private static final String TAG = "NoteData"; - /** - * 构造函数 - */ public NoteData() { mTextDataValues = new ContentValues(); mCallDataValues = new ContentValues(); @@ -186,16 +148,10 @@ public class Note { mCallDataId = 0; } - /** - * 检查是否有本地修改 - */ boolean isLocalModified() { return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; } - /** - * 设置文本数据ID - */ void setTextDataId(long id) { if(id <= 0) { throw new IllegalArgumentException("Text data id should larger than 0"); @@ -203,9 +159,6 @@ public class Note { mTextDataId = id; } - /** - * 设置通话记录数据ID - */ void setCallDataId(long id) { if (id <= 0) { throw new IllegalArgumentException("Call data id should larger than 0"); @@ -213,29 +166,22 @@ public class Note { mCallDataId = id; } - /** - * 设置通话记录数据 - */ void setCallData(String key, String value) { mCallDataValues.put(key, value); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); } - /** - * 设置文本数据 - */ void setTextData(String key, String value) { mTextDataValues.put(key, value); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); } - /** - * 将数据推送到内容提供者 - */ Uri pushIntoContentResolver(Context context, long noteId) { - // 安全检查 + /** + * Check for safety + */ if (noteId <= 0) { throw new IllegalArgumentException("Wrong note id:" + noteId); } @@ -243,11 +189,9 @@ public class Note { ArrayList operationList = new ArrayList(); ContentProviderOperation.Builder builder = null; - // 处理文本数据 if(mTextDataValues.size() > 0) { mTextDataValues.put(DataColumns.NOTE_ID, noteId); if (mTextDataId == 0) { - // 插入新文本数据 mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, mTextDataValues); @@ -259,7 +203,6 @@ public class Note { return null; } } else { - // 更新现有文本数据 builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mTextDataId)); builder.withValues(mTextDataValues); @@ -268,11 +211,9 @@ public class Note { mTextDataValues.clear(); } - // 处理通话记录数据 if(mCallDataValues.size() > 0) { mCallDataValues.put(DataColumns.NOTE_ID, noteId); if (mCallDataId == 0) { - // 插入新通话记录数据 mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, mCallDataValues); @@ -284,7 +225,6 @@ public class Note { return null; } } else { - // 更新现有通话记录数据 builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mCallDataId)); builder.withValues(mCallDataValues); @@ -293,7 +233,6 @@ public class Note { mCallDataValues.clear(); } - // 批量执行操作 if (operationList.size() > 0) { try { ContentProviderResult[] results = context.getContentResolver().applyBatch( @@ -311,4 +250,4 @@ public class Note { return null; } } -} \ No newline at end of file +}