|
|
|
@ -38,11 +38,17 @@ import org.json.JSONObject;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SqlNote类,用于封装和操作数据库中的笔记数据。
|
|
|
|
|
*/
|
|
|
|
|
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,84 @@ 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;
|
|
|
|
|
|
|
|
|
|
// 差异笔记值
|
|
|
|
|
private ContentValues mDiffNoteValues;
|
|
|
|
|
|
|
|
|
|
// 数据列表
|
|
|
|
|
private ArrayList<SqlData> mDataList;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,用于创建一个新的SqlNote对象。
|
|
|
|
|
*
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
*/
|
|
|
|
|
public SqlNote(Context context) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
@ -143,6 +157,12 @@ public class SqlNote {
|
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,用于从Cursor加载数据创建SqlNote对象。
|
|
|
|
|
*
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @param c Cursor对象
|
|
|
|
|
*/
|
|
|
|
|
public SqlNote(Context context, Cursor c) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
@ -154,6 +174,12 @@ public class SqlNote {
|
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造函数,用于从笔记ID加载数据创建SqlNote对象。
|
|
|
|
|
*
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
* @param id 笔记ID
|
|
|
|
|
*/
|
|
|
|
|
public SqlNote(Context context, long id) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
@ -163,9 +189,13 @@ 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 +215,11 @@ 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 +235,9 @@ public class SqlNote {
|
|
|
|
|
mVersion = c.getLong(VERSION_COLUMN);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 加载数据内容。
|
|
|
|
|
*/
|
|
|
|
|
private void loadDataContent() {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
mDataList.clear();
|
|
|
|
@ -226,13 +264,19 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置内容。
|
|
|
|
|
*
|
|
|
|
|
* @param js JSON对象
|
|
|
|
|
* @return 是否设置成功
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
// for folder we can only update the snippet and type
|
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
|
|
|
@ -262,40 +306,10 @@ public class SqlNote {
|
|
|
|
|
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) {
|
|
|
|
|
.getInt(NoteColumnsmParentId != parentId) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId);
|
|
|
|
|
}
|
|
|
|
|
mParentId = parentId;
|
|
|
|
|
|
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
|
|
|
@ -331,6 +345,7 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
mOriginParent = originParent;
|
|
|
|
|
|
|
|
|
|
// 处理数据内容
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
|
JSONObject data = dataArray.getJSONObject(i);
|
|
|
|
|
SqlData sqlData = null;
|
|
|
|
@ -357,9 +372,14 @@ public class SqlNote {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONObject getContent() {
|
|
|
|
|
/**
|
|
|
|
|
* 获取内容。
|
|
|
|
|
*
|
|
|
|
|
* @return JSON对象
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject getContent() {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
@ -405,42 +425,85 @@ public class SqlNote {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setParentId(long id) {
|
|
|
|
|
/**
|
|
|
|
|
* 设置父ID。
|
|
|
|
|
*
|
|
|
|
|
* @param id 父ID
|
|
|
|
|
*/
|
|
|
|
|
public void setParentId(long id) {
|
|
|
|
|
mParentId = id;
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setGtaskId(String gid) {
|
|
|
|
|
/**
|
|
|
|
|
* 设置GTask ID。
|
|
|
|
|
*
|
|
|
|
|
* @param gid GTask ID
|
|
|
|
|
*/
|
|
|
|
|
public void setGtaskId(String gid) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setSyncId(long syncId) {
|
|
|
|
|
/**
|
|
|
|
|
* 设置同步ID。
|
|
|
|
|
*
|
|
|
|
|
* @param syncId 同步ID
|
|
|
|
|
*/
|
|
|
|
|
public void setSyncId(long syncId) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void resetLocalModified() {
|
|
|
|
|
/**
|
|
|
|
|
* 重置本地修改标记。
|
|
|
|
|
*/
|
|
|
|
|
public void resetLocalModified() {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long getId() {
|
|
|
|
|
/**
|
|
|
|
|
* 获取笔记ID。
|
|
|
|
|
*
|
|
|
|
|
* @return 笔记ID
|
|
|
|
|
*/
|
|
|
|
|
public long getId() {
|
|
|
|
|
return mId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long getParentId() {
|
|
|
|
|
/**
|
|
|
|
|
* 获取父ID。
|
|
|
|
|
*
|
|
|
|
|
* @return 父ID
|
|
|
|
|
*/
|
|
|
|
|
public long getParentId() {
|
|
|
|
|
return mParentId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getSnippet() {
|
|
|
|
|
/**
|
|
|
|
|
* 获取摘要。
|
|
|
|
|
*
|
|
|
|
|
* @return 摘要
|
|
|
|
|
*/
|
|
|
|
|
public String getSnippet() {
|
|
|
|
|
return mSnippet;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isNoteType() {
|
|
|
|
|
/**
|
|
|
|
|
* 判断是否为笔记类型。
|
|
|
|
|
*
|
|
|
|
|
* @return 是否为笔记类型
|
|
|
|
|
*/
|
|
|
|
|
public boolean isNoteType() {
|
|
|
|
|
return mType == Notes.TYPE_NOTE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void commit(boolean validateVersion) {
|
|
|
|
|
/**
|
|
|
|
|
* 提交数据到数据库。
|
|
|
|
|
*
|
|
|
|
|
* @param validateVersion 是否验证版本
|
|
|
|
|
*/
|
|
|
|
|
public void commit(boolean validateVersion) {
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
|
if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
|
|
|
|
|
mDiffNoteValues.remove(NoteColumns.ID);
|
|
|
|
@ -468,7 +531,7 @@ public class SqlNote {
|
|
|
|
|
throw new IllegalStateException("Try to update note with invalid id");
|
|
|
|
|
}
|
|
|
|
|
if (mDiffNoteValues.size() > 0) {
|
|
|
|
|
mVersion ++;
|
|
|
|
|
mVersion++;
|
|
|
|
|
int result = 0;
|
|
|
|
|
if (!validateVersion) {
|
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
@ -494,12 +557,11 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// refresh local info
|
|
|
|
|
// 刷新本地信息
|
|
|
|
|
loadFromCursor(mId);
|
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
|
loadDataContent();
|
|
|
|
|
|
|
|
|
|
mDiffNoteValues.clear();
|
|
|
|
|
mIsCreate = false;
|
|
|
|
|
}
|
|
|
|
|
}
|