From 99e1ac9d40616ceb6653d7258931ba4bce9e2fc3 Mon Sep 17 00:00:00 2001 From: HBK <694985294@qq.com> Date: Fri, 6 Dec 2024 17:09:39 +0800 Subject: [PATCH] 2 --- src/net/micode/notes/gtask/data/MetaData.java | 61 ++++++++++++++-- src/net/micode/notes/gtask/data/Node.java | 23 +++--- src/net/micode/notes/gtask/data/SqlData.java | 71 +++++++++++++++++-- src/net/micode/notes/gtask/data/SqlNote.java | 24 ++++++- src/net/micode/notes/gtask/data/TaskList.java | 68 ++++++++++++++++-- 5 files changed, 218 insertions(+), 29 deletions(-) diff --git a/src/net/micode/notes/gtask/data/MetaData.java b/src/net/micode/notes/gtask/data/MetaData.java index 3a2050b..b1787b9 100644 --- a/src/net/micode/notes/gtask/data/MetaData.java +++ b/src/net/micode/notes/gtask/data/MetaData.java @@ -26,28 +26,54 @@ import org.json.JSONObject; public class MetaData extends Task { + /* + * 功能描述:得到类的简写名称存入字符串TAG中 + * 实现过程:调用getSimpleName ()函数 + */ private final static String TAG = MetaData.class.getSimpleName(); private String mRelatedGid = null; - + /* + * 功能描述:设置数据,即生成元数据库 + * 实现过程:调用JSONObject库函数put (),Task类中的setNotes ()和setName ()函数 + * 参数注解: + */ public void setMeta(String gid, JSONObject metaInfo) { + + //对函数块进行注释 try { metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid); + /* + * 将这对键值放入metaInfo这个jsonobject对象中 + */ } catch (JSONException e) { Log.e(TAG, "failed to put related gid"); + /* + * 输出错误信息 + */ } setNotes(metaInfo.toString()); setName(GTaskStringUtils.META_NOTE_NAME); } - + /* + * 功能描述:获取相关联的Gid + */ public String getRelatedGid() { return mRelatedGid; } - + /* + * 功能描述:判断当前数据是否为空,若为空则返回真即值得保存 + * Made By CuiCan + */ @Override public boolean isWorthSaving() { return getNotes() != null; } + /* + * 功能描述:使用远程json数据对象设置元数据内容 + * 实现过程:调用父类Task中的setContentByRemoteJSON ()函数,并 + * 参数注解: + */ @Override public void setContentByRemoteJSON(JSONObject js) { @@ -58,25 +84,50 @@ public class MetaData extends Task { mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); } catch (JSONException e) { Log.w(TAG, "failed to get related gid"); + + /* + * 输出警告信息 + */ mRelatedGid = null; } } } + /* + * 功能描述:使用本地json数据对象设置元数据内容,一般不会用到,若用到,则抛出异常 + * Made By CuiCan + */ @Override public void setContentByLocalJSON(JSONObject js) { // this function should not be called throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called"); + /* + * 传递非法参数异常 + */ } - + /* + * 功能描述:从元数据内容中获取本地json对象,一般不会用到,若用到,则抛出异常 + * Made By CuiCan + */ @Override public JSONObject getLocalJSONFromContent() { throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called"); + /* + * 传递非法参数异常 + * Made By Cui Can + */ } - + /* + * 功能描述:获取同步动作状态,一般不会用到,若用到,则抛出异常 + * Made By CuiCan + */ @Override public int getSyncAction(Cursor c) { throw new IllegalAccessError("MetaData:getSyncAction should not be called"); } + /* + * 传递非法参数异常 + * Made By Cui Can + */ } diff --git a/src/net/micode/notes/gtask/data/Node.java b/src/net/micode/notes/gtask/data/Node.java index 63950e0..26090da 100644 --- a/src/net/micode/notes/gtask/data/Node.java +++ b/src/net/micode/notes/gtask/data/Node.java @@ -21,31 +21,32 @@ import android.database.Cursor; import org.json.JSONObject; public abstract class Node { - public static final int SYNC_ACTION_NONE = 0; + //定义了各种用于表征同步状态的常量 + public static final int SYNC_ACTION_NONE = 0;// 本地和云端都无可更新内容(即本地和云端内容一致) - public static final int SYNC_ACTION_ADD_REMOTE = 1; + public static final int SYNC_ACTION_ADD_REMOTE = 1;// 需要在远程云端增加内容 - public static final int SYNC_ACTION_ADD_LOCAL = 2; + 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_REMOTE = 3;// 需要在本地增加内容 - public static final int SYNC_ACTION_DEL_LOCAL = 4; + 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_REMOTE = 5;// 需要在本地删除内容 - public static final int SYNC_ACTION_UPDATE_LOCAL = 6; + public static final int SYNC_ACTION_UPDATE_LOCAL = 6;// 需要将本地内容更新到远程云端 - public static final int SYNC_ACTION_UPDATE_CONFLICT = 7; + public static final int SYNC_ACTION_UPDATE_CONFLICT = 7;// 需要将远程云端内容更新到本地 - public static final int SYNC_ACTION_ERROR = 8; + public static final int SYNC_ACTION_ERROR = 8;// 同步出现错误 private String mGid; private String mName; - private long mLastModified; + private long mLastModified;//记录最后一次修改时间 - private boolean mDeleted; + private boolean mDeleted;//记录最后一次修改时间 public Node() { mGid = null; diff --git a/src/net/micode/notes/gtask/data/SqlData.java b/src/net/micode/notes/gtask/data/SqlData.java index d3ec3be..f50f2f4 100644 --- a/src/net/micode/notes/gtask/data/SqlData.java +++ b/src/net/micode/notes/gtask/data/SqlData.java @@ -14,8 +14,16 @@ * limitations under the License. */ +/* + * Description:用于支持小米便签最底层的数据库相关操作,和sqlnote的关系上是子集关系,即data是note的子集(节点)。 + * SqlData其实就是也就是所谓数据中的数据 + */ package net.micode.notes.gtask.data; +/* + * Description:用于支持小米便签最底层的数据库相关操作,和sqlnote的关系上是子集关系,即data是note的子集(节点)。 + * SqlData其实就是也就是所谓数据中的数据 + */ import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; @@ -36,15 +44,29 @@ import org.json.JSONObject; public class SqlData { + + /* + * 功能描述:得到类的简写名称存入字符串TAG中 + * 实现过程:调用getSimpleName ()函数 + * Made By CuiCan + */ private static final String TAG = SqlData.class.getSimpleName(); private static final int INVALID_ID = -99999; + /** + * 来自Notes类中定义的DataColumn中的一些常量 + */ + + // 集合了interface DataColumns中所有SF常量 public static final String[] PROJECTION_DATA = new String[] { DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1, DataColumns.DATA3 }; + /** + * 以下五个变量作为sql表中5列的编号 + */ public static final int DATA_ID_COLUMN = 0; public static final int DATA_MIME_TYPE_COLUMN = 1; @@ -56,7 +78,7 @@ public class SqlData { public static final int DATA_CONTENT_DATA_3_COLUMN = 4; private ContentResolver mContentResolver; - + //判断是否直接用Content生成,是为true,否则为false private boolean mIsCreate; private long mDataId; @@ -70,11 +92,17 @@ public class SqlData { private String mDataContentData3; private ContentValues mDiffDataValues; - + /* + * 功能描述:构造函数,用于初始化数据 + * 参数注解:mContentResolver用于获取ContentProvider提供的数据 + * 参数注解: mIsCreate表征当前数据是用哪种方式创建(两种构造函数的参数不同) + * 参数注解: + * Made By CuiCan + */ public SqlData(Context context) { mContentResolver = context.getContentResolver(); mIsCreate = true; - mDataId = INVALID_ID; + mDataId = INVALID_ID;//mDataId置初始值-99999 mDataMimeType = DataConstants.NOTE; mDataContent = ""; mDataContentData1 = 0; @@ -82,6 +110,13 @@ public class SqlData { mDiffDataValues = new ContentValues(); } + /* + * 功能描述:构造函数,初始化数据 + * 参数注解:mContentResolver用于获取ContentProvider提供的数据 + * 参数注解: mIsCreate表征当前数据是用哪种方式创建(两种构造函数的参数不同) + * 参数注解: + * Made By CuiCan + */ public SqlData(Context context, Cursor c) { mContentResolver = context.getContentResolver(); mIsCreate = false; @@ -89,6 +124,11 @@ public class SqlData { mDiffDataValues = new ContentValues(); } + /* + * 功能描述:从光标处加载数据 + * 从当前的光标处将五列的数据加载到该类的对象 + * Made By CuiCan + */ private void loadFromCursor(Cursor c) { mDataId = c.getLong(DATA_ID_COLUMN); mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN); @@ -97,7 +137,13 @@ public class SqlData { mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN); } + /* + * 功能描述:设置用于共享的数据,并提供异常抛出与处理机制 + * 参数注解: + * Made By CuiCan + */ public void setContent(JSONObject js) throws JSONException { + //如果传入的JSONObject对象中有DataColumns.ID这一项,则设置,否则设为INVALID_ID long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID; if (mIsCreate || mDataId != dataId) { mDiffDataValues.put(DataColumns.ID, dataId); @@ -129,12 +175,18 @@ public class SqlData { } mDataContentData3 = dataContentData3; } - + /* + * 功能描述:获取共享的数据内容,并提供异常抛出与处理机制 + * 参数注解: + * Made By CuiCan + */ public JSONObject getContent() throws JSONException { if (mIsCreate) { Log.e(TAG, "it seems that we haven't created this in database yet"); return null; } + + //创建JSONObject对象。并将相关数据放入其中,并返回。 JSONObject js = new JSONObject(); js.put(DataColumns.ID, mDataId); js.put(DataColumns.MIME_TYPE, mDataMimeType); @@ -144,6 +196,11 @@ public class SqlData { return js; } + /* + * 功能描述:commit函数用于把当前造作所做的修改保存到数据库 + * 参数注解: + * Made By CuiCan + */ public void commit(long noteId, boolean validateVersion, long version) { if (mIsCreate) { @@ -183,6 +240,12 @@ public class SqlData { mIsCreate = false; } + /* + * 功能描述:获取当前id + * 实现过程: + * 参数注解: + * Made By CuiCan + */ public long getId() { return mDataId; } diff --git a/src/net/micode/notes/gtask/data/SqlNote.java b/src/net/micode/notes/gtask/data/SqlNote.java index 79a4095..434f73d 100644 --- a/src/net/micode/notes/gtask/data/SqlNote.java +++ b/src/net/micode/notes/gtask/data/SqlNote.java @@ -14,8 +14,18 @@ * limitations under the License. */ +/* + * Description:用于支持小米便签最底层的数据库相关操作,和sqlnote的关系上是子集关系,即data是note的子集(节点)。 + * SqlData其实就是也就是所谓数据中的数据 + */ package net.micode.notes.gtask.data; +/* + * 功能描述: + * 实现过程: + * 参数注解: + * Made By CuiCan + */ import android.appwidget.AppWidgetManager; import android.content.ContentResolver; import android.content.ContentValues; @@ -39,10 +49,20 @@ import java.util.ArrayList; public class SqlNote { + /* + * 功能描述:得到类的简写名称存入字符串TAG中 + * 实现过程:调用getSimpleName ()函数 + * Made By CuiCan + */ private static final String TAG = SqlNote.class.getSimpleName(); private static final int INVALID_ID = -99999; + /** + * 来自Notes类中定义的DataColumn中的一些常量 + */ + + // 集合了interface DataColumns中所有SF常量 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, @@ -87,7 +107,7 @@ public class SqlNote { public static final int VERSION_COLUMN = 16; private Context mContext; - + //判断是否直接用Content生成,是为true,否则为false private ContentResolver mContentResolver; private boolean mIsCreate; @@ -126,7 +146,7 @@ public class SqlNote { mContext = context; mContentResolver = context.getContentResolver(); mIsCreate = true; - mId = INVALID_ID; + mId = INVALID_ID;//mDataId置初始值-99999 mAlertDate = 0; mBgColorId = ResourceParser.getDefaultBgId(context); mCreatedDate = System.currentTimeMillis(); diff --git a/src/net/micode/notes/gtask/data/TaskList.java b/src/net/micode/notes/gtask/data/TaskList.java index 4ea21c5..0ee9f21 100644 --- a/src/net/micode/notes/gtask/data/TaskList.java +++ b/src/net/micode/notes/gtask/data/TaskList.java @@ -31,11 +31,11 @@ import java.util.ArrayList; public class TaskList extends Node { - private static final String TAG = TaskList.class.getSimpleName(); + private static final String TAG = TaskList.class.getSimpleName();//tag标记 - private int mIndex; + private int mIndex;//当前TaskList的指针 - private ArrayList mChildren; + private ArrayList mChildren;//类中主要的保存数据的单元,用来实现一个以Task为元素的ArrayList public TaskList() { super(); @@ -43,6 +43,10 @@ public class TaskList extends Node { mIndex = 1; } + /* (non-Javadoc) + * @see net.micode.notes.gtask.data.Node#getCreateAction(int) + * 生成并返回一个包含了一定数据的JSONObject实体 + */ public JSONObject getCreateAction(int actionId) { JSONObject js = new JSONObject(); @@ -103,6 +107,10 @@ public class TaskList extends Node { return js; } + /* (non-Javadoc) + * @see net.micode.notes.gtask.data.Node#getUpdateAction(int) + * 生成并返回一个包含了一定数据的JSONObject实体 + */ public void setContentByRemoteJSON(JSONObject js) { if (js != null) { try { @@ -216,10 +224,19 @@ public class TaskList extends Node { return SYNC_ACTION_ERROR; } + /** + * @return + * 功能:获得TaskList的大小,即mChildren的大小 + */ public int getChildTaskCount() { return mChildren.size(); } + /** + * @param task + * @return 返回值为是否成功添加任务。 + * 功能:在当前任务表末尾添加新的任务。 + */ public boolean addChildTask(Task task) { boolean ret = false; if (task != null && !mChildren.contains(task)) { @@ -229,11 +246,18 @@ public class TaskList extends Node { task.setPriorSibling(mChildren.isEmpty() ? null : mChildren .get(mChildren.size() - 1)); task.setParent(this); + //注意:每一次ArrayList的变化都要紧跟相关Task中PriorSibling的更改 + //,接下来几个函数都有相关操作 } } return ret; } - + /** + * @param task + * @param index + * @return + * 功能:在当前任务表的指定位置添加新的任务。 + */ public boolean addChildTask(Task task, int index) { if (index < 0 || index > mChildren.size()) { Log.e(TAG, "add child task: invalid index"); @@ -260,6 +284,11 @@ public class TaskList extends Node { return true; } + /** + * @param task + * @return 返回删除是否成功 + * 功能:删除TaskList中的一个Task + */ public boolean removeChildTask(Task task) { boolean ret = false; int index = mChildren.indexOf(task); @@ -281,6 +310,12 @@ public class TaskList extends Node { return ret; } + /** + * @param task + * @param index + * @return + * 功能:将当前TaskList中含有的某个Task移到index位置 + */ public boolean moveChildTask(Task task, int index) { if (index < 0 || index >= mChildren.size()) { @@ -297,8 +332,14 @@ public class TaskList extends Node { if (pos == index) return true; return (removeChildTask(task) && addChildTask(task, index)); + //利用已实现好的功能完成当下功能; } + /** + * @param gid + * @return返回寻找结果 + * 功能:按gid寻找Task + */ public Task findChildTaskByGid(String gid) { for (int i = 0; i < mChildren.size(); i++) { Task t = mChildren.get(i); @@ -309,10 +350,19 @@ public class TaskList extends Node { return null; } + /** + * @param task + * @return + * 功能:返回指定Task的index + */ public int getChildTaskIndex(Task task) { return mChildren.indexOf(task); } - + /** + * @param index + * @return + * 功能:返回指定index的Task + */ public Task getChildTaskByIndex(int index) { if (index < 0 || index >= mChildren.size()) { Log.e(TAG, "getTaskByIndex: invalid index"); @@ -320,9 +370,13 @@ public class TaskList extends Node { } return mChildren.get(index); } - + /** + * @param gid + * @return + * 功能:返回指定gid的Task + */ public Task getChilTaskByGid(String gid) { - for (Task task : mChildren) { + for (Task task : mChildren) {//一种常见的ArrayList的遍历方法(四种,见精读笔记) if (task.getGid().equals(gid)) return task; } -- 2.34.1