|
|
|
@ -13,7 +13,6 @@
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.data;
|
|
|
|
|
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
@ -37,13 +36,17 @@ import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 数据成员注释:SqlNote类负责与数据库中的笔记数据进行交互处理。
|
|
|
|
|
*/
|
|
|
|
|
public class SqlNote {
|
|
|
|
|
private static final String TAG = SqlNote.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
@ -52,38 +55,23 @@ public class SqlNote {
|
|
|
|
|
NoteColumns.VERSION
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// 以下依次对应PROJECTION_NOTE中列的下标
|
|
|
|
|
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;
|
|
|
|
@ -93,86 +81,93 @@ public class SqlNote {
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 接口注释:SqlNote类用于对笔记数据进行数据库操作的封装。
|
|
|
|
|
*
|
|
|
|
|
* @param context 上下文环境
|
|
|
|
|
*/
|
|
|
|
|
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(); // 获取内容解析器
|
|
|
|
|
mIsCreate = true; // 是否是创建状态
|
|
|
|
|
mId = INVALID_ID; // ID 设置为无效值
|
|
|
|
|
mAlertDate = 0; // 提醒日期初始化为0
|
|
|
|
|
mBgColorId = ResourceParser.getDefaultBgId(context); // 背景颜色ID初始化为默认背景颜色
|
|
|
|
|
mCreatedDate = System.currentTimeMillis(); // 创建日期初始化为当前时间
|
|
|
|
|
mHasAttachment = 0; // 是否有附件初始化为0 - 表示没有附件
|
|
|
|
|
mModifiedDate = System.currentTimeMillis(); // 修改日期初始化为当前时间
|
|
|
|
|
mParentId = 0; // 父级ID初始化为0
|
|
|
|
|
mSnippet = ""; // 摘录初始化为空字符串
|
|
|
|
|
mType = Notes.TYPE_NOTE; // 类型为普通笔记类型
|
|
|
|
|
mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; // 小部件ID初始化为无效ID
|
|
|
|
|
mWidgetType = Notes.TYPE_WIDGET_INVALIDE; // 小部件类型初始化为无效类型
|
|
|
|
|
mOriginParent = 0; // 原始父级ID初始化为0
|
|
|
|
|
mVersion = 0; // 版本号初始化为0
|
|
|
|
|
mDiffNoteValues = new ContentValues(); // 差异笔记值的容器
|
|
|
|
|
mDataList = new ArrayList<SqlData>(); // 数据列表容器
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 实现注释:通过数据库游标加载SqlNote对象。
|
|
|
|
|
*
|
|
|
|
|
* @param context 上下文环境
|
|
|
|
|
* @param c 数据库游标
|
|
|
|
|
*/
|
|
|
|
|
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(); // 获取内容解析器
|
|
|
|
|
mIsCreate = false; // 是否是创建状态设置为false
|
|
|
|
|
loadFromCursor(c); // 从游标加载数据
|
|
|
|
|
mDataList = new ArrayList<SqlData>(); // 数据列表容器
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) // 如果类型为普通笔记类型
|
|
|
|
|
loadDataContent(); // 加载数据内容
|
|
|
|
|
mDiffNoteValues = new ContentValues(); // 差异笔记值的容器
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 实现注释:通过笔记ID加载SqlNote对象。
|
|
|
|
|
*
|
|
|
|
|
* @param context 上下文环境
|
|
|
|
|
* @param id 笔记ID
|
|
|
|
|
*/
|
|
|
|
|
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(); // 获取内容解析器
|
|
|
|
|
mIsCreate = false; // 是否是创建状态设置为 false
|
|
|
|
|
loadFromCursor(id); // 根据ID从游标加载数据
|
|
|
|
|
mDataList = new ArrayList<SqlData>(); // 数据列表容器
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) // 如果类型为普通笔记类型
|
|
|
|
|
loadDataContent(); // 加载数据内容
|
|
|
|
|
mDiffNoteValues = new ContentValues(); // 差异笔记值的容器
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ...(未完)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从Cursor中加载指定id的笔记数据。
|
|
|
|
|
* @param id 笔记ID
|
|
|
|
|
*/
|
|
|
|
|
private void loadFromCursor(long id) {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
try {
|
|
|
|
|
// 通过查询URI和ID获取匹配的Cursor对象
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)",
|
|
|
|
|
new String[] {
|
|
|
|
|
String.valueOf(id)
|
|
|
|
|
}, null);
|
|
|
|
|
new String[] { String.valueOf(id) }, null);
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.moveToNext();
|
|
|
|
|
loadFromCursor(c);
|
|
|
|
@ -185,34 +180,40 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 从Cursor中加载笔记数据并赋值给相应的成员变量。
|
|
|
|
|
* @param c 笔记Cursor对象
|
|
|
|
|
*/
|
|
|
|
|
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();
|
|
|
|
|
try {
|
|
|
|
|
// 通过查询URI和笔记ID获取匹配的笔记附加数据Cursor对象
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA,
|
|
|
|
|
"(note_id=?)", new String[] {
|
|
|
|
|
String.valueOf(mId)
|
|
|
|
|
}, null);
|
|
|
|
|
"(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");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 遍历Cursor,构造SqlData对象并添加到附加数据列表中
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
SqlData data = new SqlData(mContext, c);
|
|
|
|
|
mDataList.add(data);
|
|
|
|
@ -226,111 +227,81 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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);
|
|
|
|
|
long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_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;
|
|
|
|
|
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) : "";
|
|
|
|
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
|
|
|
|
|
}
|
|
|
|
|
mSnippet = snippet;
|
|
|
|
|
/**
|
|
|
|
|
* 设置笔记的内容,更新笔记的类型、小部件信息、原始父文件夹 ID 和附件数据
|
|
|
|
|
*
|
|
|
|
|
* @param js 包含笔记和数据的 JSON 对象
|
|
|
|
|
* @return 设置是否成功的布尔值
|
|
|
|
|
*/
|
|
|
|
|
// 数据成员注释:
|
|
|
|
|
// mIsCreate:标记是否为创建新笔记的操作
|
|
|
|
|
// mType:笔记的类型
|
|
|
|
|
// mWidgetId:笔记的小部件 ID
|
|
|
|
|
// mWidgetType:笔记的小部件类型
|
|
|
|
|
// mOriginParent:笔记的原始父文件夹 ID
|
|
|
|
|
// mDiffNoteValues:记录与原始笔记不同的属性和值的映射关系
|
|
|
|
|
// mDataList:存储附件数据的列表
|
|
|
|
|
// 实现注释:
|
|
|
|
|
// 更新笔记属性:
|
|
|
|
|
// 更新笔记的类型(如果有改变)
|
|
|
|
|
// 更新笔记的小部件 ID(如果有改变)
|
|
|
|
|
// 更新笔记的小部件类型(如果有改变)
|
|
|
|
|
// 更新笔记的原始父文件夹 ID(如果有改变)
|
|
|
|
|
// 遍历附件数据,处理每一项:
|
|
|
|
|
// 如果附件数据已存在于 mDataList 中,则使用现有的 SqlData 对象
|
|
|
|
|
// 否则,创建新的 SqlData 对象并添加到 mDataList 中
|
|
|
|
|
// 设置附件数据的内容
|
|
|
|
|
|
|
|
|
|
// 模块依赖注释:
|
|
|
|
|
// 依赖的类:
|
|
|
|
|
// JSONObject:JSON 对象类
|
|
|
|
|
// JSONArray:JSON 数组类
|
|
|
|
|
// SqlData:自定义的 SQL 数据类
|
|
|
|
|
// 依赖的接口和方法:
|
|
|
|
|
// AppWidgetManager.INVALID_APPWIDGET_ID:无效的小部件 ID 值
|
|
|
|
|
// NoteColumns.TYPE:笔记类型在数据库中的列名
|
|
|
|
|
// NoteColumns.WIDGET_ID:小部件 ID 在数据库中的列名
|
|
|
|
|
// NoteColumns.WIDGET_TYPE:小部件类型在数据库中的列名
|
|
|
|
|
// NoteColumns.ORIGIN_PARENT_ID:原始父文件夹 ID 在数据库中的列名
|
|
|
|
|
// DataColumns.ID:附件数据 ID 在数据库中的列名
|
|
|
|
|
// SqlData.setContent(data):设置附件数据的内容的方法
|
|
|
|
|
public boolean setContent(JSONObject js) {
|
|
|
|
|
try {
|
|
|
|
|
// 提取笔记对象和数据数组
|
|
|
|
|
JSONObject note = js.getJSONObject("note"); // 提取笔记对象
|
|
|
|
|
JSONArray dataArray = js.getJSONArray("dataArray"); // 提取数据数组
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
if (mIsCreate || mType != type) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type);
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, type); // 将类型加入差异笔记值中
|
|
|
|
|
}
|
|
|
|
|
mType = type;
|
|
|
|
|
|
|
|
|
|
int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID)
|
|
|
|
|
: AppWidgetManager.INVALID_APPWIDGET_ID;
|
|
|
|
|
// 更新笔记的小部件 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);
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId); // 将小部件 ID 加入差异笔记值中
|
|
|
|
|
}
|
|
|
|
|
mWidgetId = widgetId;
|
|
|
|
|
|
|
|
|
|
int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note
|
|
|
|
|
.getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE;
|
|
|
|
|
// 更新笔记的小部件类型
|
|
|
|
|
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);
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType); // 将小部件类型加入差异笔记值中
|
|
|
|
|
}
|
|
|
|
|
mWidgetType = widgetType;
|
|
|
|
|
|
|
|
|
|
long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note
|
|
|
|
|
.getLong(NoteColumns.ORIGIN_PARENT_ID) : 0;
|
|
|
|
|
// 更新笔记的原始父文件夹 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);
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent); // 将原始父文件夹 ID 加入差异笔记值中
|
|
|
|
|
}
|
|
|
|
|
mOriginParent = originParent;
|
|
|
|
|
|
|
|
|
|
// 更新笔记的附件数据
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
|
JSONObject data = dataArray.getJSONObject(i);
|
|
|
|
|
SqlData sqlData = null;
|
|
|
|
@ -348,28 +319,45 @@ public class SqlNote {
|
|
|
|
|
mDataList.add(sqlData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sqlData.setContent(data);
|
|
|
|
|
}
|
|
|
|
|
// 设置附件数据的内容
|
|
|
|
|
sqlData.setContent(data); // 设置附件数据的内容
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
return false; // 解析失败,返回 false
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
return true; // 解析成功,返回 true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取笔记内容的 JSON 对象
|
|
|
|
|
*
|
|
|
|
|
* @return 笔记内容的 JSON 对象,或者返回 null 如果笔记未创建在数据库中
|
|
|
|
|
*/
|
|
|
|
|
public JSONObject getContent() {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
|
Log.e(TAG, "it seems that we haven't created this in database yet");
|
|
|
|
|
Log.e(TAG, "在数据库中似乎还没有创建此笔记");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSONObject note = new JSONObject();
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
// 数据成员注释:
|
|
|
|
|
// NoteColumns.ID:笔记 ID
|
|
|
|
|
// NoteColumns.ALERTED_DATE:提醒日期
|
|
|
|
|
// NoteColumns.BG_COLOR_ID:背景颜色 ID
|
|
|
|
|
// NoteColumns.CREATED_DATE:创建日期
|
|
|
|
|
// NoteColumns.HAS_ATTACHMENT:是否有附件
|
|
|
|
|
// NoteColumns.MODIFIED_DATE:修改日期
|
|
|
|
|
// NoteColumns.PARENT_ID:父文件夹 ID
|
|
|
|
|
// NoteColumns.SNIPPET:内容摘要
|
|
|
|
|
// NoteColumns.TYPE:笔记类型
|
|
|
|
|
// NoteColumns.WIDGET_ID:小部件 ID
|
|
|
|
|
// NoteColumns.WIDGET_TYPE:小部件类型
|
|
|
|
|
// NoteColumns.ORIGIN_PARENT_ID:原始父文件夹 ID
|
|
|
|
|
note.put(NoteColumns.ID, mId);
|
|
|
|
|
note.put(NoteColumns.ALERTED_DATE, mAlertDate);
|
|
|
|
|
note.put(NoteColumns.BG_COLOR_ID, mBgColorId);
|
|
|
|
@ -393,6 +381,10 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
|
|
|
|
|
} else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) {
|
|
|
|
|
// 数据成员注释:
|
|
|
|
|
// NoteColumns.ID:笔记 ID
|
|
|
|
|
// NoteColumns.TYPE:笔记类型
|
|
|
|
|
// NoteColumns.SNIPPET:内容摘要
|
|
|
|
|
note.put(NoteColumns.ID, mId);
|
|
|
|
|
note.put(NoteColumns.TYPE, mType);
|
|
|
|
|
note.put(NoteColumns.SNIPPET, mSnippet);
|
|
|
|
@ -440,35 +432,40 @@ public class SqlNote {
|
|
|
|
|
return mType == Notes.TYPE_NOTE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提交操作,根据不同情况执行创建或更新操作
|
|
|
|
|
*
|
|
|
|
|
* @param validateVersion 是否验证版本
|
|
|
|
|
*/
|
|
|
|
|
public void commit(boolean validateVersion) {
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
|
if (mIsCreate) { // 如果是创建状态
|
|
|
|
|
if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
|
|
|
|
|
mDiffNoteValues.remove(NoteColumns.ID);
|
|
|
|
|
mDiffNoteValues.remove(NoteColumns.ID); // 移除差异笔记值中的 ID 键
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);
|
|
|
|
|
Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues); // 将差异笔记值插入内容提供器
|
|
|
|
|
try {
|
|
|
|
|
mId = Long.valueOf(uri.getPathSegments().get(1));
|
|
|
|
|
mId = Long.valueOf(uri.getPathSegments().get(1)); // 从 URI 中获取插入的笔记 ID
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
Log.e(TAG, "Get note id error :" + e.toString());
|
|
|
|
|
throw new ActionFailureException("create note failed");
|
|
|
|
|
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");
|
|
|
|
|
throw new IllegalStateException("Create thread id failed"); // 抛出非法状态异常
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
|
sqlData.commit(mId, false, -1);
|
|
|
|
|
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");
|
|
|
|
|
Log.e(TAG, "No such note"); // 打印错误日志
|
|
|
|
|
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, "("
|
|
|
|
@ -487,19 +484,19 @@ public class SqlNote {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
|
sqlData.commit(mId, validateVersion, mVersion);
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) { // 如果类型为普通笔记类型
|
|
|
|
|
for (SqlData sqlData : mDataList) { // 遍历数据列表
|
|
|
|
|
sqlData.commit(mId, validateVersion, mVersion); // 提交数据
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// refresh local info
|
|
|
|
|
// 刷新本地信息
|
|
|
|
|
loadFromCursor(mId);
|
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
|
loadDataContent();
|
|
|
|
|
|
|
|
|
|
mDiffNoteValues.clear();
|
|
|
|
|
mIsCreate = false;
|
|
|
|
|
mDiffNoteValues.clear(); // 清空差异笔记值
|
|
|
|
|
mIsCreate = false; // 设置为非创建状态
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|