|
|
/*
|
|
|
* 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; // 导入Cursor类
|
|
|
import android.text.TextUtils; // 导入TextUtils类
|
|
|
import android.util.Log; // 导入Log类
|
|
|
|
|
|
import net.micode.notes.data.Notes; // 导入Notes类
|
|
|
import net.micode.notes.data.Notes.DataColumns; // 导入DataColumns类
|
|
|
import net.micode.notes.data.Notes.DataConstants; // 导入DataConstants类
|
|
|
import net.micode.notes.data.Notes.NoteColumns; // 导入NoteColumns类
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException; // 导入ActionFailureException类
|
|
|
import net.micode.notes.tool.GTaskStringUtils; // 导入GTaskStringUtils类
|
|
|
|
|
|
import org.json.JSONArray; // 导入JSONArray类
|
|
|
import org.json.JSONException; // 导入JSONException类
|
|
|
import org.json.JSONObject; // 导入JSONObject类
|
|
|
|
|
|
public class Task extends Node { // 定义Task类,继承自Node类
|
|
|
private static final String TAG = Task.class.getSimpleName(); // 定义静态常量TAG为类名
|
|
|
|
|
|
private boolean mCompleted; // 声明布尔型变量mCompleted,表示任务是否已完成
|
|
|
private String mNotes; // 声明字符串变量mNotes,表示笔记内容
|
|
|
private JSONObject mMetaInfo; // 声明JSONObject变量mMetaInfo,表示元信息
|
|
|
private Task mPriorSibling; // 声明Task变量mPriorSibling,表示前一个兄弟任务
|
|
|
private TaskList mParent; // 声明TaskList变量mParent,表示父任务列表
|
|
|
|
|
|
public Task() { // Task类的构造方法
|
|
|
super(); // 调用父类的构造方法
|
|
|
mCompleted = false; // 初始化mCompleted为false
|
|
|
mNotes = null; // 初始化mNotes为null
|
|
|
mPriorSibling = null; // 初始化mPriorSibling为null
|
|
|
mParent = null; // 初始化mParent为null
|
|
|
mMetaInfo = null; // 初始化mMetaInfo为null
|
|
|
}
|
|
|
|
|
|
public JSONObject getCreateAction(int actionId) { // 定义获取创建动作的JSONObject方法
|
|
|
JSONObject js = new JSONObject(); // 创建新的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); // 设置动作ID
|
|
|
|
|
|
// 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为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()); // 设置父ID
|
|
|
|
|
|
// 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()); // 设置列表ID
|
|
|
|
|
|
// prior_sibling_id
|
|
|
if (mPriorSibling != null) {
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid()); // 设置前一个兄弟任务ID
|
|
|
}
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
Log.e(TAG, e.toString());
|
|
|
e.printStackTrace();
|
|
|
throw new ActionFailureException("fail to generate task-create jsonobject"); // 抛出创建任务JSONObject失败的异常
|
|
|
}
|
|
|
|
|
|
return js; // 返回JSONObject对象
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 获取更新动作的JSONObject
|
|
|
*
|
|
|
* @param actionId 动作ID
|
|
|
* @return JSONObject对象
|
|
|
*/
|
|
|
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
|
|
|
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());
|
|
|
// 将实体变化JSONObject放入主JSONObject
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据远程的JSONObject设置内容
|
|
|
*
|
|
|
* @param js 远程JSONObject对象
|
|
|
*/
|
|
|
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");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据本地的JSONObject设置内容
|
|
|
*
|
|
|
* @param js 本地JSONObject对象
|
|
|
*/
|
|
|
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 available");
|
|
|
}
|
|
|
|
|
|
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() {
|
|
|
String name = getName(); // 获取名称
|
|
|
|
|
|
try {
|
|
|
if (mMetaInfo == null) { // 如果元信息为空
|
|
|
// 从网络创建新任务
|
|
|
if (name == null) { // 如果名称为空
|
|
|
Log.w(TAG, "the note seems to be an empty one"); // 记录警告日志
|
|
|
return null; // 返回空值
|
|
|
}
|
|
|
|
|
|
JSONObject js = new JSONObject(); // 创建JSONObject对象
|
|
|
JSONObject note = new JSONObject(); // 创建JSONObject对象
|
|
|
JSONArray dataArray = new JSONArray(); // 创建JSONArray对象
|
|
|
JSONObject data = new JSONObject(); // 创建JSONObject对象
|
|
|
data.put(DataColumns.CONTENT, name); // 向data中添加内容字段
|
|
|
dataArray.put(data); // 将data放入dataArray中
|
|
|
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); // 向js中添加元信息数据
|
|
|
note.put(NoteColumns.TYPE, Notes.TYPE_NOTE); // 向note中添加类型字段
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, note); // 向js中添加元信息笔记
|
|
|
return js; // 返回js对象
|
|
|
} else {
|
|
|
// 同步的任务
|
|
|
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)) { // 如果数据类型为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) {
|
|
|
if (metaData != null && metaData.getNotes() != null) { // 如果元数据和笔记不为空
|
|
|
try {
|
|
|
mMetaInfo = new JSONObject(metaData.getNotes()); // 从元数据中获取笔记并转换为JSONObject对象
|
|
|
} 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)) { // 如果笔记信息不包含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)) { // 如果游标中的ID与笔记ID不匹配
|
|
|
Log.w(TAG, "note id doesn't match"); // 记录警告日志
|
|
|
return SYNC_ACTION_UPDATE_LOCAL; // 返回更新本地操作
|
|
|
}
|
|
|
|
|
|
if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { // 如果本地修改为0
|
|
|
// 没有本地更新
|
|
|
if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { // 如果同步ID等于最后修改时间
|
|
|
// 双方都没有更新
|
|
|
return SYNC_ACTION_NONE; // 返回无操作
|
|
|
} else {
|
|
|
// 应用远程到本地
|
|
|
return SYNC_ACTION_UPDATE_LOCAL; // 返回更新本地操作
|
|
|
}
|
|
|
} else {
|
|
|
// 验证gtask id
|
|
|
if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { // 如果游标中的gtask id与当前ID不匹配
|
|
|
Log.e(TAG, "gtask id doesn't match"); // 记录错误日志
|
|
|
return SYNC_ACTION_ERROR; // 返回错误操作
|
|
|
}
|
|
|
if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { // 如果同步ID等于最后修改时间
|
|
|
// 仅本地修改
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
public void setPriorSibling(Task priorSibling) {
|
|
|
// 设置前一个兄弟任务的方法
|
|
|
this.mPriorSibling = priorSibling;
|
|
|
}
|
|
|
|
|
|
public void setParent(TaskList parent) {
|
|
|
// 设置父任务列表的方法
|
|
|
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;
|
|
|
}
|
|
|
} |