|
|
|
@ -39,7 +39,7 @@ import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class SqlNote {
|
|
|
|
|
private static final String TAG = SqlNote.class.getSimpleName();////调用getSimpleName ()函数得到类的简写名称存入字符串TAG中////
|
|
|
|
|
private static final String TAG = SqlNote.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
private static final int INVALID_ID = -99999;
|
|
|
|
|
|
|
|
|
@ -50,51 +50,54 @@ public class SqlNote {
|
|
|
|
|
NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE, NoteColumns.SYNC_ID,
|
|
|
|
|
NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID,
|
|
|
|
|
NoteColumns.VERSION
|
|
|
|
|
};//集合了interface NoteColumns中所有17个SF常量//
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//以下设置17个列的编号,对应不同的属性//
|
|
|
|
|
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;//父节点ID//
|
|
|
|
|
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;//窗口小部件ID//
|
|
|
|
|
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;//同步ID//
|
|
|
|
|
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;//原始文件夹ID//
|
|
|
|
|
public static final int ORIGIN_PARENT_ID_COLUMN = 14;
|
|
|
|
|
|
|
|
|
|
public static final int GTASK_ID_COLUMN = 15;//用户ID//
|
|
|
|
|
public static final int GTASK_ID_COLUMN = 15;
|
|
|
|
|
|
|
|
|
|
public static final int VERSION_COLUMN = 16;//版本号//
|
|
|
|
|
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;//通过ArrayList记录note中的data//
|
|
|
|
|
private long mId;
|
|
|
|
|
|
|
|
|
|
private long mAlertDate;
|
|
|
|
|
|
|
|
|
|
private int mBgColorId;
|
|
|
|
|
|
|
|
|
|
private long mCreatedDate;
|
|
|
|
|
|
|
|
|
|
private int mHasAttachment;
|
|
|
|
@ -119,17 +122,16 @@ public class SqlNote {
|
|
|
|
|
|
|
|
|
|
private ArrayList<SqlData> mDataList;
|
|
|
|
|
|
|
|
|
|
//以下定义了三种构造SqlNote的方法,分别需要不同的参数//
|
|
|
|
|
public SqlNote(Context context) {
|
|
|
|
|
mContext = context;//获取context程序间共享数据//
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
|
mIsCreate = true;//第一种构造方式的标识,对象是新建的//
|
|
|
|
|
mId = INVALID_ID;//无效ID//
|
|
|
|
|
mIsCreate = true;
|
|
|
|
|
mId = INVALID_ID;
|
|
|
|
|
mAlertDate = 0;
|
|
|
|
|
mBgColorId = ResourceParser.getDefaultBgId(context);//系统默认背景//
|
|
|
|
|
mCreatedDate = System.currentTimeMillis();//调用系统函数获取创建时间//
|
|
|
|
|
mBgColorId = ResourceParser.getDefaultBgId(context);
|
|
|
|
|
mCreatedDate = System.currentTimeMillis();
|
|
|
|
|
mHasAttachment = 0;
|
|
|
|
|
mModifiedDate = System.currentTimeMillis();//调用系统函数最近修改时间//
|
|
|
|
|
mModifiedDate = System.currentTimeMillis();
|
|
|
|
|
mParentId = 0;
|
|
|
|
|
mSnippet = "";
|
|
|
|
|
mType = Notes.TYPE_NOTE;
|
|
|
|
@ -137,23 +139,23 @@ public class SqlNote {
|
|
|
|
|
mWidgetType = Notes.TYPE_WIDGET_INVALIDE;
|
|
|
|
|
mOriginParent = 0;
|
|
|
|
|
mVersion = 0;
|
|
|
|
|
mDiffNoteValues = new ContentValues();//新建一个NoteValues值,用来记录改变的values//
|
|
|
|
|
mDataList = new ArrayList<SqlData>();//新建一个data的列表//
|
|
|
|
|
}//构造函数参数只有context,对所有的变量进行初始化//
|
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SqlNote(Context context, Cursor c) {
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
|
mIsCreate = false;//第二种方式标识为false,对象已存在,不是新建的//
|
|
|
|
|
mIsCreate = false;
|
|
|
|
|
loadFromCursor(c);
|
|
|
|
|
mDataList = new ArrayList<SqlData>();
|
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
|
loadDataContent();//如果是note类型,则调用下面的 loadDataContent()函数,加载数据内容//
|
|
|
|
|
loadDataContent();
|
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
|
}//构造函数有context和一个数据库的cursor两个参数,多数变量通过cursor指向的一条记录直接进行初始化//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public SqlNote(Context context, long id) {
|
|
|
|
|
mContext = context;//将mIsCreate定义为False作为标识,以id为参数,运行loadFromCursor函数//
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = context.getContentResolver();
|
|
|
|
|
mIsCreate = false;
|
|
|
|
|
loadFromCursor(id);
|
|
|
|
@ -162,26 +164,26 @@ public class SqlNote {
|
|
|
|
|
loadDataContent();
|
|
|
|
|
mDiffNoteValues = new ContentValues();
|
|
|
|
|
|
|
|
|
|
}//第三种构造方式,采用context和id//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);//通过try避免异常//
|
|
|
|
|
String.valueOf(id)
|
|
|
|
|
}, null);
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.moveToNext();
|
|
|
|
|
loadFromCursor(c);
|
|
|
|
|
} else {
|
|
|
|
|
Log.w(TAG, "loadFromCursor: cursor = null");
|
|
|
|
|
}//代码块:如果有内容就将移入文档,并再次等待光标的内容,否则报错//
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (c != null)
|
|
|
|
|
c.close();
|
|
|
|
|
}//执行完后关闭释放//
|
|
|
|
|
}//通过id从cursor加载数据//
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadFromCursor(Cursor c) {
|
|
|
|
|
mId = c.getLong(ID_COLUMN);
|
|
|
|
@ -196,142 +198,141 @@ public class SqlNote {
|
|
|
|
|
mWidgetId = c.getInt(WIDGET_ID_COLUMN);
|
|
|
|
|
mWidgetType = c.getInt(WIDGET_TYPE_COLUMN);
|
|
|
|
|
mVersion = c.getLong(VERSION_COLUMN);
|
|
|
|
|
}//通过游标从光标处加载数据//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadDataContent() {
|
|
|
|
|
Cursor c = null;//通过content获取共享数据并加载到数据库当前游标处//
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
mDataList.clear();
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA,
|
|
|
|
|
"(note_id=?)", new String[] {
|
|
|
|
|
String.valueOf(mId)
|
|
|
|
|
}, null);//获取ID对应content内容//
|
|
|
|
|
String.valueOf(mId)
|
|
|
|
|
}, null);
|
|
|
|
|
if (c != null) {
|
|
|
|
|
if (c.getCount() == 0) {
|
|
|
|
|
Log.w(TAG, "it seems that the note has not data");
|
|
|
|
|
return;
|
|
|
|
|
}//查询到该note的id确实有对应项,即cursor有对应//
|
|
|
|
|
}
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
SqlData data = new SqlData(mContext, c);//将获取数据存入数据表//
|
|
|
|
|
SqlData data = new SqlData(mContext, c);
|
|
|
|
|
mDataList.add(data);
|
|
|
|
|
}//记录数量不为0,则循环直到记录不存在,不断地取出记录放到DataList中//
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.w(TAG, "loadDataContent: cursor = null");
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (c != null)
|
|
|
|
|
c.close();
|
|
|
|
|
}//最后若游标不为空,关闭游标并释放//
|
|
|
|
|
}/*获取ID对应content内容,如果查询到该note的id确实有对应项,即cursor有对应,获取ID对应content内容*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*设置通过content机制共享的数据信息*/
|
|
|
|
|
public boolean setContent(JSONObject js) {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);//创建一个JSONObject对象note//
|
|
|
|
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
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) {
|
|
|
|
|
// for folder we can only update the snnipet and type
|
|
|
|
|
String snippet = note.has(NoteColumns.SNIPPET) ? note
|
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";//如果共享数据存在摘要,则将其赋给snippet变量,否则该变量为空//
|
|
|
|
|
.getString(NoteColumns.SNIPPET) : "";
|
|
|
|
|
if (mIsCreate || !mSnippet.equals(snippet)) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
|
|
|
|
|
}//如果SQLNote采用的是第一种构造方式,或者snippet为空,则将snippet这一项键值存入contentvalue中,尽管是“”//
|
|
|
|
|
}
|
|
|
|
|
mSnippet = snippet;
|
|
|
|
|
|
|
|
|
|
int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
|
|
|
|
|
: Notes.TYPE_NOTE;// 获取数据类型,以下操作都和上面对snippet的操作一样,一起根据共享的数据设置SqlNote内容的上述17项//
|
|
|
|
|
: Notes.TYPE_NOTE;
|
|
|
|
|
if (mIsCreate || mType != type) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.TYPE, 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;//获取ID//
|
|
|
|
|
long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID;
|
|
|
|
|
if (mIsCreate || mId != id) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.ID, id);
|
|
|
|
|
}//如果只是通过上下文对note进行数据库操作,或者该ID与原ID不相同//
|
|
|
|
|
}
|
|
|
|
|
mId = id;
|
|
|
|
|
|
|
|
|
|
long alertDate = note.has(NoteColumns.ALERTED_DATE) ? note
|
|
|
|
|
.getLong(NoteColumns.ALERTED_DATE) : 0;//获取提醒时间//
|
|
|
|
|
.getLong(NoteColumns.ALERTED_DATE) : 0;
|
|
|
|
|
if (mIsCreate || mAlertDate != alertDate) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.ALERTED_DATE, alertDate);
|
|
|
|
|
}//如果只是通过上下文对note进行数据库操作,或者该提醒日期与原提醒日期不相同,//
|
|
|
|
|
}
|
|
|
|
|
mAlertDate = alertDate;
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId);
|
|
|
|
|
}//同上,此处为背景颜色//
|
|
|
|
|
}
|
|
|
|
|
mBgColorId = bgColorId;
|
|
|
|
|
|
|
|
|
|
long createDate = note.has(NoteColumns.CREATED_DATE) ? note
|
|
|
|
|
.getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis();//获取数据的创建日期,//
|
|
|
|
|
.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);
|
|
|
|
|
}//同上,此处为父类ID//
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}//同上,此处为小部件ID//
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}//同上,此处为将该原始父文件夹ID覆盖原原始父文件夹ID//
|
|
|
|
|
}
|
|
|
|
|
mOriginParent = originParent;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
|
JSONObject data = dataArray.getJSONObject(i);//依次获取数据表中的数据ID//
|
|
|
|
|
JSONObject data = dataArray.getJSONObject(i);
|
|
|
|
|
SqlData sqlData = null;
|
|
|
|
|
if (data.has(DataColumns.ID)) {
|
|
|
|
|
long dataId = data.getLong(DataColumns.ID);
|
|
|
|
@ -339,25 +340,25 @@ public class SqlNote {
|
|
|
|
|
if (dataId == temp.getId()) {
|
|
|
|
|
sqlData = temp;
|
|
|
|
|
}
|
|
|
|
|
}// 该数据ID对应的数据如果存在,将对应的数据存在数据库中//
|
|
|
|
|
}//遍历 dataArray,查找 id 为 dataId 的数据//
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (sqlData == null) {
|
|
|
|
|
sqlData = new SqlData(mContext);
|
|
|
|
|
mDataList.add(sqlData);
|
|
|
|
|
}/*如果数据库没有更新, 就根据上下文创建一个数据库数据,并添加到数据列表中*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sqlData.setContent(data);// 最后为数据库数据进行设置//
|
|
|
|
|
sqlData.setContent(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return false;
|
|
|
|
|
}//出现JSONException时,日志显示错误,同时打印堆栈轨迹//
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
//获取content机制提供的数据并加载到note中//
|
|
|
|
|
|
|
|
|
|
public JSONObject getContent() {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
@ -365,10 +366,10 @@ public class SqlNote {
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
|
Log.e(TAG, "it seems that we haven't created this in database yet");
|
|
|
|
|
return null;
|
|
|
|
|
}//采用的是第一种构造方式,自然实施初始化而已,显示错误:没创建数据库//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JSONObject note = new JSONObject();//新建变量note用于传输共享数据//
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {//如果对象的类型是note类型,设置以上12个内部变量//
|
|
|
|
|
JSONObject note = new JSONObject();
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
note.put(NoteColumns.ID, mId);
|
|
|
|
|
note.put(NoteColumns.ALERTED_DATE, mAlertDate);
|
|
|
|
|
note.put(NoteColumns.BG_COLOR_ID, mBgColorId);
|
|
|
|
@ -388,118 +389,117 @@ public class SqlNote {
|
|
|
|
|
JSONObject data = sqlData.getContent();
|
|
|
|
|
if (data != null) {
|
|
|
|
|
dataArray.put(data);
|
|
|
|
|
}//将note中的所有数据存进dataarray中,
|
|
|
|
|
}//利用循环将数据链表的数据获取//
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}//如果是文件夹或者系统文件,那么只存放id,种类,摘要和note//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}//捕获json类型异常,显示错误,打印堆栈痕迹//
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setParentId(long id) {
|
|
|
|
|
mParentId = id;
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
|
|
|
|
|
}//给当前id设置父id//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setGtaskId(String gid) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
|
|
|
|
|
}//给当前id设置Gtaskid//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setSyncId(long syncId) {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
|
|
|
|
|
}//给当前id设置同步id//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void resetLocalModified() {
|
|
|
|
|
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
|
|
|
|
|
}//重新设置本地的修改//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long getId() {
|
|
|
|
|
return mId;
|
|
|
|
|
}//获取当前ID//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public long getParentId() {
|
|
|
|
|
return mParentId;
|
|
|
|
|
}//获取当前ID的父类ID//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getSnippet() {
|
|
|
|
|
return mSnippet;
|
|
|
|
|
}//获取小片段即用于显示的部分便签内容//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isNoteType() {
|
|
|
|
|
return mType == Notes.TYPE_NOTE;
|
|
|
|
|
}//判断是否为便签类型//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//commit函数用于把当前造作所做的修改保存到数据库//
|
|
|
|
|
public void commit(boolean validateVersion) {
|
|
|
|
|
if (mIsCreate) {//如果是第一种构造方式//
|
|
|
|
|
if (mIsCreate) {
|
|
|
|
|
if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
|
|
|
|
|
mDiffNoteValues.remove(NoteColumns.ID);
|
|
|
|
|
}//如果是一个无效的id并且还含有这个id,就将它移除//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);//更新Uri//
|
|
|
|
|
Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);
|
|
|
|
|
try {
|
|
|
|
|
mId = Long.valueOf(uri.getPathSegments().get(1));//强制转换path为id ,Long型//
|
|
|
|
|
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");
|
|
|
|
|
} //捕获异常,转换出错,显示错误“获取note的id出现错误”//
|
|
|
|
|
}
|
|
|
|
|
if (mId == 0) {
|
|
|
|
|
throw new IllegalStateException("Create thread id failed");
|
|
|
|
|
}//创建线程 id 失败//
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mType == Notes.TYPE_NOTE) {
|
|
|
|
|
for (SqlData sqlData : mDataList) {
|
|
|
|
|
sqlData.commit(mId, false, -1);
|
|
|
|
|
}
|
|
|
|
|
}//对于note类型,引用sqlData.commit方法操作//
|
|
|
|
|
}
|
|
|
|
|
} 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");
|
|
|
|
|
}//判断是否含有这个便签,不存在的话,尝试以无效 id 更新 note//
|
|
|
|
|
}
|
|
|
|
|
if (mDiffNoteValues.size() > 0) {
|
|
|
|
|
mVersion ++;//更新版本:版本升级一个等级//
|
|
|
|
|
mVersion ++;
|
|
|
|
|
int result = 0;
|
|
|
|
|
if (!validateVersion) {
|
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
|
+ NoteColumns.ID + "=?)", new String[] {
|
|
|
|
|
String.valueOf(mId)
|
|
|
|
|
});//如果是无效版本,更新内容解析器:存入便签内容uri,便签ID,mID,构造字符串//
|
|
|
|
|
String.valueOf(mId)
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
|
|
|
|
|
+ NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
|
|
|
|
|
+ NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
|
|
|
|
|
new String[] {
|
|
|
|
|
String.valueOf(mId), String.valueOf(mVersion)
|
|
|
|
|
});
|
|
|
|
|
}//如果是有效版本, 更新内容解析器:存入便签内容uri,便签ID,便签版本,mID,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);
|
|
|
|
|
}
|
|
|
|
|
}//对note类型,还是对其中的data引用commit,从而实现目的//
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// refresh local info
|
|
|
|
|
loadFromCursor(mId);//通过 cursor 从当前 id 处加载数据//
|
|
|
|
|
loadFromCursor(mId);
|
|
|
|
|
if (mType == Notes.TYPE_NOTE)
|
|
|
|
|
loadDataContent();// 如果是便签类型:获取共享数据并加载到数据库//
|
|
|
|
|
loadDataContent();
|
|
|
|
|
|
|
|
|
|
mDiffNoteValues.clear();//改变数据库构造模式//
|
|
|
|
|
mIsCreate = false;//改变数据库构造模式//
|
|
|
|
|
mDiffNoteValues.clear();
|
|
|
|
|
mIsCreate = false;
|
|
|
|
|
}
|
|
|
|
|
}
|