From d8fe0ecde68b729835a2173d99ad5ac3f31567a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=BF=8A=E5=BD=A6?= <1101703918@qq.com> Date: Thu, 17 Apr 2025 15:41:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B3=A8=E9=87=8Asrc/main/java/net/micode/note?= =?UTF-8?q?s/gtask/data/SqlNote.java?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/micode/notes/gtask/data/SqlNote.java | 77 ++++++++++++------- 1 file changed, 50 insertions(+), 27 deletions(-) diff --git a/src/main/java/net/micode/notes/gtask/data/SqlNote.java b/src/main/java/net/micode/notes/gtask/data/SqlNote.java index 79a4095..db9df1a 100644 --- a/src/main/java/net/micode/notes/gtask/data/SqlNote.java +++ b/src/main/java/net/micode/notes/gtask/data/SqlNote.java @@ -37,12 +37,15 @@ import org.json.JSONObject; import java.util.ArrayList; - +// SqlNote类用于处理便签数据的存储和操作,包括数据的加载、设置、获取和提交等操作。 public class SqlNote { + // 日志标签,用于调试和日志记录 private static final String TAG = SqlNote.class.getSimpleName(); + // 无效ID的常量值 private static final int INVALID_ID = -99999; + // 定义查询便签时需要的列 public static final String[] PROJECTION_NOTE = new String[] { NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID, NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE, @@ -52,76 +55,80 @@ public class SqlNote { NoteColumns.VERSION }; + // 定义PROJECTION_NOTE中各列的索引 public static final int ID_COLUMN = 0; - public static final int ALERTED_DATE_COLUMN = 1; - public static final int BG_COLOR_ID_COLUMN = 2; - public static final int CREATED_DATE_COLUMN = 3; - public static final int HAS_ATTACHMENT_COLUMN = 4; - public static final int MODIFIED_DATE_COLUMN = 5; - public static final int NOTES_COUNT_COLUMN = 6; - public static final int PARENT_ID_COLUMN = 7; - public static final int SNIPPET_COLUMN = 8; - public static final int TYPE_COLUMN = 9; - public static final int WIDGET_ID_COLUMN = 10; - public static final int WIDGET_TYPE_COLUMN = 11; - public static final int SYNC_ID_COLUMN = 12; - public static final int LOCAL_MODIFIED_COLUMN = 13; - public static final int ORIGIN_PARENT_ID_COLUMN = 14; - public static final int GTASK_ID_COLUMN = 15; - public static final int VERSION_COLUMN = 16; + // 上下文环境,用于访问应用程序资源和数据库 private Context mContext; + // 内容解析器,用于与内容提供器进行交互 private ContentResolver mContentResolver; + // 标志位,表示该便签是否是新创建的 private boolean mIsCreate; + // 便签的唯一标识符 private long mId; + // 便签的提醒日期 private long mAlertDate; + // 便签的背景颜色标识符 private int mBgColorId; + // 便签的创建日期 private long mCreatedDate; + // 标志位,表示便签是否包含附件 private int mHasAttachment; + // 便签的最后修改日期 private long mModifiedDate; + // 便签的父便签标识符 private long mParentId; + // 便签的简短摘要或标题 private String mSnippet; + // 便签的类型(如普通便签、文件夹、系统文件夹等) private int mType; + // 与便签关联的小部件标识符 private int mWidgetId; + // 便签的小部件类型 private int mWidgetType; + // 便签的原始父便签标识符 private long mOriginParent; + // 便签的版本号,用于数据同步和冲突检测 private long mVersion; + // 用于存储便签属性的更改,以便后续提交到数据库 private ContentValues mDiffNoteValues; + // 存储与便签相关的数据内容列表 private ArrayList mDataList; + // 构造方法,创建一个新的SqlNote对象,初始化成员变量,并设置默认值 public SqlNote(Context context) { mContext = context; mContentResolver = context.getContentResolver(); @@ -143,6 +150,7 @@ public class SqlNote { mDataList = new ArrayList(); } + // 构造方法,从数据库游标中加载便签数据,初始化SqlNote对象 public SqlNote(Context context, Cursor c) { mContext = context; mContentResolver = context.getContentResolver(); @@ -154,6 +162,7 @@ public class SqlNote { mDiffNoteValues = new ContentValues(); } + // 构造方法,根据便签ID从数据库中加载便签数据,初始化SqlNote对象 public SqlNote(Context context, long id) { mContext = context; mContentResolver = context.getContentResolver(); @@ -163,15 +172,15 @@ public class SqlNote { if (mType == Notes.TYPE_NOTE) loadDataContent(); mDiffNoteValues = new ContentValues(); - } + // 根据便签ID从数据库中查询便签数据,并加载到当前对象中 private void loadFromCursor(long id) { Cursor c = null; try { c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)", new String[] { - String.valueOf(id) + String.valueOf(id) }, null); if (c != null) { c.moveToNext(); @@ -185,6 +194,7 @@ public class SqlNote { } } + // 从数据库游标中加载便签数据到当前对象中 private void loadFromCursor(Cursor c) { mId = c.getLong(ID_COLUMN); mAlertDate = c.getLong(ALERTED_DATE_COLUMN); @@ -200,13 +210,14 @@ public class SqlNote { mVersion = c.getLong(VERSION_COLUMN); } + // 加载与当前便签相关的数据内容 private void loadDataContent() { Cursor c = null; mDataList.clear(); try { c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA, "(note_id=?)", new String[] { - String.valueOf(mId) + String.valueOf(mId) }, null); if (c != null) { if (c.getCount() == 0) { @@ -226,13 +237,14 @@ public class SqlNote { } } + // 从JSON对象中设置便签的内容和属性 public boolean setContent(JSONObject js) { try { JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) { Log.w(TAG, "cannot set system folder"); } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { - // for folder we can only update the snnipet and type + // 对于文件夹,我们只能更新摘要和类型 String snippet = note.has(NoteColumns.SNIPPET) ? note .getString(NoteColumns.SNIPPET) : ""; if (mIsCreate || !mSnippet.equals(snippet)) { @@ -248,7 +260,8 @@ public class SqlNote { mType = type; } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) { JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); - long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID; + long id = note.has(NoteColumns.ID) ? note + .getLong(NoteColumns.ID) : INVALID_ID; if (mIsCreate || mId != id) { mDiffNoteValues.put(NoteColumns.ID, id); } @@ -359,6 +372,7 @@ public class SqlNote { return true; } + // 将便签的内容和属性转换为JSON对象 public JSONObject getContent() { try { JSONObject js = new JSONObject(); @@ -407,39 +421,48 @@ public class SqlNote { return null; } + // 设置便签的父便签标识符 public void setParentId(long id) { mParentId = id; mDiffNoteValues.put(NoteColumns.PARENT_ID, id); } + // 设置便签的Gtask ID public void setGtaskId(String gid) { mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); } + // 设置便签的同步ID public void setSyncId(long syncId) { mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); } + // 重置便签的本地修改标志 public void resetLocalModified() { mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); } + // 获取便签的唯一标识符 public long getId() { return mId; } + // 获取便签的父便签标识符 public long getParentId() { return mParentId; } + // 获取便签的简短摘要或标题 public String getSnippet() { return mSnippet; } + // 判断便签是否为普通便签类型 public boolean isNoteType() { return mType == Notes.TYPE_NOTE; } + // 将便签的更改提交到数据库,包括插入新便签或更新现有便签,并处理数据同步和冲突检测 public void commit(boolean validateVersion) { if (mIsCreate) { if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { @@ -468,16 +491,16 @@ public class SqlNote { throw new IllegalStateException("Try to update note with invalid id"); } if (mDiffNoteValues.size() > 0) { - mVersion ++; + mVersion++; int result = 0; if (!validateVersion) { result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" + NoteColumns.ID + "=?)", new String[] { - String.valueOf(mId) + String.valueOf(mId) }); } else { result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" - + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)", + + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)", new String[] { String.valueOf(mId), String.valueOf(mVersion) }); @@ -494,7 +517,7 @@ public class SqlNote { } } - // refresh local info + // 刷新本地信息 loadFromCursor(mId); if (mType == Notes.TYPE_NOTE) loadDataContent(); @@ -502,4 +525,4 @@ public class SqlNote { mDiffNoteValues.clear(); mIsCreate = false; } -} +} \ No newline at end of file