添加注释

pull/1/head
ZYP 1 year ago
parent e99ba549ae
commit 4617ba01d7

@ -123,34 +123,35 @@ public class SqlNote {
private ArrayList<SqlData> mDataList; private ArrayList<SqlData> mDataList;
public SqlNote(Context context) { public SqlNote(Context context) {
mContext = context; //初始化成员变量
mContentResolver = context.getContentResolver(); mContext = context;// 上下文
mIsCreate = true; mContentResolver = context.getContentResolver();// 获取内容解析器
mId = INVALID_ID; mIsCreate = true; // 是否为新建笔记
mAlertDate = 0; mId = INVALID_ID; // 笔记ID
mBgColorId = ResourceParser.getDefaultBgId(context); mAlertDate = 0; // 提醒时间
mCreatedDate = System.currentTimeMillis(); mBgColorId = ResourceParser.getDefaultBgId(context); // 背景颜色ID
mHasAttachment = 0; mCreatedDate = System.currentTimeMillis(); // 创建时间
mModifiedDate = System.currentTimeMillis(); mHasAttachment = 0; // 是否有附件
mParentId = 0; mModifiedDate = System.currentTimeMillis(); // 修改时间
mSnippet = ""; mParentId = 0; // 所属父笔记ID
mType = Notes.TYPE_NOTE; mSnippet = ""; // 笔记摘要
mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; mType = Notes.TYPE_NOTE; // 笔记类型
mWidgetType = Notes.TYPE_WIDGET_INVALIDE; mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; // 小部件ID
mOriginParent = 0; mWidgetType = Notes.TYPE_WIDGET_INVALIDE; // 小部件类型
mVersion = 0; mOriginParent = 0; // 原始的父笔记ID
mDiffNoteValues = new ContentValues(); mVersion = 0; // 笔记版本
mDataList = new ArrayList<SqlData>(); mDiffNoteValues = new ContentValues(); // 存储笔记差异内容的 ContentValues 对象
mDataList = new ArrayList<SqlData>(); // 存储笔记数据的 ArrayList
} }
public SqlNote(Context context, Cursor c) { public SqlNote(Context context, Cursor c) {
mContext = context; mContext = context;
mContentResolver = context.getContentResolver(); mContentResolver = context.getContentResolver();
mIsCreate = false; mIsCreate = false;
loadFromCursor(c); loadFromCursor(c); // 从Cursor加载笔记数据
mDataList = new ArrayList<SqlData>(); mDataList = new ArrayList<SqlData>();
if (mType == Notes.TYPE_NOTE) if (mType == Notes.TYPE_NOTE)
loadDataContent(); loadDataContent(); // 加载笔记内容
mDiffNoteValues = new ContentValues(); mDiffNoteValues = new ContentValues();
} }
@ -158,10 +159,10 @@ public class SqlNote {
mContext = context; mContext = context;
mContentResolver = context.getContentResolver(); mContentResolver = context.getContentResolver();
mIsCreate = false; mIsCreate = false;
loadFromCursor(id); loadFromCursor(id); // 根据ID从数据库中加载笔记数据
mDataList = new ArrayList<SqlData>(); mDataList = new ArrayList<SqlData>();
if (mType == Notes.TYPE_NOTE) if (mType == Notes.TYPE_NOTE)
loadDataContent(); loadDataContent(); // 加载笔记内容
mDiffNoteValues = new ContentValues(); mDiffNoteValues = new ContentValues();
} }
@ -175,7 +176,7 @@ public class SqlNote {
}, null); }, null);
if (c != null) { if (c != null) {
c.moveToNext(); c.moveToNext();
loadFromCursor(c); loadFromCursor(c);// 从Cursor加载笔记数据
} else { } else {
Log.w(TAG, "loadFromCursor: cursor = null"); Log.w(TAG, "loadFromCursor: cursor = null");
} }
@ -186,43 +187,45 @@ public class SqlNote {
} }
private void loadFromCursor(Cursor c) { private void loadFromCursor(Cursor c) {
mId = c.getLong(ID_COLUMN); mId = c.getLong(ID_COLUMN); // 加载ID数据
mAlertDate = c.getLong(ALERTED_DATE_COLUMN); mAlertDate = c.getLong(ALERTED_DATE_COLUMN); // 加载提醒日期数据
mBgColorId = c.getInt(BG_COLOR_ID_COLUMN); mBgColorId = c.getInt(BG_COLOR_ID_COLUMN); // 加载背景颜色ID数据
mCreatedDate = c.getLong(CREATED_DATE_COLUMN); mCreatedDate = c.getLong(CREATED_DATE_COLUMN); // 加载创建日期数据
mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN); mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN); // 加载附件存在标记数据
mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN); mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN); // 加载修改日期数据
mParentId = c.getLong(PARENT_ID_COLUMN); mParentId = c.getLong(PARENT_ID_COLUMN); // 加载父级ID数据
mSnippet = c.getString(SNIPPET_COLUMN); mSnippet = c.getString(SNIPPET_COLUMN); // 加载摘要数据
mType = c.getInt(TYPE_COLUMN); mType = c.getInt(TYPE_COLUMN); // 加载类型数据
mWidgetId = c.getInt(WIDGET_ID_COLUMN); mWidgetId = c.getInt(WIDGET_ID_COLUMN); // 加载小部件ID数据
mWidgetType = c.getInt(WIDGET_TYPE_COLUMN); mWidgetType = c.getInt(WIDGET_TYPE_COLUMN); // 加载小部件类型数据
mVersion = c.getLong(VERSION_COLUMN); mVersion = c.getLong(VERSION_COLUMN); // 加载版本号数据
} }
private void loadDataContent() { private void loadDataContent() {
Cursor c = null; Cursor c = null;
mDataList.clear(); mDataList.clear();// 清空数据列表
try { try {
// 通过ContentResolver从Content Provider查询数据内容
c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA, c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA,
"(note_id=?)", new String[] { "(note_id=?)", new String[] {
String.valueOf(mId) String.valueOf(mId)
}, null); }, null);
if (c != null) { if (c != null) {
if (c.getCount() == 0) { if (c.getCount() == 0) {
// 数据内容为空
Log.w(TAG, "it seems that the note has not data"); Log.w(TAG, "it seems that the note has not data");
return; return;
} }
while (c.moveToNext()) { while (c.moveToNext()) {// 遍历Cursor创建SqlData对象并添加到数据列表中
SqlData data = new SqlData(mContext, c); SqlData data = new SqlData(mContext, c);
mDataList.add(data); mDataList.add(data);
} }
} else { } else {
Log.w(TAG, "loadDataContent: cursor = null"); Log.w(TAG, "loadDataContent: cursor = null");
} }
} finally { } finally { // Cursor为空
if (c != null) if (c != null)
c.close(); c.close();// 关闭Cursor
} }
} }
@ -441,22 +444,25 @@ public class SqlNote {
} }
public void commit(boolean validateVersion) { public void commit(boolean validateVersion) {
if (mIsCreate) { if (mIsCreate) {// 判断当前对象是否为新建对象。如果是,则执行以下代码块。
if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
// 如果当前对象的id为无效id并且mDiffNoteValues中包含NoteColumns.ID这个键则移除该键值对。
mDiffNoteValues.remove(NoteColumns.ID); mDiffNoteValues.remove(NoteColumns.ID);
} }
// 通过ContentResolver向Notes表中插入一条记录第一部分是插入的uri。
Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues); Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);
try { try {// 试图从uri的路径段中获取note的id。
// 将获取到的id转换为long类型并赋值给mId。如果转换失败会抛出NumberFormatException异常。
mId = Long.valueOf(uri.getPathSegments().get(1)); mId = Long.valueOf(uri.getPathSegments().get(1));
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// 记录错误日志创建note失败。抛出ActionFailureException异常异常信息为create note failed。
Log.e(TAG, "Get note id error :" + e.toString()); Log.e(TAG, "Get note id error :" + e.toString());
throw new ActionFailureException("create note failed"); throw new ActionFailureException("create note failed");
} }
if (mId == 0) { if (mId == 0) {// 如果mId仍然为0抛出一个IllegalStateException异常创建thread id失败。
throw new IllegalStateException("Create thread id failed"); throw new IllegalStateException("Create thread id failed");
} }
// 如果mType等于Notes.TYPE_NOTE执行以下代码块。
if (mType == Notes.TYPE_NOTE) { if (mType == Notes.TYPE_NOTE) {
for (SqlData sqlData : mDataList) { for (SqlData sqlData : mDataList) {
sqlData.commit(mId, false, -1); sqlData.commit(mId, false, -1);
@ -464,24 +470,28 @@ public class SqlNote {
} }
} else { } else {
if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) { if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) {
Log.e(TAG, "No such note"); Log.e(TAG, "No such note");// 打印错误日志"No such note
// 抛出异常IllegalStateException
throw new IllegalStateException("Try to update note with invalid id"); throw new IllegalStateException("Try to update note with invalid id");
} }
if (mDiffNoteValues.size() > 0) { if (mDiffNoteValues.size() > 0) {
mVersion ++; mVersion ++;
int result = 0; int result = 0;
if (!validateVersion) { if (!validateVersion) {
//更新Notes表中的记录将mDiffNoteValues中的键值对更新到Notes表中
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
+ NoteColumns.ID + "=?)", new String[] { + NoteColumns.ID + "=?)", new String[] {
String.valueOf(mId) String.valueOf(mId)
}); });
} else { } else {
// 更新Notes表中的记录将mDiffNoteValues中的键值对更新到Notes表中
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
+ NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)", + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
new String[] { new String[] {
String.valueOf(mId), String.valueOf(mVersion) String.valueOf(mId), String.valueOf(mVersion)
}); });
} }
// 如果result为0即没有更新记录则打印警告日志
if (result == 0) { if (result == 0) {
Log.w(TAG, "there is no update. maybe user updates note when syncing"); Log.w(TAG, "there is no update. maybe user updates note when syncing");
} }

Loading…
Cancel
Save