|
|
|
|
@ -1,19 +1,3 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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;
|
|
|
|
|
@ -29,98 +13,93 @@ import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TaskList类继承自Node类,用于表示任务列表,包含任务列表的相关属性和操作方法
|
|
|
|
|
public class TaskList extends Node {
|
|
|
|
|
// 日志标签,用于标识该类的日志输出
|
|
|
|
|
private static final String TAG = TaskList.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
// 任务列表的索引
|
|
|
|
|
private int mIndex;
|
|
|
|
|
|
|
|
|
|
// 存储任务列表中的子任务
|
|
|
|
|
private ArrayList<Task> mChildren;
|
|
|
|
|
|
|
|
|
|
// 构造函数,初始化任务列表的属性
|
|
|
|
|
public TaskList() {
|
|
|
|
|
super();
|
|
|
|
|
mChildren = new ArrayList<Task>();
|
|
|
|
|
mChildren = new ArrayList<>();
|
|
|
|
|
mIndex = 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取创建任务列表的操作JSON对象
|
|
|
|
|
public JSONObject getCreateAction(int actionId) {
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// action_type
|
|
|
|
|
// 设置操作类型为创建
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);
|
|
|
|
|
|
|
|
|
|
// action_id
|
|
|
|
|
// 设置操作ID
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
|
|
|
|
|
|
|
|
|
|
// index
|
|
|
|
|
// 设置任务列表的索引
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex);
|
|
|
|
|
|
|
|
|
|
// 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_GROUP);
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
throw new ActionFailureException("fail to generate tasklist-create jsonobject");
|
|
|
|
|
throw new ActionFailureException("fail to generate tasklist - create jsonobject");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取更新任务列表的操作JSON对象
|
|
|
|
|
public JSONObject getUpdateAction(int actionId) {
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// action_type
|
|
|
|
|
// 设置操作类型为更新
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);
|
|
|
|
|
|
|
|
|
|
// action_id
|
|
|
|
|
// 设置操作ID
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
|
|
|
|
|
|
|
|
|
|
// id
|
|
|
|
|
// 设置任务列表的ID
|
|
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());
|
|
|
|
|
|
|
|
|
|
// entity_delta
|
|
|
|
|
// 设置任务列表实体的变化
|
|
|
|
|
JSONObject entity = new JSONObject();
|
|
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
|
|
|
|
|
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 tasklist-update jsonobject");
|
|
|
|
|
throw new ActionFailureException("fail to generate tasklist - update jsonobject");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据从远程获取的JSON数据设置任务列表内容
|
|
|
|
|
public void setContentByRemoteJSON(JSONObject js) {
|
|
|
|
|
if (js != null) {
|
|
|
|
|
if (js!= null) {
|
|
|
|
|
try {
|
|
|
|
|
// id
|
|
|
|
|
// 设置任务列表的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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
@ -129,8 +108,9 @@ public class TaskList extends Node {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据本地的JSON数据设置任务列表内容
|
|
|
|
|
public void setContentByLocalJSON(JSONObject js) {
|
|
|
|
|
if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) {
|
|
|
|
|
if (js == null ||!js.has(GTaskStringUtils.META_HEAD_NOTE)) {
|
|
|
|
|
Log.w(TAG, "setContentByLocalJSON: nothing is avaiable");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -144,8 +124,7 @@ public class TaskList extends Node {
|
|
|
|
|
if (folder.getLong(NoteColumns.ID) == Notes.ID_ROOT_FOLDER)
|
|
|
|
|
setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT);
|
|
|
|
|
else if (folder.getLong(NoteColumns.ID) == Notes.ID_CALL_RECORD_FOLDER)
|
|
|
|
|
setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
|
+ GTaskStringUtils.FOLDER_CALL_NOTE);
|
|
|
|
|
setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE);
|
|
|
|
|
else
|
|
|
|
|
Log.e(TAG, "invalid system folder");
|
|
|
|
|
} else {
|
|
|
|
|
@ -157,6 +136,7 @@ public class TaskList extends Node {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从任务列表内容生成本地JSON对象
|
|
|
|
|
public JSONObject getLocalJSONFromContent() {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
@ -183,28 +163,29 @@ public class TaskList extends Node {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据数据库游标获取任务列表的同步操作类型
|
|
|
|
|
public int getSyncAction(Cursor c) {
|
|
|
|
|
try {
|
|
|
|
|
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
|
|
|
|
|
// 验证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 {
|
|
|
|
|
// for folder conflicts, just apply local modification
|
|
|
|
|
// 对于文件夹冲突,直接应用本地修改
|
|
|
|
|
return SYNC_ACTION_UPDATE_REMOTE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -216,24 +197,26 @@ public class TaskList extends Node {
|
|
|
|
|
return SYNC_ACTION_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取任务列表中的子任务数量
|
|
|
|
|
public int getChildTaskCount() {
|
|
|
|
|
return mChildren.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 向任务列表中添加子任务
|
|
|
|
|
public boolean addChildTask(Task task) {
|
|
|
|
|
boolean ret = false;
|
|
|
|
|
if (task != null && !mChildren.contains(task)) {
|
|
|
|
|
if (task!= null &&!mChildren.contains(task)) {
|
|
|
|
|
ret = mChildren.add(task);
|
|
|
|
|
if (ret) {
|
|
|
|
|
// need to set prior sibling and parent
|
|
|
|
|
task.setPriorSibling(mChildren.isEmpty() ? null : mChildren
|
|
|
|
|
.get(mChildren.size() - 1));
|
|
|
|
|
// 需要设置前一个兄弟任务和父任务列表
|
|
|
|
|
task.setPriorSibling(mChildren.isEmpty()? null : mChildren.get(mChildren.size() - 1));
|
|
|
|
|
task.setParent(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在指定索引位置向任务列表中添加子任务
|
|
|
|
|
public boolean addChildTask(Task task, int index) {
|
|
|
|
|
if (index < 0 || index > mChildren.size()) {
|
|
|
|
|
Log.e(TAG, "add child task: invalid index");
|
|
|
|
|
@ -241,48 +224,49 @@ public class TaskList extends Node {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int pos = mChildren.indexOf(task);
|
|
|
|
|
if (task != null && pos == -1) {
|
|
|
|
|
if (task!= null && pos == -1) {
|
|
|
|
|
mChildren.add(index, task);
|
|
|
|
|
|
|
|
|
|
// update the task list
|
|
|
|
|
// 更新任务列表
|
|
|
|
|
Task preTask = null;
|
|
|
|
|
Task afterTask = null;
|
|
|
|
|
if (index != 0)
|
|
|
|
|
if (index!= 0)
|
|
|
|
|
preTask = mChildren.get(index - 1);
|
|
|
|
|
if (index != mChildren.size() - 1)
|
|
|
|
|
if (index!= mChildren.size() - 1)
|
|
|
|
|
afterTask = mChildren.get(index + 1);
|
|
|
|
|
|
|
|
|
|
task.setPriorSibling(preTask);
|
|
|
|
|
if (afterTask != null)
|
|
|
|
|
if (afterTask!= null)
|
|
|
|
|
afterTask.setPriorSibling(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从任务列表中移除子任务
|
|
|
|
|
public boolean removeChildTask(Task task) {
|
|
|
|
|
boolean ret = false;
|
|
|
|
|
int index = mChildren.indexOf(task);
|
|
|
|
|
if (index != -1) {
|
|
|
|
|
if (index!= -1) {
|
|
|
|
|
ret = mChildren.remove(task);
|
|
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
|
// reset prior sibling and parent
|
|
|
|
|
// 重置前一个兄弟任务和父任务列表
|
|
|
|
|
task.setPriorSibling(null);
|
|
|
|
|
task.setParent(null);
|
|
|
|
|
|
|
|
|
|
// update the task list
|
|
|
|
|
if (index != mChildren.size()) {
|
|
|
|
|
// 更新任务列表
|
|
|
|
|
if (index!= mChildren.size()) {
|
|
|
|
|
mChildren.get(index).setPriorSibling(
|
|
|
|
|
index == 0 ? null : mChildren.get(index - 1));
|
|
|
|
|
index == 0? null : mChildren.get(index - 1));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将子任务移动到指定索引位置
|
|
|
|
|
public boolean moveChildTask(Task task, int index) {
|
|
|
|
|
|
|
|
|
|
if (index < 0 || index >= mChildren.size()) {
|
|
|
|
|
Log.e(TAG, "move child task: invalid index");
|
|
|
|
|
return false;
|
|
|
|
|
@ -299,6 +283,7 @@ public class TaskList extends Node {
|
|
|
|
|
return (removeChildTask(task) && addChildTask(task, index));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据任务的gid查找子任务
|
|
|
|
|
public Task findChildTaskByGid(String gid) {
|
|
|
|
|
for (int i = 0; i < mChildren.size(); i++) {
|
|
|
|
|
Task t = mChildren.get(i);
|
|
|
|
|
@ -309,10 +294,12 @@ public class TaskList extends Node {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取子任务在任务列表中的索引
|
|
|
|
|
public int getChildTaskIndex(Task task) {
|
|
|
|
|
return mChildren.indexOf(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据索引获取子任务
|
|
|
|
|
public Task getChildTaskByIndex(int index) {
|
|
|
|
|
if (index < 0 || index >= mChildren.size()) {
|
|
|
|
|
Log.e(TAG, "getTaskByIndex: invalid index");
|
|
|
|
|
@ -321,6 +308,7 @@ public class TaskList extends Node {
|
|
|
|
|
return mChildren.get(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据gid获取子任务(方法名有误,应为getChildTaskByGid)
|
|
|
|
|
public Task getChilTaskByGid(String gid) {
|
|
|
|
|
for (Task task : mChildren) {
|
|
|
|
|
if (task.getGid().equals(gid))
|
|
|
|
|
@ -329,15 +317,18 @@ public class TaskList extends Node {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取任务列表中的所有子任务
|
|
|
|
|
public ArrayList<Task> getChildTaskList() {
|
|
|
|
|
return this.mChildren;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置任务列表的索引
|
|
|
|
|
public void setIndex(int index) {
|
|
|
|
|
this.mIndex = index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取任务列表的索引
|
|
|
|
|
public int getIndex() {
|
|
|
|
|
return this.mIndex;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|