|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
* You may obtain a copy of the License at
|
|
|
*
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
*
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
* See the License for the specific language governing permissions and
|
|
|
* limitations under the License.
|
|
|
*/
|
|
|
|
|
|
package net.micode.notes.gtask.data;
|
|
|
|
|
|
import android.appwidget.AppWidgetManager; // 导入android.appwidget.AppWidgetManager类
|
|
|
import android.content.ContentResolver; // 导入android.content.ContentResolver类
|
|
|
import android.content.ContentValues; // 导入android.content.ContentValues类
|
|
|
import android.content.Context; // 导入android.content.Context类
|
|
|
import android.database.Cursor; // 导入android.database.Cursor类
|
|
|
import android.net.Uri; // 导入android.net.Uri类
|
|
|
import android.util.Log; // 导入android.util.Log类
|
|
|
|
|
|
import net.micode.notes.data.Notes; // 导入net.micode.notes.data.Notes类
|
|
|
import net.micode.notes.data.Notes.DataColumns; // 导入net.micode.notes.data.Notes.DataColumns类
|
|
|
import net.micode.notes.data.Notes.NoteColumns; // 导入net.micode.notes.data.Notes.NoteColumns类
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException; // 导入net.micode.notes.gtask.exception.ActionFailureException类
|
|
|
import net.micode.notes.tool.GTaskStringUtils; // 导入net.micode.notes.tool.GTaskStringUtils类
|
|
|
import net.micode.notes.tool.ResourceParser; // 导入net.micode.notes.tool.ResourceParser类
|
|
|
|
|
|
import org.json.JSONArray; // 导入org.json.JSONArray类
|
|
|
import org.json.JSONException; // 导入org.json.JSONException类
|
|
|
import org.json.JSONObject; // 导入org.json.JSONObject类
|
|
|
|
|
|
import java.util.ArrayList; // 导入java.util.ArrayList类
|
|
|
|
|
|
/**
|
|
|
* SqlNote类用于管理便签的数据库操作
|
|
|
*/
|
|
|
public class SqlNote {
|
|
|
private static final String TAG = SqlNote.class.getSimpleName(); // 定义私有静态常量TAG,值为SqlNote类的简单名称
|
|
|
|
|
|
private static final int INVALID_ID = -99999; // 定义私有静态常量INVALID_ID,值为-99999
|
|
|
|
|
|
public static final String[] PROJECTION_NOTE = new String[] { // 定义公共静态常量PROJECTION_NOTE,字符串数组类型
|
|
|
NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID,
|
|
|
NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE,
|
|
|
NoteColumns.NOTES_COUNT, NoteColumns.PARENT_ID, NoteColumns.SNIPPET, NoteColumns.TYPE,
|
|
|
NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE, NoteColumns.SYNC_ID,
|
|
|
NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID,
|
|
|
NoteColumns.VERSION
|
|
|
};
|
|
|
|
|
|
public static final int ID_COLUMN = 0; // 定义公共静态常量ID_COLUMN,值为0
|
|
|
|
|
|
public static final int ALERTED_DATE_COLUMN = 1; // 定义公共静态常量ALERTED_DATE_COLUMN,值为1
|
|
|
|
|
|
public static final int BG_COLOR_ID_COLUMN = 2; // 定义公共静态常量BG_COLOR_ID_COLUMN,值为2
|
|
|
|
|
|
public static final int CREATED_DATE_COLUMN = 3; // 定义公共静态常量CREATED_DATE_COLUMN,值为3
|
|
|
|
|
|
public static final int HAS_ATTACHMENT_COLUMN = 4; // 定义公共静态常量HAS_ATTACHMENT_COLUMN,值为4
|
|
|
|
|
|
public static final int MODIFIED_DATE_COLUMN = 5; // 定义公共静态常量MODIFIED_DATE_COLUMN,值为5
|
|
|
|
|
|
public static final int NOTES_COUNT_COLUMN = 6; // 定义公共静态常量NOTES_COUNT_COLUMN,值为6
|
|
|
|
|
|
public static final int PARENT_ID_COLUMN = 7; // 定义公共静态常量PARENT_ID_COLUMN,值为7
|
|
|
|
|
|
public static final int SNIPPET_COLUMN = 8; // 定义公共静态常量SNIPPET_COLUMN,值为8
|
|
|
|
|
|
public static final int TYPE_COLUMN = 9; // 定义公共静态常量TYPE_COLUMN,值为9
|
|
|
|
|
|
public static final int WIDGET_ID_COLUMN = 10; // 定义公共静态常量WIDGET_ID_COLUMN,值为10
|
|
|
|
|
|
public static final int WIDGET_TYPE_COLUMN = 11; // 定义公共静态常量WIDGET_TYPE_COLUMN,值为11
|
|
|
|
|
|
public static final int SYNC_ID_COLUMN = 12; // 定义公共静态常量SYNC_ID_COLUMN,值为12
|
|
|
|
|
|
public static final int LOCAL_MODIFIED_COLUMN = 13; // 定义公共静态常量LOCAL_MODIFIED_COLUMN,值为13
|
|
|
|
|
|
public static final int ORIGIN_PARENT_ID_COLUMN = 14; // 定义公共静态常量ORIGIN_PARENT_ID_COLUMN,值为14
|
|
|
|
|
|
public static final int GTASK_ID_COLUMN = 15; // 定义公共静态常量GTASK_ID_COLUMN,值为15
|
|
|
|
|
|
public static final int VERSION_COLUMN = 16; // 定义公共静态常量VERSION_COLUMN,值为16
|
|
|
|
|
|
private Context mContext; // 定义私有成员变量mContext,Context类型
|
|
|
|
|
|
private ContentResolver mContentResolver; // 定义私有成员变量mContentResolver,ContentResolver类型
|
|
|
|
|
|
private boolean mIsCreate; // 定义私有成员变量mIsCreate,boolean类型
|
|
|
|
|
|
private long mId; // 定义私有成员变量mId,long类型
|
|
|
|
|
|
private long mAlertDate; // 定义私有成员变量mAlertDate,long类型
|
|
|
|
|
|
private int mBgColorId; // 定义私有成员变量mBgColorId,int类型
|
|
|
|
|
|
private long mCreatedDate; // 定义私有成员变量mCreatedDate,long类型
|
|
|
|
|
|
private int mHasAttachment; // 定义私有成员变量mHasAttachment,int类型
|
|
|
|
|
|
private long mModifiedDate; // 定义私有成员变量mModifiedDate,long类型
|
|
|
|
|
|
private long mParentId; // 定义私有成员变量mParentId,long类型
|
|
|
|
|
|
private String mSnippet; // 定义私有成员变量mSnippet,String类型
|
|
|
|
|
|
private int mType; // 定义私有成员变量mType,int类型
|
|
|
|
|
|
private int mWidgetId; // 定义私有成员变量mWidgetId,int类型
|
|
|
|
|
|
private int mWidgetType; // 定义私有成员变量mWidgetType,int类型
|
|
|
|
|
|
private long mOriginParent; // 定义私有成员变量mOriginParent,long类型
|
|
|
|
|
|
private long mVersion; // 定义私有成员变量mVersion,long类型
|
|
|
|
|
|
private ContentValues mDiffNoteValues; // 定义私有成员变量mDiffNoteValues,ContentValues类型
|
|
|
|
|
|
private ArrayList<SqlData> mDataList; // 定义私有成员变量mDataList,ArrayList<SqlData>类型
|
|
|
|
|
|
public SqlNote(Context context) {
|
|
|
mContext = context; // 初始化成员变量mContext
|
|
|
mContentResolver = context.getContentResolver(); // 获取上下文的内容解析器
|
|
|
mIsCreate = true; // 设置mIsCreate为true,表示创建新的便签
|
|
|
mId = INVALID_ID; // 设置mId为INVALID_ID,表示无效的id
|
|
|
mAlertDate = 0; // 设置mAlertDate为0,表示没有提醒日期
|
|
|
mBgColorId = ResourceParser.getDefaultBgId(context); // 设置mBgColorId为默认背景颜色的id
|
|
|
mCreatedDate = System.currentTimeMillis(); // 设置mCreatedDate为当前时间的毫秒数
|
|
|
mHasAttachment = 0; // 设置mHasAttachment为0,表示没有附件
|
|
|
mModifiedDate = System.currentTimeMillis(); // 设置mModifiedDate为当前时间的毫秒数
|
|
|
mParentId = 0; // 设置mParentId为0,表示没有父级便签
|
|
|
mSnippet = ""; // 设置mSnippet为空字符串
|
|
|
mType = Notes.TYPE_NOTE; // 设置mType为Notes.TYPE_NOTE,表示便签类型为普通便签
|
|
|
mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; // 设置mWidgetId为无效的小部件id
|
|
|
mWidgetType = Notes.TYPE_WIDGET_INVALIDE; // 设置mWidgetType为无效的小部件类型
|
|
|
mOriginParent = 0; // 设置mOriginParent为0,表示没有原始父级便签
|
|
|
mVersion = 0; // 设置mVersion为0,表示版本号为0
|
|
|
mDiffNoteValues = new ContentValues(); // 创建空的ContentValues对象,用于存储便签的差异值
|
|
|
mDataList = new ArrayList<SqlData>(); // 创建空的ArrayList<SqlData>对象,用于存储便签的数据列表
|
|
|
}
|
|
|
|
|
|
public SqlNote(Context context, Cursor c) {
|
|
|
mContext = context; // 初始化成员变量mContext
|
|
|
mContentResolver = context.getContentResolver(); // 获取上下文的内容解析器
|
|
|
mIsCreate = false; // 设置mIsCreate为false,表示不是创建新的便签
|
|
|
loadFromCursor(c); // 从Cursor加载便签数据
|
|
|
mDataList = new ArrayList<SqlData>(); // 创建空的ArrayList<SqlData>对象,用于存储便签的数据列表
|
|
|
if (mType == Notes.TYPE_NOTE) // 若便签类型为普通便签
|
|
|
loadDataContent(); // 加载便签的内容数据
|
|
|
mDiffNoteValues = new ContentValues(); // 创建空的ContentValues对象,用于存储便签的差异值
|
|
|
}
|
|
|
|
|
|
public SqlNote(Context context, long id) {
|
|
|
mContext = context; // 初始化成员变量mContext
|
|
|
mContentResolver = context.getContentResolver(); // 获取上下文的内容解析器
|
|
|
mIsCreate = false; // 设置mIsCreate为false,表示不是创建新的便签
|
|
|
loadFromCursor(id); // 从数据库中加载指定id的便签数据
|
|
|
mDataList = new ArrayList<SqlData>(); // 创建空的ArrayList<SqlData>对象,用于存储便签的数据列表
|
|
|
if (mType == Notes.TYPE_NOTE) // 若便签类型为普通便签
|
|
|
loadDataContent(); // 加载便签的内容数据
|
|
|
mDiffNoteValues = new ContentValues(); // 创建空的ContentValues对象,用于存储便签的差异值
|
|
|
}
|
|
|
|
|
|
private void loadFromCursor(long id) {
|
|
|
Cursor c = null; // 定义Cursor对象c,初始值为null
|
|
|
try {
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)",
|
|
|
new String[] {
|
|
|
String.valueOf(id)
|
|
|
}, null); // 查询数据库中指定id的便签数据
|
|
|
if (c != null) { // 若Cursor不为null
|
|
|
c.moveToNext(); // 移动到第一行
|
|
|
loadFromCursor(c); // 从Cursor加载便签数据
|
|
|
} else {
|
|
|
Log.w(TAG, "loadFromCursor: cursor = null"); // 在日志中输出警告信息
|
|
|
}
|
|
|
} finally {
|
|
|
if (c != null)
|
|
|
c.close(); // 关闭Cursor
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 从Cursor加载数据到当前便签对象中
|
|
|
private void loadFromCursor(Cursor c) {
|
|
|
mId = c.getLong(ID_COLUMN); // 从Cursor获取id并赋值给mId
|
|
|
mAlertDate = c.getLong(ALERTED_DATE_COLUMN); // 从Cursor获取提醒日期并赋值给mAlertDate
|
|
|
mBgColorId = c.getInt(BG_COLOR_ID_COLUMN); // 从Cursor获取背景颜色id并赋值给mBgColorId
|
|
|
mCreatedDate = c.getLong(CREATED_DATE_COLUMN); // 从Cursor获取创建日期并赋值给mCreatedDate
|
|
|
mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN); // 从Cursor获取是否有附件并赋值给mHasAttachment
|
|
|
mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN); // 从Cursor获取修改日期并赋值给mModifiedDate
|
|
|
mParentId = c.getLong(PARENT_ID_COLUMN); // 从Cursor获取父级id并赋值给mParentId
|
|
|
mSnippet = c.getString(SNIPPET_COLUMN); // 从Cursor获取摘要并赋值给mSnippet
|
|
|
mType = c.getInt(TYPE_COLUMN); // 从Cursor获取类型并赋值给mType
|
|
|
mWidgetId = c.getInt(WIDGET_ID_COLUMN); // 从Cursor获取小部件id并赋值给mWidgetId
|
|
|
mWidgetType = c.getInt(WIDGET_TYPE_COLUMN); // 从Cursor获取小部件类型并赋值给mWidgetType
|
|
|
mVersion = c.getLong(VERSION_COLUMN); // 从Cursor获取版本号并赋值给mVersion
|
|
|
}
|
|
|
|
|
|
// 加载便签内容数据
|
|
|
private void loadDataContent() {
|
|
|
Cursor c = null; // 定义Cursor对象c,初始值为null
|
|
|
mDataList.clear(); // 清空数据列表
|
|
|
try {
|
|
|
c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA,
|
|
|
"(note_id=?)", new String[] {
|
|
|
String.valueOf(mId)
|
|
|
}, null); // 查询数据库中指定id的便签内容数据
|
|
|
if (c != null) { // 若Cursor不为null
|
|
|
if (c.getCount() == 0) { // 若Cursor中无数据
|
|
|
Log.w(TAG, "it seems that the note has not data"); // 在日志中输出警告信息
|
|
|
return; // 返回
|
|
|
}
|
|
|
while (c.moveToNext()) { // 遍历Cursor中的数据
|
|
|
SqlData data = new SqlData(mContext, c); // 创建SqlData对象并初始化
|
|
|
mDataList.add(data); // 将SqlData对象添加到数据列表中
|
|
|
}
|
|
|
} else {
|
|
|
Log.w(TAG, "loadDataContent: cursor = null"); // 在日志中输出警告信息
|
|
|
}
|
|
|
} finally {
|
|
|
if (c != null)
|
|
|
c.close(); // 关闭Cursor
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public boolean setContent(JSONObject js) {
|
|
|
try {
|
|
|
// 从传入的JSONObject中获取note对象
|
|
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
|
// 检查便签类型
|
|
|
if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
|
|
|
Log.w(TAG, "cannot set system folder"); // 不能设置系统文件夹,记录警告信息
|
|
|
} else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) {
|
|
|
// 对于文件夹,我们只能更新摘要和类型
|
|
|
|
|
|
// 获取摘要内容
|
|
|
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;
|
|
|
if (mIsCreate || mType != type) {
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type); // 如果是新创建的便签或者类型有变化,将类型添加到差异值列表中
|
|
|
}
|
|
|
mType = type; // 更新当前便签对象的类型值
|
|
|
} else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) {
|
|
|
// 对于便签,我们需要更新更多的属性
|
|
|
|
|
|
// 获取便签数据数组
|
|
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
|
|
// 获取便签id
|
|
|
long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID;
|
|
|
if (mIsCreate || mId != id) {
|
|
|
mDiffNoteValues.put(NoteColumns.ID, id); // 如果是新创建的便签或者id有变化,将id添加到差异值列表中
|
|
|
}
|
|
|
mId = id; // 更新当前便签对象的id值
|
|
|
|
|
|
// 获取提醒日期
|
|
|
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; // 更新当前便签对象的提醒日期值
|
|
|
|
|
|
// 获取背景颜色id
|
|
|
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); // 如果是新创建的便签或者背景颜色id有变化,将背景颜色id添加到差异值列表中
|
|
|
}
|
|
|
mBgColorId = bgColorId; // 更新当前便签对象的背景颜色id值
|
|
|
|
|
|
// 获取创建日期
|
|
|
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;
|
|
|
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();
|
|
|
if (mIsCreate || mModifiedDate != modifiedDate) {
|
|
|
mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate); // 如果是新创建的便签或者修改日期有变化,将修改日期添加到差异值列表中
|
|
|
}
|
|
|
mModifiedDate = modifiedDate; // 更新当前便签对象的修改日期值
|
|
|
|
|
|
// 获取父级id
|
|
|
long parentId = note.has(NoteColumns.PARENT_ID) ? note.getLong(NoteColumns.PARENT_ID) : 0;
|
|
|
if (mIsCreate || mParentId != parentId) {
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId); // 如果是新创建的便签或者父级id有变化,将父级id添加到差异值列表中
|
|
|
}
|
|
|
mParentId = parentId; // 更新当前便签对象的父级id值
|
|
|
|
|
|
// 获取摘要内容
|
|
|
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;
|
|
|
if (mIsCreate || mType != type) {
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type); // 如果是新创建的便签或者类型有变化,将类型添加到差异值列表中
|
|
|
}
|
|
|
mType = type; // 更新当前便签对象的类型值
|
|
|
|
|
|
// 获取小部件ID
|
|
|
int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID) : AppWidgetManager.INVALID_APPWIDGET_ID;
|
|
|
if (mIsCreate || mWidgetId != widgetId) {
|
|
|
mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId); // 如果是新创建的便签或者小部件ID有变化,将小部件ID添加到差异值列表中
|
|
|
}
|
|
|
mWidgetId = widgetId; // 更新当前便签对象的小部件ID值
|
|
|
|
|
|
// 获取小部件类型
|
|
|
int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note.getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE;
|
|
|
if (mIsCreate || mWidgetType != widgetType) {
|
|
|
mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType); // 如果是新创建的便签或者小部件类型有变化,将小部件类型添加到差异值列表中
|
|
|
}
|
|
|
mWidgetType = widgetType; // 更新当前便签对象的小部件类型值
|
|
|
|
|
|
// 获取原始父级id
|
|
|
long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note.getLong(NoteColumns.ORIGIN_PARENT_ID) : 0;
|
|
|
if (mIsCreate || mOriginParent != originParent) {
|
|
|
mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent); // 如果是新创建的便签或者原始父级id有变化,将原始父级id添加到差异值列表中
|
|
|
}
|
|
|
mOriginParent = originParent; // 更新当前便签对象的原始父级id值
|
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
// 获取当前数据对象
|
|
|
JSONObject data = dataArray.getJSONObject(i);
|
|
|
SqlData sqlData = null;
|
|
|
if (data.has(DataColumns.ID)) {
|
|
|
long dataId = data.getLong(DataColumns.ID);
|
|
|
for (SqlData temp : mDataList) {
|
|
|
if (dataId == temp.getId()) {
|
|
|
sqlData = temp; // 找到匹配的数据对象
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (sqlData == null) {
|
|
|
sqlData = new SqlData(mContext); // 如果未找到匹配的数据对象,则创建新的数据对象并添加到列表中
|
|
|
mDataList.add(sqlData);
|
|
|
}
|
|
|
|
|
|
sqlData.setContent(data); // 将数据内容设置到对应的数据对象中
|
|
|
}
|
|
|
}
|
|
|
} catch (JSONException e) {
|
|
|
Log.e(TAG, e.toString()); // 捕获 JSON 解析异常,记录错误日志
|
|
|
e.printStackTrace(); // 打印堆栈信息
|
|
|
return false; // 操作失败
|
|
|
}
|
|
|
return true; // 操作成功
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取数据内容,返回一个JSONObject对象
|
|
|
* @return JSONObject 数据内容
|
|
|
*/
|
|
|
public JSONObject getContent() {
|
|
|
try {
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
// 检查是否已经在数据库中创建该数据对象
|
|
|
if (mIsCreate) {
|
|
|
Log.e(TAG, "it seems that we haven't created this in database yet");
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
JSONObject note = new JSONObject();
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
// 将笔记的各个属性设置到note对象中
|
|
|
note.put(NoteColumns.ID, mId); // 设置笔记ID
|
|
|
note.put(NoteColumns.ALERTED_DATE, mAlertDate); // 设置提醒日期
|
|
|
note.put(NoteColumns.BG_COLOR_ID, mBgColorId); // 设置背景颜色ID
|
|
|
note.put(NoteColumns.CREATED_DATE, mCreatedDate); // 设置创建日期
|
|
|
note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment); // 设置是否有附件
|
|
|
note.put(NoteColumns.MODIFIED_DATE, mModifiedDate); // 设置修改日期
|
|
|
note.put(NoteColumns.PARENT_ID, mParentId); // 设置父级ID
|
|
|
note.put(NoteColumns.SNIPPET, mSnippet); // 设置摘要
|
|
|
note.put(NoteColumns.TYPE, mType); // 设置类型
|
|
|
note.put(NoteColumns.WIDGET_ID, mWidgetId); // 设置小部件ID
|
|
|
note.put(NoteColumns.WIDGET_TYPE, mWidgetType); // 设置小部件类型
|
|
|
note.put(NoteColumns.ORIGIN_PARENT_ID, mOriginParent); // 设置原始父级ID
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note); // 将note对象放入js中
|
|
|
|
|
|
JSONArray dataArray = new JSONArray();
|
|
|
// 遍历数据列表,将每个数据对象的内容设置到dataArray中
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
JSONObject data = sqlData.getContent();
|
|
|
if (data != null) {
|
|
|
dataArray.put(data);
|
|
|
}
|
|
|
}
|
|
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); // 将dataArray放入js中
|
|
|
} else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) {
|
|
|
// 将文件夹或系统类型的笔记的属性设置到note对象中
|
|
|
note.put(NoteColumns.ID, mId); // 设置笔记ID
|
|
|
note.put(NoteColumns.TYPE, mType); // 设置类型
|
|
|
note.put(NoteColumns.SNIPPET, mSnippet); // 设置摘要
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note); // 将note对象放入js中
|
|
|
}
|
|
|
|
|
|
return js; // 返回包含数据内容的JSONObject对象
|
|
|
} catch (JSONException e) {
|
|
|
Log.e(TAG, e.toString());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null; // 发生异常时返回null
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置父级ID
|
|
|
* @param id 父级ID
|
|
|
*/
|
|
|
public void setParentId(long id) {
|
|
|
mParentId = id;
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, id); // 将父级ID存入mDiffNoteValues
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置GTask ID
|
|
|
* @param gid GTask ID
|
|
|
*/
|
|
|
public void setGtaskId(String gid) {
|
|
|
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); // 将GTask ID存入mDiffNoteValues
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置同步ID
|
|
|
* @param syncId 同步ID
|
|
|
*/
|
|
|
public void setSyncId(long syncId) {
|
|
|
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); // 将同步ID存入mDiffNoteValues
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 重置本地修改状态
|
|
|
*/
|
|
|
public void resetLocalModified() {
|
|
|
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); // 将本地修改状态重置为0
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取笔记ID
|
|
|
* @return 笔记ID
|
|
|
*/
|
|
|
public long getId() {
|
|
|
return mId; // 返回笔记ID
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取父级ID
|
|
|
* @return 父级ID
|
|
|
*/
|
|
|
public long getParentId() {
|
|
|
return mParentId; // 返回父级ID
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取摘要
|
|
|
* @return 摘要
|
|
|
*/
|
|
|
public String getSnippet() {
|
|
|
return mSnippet; // 返回摘要
|
|
|
}
|
|
|
|
|
|
|
|
|
public boolean isNoteType() { // 判断是否为笔记类型的方法
|
|
|
return mType == Notes.TYPE_NOTE; // 返回mType是否等于Notes.TYPE_NOTE
|
|
|
}
|
|
|
|
|
|
public void commit(boolean validateVersion) { // 提交方法,参数为是否验证版本号
|
|
|
if (mIsCreate) { // 如果是新建
|
|
|
if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) { // 如果mId为无效ID且mDiffNoteValues包含NoteColumns.ID
|
|
|
mDiffNoteValues.remove(NoteColumns.ID); // 移除mDiffNoteValues中的NoteColumns.ID
|
|
|
}
|
|
|
|
|
|
Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues); // 插入笔记内容到数据库
|
|
|
try {
|
|
|
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"); // 抛出创建笔记失败的异常
|
|
|
}
|
|
|
if (mId == 0) {
|
|
|
throw new IllegalStateException("Create thread id failed"); // 抛出创建线程ID失败的异常
|
|
|
}
|
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) { // 如果是笔记类型
|
|
|
for (SqlData sqlData : mDataList) { // 遍历数据列表
|
|
|
sqlData.commit(mId, false, -1); // 提交数据
|
|
|
}
|
|
|
}
|
|
|
} else { // 如果不是新建
|
|
|
// 省略部分代码...
|
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) { // 如果是笔记类型
|
|
|
for (SqlData sqlData : mDataList) { // 遍历数据列表
|
|
|
sqlData.commit(mId, validateVersion, mVersion); // 提交数据
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 刷新本地信息
|
|
|
loadFromCursor(mId); // 从游标加载信息
|
|
|
if (mType == Notes.TYPE_NOTE) // 如果是笔记类型
|
|
|
loadDataContent(); // 加载数据内容
|
|
|
|
|
|
mDiffNoteValues.clear(); // 清空差异笔记值
|
|
|
mIsCreate = false; // 设置为非新建状态
|
|
|
}
|
|
|
}
|