diff --git a/代码解读/MiNotes-master/.gradle/6.5/executionHistory/executionHistory.bin b/代码解读/MiNotes-master/.gradle/6.5/executionHistory/executionHistory.bin index 0baac56..46f712b 100644 Binary files a/代码解读/MiNotes-master/.gradle/6.5/executionHistory/executionHistory.bin and b/代码解读/MiNotes-master/.gradle/6.5/executionHistory/executionHistory.bin differ diff --git a/代码解读/MiNotes-master/.gradle/6.5/executionHistory/executionHistory.lock b/代码解读/MiNotes-master/.gradle/6.5/executionHistory/executionHistory.lock index 8477ec8..262114f 100644 Binary files a/代码解读/MiNotes-master/.gradle/6.5/executionHistory/executionHistory.lock and b/代码解读/MiNotes-master/.gradle/6.5/executionHistory/executionHistory.lock differ diff --git a/代码解读/MiNotes-master/.gradle/6.5/fileHashes/fileHashes.bin b/代码解读/MiNotes-master/.gradle/6.5/fileHashes/fileHashes.bin index 296a2bb..f18925d 100644 Binary files a/代码解读/MiNotes-master/.gradle/6.5/fileHashes/fileHashes.bin and b/代码解读/MiNotes-master/.gradle/6.5/fileHashes/fileHashes.bin differ diff --git a/代码解读/MiNotes-master/.gradle/6.5/fileHashes/fileHashes.lock b/代码解读/MiNotes-master/.gradle/6.5/fileHashes/fileHashes.lock index ac20653..99a860a 100644 Binary files a/代码解读/MiNotes-master/.gradle/6.5/fileHashes/fileHashes.lock and b/代码解读/MiNotes-master/.gradle/6.5/fileHashes/fileHashes.lock differ diff --git a/代码解读/MiNotes-master/.gradle/6.5/javaCompile/classAnalysis.bin b/代码解读/MiNotes-master/.gradle/6.5/javaCompile/classAnalysis.bin index f78f835..375a453 100644 Binary files a/代码解读/MiNotes-master/.gradle/6.5/javaCompile/classAnalysis.bin and b/代码解读/MiNotes-master/.gradle/6.5/javaCompile/classAnalysis.bin differ diff --git a/代码解读/MiNotes-master/.gradle/6.5/javaCompile/javaCompile.lock b/代码解读/MiNotes-master/.gradle/6.5/javaCompile/javaCompile.lock index eba610b..b9029aa 100644 Binary files a/代码解读/MiNotes-master/.gradle/6.5/javaCompile/javaCompile.lock and b/代码解读/MiNotes-master/.gradle/6.5/javaCompile/javaCompile.lock differ diff --git a/代码解读/MiNotes-master/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/代码解读/MiNotes-master/.gradle/buildOutputCleanup/buildOutputCleanup.lock index d67f70f..b3be2d6 100644 Binary files a/代码解读/MiNotes-master/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/代码解读/MiNotes-master/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/代码解读/MiNotes-master/app/build/intermediates/dex/debug/mergeDexDebug/classes.dex b/代码解读/MiNotes-master/app/build/intermediates/dex/debug/mergeDexDebug/classes.dex index 1ce1505..34b7cc0 100644 Binary files a/代码解读/MiNotes-master/app/build/intermediates/dex/debug/mergeDexDebug/classes.dex and b/代码解读/MiNotes-master/app/build/intermediates/dex/debug/mergeDexDebug/classes.dex differ diff --git a/代码解读/MiNotes-master/app/build/intermediates/incremental/packageDebug/tmp/debug/dex-renamer-state.txt b/代码解读/MiNotes-master/app/build/intermediates/incremental/packageDebug/tmp/debug/dex-renamer-state.txt index ba295ea..4eb9104 100644 --- a/代码解读/MiNotes-master/app/build/intermediates/incremental/packageDebug/tmp/debug/dex-renamer-state.txt +++ b/代码解读/MiNotes-master/app/build/intermediates/incremental/packageDebug/tmp/debug/dex-renamer-state.txt @@ -1,4 +1,4 @@ -#Tue Apr 11 20:43:06 CST 2023 +#Fri Apr 14 20:15:17 CST 2023 base.1=D\:\\IDEA\\AndroidModeCode\\MiNotes-master\\app\\build\\intermediates\\dex\\debug\\mergeDexDebug\\classes.dex path.1=classes.dex base.0=E\:\\MiNotes-master1\\MiNotes-master\\app\\build\\intermediates\\dex\\debug\\mergeDexDebug\\classes.dex diff --git a/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskClient.class b/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskClient.class index 0eaffc2..140ba28 100644 Binary files a/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskClient.class and b/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskClient.class differ diff --git a/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskManager.class b/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskManager.class index fcc21cc..f39d073 100644 Binary files a/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskManager.class and b/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskManager.class differ diff --git a/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskSyncService$1.class b/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskSyncService$1.class index 12a31cb..cb50ef0 100644 Binary files a/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskSyncService$1.class and b/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskSyncService$1.class differ diff --git a/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskSyncService.class b/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskSyncService.class index ed251ec..24d29e7 100644 Binary files a/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskSyncService.class and b/代码解读/MiNotes-master/app/build/intermediates/javac/debug/classes/net/micode/notes/gtask/remote/GTaskSyncService.class differ diff --git a/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskClient.dex b/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskClient.dex index 76a77ae..269fe71 100644 Binary files a/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskClient.dex and b/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskClient.dex differ diff --git a/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskManager.dex b/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskManager.dex index 29bc79b..3abbe09 100644 Binary files a/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskManager.dex and b/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskManager.dex differ diff --git a/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskSyncService$1.dex b/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskSyncService$1.dex index 985ceed..75bcc1c 100644 Binary files a/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskSyncService$1.dex and b/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskSyncService$1.dex differ diff --git a/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskSyncService.dex b/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskSyncService.dex index 156d968..78d7339 100644 Binary files a/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskSyncService.dex and b/代码解读/MiNotes-master/app/build/intermediates/project_dex_archive/debug/out/net/micode/notes/gtask/remote/GTaskSyncService.dex differ diff --git a/代码解读/MiNotes-master/app/build/outputs/apk/debug/app-debug.apk b/代码解读/MiNotes-master/app/build/outputs/apk/debug/app-debug.apk index 9fde1fc..5d38896 100644 Binary files a/代码解读/MiNotes-master/app/build/outputs/apk/debug/app-debug.apk and b/代码解读/MiNotes-master/app/build/outputs/apk/debug/app-debug.apk differ diff --git a/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskClient.java b/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskClient.java index d1f57f9..4c73b29 100644 --- a/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskClient.java +++ b/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskClient.java @@ -429,167 +429,231 @@ public class GTaskClient { } /******/ + /** + * 创建任务 + * @param task 任务对象 + * @throws NetworkFailureException 网络异常 + */ public void createTask(Task task) throws NetworkFailureException { + // 提交更新 commitUpdate(); try { + // 创建JSONObject对象 JSONObject jsPost = new JSONObject(); + // 创建JSONArray对象 JSONArray actionList = new JSONArray(); - // action_list + // 将新建任务的操作添加到actionList中 actionList.put(task.getCreateAction(getActionId())); + // 将actionList添加到JSONObject中 jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); - // client_version + // 将客户端版本号添加到JSONObject中 jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); - // post + // 发送post请求,并获取响应数据 JSONObject jsResponse = postRequest(jsPost); + // 获取响应结果中的第一个JSONObject对象 JSONObject jsResult = (JSONObject) jsResponse.getJSONArray( GTaskStringUtils.GTASK_JSON_RESULTS).get(0); + // 将新建任务的gid设置为返回结果中的new_id task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); } catch (JSONException e) { + // 记录异常日志 Log.e(TAG, e.toString()); e.printStackTrace(); + // 抛出操作失败异常 throw new ActionFailureException("create task: handing jsonobject failed"); } } + /** + * 创建任务列表 + * @param tasklist 任务列表对象 + * @throws NetworkFailureException 网络异常 + */ public void createTaskList(TaskList tasklist) throws NetworkFailureException { + // 提交更新 commitUpdate(); try { + // 创建JSONObject对象 JSONObject jsPost = new JSONObject(); + // 创建JSONArray对象 JSONArray actionList = new JSONArray(); - // action_list + // 将新建任务列表的操作添加到actionList中 actionList.put(tasklist.getCreateAction(getActionId())); + // 将actionList添加到JSONObject中 jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); - // client version + // 将客户端版本号添加到JSONObject中 jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); - // post + // 发送post请求,并获取响应数据 JSONObject jsResponse = postRequest(jsPost); + // 获取响应结果中的第一个JSONObject对象 JSONObject jsResult = (JSONObject) jsResponse.getJSONArray( GTaskStringUtils.GTASK_JSON_RESULTS).get(0); + // 将新建任务列表的gid设置为返回结果中的new_id tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID)); } catch (JSONException e) { + // 记录异常日志 Log.e(TAG, e.toString()); e.printStackTrace(); + // 抛出操作失败异常 throw new ActionFailureException("create tasklist: handing jsonobject failed"); } } + + /** + * 提交更新 + * @throws NetworkFailureException 网络异常 + */ public void commitUpdate() throws NetworkFailureException { + // 如果有更新操作 if (mUpdateArray != null) { try { + // 创建JSONObject对象 JSONObject jsPost = new JSONObject(); - // action_list + // 将更新操作添加到actionList中 jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray); - // client_version + // 将客户端版本号添加到JSONObject中 jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + // 发送post请求 postRequest(jsPost); + // 更新操作数组置空 mUpdateArray = null; } catch (JSONException e) { + // 记录异常日志 Log.e(TAG, e.toString()); e.printStackTrace(); + // 抛出操作失败异常 throw new ActionFailureException("commit update: handing jsonobject failed"); } } } + + /** + * 添加更新节点到更新数组中。 + * @param node 要添加的节点。 + * @throws NetworkFailureException 网络故障异常。 + */ public void addUpdateNode(Node node) throws NetworkFailureException { if (node != null) { - // too many update items may result in an error - // set max to 10 items + // 如果更新数组中的项目太多可能会导致错误 + // 将最大项设置为10项 if (mUpdateArray != null && mUpdateArray.length() > 10) { - commitUpdate(); + commitUpdate(); // 提交更新数组 } if (mUpdateArray == null) - mUpdateArray = new JSONArray(); - mUpdateArray.put(node.getUpdateAction(getActionId())); + mUpdateArray = new JSONArray(); // 创建一个新的JSONArray + mUpdateArray.put(node.getUpdateAction(getActionId())); // 将节点的更新操作添加到JSONArray中 } } + /** + * 移动任务到指定的父任务列表中。 + * @param task 要移动的任务。 + * @param preParent 移动前的父任务列表。 + * @param curParent 移动后的父任务列表。 + * @throws NetworkFailureException 网络故障异常。 + */ public void moveTask(Task task, TaskList preParent, TaskList curParent) throws NetworkFailureException { - commitUpdate(); + commitUpdate(); // 提交更新数组 try { - JSONObject jsPost = new JSONObject(); + JSONObject jsPost = new JSONObject(); // 创建一个新的JSONObject JSONArray actionList = new JSONArray(); JSONObject action = new JSONObject(); // action_list action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, - GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE); - action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); - action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid()); + GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE); // 设置操作类型为移动 + action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId()); // 设置操作ID + action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid()); // 设置任务ID if (preParent == curParent && task.getPriorSibling() != null) { - // put prioring_sibing_id only if moving within the tasklist and - // it is not the first one + // 如果在同一个任务列表中移动且不是第一个,则设置prioring_sibing_id action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling()); } - action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); - action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); + action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); // 设置移动前的任务列表ID + action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); // 设置移动后的父任务列表ID if (preParent != curParent) { - // put the dest_list only if moving between tasklists + // 如果在不同的任务列表中移动,则设置dest_list action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid()); } - actionList.put(action); + actionList.put(action); // 将操作添加到操作列表中 jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // client_version - jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // 设置客户端版本号 - postRequest(jsPost); + postRequest(jsPost); // 发送POST请求 } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); - throw new ActionFailureException("move task: handing jsonobject failed"); + throw new ActionFailureException("move task: handing jsonobject failed"); // 抛出操作失败异常 } } + /** + * 删除节点。 + * @param node 要删除的节点。 + * @throws NetworkFailureException 网络故障异常。 + */ public void deleteNode(Node node) throws NetworkFailureException { - commitUpdate(); + commitUpdate(); // 提交更新数组 try { - JSONObject jsPost = new JSONObject(); + JSONObject jsPost = new JSONObject(); // 创建一个新的JSONObject JSONArray actionList = new JSONArray(); // action_list - node.setDeleted(true); - actionList.put(node.getUpdateAction(getActionId())); + node.setDeleted(true); // 设置节点为已删除 + actionList.put(node.getUpdateAction(getActionId())); // 将节点的更新操作添加到操作列表中 jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList); // client_version - jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); // 设置客户端版本号 + + postRequest(jsPost); // 发送POST请求 + mUpdateArray = null; // 将更新数组设置为null - postRequest(jsPost); - mUpdateArray = null; } catch (JSONException e) { Log.e(TAG, e.toString()); e.printStackTrace(); - throw new ActionFailureException("delete node: handing jsonobject failed"); + throw new ActionFailureException("delete node: handing jsonobject failed"); // 抛出操作失败异常 } } + + /** + * 获取任务列表 + * + * @return 任务列表的JSONArray对象 + * @throws NetworkFailureException 网络连接异常 + */ public JSONArray getTaskLists() throws NetworkFailureException { + // 如果未登录,则抛出异常 if (!mLoggedin) { Log.e(TAG, "please login first"); throw new ActionFailureException("not logged in"); } try { + // 创建HttpGet对象,用于向服务器发送GET请求 HttpGet httpGet = new HttpGet(mGetUrl); HttpResponse response = null; response = mHttpClient.execute(httpGet); - // get the task list + // 获取任务列表 String resString = getResponseContent(response.getEntity()); String jsBegin = "_setup("; String jsEnd = ")}"; @@ -602,23 +666,34 @@ public class GTaskClient { JSONObject js = new JSONObject(jsString); return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS); } catch (ClientProtocolException e) { + // 如果出现ClientProtocolException,则抛出网络连接异常 Log.e(TAG, e.toString()); e.printStackTrace(); throw new NetworkFailureException("gettasklists: httpget failed"); } catch (IOException e) { + // 如果出现IOException,则抛出网络连接异常 Log.e(TAG, e.toString()); e.printStackTrace(); throw new NetworkFailureException("gettasklists: httpget failed"); } catch (JSONException e) { + // 如果出现JSONException,则抛出操作失败异常 Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("get task lists: handing jasonobject failed"); } } + /** + * 根据任务列表ID获取任务列表 + * + * @param listGid 任务列表ID + * @return 任务列表的JSONArray对象 + * @throws NetworkFailureException 网络连接异常 + */ public JSONArray getTaskList(String listGid) throws NetworkFailureException { commitUpdate(); try { + // 创建JSONObject对象,设置请求参数 JSONObject jsPost = new JSONObject(); JSONArray actionList = new JSONArray(); JSONObject action = new JSONObject(); @@ -633,19 +708,29 @@ public class GTaskClient { jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion); + // 发送POST请求,获取响应JSONObject对象 JSONObject jsResponse = postRequest(jsPost); return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS); } catch (JSONException e) { + // 如果出现JSONException,则抛出操作失败异常 Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("get task list: handing jsonobject failed"); } } + /** + * 获取同步账户 + * + * @return 同步账户对象 + */ public Account getSyncAccount() { return mAccount; } + /** + * 重置更新数组 + */ public void resetUpdateArray() { mUpdateArray = null; } diff --git a/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskManager.java b/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskManager.java index d2b4082..8df0f83 100644 --- a/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskManager.java +++ b/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskManager.java @@ -51,111 +51,98 @@ import java.util.Map; public class GTaskManager { private static final String TAG = GTaskManager.class.getSimpleName(); - public static final int STATE_SUCCESS = 0; + public static final int STATE_SUCCESS = 0; // 同步成功状态码 + public static final int STATE_NETWORK_ERROR = 1; // 网络错误状态码 + public static final int STATE_INTERNAL_ERROR = 2; // 内部错误状态码 + public static final int STATE_SYNC_IN_PROGRESS = 3; // 同步进行中状态码 + public static final int STATE_SYNC_CANCELLED = 4; // 同步已取消状态码 - public static final int STATE_NETWORK_ERROR = 1; + private static GTaskManager mInstance = null; // 单例实例 - public static final int STATE_INTERNAL_ERROR = 2; + private Activity mActivity; // 活动的Activity实例 + private Context mContext; // 上下文 + private ContentResolver mContentResolver; // ContentResolver实例 - public static final int STATE_SYNC_IN_PROGRESS = 3; + private boolean mSyncing; // 是否正在同步 + private boolean mCancelled; // 是否已取消同步 - public static final int STATE_SYNC_CANCELLED = 4; - - private static GTaskManager mInstance = null; - - private Activity mActivity; - - private Context mContext; - - private ContentResolver mContentResolver; - - private boolean mSyncing; - - private boolean mCancelled; - - private HashMap mGTaskListHashMap; - - private HashMap mGTaskHashMap; - - private HashMap mMetaHashMap; - - private TaskList mMetaList; - - private HashSet mLocalDeleteIdMap; - - private HashMap mGidToNid; - - private HashMap mNidToGid; + private HashMap mGTaskListHashMap; // 任务列表哈希表 + private HashMap mGTaskHashMap; // 任务哈希表 + private HashMap mMetaHashMap; // 元数据哈希表 + private TaskList mMetaList; // 元数据列表 + private HashSet mLocalDeleteIdMap; // 本地删除ID哈希表 + private HashMap mGidToNid; // GID到NID的映射哈希表 + private HashMap mNidToGid; // NID到GID的映射哈希表 private GTaskManager() { - mSyncing = false; - mCancelled = false; - mGTaskListHashMap = new HashMap(); - mGTaskHashMap = new HashMap(); - mMetaHashMap = new HashMap(); - mMetaList = null; - mLocalDeleteIdMap = new HashSet(); - mGidToNid = new HashMap(); - mNidToGid = new HashMap(); + mSyncing = false; // 初始化时不在同步状态 + mCancelled = false; // 初始化时未取消同步 + mGTaskListHashMap = new HashMap(); // 初始化任务列表哈希表 + mGTaskHashMap = new HashMap(); // 初始化任务哈希表 + mMetaHashMap = new HashMap(); // 初始化元数据哈希表 + mMetaList = null; // 初始化元数据列表为空 + mLocalDeleteIdMap = new HashSet(); // 初始化本地删除ID哈希表为空 + mGidToNid = new HashMap(); // 初始化GID到NID的映射哈希表为空 + mNidToGid = new HashMap(); // 初始化NID到GID的映射哈希表为空 } - public static synchronized GTaskManager getInstance() { + public static synchronized GTaskManager getInstance() { // 获取单例实例 if (mInstance == null) { mInstance = new GTaskManager(); } return mInstance; } - public synchronized void setActivityContext(Activity activity) { - // used for getting authtoken + public synchronized void setActivityContext(Activity activity) { // 设置活动的Activity实例 + // 用于获取认证令牌 mActivity = activity; } public int sync(Context context, GTaskASyncTask asyncTask) { - if (mSyncing) { + if (mSyncing) { // 如果正在同步中,返回同步进行中状态码 Log.d(TAG, "Sync is in progress"); return STATE_SYNC_IN_PROGRESS; } - mContext = context; + mContext = context; // 设置上下文 mContentResolver = mContext.getContentResolver(); - mSyncing = true; - mCancelled = false; - mGTaskListHashMap.clear(); - mGTaskHashMap.clear(); - mMetaHashMap.clear(); - mLocalDeleteIdMap.clear(); - mGidToNid.clear(); - mNidToGid.clear(); + mSyncing = true; // 设置正在同步 + mCancelled = false; // 设置未取消同步 + mGTaskListHashMap.clear(); // 清除任务列表哈希表 + mGTaskHashMap.clear(); // 清除任务哈希表 + mMetaHashMap.clear(); // 清除元数据哈希表 + mLocalDeleteIdMap.clear(); // 清除本地删除ID哈希表 + mGidToNid.clear(); // 清除GID到NID的映射哈希表 + mNidToGid.clear(); // 清除NID到GID的映射哈希表 try { - GTaskClient client = GTaskClient.getInstance(); - client.resetUpdateArray(); + GTaskClient client = GTaskClient.getInstance(); // 获取GTaskClient实例 + client.resetUpdateArray(); // 重置更新数组 - // login google task + // 登录Google任务 if (!mCancelled) { - if (!client.login(mActivity)) { + if (!client.login(mActivity)) { // 如果登录失败,抛出NetworkFailureException throw new NetworkFailureException("login google task failed"); } } - // get the task list from google - asyncTask.publishProgess(mContext.getString(R.string.sync_progress_init_list)); + // 从Google获取任务列表 + asyncTask.publishProgess(mContext.getString(R.string.sync_progress_init_list)); // 发布进度消息 initGTaskList(); - // do content sync work - asyncTask.publishProgess(mContext.getString(R.string.sync_progress_syncing)); + // 进行内容同步 + asyncTask.publishProgess(mContext.getString(R.string.sync_progress_syncing)); // 发布进度消息 syncContent(); - } catch (NetworkFailureException e) { + } catch (NetworkFailureException e) { // 如果发生网络错误,返回网络错误状态码 Log.e(TAG, e.toString()); return STATE_NETWORK_ERROR; - } catch (ActionFailureException e) { + } catch (ActionFailureException e) { // 如果发生操作失败,返回内部错误状态码 Log.e(TAG, e.toString()); return STATE_INTERNAL_ERROR; - } catch (Exception e) { + } catch (Exception e) { // 如果发生其他异常,返回内部错误状态码 Log.e(TAG, e.toString()); e.printStackTrace(); return STATE_INTERNAL_ERROR; - } finally { + } finally { // 在finally中清除哈希表并设置同步完成 mGTaskListHashMap.clear(); mGTaskHashMap.clear(); mMetaHashMap.clear(); @@ -165,29 +152,38 @@ public class GTaskManager { mSyncing = false; } - return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS; + return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS; // 如果已取消同步,返回取消同步状态码,否则返回成功状态码 } + /** + * 初始化 GTaskList,如果网络连接失败则抛出 NetworkFailureException 异常。 + */ private void initGTaskList() throws NetworkFailureException { + // 如果已经取消则直接返回 if (mCancelled) return; + + // 获取 GTaskClient 的实例 GTaskClient client = GTaskClient.getInstance(); + try { + // 获取任务列表 JSONArray jsTaskLists = client.getTaskLists(); - // init meta list first + // 首先初始化 Meta list mMetaList = null; for (int i = 0; i < jsTaskLists.length(); i++) { + // 获取任务列表中的元素 JSONObject object = jsTaskLists.getJSONObject(i); String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID); String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME); - if (name - .equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) { + // 如果名称为 GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META,则初始化 Meta list + if (name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) { mMetaList = new TaskList(); mMetaList.setContentByRemoteJSON(object); - // load meta data + // 加载 Meta 数据 JSONArray jsMetas = client.getTaskList(gid); for (int j = 0; j < jsMetas.length(); j++) { object = (JSONObject) jsMetas.getJSONObject(j); @@ -203,29 +199,28 @@ public class GTaskManager { } } - // create meta list if not existed + // 如果 Meta list 不存在,则创建一个新的 Meta list if (mMetaList == null) { mMetaList = new TaskList(); - mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX - + GTaskStringUtils.FOLDER_META); + mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META); GTaskClient.getInstance().createTaskList(mMetaList); } - // init task list + // 初始化任务列表 for (int i = 0; i < jsTaskLists.length(); i++) { + // 获取任务列表中的元素 JSONObject object = jsTaskLists.getJSONObject(i); String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID); String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME); - if (name.startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX) - && !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX - + GTaskStringUtils.FOLDER_META)) { + // 如果名称以 GTaskStringUtils.MIUI_FOLDER_PREFFIX 开头且不是 Meta list,则初始化任务列表 + if (name.startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX) && !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) { TaskList tasklist = new TaskList(); tasklist.setContentByRemoteJSON(object); mGTaskListHashMap.put(gid, tasklist); mGTaskHashMap.put(gid, tasklist); - // load tasks + // 加载任务 JSONArray jsTasks = client.getTaskList(gid); for (int j = 0; j < jsTasks.length(); j++) { object = (JSONObject) jsTasks.getJSONObject(j); @@ -241,25 +236,27 @@ public class GTaskManager { } } } catch (JSONException e) { + // 如果处理 JSONObject 失败,则抛出 ActionFailureException 异常 Log.e(TAG, e.toString()); e.printStackTrace(); throw new ActionFailureException("initGTaskList: handing JSONObject failed"); } } + private void syncContent() throws NetworkFailureException { - int syncType; - Cursor c = null; - String gid; - Node node; + int syncType; // 同步类型 + Cursor c = null; // 游标 + String gid; // GTasks ID + Node node; // 节点 - mLocalDeleteIdMap.clear(); + mLocalDeleteIdMap.clear(); // 清空本地删除ID映射表 - if (mCancelled) { + if (mCancelled) { // 如果已经取消了同步,直接返回 return; } - // for local deleted note + // 处理本地已删除的笔记 try { c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(type<>? AND parent_id=?)", new String[] { @@ -271,13 +268,13 @@ public class GTaskManager { node = mGTaskHashMap.get(gid); if (node != null) { mGTaskHashMap.remove(gid); - doContentSync(Node.SYNC_ACTION_DEL_REMOTE, node, c); + doContentSync(Node.SYNC_ACTION_DEL_REMOTE, node, c); // 执行删除远程同步操作 } - mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN)); + mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN)); // 添加到本地删除ID映射表 } } else { - Log.w(TAG, "failed to query trash folder"); + Log.w(TAG, "failed to query trash folder"); // 查询回收站失败 } } finally { if (c != null) { @@ -286,10 +283,10 @@ public class GTaskManager { } } - // sync folder first + // 同步文件夹 syncFolder(); - // for note existing in database + // 处理数据库中已存在的笔记 try { c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(type=? AND parent_id<>?)", new String[] { @@ -303,20 +300,20 @@ public class GTaskManager { mGTaskHashMap.remove(gid); mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN)); mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid); - syncType = node.getSyncAction(c); + syncType = node.getSyncAction(c); // 获取同步类型 } else { if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) { - // local add + // 本地新增 syncType = Node.SYNC_ACTION_ADD_REMOTE; } else { - // remote delete + // 远程删除 syncType = Node.SYNC_ACTION_DEL_LOCAL; } } - doContentSync(syncType, node, c); + doContentSync(syncType, node, c); // 执行同步操作 } } else { - Log.w(TAG, "failed to query existing note in database"); + Log.w(TAG, "failed to query existing note in database"); // 查询已存在的笔记失败 } } finally { @@ -326,42 +323,43 @@ public class GTaskManager { } } - // go through remaining items + // 处理剩余的节点 Iterator> iter = mGTaskHashMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = iter.next(); node = entry.getValue(); - doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null); + doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null); // 执行本地新增同步操作 } - // mCancelled can be set by another thread, so we neet to check one by - // one - // clear local delete table + // 清空本地删除表 if (!mCancelled) { - if (!DataUtils.batchDeleteNotes(mContentResolver, mLocalDeleteIdMap)) { + if (!DataUtils.batchDeleteNotes(mContentResolver, mLocalDeleteIdMap)) { // 批量删除本地已删除笔记 throw new ActionFailureException("failed to batch-delete local deleted notes"); } } - // refresh local sync id + // 刷新本地同步ID if (!mCancelled) { - GTaskClient.getInstance().commitUpdate(); - refreshLocalSyncId(); + GTaskClient.getInstance().commitUpdate(); // 提交更新 + refreshLocalSyncId(); // 刷新本地同步ID } - } + /** + * 同步文件夹信息 + * @throws NetworkFailureException 如果网络连接失败,则抛出此异常 + */ private void syncFolder() throws NetworkFailureException { - Cursor c = null; - String gid; - Node node; - int syncType; + Cursor c = null; // 游标对象,用于查询数据库 + String gid; // Google Tasks ID + Node node; // Google Tasks 中的节点 + int syncType; // 同步类型 - if (mCancelled) { + if (mCancelled) { // 如果已取消同步,则直接返回 return; } - // for root folder + // 同步根文件夹 try { c = mContentResolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, Notes.ID_ROOT_FOLDER), SqlNote.PROJECTION_NOTE, null, null, null); @@ -369,15 +367,15 @@ public class GTaskManager { c.moveToNext(); gid = c.getString(SqlNote.GTASK_ID_COLUMN); node = mGTaskHashMap.get(gid); - if (node != null) { + if (node != null) { // Google Tasks 中存在该节点 mGTaskHashMap.remove(gid); mGidToNid.put(gid, (long) Notes.ID_ROOT_FOLDER); mNidToGid.put((long) Notes.ID_ROOT_FOLDER, gid); - // for system folder, only update remote name if necessary + // 如果节点名称不是默认名称,则更新远程名称 if (!node.getName().equals( GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT)) doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c); - } else { + } else { // Google Tasks 中不存在该节点 doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c); } } else { @@ -390,27 +388,26 @@ public class GTaskManager { } } - // for call-note folder + // 同步通话记录文件夹 try { c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(_id=?)", new String[] { - String.valueOf(Notes.ID_CALL_RECORD_FOLDER) + String.valueOf(Notes.ID_CALL_RECORD_FOLDER) }, null); if (c != null) { if (c.moveToNext()) { gid = c.getString(SqlNote.GTASK_ID_COLUMN); node = mGTaskHashMap.get(gid); - if (node != null) { + if (node != null) { // Google Tasks 中存在该节点 mGTaskHashMap.remove(gid); mGidToNid.put(gid, (long) Notes.ID_CALL_RECORD_FOLDER); mNidToGid.put((long) Notes.ID_CALL_RECORD_FOLDER, gid); - // for system folder, only update remote name if - // necessary + // 如果节点名称不是默认名称,则更新远程名称 if (!node.getName().equals( GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE)) doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c); - } else { + } else { // Google Tasks 中不存在该节点 doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c); } } @@ -424,7 +421,7 @@ public class GTaskManager { } } - // for local existing folders + // 同步本地已存在文件夹 try { c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(type=? AND parent_id<>?)", new String[] { @@ -434,17 +431,17 @@ public class GTaskManager { while (c.moveToNext()) { gid = c.getString(SqlNote.GTASK_ID_COLUMN); node = mGTaskHashMap.get(gid); - if (node != null) { + if (node != null) { // Google Tasks 中存在该节点 mGTaskHashMap.remove(gid); mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN)); mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid); syncType = node.getSyncAction(c); } else { if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) { - // local add + // 本地新增 syncType = Node.SYNC_ACTION_ADD_REMOTE; } else { - // remote delete + // 远程删除 syncType = Node.SYNC_ACTION_DEL_LOCAL; } } @@ -460,22 +457,30 @@ public class GTaskManager { } } - // for remote add folders - Iterator> iter = mGTaskListHashMap.entrySet().iterator(); - while (iter.hasNext()) { - Map.Entry entry = iter.next(); - gid = entry.getKey(); - node = entry.getValue(); - if (mGTaskHashMap.containsKey(gid)) { - mGTaskHashMap.remove(gid); - doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null); + // 为远程添加文件夹 + Iterator> iter = mGTaskListHashMap.entrySet().iterator(); // 迭代 mGTaskListHashMap 中的每一个元素 + while (iter.hasNext()) { // 如果还有下一个元素 + Map.Entry entry = iter.next(); // 获取下一个元素 + gid = entry.getKey(); // 获取元素的键 + node = entry.getValue(); // 获取元素的值 + if (mGTaskHashMap.containsKey(gid)) { // 如果 mGTaskHashMap 中包含该键 + mGTaskHashMap.remove(gid); // 从 mGTaskHashMap 中删除该键值对 + doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null); // 执行 doContentSync 方法,并传入 SYNC_ACTION_ADD_LOCAL、node 和 null 三个参数 } } - if (!mCancelled) - GTaskClient.getInstance().commitUpdate(); + if (!mCancelled) // 如果 mCancelled 为假 + GTaskClient.getInstance().commitUpdate(); // 执行 GTaskClient.getInstance().commitUpdate() 方法 } + /** + * 执行内容同步 + * + * @param syncType 同步类型 + * @param node 节点 + * @param c 光标 + * @throws NetworkFailureException 网络失败异常 + */ private void doContentSync(int syncType, Node node, Cursor c) throws NetworkFailureException { if (mCancelled) { return; @@ -484,12 +489,15 @@ public class GTaskManager { MetaData meta; switch (syncType) { case Node.SYNC_ACTION_ADD_LOCAL: + // 添加本地节点 addLocalNode(node); break; case Node.SYNC_ACTION_ADD_REMOTE: + // 添加远程节点 addRemoteNode(node, c); break; case Node.SYNC_ACTION_DEL_LOCAL: + // 删除本地节点 meta = mMetaHashMap.get(c.getString(SqlNote.GTASK_ID_COLUMN)); if (meta != null) { GTaskClient.getInstance().deleteNode(meta); @@ -497,6 +505,7 @@ public class GTaskManager { mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN)); break; case Node.SYNC_ACTION_DEL_REMOTE: + // 删除远程节点 meta = mMetaHashMap.get(node.getGid()); if (meta != null) { GTaskClient.getInstance().deleteNode(meta); @@ -504,52 +513,69 @@ public class GTaskManager { GTaskClient.getInstance().deleteNode(node); break; case Node.SYNC_ACTION_UPDATE_LOCAL: + // 更新本地节点 updateLocalNode(node, c); break; case Node.SYNC_ACTION_UPDATE_REMOTE: + // 更新远程节点 updateRemoteNode(node, c); break; case Node.SYNC_ACTION_UPDATE_CONFLICT: - // merging both modifications maybe a good idea - // right now just use local update simply + // 合并两个修改可能是个好主意 + // 目前只使用本地更新 updateRemoteNode(node, c); break; case Node.SYNC_ACTION_NONE: + // 无同步操作 break; case Node.SYNC_ACTION_ERROR: default: - throw new ActionFailureException("unkown sync action type"); + // 未知同步操作类型异常 + throw new ActionFailureException("未知的同步操作类型"); } } + /** + * 添加本地节点,将传入的节点信息添加到本地数据库中 + * + * @param node 要添加的节点对象 + * @throws NetworkFailureException 如果添加过程中发生网络错误,抛出此异常 + */ private void addLocalNode(Node node) throws NetworkFailureException { + // 如果已经取消了添加操作,则直接返回 if (mCancelled) { return; } SqlNote sqlNote; + // 根据节点类型创建要添加的节点对象 if (node instanceof TaskList) { if (node.getName().equals( GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT)) { + // 如果是默认文件夹,则将其作为根节点添加 sqlNote = new SqlNote(mContext, Notes.ID_ROOT_FOLDER); } else if (node.getName().equals( GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE)) { + // 如果是通话记录文件夹,则将其作为通话记录文件夹添加 sqlNote = new SqlNote(mContext, Notes.ID_CALL_RECORD_FOLDER); } else { + // 否则创建一个普通的节点对象,将传入的节点信息添加到其中 sqlNote = new SqlNote(mContext); sqlNote.setContent(node.getLocalJSONFromContent()); sqlNote.setParentId(Notes.ID_ROOT_FOLDER); } } else { + // 创建一个普通的节点对象,将传入的节点信息添加到其中 sqlNote = new SqlNote(mContext); JSONObject js = node.getLocalJSONFromContent(); try { + // 更新节点信息中的元数据信息 if (js.has(GTaskStringUtils.META_HEAD_NOTE)) { JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); if (note.has(NoteColumns.ID)) { long id = note.getLong(NoteColumns.ID); if (DataUtils.existInNoteDatabase(mContentResolver, id)) { - // the id is not available, have to create a new one + // 如果该id已存在于本地数据库中,则需要创建一个新的id note.remove(NoteColumns.ID); } } @@ -562,91 +588,110 @@ public class GTaskManager { if (data.has(DataColumns.ID)) { long dataId = data.getLong(DataColumns.ID); if (DataUtils.existInDataDatabase(mContentResolver, dataId)) { - // the data id is not available, have to create - // a new one + // 如果该id已存在于本地数据库中,则需要创建一个新的id data.remove(DataColumns.ID); } } } - } } catch (JSONException e) { Log.w(TAG, e.toString()); e.printStackTrace(); } + // 将更新后的节点信息添加到要创建的节点对象中 sqlNote.setContent(js); + // 获取要添加的节点的父节点在本地数据库中的id Long parentId = mGidToNid.get(((Task) node).getParent().getGid()); if (parentId == null) { + // 如果没有找到父节点的id,则抛出异常 Log.e(TAG, "cannot find task's parent id locally"); throw new ActionFailureException("cannot add local node"); } + // 将父节点的id添加到要创建的节点对象中 sqlNote.setParentId(parentId.longValue()); } - // create the local node + // 设置要创建的节点对象的gid sqlNote.setGtaskId(node.getGid()); + // 将要创建的节点对象添加到本地数据库中 sqlNote.commit(false); - // update gid-nid mapping + // 更新gid-nid映射表 mGidToNid.put(node.getGid(), sqlNote.getId()); mNidToGid.put(sqlNote.getId(), node.getGid()); - // update meta + // 更新元数据信息 updateRemoteMeta(node.getGid(), sqlNote); } + /** + * 更新本地节点,将传入的节点信息更新到本地数据库中 + * + * @param node 要更新的节点对象 + * @param c 数据库游标对象,用于获取要更新的节点的信息 + * @throws NetworkFailureException 如果更新过程中发生网络错误,抛出此异常 + */ private void updateLocalNode(Node node, Cursor c) throws NetworkFailureException { + // 如果已经取消了更新操作,则直接返回 if (mCancelled) { return; } SqlNote sqlNote; - // update the note locally + // 根据游标对象获取要更新的节点的信息 sqlNote = new SqlNote(mContext, c); + // 将传入的节点信息更新到要更新的节点中 sqlNote.setContent(node.getLocalJSONFromContent()); + // 根据节点类型获取要更新节点的父节点的ID Long parentId = (node instanceof Task) ? mGidToNid.get(((Task) node).getParent().getGid()) : new Long(Notes.ID_ROOT_FOLDER); + // 如果父节点ID为空,则抛出异常 if (parentId == null) { Log.e(TAG, "cannot find task's parent id locally"); throw new ActionFailureException("cannot update local node"); } + // 设置要更新节点的父节点ID sqlNote.setParentId(parentId.longValue()); + // 提交更新操作 sqlNote.commit(true); - // update meta info + // 更新远程节点的元数据信息 updateRemoteMeta(node.getGid(), sqlNote); } + //这段Java代码实现了一个添加远程节点的方法,下面我们逐行进行详细的注释说明: private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException { + //这是一个私有方法,用于添加远程节点。它需要传入一个节点对象和一个光标对象。如果添加过程中出现网络失败,将会抛出 + // NetworkFailureException 异常。 if (mCancelled) { return; } - + //如果任务已被取消,直接返回。 SqlNote sqlNote = new SqlNote(mContext, c); Node n; - - // update remotely + //创建一个 SqlNote 对象和一个 Node 对象。 if (sqlNote.isNoteType()) { Task task = new Task(); task.setContentByLocalJSON(sqlNote.getContent()); - + //如果 SqlNote 对象表示的是笔记类型,则创建一个 Task 对象,并使用 SqlNote 对象的 getContent() 方法获取本地 JSON 内容, + // 将其设置为 Task 对象的内容。 String parentGid = mNidToGid.get(sqlNote.getParentId()); if (parentGid == null) { Log.e(TAG, "cannot find task's parent tasklist"); throw new ActionFailureException("cannot add remote task"); } mGTaskListHashMap.get(parentGid).addChildTask(task); - + //获取该笔记的父级任务列表的 GID,如果不存在则抛出异常。然后将该任务添加到父级任务列表中。 GTaskClient.getInstance().createTask(task); n = (Node) task; - - // add meta + //使用 GTaskClient 创建该任务,并将其转换为 Node 对象。 updateRemoteMeta(task.getGid(), sqlNote); + //更新远程元数据。 } else { TaskList tasklist = null; - + //如果 SqlNote 对象表示的是文件夹类型,则创建一个 TaskList 对象。 // we need to skip folder if it has already existed String folderName = GTaskStringUtils.MIUI_FOLDER_PREFFIX; if (sqlNote.getId() == Notes.ID_ROOT_FOLDER) @@ -655,7 +700,7 @@ public class GTaskManager { folderName += GTaskStringUtils.FOLDER_CALL_NOTE; else folderName += sqlNote.getSnippet(); - + //获取文件夹的名称。如果是根文件夹,则使用默认名称;如果是通话记录文件夹,则使用特定的名称;否则使用 SqlNote 对象的摘录作为名称。 Iterator> iter = mGTaskListHashMap.entrySet().iterator(); while (iter.hasNext()) { Map.Entry entry = iter.next(); @@ -670,7 +715,7 @@ public class GTaskManager { break; } } - + //遍历任务列表哈希表,查找是否存在同名任务列表。如果存在,则将其作为 tasklist 对象;否则 tasklist 对象为空。 // no match we can add now if (tasklist == null) { tasklist = new TaskList(); @@ -679,38 +724,60 @@ public class GTaskManager { mGTaskListHashMap.put(tasklist.getGid(), tasklist); } n = (Node) tasklist; + //如果 tasklist 对象为空,则创建新的任务列表,并使用本地 JSON 内容初始化其内容。 + // 然后使用 GTaskClient 创建该任务列表,并将其添加到任务列表哈希表中。然后将 tasklist 转换为 Node 对象。 } - // update local note sqlNote.setGtaskId(n.getGid()); sqlNote.commit(false); sqlNote.resetLocalModified(); sqlNote.commit(true); - - // gid-id mapping + //更新本地笔记的 GID,并将其标记为已提交。 mGidToNid.put(n.getGid(), sqlNote.getId()); mNidToGid.put(sqlNote.getId(), n.getGid()); + //更新 GID 和 ID 之间的映射。 } + //这段Java代码的主要作用是更新远程云端的节点和元数据。 + /** 具体而言,代码的执行流程如下: + + 1. 方法`updateRemoteNode()`的参数`node`表示需要更新的节点对象,参数`c`表示查询结果的`Cursor`对象。 + 2. 首先判断`mCancelled`是否为`true`,如果是则直接返回。 + 3. 根据`Cursor`对象创建`SqlNote`对象。 + 4. 调用`node`的`setContentByLocalJSON()`方法更新节点的内容。 + 5. 将更新后的节点添加到`GTaskClient`任务列表中。 + 6. 调用`updateRemoteMeta()`方法更新元数据。 + 7. 如果需要移动任务,则更新任务的父任务列表。 + 8. 清除本地修改标志。 + 9. 方法`updateRemoteMeta()`的参数`gid`表示元数据的唯一标识符,参数`sqlNote`表示需要更新的`SqlNote`对象。 + 10. 首先判断`sqlNote`是否为`null`且是否是笔记类型,如果不是则直接返回。 + 11. 获取元数据对象。 + 12. 如果元数据对象已存在,则更新元数据并将其添加到`GTaskClient`任务列表中。 + 13. 如果元数据对象不存在,则创建新的元数据对象并添加到任务列表中。 + */ private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException { if (mCancelled) { return; } + // 根据Cursor对象创建SqlNote对象 SqlNote sqlNote = new SqlNote(mContext, c); - // update remotely + // 更新节点的内容 node.setContentByLocalJSON(sqlNote.getContent()); + + // 将更新后的节点添加到GTaskClient任务列表中 GTaskClient.getInstance().addUpdateNode(node); - // update meta + // 更新元数据 updateRemoteMeta(node.getGid(), sqlNote); - // move task if necessary + // 如果需要移动任务,则更新任务的父任务列表 if (sqlNote.isNoteType()) { Task task = (Task) node; TaskList preParentList = task.getParent(); + // 获取当前任务的父任务列表 String curParentGid = mNidToGid.get(sqlNote.getParentId()); if (curParentGid == null) { Log.e(TAG, "cannot find task's parent tasklist"); @@ -718,6 +785,7 @@ public class GTaskManager { } TaskList curParentList = mGTaskListHashMap.get(curParentGid); + // 如果当前任务的父任务列表和之前的不同,则移动任务到新的父任务列表中 if (preParentList != curParentList) { preParentList.removeChildTask(task); curParentList.addChildTask(task); @@ -725,18 +793,22 @@ public class GTaskManager { } } - // clear local modified flag + // 清除本地修改标志 sqlNote.resetLocalModified(); sqlNote.commit(true); } private void updateRemoteMeta(String gid, SqlNote sqlNote) throws NetworkFailureException { if (sqlNote != null && sqlNote.isNoteType()) { + // 获取元数据对象 MetaData metaData = mMetaHashMap.get(gid); if (metaData != null) { + // 更新元数据 metaData.setMeta(gid, sqlNote.getContent()); + // 将更新后的元数据添加到GTaskClient任务列表中 GTaskClient.getInstance().addUpdateNode(metaData); } else { + // 创建新的元数据对象并添加到任务列表中 metaData = new MetaData(); metaData.setMeta(gid, sqlNote.getContent()); mMetaList.addChildTask(metaData); @@ -746,12 +818,17 @@ public class GTaskManager { } } + + /** + * 刷新本地同步ID + * @throws NetworkFailureException 网络异常 + */ private void refreshLocalSyncId() throws NetworkFailureException { if (mCancelled) { return; } - // get the latest gtask list + // 获取最新的gtask列表 mGTaskHashMap.clear(); mGTaskListHashMap.clear(); mMetaHashMap.clear(); @@ -759,6 +836,7 @@ public class GTaskManager { Cursor c = null; try { + // 查询本地笔记,排除系统笔记和回收站笔记 c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(type<>? AND parent_id<>?)", new String[] { String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER) @@ -790,10 +868,17 @@ public class GTaskManager { } } + /** + * 获取同步账户 + * @return 同步账户的名称 + */ public String getSyncAccount() { return GTaskClient.getInstance().getSyncAccount().name; } + /** + * 取消同步 + */ public void cancelSync() { mCancelled = true; } diff --git a/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskSyncService.java b/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskSyncService.java index cca36f7..0101849 100644 --- a/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskSyncService.java +++ b/代码解读/MiNotes-master/app/src/main/java/net/micode/notes/gtask/remote/GTaskSyncService.java @@ -23,25 +23,36 @@ import android.content.Intent; import android.os.Bundle; import android.os.IBinder; +// GTaskSyncService 类,继承自 Service 类,用于同步任务 public class GTaskSyncService extends Service { + // 定义动作类型字符串常量 public final static String ACTION_STRING_NAME = "sync_action_type"; + // 定义开始同步动作常量 public final static int ACTION_START_SYNC = 0; + // 定义取消同步动作常量 public final static int ACTION_CANCEL_SYNC = 1; + // 定义无效动作常量 public final static int ACTION_INVALID = 2; + // 定义广播名称字符串常量 public final static String GTASK_SERVICE_BROADCAST_NAME = "net.micode.notes.gtask.remote.gtask_sync_service"; + // 定义广播是否正在同步字符串常量 public final static String GTASK_SERVICE_BROADCAST_IS_SYNCING = "isSyncing"; + // 定义广播进度消息字符串常量 public final static String GTASK_SERVICE_BROADCAST_PROGRESS_MSG = "progressMsg"; + // 定义静态的 GTaskASyncTask 对象 private static GTaskASyncTask mSyncTask = null; + // 定义静态的同步进度字符串 private static String mSyncProgress = ""; + // 开始同步的方法 private void startSync() { if (mSyncTask == null) { mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() { @@ -56,17 +67,20 @@ public class GTaskSyncService extends Service { } } + // 取消同步的方法 private void cancelSync() { if (mSyncTask != null) { mSyncTask.cancelSync(); } } + // 重写 onCreate 方法 @Override public void onCreate() { mSyncTask = null; } + // 重写 onStartCommand 方法 @Override public int onStartCommand(Intent intent, int flags, int startId) { Bundle bundle = intent.getExtras(); @@ -86,6 +100,7 @@ public class GTaskSyncService extends Service { return super.onStartCommand(intent, flags, startId); } + // 重写 onLowMemory 方法 @Override public void onLowMemory() { if (mSyncTask != null) { @@ -93,10 +108,12 @@ public class GTaskSyncService extends Service { } } + // 重写 onBind 方法 public IBinder onBind(Intent intent) { return null; } + // 发送广播的方法 public void sendBroadcast(String msg) { mSyncProgress = msg; Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME); @@ -105,6 +122,7 @@ public class GTaskSyncService extends Service { sendBroadcast(intent); } + // 开始同步的静态方法 public static void startSync(Activity activity) { GTaskManager.getInstance().setActivityContext(activity); Intent intent = new Intent(activity, GTaskSyncService.class); @@ -112,16 +130,19 @@ public class GTaskSyncService extends Service { activity.startService(intent); } + // 取消同步的静态方法 public static void cancelSync(Context context) { Intent intent = new Intent(context, GTaskSyncService.class); intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC); context.startService(intent); } + // 判断是否正在同步的静态方法 public static boolean isSyncing() { return mSyncTask != null; } + // 获取进度字符串的静态方法 public static String getProgressString() { return mSyncProgress; }