From e1d345352a40bd1ca319b400e457a4180288c8ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=A0=E7=9A=84=E5=90=8D=E5=AD=97?= <2336156919@qq.com> Date: Thu, 15 May 2025 15:43:51 +0800 Subject: [PATCH] =?UTF-8?q?gtask/data=E5=8C=85=E4=B8=AD=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/net/micode/notes/gtask/data/Node.java | 94 +++++---- src/net/micode/notes/gtask/data/SqlData.java | 78 ++++++-- src/net/micode/notes/gtask/data/SqlNote.java | 181 ++++++++++++++---- src/net/micode/notes/gtask/data/Task.java | 145 ++++++++++---- src/net/micode/notes/gtask/data/TaskList.java | 144 +++++++++++--- 5 files changed, 491 insertions(+), 151 deletions(-) diff --git a/src/net/micode/notes/gtask/data/Node.java b/src/net/micode/notes/gtask/data/Node.java index 63950e0..1165147 100644 --- a/src/net/micode/notes/gtask/data/Node.java +++ b/src/net/micode/notes/gtask/data/Node.java @@ -1,52 +1,38 @@ /* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 根据Apache License 2.0授权 (http://www.apache.org/licenses/LICENSE-2.0) */ package net.micode.notes.gtask.data; import android.database.Cursor; - import org.json.JSONObject; +/** + * 抽象节点类,用于表示GTask中的基础数据节点 + * 提供同步操作类型定义和基础节点属性管理 + */ public abstract class Node { - public static final int SYNC_ACTION_NONE = 0; - - public static final int SYNC_ACTION_ADD_REMOTE = 1; - - public static final int SYNC_ACTION_ADD_LOCAL = 2; - - public static final int SYNC_ACTION_DEL_REMOTE = 3; - - public static final int SYNC_ACTION_DEL_LOCAL = 4; - - public static final int SYNC_ACTION_UPDATE_REMOTE = 5; - - public static final int SYNC_ACTION_UPDATE_LOCAL = 6; - - public static final int SYNC_ACTION_UPDATE_CONFLICT = 7; - - public static final int SYNC_ACTION_ERROR = 8; - - private String mGid; - - private String mName; - - private long mLastModified; - - private boolean mDeleted; - + // 同步操作类型常量定义 + public static final int SYNC_ACTION_NONE = 0; // 无同步操作 + public static final int SYNC_ACTION_ADD_REMOTE = 1; // 添加到远程 + public static final int SYNC_ACTION_ADD_LOCAL = 2; // 添加到本地 + public static final int SYNC_ACTION_DEL_REMOTE = 3; // 从远程删除 + public static final int SYNC_ACTION_DEL_LOCAL = 4; // 从本地删除 + public static final int SYNC_ACTION_UPDATE_REMOTE = 5; // 更新远程 + public static final int SYNC_ACTION_UPDATE_LOCAL = 6; // 更新本地 + public static final int SYNC_ACTION_UPDATE_CONFLICT = 7; // 更新冲突 + public static final int SYNC_ACTION_ERROR = 8; // 同步错误 + + private String mGid; // 节点全局ID(Google Task ID) + private String mName; // 节点名称 + private long mLastModified; // 最后修改时间戳 + private boolean mDeleted; // 删除标记 + + /** + * 构造函数,初始化节点属性 + */ public Node() { mGid = null; mName = ""; @@ -54,18 +40,47 @@ public abstract class Node { mDeleted = false; } + /** + * 抽象方法 - 获取创建操作的JSON对象 + * @param actionId 操作ID + * @return 包含创建操作的JSON对象 + */ public abstract JSONObject getCreateAction(int actionId); + /** + * 抽象方法 - 获取更新操作的JSON对象 + * @param actionId 操作ID + * @return 包含更新操作的JSON对象 + */ public abstract JSONObject getUpdateAction(int actionId); + /** + * 抽象方法 - 从远程JSON数据设置节点内容 + * @param js 远程JSON数据 + */ public abstract void setContentByRemoteJSON(JSONObject js); + /** + * 抽象方法 - 从本地JSON数据设置节点内容 + * @param js 本地JSON数据 + */ public abstract void setContentByLocalJSON(JSONObject js); + /** + * 抽象方法 - 从节点内容生成本地JSON对象 + * @return 包含节点内容的JSON对象 + */ public abstract JSONObject getLocalJSONFromContent(); + /** + * 抽象方法 - 根据数据库游标获取同步操作类型 + * @param c 数据库游标 + * @return 同步操作类型 + */ public abstract int getSyncAction(Cursor c); + // 以下是基础属性的getter和setter方法 + public void setGid(String gid) { this.mGid = gid; } @@ -97,5 +112,4 @@ public abstract class Node { public boolean getDeleted() { return this.mDeleted; } - -} +} \ No newline at end of file diff --git a/src/net/micode/notes/gtask/data/SqlData.java b/src/net/micode/notes/gtask/data/SqlData.java index d3ec3be..a1dde76 100644 --- a/src/net/micode/notes/gtask/data/SqlData.java +++ b/src/net/micode/notes/gtask/data/SqlData.java @@ -34,43 +34,57 @@ import net.micode.notes.gtask.exception.ActionFailureException; import org.json.JSONException; import org.json.JSONObject; - +/** + * SqlData类用于处理便签应用中数据的数据库操作,包括数据的创建、读取、更新等功能 + * 支持与JSON格式数据的相互转换,方便与Google Tasks等外部服务同步数据 + */ public class SqlData { private static final String TAG = SqlData.class.getSimpleName(); + // 无效ID值,用于标识尚未在数据库中创建的数据 private static final int INVALID_ID = -99999; + // 查询数据时使用的列投影 public static final String[] PROJECTION_DATA = new String[] { DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1, DataColumns.DATA3 }; + // 各数据列在投影中的索引位置 public static final int DATA_ID_COLUMN = 0; - public static final int DATA_MIME_TYPE_COLUMN = 1; - public static final int DATA_CONTENT_COLUMN = 2; - public static final int DATA_CONTENT_DATA_1_COLUMN = 3; - public static final int DATA_CONTENT_DATA_3_COLUMN = 4; + // ContentResolver用于与内容提供者交互 private ContentResolver mContentResolver; - + + // 是否为新建数据的标志 private boolean mIsCreate; - + + // 数据ID private long mDataId; - + + // 数据的MIME类型 private String mDataMimeType; - + + // 数据内容 private String mDataContent; - + + // 附加数据字段1 private long mDataContentData1; - + + // 附加数据字段3 private String mDataContentData3; - + + // 记录数据变更的ContentValues private ContentValues mDiffDataValues; + /** + * 构造函数,用于创建新的数据对象 + * @param context 应用上下文 + */ public SqlData(Context context) { mContentResolver = context.getContentResolver(); mIsCreate = true; @@ -82,6 +96,11 @@ public class SqlData { mDiffDataValues = new ContentValues(); } + /** + * 构造函数,用于从Cursor加载现有数据 + * @param context 应用上下文 + * @param c 包含数据的Cursor对象 + */ public SqlData(Context context, Cursor c) { mContentResolver = context.getContentResolver(); mIsCreate = false; @@ -89,6 +108,10 @@ public class SqlData { mDiffDataValues = new ContentValues(); } + /** + * 从Cursor加载数据到对象中 + * @param c 包含数据的Cursor对象 + */ private void loadFromCursor(Cursor c) { mDataId = c.getLong(DATA_ID_COLUMN); mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN); @@ -97,7 +120,13 @@ public class SqlData { mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN); } + /** + * 从JSON对象设置数据内容 + * @param js 包含数据的JSON对象 + * @throws JSONException JSON解析异常 + */ public void setContent(JSONObject js) throws JSONException { + // 从JSON中获取各字段值,并与当前值比较,记录有差异的字段 long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID; if (mIsCreate || mDataId != dataId) { mDiffDataValues.put(DataColumns.ID, dataId); @@ -130,11 +159,17 @@ public class SqlData { mDataContentData3 = dataContentData3; } + /** + * 将数据内容转换为JSON对象 + * @return 包含数据的JSON对象 + * @throws JSONException JSON生成异常 + */ public JSONObject getContent() throws JSONException { if (mIsCreate) { Log.e(TAG, "it seems that we haven't created this in database yet"); return null; } + // 将对象的各字段值存入JSON对象 JSONObject js = new JSONObject(); js.put(DataColumns.ID, mDataId); js.put(DataColumns.MIME_TYPE, mDataMimeType); @@ -144,9 +179,15 @@ public class SqlData { return js; } + /** + * 将数据变更提交到数据库 + * @param noteId 关联的便签ID + * @param validateVersion 是否验证版本 + * @param version 版本号 + */ public void commit(long noteId, boolean validateVersion, long version) { - if (mIsCreate) { + // 新建数据的处理 if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) { mDiffDataValues.remove(DataColumns.ID); } @@ -154,18 +195,22 @@ public class SqlData { mDiffDataValues.put(DataColumns.NOTE_ID, noteId); Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues); try { + // 从返回的URI中获取新生成的ID mDataId = Long.valueOf(uri.getPathSegments().get(1)); } catch (NumberFormatException e) { Log.e(TAG, "Get note id error :" + e.toString()); throw new ActionFailureException("create note failed"); } } else { + // 更新现有数据的处理 if (mDiffDataValues.size() > 0) { int result = 0; if (!validateVersion) { + // 不验证版本时直接更新 result = mContentResolver.update(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null); } else { + // 验证版本时,确保更新的是指定版本的便签 result = mContentResolver.update(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, " ? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE @@ -179,11 +224,16 @@ public class SqlData { } } + // 提交后清除变更记录,并标记为非新建状态 mDiffDataValues.clear(); mIsCreate = false; } + /** + * 获取数据ID + * @return 数据ID + */ public long getId() { return mDataId; } -} +} \ No newline at end of file diff --git a/src/net/micode/notes/gtask/data/SqlNote.java b/src/net/micode/notes/gtask/data/SqlNote.java index 79a4095..b169632 100644 --- a/src/net/micode/notes/gtask/data/SqlNote.java +++ b/src/net/micode/notes/gtask/data/SqlNote.java @@ -37,12 +37,18 @@ import org.json.JSONObject; import java.util.ArrayList; - +/** + * SqlNote类用于处理笔记的数据库操作,包括创建、读取、更新和删除操作 + * 支持与JSON格式数据的相互转换,方便与Google Tasks等外部服务同步 + * 同时管理笔记的基本信息和相关数据项 + */ 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 +58,83 @@ public class SqlNote { NoteColumns.VERSION }; + // 各列在投影中的索引位置 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; - + + // 笔记ID private long mId; - + + // 提醒日期 private long mAlertDate; - + + // 背景颜色ID private int mBgColorId; - + + // 创建日期 private long mCreatedDate; - + + // 是否有附件的标志 private int mHasAttachment; - + + // 修改日期 private long mModifiedDate; - + + // 父文件夹ID private long mParentId; - + + // 摘要内容 private String mSnippet; - + + // 笔记类型(普通笔记、文件夹、系统文件夹) private int mType; - + + // 桌面小部件ID private int mWidgetId; - + + // 桌面小部件类型 private int mWidgetType; - + + // 原始父文件夹ID private long mOriginParent; - + + // 版本号 private long mVersion; - + + // 记录笔记变更的ContentValues private ContentValues mDiffNoteValues; - + + // 笔记关联的数据列表 private ArrayList mDataList; + /** + * 构造函数,创建一个新的空笔记 + * @param context 应用上下文 + */ public SqlNote(Context context) { mContext = context; mContentResolver = context.getContentResolver(); @@ -143,6 +156,11 @@ public class SqlNote { mDataList = new ArrayList(); } + /** + * 构造函数,从Cursor加载现有笔记 + * @param context 应用上下文 + * @param c 包含笔记数据的Cursor + */ public SqlNote(Context context, Cursor c) { mContext = context; mContentResolver = context.getContentResolver(); @@ -154,6 +172,11 @@ public class SqlNote { mDiffNoteValues = new ContentValues(); } + /** + * 构造函数,根据ID从数据库加载笔记 + * @param context 应用上下文 + * @param id 笔记ID + */ public SqlNote(Context context, long id) { mContext = context; mContentResolver = context.getContentResolver(); @@ -163,9 +186,12 @@ public class SqlNote { if (mType == Notes.TYPE_NOTE) loadDataContent(); mDiffNoteValues = new ContentValues(); - } + /** + * 从数据库根据ID加载笔记数据 + * @param id 笔记ID + */ private void loadFromCursor(long id) { Cursor c = null; try { @@ -185,6 +211,10 @@ public class SqlNote { } } + /** + * 从Cursor加载笔记数据到对象中 + * @param c 包含笔记数据的Cursor + */ private void loadFromCursor(Cursor c) { mId = c.getLong(ID_COLUMN); mAlertDate = c.getLong(ALERTED_DATE_COLUMN); @@ -200,6 +230,9 @@ public class SqlNote { mVersion = c.getLong(VERSION_COLUMN); } + /** + * 加载与笔记关联的数据内容 + */ private void loadDataContent() { Cursor c = null; mDataList.clear(); @@ -226,13 +259,22 @@ public class SqlNote { } } + /** + * 从JSON对象设置笔记内容 + * @param js 包含笔记内容的JSON对象 + * @return 设置成功返回true,失败返回false + */ 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 + } + // 文件夹类型只能更新摘要和类型 + else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { String snippet = note.has(NoteColumns.SNIPPET) ? note .getString(NoteColumns.SNIPPET) : ""; if (mIsCreate || !mSnippet.equals(snippet)) { @@ -246,8 +288,12 @@ public class SqlNote { mDiffNoteValues.put(NoteColumns.TYPE, type); } mType = type; - } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) { + } + // 普通笔记类型可以更新全部属性 + else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) { JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + + // 从JSON中提取笔记各属性值,并与当前值比较,记录差异 long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID; if (mIsCreate || mId != id) { mDiffNoteValues.put(NoteColumns.ID, id); @@ -331,9 +377,12 @@ public class SqlNote { } mOriginParent = originParent; + // 处理笔记关联的数据项 for (int i = 0; i < dataArray.length(); i++) { JSONObject data = dataArray.getJSONObject(i); SqlData sqlData = null; + + // 如果数据项已存在,则获取它 if (data.has(DataColumns.ID)) { long dataId = data.getLong(DataColumns.ID); for (SqlData temp : mDataList) { @@ -343,11 +392,13 @@ public class SqlNote { } } + // 如果数据项不存在,则创建新的 if (sqlData == null) { sqlData = new SqlData(mContext); mDataList.add(sqlData); } + // 设置数据项内容 sqlData.setContent(data); } } @@ -359,6 +410,10 @@ public class SqlNote { return true; } + /** + * 将笔记内容转换为JSON对象 + * @return 包含笔记内容的JSON对象 + */ public JSONObject getContent() { try { JSONObject js = new JSONObject(); @@ -370,6 +425,7 @@ public class SqlNote { JSONObject note = new JSONObject(); if (mType == Notes.TYPE_NOTE) { + // 将笔记各属性添加到JSON对象 note.put(NoteColumns.ID, mId); note.put(NoteColumns.ALERTED_DATE, mAlertDate); note.put(NoteColumns.BG_COLOR_ID, mBgColorId); @@ -384,6 +440,7 @@ public class SqlNote { note.put(NoteColumns.ORIGIN_PARENT_ID, mOriginParent); js.put(GTaskStringUtils.META_HEAD_NOTE, note); + // 添加关联的数据项 JSONArray dataArray = new JSONArray(); for (SqlData sqlData : mDataList) { JSONObject data = sqlData.getContent(); @@ -393,6 +450,7 @@ public class SqlNote { } js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); } else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) { + // 文件夹类型只需要基本信息 note.put(NoteColumns.ID, mId); note.put(NoteColumns.TYPE, mType); note.put(NoteColumns.SNIPPET, mSnippet); @@ -407,45 +465,82 @@ public class SqlNote { return null; } + /** + * 设置笔记的父文件夹ID + * @param id 父文件夹ID + */ public void setParentId(long id) { mParentId = id; mDiffNoteValues.put(NoteColumns.PARENT_ID, id); } + /** + * 设置Google Tasks ID + * @param gid Google Tasks ID + */ public void setGtaskId(String gid) { mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); } + /** + * 设置同步ID + * @param syncId 同步ID + */ public void setSyncId(long syncId) { mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); } + /** + * 重置本地修改标志 + */ public void resetLocalModified() { mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); } + /** + * 获取笔记ID + * @return 笔记ID + */ public long getId() { return mId; } + /** + * 获取父文件夹ID + * @return 父文件夹ID + */ public long getParentId() { return mParentId; } + /** + * 获取笔记摘要 + * @return 笔记摘要 + */ public String getSnippet() { return mSnippet; } + /** + * 判断是否为普通笔记类型 + * @return 是普通笔记返回true,否则返回false + */ public boolean isNoteType() { return mType == Notes.TYPE_NOTE; } + /** + * 将笔记变更提交到数据库 + * @param validateVersion 是否验证版本,防止并发修改冲突 + */ public void commit(boolean validateVersion) { if (mIsCreate) { + // 创建新笔记 if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { mDiffNoteValues.remove(NoteColumns.ID); } + // 插入新笔记 Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues); try { mId = Long.valueOf(uri.getPathSegments().get(1)); @@ -457,12 +552,14 @@ public class SqlNote { throw new IllegalStateException("Create thread id failed"); } + // 如果是普通笔记,提交关联的数据项 if (mType == Notes.TYPE_NOTE) { for (SqlData sqlData : mDataList) { sqlData.commit(mId, false, -1); } } } else { + // 更新现有笔记 if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) { Log.e(TAG, "No such note"); throw new IllegalStateException("Try to update note with invalid id"); @@ -471,11 +568,13 @@ public class SqlNote { mVersion ++; int result = 0; if (!validateVersion) { + // 不验证版本直接更新 result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" + NoteColumns.ID + "=?)", new String[] { String.valueOf(mId) }); } else { + // 验证版本,确保只更新版本号小于等于当前版本的记录 result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)", new String[] { @@ -487,6 +586,7 @@ public class SqlNote { } } + // 如果是普通笔记,提交关联的数据项 if (mType == Notes.TYPE_NOTE) { for (SqlData sqlData : mDataList) { sqlData.commit(mId, validateVersion, mVersion); @@ -494,12 +594,13 @@ public class SqlNote { } } - // refresh local info + // 提交后刷新本地信息 loadFromCursor(mId); if (mType == Notes.TYPE_NOTE) loadDataContent(); + // 清除变更记录 mDiffNoteValues.clear(); mIsCreate = false; } -} +} \ No newline at end of file diff --git a/src/net/micode/notes/gtask/data/Task.java b/src/net/micode/notes/gtask/data/Task.java index 6a19454..97625ce 100644 --- a/src/net/micode/notes/gtask/data/Task.java +++ b/src/net/micode/notes/gtask/data/Task.java @@ -31,20 +31,32 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; - +/** + * Task类表示Google Tasks中的一个任务项 + * 负责处理任务的创建、更新操作,以及与本地笔记数据的同步 + * 继承自Node类,包含任务的基本属性和操作方法 + */ public class Task extends Node { private static final String TAG = Task.class.getSimpleName(); + // 任务完成状态 private boolean mCompleted; - + + // 任务备注 private String mNotes; - + + // 任务元数据 private JSONObject mMetaInfo; - + + // 前一个兄弟任务 private Task mPriorSibling; - + + // 父任务列表 private TaskList mParent; + /** + * 构造函数,初始化任务对象 + */ public Task() { super(); mCompleted = false; @@ -54,21 +66,26 @@ public class Task extends Node { mMetaInfo = null; } + /** + * 生成创建任务的JSON对象 + * @param actionId 操作ID + * @return 创建任务的JSON对象 + */ public JSONObject getCreateAction(int actionId) { JSONObject js = new JSONObject(); try { - // action_type + // 设置操作类型为创建 js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); - // action_id + // 设置操作ID js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); - // index + // 设置任务在父列表中的索引位置 js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this)); - // entity_delta + // 设置任务实体信息 JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); @@ -79,17 +96,17 @@ public class Task extends Node { } js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); - // parent_id + // 设置父任务列表ID js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid()); - // dest_parent_type + // 设置目标父类型为任务组 js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE, GTaskStringUtils.GTASK_JSON_TYPE_GROUP); - // list_id + // 设置任务列表ID js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid()); - // prior_sibling_id + // 设置前一个兄弟任务ID(如果有) if (mPriorSibling != null) { js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid()); } @@ -103,21 +120,26 @@ public class Task extends Node { return js; } + /** + * 生成更新任务的JSON对象 + * @param actionId 操作ID + * @return 更新任务的JSON对象 + */ public JSONObject getUpdateAction(int actionId) { JSONObject js = new JSONObject(); try { - // action_type + // 设置操作类型为更新 js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE); - // action_id + // 设置操作ID js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); - // id + // 设置任务ID js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); - // entity_delta + // 设置任务更新的实体信息 JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); if (getNotes() != null) { @@ -135,35 +157,34 @@ public class Task extends Node { return js; } + /** + * 根据远程JSON数据设置任务内容 + * @param js 包含任务信息的JSON对象 + */ public void setContentByRemoteJSON(JSONObject js) { if (js != null) { try { - // id + // 从JSON中提取任务各属性值 if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); } - // last_modified if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)); } - // name if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)); } - // notes if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) { setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES)); } - // deleted if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) { setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED)); } - // completed if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) { setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED)); } @@ -175,6 +196,10 @@ public class Task extends Node { } } + /** + * 根据本地JSON数据设置任务内容 + * @param js 包含本地笔记信息的JSON对象 + */ public void setContentByLocalJSON(JSONObject js) { if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE) || !js.has(GTaskStringUtils.META_HEAD_DATA)) { @@ -185,11 +210,13 @@ public class Task extends Node { JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + // 确保是笔记类型 if (note.getInt(NoteColumns.TYPE) != Notes.TYPE_NOTE) { Log.e(TAG, "invalid type"); return; } + // 从数据列表中找到笔记内容并设置为任务名称 for (int i = 0; i < dataArray.length(); i++) { JSONObject data = dataArray.getJSONObject(i); if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { @@ -204,16 +231,21 @@ public class Task extends Node { } } + /** + * 根据任务内容生成本地JSON对象 + * @return 包含任务信息的本地JSON对象 + */ public JSONObject getLocalJSONFromContent() { String name = getName(); try { if (mMetaInfo == null) { - // new task created from web + // 新创建的任务(来自网页) if (name == null) { Log.w(TAG, "the note seems to be an empty one"); return null; } + // 创建基本的笔记JSON结构 JSONObject js = new JSONObject(); JSONObject note = new JSONObject(); JSONArray dataArray = new JSONArray(); @@ -225,10 +257,11 @@ public class Task extends Node { js.put(GTaskStringUtils.META_HEAD_NOTE, note); return js; } else { - // synced task + // 已同步的任务,更新现有元数据 JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA); + // 更新笔记内容 for (int i = 0; i < dataArray.length(); i++) { JSONObject data = dataArray.getJSONObject(i); if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { @@ -247,6 +280,10 @@ public class Task extends Node { } } + /** + * 设置任务元数据 + * @param metaData 元数据对象 + */ public void setMetaInfo(MetaData metaData) { if (metaData != null && metaData.getNotes() != null) { try { @@ -258,6 +295,11 @@ public class Task extends Node { } } + /** + * 获取同步操作类型,决定如何处理本地和远程数据的同步 + * @param c 包含本地笔记数据的Cursor + * @return 同步操作类型(无操作、更新本地、更新远程、冲突等) + */ public int getSyncAction(Cursor c) { try { JSONObject noteInfo = null; @@ -275,31 +317,33 @@ public class Task extends Node { return SYNC_ACTION_UPDATE_LOCAL; } - // validate the note id now + // 验证笔记ID if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) { Log.w(TAG, "note id doesn't match"); return SYNC_ACTION_UPDATE_LOCAL; } + // 根据本地修改标志和同步ID决定同步操作 if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { - // there is no local update + // 本地没有更新 if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { - // no update both side + // 两边都没有更新 return SYNC_ACTION_NONE; } else { - // apply remote to local + // 应用远程更新到本地 return SYNC_ACTION_UPDATE_LOCAL; } } else { - // validate gtask id + // 验证Google Tasks ID if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { Log.e(TAG, "gtask id doesn't match"); return SYNC_ACTION_ERROR; } if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { - // local modification only + // 只有本地修改 return SYNC_ACTION_UPDATE_REMOTE; } else { + // 发生冲突 return SYNC_ACTION_UPDATE_CONFLICT; } } @@ -311,41 +355,76 @@ public class Task extends Node { return SYNC_ACTION_ERROR; } + /** + * 判断任务是否值得保存(即是否包含有效内容) + * @return 任务有内容返回true,否则返回false + */ public boolean isWorthSaving() { return mMetaInfo != null || (getName() != null && getName().trim().length() > 0) || (getNotes() != null && getNotes().trim().length() > 0); } + /** + * 设置任务完成状态 + * @param completed 完成状态 + */ public void setCompleted(boolean completed) { this.mCompleted = completed; } + /** + * 设置任务备注 + * @param notes 任务备注 + */ public void setNotes(String notes) { this.mNotes = notes; } + /** + * 设置前一个兄弟任务 + * @param priorSibling 前一个兄弟任务 + */ public void setPriorSibling(Task priorSibling) { this.mPriorSibling = priorSibling; } + /** + * 设置父任务列表 + * @param parent 父任务列表 + */ public void setParent(TaskList parent) { this.mParent = parent; } + /** + * 获取任务完成状态 + * @return 任务完成状态 + */ public boolean getCompleted() { return this.mCompleted; } + /** + * 获取任务备注 + * @return 任务备注 + */ public String getNotes() { return this.mNotes; } + /** + * 获取前一个兄弟任务 + * @return 前一个兄弟任务 + */ public Task getPriorSibling() { return this.mPriorSibling; } + /** + * 获取父任务列表 + * @return 父任务列表 + */ public TaskList getParent() { return this.mParent; } - -} +} \ No newline at end of file diff --git a/src/net/micode/notes/gtask/data/TaskList.java b/src/net/micode/notes/gtask/data/TaskList.java index 4ea21c5..4b32c91 100644 --- a/src/net/micode/notes/gtask/data/TaskList.java +++ b/src/net/micode/notes/gtask/data/TaskList.java @@ -29,35 +29,49 @@ import org.json.JSONObject; import java.util.ArrayList; - +/** + * TaskList类表示Google Tasks中的一个任务列表 + * 负责处理任务列表的创建、更新操作,以及与本地文件夹的同步 + * 包含多个子任务,继承自Node类 + */ public class TaskList extends Node { private static final String TAG = TaskList.class.getSimpleName(); + // 任务列表在父级中的索引位置 private int mIndex; - + + // 子任务列表 private ArrayList mChildren; + /** + * 构造函数,初始化任务列表对象 + */ public TaskList() { super(); mChildren = new ArrayList(); mIndex = 1; } + /** + * 生成创建任务列表的JSON对象 + * @param actionId 操作ID + * @return 创建任务列表的JSON对象 + */ public JSONObject getCreateAction(int actionId) { JSONObject js = new JSONObject(); try { - // action_type + // 设置操作类型为创建 js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); - // action_id + // 设置操作ID js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); - // index + // 设置任务列表索引 js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex); - // entity_delta + // 设置任务列表实体信息 JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); @@ -74,21 +88,26 @@ public class TaskList extends Node { return js; } + /** + * 生成更新任务列表的JSON对象 + * @param actionId 操作ID + * @return 更新任务列表的JSON对象 + */ public JSONObject getUpdateAction(int actionId) { JSONObject js = new JSONObject(); try { - // action_type + // 设置操作类型为更新 js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE); - // action_id + // 设置操作ID js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); - // id + // 设置任务列表ID js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); - // entity_delta + // 设置任务列表更新的实体信息 JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted()); @@ -103,20 +122,22 @@ public class TaskList extends Node { return js; } + /** + * 根据远程JSON数据设置任务列表内容 + * @param js 包含任务列表信息的JSON对象 + */ public void setContentByRemoteJSON(JSONObject js) { if (js != null) { try { - // id + // 从JSON中提取任务列表各属性值 if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); } - // last_modified if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)); } - // name if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)); } @@ -129,6 +150,10 @@ public class TaskList extends Node { } } + /** + * 根据本地JSON数据设置任务列表内容 + * @param js 包含本地文件夹信息的JSON对象 + */ public void setContentByLocalJSON(JSONObject js) { if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) { Log.w(TAG, "setContentByLocalJSON: nothing is avaiable"); @@ -137,6 +162,7 @@ public class TaskList extends Node { try { JSONObject folder = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); + // 根据文件夹类型设置任务列表名称 if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { String name = folder.getString(NoteColumns.SNIPPET); setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name); @@ -157,16 +183,23 @@ public class TaskList extends Node { } } + /** + * 根据任务列表内容生成本地JSON对象 + * @return 包含任务列表信息的本地JSON对象 + */ public JSONObject getLocalJSONFromContent() { try { JSONObject js = new JSONObject(); JSONObject folder = new JSONObject(); + // 处理任务列表名称,移除前缀 String folderName = getName(); if (getName().startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX)) folderName = folderName.substring(GTaskStringUtils.MIUI_FOLDER_PREFFIX.length(), folderName.length()); folder.put(NoteColumns.SNIPPET, folderName); + + // 根据名称确定文件夹类型 if (folderName.equals(GTaskStringUtils.FOLDER_DEFAULT) || folderName.equals(GTaskStringUtils.FOLDER_CALL_NOTE)) folder.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); @@ -183,28 +216,33 @@ public class TaskList extends Node { } } + /** + * 获取同步操作类型,决定如何处理本地和远程数据的同步 + * @param c 包含本地文件夹数据的Cursor + * @return 同步操作类型(无操作、更新本地、更新远程等) + */ public int getSyncAction(Cursor c) { try { if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { - // there is no local update + // 本地没有更新 if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { - // no update both side + // 两边都没有更新 return SYNC_ACTION_NONE; } else { - // apply remote to local + // 应用远程更新到本地 return SYNC_ACTION_UPDATE_LOCAL; } } else { - // validate gtask id + // 验证Google Tasks ID if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { Log.e(TAG, "gtask id doesn't match"); return SYNC_ACTION_ERROR; } if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { - // local modification only + // 只有本地修改 return SYNC_ACTION_UPDATE_REMOTE; } else { - // for folder conflicts, just apply local modification + // 对于文件夹冲突,直接应用本地修改 return SYNC_ACTION_UPDATE_REMOTE; } } @@ -216,16 +254,25 @@ public class TaskList extends Node { return SYNC_ACTION_ERROR; } + /** + * 获取子任务数量 + * @return 子任务数量 + */ public int getChildTaskCount() { return mChildren.size(); } + /** + * 添加子任务到任务列表末尾 + * @param task 要添加的任务 + * @return 添加成功返回true,失败返回false + */ public boolean addChildTask(Task task) { boolean ret = false; if (task != null && !mChildren.contains(task)) { ret = mChildren.add(task); if (ret) { - // need to set prior sibling and parent + // 设置任务的前一个兄弟任务和父任务列表 task.setPriorSibling(mChildren.isEmpty() ? null : mChildren .get(mChildren.size() - 1)); task.setParent(this); @@ -234,6 +281,12 @@ public class TaskList extends Node { return ret; } + /** + * 在指定位置添加子任务 + * @param task 要添加的任务 + * @param index 指定位置索引 + * @return 添加成功返回true,失败返回false + */ public boolean addChildTask(Task task, int index) { if (index < 0 || index > mChildren.size()) { Log.e(TAG, "add child task: invalid index"); @@ -244,7 +297,7 @@ public class TaskList extends Node { if (task != null && pos == -1) { mChildren.add(index, task); - // update the task list + // 更新任务列表中前后任务的关系 Task preTask = null; Task afterTask = null; if (index != 0) @@ -260,6 +313,11 @@ public class TaskList extends Node { return true; } + /** + * 移除子任务 + * @param task 要移除的任务 + * @return 移除成功返回true,失败返回false + */ public boolean removeChildTask(Task task) { boolean ret = false; int index = mChildren.indexOf(task); @@ -267,11 +325,11 @@ public class TaskList extends Node { ret = mChildren.remove(task); if (ret) { - // reset prior sibling and parent + // 重置任务的前一个兄弟任务和父任务列表 task.setPriorSibling(null); task.setParent(null); - // update the task list + // 更新任务列表中后续任务的前一个兄弟任务 if (index != mChildren.size()) { mChildren.get(index).setPriorSibling( index == 0 ? null : mChildren.get(index - 1)); @@ -281,6 +339,12 @@ public class TaskList extends Node { return ret; } + /** + * 移动子任务到指定位置 + * @param task 要移动的任务 + * @param index 指定位置索引 + * @return 移动成功返回true,失败返回false + */ public boolean moveChildTask(Task task, int index) { if (index < 0 || index >= mChildren.size()) { @@ -299,6 +363,11 @@ public class TaskList extends Node { return (removeChildTask(task) && addChildTask(task, index)); } + /** + * 根据Google Tasks ID查找子任务 + * @param gid Google Tasks ID + * @return 找到的任务,未找到返回null + */ public Task findChildTaskByGid(String gid) { for (int i = 0; i < mChildren.size(); i++) { Task t = mChildren.get(i); @@ -309,10 +378,20 @@ public class TaskList extends Node { return null; } + /** + * 获取子任务在列表中的索引位置 + * @param task 要查找的任务 + * @return 任务的索引位置,未找到返回-1 + */ public int getChildTaskIndex(Task task) { return mChildren.indexOf(task); } + /** + * 根据索引位置获取子任务 + * @param index 索引位置 + * @return 对应的任务,索引无效返回null + */ public Task getChildTaskByIndex(int index) { if (index < 0 || index >= mChildren.size()) { Log.e(TAG, "getTaskByIndex: invalid index"); @@ -321,6 +400,11 @@ public class TaskList extends Node { return mChildren.get(index); } + /** + * 根据Google Tasks ID获取子任务 + * @param gid Google Tasks ID + * @return 对应的任务,未找到返回null + */ public Task getChilTaskByGid(String gid) { for (Task task : mChildren) { if (task.getGid().equals(gid)) @@ -329,15 +413,27 @@ public class TaskList extends Node { return null; } + /** + * 获取所有子任务列表 + * @return 子任务列表 + */ public ArrayList getChildTaskList() { return this.mChildren; } + /** + * 设置任务列表在父级中的索引位置 + * @param index 索引位置 + */ public void setIndex(int index) { this.mIndex = index; } + /** + * 获取任务列表在父级中的索引位置 + * @return 索引位置 + */ public int getIndex() { return this.mIndex; } -} +} \ No newline at end of file