添加注释

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

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

Loading…
Cancel
Save