|
|
|
@ -13,52 +13,50 @@
|
|
|
|
|
* 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.util.Log;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException;
|
|
|
|
|
import net.micode.notes.tool.GTaskStringUtils;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
|
// 任务列表类,继承自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>();
|
|
|
|
|
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
|
|
|
|
|
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());
|
|
|
|
@ -66,47 +64,45 @@ public class TaskList extends Node {
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
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());
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据远程JSON对象设置任务列表的内容
|
|
|
|
|
|
|
|
|
|
public void setContentByRemoteJSON(JSONObject js) {
|
|
|
|
|
if (js != null) {
|
|
|
|
|
try {
|
|
|
|
@ -114,17 +110,17 @@ public class TaskList extends Node {
|
|
|
|
|
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();
|
|
|
|
@ -132,16 +128,15 @@ public class TaskList extends Node {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据本地JSON对象设置任务列表的内容
|
|
|
|
|
|
|
|
|
|
public void setContentByLocalJSON(JSONObject js) {
|
|
|
|
|
if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) {
|
|
|
|
|
Log.w(TAG, "setContentByLocalJSON: nothing is avaiable");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
JSONObject folder = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) {
|
|
|
|
|
String name = folder.getString(NoteColumns.SNIPPET);
|
|
|
|
|
setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name);
|
|
|
|
@ -161,13 +156,12 @@ public class TaskList extends Node {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据任务列表的内容生成本地JSON对象
|
|
|
|
|
|
|
|
|
|
public JSONObject getLocalJSONFromContent() {
|
|
|
|
|
try {
|
|
|
|
|
JSONObject js = new JSONObject();
|
|
|
|
|
JSONObject folder = new JSONObject();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String folderName = getName();
|
|
|
|
|
if (getName().startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX))
|
|
|
|
|
folderName = folderName.substring(GTaskStringUtils.MIUI_FOLDER_PREFFIX.length(),
|
|
|
|
@ -178,9 +172,9 @@ public class TaskList extends Node {
|
|
|
|
|
folder.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
|
|
|
|
|
else
|
|
|
|
|
folder.put(NoteColumns.TYPE, Notes.TYPE_FOLDER);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, folder);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return js;
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
@ -188,8 +182,7 @@ public class TaskList extends Node {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据游标中的数据返回同步操作类型
|
|
|
|
|
|
|
|
|
|
public int getSyncAction(Cursor c) {
|
|
|
|
|
try {
|
|
|
|
|
if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) {
|
|
|
|
@ -219,16 +212,14 @@ public class TaskList extends Node {
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return SYNC_ACTION_ERROR;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取子任务的数量
|
|
|
|
|
|
|
|
|
|
public int getChildTaskCount() {
|
|
|
|
|
return mChildren.size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加子任务到任务列表
|
|
|
|
|
|
|
|
|
|
public boolean addChildTask(Task task) {
|
|
|
|
|
boolean ret = false;
|
|
|
|
|
if (task != null && !mChildren.contains(task)) {
|
|
|
|
@ -242,18 +233,17 @@ public class TaskList extends Node {
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在指定位置添加子任务到任务列表
|
|
|
|
|
|
|
|
|
|
public boolean addChildTask(Task task, int index) {
|
|
|
|
|
if (index < 0 || index > mChildren.size()) {
|
|
|
|
|
Log.e(TAG, "add child task: invalid index");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int pos = mChildren.indexOf(task);
|
|
|
|
|
if (task != null && pos == -1) {
|
|
|
|
|
mChildren.add(index, task);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// update the task list
|
|
|
|
|
Task preTask = null;
|
|
|
|
|
Task afterTask = null;
|
|
|
|
@ -261,27 +251,26 @@ public class TaskList extends Node {
|
|
|
|
|
preTask = mChildren.get(index - 1);
|
|
|
|
|
if (index != mChildren.size() - 1)
|
|
|
|
|
afterTask = mChildren.get(index + 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
task.setPriorSibling(preTask);
|
|
|
|
|
if (afterTask != null)
|
|
|
|
|
afterTask.setPriorSibling(task);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 从任务列表中移除指定的子任务
|
|
|
|
|
|
|
|
|
|
public boolean removeChildTask(Task task) {
|
|
|
|
|
boolean ret = false;
|
|
|
|
|
int index = mChildren.indexOf(task);
|
|
|
|
|
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()) {
|
|
|
|
|
mChildren.get(index).setPriorSibling(
|
|
|
|
@ -291,27 +280,25 @@ public class TaskList extends Node {
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int pos = mChildren.indexOf(task);
|
|
|
|
|
if (pos == -1) {
|
|
|
|
|
Log.e(TAG, "move child task: the task should in the list");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (pos == index)
|
|
|
|
|
return true;
|
|
|
|
|
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);
|
|
|
|
@ -321,13 +308,11 @@ 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");
|
|
|
|
@ -335,8 +320,7 @@ public class TaskList extends Node {
|
|
|
|
|
}
|
|
|
|
|
return mChildren.get(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据gid获取子任务
|
|
|
|
|
|
|
|
|
|
public Task getChilTaskByGid(String gid) {
|
|
|
|
|
for (Task task : mChildren) {
|
|
|
|
|
if (task.getGid().equals(gid))
|
|
|
|
@ -344,19 +328,16 @@ 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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|