|
|
|
@ -43,198 +43,184 @@ public class SqlNote {
|
|
|
|
|
|
|
|
|
|
private static final int INVALID_ID = -99999;
|
|
|
|
|
|
|
|
|
|
// 定义查询笔记的投影,包含需要查询的列
|
|
|
|
|
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,
|
|
|
|
|
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
|
|
|
|
|
NoteColumns.ID, // 笔记ID
|
|
|
|
|
NoteColumns.ALERTED_DATE, // 提醒时间
|
|
|
|
|
NoteColumns.BG_COLOR_ID, // 背景颜色
|
|
|
|
|
NoteColumns.CREATED_DATE, // 创建时间
|
|
|
|
|
NoteColumns.HAS_ATTACHMENT, // 是否有附件
|
|
|
|
|
NoteColumns.MODIFIED_DATE, // 修改时间
|
|
|
|
|
NoteColumns.NOTES_COUNT, // 子笔记数量
|
|
|
|
|
NoteColumns.PARENT_ID, // 父笔记ID
|
|
|
|
|
NoteColumns.SNIPPET, // 摘要
|
|
|
|
|
NoteColumns.TYPE, // 类型
|
|
|
|
|
NoteColumns.WIDGET_ID, // Widget ID
|
|
|
|
|
NoteColumns.WIDGET_TYPE, // Widget 类型
|
|
|
|
|
NoteColumns.SYNC_ID, // 同步ID
|
|
|
|
|
NoteColumns.LOCAL_MODIFIED, // 本地修改标记
|
|
|
|
|
NoteColumns.ORIGIN_PARENT_ID, // 原始父笔记ID
|
|
|
|
|
NoteColumns.GTASK_ID, // Google Task ID
|
|
|
|
|
NoteColumns.VERSION // 版本号
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public static final int ID_COLUMN = 0;
|
|
|
|
|
|
|
|
|
|
public static final int ALERTED_DATE_COLUMN = 1;
|
|
|
|
|
|
|
|
|
|
public static final int BG_COLOR_ID_COLUMN = 2;
|
|
|
|
|
|
|
|
|
|
public static final int CREATED_DATE_COLUMN = 3;
|
|
|
|
|
|
|
|
|
|
public static final int HAS_ATTACHMENT_COLUMN = 4;
|
|
|
|
|
|
|
|
|
|
public static final int MODIFIED_DATE_COLUMN = 5;
|
|
|
|
|
|
|
|
|
|
public static final int NOTES_COUNT_COLUMN = 6;
|
|
|
|
|
|
|
|
|
|
public static final int PARENT_ID_COLUMN = 7;
|
|
|
|
|
|
|
|
|
|
public static final int SNIPPET_COLUMN = 8;
|
|
|
|
|
|
|
|
|
|
public static final int TYPE_COLUMN = 9;
|
|
|
|
|
|
|
|
|
|
public static final int WIDGET_ID_COLUMN = 10;
|
|
|
|
|
|
|
|
|
|
public static final int WIDGET_TYPE_COLUMN = 11;
|
|
|
|
|
|
|
|
|
|
public static final int SYNC_ID_COLUMN = 12;
|
|
|
|
|
|
|
|
|
|
public static final int LOCAL_MODIFIED_COLUMN = 13;
|
|
|
|
|
|
|
|
|
|
public static final int ORIGIN_PARENT_ID_COLUMN = 14;
|
|
|
|
|
|
|
|
|
|
public static final int GTASK_ID_COLUMN = 15;
|
|
|
|
|
|
|
|
|
|
public static final int VERSION_COLUMN = 16;
|
|
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
|
|
private ContentResolver mContentResolver;
|
|
|
|
|
|
|
|
|
|
private boolean mIsCreate;
|
|
|
|
|
|
|
|
|
|
private long mId;
|
|
|
|
|
|
|
|
|
|
private long mAlertDate;
|
|
|
|
|
|
|
|
|
|
private int mBgColorId;
|
|
|
|
|
|
|
|
|
|
private long mCreatedDate;
|
|
|
|
|
|
|
|
|
|
private int mHasAttachment;
|
|
|
|
|
|
|
|
|
|
private long mModifiedDate;
|
|
|
|
|
|
|
|
|
|
private long mParentId;
|
|
|
|
|
|
|
|
|
|
private String mSnippet;
|
|
|
|
|
|
|
|
|
|
private int mType;
|
|
|
|
|
|
|
|
|
|
private int mWidgetId;
|
|
|
|
|
|
|
|
|
|
private int mWidgetType;
|
|
|
|
|
|
|
|
|
|
private long mOriginParent;
|
|
|
|
|
|
|
|
|
|
private long mVersion;
|
|
|
|
|
|
|
|
|
|
private ContentValues mDiffNoteValues;
|
|
|
|
|
|
|
|
|
|
private ArrayList<SqlData> mDataList;
|
|
|
|
|
public static final int ID_COLUMN = 0; // ID 列索引
|
|
|
|
|
public static final int ALERTED_DATE_COLUMN = 1; // ALERTED_DATE 列索引
|
|
|
|
|
public static final int BG_COLOR_ID_COLUMN = 2; // BG_COLOR_ID 列索引
|
|
|
|
|
public static final int CREATED_DATE_COLUMN = 3; // CREATED_DATE 列索引
|
|
|
|
|
public static final int HAS_ATTACHMENT_COLUMN = 4; // HAS_ATTACHMENT 列索引
|
|
|
|
|
public static final int MODIFIED_DATE_COLUMN = 5; // MODIFIED_DATE 列索引
|
|
|
|
|
public static final int NOTES_COUNT_COLUMN = 6; // NOTES_COUNT 列索引
|
|
|
|
|
public static final int PARENT_ID_COLUMN = 7; // PARENT_ID 列索引
|
|
|
|
|
public static final int SNIPPET_COLUMN = 8; // SNIPPET 列索引
|
|
|
|
|
public static final int TYPE_COLUMN = 9; // TYPE 列索引
|
|
|
|
|
public static final int WIDGET_ID_COLUMN = 10; // WIDGET_ID 列索引
|
|
|
|
|
public static final int WIDGET_TYPE_COLUMN = 11; // WIDGET_TYPE 列索引
|
|
|
|
|
public static final int SYNC_ID_COLUMN = 12; // SYNC_ID 列索引
|
|
|
|
|
public static final int LOCAL_MODIFIED_COLUMN = 13; // LOCAL_MODIFIED 列索引
|
|
|
|
|
public static final int ORIGIN_PARENT_ID_COLUMN = 14; // ORIGIN_PARENT_ID 列索引
|
|
|
|
|
public static final int GTASK_ID_COLUMN = 15; // GTASK_ID 列索引
|
|
|
|
|
public static final int VERSION_COLUMN = 16; // VERSION 列索引
|
|
|
|
|
|
|
|
|
|
private Context mContext; // 上下文对象
|
|
|
|
|
private ContentResolver mContentResolver; // ContentResolver 对象
|
|
|
|
|
private boolean mIsCreate; // 是否是新建数据的标记
|
|
|
|
|
private long mId; // 数据ID
|
|
|
|
|
private long mAlertDate; // 提醒时间
|
|
|
|
|
private int mBgColorId; // 背景颜色
|
|
|
|
|
private long mCreatedDate; // 创建时间
|
|
|
|
|
private int mHasAttachment; // 是否有附件
|
|
|
|
|
private long mModifiedDate; // 修改时间
|
|
|
|
|
private long mParentId; // 父笔记ID
|
|
|
|
|
private String mSnippet; // 摘要
|
|
|
|
|
private int mType; // 类型
|
|
|
|
|
private int mWidgetId; // Widget ID
|
|
|
|
|
private int mWidgetType; // Widget 类型
|
|
|
|
|
private long mOriginParent; // 原始父笔记ID
|
|
|
|
|
private long mVersion; // 版本号
|
|
|
|
|
private ContentValues mDiffNoteValues; // 笔记数据差异值
|
|
|
|
|
private ArrayList<SqlData> mDataList; // SqlData 列表
|
|
|
|
|
|
|
|
|
|
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(); // 初始化 ContentResolver 对象
|
|
|
|
|
mIsCreate = true; // 标记为新建数据
|
|
|
|
|
mId = INVALID_ID; // 初始化 ID 为无效值
|
|
|
|
|
mAlertDate = 0; // 初始化提醒时间为 0
|
|
|
|
|
mBgColorId = ResourceParser.getDefaultBgId(context);// 初始化背景颜色为默认值
|
|
|
|
|
mCreatedDate = System.currentTimeMillis(); // 初始化创建时间为当前时间
|
|
|
|
|
mHasAttachment = 0; // 初始化是否有附件为 0
|
|
|
|
|
mModifiedDate = System.currentTimeMillis(); // 初始化修改时间为当前时间
|
|
|
|
|
mParentId = 0; // 初始化父笔记 ID 为 0
|
|
|
|
|
mSnippet = ""; // 初始化摘要为空字符串
|
|
|
|
|
mType = Notes.TYPE_NOTE; // 初始化类型为普通笔记
|
|
|
|
|
mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; // 初始化 Widget ID 为无效值
|
|
|
|
|
mWidgetType = Notes.TYPE_WIDGET_INVALIDE; // 初始化 Widget 类型为无效类型
|
|
|
|
|
mOriginParent = 0; // 初始化原始父笔记 ID 为 0
|
|
|
|
|
mVersion = 0; // 初始化版本号为 0
|
|
|
|
|
mDiffNoteValues = new ContentValues(); // 初始化笔记数据差异值为空 ContentValues 对象
|
|
|
|
|
mDataList = new ArrayList<SqlData>(); // 初始化 SqlData 列表为空列表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SqlNote(Context context, Cursor c) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
|
mIsCreate = false;
|
|
|
|
|
loadFromCursor(c);
|
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
|
loadDataContent();
|
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
|
mContext = context; // 初始化上下文对象
|
|
|
|
|
mContentResolver = context.getContentResolver(); // 初始化 ContentResolver 对象
|
|
|
|
|
mIsCreate = false; // 标记为非新建数据
|
|
|
|
|
loadFromCursor(c); // 从 Cursor 中加载数据
|
|
|
|
|
mDataList = new ArrayList<SqlData>(); // 初始化 SqlData 列表为空列表
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) // 如果类型为普通笔记
|
|
|
|
|
loadDataContent(); // 加载笔记内容数据
|
|
|
|
|
mDiffNoteValues = new ContentValues(); // 初始化笔记数据差异值为新的 ContentValues 对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SqlNote(Context context, long id) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
|
mIsCreate = false;
|
|
|
|
|
loadFromCursor(id);
|
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
|
loadDataContent();
|
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
|
|
|
|
|
|
mContext = context; // 初始化上下文对象
|
|
|
|
|
mContentResolver = context.getContentResolver(); // 初始化 ContentResolver 对象
|
|
|
|
|
mIsCreate = false; // 标记为非新建数据
|
|
|
|
|
loadFromCursor(id); // 从数据库中加载指定 ID 的数据
|
|
|
|
|
mDataList = new ArrayList<SqlData>(); // 初始化 SqlData 列表为空列表
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) // 如果类型为普通笔记
|
|
|
|
|
loadDataContent(); // 加载笔记内容数据
|
|
|
|
|
mDiffNoteValues = new ContentValues(); // 初始化笔记数据差异值为新的 ContentValues 对象
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadFromCursor(long id) {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
Cursor c = null;// 初始化游标对象
|
|
|
|
|
try {
|
|
|
|
|
// 通过 ID 查询笔记数据
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)",
|
|
|
|
|
new String[] {
|
|
|
|
|
String.valueOf(id)
|
|
|
|
|
}, null);
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.moveToNext();
|
|
|
|
|
loadFromCursor(c);
|
|
|
|
|
c.moveToNext(); // 移动到第一条数据
|
|
|
|
|
loadFromCursor(c); // 载入数据
|
|
|
|
|
} else {
|
|
|
|
|
Log.w(TAG, "loadFromCursor: cursor = null");
|
|
|
|
|
Log.w(TAG, "loadFromCursor: cursor = null");// 输出警告日志
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (c != null)
|
|
|
|
|
c.close();
|
|
|
|
|
c.close(); // 关闭游标
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
Cursor c = null; // 初始化游标对象
|
|
|
|
|
mDataList.clear(); // 清空数据列表
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 通过笔记ID查询数据内容
|
|
|
|
|
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");
|
|
|
|
|
Log.w(TAG, "it seems that the note has not data");// 输出警告日志
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
SqlData data = new SqlData(mContext, c);
|
|
|
|
|
mDataList.add(data);
|
|
|
|
|
SqlData data = new SqlData(mContext, c); // 创建数据对象
|
|
|
|
|
mDataList.add(data); // 将数据对象添加到数据列表中
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.w(TAG, "loadDataContent: cursor = null");
|
|
|
|
|
Log.w(TAG, "loadDataContent: cursor = null");// 输出警告日志
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (c != null)
|
|
|
|
|
c.close();
|
|
|
|
|
c.close();// 关闭游标
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean setContent(JSONObject js) {
|
|
|
|
|
try {
|
|
|
|
|
// 解析JSON对象中的note字段
|
|
|
|
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
// 检查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) {
|
|
|
|
|
// 如果类型为文件夹类型,则更新snippet和type
|
|
|
|
|
// for folder we can only update the snnipet and type
|
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
|
// 如果正在创建一个新的note,或者之前的snippet和当前传入的snippet不相等,则更新snippet字段
|
|
|
|
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
|
|
|
|
|
}
|
|
|
|
@ -242,98 +228,50 @@ public class SqlNote {
|
|
|
|
|
|
|
|
|
|
int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
|
|
|
|
|
: Notes.TYPE_NOTE;
|
|
|
|
|
// 如果正在创建一个新的note,或者之前的type和当前传入的type不相等,则更新type字段
|
|
|
|
|
if (mIsCreate || mType != type) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type);
|
|
|
|
|
}
|
|
|
|
|
mType = type;
|
|
|
|
|
}else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) {
|
|
|
|
|
// 如果类型为笔记类型,则解析data数组
|
|
|
|
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
|
// 获取note的ID字段,如果没有则设置为INVALID_ID常量值
|
|
|
|
|
long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID;
|
|
|
|
|
// 如果正在创建一个新的note,或者之前的id和当前传入的id不相等,则更新ID字段
|
|
|
|
|
if (mIsCreate || mId != id) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.ID, id);
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
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();
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
long parentId = note.has(NoteColumns.PARENT_ID) ? note
|
|
|
|
|
.getLong(NoteColumns.PARENT_ID) : 0;
|
|
|
|
|
if (mIsCreate || mParentId != parentId) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId);
|
|
|
|
|
}
|
|
|
|
|
mParentId = parentId;
|
|
|
|
|
|
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
|
// 解析snippet和type字段
|
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
|
// 如果正在创建一个新的note,或者之前的snippet和当前传入的snippet不相等,则更新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;
|
|
|
|
|
// 如果正在创建一个新的note,或者之前的type和当前传入的type不相等,则更新type字段
|
|
|
|
|
if (mIsCreate || mType != type) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type);
|
|
|
|
|
}
|
|
|
|
|
mType = type;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
mWidgetId = widgetId;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
mOriginParent = originParent;
|
|
|
|
|
|
|
|
|
|
// 遍历data数组
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
|
JSONObject data = dataArray.getJSONObject(i);
|
|
|
|
|
SqlData sqlData = null;
|
|
|
|
|
|
|
|
|
|
// 检查data数组中的id字段,并查找对应的SqlData对象
|
|
|
|
|
if (data.has(DataColumns.ID)) {
|
|
|
|
|
long dataId = data.getLong(DataColumns.ID);
|
|
|
|
|
for (SqlData temp : mDataList) {
|
|
|
|
@ -343,111 +281,130 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果未找到对应的SqlData对象,则创建一个新的对象并添加到数据列表中
|
|
|
|
|
if (sqlData == null) {
|
|
|
|
|
sqlData = new SqlData(mContext);
|
|
|
|
|
mDataList.add(sqlData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 调用SqlData对象的setContent方法,传递JSON数据进行处理
|
|
|
|
|
sqlData.setContent(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
// 处理异常情况,打印错误信息并返回false
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回true,表示设置内容成功
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.put(NoteColumns.ID, mId);
|
|
|
|
|
note.put(NoteColumns.ALERTED_DATE, mAlertDate);
|
|
|
|
|
note.put(NoteColumns.BG_COLOR_ID, mBgColorId);
|
|
|
|
|
note.put(NoteColumns.CREATED_DATE, mCreatedDate);
|
|
|
|
|
note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment);
|
|
|
|
|
note.put(NoteColumns.MODIFIED_DATE, mModifiedDate);
|
|
|
|
|
note.put(NoteColumns.PARENT_ID, mParentId);
|
|
|
|
|
note.put(NoteColumns.SNIPPET, mSnippet);
|
|
|
|
|
note.put(NoteColumns.TYPE, mType);
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
JSONArray dataArray = new JSONArray();
|
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
|
JSONObject data = sqlData.getContent();
|
|
|
|
|
if (data != null) {
|
|
|
|
|
dataArray.put(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
|
|
|
|
|
} 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);
|
|
|
|
|
public JSONObject getContent() {// 定义一个返回JSONObject类型的方法getContent()
|
|
|
|
|
try {// 尝试执行以下代码块
|
|
|
|
|
// 创建一个空的 JSONObject 对象
|
|
|
|
|
JSONObject js = new JSONObject();// 实例化一个空的JSONObject对象
|
|
|
|
|
|
|
|
|
|
// 如果笔记还没有创建到数据库中,则返回 null
|
|
|
|
|
if (mIsCreate) {// 判断mIsCreate属性值
|
|
|
|
|
Log.e(TAG, "it seems that we haven't created this in database yet");// 打印错误日志
|
|
|
|
|
return null;// 返回null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据笔记类型创建不同的 JSONObject
|
|
|
|
|
JSONObject note = new JSONObject(); // 实例化一个JSONObject对象
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) { // 判断mType属性值是否为Notes.TYPE_NOTE
|
|
|
|
|
note.put(NoteColumns.ID, mId); // 向note对象中添加ID键值对
|
|
|
|
|
note.put(NoteColumns.ALERTED_DATE, mAlertDate); // 添加ALERTED_DATE键值对
|
|
|
|
|
note.put(NoteColumns.BG_COLOR_ID, mBgColorId); // 添加BG_COLOR_ID键值对
|
|
|
|
|
note.put(NoteColumns.CREATED_DATE, mCreatedDate); // 添加CREATED_DATE键值对
|
|
|
|
|
note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment); // 添加HAS_ATTACHMENT键值对
|
|
|
|
|
note.put(NoteColumns.MODIFIED_DATE, mModifiedDate); // 添加MODIFIED_DATE键值对
|
|
|
|
|
note.put(NoteColumns.PARENT_ID, mParentId); // 添加PARENT_ID键值对
|
|
|
|
|
note.put(NoteColumns.SNIPPET, mSnippet); // 添加SNIPPET键值对
|
|
|
|
|
note.put(NoteColumns.TYPE, mType); // 添加TYPE键值对
|
|
|
|
|
note.put(NoteColumns.WIDGET_ID, mWidgetId); // 添加WIDGET_ID键值对
|
|
|
|
|
note.put(NoteColumns.WIDGET_TYPE, mWidgetType); // 添加WIDGET_TYPE键值对
|
|
|
|
|
note.put(NoteColumns.ORIGIN_PARENT_ID, mOriginParent); // 添加ORIGIN_PARENT_ID键值对
|
|
|
|
|
// 将笔记信息添加为 META_HEAD_NOTE 的值
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note); // 将note对象添加为js对象的META_HEAD_NOTE键值对
|
|
|
|
|
|
|
|
|
|
// 如果是笔记类型,将其附加数据添加到 META_HEAD_DATA 中
|
|
|
|
|
JSONArray dataArray = new JSONArray(); // 实例化一个JSONArray对象
|
|
|
|
|
for (SqlData sqlData : mDataList) { // 遍历mDataList列表
|
|
|
|
|
JSONObject data = sqlData.getContent(); // 调用sqlData对象的getContent方法并将返回值赋给data
|
|
|
|
|
if (data != null) { // 判断data是否为null
|
|
|
|
|
dataArray.put(data); // 将data对象添加到dataArray中
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);// 将dataArray对象添加为js对象的META_HEAD_DATA键值对
|
|
|
|
|
} else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) { // 判断mType属性值是否为Notes.TYPE_FOLDER或Notes.TYPE_SYSTEM
|
|
|
|
|
note.put(NoteColumns.ID, mId); // 添加ID键值对
|
|
|
|
|
note.put(NoteColumns.TYPE, mType); // 添加TYPE键值对
|
|
|
|
|
note.put(NoteColumns.SNIPPET, mSnippet); // 添加SNIPPET键值对
|
|
|
|
|
// 将笔记信息添加为 META_HEAD_NOTE 的值
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note); // 将note对象添加为js对象的META_HEAD_NOTE键值对
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (JSONException e) { // 捕获JSONException异常
|
|
|
|
|
Log.e(TAG, e.toString()); // 打印异常信息
|
|
|
|
|
e.printStackTrace(); // 打印异常轨迹
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
// 如果出现异常,则返回 null
|
|
|
|
|
return null; // 返回null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setParentId(long id) {
|
|
|
|
|
mParentId = id;
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
|
|
|
|
|
}
|
|
|
|
|
}//设置父节点ID
|
|
|
|
|
|
|
|
|
|
public void setGtaskId(String gid) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//设置Google任务ID
|
|
|
|
|
public void setSyncId(long syncId) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//设置同步ID
|
|
|
|
|
public void resetLocalModified() {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//重置本地修改标记
|
|
|
|
|
public long getId() {
|
|
|
|
|
return mId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取笔记ID
|
|
|
|
|
public long getParentId() {
|
|
|
|
|
return mParentId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取父节点ID
|
|
|
|
|
public String getSnippet() {
|
|
|
|
|
return mSnippet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取摘要信息
|
|
|
|
|
public boolean isNoteType() {
|
|
|
|
|
return mType == Notes.TYPE_NOTE;
|
|
|
|
|
}
|
|
|
|
|
//是否为笔记类型
|
|
|
|
|
|
|
|
|
|
public void commit(boolean validateVersion) {
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
|
// 如果是新建笔记
|
|
|
|
|
if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
|
|
|
|
|
// 如果mId为无效值,并且mDiffNoteValues中包含NoteColumns.ID字段,则移除该字段
|
|
|
|
|
mDiffNoteValues.remove(NoteColumns.ID);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在Notes.CONTENT_NOTE_URI中插入mDiffNoteValues并返回Uri对象
|
|
|
|
|
Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 将Uri路径中的第二个片段转换为long类型,并赋值给mId
|
|
|
|
|
mId = Long.valueOf(uri.getPathSegments().get(1));
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
Log.e(TAG, "Get note id error :" + e.toString());
|
|
|
|
@ -457,12 +414,14 @@ public class SqlNote {
|
|
|
|
|
throw new IllegalStateException("Create thread id failed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果是笔记类型,则循环提交数据内容更改
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
|
sqlData.commit(mId, false, -1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} 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");
|
|
|
|
@ -471,11 +430,13 @@ public class SqlNote {
|
|
|
|
|
mVersion ++;
|
|
|
|
|
int result = 0;
|
|
|
|
|
if (!validateVersion) {
|
|
|
|
|
// 如果不需要验证版本,则直接通过mId更新mDiffNoteValues到数据库中
|
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
|
+ NoteColumns.ID + "=?)", new String[] {
|
|
|
|
|
String.valueOf(mId)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// 如果需要验证版本,则通过mId和mVersion进行更新
|
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
|
+ NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
|
|
|
|
|
new String[] {
|
|
|
|
@ -487,6 +448,7 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果是笔记类型,则循环提交数据内容更改
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
|
sqlData.commit(mId, validateVersion, mVersion);
|
|
|
|
@ -494,11 +456,12 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// refresh local info
|
|
|
|
|
// 刷新本地信息
|
|
|
|
|
loadFromCursor(mId);
|
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
|
loadDataContent();
|
|
|
|
|
|
|
|
|
|
// 清空mDiffNoteValues和将mIsCreate设置为false
|
|
|
|
|
mDiffNoteValues.clear();
|
|
|
|
|
mIsCreate = false;
|
|
|
|
|
}
|
|
|
|
|