|
|
/*
|
|
|
* 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.
|
|
|
*/
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 任务类,表示Google Task中的单个任务项
|
|
|
* 继承自Node,包含任务内容、完成状态及关联的元数据
|
|
|
*/
|
|
|
public class Task extends Node {
|
|
|
private static final String TAG = Task.class.getSimpleName();
|
|
|
|
|
|
private boolean mCompleted; // 任务完成状态
|
|
|
private String mNotes; // 任务备注/内容
|
|
|
private JSONObject mMetaInfo; // 元数据信息(关联本地笔记数据)
|
|
|
private Task mPriorSibling; // 前一个兄弟任务
|
|
|
private TaskList mParent; // 所属任务列表
|
|
|
|
|
|
/**
|
|
|
* 构造方法,初始化任务属性
|
|
|
*/
|
|
|
public Task() {
|
|
|
super();
|
|
|
mCompleted = false;
|
|
|
mNotes = null;
|
|
|
mPriorSibling = null;
|
|
|
mParent = null;
|
|
|
mMetaInfo = null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取创建任务的JSON操作对象
|
|
|
* @param actionId 操作ID
|
|
|
* @return 包含创建参数的JSON对象
|
|
|
*/
|
|
|
public JSONObject getCreateAction(int actionId) {
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
try {
|
|
|
// 操作类型:创建
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);
|
|
|
|
|
|
// 操作ID
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
|
|
|
|
|
|
// 在列表中的索引位置
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this));
|
|
|
|
|
|
// 实体变更数据
|
|
|
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);
|
|
|
|
|
|
// 父任务列表ID
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid());
|
|
|
|
|
|
// 目标父类型
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE,
|
|
|
GTaskStringUtils.GTASK_JSON_TYPE_GROUP);
|
|
|
|
|
|
// 所属列表ID
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid());
|
|
|
|
|
|
// 前一个兄弟任务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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取更新任务的JSON操作对象
|
|
|
* @param actionId 操作ID
|
|
|
* @return 包含更新参数的JSON对象
|
|
|
*/
|
|
|
public JSONObject getUpdateAction(int actionId) {
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
try {
|
|
|
// 操作类型:更新
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);
|
|
|
|
|
|
// 操作ID
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
|
|
|
|
|
|
// 任务ID
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());
|
|
|
|
|
|
// 实体变更数据
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从远程JSON数据解析任务内容
|
|
|
* @param js 远程服务器返回的JSON对象
|
|
|
*/
|
|
|
public void setContentByRemoteJSON(JSONObject js) {
|
|
|
if (js != null) {
|
|
|
try {
|
|
|
// 解析ID
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_ID)) {
|
|
|
setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID));
|
|
|
}
|
|
|
|
|
|
// 解析最后修改时间
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) {
|
|
|
setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED));
|
|
|
}
|
|
|
|
|
|
// 解析名称
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) {
|
|
|
setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME));
|
|
|
}
|
|
|
|
|
|
// 解析备注内容
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) {
|
|
|
setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES));
|
|
|
}
|
|
|
|
|
|
// 解析删除标记
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) {
|
|
|
setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED));
|
|
|
}
|
|
|
|
|
|
// 解析完成状态
|
|
|
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");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从本地JSON数据设置任务内容
|
|
|
* 提取笔记内容作为任务名称
|
|
|
* @param js 本地笔记JSON对象
|
|
|
*/
|
|
|
public void setContentByLocalJSON(JSONObject js) {
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
// 从数据数组中找到NOTE类型的内容作为任务名称
|
|
|
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();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从任务内容生成本地笔记JSON
|
|
|
* 如果存在元数据则更新其中的名称,否则创建新的JSON
|
|
|
* @return 本地笔记JSON对象
|
|
|
*/
|
|
|
public JSONObject getLocalJSONFromContent() {
|
|
|
String name = getName();
|
|
|
try {
|
|
|
if (mMetaInfo == null) {
|
|
|
// 从网页新建的Task,没有元数据信息
|
|
|
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 {
|
|
|
// 已同步过的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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置元数据信息
|
|
|
* @param metaData 元数据对象
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据本地数据库状态确定同步操作类型
|
|
|
* 比较本地和远程的修改状态,返回相应的同步动作
|
|
|
* @param c 本地笔记数据库游标
|
|
|
* @return 同步操作类型常量
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
// 验证笔记ID是否匹配
|
|
|
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) {
|
|
|
// 本地未修改
|
|
|
if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
|
|
|
// 双方都未修改,无需同步
|
|
|
return SYNC_ACTION_NONE;
|
|
|
} else {
|
|
|
// 远程有更新,应用远程到本地
|
|
|
return SYNC_ACTION_UPDATE_LOCAL;
|
|
|
}
|
|
|
} else {
|
|
|
// 本地有修改,验证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()) {
|
|
|
// 仅本地修改,更新远程
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 判断该任务是否值得保存
|
|
|
* @return 有元数据、名称或备注时返回true
|
|
|
*/
|
|
|
public boolean isWorthSaving() {
|
|
|
return mMetaInfo != null || (getName() != null && getName().trim().length() > 0)
|
|
|
|| (getNotes() != null && getNotes().trim().length() > 0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置完成状态
|
|
|
* @param completed 是否完成
|
|
|
*/
|
|
|
public void setCompleted(boolean completed) {
|
|
|
this.mCompleted = completed;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置备注内容
|
|
|
* @param notes 备注字符串
|
|
|
*/
|
|
|
public void setNotes(String notes) {
|
|
|
this.mNotes = notes;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置前一个兄弟任务
|
|
|
* @param priorSibling 前一个任务
|
|
|
*/
|
|
|
public void setPriorSibling(Task priorSibling) {
|
|
|
this.mPriorSibling = priorSibling;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置所属任务列表
|
|
|
* @param parent 父任务列表
|
|
|
*/
|
|
|
public void setParent(TaskList parent) {
|
|
|
this.mParent = parent;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取完成状态
|
|
|
* @return 是否完成
|
|
|
*/
|
|
|
public boolean getCompleted() {
|
|
|
return this.mCompleted;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取备注内容
|
|
|
* @return 备注字符串
|
|
|
*/
|
|
|
public String getNotes() {
|
|
|
return this.mNotes;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取前一个兄弟任务
|
|
|
* @return 前一个任务
|
|
|
*/
|
|
|
public Task getPriorSibling() {
|
|
|
return this.mPriorSibling;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取所属任务列表
|
|
|
* @return 父任务列表
|
|
|
*/
|
|
|
public TaskList getParent() {
|
|
|
return this.mParent;
|
|
|
}
|
|
|
|
|
|
}
|