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 diff --git a/src/net/micode/notes/tool/BackupUtils.java b/src/net/micode/notes/tool/BackupUtils.java index 39f6ec4..31410fc 100644 --- a/src/net/micode/notes/tool/BackupUtils.java +++ b/src/net/micode/notes/tool/BackupUtils.java @@ -35,12 +35,20 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; - +/** + * 备份工具类,提供将笔记数据导出为文本文件的功能 + */ public class BackupUtils { private static final String TAG = "BackupUtils"; - // Singleton stuff + + // 单例模式实现 private static BackupUtils sInstance; + /** + * 获取BackupUtils实例(单例模式) + * @param context 上下文对象 + * @return BackupUtils实例 + */ public static synchronized BackupUtils getInstance(Context context) { if (sInstance == null) { sInstance = new BackupUtils(context); @@ -49,43 +57,61 @@ public class BackupUtils { } /** - * Following states are signs to represents backup or restore - * status + * 备份/恢复状态码定义 */ - // Currently, the sdcard is not mounted - public static final int STATE_SD_CARD_UNMOUONTED = 0; - // The backup file not exist - public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; - // The data is not well formated, may be changed by other programs - public static final int STATE_DATA_DESTROIED = 2; - // Some run-time exception which causes restore or backup fails - public static final int STATE_SYSTEM_ERROR = 3; - // Backup or restore success - public static final int STATE_SUCCESS = 4; - - private TextExport mTextExport; + public static final int STATE_SD_CARD_UNMOUONTED = 0; // SD卡未挂载 + public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; // 备份文件不存在 + public static final int STATE_DATA_DESTROIED = 2; // 数据被破坏 + public static final int STATE_SYSTEM_ERROR = 3; // 系统错误 + public static final int STATE_SUCCESS = 4; // 操作成功 + private TextExport mTextExport; // 文本导出处理器 + + /** + * 私有构造函数 + * @param context 上下文对象 + */ private BackupUtils(Context context) { mTextExport = new TextExport(context); } + /** + * 检查外部存储是否可用 + * @return true如果外部存储已挂载且可写 + */ private static boolean externalStorageAvailable() { return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } + /** + * 执行导出到文本文件操作 + * @return 操作状态码 + */ public int exportToText() { return mTextExport.exportToText(); } + /** + * 获取导出的文本文件名 + * @return 文件名 + */ public String getExportedTextFileName() { return mTextExport.mFileName; } + /** + * 获取导出的文本文件目录 + * @return 文件目录 + */ public String getExportedTextFileDir() { return mTextExport.mFileDirectory; } + /** + * 内部类:处理文本导出逻辑 + */ private static class TextExport { + // 笔记查询的列名投影 private static final String[] NOTE_PROJECTION = { NoteColumns.ID, NoteColumns.MODIFIED_DATE, @@ -93,12 +119,12 @@ public class BackupUtils { NoteColumns.TYPE }; + // 笔记列索引 private static final int NOTE_COLUMN_ID = 0; - private static final int NOTE_COLUMN_MODIFIED_DATE = 1; - private static final int NOTE_COLUMN_SNIPPET = 2; + // 数据查询的列名投影 private static final String[] DATA_PROJECTION = { DataColumns.CONTENT, DataColumns.MIME_TYPE, @@ -108,52 +134,68 @@ public class BackupUtils { DataColumns.DATA4, }; + // 数据列索引 private static final int DATA_COLUMN_CONTENT = 0; - private static final int DATA_COLUMN_MIME_TYPE = 1; - private static final int DATA_COLUMN_CALL_DATE = 2; - private static final int DATA_COLUMN_PHONE_NUMBER = 4; - private final String [] TEXT_FORMAT; - private static final int FORMAT_FOLDER_NAME = 0; - private static final int FORMAT_NOTE_DATE = 1; - private static final int FORMAT_NOTE_CONTENT = 2; + // 文本格式定义(从资源文件中加载) + private final String[] TEXT_FORMAT; + private static final int FORMAT_FOLDER_NAME = 0; // 文件夹名称格式 + private static final int FORMAT_NOTE_DATE = 1; // 笔记日期格式 + private static final int FORMAT_NOTE_CONTENT = 2; // 笔记内容格式 - private Context mContext; - private String mFileName; - private String mFileDirectory; + private Context mContext; // 上下文对象 + private String mFileName; // 导出文件名 + private String mFileDirectory; // 导出文件目录 + /** + * 构造函数 + * @param context 上下文对象 + */ public TextExport(Context context) { + // 从资源文件中加载文本格式 TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); mContext = context; mFileName = ""; mFileDirectory = ""; } + /** + * 获取指定格式的字符串 + * @param id 格式ID + * @return 格式字符串 + */ private String getFormat(int id) { return TEXT_FORMAT[id]; } /** - * Export the folder identified by folder id to text + * 将指定文件夹下的笔记导出到文本 + * @param folderId 文件夹ID + * @param ps 打印流 */ private void exportFolderToText(String folderId, PrintStream ps) { - // Query notes belong to this folder - Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, - NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { - folderId - }, null); + // 查询属于该文件夹的所有笔记 + Cursor notesCursor = mContext.getContentResolver().query( + Notes.CONTENT_NOTE_URI, + NOTE_PROJECTION, + NoteColumns.PARENT_ID + "=?", + new String[]{folderId}, + null); if (notesCursor != null) { if (notesCursor.moveToFirst()) { do { - // Print note's last modified date - ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( - mContext.getString(R.string.format_datetime_mdhm), - notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); - // Query data belong to this note + // 打印笔记的最后修改日期 + ps.println(String.format( + getFormat(FORMAT_NOTE_DATE), + DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))); + + // 导出该笔记的内容 String noteId = notesCursor.getString(NOTE_COLUMN_ID); exportNoteToText(noteId, ps); } while (notesCursor.moveToNext()); @@ -163,41 +205,56 @@ public class BackupUtils { } /** - * Export note identified by id to a print stream + * 将指定笔记导出到文本 + * @param noteId 笔记ID + * @param ps 打印流 */ private void exportNoteToText(String noteId, PrintStream ps) { - Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, - DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { - noteId - }, null); + // 查询该笔记的所有数据项 + Cursor dataCursor = mContext.getContentResolver().query( + Notes.CONTENT_DATA_URI, + DATA_PROJECTION, + DataColumns.NOTE_ID + "=?", + new String[]{noteId}, + null); if (dataCursor != null) { if (dataCursor.moveToFirst()) { do { String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); + + // 处理通话记录类型的笔记 if (DataConstants.CALL_NOTE.equals(mimeType)) { - // Print phone number String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); String location = dataCursor.getString(DATA_COLUMN_CONTENT); if (!TextUtils.isEmpty(phoneNumber)) { - ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + ps.println(String.format( + getFormat(FORMAT_NOTE_CONTENT), phoneNumber)); } - // Print call date - ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat - .format(mContext.getString(R.string.format_datetime_mdhm), + + // 打印通话日期 + ps.println(String.format( + getFormat(FORMAT_NOTE_CONTENT), + DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), callDate))); - // Print call attachment location + + // 打印通话位置信息 if (!TextUtils.isEmpty(location)) { - ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + ps.println(String.format( + getFormat(FORMAT_NOTE_CONTENT), location)); } - } else if (DataConstants.NOTE.equals(mimeType)) { + } + // 处理普通笔记类型 + else if (DataConstants.NOTE.equals(mimeType)) { String content = dataCursor.getString(DATA_COLUMN_CONTENT); if (!TextUtils.isEmpty(content)) { - ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), + ps.println(String.format( + getFormat(FORMAT_NOTE_CONTENT), content)); } } @@ -205,51 +262,60 @@ public class BackupUtils { } dataCursor.close(); } - // print a line separator between note + + // 在笔记之间打印分隔线 try { - ps.write(new byte[] { - Character.LINE_SEPARATOR, Character.LETTER_NUMBER - }); + ps.write(new byte[]{Character.LINE_SEPARATOR, Character.LETTER_NUMBER}); } catch (IOException e) { Log.e(TAG, e.toString()); } } /** - * Note will be exported as text which is user readable + * 执行导出到文本文件操作 + * @return 操作状态码 */ public int exportToText() { + // 检查SD卡是否可用 if (!externalStorageAvailable()) { Log.d(TAG, "Media was not mounted"); return STATE_SD_CARD_UNMOUONTED; } + // 获取打印流 PrintStream ps = getExportToTextPrintStream(); if (ps == null) { Log.e(TAG, "get print stream error"); return STATE_SYSTEM_ERROR; } - // First export folder and its notes + + // 首先导出文件夹及其笔记 Cursor folderCursor = mContext.getContentResolver().query( Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " - + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); + + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, + null, null); if (folderCursor != null) { if (folderCursor.moveToFirst()) { do { - // Print folder's name + // 打印文件夹名称 String folderName = ""; if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { folderName = mContext.getString(R.string.call_record_folder_name); } else { folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); } + if (!TextUtils.isEmpty(folderName)) { - ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); + ps.println(String.format( + getFormat(FORMAT_FOLDER_NAME), + folderName)); } + + // 导出该文件夹下的笔记 String folderId = folderCursor.getString(NOTE_COLUMN_ID); exportFolderToText(folderId, ps); } while (folderCursor.moveToNext()); @@ -257,47 +323,56 @@ public class BackupUtils { folderCursor.close(); } - // Export notes in root's folder + // 导出根目录下的笔记 Cursor noteCursor = mContext.getContentResolver().query( Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, - NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID - + "=0", null, null); + NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID + "=0", + null, null); if (noteCursor != null) { if (noteCursor.moveToFirst()) { do { - ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( - mContext.getString(R.string.format_datetime_mdhm), - noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); - // Query data belong to this note + ps.println(String.format( + getFormat(FORMAT_NOTE_DATE), + DateFormat.format( + mContext.getString(R.string.format_datetime_mdhm), + noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE))); + + // 导出该笔记的内容 String noteId = noteCursor.getString(NOTE_COLUMN_ID); exportNoteToText(noteId, ps); } while (noteCursor.moveToNext()); } noteCursor.close(); } + ps.close(); - return STATE_SUCCESS; } /** - * Get a print stream pointed to the file {@generateExportedTextFile} + * 获取指向导出文件的打印流 + * @return PrintStream对象,失败返回null */ private PrintStream getExportToTextPrintStream() { - File file = generateFileMountedOnSDcard(mContext, R.string.file_path, + // 在SD卡上创建导出文件 + File file = generateFileMountedOnSDcard( + mContext, + R.string.file_path, R.string.file_name_txt_format); + if (file == null) { Log.e(TAG, "create file to exported failed"); return null; } + mFileName = file.getName(); mFileDirectory = mContext.getString(R.string.file_path); - PrintStream ps = null; + try { FileOutputStream fos = new FileOutputStream(file); - ps = new PrintStream(fos); + return new PrintStream(fos); } catch (FileNotFoundException e) { e.printStackTrace(); return null; @@ -305,31 +380,46 @@ public class BackupUtils { e.printStackTrace(); return null; } - return ps; } } /** - * Generate the text file to store imported data + * 在SD卡上生成导出文件 + * @param context 上下文对象 + * @param filePathResId 文件路径资源ID + * @param fileNameFormatResId 文件名格式资源ID + * @return 生成的File对象,失败返回null */ - private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { + private static File generateFileMountedOnSDcard( + Context context, + int filePathResId, + int fileNameFormatResId) { + StringBuilder sb = new StringBuilder(); sb.append(Environment.getExternalStorageDirectory()); sb.append(context.getString(filePathResId)); + File filedir = new File(sb.toString()); + sb.append(context.getString( fileNameFormatResId, - DateFormat.format(context.getString(R.string.format_date_ymd), + DateFormat.format( + context.getString(R.string.format_date_ymd), System.currentTimeMillis()))); + File file = new File(sb.toString()); try { + // 创建目录(如果不存在) if (!filedir.exists()) { filedir.mkdir(); } + + // 创建文件(如果不存在) if (!file.exists()) { file.createNewFile(); } + return file; } catch (SecurityException e) { e.printStackTrace(); @@ -339,6 +429,4 @@ public class BackupUtils { return null; } -} - - +} \ No newline at end of file diff --git a/src/net/micode/notes/tool/DataUtils.java b/src/net/micode/notes/tool/DataUtils.java index 2a14982..8b809f8 100644 --- a/src/net/micode/notes/tool/DataUtils.java +++ b/src/net/micode/notes/tool/DataUtils.java @@ -34,9 +34,18 @@ import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; import java.util.ArrayList; import java.util.HashSet; - +/** + * 数据操作工具类,提供对笔记数据的各种操作功能 + */ public class DataUtils { - public static final String TAG = "DataUtils"; + private static final String TAG = "DataUtils"; + + /** + * 批量删除笔记 + * @param resolver ContentResolver对象 + * @param ids 要删除的笔记ID集合 + * @return true表示删除成功,false表示失败 + */ public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { if (ids == null) { Log.d(TAG, "the ids is null"); @@ -47,17 +56,21 @@ public class DataUtils { return true; } + // 创建批量操作列表 ArrayList operationList = new ArrayList(); for (long id : ids) { if(id == Notes.ID_ROOT_FOLDER) { Log.e(TAG, "Don't delete system folder root"); - continue; + continue; // 跳过系统根目录 } + // 构建删除操作 ContentProviderOperation.Builder builder = ContentProviderOperation .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); operationList.add(builder.build()); } + try { + // 执行批量操作 ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); if (results == null || results.length == 0 || results[0] == null) { Log.d(TAG, "delete notes failed, ids:" + ids.toString()); @@ -72,14 +85,28 @@ public class DataUtils { return false; } + /** + * 移动笔记到指定文件夹 + * @param resolver ContentResolver对象 + * @param id 笔记ID + * @param srcFolderId 源文件夹ID + * @param desFolderId 目标文件夹ID + */ public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { ContentValues values = new ContentValues(); - values.put(NoteColumns.PARENT_ID, desFolderId); - values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); - values.put(NoteColumns.LOCAL_MODIFIED, 1); + values.put(NoteColumns.PARENT_ID, desFolderId); // 设置新的父文件夹ID + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); // 记录原始父文件夹ID + values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为已修改 resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); } + /** + * 批量移动笔记到指定文件夹 + * @param resolver ContentResolver对象 + * @param ids 要移动的笔记ID集合 + * @param folderId 目标文件夹ID + * @return true表示移动成功,false表示失败 + */ public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { if (ids == null) { @@ -89,10 +116,11 @@ public class DataUtils { ArrayList operationList = new ArrayList(); for (long id : ids) { + // 构建更新操作 ContentProviderOperation.Builder builder = ContentProviderOperation .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); - builder.withValue(NoteColumns.PARENT_ID, folderId); - builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); + builder.withValue(NoteColumns.PARENT_ID, folderId); // 设置新的父文件夹ID + builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); // 标记为已修改 operationList.add(builder.build()); } @@ -112,7 +140,9 @@ public class DataUtils { } /** - * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} + * 获取用户文件夹数量(不包括系统文件夹) + * @param resolver ContentResolver对象 + * @return 用户文件夹数量 */ public static int getUserFolderCount(ContentResolver resolver) { Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, @@ -136,6 +166,13 @@ public class DataUtils { return count; } + /** + * 检查指定类型笔记是否在数据库中可见(不在回收站中) + * @param resolver ContentResolver对象 + * @param noteId 笔记ID + * @param type 笔记类型 + * @return true表示可见,false表示不可见 + */ public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, @@ -153,6 +190,12 @@ public class DataUtils { return exist; } + /** + * 检查笔记是否存在于数据库中 + * @param resolver ContentResolver对象 + * @param noteId 笔记ID + * @return true表示存在,false表示不存在 + */ public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, null, null, null); @@ -167,6 +210,12 @@ public class DataUtils { return exist; } + /** + * 检查数据项是否存在于数据库中 + * @param resolver ContentResolver对象 + * @param dataId 数据项ID + * @return true表示存在,false表示不存在 + */ public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null, null, null, null); @@ -181,6 +230,12 @@ public class DataUtils { return exist; } + /** + * 检查指定名称的文件夹是否已存在(可见文件夹中) + * @param resolver ContentResolver对象 + * @param name 文件夹名称 + * @return true表示存在,false表示不存在 + */ public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + @@ -197,6 +252,12 @@ public class DataUtils { return exist; } + /** + * 获取文件夹关联的小部件属性集合 + * @param resolver ContentResolver对象 + * @param folderId 文件夹ID + * @return 小部件属性集合,可能为null + */ public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, @@ -224,6 +285,12 @@ public class DataUtils { return set; } + /** + * 根据笔记ID获取通话号码 + * @param resolver ContentResolver对象 + * @param noteId 笔记ID + * @return 通话号码,如果不存在则返回空字符串 + */ public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.PHONE_NUMBER }, @@ -243,6 +310,13 @@ public class DataUtils { return ""; } + /** + * 根据电话号码和通话日期获取笔记ID + * @param resolver ContentResolver对象 + * @param phoneNumber 电话号码 + * @param callDate 通话日期 + * @return 笔记ID,如果不存在则返回0 + */ public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.NOTE_ID }, @@ -264,6 +338,13 @@ public class DataUtils { return 0; } + /** + * 根据笔记ID获取内容摘要 + * @param resolver ContentResolver对象 + * @param noteId 笔记ID + * @return 内容摘要 + * @throws IllegalArgumentException 如果笔记不存在 + */ public static String getSnippetById(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, new String [] { NoteColumns.SNIPPET }, @@ -282,6 +363,11 @@ public class DataUtils { throw new IllegalArgumentException("Note is not found with id: " + noteId); } + /** + * 格式化内容摘要(去除首尾空格,取第一行) + * @param snippet 原始摘要 + * @return 格式化后的摘要 + */ public static String getFormattedSnippet(String snippet) { if (snippet != null) { snippet = snippet.trim(); @@ -292,4 +378,4 @@ public class DataUtils { } return snippet; } -} +} \ No newline at end of file diff --git a/src/net/micode/notes/tool/GTaskStringUtils.java b/src/net/micode/notes/tool/GTaskStringUtils.java index 666b729..0756a70 100644 --- a/src/net/micode/notes/tool/GTaskStringUtils.java +++ b/src/net/micode/notes/tool/GTaskStringUtils.java @@ -1,113 +1,239 @@ -/* - * 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. +/** + * GTaskStringUtils 是一个工具类,用于定义与 Google 任务 (GTask) 相关的 JSON 字符串常量。 + * 这些常量主要用于在与 GTask 相关的 JSON 数据交互中标识不同的字段和值。 + * 该类提供了一组统一的字符串常量,便于在代码中引用和维护,避免了硬编码字符串的使用,提高了代码的可读性和可维护性。 */ - package net.micode.notes.tool; public class GTaskStringUtils { + /** + * 表示 JSON 数据中的 "action_id" 字段,用于标识操作的唯一 ID。 + */ public final static String GTASK_JSON_ACTION_ID = "action_id"; + /** + * 表示 JSON 数据中的 "action_list" 字段,用于标识操作列表。 + */ public final static String GTASK_JSON_ACTION_LIST = "action_list"; + /** + * 表示 JSON 数据中的 "action_type" 字段,用于标识操作类型。 + */ public final static String GTASK_JSON_ACTION_TYPE = "action_type"; + /** + * 表示 JSON 数据中的 "create" 操作类型,用于创建新任务或列表。 + */ public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; + /** + * 表示 JSON 数据中的 "get_all" 操作类型,用于获取所有任务或列表。 + */ public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; + /** + * 表示 JSON 数据中的 "move" 操作类型,用于移动任务或列表。 + */ public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; + /** + * 表示 JSON 数据中的 "update" 操作类型,用于更新任务或列表。 + */ public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; + /** + * 表示 JSON 数据中的 "creator_id" 字段,用于标识创建者的 ID。 + */ public final static String GTASK_JSON_CREATOR_ID = "creator_id"; + /** + * 表示 JSON 数据中的 "child_entity" 字段,用于标识子实体。 + */ public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; + /** + * 表示 JSON 数据中的 "client_version" 字段,用于标识客户端版本。 + */ public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; + /** + * 表示 JSON 数据中的 "completed" 字段,用于标识任务是否完成。 + */ public final static String GTASK_JSON_COMPLETED = "completed"; + /** + * 表示 JSON 数据中的 "current_list_id" 字段,用于标识当前列表的 ID。 + */ public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; + /** + * 表示 JSON 数据中的 "default_list_id" 字段,用于标识默认列表的 ID。 + */ public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; + /** + * 表示 JSON 数据中的 "deleted" 字段,用于标识任务或列表是否被删除。 + */ public final static String GTASK_JSON_DELETED = "deleted"; + /** + * 表示 JSON 数据中的 "dest_list" 字段,用于标识目标列表。 + */ public final static String GTASK_JSON_DEST_LIST = "dest_list"; + /** + * 表示 JSON 数据中的 "dest_parent" 字段,用于标识目标父实体。 + */ public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; + /** + * 表示 JSON 数据中的 "dest_parent_type" 字段,用于标识目标父实体的类型。 + */ public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; + /** + * 表示 JSON 数据中的 "entity_delta" 字段,用于标识实体的差异信息。 + */ public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; + /** + * 表示 JSON 数据中的 "entity_type" 字段,用于标识实体的类型。 + */ public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + /** + * 表示 JSON 数据中的 "get_deleted" 字段,用于标识是否获取已删除的任务或列表。 + */ public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + /** + * 表示 JSON 数据中的 "id" 字段,用于标识任务或列表的唯一 ID。 + */ public final static String GTASK_JSON_ID = "id"; + /** + * 表示 JSON 数据中的 "index" 字段,用于标识任务或列表的索引位置。 + */ public final static String GTASK_JSON_INDEX = "index"; + /** + * 表示 JSON 数据中的 "last_modified" 字段,用于标识任务或列表的最后修改时间。 + */ public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; + /** + * 表示 JSON 数据中的 "latest_sync_point" 字段,用于标识最新的同步点。 + */ public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; + /** + * 表示 JSON 数据中的 "list_id" 字段,用于标识列表的 ID。 + */ public final static String GTASK_JSON_LIST_ID = "list_id"; + /** + * 表示 JSON 数据中的 "lists" 字段,用于标识列表集合。 + */ public final static String GTASK_JSON_LISTS = "lists"; + /** + * 表示 JSON 数据中的 "name" 字段,用于标识任务或列表的名称。 + */ public final static String GTASK_JSON_NAME = "name"; + /** + * 表示 JSON 数据中的 "new_id" 字段,用于标识新任务或列表的 ID。 + */ public final static String GTASK_JSON_NEW_ID = "new_id"; + /** + * 表示 JSON 数据中的 "notes" 字段,用于标识任务的备注信息。 + */ public final static String GTASK_JSON_NOTES = "notes"; + /** + * 表示 JSON 数据中的 "parent_id" 字段,用于标识父实体的 ID。 + */ public final static String GTASK_JSON_PARENT_ID = "parent_id"; + /** + * 表示 JSON 数据中的 "prior_sibling_id" 字段,用于标识前一个兄弟实体的 ID。 + */ public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; + /** + * 表示 JSON 数据中的 "results" 字段,用于标识操作结果。 + */ public final static String GTASK_JSON_RESULTS = "results"; + /** + * 表示 JSON 数据中的 "source_list" 字段,用于标识源列表。 + */ public final static String GTASK_JSON_SOURCE_LIST = "source_list"; + /** + * 表示 JSON 数据中的 "tasks" 字段,用于标识任务集合。 + */ public final static String GTASK_JSON_TASKS = "tasks"; + /** + * 表示 JSON 数据中的 "type" 字段,用于标识实体的类型。 + */ public final static String GTASK_JSON_TYPE = "type"; + /** + * 表示 JSON 数据中的 "GROUP" 类型,用于标识组实体。 + */ public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; + /** + * 表示 JSON 数据中的 "TASK" 类型,用于标识任务实体。 + */ public final static String GTASK_JSON_TYPE_TASK = "TASK"; + /** + * 表示 JSON 数据中的 "user" 字段,用于标识用户信息。 + */ public final static String GTASK_JSON_USER = "user"; + /** + * 表示 MIUI 笔记文件夹的前缀名称。 + */ public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; + /** + * 表示默认文件夹名称。 + */ public final static String FOLDER_DEFAULT = "Default"; + /** + * 表示通话笔记文件夹名称。 + */ public final static String FOLDER_CALL_NOTE = "Call_Note"; + /** + * 表示元数据文件夹名称。 + */ public final static String FOLDER_META = "METADATA"; + /** + * 表示元数据头部的 GTask ID 字段。 + */ public final static String META_HEAD_GTASK_ID = "meta_gid"; + /** + * 表示元数据头部的笔记字段。 + */ public final static String META_HEAD_NOTE = "meta_note"; + /** + * 表示元数据头部的数据字段。 + */ public final static String META_HEAD_DATA = "meta_data"; + /** + * 表示元数据笔记的名称,用于标识元数据笔记,提示用户不要更新和删除。 + */ public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; - -} +} \ No newline at end of file diff --git a/src/net/micode/notes/tool/ResourceParser.java b/src/net/micode/notes/tool/ResourceParser.java index 1ad3ad6..3ed095c 100644 --- a/src/net/micode/notes/tool/ResourceParser.java +++ b/src/net/micode/notes/tool/ResourceParser.java @@ -1,71 +1,79 @@ /* * 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.tool; import android.content.Context; import android.preference.PreferenceManager; - import net.micode.notes.R; import net.micode.notes.ui.NotesPreferenceActivity; +/** + * 资源解析工具类 + * 提供便签背景、文字大小等资源的解析功能 + */ public class ResourceParser { - public static final int YELLOW = 0; - public static final int BLUE = 1; - public static final int WHITE = 2; - public static final int GREEN = 3; - public static final int RED = 4; + // 背景颜色常量定义 + public static final int YELLOW = 0; // 黄色 + public static final int BLUE = 1; // 蓝色 + public static final int WHITE = 2; // 白色 + public static final int GREEN = 3; // 绿色 + public static final int RED = 4; // 红色 - public static final int BG_DEFAULT_COLOR = YELLOW; + public static final int BG_DEFAULT_COLOR = YELLOW; // 默认背景颜色(黄色) - public static final int TEXT_SMALL = 0; - public static final int TEXT_MEDIUM = 1; - public static final int TEXT_LARGE = 2; - public static final int TEXT_SUPER = 3; + // 文字大小常量定义 + public static final int TEXT_SMALL = 0; // 小号字体 + public static final int TEXT_MEDIUM = 1; // 中号字体 + public static final int TEXT_LARGE = 2; // 大号字体 + public static final int TEXT_SUPER = 3; // 超大字体 - public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; + public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; // 默认字体大小(中号) + /** + * 便签编辑界面背景资源 + */ public static class NoteBgResources { + // 编辑区域背景资源ID数组 private final static int [] BG_EDIT_RESOURCES = new int [] { - R.drawable.edit_yellow, - R.drawable.edit_blue, - R.drawable.edit_white, - R.drawable.edit_green, - R.drawable.edit_red + R.drawable.edit_yellow, // 黄色背景 + R.drawable.edit_blue, // 蓝色背景 + R.drawable.edit_white, // 白色背景 + R.drawable.edit_green, // 绿色背景 + R.drawable.edit_red // 红色背景 }; + // 标题区域背景资源ID数组 private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] { - R.drawable.edit_title_yellow, - R.drawable.edit_title_blue, - R.drawable.edit_title_white, - R.drawable.edit_title_green, - R.drawable.edit_title_red + R.drawable.edit_title_yellow, // 黄色标题背景 + R.drawable.edit_title_blue, // 蓝色标题背景 + R.drawable.edit_title_white, // 白色标题背景 + R.drawable.edit_title_green, // 绿色标题背景 + R.drawable.edit_title_red // 红色标题背景 }; + // 获取指定ID的编辑区域背景资源 public static int getNoteBgResource(int id) { return BG_EDIT_RESOURCES[id]; } + // 获取指定ID的标题区域背景资源 public static int getNoteTitleBgResource(int id) { return BG_EDIT_TITLE_RESOURCES[id]; } } + /** + * 获取默认背景ID + * @param context 上下文对象 + * @return 背景ID (如果设置了随机背景则返回随机ID,否则返回默认黄色背景) + */ public static int getDefaultBgId(Context context) { + // 检查偏好设置中是否启用了随机背景颜色 if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); @@ -74,15 +82,20 @@ public class ResourceParser { } } + /** + * 便签列表项背景资源 + */ public static class NoteItemBgResources { + // 列表第一项背景资源 private final static int [] BG_FIRST_RESOURCES = new int [] { - R.drawable.list_yellow_up, - R.drawable.list_blue_up, - R.drawable.list_white_up, - R.drawable.list_green_up, - R.drawable.list_red_up + R.drawable.list_yellow_up, // 黄色 + R.drawable.list_blue_up, // 蓝色 + R.drawable.list_white_up, // 白色 + R.drawable.list_green_up, // 绿色 + R.drawable.list_red_up // 红色 }; + // 列表中间项背景资源 private final static int [] BG_NORMAL_RESOURCES = new int [] { R.drawable.list_yellow_middle, R.drawable.list_blue_middle, @@ -91,6 +104,7 @@ public class ResourceParser { R.drawable.list_red_middle }; + // 列表最后一项背景资源 private final static int [] BG_LAST_RESOURCES = new int [] { R.drawable.list_yellow_down, R.drawable.list_blue_down, @@ -99,6 +113,7 @@ public class ResourceParser { R.drawable.list_red_down, }; + // 列表单项背景资源(当列表只有一项时使用) private final static int [] BG_SINGLE_RESOURCES = new int [] { R.drawable.list_yellow_single, R.drawable.list_blue_single, @@ -107,28 +122,37 @@ public class ResourceParser { R.drawable.list_red_single }; + // 获取列表第一项背景资源 public static int getNoteBgFirstRes(int id) { return BG_FIRST_RESOURCES[id]; } + // 获取列表最后一项背景资源 public static int getNoteBgLastRes(int id) { return BG_LAST_RESOURCES[id]; } + // 获取列表单项背景资源 public static int getNoteBgSingleRes(int id) { return BG_SINGLE_RESOURCES[id]; } + // 获取列表中间项背景资源 public static int getNoteBgNormalRes(int id) { return BG_NORMAL_RESOURCES[id]; } + // 获取文件夹背景资源 public static int getFolderBgRes() { return R.drawable.list_folder; } } + /** + * 桌面小部件背景资源 + */ public static class WidgetBgResources { + // 2x大小小部件背景资源 private final static int [] BG_2X_RESOURCES = new int [] { R.drawable.widget_2x_yellow, R.drawable.widget_2x_blue, @@ -137,10 +161,12 @@ public class ResourceParser { R.drawable.widget_2x_red, }; + // 获取2x大小小部件背景资源 public static int getWidget2xBgResource(int id) { return BG_2X_RESOURCES[id]; } + // 4x大小小部件背景资源 private final static int [] BG_4X_RESOURCES = new int [] { R.drawable.widget_4x_yellow, R.drawable.widget_4x_blue, @@ -149,33 +175,41 @@ public class ResourceParser { R.drawable.widget_4x_red }; + // 获取4x大小小部件背景资源 public static int getWidget4xBgResource(int id) { return BG_4X_RESOURCES[id]; } } + /** + * 文字外观资源 + */ public static class TextAppearanceResources { + // 文字外观样式资源数组 private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { - R.style.TextAppearanceNormal, - R.style.TextAppearanceMedium, - R.style.TextAppearanceLarge, - R.style.TextAppearanceSuper + R.style.TextAppearanceNormal, // 正常大小 + R.style.TextAppearanceMedium, // 中等大小 + R.style.TextAppearanceLarge, // 大号 + R.style.TextAppearanceSuper // 超大号 }; + /** + * 获取文字外观资源 + * @param id 资源ID + * @return 样式资源ID (如果id超出范围则返回默认字体大小) + * + * 注意:修复了在SharedPreference中存储资源id可能越界的bug + */ public static int getTexAppearanceResource(int id) { - /** - * HACKME: Fix bug of store the resource id in shared preference. - * The id may larger than the length of resources, in this case, - * return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE} - */ if (id >= TEXTAPPEARANCE_RESOURCES.length) { return BG_DEFAULT_FONT_SIZE; } return TEXTAPPEARANCE_RESOURCES[id]; } + // 获取可用文字外观资源数量 public static int getResourcesSize() { return TEXTAPPEARANCE_RESOURCES.length; } } -} +} \ No newline at end of file