|
|
/*
|
|
|
* 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.
|
|
|
*/
|
|
|
|
|
|
//这段代码是一个Java类,它定义了一个TaskList类这段代码是一个Java类,它定义了一个TaskList类
|
|
|
|
|
|
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;
|
|
|
//这些是所需的导入语句,用于引入需要的类和库。
|
|
|
|
|
|
public class TaskList extends Node {
|
|
|
//这行代码定义了一个名为TaskList的类,它扩展自Node类,表明TaskList是一个节点,可能是树形结构中的一部分。
|
|
|
private static final String TAG = TaskList.class.getSimpleName();
|
|
|
//这行代码声明了一个私有静态常量TAG,用于在日志中标识当前类。
|
|
|
private int mIndex;
|
|
|
|
|
|
private ArrayList<Task> mChildren;
|
|
|
//这两行声明了私有变量mIndex和mChildren,分别表示任务列表的索引和子任务列表。mChildren是一个ArrayList,其中的元素类型是Task。
|
|
|
public TaskList() {
|
|
|
super();
|
|
|
mChildren = new ArrayList<Task>();
|
|
|
mIndex = 1;
|
|
|
}
|
|
|
//这是类的构造函数,初始化了mChildren为一个新的空ArrayList,并将mIndex设置为1。
|
|
|
|
|
|
public JSONObject getCreateAction(int actionId) {
|
|
|
//这是一个公共方法,用于生成创建操作的JSON对象。它接受一个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());
|
|
|
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");
|
|
|
}
|
|
|
|
|
|
return js;
|
|
|
}
|
|
|
|
|
|
public JSONObject getUpdateAction(int actionId) {
|
|
|
//这是另一个公共方法,用于生成更新操作的JSON对象。同样接受一个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;
|
|
|
}
|
|
|
|
|
|
public void setContentByRemoteJSON(JSONObject js) {
|
|
|
//这是一个公共方法,用于从远程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));
|
|
|
}
|
|
|
|
|
|
} catch (JSONException e) {
|
|
|
Log.e(TAG, e.toString());
|
|
|
e.printStackTrace();
|
|
|
throw new ActionFailureException("fail to get tasklist content from jsonobject");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void setContentByLocalJSON(JSONObject js) {
|
|
|
//这是另一个公共方法,用于从本地JSON对象设置任务列表的内容。
|
|
|
|
|
|
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);
|
|
|
} else if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
|
|
|
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);
|
|
|
else
|
|
|
Log.e(TAG, "invalid system folder");
|
|
|
} else {
|
|
|
Log.e(TAG, "error type");
|
|
|
}
|
|
|
} catch (JSONException e) {
|
|
|
Log.e(TAG, e.toString());
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public JSONObject getLocalJSONFromContent() {
|
|
|
//这是一个公共方法,用于从任务列表内容生成本地JSON对象。
|
|
|
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(),
|
|
|
folderName.length());
|
|
|
folder.put(NoteColumns.SNIPPET, folderName);
|
|
|
if (folderName.equals(GTaskStringUtils.FOLDER_DEFAULT)
|
|
|
|| folderName.equals(GTaskStringUtils.FOLDER_CALL_NOTE))
|
|
|
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());
|
|
|
e.printStackTrace();
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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
|
|
|
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;
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
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)) {
|
|
|
ret = mChildren.add(task);
|
|
|
if (ret) {
|
|
|
// need to set prior sibling and parent
|
|
|
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");
|
|
|
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;
|
|
|
if (index != 0)
|
|
|
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(
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
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));
|
|
|
}
|
|
|
|
|
|
public Task findChildTaskByGid(String gid) {
|
|
|
//这是一个公共方法,用于根据全局ID查找子任务。
|
|
|
|
|
|
for (int i = 0; i < mChildren.size(); i++) {
|
|
|
Task t = mChildren.get(i);
|
|
|
if (t.getGid().equals(gid)) {
|
|
|
return t;
|
|
|
}
|
|
|
}
|
|
|
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");
|
|
|
return null;
|
|
|
}
|
|
|
return mChildren.get(index);
|
|
|
}
|
|
|
|
|
|
public Task getChilTaskByGid(String gid) {
|
|
|
//这是一个公共方法,用于根据全局ID获取子任务。
|
|
|
|
|
|
for (Task task : mChildren) {
|
|
|
if (task.getGid().equals(gid))
|
|
|
return task;
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public ArrayList<Task> getChildTaskList() {
|
|
|
//这是一个公共方法,用于获取子任务列表。
|
|
|
|
|
|
return this.mChildren;
|
|
|
}
|
|
|
|
|
|
public void setIndex(int index) {//这是一个公共方法,用于设置索引。
|
|
|
this.mIndex = index;
|
|
|
}
|
|
|
|
|
|
public int getIndex() { //这是一个公共方法,用于获取索引
|
|
|
return this.mIndex;
|
|
|
}
|
|
|
//这段代码定义了一个任务列表类,提供了一系列方法用于管理任务列表及其子任务。
|
|
|
}
|