|
|
|
@ -37,12 +37,18 @@ import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SqlNote类用于处理笔记的数据库操作,包括创建、读取、更新和删除操作
|
|
|
|
|
* 支持与JSON格式数据的相互转换,方便与Google Tasks等外部服务同步
|
|
|
|
|
* 同时管理笔记的基本信息和相关数据项
|
|
|
|
|
*/
|
|
|
|
|
public class SqlNote {
|
|
|
|
|
private static final String TAG = SqlNote.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
// 无效ID,用于标识尚未创建的笔记
|
|
|
|
|
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,
|
|
|
|
@ -52,76 +58,83 @@ public class SqlNote {
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 笔记ID
|
|
|
|
|
private long mId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 提醒日期
|
|
|
|
|
private long mAlertDate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 背景颜色ID
|
|
|
|
|
private int mBgColorId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 创建日期
|
|
|
|
|
private long mCreatedDate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 是否有附件的标志
|
|
|
|
|
private int mHasAttachment;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 修改日期
|
|
|
|
|
private long mModifiedDate;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 父文件夹ID
|
|
|
|
|
private long mParentId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 摘要内容
|
|
|
|
|
private String mSnippet;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 笔记类型(普通笔记、文件夹、系统文件夹)
|
|
|
|
|
private int mType;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 桌面小部件ID
|
|
|
|
|
private int mWidgetId;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 桌面小部件类型
|
|
|
|
|
private int mWidgetType;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 原始父文件夹ID
|
|
|
|
|
private long mOriginParent;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 版本号
|
|
|
|
|
private long mVersion;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 记录笔记变更的ContentValues
|
|
|
|
|
private ContentValues mDiffNoteValues;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 笔记关联的数据列表
|
|
|
|
|
private ArrayList<SqlData> mDataList;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,创建一个新的空笔记
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
*/
|
|
|
|
|
public SqlNote(Context context) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
@ -143,6 +156,11 @@ public class SqlNote {
|
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,从Cursor加载现有笔记
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
* @param c 包含笔记数据的Cursor
|
|
|
|
|
*/
|
|
|
|
|
public SqlNote(Context context, Cursor c) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
@ -154,6 +172,11 @@ public class SqlNote {
|
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,根据ID从数据库加载笔记
|
|
|
|
|
* @param context 应用上下文
|
|
|
|
|
* @param id 笔记ID
|
|
|
|
|
*/
|
|
|
|
|
public SqlNote(Context context, long id) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
@ -163,9 +186,12 @@ public class SqlNote {
|
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
|
loadDataContent();
|
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从数据库根据ID加载笔记数据
|
|
|
|
|
* @param id 笔记ID
|
|
|
|
|
*/
|
|
|
|
|
private void loadFromCursor(long id) {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
try {
|
|
|
|
@ -185,6 +211,10 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从Cursor加载笔记数据到对象中
|
|
|
|
|
* @param c 包含笔记数据的Cursor
|
|
|
|
|
*/
|
|
|
|
|
private void loadFromCursor(Cursor c) {
|
|
|
|
|
mId = c.getLong(ID_COLUMN);
|
|
|
|
|
mAlertDate = c.getLong(ALERTED_DATE_COLUMN);
|
|
|
|
@ -200,6 +230,9 @@ public class SqlNote {
|
|
|
|
|
mVersion = c.getLong(VERSION_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加载与笔记关联的数据内容
|
|
|
|
|
*/
|
|
|
|
|
private void loadDataContent() {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
mDataList.clear();
|
|
|
|
@ -226,13 +259,22 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从JSON对象设置笔记内容
|
|
|
|
|
* @param js 包含笔记内容的JSON对象
|
|
|
|
|
* @return 设置成功返回true,失败返回false
|
|
|
|
|
*/
|
|
|
|
|
public boolean setContent(JSONObject js) {
|
|
|
|
|
try {
|
|
|
|
|
// 获取笔记元数据
|
|
|
|
|
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) {
|
|
|
|
|
// for folder we can only update the snnipet and type
|
|
|
|
|
}
|
|
|
|
|
// 文件夹类型只能更新摘要和类型
|
|
|
|
|
else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) {
|
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
|
|
|
@ -246,8 +288,12 @@ public class SqlNote {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type);
|
|
|
|
|
}
|
|
|
|
|
mType = type;
|
|
|
|
|
} else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) {
|
|
|
|
|
}
|
|
|
|
|
// 普通笔记类型可以更新全部属性
|
|
|
|
|
else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) {
|
|
|
|
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
|
|
|
|
|
|
// 从JSON中提取笔记各属性值,并与当前值比较,记录差异
|
|
|
|
|
long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID;
|
|
|
|
|
if (mIsCreate || mId != id) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.ID, id);
|
|
|
|
@ -331,9 +377,12 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
mOriginParent = originParent;
|
|
|
|
|
|
|
|
|
|
// 处理笔记关联的数据项
|
|
|
|
|
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) {
|
|
|
|
@ -343,11 +392,13 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果数据项不存在,则创建新的
|
|
|
|
|
if (sqlData == null) {
|
|
|
|
|
sqlData = new SqlData(mContext);
|
|
|
|
|
mDataList.add(sqlData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置数据项内容
|
|
|
|
|
sqlData.setContent(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -359,6 +410,10 @@ public class SqlNote {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将笔记内容转换为JSON对象
|
|
|
|
|
* @return 包含笔记内容的JSON对象
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject getContent() {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
@ -370,6 +425,7 @@ public class SqlNote {
|
|
|
|
|
|
|
|
|
|
JSONObject note = new JSONObject();
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
// 将笔记各属性添加到JSON对象
|
|
|
|
|
note.put(NoteColumns.ID, mId);
|
|
|
|
|
note.put(NoteColumns.ALERTED_DATE, mAlertDate);
|
|
|
|
|
note.put(NoteColumns.BG_COLOR_ID, mBgColorId);
|
|
|
|
@ -384,6 +440,7 @@ public class SqlNote {
|
|
|
|
|
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();
|
|
|
|
@ -393,6 +450,7 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
@ -407,45 +465,82 @@ public class SqlNote {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置笔记的父文件夹ID
|
|
|
|
|
* @param id 父文件夹ID
|
|
|
|
|
*/
|
|
|
|
|
public void setParentId(long id) {
|
|
|
|
|
mParentId = id;
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置Google Tasks ID
|
|
|
|
|
* @param gid Google Tasks ID
|
|
|
|
|
*/
|
|
|
|
|
public void setGtaskId(String gid) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置同步ID
|
|
|
|
|
* @param syncId 同步ID
|
|
|
|
|
*/
|
|
|
|
|
public void setSyncId(long syncId) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 重置本地修改标志
|
|
|
|
|
*/
|
|
|
|
|
public void resetLocalModified() {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取笔记ID
|
|
|
|
|
* @return 笔记ID
|
|
|
|
|
*/
|
|
|
|
|
public long getId() {
|
|
|
|
|
return mId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取父文件夹ID
|
|
|
|
|
* @return 父文件夹ID
|
|
|
|
|
*/
|
|
|
|
|
public long getParentId() {
|
|
|
|
|
return mParentId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取笔记摘要
|
|
|
|
|
* @return 笔记摘要
|
|
|
|
|
*/
|
|
|
|
|
public String getSnippet() {
|
|
|
|
|
return mSnippet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 判断是否为普通笔记类型
|
|
|
|
|
* @return 是普通笔记返回true,否则返回false
|
|
|
|
|
*/
|
|
|
|
|
public boolean isNoteType() {
|
|
|
|
|
return mType == Notes.TYPE_NOTE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 将笔记变更提交到数据库
|
|
|
|
|
* @param validateVersion 是否验证版本,防止并发修改冲突
|
|
|
|
|
*/
|
|
|
|
|
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));
|
|
|
|
@ -457,12 +552,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 +568,13 @@ public class SqlNote {
|
|
|
|
|
mVersion ++;
|
|
|
|
|
int result = 0;
|
|
|
|
|
if (!validateVersion) {
|
|
|
|
|
// 不验证版本直接更新
|
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
|
+ NoteColumns.ID + "=?)", new String[] {
|
|
|
|
|
String.valueOf(mId)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// 验证版本,确保只更新版本号小于等于当前版本的记录
|
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
|
+ NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
|
|
|
|
|
new String[] {
|
|
|
|
@ -487,6 +586,7 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果是普通笔记,提交关联的数据项
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
|
sqlData.commit(mId, validateVersion, mVersion);
|
|
|
|
@ -494,12 +594,13 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// refresh local info
|
|
|
|
|
// 提交后刷新本地信息
|
|
|
|
|
loadFromCursor(mId);
|
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
|
loadDataContent();
|
|
|
|
|
|
|
|
|
|
// 清除变更记录
|
|
|
|
|
mDiffNoteValues.clear();
|
|
|
|
|
mIsCreate = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|