|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
/**
|
|
|
|
|
* @ProjectName: MiNote
|
|
|
|
|
* @Package: net.micode.notes.gtask.data
|
|
|
|
|
* @ClassName: SplNote
|
|
|
|
|
* @Description: 用于支持小米便签最底层的数据库相关操作,和sqldata的关系上是父集关系,即note是data的子父集。基于 Java 编写的笔记管理应用的部分源代码,主要包括对笔记进行数据库操作的相关类。这段代码涉及到对笔记的创建、更新、数据加载等操作,并且包含了对 JSON 数据的处理以及与 ContentProvider 进行交互的相关方法。
|
|
|
|
|
* @Author: cyh
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.data;
|
|
|
|
|
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.DataColumns;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException;
|
|
|
|
|
import net.micode.notes.tool.GTaskStringUtils;
|
|
|
|
|
import net.micode.notes.tool.ResourceParser;
|
|
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class SqlNote {
|
|
|
|
|
|
|
|
|
|
private static final String TAG = SqlNote.class.getSimpleName();//调用getSimpleName()函数,得到类的简写名称存入字符串TAG中
|
|
|
|
|
|
|
|
|
|
private static final int INVALID_ID = -99999;
|
|
|
|
|
|
|
|
|
|
public static final String[] PROJECTION_NOTE = new String[] {// 集合了interface NoteColumns中所有SF常量(17个)
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//以下设置17个列的编号
|
|
|
|
|
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;
|
|
|
|
|
//一下定义了17个内部的变量,其中12个可以由content中获得,5个需要初始化为0或者new
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:SqlNote(Context context)
|
|
|
|
|
* @功能描述:构造函数
|
|
|
|
|
* @实现过程:初始化参数,mIsCreate用于标示构造方式
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public SqlNote(Context 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>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:SqlNote(Context context,Cursor c)
|
|
|
|
|
* @功能描述:构造函数
|
|
|
|
|
* @实现过程:初始化参数,mIsCreate用于标示构造方式
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public SqlNote(Context context, Cursor c) {//构造函数有context和一个数据库的cursor,多数变量通过cursor指向的一条记录直接进行初始化
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
|
mIsCreate = false;
|
|
|
|
|
loadFromCursor(c);
|
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
|
loadDataContent();
|
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:SqlNote(Context context, long id)
|
|
|
|
|
* @功能描述:构造函数,接受一个上下文(Context)对象和一个笔记ID,并使用这些参数来初始化SqlNote对象的成员变量
|
|
|
|
|
* @实现过程:初始化参数,mIsCreate用于标示构造方式将上下文对象和内容解析器对象存储在成员变量中,将“是否为新建笔记”标志设置为false,并调用loadFromCursor()方法来从数据库中加载笔记数据并进行初始化。接着,它创建一个空的SqlData对象列表和一个用于存储修改后的笔记值的ContentValues对象。最后,如果笔记类型是普通笔记(Notes.TYPE_NOTE),则调用loadDataContent()方法来加载笔记正文数据。
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:loadFromCursor(long id)
|
|
|
|
|
* @功能描述:通过游标从光标处加载数据
|
|
|
|
|
* @实现过程:首先尝试使用指定的ID查询笔记数据,然后检查返回的Cursor是否为空。如果Cursor不为空,则移动到第一条记录并调用另一个方法来加载数据进行初始化。最后,在finally块中关闭Cursor,确保资源被正确释放。
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
private void loadFromCursor(long id) {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)",
|
|
|
|
|
new String[] {
|
|
|
|
|
String.valueOf(id)
|
|
|
|
|
}, null);//通过id获得对应的ContentResolver中的cursor
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.moveToNext();
|
|
|
|
|
loadFromCursor(c);//然后加载数据进行初始化,这样函数
|
|
|
|
|
} else {
|
|
|
|
|
Log.w(TAG, "loadFromCursor: cursor = null");
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (c != null)
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:loadFromCursor(Cursor c)
|
|
|
|
|
* @参数:Cursor c
|
|
|
|
|
* @功能描述:从Cursor中加载笔记数据并进行初始化
|
|
|
|
|
* @实现过程:使用Cursor对象的getColumnIndex()方法和相应的列名常量(例如ID_COLUMN、ALERTED_DATE_COLUMN等)来获取每个属性的索引,然后使用Cursor对象的getLong()、getInt()和getString()等方法来获取相应的属性值,并将它们存储在SqlNote对象的成员变量中。
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:loadDataContent()
|
|
|
|
|
* @功能描述:加载笔记正文数据
|
|
|
|
|
* @实现过程:使用内容解析器对象(mContentResolver)执行查询操作,查询指定note_id对应的正文数据。其中,查询条件为"note_id=?",并将mId转换为字符串作为参数传入。然后,它检查返回的Cursor对象是否为null。如果不为null,则检查Cursor中的记录数。如果记录数为0,表示该笔记没有正文数据,会输出一条警告日志并返回。否则,它遍历Cursor中的每条记录,在每条记录上调用SqlData的构造方法创建SqlData对象,并将其添加到SqlNote对象的数据列表中。最后,在finally块中关闭Cursor对象,确保资源被正确释放。
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
private void loadDataContent() {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
mDataList.clear();
|
|
|
|
|
try {
|
|
|
|
|
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");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
SqlData data = new SqlData(mContext, c);
|
|
|
|
|
mDataList.add(data);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.w(TAG, "loadDataContent: cursor = null");
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (c != null)
|
|
|
|
|
c.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:setContent(JSONObject js)
|
|
|
|
|
* @参数:JSONObject js
|
|
|
|
|
* @功能描述:接受一个JSONObject作为参数,并根据传入的JSON对象更新内部的成员变量。
|
|
|
|
|
* @实现过程:从JSON对象中获取一个名为note的子对象,判断note的类型,如果是系统文件夹类型,则打印警告日志并返回。如果是文件夹类型,则更新成员变量mSnippet和mType。如果是笔记类型,则依次更新多个成员变量,包括mId、mAlertDate、mBgColorId、mCreatedDate、mHasAttachment、mModifiedDate、mParentId、mSnippet、mType、mWidgetId、mWidgetType和mOriginParent。然后,它从JSON对象中获取一个名为dataArray的数组,遍历数组中的每个元素,将每个元素转换为SqlData对象,并调用SqlData对象的setContent方法更新数据。最后,如果过程中发生了JSON解析异常,捕获异常并打印错误日志,然后返回false。否则,返回true表示更新成功。
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
|
|
|
|
|
: Notes.TYPE_NOTE;
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
if (dataId == temp.getId()) {
|
|
|
|
|
sqlData = temp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sqlData == null) {
|
|
|
|
|
sqlData = new SqlData(mContext);
|
|
|
|
|
mDataList.add(sqlData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sqlData.setContent(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:JSONObject getContent()
|
|
|
|
|
* @功能描述:创建返回一个JSONObject对象
|
|
|
|
|
* @实现过程:创建一个JSONObject对象,然后根据一些条件进行填充,最终返回这个JSONObject对象。
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
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时
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:setParentId(long id)
|
|
|
|
|
* @参数:long id
|
|
|
|
|
* @功能描述:设置笔记的父级ID
|
|
|
|
|
* @实现过程:接收一个long类型的参数id,将其赋值给成员变量mParentId,并将其添加到mDiffNoteValues中
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
public void setParentId(long id) {
|
|
|
|
|
mParentId = id;
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:setGtaskId(String gid)
|
|
|
|
|
* @参数:String gid
|
|
|
|
|
* @功能描述:设置笔记的GTASK_ID
|
|
|
|
|
* @实现过程:接收一个字符串类型的参数gid,将其添加到mDiffNoteValues中,以表示与原始笔记对象的差异
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
public void setGtaskId(String gid) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:setSyncId(long syncId)
|
|
|
|
|
* @参数:long syncId
|
|
|
|
|
* @功能描述:给当前id设置同步id
|
|
|
|
|
* @实现过程:接收一个 long 类型的参数 syncId,将其添加到 mDiffNoteValues 中,以表示与原始笔记对象的差异
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
public void setSyncId(long syncId) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:resetLocalModified()
|
|
|
|
|
* @功能描述:重置笔记的本地修改状态
|
|
|
|
|
* @实现过程:将本地修改标志(LOCAL_MODIFIED)设置为0,表示该笔记没有进行任何本地修改
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
public void resetLocalModified() {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:getId()
|
|
|
|
|
* @功能描述:获取笔记的ID
|
|
|
|
|
* @实现过程:回成员变量 mId 的值
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
public long getId() {
|
|
|
|
|
return mId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:getParentId()
|
|
|
|
|
* @功能描述:获取笔记的父Id
|
|
|
|
|
* @实现过程:回成员变量 mParentId 的值
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
public long getParentId() {
|
|
|
|
|
return mParentId;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:getSnippet()
|
|
|
|
|
* @功能描述:获取小片段即用于显示的部分便签内容
|
|
|
|
|
* @实现过程:返回成员变量 mSnippet 的值,表示该笔记对象的摘要信息。
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
public String getSnippet() {
|
|
|
|
|
return mSnippet;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:isNoteType()
|
|
|
|
|
* @功能描述:判断笔记类型是否为普通笔记
|
|
|
|
|
* @实现过程:返回一个布尔值,如果笔记类型为 TYPE_NOTE,则返回 true,否则返回 false
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
public boolean isNoteType() {
|
|
|
|
|
return mType == Notes.TYPE_NOTE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @功能名:commit(boolean validateVersion)
|
|
|
|
|
* @参数:boolean validateVersion
|
|
|
|
|
* @功能描述:提交笔记的更改,接受一个布尔值参数 validateVersion,指定是否验证版本信息。
|
|
|
|
|
* @实现过程:首先,方法检查笔记是否是新创建的(mIsCreate),如果是新创建的,则执行以下操作:
|
|
|
|
|
* 如果笔记的 ID(mId)为无效 ID(INVALID_ID),并且 mDiffNoteValues(笔记差异值)包含 NoteColumns.ID(ID 列),则从 mDiffNoteValues 中移除 NoteColumns.ID。
|
|
|
|
|
* 使用 mContentResolver 插入 mDiffNoteValues 中的值到 Notes.CONTENT_NOTE_URI(笔记内容的 URI)并返回插入的 Uri。
|
|
|
|
|
* 通过解析 Uri 中的路径段,获取笔记的 ID,并将其赋值给 mId。
|
|
|
|
|
* 如果 mId 为 0,则抛出 IllegalStateException 异常,表示创建笔记失败。
|
|
|
|
|
* 如果笔记类型是普通笔记(Notes.TYPE_NOTE),则遍历 mDataList(数据列表)中的 SqlData 对象,并调用其 commit() 方法提交数据。
|
|
|
|
|
* 如果笔记不是新创建的,则执行以下操作:
|
|
|
|
|
* 检查笔记的 ID(mId)是否有效,如果小于等于 0,且不是特定的 ID(Notes.ID_ROOT_FOLDER 和 Notes.ID_CALL_RECORD_FOLDER),则抛出 IllegalStateException 异常,表示更新笔记时使用了无效的 ID。
|
|
|
|
|
* 如果 mDiffNoteValues(笔记差异值)的大小大于 0,则增加笔记的版本号(mVersion)。
|
|
|
|
|
* 根据 validateVersion 参数的值,构造不同的查询条件,并使用 mContentResolver 更新 Notes.CONTENT_NOTE_URI(笔记内容的 URI)中的数据。
|
|
|
|
|
* 如果更新结果为 0,则打印日志提示用户在同步过程中可能已经更新了笔记。
|
|
|
|
|
* 如果笔记类型是普通笔记(Notes.TYPE_NOTE),则遍历 mDataList(数据列表)中的 SqlData 对象,并调用其 commit() 方法提交数据。
|
|
|
|
|
* 最后,方法刷新本地信息,加载更新后的笔记数据,并清空 mDiffNoteValues(笔记差异值),将创建标志 mIsCreate 设置为 false。
|
|
|
|
|
* @Author:cyh
|
|
|
|
|
*/
|
|
|
|
|
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));
|
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
for (SqlData sqlData : mDataList) {//直接使用sqldata中的实现
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
if (mDiffNoteValues.size() > 0) {
|
|
|
|
|
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[] {
|
|
|
|
|
String.valueOf(mId), String.valueOf(mVersion)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (result == 0) {
|
|
|
|
|
Log.w(TAG, "there is no update. maybe user updates note when syncing");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|