|
|
|
|
package net.micode.notes.gtask.data;
|
|
|
|
|
//这行代码指定了 Task 类所在的包路径。
|
|
|
|
|
|
|
|
|
|
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 类,表示GTask中的任务。
|
|
|
|
|
private static final String TAG = Task.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
private boolean mCompleted;
|
|
|
|
|
|
|
|
|
|
private String mNotes;
|
|
|
|
|
|
|
|
|
|
private JSONObject mMetaInfo;
|
|
|
|
|
|
|
|
|
|
private Task mPriorSibling;
|
|
|
|
|
|
|
|
|
|
private TaskList mParent;
|
|
|
|
|
//这些是 Task 类的成员变量:
|
|
|
|
|
|
|
|
|
|
TAG: 用于日志记录的标签。
|
|
|
|
|
mCompleted: 表示任务是否已完成。
|
|
|
|
|
mNotes: 存储任务的备注信息。
|
|
|
|
|
mMetaInfo: 存储任务的元信息。
|
|
|
|
|
mPriorSibling: 任务的前一个兄弟任务。
|
|
|
|
|
mParent: 任务的父任务列表。
|
|
|
|
|
|
|
|
|
|
public Task() {
|
|
|
|
|
super();
|
|
|
|
|
mCompleted = false;
|
|
|
|
|
mNotes = null;
|
|
|
|
|
mPriorSibling = null;
|
|
|
|
|
mParent = null;
|
|
|
|
|
mMetaInfo = null;
|
|
|
|
|
}
|
|
|
|
|
//这是 Task 类的构造函数,用于初始化成员变量。
|
|
|
|
|
|
|
|
|
|
public JSONObject getCreateAction(int actionId) {
|
|
|
|
|
//这个方法用于生成创建任务的操作所需的 JSON 对象。它接受一个操作 ID 参数,并返回一个包含创建任务所需信息的 JSON 对象。
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// action_type
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);
|
|
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
// parent_id
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid());
|
|
|
|
|
|
|
|
|
|
// dest_parent_type
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_TYPE_GROUP);
|
|
|
|
|
|
|
|
|
|
// 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());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("fail to generate task-create jsonobject");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public JSONObject getUpdateAction(int actionId) {
|
|
|
|
|
//这个方法用于生成更新任务的操作所需的 JSON 对象。它接受一个操作 ID 参数,并返回一个包含更新任务所需信息的 JSON 对象。
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// action_type
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_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());
|
|
|
|
|
}
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted());
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);
|
|
|
|
|
|
|
|
|
|
} 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) {
|
|
|
|
|
//这个方法用于根据从远程获取的 JSON 对象设置任务的内容。它接受一个 JSON 对象作为参数,从中提取任务的相关信息,并设置到任务对象的相应属性中。
|
|
|
|
|
if (js != null) {
|
|
|
|
|
try {
|
|
|
|
|
// id
|
|
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_ID)) {
|
|
|
|
|
setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
|
//这个方法用于根据本地的 JSON 对象设置任务的内容。它接受一个 JSON 对象作为参数,从中提取任务的相关信息,并设置到任务对象的相应属性中。
|
|
|
|
|
if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)
|
|
|
|
|
|| !js.has(GTaskStringUtils.META_HEAD_DATA)) {
|
|
|
|
|
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) {
|
|
|
|
|
Log.e(TAG, "invalid type");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
|
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() {
|
|
|
|
|
//这个方法用于根据任务的内容生成本地 JSON 对象。根据任务的元信息和备注信息,它返回一个包含任务内容的 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
//这个方法用于设置任务的元信息。它接受一个 MetaData 对象作为参数,从中提取任务的元信息,并设置到任务对象的 mMetaInfo 属性中。
|
|
|
|
|
if (metaData != null && metaData.getNotes() != null) {
|
|
|
|
|
try {
|
|
|
|
|
mMetaInfo = new JSONObject(metaData.getNotes());
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.w(TAG, e.toString());
|
|
|
|
|
mMetaInfo = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getSyncAction(Cursor c) {
|
|
|
|
|
//这个方法用于确定任务的同步操作类型。它接受一个数据库游标作为参数,根据游标中的信息和任务对象的信息,确定需要执行的同步操作类型,并返回相应的整数值。
|
|
|
|
|
try {
|
|
|
|
|
JSONObject noteInfo = null;
|
|
|
|
|
if (mMetaInfo != null && mMetaInfo.has(GTaskStringUtils.META_HEAD_NOTE)) {
|
|
|
|
|
noteInfo = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (noteInfo == null) {
|
|
|
|
|
Log.w(TAG, "it seems that note meta has been deleted");
|
|
|
|
|
return SYNC_ACTION_UPDATE_REMOTE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!noteInfo.has(NoteColumns.ID)) {
|
|
|
|
|
Log.w(TAG, "remote note id seems to be deleted");
|
|
|
|
|
return SYNC_ACTION_UPDATE_LOCAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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())) {
|
|
|
|
|
Log.e(TAG, "gtask id doesn't match");
|
|
|
|
|
return SYNC_ACTION_ERROR;
|
|
|
|
|
}
|
|
|
|
|
if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
|
|
|
|
|
// 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() {
|
|
|
|
|
//这个方法用于判断任务是否值得保存。如果任务的元信息不为空,或者任务名称和备注信息不为空,则返回 true,表示任务值得保存;否则返回 false。
|
|
|
|
|
return mMetaInfo != null || (getName() != null && getName().trim().length() > 0)
|
|
|
|
|
|| (getNotes() != null && getNotes().trim().length() > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setCompleted(boolean completed) {
|
|
|
|
|
//这个方法用于设置任务的完成状态。它接受一个布尔值作为参数,表示任务是否已完成,并将其设置到任务对象的 mCompleted 属性中。
|
|
|
|
|
this.mCompleted = completed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setNotes(String notes) {
|
|
|
|
|
//这个方法用于设置任务的备注信息。它接受一个字符串作为参数,表示任务的备注信息,并将其设置到任务对象的 mNotes 属性中。
|
|
|
|
|
this.mNotes = notes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setPriorSibling(Task priorSibling) {
|
|
|
|
|
//这个方法用于设置任务的前一个兄弟任务。它接受一个 Task 对象作为参数,表示任务的前一个兄弟任务,并将其设置到任务对象的 mPriorSibling 属性中。
|
|
|
|
|
this.mPriorSibling = priorSibling;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setParent(TaskList parent) {
|
|
|
|
|
//这个方法用于设置任务的父任务列表。它接受一个 TaskList 对象作为参数,表示任务的父任务列表,并将其设置到任务对象的 mParent 属性中。
|
|
|
|
|
this.mParent = parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean getCompleted() {
|
|
|
|
|
return this.mCompleted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getNotes() {
|
|
|
|
|
return this.mNotes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task getPriorSibling() {
|
|
|
|
|
return this.mPriorSibling;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TaskList getParent() {
|
|
|
|
|
return this.mParent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|