|
|
@ -37,12 +37,16 @@ import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* SqlNote 类用于处理笔记数据的管理,包括从数据库加载笔记、
|
|
|
|
|
|
|
|
* 根据 JSON 设置笔记内容、将笔记内容保存到数据库等操作。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public class SqlNote {
|
|
|
|
public class SqlNote {
|
|
|
|
|
|
|
|
// 日志标签,用于在日志中标识该类的相关信息
|
|
|
|
private static final String TAG = SqlNote.class.getSimpleName();
|
|
|
|
private static final String TAG = SqlNote.class.getSimpleName();
|
|
|
|
|
|
|
|
// 无效的 ID,用于表示未分配或无效的笔记 ID
|
|
|
|
private static final int INVALID_ID = -99999;
|
|
|
|
private static final int INVALID_ID = -99999;
|
|
|
|
|
|
|
|
// 查询笔记时使用的列投影,指定要查询的列
|
|
|
|
public static final String[] PROJECTION_NOTE = new String[] {
|
|
|
|
public static final String[] PROJECTION_NOTE = new String[] {
|
|
|
|
NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID,
|
|
|
|
NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID,
|
|
|
|
NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE,
|
|
|
|
NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE,
|
|
|
@ -51,191 +55,268 @@ public class SqlNote {
|
|
|
|
NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID,
|
|
|
|
NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID,
|
|
|
|
NoteColumns.VERSION
|
|
|
|
NoteColumns.VERSION
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// 各列在投影数组中的索引,方便从 Cursor 中获取对应列的值
|
|
|
|
public static final int ID_COLUMN = 0;
|
|
|
|
public static final int ID_COLUMN = 0;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int ALERTED_DATE_COLUMN = 1;
|
|
|
|
public static final int ALERTED_DATE_COLUMN = 1;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int BG_COLOR_ID_COLUMN = 2;
|
|
|
|
public static final int BG_COLOR_ID_COLUMN = 2;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int CREATED_DATE_COLUMN = 3;
|
|
|
|
public static final int CREATED_DATE_COLUMN = 3;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int HAS_ATTACHMENT_COLUMN = 4;
|
|
|
|
public static final int HAS_ATTACHMENT_COLUMN = 4;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int MODIFIED_DATE_COLUMN = 5;
|
|
|
|
public static final int MODIFIED_DATE_COLUMN = 5;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int NOTES_COUNT_COLUMN = 6;
|
|
|
|
public static final int NOTES_COUNT_COLUMN = 6;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int PARENT_ID_COLUMN = 7;
|
|
|
|
public static final int PARENT_ID_COLUMN = 7;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int SNIPPET_COLUMN = 8;
|
|
|
|
public static final int SNIPPET_COLUMN = 8;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int TYPE_COLUMN = 9;
|
|
|
|
public static final int TYPE_COLUMN = 9;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int WIDGET_ID_COLUMN = 10;
|
|
|
|
public static final int WIDGET_ID_COLUMN = 10;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int WIDGET_TYPE_COLUMN = 11;
|
|
|
|
public static final int WIDGET_TYPE_COLUMN = 11;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int SYNC_ID_COLUMN = 12;
|
|
|
|
public static final int SYNC_ID_COLUMN = 12;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int LOCAL_MODIFIED_COLUMN = 13;
|
|
|
|
public static final int LOCAL_MODIFIED_COLUMN = 13;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int ORIGIN_PARENT_ID_COLUMN = 14;
|
|
|
|
public static final int ORIGIN_PARENT_ID_COLUMN = 14;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int GTASK_ID_COLUMN = 15;
|
|
|
|
public static final int GTASK_ID_COLUMN = 15;
|
|
|
|
|
|
|
|
|
|
|
|
public static final int VERSION_COLUMN = 16;
|
|
|
|
public static final int VERSION_COLUMN = 16;
|
|
|
|
|
|
|
|
// 上下文对象,用于获取系统服务和资源
|
|
|
|
private Context mContext;
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
// 内容解析器,用于与内容提供者进行交互,执行数据库操作
|
|
|
|
private ContentResolver mContentResolver;
|
|
|
|
private ContentResolver mContentResolver;
|
|
|
|
|
|
|
|
// 标记笔记是否为新创建的
|
|
|
|
private boolean mIsCreate;
|
|
|
|
private boolean mIsCreate;
|
|
|
|
|
|
|
|
// 笔记的 ID
|
|
|
|
private long mId;
|
|
|
|
private long mId;
|
|
|
|
|
|
|
|
// 笔记的提醒日期
|
|
|
|
private long mAlertDate;
|
|
|
|
private long mAlertDate;
|
|
|
|
|
|
|
|
// 笔记的背景颜色 ID
|
|
|
|
private int mBgColorId;
|
|
|
|
private int mBgColorId;
|
|
|
|
|
|
|
|
// 笔记的创建日期
|
|
|
|
private long mCreatedDate;
|
|
|
|
private long mCreatedDate;
|
|
|
|
|
|
|
|
// 笔记是否有附件的标志
|
|
|
|
private int mHasAttachment;
|
|
|
|
private int mHasAttachment;
|
|
|
|
|
|
|
|
// 笔记的修改日期
|
|
|
|
private long mModifiedDate;
|
|
|
|
private long mModifiedDate;
|
|
|
|
|
|
|
|
// 笔记的父文件夹 ID
|
|
|
|
private long mParentId;
|
|
|
|
private long mParentId;
|
|
|
|
|
|
|
|
// 笔记的摘要
|
|
|
|
private String mSnippet;
|
|
|
|
private String mSnippet;
|
|
|
|
|
|
|
|
// 笔记的类型,如普通笔记、文件夹等
|
|
|
|
private int mType;
|
|
|
|
private int mType;
|
|
|
|
|
|
|
|
// 笔记关联的小部件 ID
|
|
|
|
private int mWidgetId;
|
|
|
|
private int mWidgetId;
|
|
|
|
|
|
|
|
// 笔记关联的小部件类型
|
|
|
|
private int mWidgetType;
|
|
|
|
private int mWidgetType;
|
|
|
|
|
|
|
|
// 笔记的原始父文件夹 ID
|
|
|
|
private long mOriginParent;
|
|
|
|
private long mOriginParent;
|
|
|
|
|
|
|
|
// 笔记的版本号
|
|
|
|
private long mVersion;
|
|
|
|
private long mVersion;
|
|
|
|
|
|
|
|
// 存储笔记差异值的 ContentValues 对象,用于记录需要更新的字段
|
|
|
|
private ContentValues mDiffNoteValues;
|
|
|
|
private ContentValues mDiffNoteValues;
|
|
|
|
|
|
|
|
// 存储笔记相关数据的列表,每个元素是一个 SqlData 对象
|
|
|
|
private ArrayList<SqlData> mDataList;
|
|
|
|
private ArrayList<SqlData> mDataList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 构造函数,用于创建一个新的笔记对象。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
|
|
|
*/
|
|
|
|
public SqlNote(Context context) {
|
|
|
|
public SqlNote(Context context) {
|
|
|
|
|
|
|
|
// 保存上下文对象
|
|
|
|
mContext = context;
|
|
|
|
mContext = context;
|
|
|
|
|
|
|
|
// 获取内容解析器
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
|
|
|
|
// 标记为新创建的笔记
|
|
|
|
mIsCreate = true;
|
|
|
|
mIsCreate = true;
|
|
|
|
|
|
|
|
// 初始化笔记 ID 为无效值
|
|
|
|
mId = INVALID_ID;
|
|
|
|
mId = INVALID_ID;
|
|
|
|
|
|
|
|
// 初始化提醒日期为 0
|
|
|
|
mAlertDate = 0;
|
|
|
|
mAlertDate = 0;
|
|
|
|
|
|
|
|
// 初始化背景颜色 ID 为默认值
|
|
|
|
mBgColorId = ResourceParser.getDefaultBgId(context);
|
|
|
|
mBgColorId = ResourceParser.getDefaultBgId(context);
|
|
|
|
|
|
|
|
// 初始化创建日期为当前时间
|
|
|
|
mCreatedDate = System.currentTimeMillis();
|
|
|
|
mCreatedDate = System.currentTimeMillis();
|
|
|
|
|
|
|
|
// 初始化是否有附件标志为 0
|
|
|
|
mHasAttachment = 0;
|
|
|
|
mHasAttachment = 0;
|
|
|
|
|
|
|
|
// 初始化修改日期为当前时间
|
|
|
|
mModifiedDate = System.currentTimeMillis();
|
|
|
|
mModifiedDate = System.currentTimeMillis();
|
|
|
|
|
|
|
|
// 初始化父文件夹 ID 为 0
|
|
|
|
mParentId = 0;
|
|
|
|
mParentId = 0;
|
|
|
|
|
|
|
|
// 初始化摘要为空字符串
|
|
|
|
mSnippet = "";
|
|
|
|
mSnippet = "";
|
|
|
|
|
|
|
|
// 初始化笔记类型为普通笔记
|
|
|
|
mType = Notes.TYPE_NOTE;
|
|
|
|
mType = Notes.TYPE_NOTE;
|
|
|
|
|
|
|
|
// 初始化小部件 ID 为无效值
|
|
|
|
mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
|
|
|
|
mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
|
|
|
|
|
|
|
|
// 初始化小部件类型为无效类型
|
|
|
|
mWidgetType = Notes.TYPE_WIDGET_INVALIDE;
|
|
|
|
mWidgetType = Notes.TYPE_WIDGET_INVALIDE;
|
|
|
|
|
|
|
|
// 初始化原始父文件夹 ID 为 0
|
|
|
|
mOriginParent = 0;
|
|
|
|
mOriginParent = 0;
|
|
|
|
|
|
|
|
// 初始化版本号为 0
|
|
|
|
mVersion = 0;
|
|
|
|
mVersion = 0;
|
|
|
|
|
|
|
|
// 初始化差异值 ContentValues 对象
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
|
|
|
|
// 初始化数据列表
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 构造函数,用于从 Cursor 加载笔记数据创建笔记对象。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
|
|
|
* @param c 包含笔记数据的 Cursor
|
|
|
|
|
|
|
|
*/
|
|
|
|
public SqlNote(Context context, Cursor c) {
|
|
|
|
public SqlNote(Context context, Cursor c) {
|
|
|
|
|
|
|
|
// 保存上下文对象
|
|
|
|
mContext = context;
|
|
|
|
mContext = context;
|
|
|
|
|
|
|
|
// 获取内容解析器
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
|
|
|
|
// 标记为非新创建的笔记
|
|
|
|
mIsCreate = false;
|
|
|
|
mIsCreate = false;
|
|
|
|
|
|
|
|
// 从 Cursor 中加载笔记数据
|
|
|
|
loadFromCursor(c);
|
|
|
|
loadFromCursor(c);
|
|
|
|
|
|
|
|
// 初始化数据列表
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
|
|
|
|
// 如果是普通笔记,加载笔记相关数据
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
loadDataContent();
|
|
|
|
loadDataContent();
|
|
|
|
|
|
|
|
// 初始化差异值 ContentValues 对象
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 构造函数,用于根据笔记 ID 从数据库加载笔记数据创建笔记对象。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param context 上下文对象
|
|
|
|
|
|
|
|
* @param id 笔记的 ID
|
|
|
|
|
|
|
|
*/
|
|
|
|
public SqlNote(Context context, long id) {
|
|
|
|
public SqlNote(Context context, long id) {
|
|
|
|
|
|
|
|
// 保存上下文对象
|
|
|
|
mContext = context;
|
|
|
|
mContext = context;
|
|
|
|
|
|
|
|
// 获取内容解析器
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
|
|
|
|
// 标记为非新创建的笔记
|
|
|
|
mIsCreate = false;
|
|
|
|
mIsCreate = false;
|
|
|
|
|
|
|
|
// 根据 ID 从数据库加载笔记数据
|
|
|
|
loadFromCursor(id);
|
|
|
|
loadFromCursor(id);
|
|
|
|
|
|
|
|
// 初始化数据列表
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
|
|
|
|
// 如果是普通笔记,加载笔记相关数据
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
loadDataContent();
|
|
|
|
loadDataContent();
|
|
|
|
|
|
|
|
// 初始化差异值 ContentValues 对象
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 根据笔记 ID 从数据库查询并加载笔记数据。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param id 笔记的 ID
|
|
|
|
|
|
|
|
*/
|
|
|
|
private void loadFromCursor(long id) {
|
|
|
|
private void loadFromCursor(long id) {
|
|
|
|
Cursor c = null;
|
|
|
|
Cursor c = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 根据 ID 查询笔记数据
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)",
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)",
|
|
|
|
new String[] {
|
|
|
|
new String[] {
|
|
|
|
String.valueOf(id)
|
|
|
|
String.valueOf(id)
|
|
|
|
}, null);
|
|
|
|
}, null);
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
|
|
|
|
|
// 移动到查询结果的第一行
|
|
|
|
c.moveToNext();
|
|
|
|
c.moveToNext();
|
|
|
|
|
|
|
|
// 从 Cursor 中加载笔记数据
|
|
|
|
loadFromCursor(c);
|
|
|
|
loadFromCursor(c);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 记录警告日志,提示 Cursor 为空
|
|
|
|
Log.w(TAG, "loadFromCursor: cursor = null");
|
|
|
|
Log.w(TAG, "loadFromCursor: cursor = null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
if (c != null)
|
|
|
|
if (c != null)
|
|
|
|
|
|
|
|
// 关闭 Cursor
|
|
|
|
c.close();
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 从 Cursor 中加载笔记数据到当前对象。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param c 包含笔记数据的 Cursor
|
|
|
|
|
|
|
|
*/
|
|
|
|
private void loadFromCursor(Cursor c) {
|
|
|
|
private void loadFromCursor(Cursor c) {
|
|
|
|
|
|
|
|
// 从 Cursor 中获取笔记 ID
|
|
|
|
mId = c.getLong(ID_COLUMN);
|
|
|
|
mId = c.getLong(ID_COLUMN);
|
|
|
|
|
|
|
|
// 从 Cursor 中获取提醒日期
|
|
|
|
mAlertDate = c.getLong(ALERTED_DATE_COLUMN);
|
|
|
|
mAlertDate = c.getLong(ALERTED_DATE_COLUMN);
|
|
|
|
|
|
|
|
// 从 Cursor 中获取背景颜色 ID
|
|
|
|
mBgColorId = c.getInt(BG_COLOR_ID_COLUMN);
|
|
|
|
mBgColorId = c.getInt(BG_COLOR_ID_COLUMN);
|
|
|
|
|
|
|
|
// 从 Cursor 中获取创建日期
|
|
|
|
mCreatedDate = c.getLong(CREATED_DATE_COLUMN);
|
|
|
|
mCreatedDate = c.getLong(CREATED_DATE_COLUMN);
|
|
|
|
|
|
|
|
// 从 Cursor 中获取是否有附件标志
|
|
|
|
mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN);
|
|
|
|
mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN);
|
|
|
|
|
|
|
|
// 从 Cursor 中获取修改日期
|
|
|
|
mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN);
|
|
|
|
mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN);
|
|
|
|
|
|
|
|
// 从 Cursor 中获取父文件夹 ID
|
|
|
|
mParentId = c.getLong(PARENT_ID_COLUMN);
|
|
|
|
mParentId = c.getLong(PARENT_ID_COLUMN);
|
|
|
|
|
|
|
|
// 从 Cursor 中获取摘要
|
|
|
|
mSnippet = c.getString(SNIPPET_COLUMN);
|
|
|
|
mSnippet = c.getString(SNIPPET_COLUMN);
|
|
|
|
|
|
|
|
// 从 Cursor 中获取笔记类型
|
|
|
|
mType = c.getInt(TYPE_COLUMN);
|
|
|
|
mType = c.getInt(TYPE_COLUMN);
|
|
|
|
|
|
|
|
// 从 Cursor 中获取小部件 ID
|
|
|
|
mWidgetId = c.getInt(WIDGET_ID_COLUMN);
|
|
|
|
mWidgetId = c.getInt(WIDGET_ID_COLUMN);
|
|
|
|
|
|
|
|
// 从 Cursor 中获取小部件类型
|
|
|
|
mWidgetType = c.getInt(WIDGET_TYPE_COLUMN);
|
|
|
|
mWidgetType = c.getInt(WIDGET_TYPE_COLUMN);
|
|
|
|
|
|
|
|
// 从 Cursor 中获取版本号
|
|
|
|
mVersion = c.getLong(VERSION_COLUMN);
|
|
|
|
mVersion = c.getLong(VERSION_COLUMN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 加载笔记的相关数据。
|
|
|
|
|
|
|
|
*/
|
|
|
|
private void loadDataContent() {
|
|
|
|
private void loadDataContent() {
|
|
|
|
Cursor c = null;
|
|
|
|
Cursor c = null;
|
|
|
|
|
|
|
|
// 清空数据列表
|
|
|
|
mDataList.clear();
|
|
|
|
mDataList.clear();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 根据笔记 ID 查询相关数据
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA,
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA,
|
|
|
|
"(note_id=?)", new String[] {
|
|
|
|
"(note_id=?)", new String[] {
|
|
|
|
String.valueOf(mId)
|
|
|
|
String.valueOf(mId)
|
|
|
|
}, null);
|
|
|
|
}, null);
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
|
if (c.getCount() == 0) {
|
|
|
|
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;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
|
|
|
// 创建 SqlData 对象并添加到数据列表中
|
|
|
|
SqlData data = new SqlData(mContext, c);
|
|
|
|
SqlData data = new SqlData(mContext, c);
|
|
|
|
mDataList.add(data);
|
|
|
|
mDataList.add(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 记录警告日志,提示 Cursor 为空
|
|
|
|
Log.w(TAG, "loadDataContent: cursor = null");
|
|
|
|
Log.w(TAG, "loadDataContent: cursor = null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
if (c != null)
|
|
|
|
if (c != null)
|
|
|
|
|
|
|
|
// 关闭 Cursor
|
|
|
|
c.close();
|
|
|
|
c.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 根据 JSON 对象设置笔记内容。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param js 包含笔记内容的 JSON 对象
|
|
|
|
|
|
|
|
* @return 设置成功返回 true,失败返回 false
|
|
|
|
|
|
|
|
*/
|
|
|
|
public boolean setContent(JSONObject js) {
|
|
|
|
public boolean setContent(JSONObject js) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 从 JSON 对象中获取笔记信息
|
|
|
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
|
|
|
|
if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
|
|
|
|
|
|
|
|
// 记录警告日志,提示不能设置系统文件夹
|
|
|
|
Log.w(TAG, "cannot set system folder");
|
|
|
|
Log.w(TAG, "cannot set system folder");
|
|
|
|
} else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_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
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
|
|
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
|
|
|
|
|
|
|
// 记录摘要差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
|
|
|
|
mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mSnippet = snippet;
|
|
|
|
mSnippet = snippet;
|
|
|
@ -243,13 +324,16 @@ public class SqlNote {
|
|
|
|
int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
|
|
|
|
int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
|
|
|
|
: Notes.TYPE_NOTE;
|
|
|
|
: Notes.TYPE_NOTE;
|
|
|
|
if (mIsCreate || mType != type) {
|
|
|
|
if (mIsCreate || mType != type) {
|
|
|
|
|
|
|
|
// 记录类型差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type);
|
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mType = 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);
|
|
|
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID;
|
|
|
|
long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID;
|
|
|
|
if (mIsCreate || mId != id) {
|
|
|
|
if (mIsCreate || mId != id) {
|
|
|
|
|
|
|
|
// 记录 ID 差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.ID, id);
|
|
|
|
mDiffNoteValues.put(NoteColumns.ID, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mId = id;
|
|
|
|
mId = id;
|
|
|
@ -257,6 +341,7 @@ public class SqlNote {
|
|
|
|
long alertDate = note.has(NoteColumns.ALERTED_DATE) ? note
|
|
|
|
long alertDate = note.has(NoteColumns.ALERTED_DATE) ? note
|
|
|
|
.getLong(NoteColumns.ALERTED_DATE) : 0;
|
|
|
|
.getLong(NoteColumns.ALERTED_DATE) : 0;
|
|
|
|
if (mIsCreate || mAlertDate != alertDate) {
|
|
|
|
if (mIsCreate || mAlertDate != alertDate) {
|
|
|
|
|
|
|
|
// 记录提醒日期差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.ALERTED_DATE, alertDate);
|
|
|
|
mDiffNoteValues.put(NoteColumns.ALERTED_DATE, alertDate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mAlertDate = alertDate;
|
|
|
|
mAlertDate = alertDate;
|
|
|
@ -264,6 +349,7 @@ public class SqlNote {
|
|
|
|
int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? note
|
|
|
|
int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? note
|
|
|
|
.getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext);
|
|
|
|
.getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext);
|
|
|
|
if (mIsCreate || mBgColorId != bgColorId) {
|
|
|
|
if (mIsCreate || mBgColorId != bgColorId) {
|
|
|
|
|
|
|
|
// 记录背景颜色 ID 差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId);
|
|
|
|
mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mBgColorId = bgColorId;
|
|
|
|
mBgColorId = bgColorId;
|
|
|
@ -271,6 +357,7 @@ public class SqlNote {
|
|
|
|
long createDate = note.has(NoteColumns.CREATED_DATE) ? note
|
|
|
|
long createDate = note.has(NoteColumns.CREATED_DATE) ? note
|
|
|
|
.getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis();
|
|
|
|
.getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis();
|
|
|
|
if (mIsCreate || mCreatedDate != createDate) {
|
|
|
|
if (mIsCreate || mCreatedDate != createDate) {
|
|
|
|
|
|
|
|
// 记录创建日期差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.CREATED_DATE, createDate);
|
|
|
|
mDiffNoteValues.put(NoteColumns.CREATED_DATE, createDate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mCreatedDate = createDate;
|
|
|
|
mCreatedDate = createDate;
|
|
|
@ -278,6 +365,7 @@ public class SqlNote {
|
|
|
|
int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? note
|
|
|
|
int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? note
|
|
|
|
.getInt(NoteColumns.HAS_ATTACHMENT) : 0;
|
|
|
|
.getInt(NoteColumns.HAS_ATTACHMENT) : 0;
|
|
|
|
if (mIsCreate || mHasAttachment != hasAttachment) {
|
|
|
|
if (mIsCreate || mHasAttachment != hasAttachment) {
|
|
|
|
|
|
|
|
// 记录是否有附件标志差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.HAS_ATTACHMENT, hasAttachment);
|
|
|
|
mDiffNoteValues.put(NoteColumns.HAS_ATTACHMENT, hasAttachment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mHasAttachment = hasAttachment;
|
|
|
|
mHasAttachment = hasAttachment;
|
|
|
@ -285,6 +373,7 @@ public class SqlNote {
|
|
|
|
long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? note
|
|
|
|
long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? note
|
|
|
|
.getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis();
|
|
|
|
.getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis();
|
|
|
|
if (mIsCreate || mModifiedDate != modifiedDate) {
|
|
|
|
if (mIsCreate || mModifiedDate != modifiedDate) {
|
|
|
|
|
|
|
|
// 记录修改日期差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate);
|
|
|
|
mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mModifiedDate = modifiedDate;
|
|
|
|
mModifiedDate = modifiedDate;
|
|
|
@ -292,6 +381,7 @@ public class SqlNote {
|
|
|
|
long parentId = note.has(NoteColumns.PARENT_ID) ? note
|
|
|
|
long parentId = note.has(NoteColumns.PARENT_ID) ? note
|
|
|
|
.getLong(NoteColumns.PARENT_ID) : 0;
|
|
|
|
.getLong(NoteColumns.PARENT_ID) : 0;
|
|
|
|
if (mIsCreate || mParentId != parentId) {
|
|
|
|
if (mIsCreate || mParentId != parentId) {
|
|
|
|
|
|
|
|
// 记录父文件夹 ID 差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId);
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mParentId = parentId;
|
|
|
|
mParentId = parentId;
|
|
|
@ -299,6 +389,7 @@ public class SqlNote {
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
|
|
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
|
|
|
|
|
|
|
// 记录摘要差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
|
|
|
|
mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mSnippet = snippet;
|
|
|
|
mSnippet = snippet;
|
|
|
@ -306,6 +397,7 @@ public class SqlNote {
|
|
|
|
int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
|
|
|
|
int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
|
|
|
|
: Notes.TYPE_NOTE;
|
|
|
|
: Notes.TYPE_NOTE;
|
|
|
|
if (mIsCreate || mType != type) {
|
|
|
|
if (mIsCreate || mType != type) {
|
|
|
|
|
|
|
|
// 记录类型差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type);
|
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mType = type;
|
|
|
|
mType = type;
|
|
|
@ -313,6 +405,7 @@ public class SqlNote {
|
|
|
|
int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID)
|
|
|
|
int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID)
|
|
|
|
: AppWidgetManager.INVALID_APPWIDGET_ID;
|
|
|
|
: AppWidgetManager.INVALID_APPWIDGET_ID;
|
|
|
|
if (mIsCreate || mWidgetId != widgetId) {
|
|
|
|
if (mIsCreate || mWidgetId != widgetId) {
|
|
|
|
|
|
|
|
// 记录小部件 ID 差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId);
|
|
|
|
mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mWidgetId = widgetId;
|
|
|
|
mWidgetId = widgetId;
|
|
|
@ -320,6 +413,7 @@ public class SqlNote {
|
|
|
|
int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note
|
|
|
|
int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note
|
|
|
|
.getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE;
|
|
|
|
.getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE;
|
|
|
|
if (mIsCreate || mWidgetType != widgetType) {
|
|
|
|
if (mIsCreate || mWidgetType != widgetType) {
|
|
|
|
|
|
|
|
// 记录小部件类型差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType);
|
|
|
|
mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mWidgetType = widgetType;
|
|
|
|
mWidgetType = widgetType;
|
|
|
@ -327,11 +421,13 @@ public class SqlNote {
|
|
|
|
long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note
|
|
|
|
long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note
|
|
|
|
.getLong(NoteColumns.ORIGIN_PARENT_ID) : 0;
|
|
|
|
.getLong(NoteColumns.ORIGIN_PARENT_ID) : 0;
|
|
|
|
if (mIsCreate || mOriginParent != originParent) {
|
|
|
|
if (mIsCreate || mOriginParent != originParent) {
|
|
|
|
|
|
|
|
// 记录原始父文件夹 ID 差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent);
|
|
|
|
mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mOriginParent = originParent;
|
|
|
|
mOriginParent = originParent;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
|
|
|
|
// 处理笔记的相关数据
|
|
|
|
JSONObject data = dataArray.getJSONObject(i);
|
|
|
|
JSONObject data = dataArray.getJSONObject(i);
|
|
|
|
SqlData sqlData = null;
|
|
|
|
SqlData sqlData = null;
|
|
|
|
if (data.has(DataColumns.ID)) {
|
|
|
|
if (data.has(DataColumns.ID)) {
|
|
|
@ -352,6 +448,7 @@ public class SqlNote {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
// 记录错误日志并打印异常堆栈信息
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
@ -359,17 +456,24 @@ public class SqlNote {
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取笔记内容的 JSON 表示。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return 包含笔记内容的 JSON 对象,出错返回 null
|
|
|
|
|
|
|
|
*/
|
|
|
|
public JSONObject getContent() {
|
|
|
|
public JSONObject getContent() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
if (mIsCreate) {
|
|
|
|
|
|
|
|
// 记录错误日志,提示笔记未在数据库中创建
|
|
|
|
Log.e(TAG, "it seems that we haven't created this in database yet");
|
|
|
|
Log.e(TAG, "it seems that we haven't created this in database yet");
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject note = new JSONObject();
|
|
|
|
JSONObject note = new JSONObject();
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
|
|
|
// 对于普通笔记,添加所有字段到 JSON 对象
|
|
|
|
note.put(NoteColumns.ID, mId);
|
|
|
|
note.put(NoteColumns.ID, mId);
|
|
|
|
note.put(NoteColumns.ALERTED_DATE, mAlertDate);
|
|
|
|
note.put(NoteColumns.ALERTED_DATE, mAlertDate);
|
|
|
|
note.put(NoteColumns.BG_COLOR_ID, mBgColorId);
|
|
|
|
note.put(NoteColumns.BG_COLOR_ID, mBgColorId);
|
|
|
@ -386,6 +490,7 @@ public class SqlNote {
|
|
|
|
|
|
|
|
|
|
|
|
JSONArray dataArray = new JSONArray();
|
|
|
|
JSONArray dataArray = new JSONArray();
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
|
|
|
|
// 添加笔记的相关数据到 JSON 数组
|
|
|
|
JSONObject data = sqlData.getContent();
|
|
|
|
JSONObject data = sqlData.getContent();
|
|
|
|
if (data != null) {
|
|
|
|
if (data != null) {
|
|
|
|
dataArray.put(data);
|
|
|
|
dataArray.put(data);
|
|
|
@ -393,6 +498,7 @@ public class SqlNote {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
|
|
|
|
} else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) {
|
|
|
|
} else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) {
|
|
|
|
|
|
|
|
// 对于文件夹或系统文件夹,添加部分字段到 JSON 对象
|
|
|
|
note.put(NoteColumns.ID, mId);
|
|
|
|
note.put(NoteColumns.ID, mId);
|
|
|
|
note.put(NoteColumns.TYPE, mType);
|
|
|
|
note.put(NoteColumns.TYPE, mType);
|
|
|
|
note.put(NoteColumns.SNIPPET, mSnippet);
|
|
|
|
note.put(NoteColumns.SNIPPET, mSnippet);
|
|
|
@ -401,81 +507,139 @@ public class SqlNote {
|
|
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
return js;
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
|
|
|
// 记录错误日志并打印异常堆栈信息
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 设置笔记的父文件夹 ID。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param id 父文件夹 ID
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void setParentId(long id) {
|
|
|
|
public void setParentId(long id) {
|
|
|
|
mParentId = id;
|
|
|
|
mParentId = id;
|
|
|
|
|
|
|
|
// 记录父文件夹 ID 差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 设置笔记的 Google 任务 ID。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param gid Google 任务 ID
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void setGtaskId(String gid) {
|
|
|
|
public void setGtaskId(String gid) {
|
|
|
|
|
|
|
|
// 记录 Google 任务 ID 差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
|
|
|
|
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 设置笔记的同步 ID。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param syncId 同步 ID
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void setSyncId(long syncId) {
|
|
|
|
public void setSyncId(long syncId) {
|
|
|
|
|
|
|
|
// 记录同步 ID 差异
|
|
|
|
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
|
|
|
|
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 重置笔记的本地修改标志。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void resetLocalModified() {
|
|
|
|
public void resetLocalModified() {
|
|
|
|
|
|
|
|
// 记录本地修改标志差异为 0
|
|
|
|
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
|
|
|
|
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取笔记的 ID。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return 笔记的 ID
|
|
|
|
|
|
|
|
*/
|
|
|
|
public long getId() {
|
|
|
|
public long getId() {
|
|
|
|
return mId;
|
|
|
|
return mId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取笔记的父文件夹 ID。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return 父文件夹 ID
|
|
|
|
|
|
|
|
*/
|
|
|
|
public long getParentId() {
|
|
|
|
public long getParentId() {
|
|
|
|
return mParentId;
|
|
|
|
return mParentId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 获取笔记的摘要。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return 笔记的摘要
|
|
|
|
|
|
|
|
*/
|
|
|
|
public String getSnippet() {
|
|
|
|
public String getSnippet() {
|
|
|
|
return mSnippet;
|
|
|
|
return mSnippet;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 判断笔记是否为普通笔记类型。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @return 是普通笔记类型返回 true,否则返回 false
|
|
|
|
|
|
|
|
*/
|
|
|
|
public boolean isNoteType() {
|
|
|
|
public boolean isNoteType() {
|
|
|
|
return mType == Notes.TYPE_NOTE;
|
|
|
|
return mType == Notes.TYPE_NOTE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 将笔记内容提交到数据库。
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* @param validateVersion 是否验证版本
|
|
|
|
|
|
|
|
*/
|
|
|
|
public void commit(boolean validateVersion) {
|
|
|
|
public void commit(boolean validateVersion) {
|
|
|
|
if (mIsCreate) {
|
|
|
|
if (mIsCreate) {
|
|
|
|
if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
|
|
|
|
if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
|
|
|
|
|
|
|
|
// 如果 ID 无效且差异值中包含 ID,移除 ID 差异
|
|
|
|
mDiffNoteValues.remove(NoteColumns.ID);
|
|
|
|
mDiffNoteValues.remove(NoteColumns.ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 插入笔记数据到数据库
|
|
|
|
Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);
|
|
|
|
Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 获取插入后的笔记 ID
|
|
|
|
mId = Long.valueOf(uri.getPathSegments().get(1));
|
|
|
|
mId = Long.valueOf(uri.getPathSegments().get(1));
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
|
|
|
// 记录错误日志并抛出异常
|
|
|
|
Log.e(TAG, "Get note id error :" + e.toString());
|
|
|
|
Log.e(TAG, "Get note id error :" + e.toString());
|
|
|
|
throw new ActionFailureException("create note failed");
|
|
|
|
throw new ActionFailureException("create note failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mId == 0) {
|
|
|
|
if (mId == 0) {
|
|
|
|
|
|
|
|
// 抛出异常,提示创建笔记 ID 失败
|
|
|
|
throw new IllegalStateException("Create thread id failed");
|
|
|
|
throw new IllegalStateException("Create thread id failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
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) {
|
|
|
|
if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) {
|
|
|
|
|
|
|
|
// 记录错误日志并抛出异常,提示笔记 ID 无效
|
|
|
|
Log.e(TAG, "No such note");
|
|
|
|
Log.e(TAG, "No such note");
|
|
|
|
throw new IllegalStateException("Try to update note with invalid id");
|
|
|
|
throw new IllegalStateException("Try to update note with invalid id");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (mDiffNoteValues.size() > 0) {
|
|
|
|
if (mDiffNoteValues.size() > 0) {
|
|
|
|
|
|
|
|
// 版本号加 1
|
|
|
|
mVersion ++;
|
|
|
|
mVersion ++;
|
|
|
|
int result = 0;
|
|
|
|
int result = 0;
|
|
|
|
if (!validateVersion) {
|
|
|
|
if (!validateVersion) {
|
|
|
|
|
|
|
|
// 不验证版本时,直接更新笔记数据
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
+ NoteColumns.ID + "=?)", new String[] {
|
|
|
|
+ NoteColumns.ID + "=?)", new String[] {
|
|
|
|
String.valueOf(mId)
|
|
|
|
String.valueOf(mId)
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 验证版本时,根据版本条件更新笔记数据
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
+ NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
|
|
|
|
+ NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
|
|
|
|
new String[] {
|
|
|
|
new String[] {
|
|
|
@ -483,23 +647,27 @@ public class SqlNote {
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result == 0) {
|
|
|
|
if (result == 0) {
|
|
|
|
|
|
|
|
// 记录警告日志,提示可能用户在同步时更新了笔记
|
|
|
|
Log.w(TAG, "there is no update. maybe user updates note when syncing");
|
|
|
|
Log.w(TAG, "there is no update. maybe user updates note when syncing");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
|
|
|
|
// 提交笔记的相关数据到数据库
|
|
|
|
sqlData.commit(mId, validateVersion, mVersion);
|
|
|
|
sqlData.commit(mId, validateVersion, mVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// refresh local info
|
|
|
|
// 刷新本地笔记信息
|
|
|
|
loadFromCursor(mId);
|
|
|
|
loadFromCursor(mId);
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
loadDataContent();
|
|
|
|
loadDataContent();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 清空差异值
|
|
|
|
mDiffNoteValues.clear();
|
|
|
|
mDiffNoteValues.clear();
|
|
|
|
|
|
|
|
// 标记为非新创建的笔记
|
|
|
|
mIsCreate = false;
|
|
|
|
mIsCreate = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|