|
|
|
@ -0,0 +1,365 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
//Task代码注释
|
|
|
|
|
//主要作用:同步任务,将创建、更新和同步动作包装成JSON对象,同时使用本地和远程的JSON对结点内容进行设置,从而获取同步信息,实现本地和远程的信息同步
|
|
|
|
|
package net.micode.notes.gtask.data;
|
|
|
|
|
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.DataColumns;
|
|
|
|
|
import net.micode.notes.data.Notes.DataConstants;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException;
|
|
|
|
|
import net.micode.notes.tool.GTaskStringUtils;
|
|
|
|
|
|
|
|
|
|
import org.json.JSONArray;
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class Task extends Node { //定义一个Task类,继承Node类
|
|
|
|
|
private static final String TAG = Task.class.getSimpleName();
|
|
|
|
|
//调用函数得到类简写名称并存入字符串TAG内
|
|
|
|
|
private boolean mCompleted; //判断当前任务是否完成
|
|
|
|
|
|
|
|
|
|
private String mNotes; //在实例中存储数据的类型,表示任务的注释信息
|
|
|
|
|
|
|
|
|
|
private JSONObject mMetaInfo; //将在实例中存储数据的类型
|
|
|
|
|
|
|
|
|
|
private Task mPriorSibling; //对应的优先兄弟Task的指针
|
|
|
|
|
|
|
|
|
|
private TaskList mParent; //所在的任务列表的指针
|
|
|
|
|
|
|
|
|
|
public Task() {
|
|
|
|
|
super();
|
|
|
|
|
mCompleted = false;
|
|
|
|
|
mNotes = null;
|
|
|
|
|
mPriorSibling = null; //TaskList中当前Task前面的Task的指针
|
|
|
|
|
mParent = null; //当前Task所在的TaskList
|
|
|
|
|
mMetaInfo = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONObject getCreateAction(int actionId) {
|
|
|
|
|
//getCreateAction方法接收一个整型参数actionId,将该参数作为返回JSONObject对象的action_id属性值。
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// action_type
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, //存入当前task指针
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);
|
|
|
|
|
//发送对actiontype的请求
|
|
|
|
|
// action_id
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
|
|
|
|
|
|
|
|
|
|
// index 父任务列表中的索引
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this)); //设置索引
|
|
|
|
|
|
|
|
|
|
// entity_delta
|
|
|
|
|
JSONObject entity = new JSONObject();
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); //获取任务名
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); //对任务创建id进行判空操作
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE, //实体类数据
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_TYPE_TASK);
|
|
|
|
|
if (getNotes() != null) {
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes());
|
|
|
|
|
} //若存在文本输入放入实体内
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); //讲实体存入js
|
|
|
|
|
|
|
|
|
|
// parent_id
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid()); //父类id类型
|
|
|
|
|
|
|
|
|
|
// dest_parent_type
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_TYPE_GROUP); //所在列表的id存入父id
|
|
|
|
|
|
|
|
|
|
// list_id
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid());
|
|
|
|
|
|
|
|
|
|
// prior_sibling_id
|
|
|
|
|
if (mPriorSibling != null) {
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid());
|
|
|
|
|
}
|
|
|
|
|
//如果该任务在兄弟任务中不是第一个,则设置prior_sibling_id属性值为前一个兄弟任务的gid
|
|
|
|
|
//将其优先存入ID序列中
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString()); //e.toString()获取异常类型和异常详细消息
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("fail to generate task-create jsonobject");
|
|
|
|
|
} //抛出异常处理机制
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONObject getUpdateAction(int actionId) { //更新action
|
|
|
|
|
//getUpdateAction方法接收一个整型参数actionId,将该参数作为返回JSONObject对象的action_id属性值
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// action_type
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);
|
|
|
|
|
//action_type属性值为update,表示更新操作
|
|
|
|
|
|
|
|
|
|
// action_id
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
|
|
|
|
|
|
|
|
|
|
// id
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());
|
|
|
|
|
|
|
|
|
|
// entity_delta
|
|
|
|
|
JSONObject entity = new JSONObject();
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
|
|
|
|
|
if (getNotes() != null) {
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes());
|
|
|
|
|
}//如果存在 notes ,则将其也放入 entity 中
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted());
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);
|
|
|
|
|
//deleted属性表示任务是否被删除,如果该值为true,则该任务会被移除
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) { //获取异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace(); //命令行打印异常信息在程序中出错的位置及原因
|
|
|
|
|
throw new ActionFailureException("fail to generate task-update jsonobject");
|
|
|
|
|
} //生成任务更新的数据传输失败
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setContentByRemoteJSON(JSONObject js) //通过云端传输数据设置内容
|
|
|
|
|
//定义了一个setContentByRemoteJSON方法,该方法从远程JSON对象中获取任务的属性值,并将这些属性值设置到本地任务对象中
|
|
|
|
|
//表示从远程服务器获取的任务信息
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
if (js != null) {
|
|
|
|
|
try {
|
|
|
|
|
// id
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { //如果js指针指向的目标存在
|
|
|
|
|
setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); //利用类自身的set函数,并且使用了JSONObject的get类型函数
|
|
|
|
|
} //判断记录是否被删除
|
|
|
|
|
|
|
|
|
|
// last_modified
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) {
|
|
|
|
|
setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// name
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) {
|
|
|
|
|
setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// notes
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) {
|
|
|
|
|
setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// deleted
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) {
|
|
|
|
|
setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// completed
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) {
|
|
|
|
|
setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED));
|
|
|
|
|
} //记录已经完成的
|
|
|
|
|
} catch (JSONException e) { //抛出异常
|
|
|
|
|
Log.e(TAG, e.toString()); //获取异常类型和异常信息
|
|
|
|
|
e.printStackTrace(); //打印
|
|
|
|
|
throw new ActionFailureException("fail to get task content from jsonobject");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setContentByLocalJSON(JSONObject js) { //通过本地的jsonobject获取内容
|
|
|
|
|
if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)
|
|
|
|
|
|| !js.has(GTaskStringUtils.META_HEAD_DATA)) { //如果js不存在或者js没有元数据的开头或者js指针没有元数据
|
|
|
|
|
Log.w(TAG, "setContentByLocalJSON: nothing is avaiable"); //反馈给用户出错信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
|
|
|
|
|
|
if (note.getInt(NoteColumns.TYPE) != Notes.TYPE_NOTE) { //note类型不匹配
|
|
|
|
|
Log.e(TAG, "invalid type"); //无效
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
|
//遍历 dataArray 查找与数据库中DataConstants.NOTE 记录信息一致的 data
|
|
|
|
|
JSONObject data = dataArray.getJSONObject(i);
|
|
|
|
|
if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) {
|
|
|
|
|
setName(data.getString(DataColumns.CONTENT)); //判断两个字符串是否相等
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONObject getLocalJSONFromContent() { //从content获取本地json
|
|
|
|
|
String name = getName(); //获取名称
|
|
|
|
|
try {
|
|
|
|
|
if (mMetaInfo == null) {
|
|
|
|
|
// new task created from web
|
|
|
|
|
if (name == null) {
|
|
|
|
|
Log.w(TAG, "the note seems to be an empty one");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
//若命名为空则创建一个新的
|
|
|
|
|
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
JSONObject note = new JSONObject();
|
|
|
|
|
JSONArray dataArray = new JSONArray();
|
|
|
|
|
JSONObject data = new JSONObject(); //对四个指针进行初始化
|
|
|
|
|
data.put(DataColumns.CONTENT, name);
|
|
|
|
|
dataArray.put(data);
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); //元数据类型信息非空,则进行同步更新操作
|
|
|
|
|
note.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note); //实现数据同步
|
|
|
|
|
return js;
|
|
|
|
|
} else { //否则将元数据同步至数据中
|
|
|
|
|
// synced task
|
|
|
|
|
JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); //同步任务
|
|
|
|
|
JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA); //同步数据
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
|
JSONObject data = dataArray.getJSONObject(i);
|
|
|
|
|
if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) {
|
|
|
|
|
data.put(DataColumns.CONTENT, getName());
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}//通过循环查找与数据库中DataConstants.NOTE 记录信息一致的 data
|
|
|
|
|
|
|
|
|
|
note.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
|
|
|
|
|
return mMetaInfo;
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setMetaInfo(MetaData metaData) { //设置元数据信息
|
|
|
|
|
if (metaData != null && metaData.getNotes() != null) {
|
|
|
|
|
try {
|
|
|
|
|
mMetaInfo = new JSONObject(metaData.getNotes());
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.w(TAG, e.toString());
|
|
|
|
|
mMetaInfo = null;
|
|
|
|
|
}
|
|
|
|
|
} //如果元数据非空且其 notes 非空,则修改元数据类型信息
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getSyncAction(Cursor c) { //实现同步操作
|
|
|
|
|
try {
|
|
|
|
|
JSONObject noteInfo = null; //新建一个 JSONObject 的对象实体
|
|
|
|
|
if (mMetaInfo != null && mMetaInfo.has(GTaskStringUtils.META_HEAD_NOTE)) {
|
|
|
|
|
noteInfo = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
} //元数据信息不为空并且元数据信息还含有“META_HEAD_NOTE”,说明便签存在
|
|
|
|
|
|
|
|
|
|
if (noteInfo == null) {
|
|
|
|
|
Log.w(TAG, "it seems that note meta has been deleted");
|
|
|
|
|
return SYNC_ACTION_UPDATE_REMOTE;
|
|
|
|
|
} //云端便签 id 已被删除,不存在,返回更新本地数据的同步行为
|
|
|
|
|
|
|
|
|
|
if (!noteInfo.has(NoteColumns.ID)) {
|
|
|
|
|
Log.w(TAG, "remote note id seems to be deleted");
|
|
|
|
|
return SYNC_ACTION_UPDATE_LOCAL;
|
|
|
|
|
} //匹配note的id
|
|
|
|
|
|
|
|
|
|
// validate the note id now
|
|
|
|
|
if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) {
|
|
|
|
|
Log.w(TAG, "note id doesn't match");
|
|
|
|
|
return SYNC_ACTION_UPDATE_LOCAL; //判断修改后的ID匹配是否成功,成功则返回无同步操作,未成功则应用云端到本地,返回本地同步更新操作
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) {
|
|
|
|
|
// there is no local update
|
|
|
|
|
if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
|
|
|
|
|
// no update both side
|
|
|
|
|
return SYNC_ACTION_NONE;
|
|
|
|
|
} else {
|
|
|
|
|
// apply remote to local
|
|
|
|
|
return SYNC_ACTION_UPDATE_LOCAL;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// validate gtask id
|
|
|
|
|
if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) {
|
|
|
|
|
//判断gtask的id与获取的id是否匹配
|
|
|
|
|
Log.e(TAG, "gtask id doesn't match"); //错误,未找到 gtask 的 id
|
|
|
|
|
return SYNC_ACTION_ERROR;
|
|
|
|
|
}
|
|
|
|
|
if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
|
|
|
|
|
////错误,未找到 gtask 的 id
|
|
|
|
|
// local modification only
|
|
|
|
|
return SYNC_ACTION_UPDATE_REMOTE;
|
|
|
|
|
} else {
|
|
|
|
|
return SYNC_ACTION_UPDATE_CONFLICT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SYNC_ACTION_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean isWorthSaving() { //判断是否修改并同步操作
|
|
|
|
|
return mMetaInfo != null || (getName() != null && getName().trim().length() > 0)
|
|
|
|
|
|| (getNotes() != null && getNotes().trim().length() > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCompleted(boolean completed) {
|
|
|
|
|
this.mCompleted = completed;
|
|
|
|
|
} //设定是否完成的成员变量
|
|
|
|
|
|
|
|
|
|
public void setNotes(String notes) {
|
|
|
|
|
this.mNotes = notes;
|
|
|
|
|
} //设定note成员变量
|
|
|
|
|
|
|
|
|
|
public void setPriorSibling(Task priorSibling) {
|
|
|
|
|
this.mPriorSibling = priorSibling;
|
|
|
|
|
} //设置优先兄弟 task 的优先级
|
|
|
|
|
|
|
|
|
|
public void setParent(TaskList parent) {
|
|
|
|
|
this.mParent = parent;
|
|
|
|
|
} //设置父类节点
|
|
|
|
|
|
|
|
|
|
public boolean getCompleted() {
|
|
|
|
|
return this.mCompleted;
|
|
|
|
|
} //判断是否完成
|
|
|
|
|
|
|
|
|
|
public String getNotes() {
|
|
|
|
|
return this.mNotes;
|
|
|
|
|
} //获取成员变量 mNotes 的信息
|
|
|
|
|
|
|
|
|
|
public Task getPriorSibling() {
|
|
|
|
|
return this.mPriorSibling;
|
|
|
|
|
} //获取优先兄弟列表
|
|
|
|
|
|
|
|
|
|
public TaskList getParent() {
|
|
|
|
|
return this.mParent;
|
|
|
|
|
} //获取父类节点列表
|
|
|
|
|
|
|
|
|
|
}
|