diff --git a/src/java/net/micode/notes/gtask/data/MetaData.java b/src/java/net/micode/notes/gtask/data/MetaData.java index 3a2050b..77d7bc0 100644 --- a/src/java/net/micode/notes/gtask/data/MetaData.java +++ b/src/java/net/micode/notes/gtask/data/MetaData.java @@ -26,57 +26,105 @@ import org.json.JSONObject; public class MetaData extends Task { + // 类名的简写,用于日志记录 private final static String TAG = MetaData.class.getSimpleName(); + // 与MetaData关联的GTask ID private String mRelatedGid = null; + /** + * 设置MetaData的元数据信息。 + * + * @param gid 关联的GTask ID + * @param metaInfo 元数据信息的JSONObject + */ public void setMeta(String gid, JSONObject metaInfo) { try { + // 将gid添加到metaInfo中 metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid); } catch (JSONException e) { + // 如果添加gid失败,记录错误日志 Log.e(TAG, "failed to put related gid"); } + // 将metaInfo转换为字符串并设置为notes setNotes(metaInfo.toString()); + // 设置MetaData的名称 setName(GTaskStringUtils.META_NOTE_NAME); } + /** + * 获取与MetaData关联的GTask ID。 + * + * @return 关联的GTask ID + */ public String getRelatedGid() { return mRelatedGid; } + /** + * 判断MetaData是否值得保存。 + * + * @return 如果notes不为空,则返回true + */ @Override public boolean isWorthSaving() { return getNotes() != null; } + /** + * 通过远程JSON设置MetaData的内容。 + * + * @param js 远程JSON对象 + */ @Override public void setContentByRemoteJSON(JSONObject js) { super.setContentByRemoteJSON(js); if (getNotes() != null) { try { + // 从notes中获取metaInfo JSONObject metaInfo = new JSONObject(getNotes().trim()); + // 获取关联的GTask ID mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); } catch (JSONException e) { + // 如果获取gid失败,记录警告日志 Log.w(TAG, "failed to get related gid"); mRelatedGid = null; } } } + /** + * 通过本地JSON设置MetaData的内容。该方法不应该被调用。 + * + * @param js 本地JSON对象 + * @throws IllegalAccessError 如果该方法被调用,抛出异常 + */ @Override public void setContentByLocalJSON(JSONObject js) { - // this function should not be called + // 该方法不应该被调用,抛出异常 throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called"); } + /** + * 从内容中获取本地JSON。该方法不应该被调用。 + * + * @throws IllegalAccessError 如果该方法被调用,抛出异常 + */ @Override public JSONObject getLocalJSONFromContent() { + // 该方法不应该被调用,抛出异常 throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called"); } + /** + * 获取同步操作。该方法不应该被调用。 + * + * @param c Cursor对象 + * @throws IllegalAccessError 如果该方法被调用,抛出异常 + */ @Override public int getSyncAction(Cursor c) { + // 该方法不应该被调用,抛出异常 throw new IllegalAccessError("MetaData:getSyncAction should not be called"); } - } diff --git a/src/java/net/micode/notes/gtask/data/Node.java b/src/java/net/micode/notes/gtask/data/Node.java index 63950e0..5276def 100644 --- a/src/java/net/micode/notes/gtask/data/Node.java +++ b/src/java/net/micode/notes/gtask/data/Node.java @@ -20,33 +20,78 @@ import android.database.Cursor; import org.json.JSONObject; +/** + * 节点类,表示一个数据节点,包含同步操作相关的属性和方法。 + */ 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 Node() { mGid = null; mName = ""; @@ -54,48 +99,120 @@ 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 Cursor对象 + * @return 同步操作类型 + */ public abstract int getSyncAction(Cursor c); + /** + * 设置节点的全局唯一标识符。 + * + * @param gid 全局唯一标识符 + */ public void setGid(String gid) { this.mGid = gid; } + /** + * 设置节点的名称。 + * + * @param name 名称 + */ public void setName(String name) { this.mName = name; } + /** + * 设置节点的最后修改时间。 + * + * @param lastModified 最后修改时间 + */ public void setLastModified(long lastModified) { this.mLastModified = lastModified; } + /** + * 设置节点是否被删除。 + * + * @param deleted 是否被删除 + */ public void setDeleted(boolean deleted) { this.mDeleted = deleted; } + /** + * 获取节点的全局唯一标识符。 + * + * @return 全局唯一标识符 + */ public String getGid() { return this.mGid; } + /** + * 获取节点的名称。 + * + * @return 名称 + */ public String getName() { return this.mName; } + /** + * 获取节点的最后修改时间。 + * + * @return 最后修改时间 + */ public long getLastModified() { return this.mLastModified; } + /** + * 获取节点是否被删除。 + * + * @return 是否被删除 + */ public boolean getDeleted() { return this.mDeleted; } - } diff --git a/src/java/net/micode/notes/gtask/data/SqlData.java b/src/java/net/micode/notes/gtask/data/SqlData.java index d3ec3be..4cbfbbe 100644 --- a/src/java/net/micode/notes/gtask/data/SqlData.java +++ b/src/java/net/micode/notes/gtask/data/SqlData.java @@ -35,42 +35,66 @@ import org.json.JSONException; import org.json.JSONObject; +/** + * SqlData类,用于封装和操作数据库中的数据。 + */ 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 }; + // 数据ID列的索引 public static final int DATA_ID_COLUMN = 0; + // 数据MIME类型列的索引 public static final int DATA_MIME_TYPE_COLUMN = 1; + // 数据内容列的索引 public static final int DATA_CONTENT_COLUMN = 2; + // 数据内容DATA1列的索引 public static final int DATA_CONTENT_DATA_1_COLUMN = 3; + // 数据内容DATA3列的索引 public static final int DATA_CONTENT_DATA_3_COLUMN = 4; + // 内容解析器 private ContentResolver mContentResolver; + // 是否为创建状态 private boolean mIsCreate; + // 数据ID private long mDataId; + // 数据MIME类型 private String mDataMimeType; + // 数据内容 private String mDataContent; + // 数据内容DATA1 private long mDataContentData1; + // 数据内容DATA3 private String mDataContentData3; + // 差异数据值 private ContentValues mDiffDataValues; + /** + * 构造函数,用于创建一个新的SqlData对象。 + * + * @param context 上下文 + */ public SqlData(Context context) { mContentResolver = context.getContentResolver(); mIsCreate = true; @@ -82,6 +106,12 @@ public class SqlData { mDiffDataValues = new ContentValues(); } + /** + * 构造函数,用于从Cursor加载数据创建SqlData对象。 + * + * @param context 上下文 + * @param c Cursor对象 + */ public SqlData(Context context, Cursor c) { mContentResolver = context.getContentResolver(); mIsCreate = false; @@ -89,6 +119,11 @@ 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,6 +132,12 @@ public class SqlData { mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN); } + /** + * 设置内容。 + * + * @param js JSON对象 + * @throws JSONException 如果JSON解析出错 + */ public void setContent(JSONObject js) throws JSONException { long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID; if (mIsCreate || mDataId != dataId) { @@ -130,6 +171,12 @@ public class SqlData { mDataContentData3 = dataContentData3; } + /** + * 获取内容。 + * + * @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"); @@ -144,6 +191,13 @@ public class SqlData { return js; } + /** + * 提交数据到数据库。 + * + * @param noteId 笔记ID + * @param validateVersion 是否验证版本 + * @param version 版本号 + */ public void commit(long noteId, boolean validateVersion, long version) { if (mIsCreate) { @@ -183,7 +237,12 @@ public class SqlData { mIsCreate = false; } + /** + * 获取数据ID。 + * + * @return 数据ID + */ public long getId() { return mDataId; } -} +} \ No newline at end of file diff --git a/src/java/net/micode/notes/gtask/data/SqlNote.java b/src/java/net/micode/notes/gtask/data/SqlNote.java index 79a4095..9545927 100644 --- a/src/java/net/micode/notes/gtask/data/SqlNote.java +++ b/src/java/net/micode/notes/gtask/data/SqlNote.java @@ -38,11 +38,17 @@ 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 +58,84 @@ 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; + // 差异笔记值 private ContentValues mDiffNoteValues; + // 数据列表 private ArrayList mDataList; + /** + * 构造函数,用于创建一个新的SqlNote对象。 + * + * @param context 上下文 + */ public SqlNote(Context context) { mContext = context; mContentResolver = context.getContentResolver(); @@ -143,6 +157,12 @@ public class SqlNote { mDataList = new ArrayList(); } + /** + * 构造函数,用于从Cursor加载数据创建SqlNote对象。 + * + * @param context 上下文 + * @param c Cursor对象 + */ public SqlNote(Context context, Cursor c) { mContext = context; mContentResolver = context.getContentResolver(); @@ -154,6 +174,12 @@ public class SqlNote { mDiffNoteValues = new ContentValues(); } + /** + * 构造函数,用于从笔记ID加载数据创建SqlNote对象。 + * + * @param context 上下文 + * @param id 笔记ID + */ public SqlNote(Context context, long id) { mContext = context; mContentResolver = context.getContentResolver(); @@ -163,9 +189,13 @@ 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 +215,11 @@ 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 +235,9 @@ public class SqlNote { mVersion = c.getLong(VERSION_COLUMN); } + /** + * 加载数据内容。 + */ private void loadDataContent() { Cursor c = null; mDataList.clear(); @@ -226,13 +264,19 @@ public class SqlNote { } } + /** + * 设置内容。 + * + * @param js JSON对象 + * @return 是否设置成功 + */ 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 + // for folder we can only update the snippet and type String snippet = note.has(NoteColumns.SNIPPET) ? note .getString(NoteColumns.SNIPPET) : ""; if (mIsCreate || !mSnippet.equals(snippet)) { @@ -262,244 +306,262 @@ public class SqlNote { mAlertDate = alertDate; int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? note - .getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext); - if (mIsCreate || mBgColorId != bgColorId) { - mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId); - } - mBgColorId = bgColorId; - - long createDate = note.has(NoteColumns.CREATED_DATE) ? note - .getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis(); - if (mIsCreate || mCreatedDate != createDate) { - mDiffNoteValues.put(NoteColumns.CREATED_DATE, createDate); - } - mCreatedDate = createDate; - - int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? note - .getInt(NoteColumns.HAS_ATTACHMENT) : 0; - if (mIsCreate || mHasAttachment != hasAttachment) { - mDiffNoteValues.put(NoteColumns.HAS_ATTACHMENT, hasAttachment); - } - mHasAttachment = hasAttachment; - - long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? note - .getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis(); - if (mIsCreate || mModifiedDate != modifiedDate) { - mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate); - } - mModifiedDate = modifiedDate; - - long parentId = note.has(NoteColumns.PARENT_ID) ? note - .getLong(NoteColumns.PARENT_ID) : 0; - if (mIsCreate || mParentId != parentId) { - mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId); - } - mParentId = parentId; - - String snippet = note.has(NoteColumns.SNIPPET) ? note - .getString(NoteColumns.SNIPPET) : ""; - if (mIsCreate || !mSnippet.equals(snippet)) { - mDiffNoteValues.put(NoteColumns.SNIPPET, snippet); - } - mSnippet = snippet; + .getInt(NoteColumnsmParentId != parentId) { + mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId); + } + mParentId = parentId; + String snippet = note.has(NoteColumns.SNIPPET) ? note + .getString(NoteColumns.SNIPPET) : ""; + if (mIsCreate || !mSnippet.equals(snippet)) { + mDiffNoteValues.put(NoteColumns.SNIPPET, snippet); + } + mSnippet = snippet; - int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE) - : Notes.TYPE_NOTE; - if (mIsCreate || mType != type) { - mDiffNoteValues.put(NoteColumns.TYPE, type); - } - mType = type; + int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE) + : Notes.TYPE_NOTE; + if (mIsCreate || mType != type) { + mDiffNoteValues.put(NoteColumns.TYPE, type); + } + mType = type; - int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID) - : AppWidgetManager.INVALID_APPWIDGET_ID; - if (mIsCreate || mWidgetId != widgetId) { - mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId); - } - mWidgetId = widgetId; + int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID) + : AppWidgetManager.INVALID_APPWIDGET_ID; + if (mIsCreate || mWidgetId != widgetId) { + mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId); + } + mWidgetId = widgetId; - int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note - .getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE; - if (mIsCreate || mWidgetType != widgetType) { - mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType); - } - mWidgetType = widgetType; + int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note + .getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE; + if (mIsCreate || mWidgetType != widgetType) { + mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType); + } + mWidgetType = widgetType; - long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note - .getLong(NoteColumns.ORIGIN_PARENT_ID) : 0; - if (mIsCreate || mOriginParent != originParent) { - mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent); - } - 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) { - if (dataId == temp.getId()) { - sqlData = temp; - } + long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note + .getLong(NoteColumns.ORIGIN_PARENT_ID) : 0; + if (mIsCreate || mOriginParent != originParent) { + mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent); + } + 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) { + if (dataId == temp.getId()) { + sqlData = temp; } } + } - if (sqlData == null) { - sqlData = new SqlData(mContext); - mDataList.add(sqlData); - } - - sqlData.setContent(data); + if (sqlData == null) { + sqlData = new SqlData(mContext); + mDataList.add(sqlData); } + + sqlData.setContent(data); } - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); - return false; } - return true; + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); + return false; } + return true; +} - public JSONObject getContent() { - try { - JSONObject js = new JSONObject(); +/** + * 获取内容。 + * + * @return JSON对象 + */ +public JSONObject getContent() { + try { + JSONObject js = new JSONObject(); - if (mIsCreate) { - Log.e(TAG, "it seems that we haven't created this in database yet"); - return null; - } + if (mIsCreate) { + Log.e(TAG, "it seems that we haven't created this in database yet"); + return null; + } - JSONObject note = new JSONObject(); - if (mType == Notes.TYPE_NOTE) { - note.put(NoteColumns.ID, mId); - note.put(NoteColumns.ALERTED_DATE, mAlertDate); - note.put(NoteColumns.BG_COLOR_ID, mBgColorId); - note.put(NoteColumns.CREATED_DATE, mCreatedDate); - note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment); - note.put(NoteColumns.MODIFIED_DATE, mModifiedDate); - note.put(NoteColumns.PARENT_ID, mParentId); - note.put(NoteColumns.SNIPPET, mSnippet); - note.put(NoteColumns.TYPE, mType); - note.put(NoteColumns.WIDGET_ID, mWidgetId); - note.put(NoteColumns.WIDGET_TYPE, mWidgetType); - 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(); - if (data != null) { - dataArray.put(data); - } + JSONObject note = new JSONObject(); + if (mType == Notes.TYPE_NOTE) { + note.put(NoteColumns.ID, mId); + note.put(NoteColumns.ALERTED_DATE, mAlertDate); + note.put(NoteColumns.BG_COLOR_ID, mBgColorId); + note.put(NoteColumns.CREATED_DATE, mCreatedDate); + note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment); + note.put(NoteColumns.MODIFIED_DATE, mModifiedDate); + note.put(NoteColumns.PARENT_ID, mParentId); + note.put(NoteColumns.SNIPPET, mSnippet); + note.put(NoteColumns.TYPE, mType); + note.put(NoteColumns.WIDGET_ID, mWidgetId); + note.put(NoteColumns.WIDGET_TYPE, mWidgetType); + 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(); + if (data != null) { + dataArray.put(data); } - 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); - js.put(GTaskStringUtils.META_HEAD_NOTE, note); } - - return js; - } catch (JSONException e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); + 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); + js.put(GTaskStringUtils.META_HEAD_NOTE, note); } - return null; - } - public void setParentId(long id) { - mParentId = id; - mDiffNoteValues.put(NoteColumns.PARENT_ID, id); + return js; + } catch (JSONException e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); } + return null; +} - public void setGtaskId(String gid) { - mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); - } +/** + * 设置父ID。 + * + * @param id 父ID + */ +public void setParentId(long id) { + mParentId = id; + mDiffNoteValues.put(NoteColumns.PARENT_ID, id); +} - public void setSyncId(long syncId) { - mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); - } +/** + * 设置GTask ID。 + * + * @param gid GTask ID + */ +public void setGtaskId(String gid) { + mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); +} - public void resetLocalModified() { - mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); - } +/** + * 设置同步ID。 + * + * @param syncId 同步ID + */ +public void setSyncId(long syncId) { + mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); +} - public long getId() { - return mId; - } +/** + * 重置本地修改标记。 + */ +public void resetLocalModified() { + mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); +} - public long getParentId() { - return mParentId; - } +/** + * 获取笔记ID。 + * + * @return 笔记ID + */ +public long getId() { + return mId; +} - public String getSnippet() { - return mSnippet; - } +/** + * 获取父ID。 + * + * @return 父ID + */ +public long getParentId() { + return mParentId; +} - public boolean isNoteType() { - return mType == Notes.TYPE_NOTE; - } +/** + * 获取摘要。 + * + * @return 摘要 + */ +public String getSnippet() { + return mSnippet; +} - public void commit(boolean validateVersion) { - if (mIsCreate) { - if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { - mDiffNoteValues.remove(NoteColumns.ID); - } +/** + * 判断是否为笔记类型。 + * + * @return 是否为笔记类型 + */ +public boolean isNoteType() { + return mType == Notes.TYPE_NOTE; +} - Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues); - try { - mId = Long.valueOf(uri.getPathSegments().get(1)); - } catch (NumberFormatException e) { - Log.e(TAG, "Get note id error :" + e.toString()); - throw new ActionFailureException("create note failed"); - } - if (mId == 0) { - throw new IllegalStateException("Create thread id failed"); - } +/** + * 提交数据到数据库。 + * + * @param validateVersion 是否验证版本 + */ +public void commit(boolean validateVersion) { + if (mIsCreate) { + if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { + mDiffNoteValues.remove(NoteColumns.ID); + } - if (mType == Notes.TYPE_NOTE) { - for (SqlData sqlData : mDataList) { - sqlData.commit(mId, false, -1); - } + Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues); + try { + mId = Long.valueOf(uri.getPathSegments().get(1)); + } catch (NumberFormatException e) { + Log.e(TAG, "Get note id error :" + e.toString()); + throw new ActionFailureException("create note failed"); + } + if (mId == 0) { + 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"); + } + } 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"); + } + if (mDiffNoteValues.size() > 0) { + 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[] { + String.valueOf(mId), String.valueOf(mVersion) + }); } - if (mDiffNoteValues.size() > 0) { - 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[] { - String.valueOf(mId), String.valueOf(mVersion) - }); - } - if (result == 0) { - Log.w(TAG, "there is no update. maybe user updates note when syncing"); - } + if (result == 0) { + Log.w(TAG, "there is no update. maybe user updates note when syncing"); } + } - if (mType == Notes.TYPE_NOTE) { - for (SqlData sqlData : mDataList) { - sqlData.commit(mId, validateVersion, mVersion); - } + if (mType == Notes.TYPE_NOTE) { + for (SqlData sqlData : mDataList) { + sqlData.commit(mId, validateVersion, mVersion); } } + } - // refresh local info - loadFromCursor(mId); - if (mType == Notes.TYPE_NOTE) - loadDataContent(); + // 刷新本地信息 + loadFromCursor(mId); + if (mType == Notes.TYPE_NOTE) + loadDataContent(); - mDiffNoteValues.clear(); - mIsCreate = false; - } -} + mDiffNoteValues.clear(); + mIsCreate = false; +} \ No newline at end of file diff --git a/src/java/net/micode/notes/gtask/data/Task.java b/src/java/net/micode/notes/gtask/data/Task.java index 6a19454..9941f63 100644 --- a/src/java/net/micode/notes/gtask/data/Task.java +++ b/src/java/net/micode/notes/gtask/data/Task.java @@ -32,19 +32,31 @@ import org.json.JSONException; import org.json.JSONObject; +/** + * Task类,表示一个任务,继承自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,27 @@ 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 +97,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 +121,27 @@ 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 +159,40 @@ public class Task extends Node { return js; } + /** + * 通过远程JSON设置任务的内容。 + * + * @param js 远程JSON对象 + */ public void setContentByRemoteJSON(JSONObject js) { if (js != null) { try { - // id + // ID 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 +204,11 @@ 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)) { @@ -204,11 +238,16 @@ public class Task extends Node { } } + /** + * 从任务的内容中获取本地JSON对象。 + * + * @return 本地JSON对象 + */ public JSONObject getLocalJSONFromContent() { String name = getName(); try { if (mMetaInfo == null) { - // new task created from web + // 从Web创建的新任务 if (name == null) { Log.w(TAG, "the note seems to be an empty one"); return null; @@ -225,7 +264,7 @@ 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); @@ -247,6 +286,11 @@ public class Task extends Node { } } + /** + * 设置任务的元数据信息。 + * + * @param metaData 元数据对象 + */ public void setMetaInfo(MetaData metaData) { if (metaData != null && metaData.getNotes() != null) { try { @@ -258,6 +302,12 @@ public class Task extends Node { } } + /** + * 获取任务的同步操作类型。 + * + * @param c Cursor对象 + * @return 同步操作类型 + */ public int getSyncAction(Cursor c) { try { JSONObject noteInfo = null; @@ -275,77 +325,117 @@ public class Task extends Node { return SYNC_ACTION_UPDATE_LOCAL; } - // validate the note id now + // 验证note ID if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) { Log.w(TAG, "note id doesn't match"); + if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { + // 本地没有更新 + if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { + // 两边都没有更新 + return SYNC_ACTION_NONE; + } else { + // 应用远程更新到本地 return SYNC_ACTION_UPDATE_LOCAL; } - - 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 { + // 验证gtask 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()) { + // 只有本地修改 + return SYNC_ACTION_UPDATE_REMOTE; } else { - // validate gtask 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; - } + return SYNC_ACTION_UPDATE_CONFLICT; } - } catch (Exception e) { - Log.e(TAG, e.toString()); - e.printStackTrace(); } - - return SYNC_ACTION_ERROR; - } - - public boolean isWorthSaving() { - return mMetaInfo != null || (getName() != null && getName().trim().length() > 0) - || (getNotes() != null && getNotes().trim().length() > 0); + } catch (Exception e) { + Log.e(TAG, e.toString()); + e.printStackTrace(); } - public void setCompleted(boolean completed) { - this.mCompleted = completed; - } + return SYNC_ACTION_ERROR; +} - public void setNotes(String notes) { - this.mNotes = notes; - } +/** + * 判断任务是否值得保存。 + * + * @return 是否值得保存 + */ +public boolean isWorthSaving() { + return mMetaInfo != null || (getName() != null && getName().trim().length() > 0) + || (getNotes() != null && getNotes().trim().length() > 0); +} - public void setPriorSibling(Task priorSibling) { - this.mPriorSibling = priorSibling; - } +/** + * 设置任务是否完成。 + * + * @param completed 是否完成 + */ +public void setCompleted(boolean completed) { + this.mCompleted = completed; +} - public void setParent(TaskList parent) { - this.mParent = parent; - } +/** + * 设置任务的备注信息。 + * + * @param notes 备注信息 + */ +public void setNotes(String notes) { + this.mNotes = notes; +} - public boolean getCompleted() { - return this.mCompleted; - } +/** + * 设置任务的前一个兄弟任务。 + * + * @param priorSibling 前一个兄弟任务 + */ +public void setPriorSibling(Task priorSibling) { + this.mPriorSibling = priorSibling; +} - public String getNotes() { - return this.mNotes; - } +/** + * 设置任务所属的任务列表。 + * + * @param parent 任务列表 + */ +public void setParent(TaskList parent) { + this.mParent = parent; +} - public Task getPriorSibling() { - return this.mPriorSibling; - } +/** + * 获取任务是否完成。 + * + * @return 是否完成 + */ +public boolean getCompleted() { + return this.mCompleted; +} - public TaskList getParent() { - return this.mParent; - } +/** + * 获取任务的备注信息。 + * + * @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/java/net/micode/notes/gtask/data/TaskList.java b/src/java/net/micode/notes/gtask/data/TaskList.java index 4ea21c5..3fa64c3 100644 --- a/src/java/net/micode/notes/gtask/data/TaskList.java +++ b/src/java/net/micode/notes/gtask/data/TaskList.java @@ -30,34 +30,49 @@ import org.json.JSONObject; import java.util.ArrayList; +/** + * TaskList类,表示一个任务列表,继承自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 +89,27 @@ 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 +124,25 @@ public class TaskList extends Node { return js; } + /** + * 通过远程JSON设置任务列表的内容。 + * + * @param js 远程JSON对象 + */ public void setContentByRemoteJSON(JSONObject js) { if (js != null) { try { - // id + // ID 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 +155,11 @@ 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"); @@ -157,6 +188,11 @@ public class TaskList extends Node { } } + /** + * 从任务列表的内容中获取本地JSON对象。 + * + * @return 本地JSON对象 + */ public JSONObject getLocalJSONFromContent() { try { JSONObject js = new JSONObject(); @@ -183,28 +219,34 @@ 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 + // 验证gtask 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 +258,27 @@ public class TaskList extends Node { return SYNC_ACTION_ERROR; } + /** + * 获取任务列表中的任务数量。 + * + * @return 任务数量 + */ public int getChildTaskCount() { return mChildren.size(); } + /** + * 向任务列表中添加任务。 + * + * @param task 任务 + * @return 是否添加成功 + */ 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 +287,13 @@ public class TaskList extends Node { return ret; } + /** + * 向任务列表中添加任务。 + * + * @param task 任务 + * @param index 索引 + * @return 是否添加成功 + */ public boolean addChildTask(Task task, int index) { if (index < 0 || index > mChildren.size()) { Log.e(TAG, "add child task: invalid index"); @@ -244,7 +304,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 +320,12 @@ public class TaskList extends Node { return true; } + /** + * 从任务列表中移除任务。 + * + * @param task 任务 + * @return 是否移除成功 + */ public boolean removeChildTask(Task task) { boolean ret = false; int index = mChildren.indexOf(task); @@ -267,11 +333,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,8 +347,14 @@ public class TaskList extends Node { return ret; } + /** + * 移动任务在任务列表中的位置。 + * + * @param task 任务 + * @param index 新的索引位置 + * @return 是否移动成功 + */ public boolean moveChildTask(Task task, int index) { - if (index < 0 || index >= mChildren.size()) { Log.e(TAG, "move child task: invalid index"); return false; @@ -290,15 +362,22 @@ public class TaskList extends Node { int pos = mChildren.indexOf(task); if (pos == -1) { - Log.e(TAG, "move child task: the task should in the list"); + Log.e(TAG, "move child task: the task should be in the list"); return false; } if (pos == index) - return true; - return (removeChildTask(task) && addChildTask(task, index)); + return true; // 如果位置没有变化,直接返回 + + return (removeChildTask(task) && addChildTask(task, index)); // 移除并添加到新位置 } + /** + * 根据GID查找子任务。 + * + * @param gid 任务的全局唯一标识符 + * @return 找到的子任务,若未找到则返回null + */ public Task findChildTaskByGid(String gid) { for (int i = 0; i < mChildren.size(); i++) { Task t = mChildren.get(i); @@ -309,10 +388,22 @@ 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 +412,12 @@ public class TaskList extends Node { return mChildren.get(index); } + /** + * 根据GID获取子任务。 + * + * @param gid 任务的全局唯一标识符 + * @return 找到的子任务,若未找到则返回null + */ public Task getChilTaskByGid(String gid) { for (Task task : mChildren) { if (task.getGid().equals(gid)) @@ -329,15 +426,30 @@ 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