|
|
|
|
@ -169,6 +169,15 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//具体地说,这个方法依次从Cursor对象中读取ID_COLUMN、ALERTED_DATE_COLUMN、BG_COLOR_ID_COLUMN、
|
|
|
|
|
//CREATED_DATE_COLUMN、HAS_ATTACHMENT_COLUMN、MODIFIED_DATE_COLUMN、PARENT_ID_COLUMN、SNIPPET_COLUMN、
|
|
|
|
|
//TYPE_COLUMN、WIDGET_ID_COLUMN、WIDGET_TYPE_COLUMN和VERSION_COLUMN对应的列的值,
|
|
|
|
|
//并将这些值设置为该类的成员变量mId、mAlertDate、mBgColorId、mCreatedDate、mHasAttachment、mModifiedDate、
|
|
|
|
|
//mParentId、mSnippet、mType、mWidgetId、mWidgetType和mVersion的值。
|
|
|
|
|
|
|
|
|
|
// oadFromCursor方法是一个私有方法,它有一个参数c,该参数是一个Cursor对象。
|
|
|
|
|
// 这个方法的作用是从Cursor对象中读取数据并将其设置为该类的成员变量的值。
|
|
|
|
|
private void loadFromCursor(Cursor c) {
|
|
|
|
|
mId = c.getLong(ID_COLUMN);
|
|
|
|
|
mAlertDate = c.getLong(ALERTED_DATE_COLUMN);
|
|
|
|
|
@ -183,7 +192,7 @@ public class SqlNote {
|
|
|
|
|
mWidgetType = c.getInt(WIDGET_TYPE_COLUMN);
|
|
|
|
|
mVersion = c.getLong(VERSION_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//loadDataContent方法也是一个私有方法,它不接受任何参数。这个方法的作用是从数据库中读取笔记的数据内容。
|
|
|
|
|
private void loadDataContent() {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
mDataList.clear();
|
|
|
|
|
@ -213,24 +222,26 @@ public class SqlNote {
|
|
|
|
|
public boolean setContent(JSONObject js) {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
// 获取JSON对象中META_HEAD_NOTE字段对应的JSONObject对象,并赋值给note
|
|
|
|
|
|
|
|
|
|
if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
|
|
|
|
|
// 如果note的TYPE字段为TYPE_SYSTEM,打印warning信息
|
|
|
|
|
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
|
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
|
// 如果note的TYPE字段为TYPE_FOLDER,更新Java对象mSnippet和mType的值
|
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
|
|
|
|
|
}
|
|
|
|
|
mSnippet = snippet;
|
|
|
|
|
|
|
|
|
|
int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
|
|
|
|
|
: Notes.TYPE_NOTE;
|
|
|
|
|
int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE) : Notes.TYPE_NOTE;
|
|
|
|
|
if (mIsCreate || mType != type) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type);
|
|
|
|
|
}
|
|
|
|
|
mType = type;
|
|
|
|
|
} else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) {
|
|
|
|
|
// 如果note的TYPE字段为TYPE_NOTE,更新Java对象的各个字段值
|
|
|
|
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
|
long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID;
|
|
|
|
|
if (mIsCreate || mId != id) {
|
|
|
|
|
@ -238,43 +249,37 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
mId = id;
|
|
|
|
|
|
|
|
|
|
long alertDate = note.has(NoteColumns.ALERTED_DATE) ? note
|
|
|
|
|
.getLong(NoteColumns.ALERTED_DATE) : 0;
|
|
|
|
|
long alertDate = note.has(NoteColumns.ALERTED_DATE) ? note.getLong(NoteColumns.ALERTED_DATE) : 0;
|
|
|
|
|
if (mIsCreate || mAlertDate != alertDate) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.ALERTED_DATE, alertDate);
|
|
|
|
|
}
|
|
|
|
|
mAlertDate = alertDate;
|
|
|
|
|
|
|
|
|
|
int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? note
|
|
|
|
|
.getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext);
|
|
|
|
|
int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? note.getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext);
|
|
|
|
|
if (mIsCreate || mBgColorId != bgColorId) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId);
|
|
|
|
|
}
|
|
|
|
|
mBgColorId = bgColorId;
|
|
|
|
|
|
|
|
|
|
long createDate = note.has(NoteColumns.CREATED_DATE) ? note
|
|
|
|
|
.getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis();
|
|
|
|
|
long createDate = note.has(NoteColumns.CREATED_DATE) ? note.getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis();
|
|
|
|
|
if (mIsCreate || mCreatedDate != createDate) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.CREATED_DATE, createDate);
|
|
|
|
|
}
|
|
|
|
|
mCreatedDate = createDate;
|
|
|
|
|
|
|
|
|
|
int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? note
|
|
|
|
|
.getInt(NoteColumns.HAS_ATTACHMENT) : 0;
|
|
|
|
|
int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? note.getInt(NoteColumns.HAS_ATTACHMENT) : 0;
|
|
|
|
|
if (mIsCreate || mHasAttachment != hasAttachment) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.HAS_ATTACHMENT, hasAttachment);
|
|
|
|
|
}
|
|
|
|
|
mHasAttachment = hasAttachment;
|
|
|
|
|
|
|
|
|
|
long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? note
|
|
|
|
|
.getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis();
|
|
|
|
|
long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? note.getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis();
|
|
|
|
|
if (mIsCreate || mModifiedDate != modifiedDate) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate);
|
|
|
|
|
}
|
|
|
|
|
mModifiedDate = modifiedDate;
|
|
|
|
|
|
|
|
|
|
long parentId = note.has(NoteColumns.PARENT_ID) ? note
|
|
|
|
|
.getLong(NoteColumns.PARENT_ID) : 0;
|
|
|
|
|
long parentId = note.has(NoteColumns.PARENT_ID) ? note.getLong(NoteColumns.PARENT_ID) : 0;
|
|
|
|
|
if (mIsCreate || mParentId != parentId) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId);
|
|
|
|
|
}
|
|
|
|
|
@ -308,6 +313,12 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
mWidgetType = widgetType;
|
|
|
|
|
|
|
|
|
|
/* 从 JSON 数据中解析出笔记和数据信息
|
|
|
|
|
|
|
|
|
|
@param jsonData 笔记和数据的 JSON 数据
|
|
|
|
|
|
|
|
|
|
@return 是否解析成功
|
|
|
|
|
*/
|
|
|
|
|
long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note
|
|
|
|
|
.getLong(NoteColumns.ORIGIN_PARENT_ID) : 0;
|
|
|
|
|
if (mIsCreate || mOriginParent != originParent) {
|
|
|
|
|
@ -347,13 +358,14 @@ public class SqlNote {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
|
if (mIsCreate) { // 如果这个对象还没有被创建
|
|
|
|
|
Log.e(TAG, "it seems that we haven't created this in database yet");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建一个名为 note 的 JSON 对象,并将这个对象的属性添加到里面
|
|
|
|
|
JSONObject note = new JSONObject();
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) { // 如果这个对象是一条笔记
|
|
|
|
|
note.put(NoteColumns.ID, mId);
|
|
|
|
|
note.put(NoteColumns.ALERTED_DATE, mAlertDate);
|
|
|
|
|
note.put(NoteColumns.BG_COLOR_ID, mBgColorId);
|
|
|
|
|
@ -366,8 +378,9 @@ public class SqlNote {
|
|
|
|
|
note.put(NoteColumns.WIDGET_ID, mWidgetId);
|
|
|
|
|
note.put(NoteColumns.WIDGET_TYPE, mWidgetType);
|
|
|
|
|
note.put(NoteColumns.ORIGIN_PARENT_ID, mOriginParent);
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note);
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note); // 将这个 JSON 对象添加到 js 中
|
|
|
|
|
|
|
|
|
|
// 创建一个名为 dataArray 的 JSON 数组,并将每个 SqlData 对象的 content 属性添加到里面
|
|
|
|
|
JSONArray dataArray = new JSONArray();
|
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
|
JSONObject data = sqlData.getContent();
|
|
|
|
|
@ -375,84 +388,85 @@ public class SqlNote {
|
|
|
|
|
dataArray.put(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
|
|
|
|
|
} else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) {
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); // 将这个 JSON 数组添加到 js 中
|
|
|
|
|
} 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);
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note);
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note); // 将这个 JSON 对象添加到 js 中
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
|
return js; // 返回 JSON 对象
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
return null; // 如果出现异常返回 null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setParentId(long id) {
|
|
|
|
|
mParentId = id;
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
|
|
|
|
|
public void setParentId(long id) { //设置笔记的父笔记ID
|
|
|
|
|
mParentId = id; //保存父笔记ID
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, id); //将更改的值加入到 mDiffNoteValues 中
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setGtaskId(String gid) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
|
|
|
|
|
public void setGtaskId(String gid) { //设置笔记的 Google 任务ID
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); //将更改的值加入到 mDiffNoteValues 中
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setSyncId(long syncId) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
|
|
|
|
|
public void setSyncId(long syncId) { //设置笔记的同步ID
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); //将更改的值加入到 mDiffNoteValues 中
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void resetLocalModified() {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
|
|
|
|
|
public void resetLocalModified() { //重置本地修改状态
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); //将更改的值加入到 mDiffNoteValues 中
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long getId() {
|
|
|
|
|
return mId;
|
|
|
|
|
public long getId() { //获取笔记ID
|
|
|
|
|
return mId; //返回笔记ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long getParentId() {
|
|
|
|
|
return mParentId;
|
|
|
|
|
public long getParentId() { //获取笔记的父笔记ID
|
|
|
|
|
return mParentId; //返回笔记的父笔记ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getSnippet() {
|
|
|
|
|
return mSnippet;
|
|
|
|
|
public String getSnippet() { //获取笔记片段
|
|
|
|
|
return mSnippet; //返回笔记片段
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isNoteType() {
|
|
|
|
|
return mType == Notes.TYPE_NOTE;
|
|
|
|
|
public boolean isNoteType() { //判断笔记是否为Note类型
|
|
|
|
|
return mType == Notes.TYPE_NOTE; //如果笔记类型是Note,则返回true,否则返回false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void commit(boolean validateVersion) {
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
|
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));
|
|
|
|
|
mId = Long.valueOf(uri.getPathSegments().get(1)); //获取新笔记ID
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
Log.e(TAG, "Get note id error :" + e.toString());
|
|
|
|
|
throw new ActionFailureException("create note failed");
|
|
|
|
|
throw new ActionFailureException("create note failed"); //抛出异常,创建笔记失败
|
|
|
|
|
}
|
|
|
|
|
if (mId == 0) {
|
|
|
|
|
throw new IllegalStateException("Create thread id failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) { //如果是笔记类型
|
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
|
sqlData.commit(mId, false, -1);
|
|
|
|
|
sqlData.commit(mId, false, -1); //保存笔记的数据
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
} 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");
|
|
|
|
|
}
|
|
|
|
|
if (mDiffNoteValues.size() > 0) {
|
|
|
|
|
mVersion ++;
|
|
|
|
|
if (mDiffNoteValues.size() > 0) { //如果有更改需要提交
|
|
|
|
|
mVersion ++; //更新版本号
|
|
|
|
|
int result = 0;
|
|
|
|
|
if (!validateVersion) {
|
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
|
@ -479,6 +493,7 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// refresh local info
|
|
|
|
|
//loadFromCursor(mId) 方法是一个私有方法,它会查询指定 ID 的笔记记录,并将返回的 Cursor 对象的数据填充到NoteItem 的成员变量中
|
|
|
|
|
loadFromCursor(mId);
|
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
|
loadDataContent();
|
|
|
|
|
|