From bd510a95ffd9944b53307011810ade144990d769 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=80=E6=9D=A1=E9=B1=BC=E5=92=8Cxiao?= <15185717938@163.com> Date: Wed, 14 May 2025 19:52:40 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/net/micode/notes/model/Note.java | 95 +++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 17 deletions(-) diff --git a/src/net/micode/notes/model/Note.java b/src/net/micode/notes/model/Note.java index 6706cf6..c4e2e62 100644 --- a/src/net/micode/notes/model/Note.java +++ b/src/net/micode/notes/model/Note.java @@ -15,6 +15,7 @@ */ package net.micode.notes.model; + import android.content.ContentProviderOperation; import android.content.ContentProviderResult; import android.content.ContentUris; @@ -33,16 +34,22 @@ 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"; + /** - * Create a new note id for adding a new note to databases + * 创建新笔记并返回ID */ 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); @@ -65,41 +72,68 @@ 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); @@ -109,19 +143,16 @@ 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; @@ -130,17 +161,24 @@ 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(); @@ -148,10 +186,16 @@ 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"); @@ -159,6 +203,9 @@ public class Note { mTextDataId = id; } + /** + * 设置通话记录数据ID + */ void setCallDataId(long id) { if (id <= 0) { throw new IllegalArgumentException("Call data id should larger than 0"); @@ -166,22 +213,29 @@ 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); } @@ -189,9 +243,11 @@ 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); @@ -203,6 +259,7 @@ public class Note { return null; } } else { + // 更新现有文本数据 builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mTextDataId)); builder.withValues(mTextDataValues); @@ -211,9 +268,11 @@ 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); @@ -225,6 +284,7 @@ public class Note { return null; } } else { + // 更新现有通话记录数据 builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mCallDataId)); builder.withValues(mCallDataValues); @@ -233,6 +293,7 @@ public class Note { mCallDataValues.clear(); } + // 批量执行操作 if (operationList.size() > 0) { try { ContentProviderResult[] results = context.getContentResolver().applyBatch( @@ -250,4 +311,4 @@ public class Note { return null; } } -} +} \ No newline at end of file