|
|
/*
|
|
|
* 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;//包 net.micode.notes.gtask.data
|
|
|
|
|
|
import android.database.Cursor;//导入安卓数据库游标
|
|
|
import android.util.Log;//导入安卓日志记录
|
|
|
|
|
|
import net.micode.notes.data.Notes;//导入 net.micode.notes 数据的 Notes
|
|
|
import net.micode.notes.data.Notes.NoteColumns;//导入 net.micode.notes 数据的 Notes 的 NoteColumns
|
|
|
import net.micode.notes.gtask.exception.ActionFailureException;//导入 net.micode.notes.gtask 异常的操作失败异常
|
|
|
import net.micode.notes.tool.GTaskStringUtils;//导入 net.micode.notes 工具的 GTaskStringUtils
|
|
|
|
|
|
import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
|
public class TaskList extends Node {//公共类 TaskList 继承自 Node 类。
|
|
|
private static final String TAG = TaskList.class.getSimpleName();//私有静态最终字符串变量 TAG,赋值为 TaskList 类的简单名称。
|
|
|
|
|
|
private int mIndex;//私有整型变量 mIndex。
|
|
|
|
|
|
private ArrayList<Task> mChildren;//私有 ArrayList 类型的变量 mChildren,泛型参数为 Task。
|
|
|
|
|
|
public TaskList() {//公共的 TaskList 构造函数。
|
|
|
super();//调用父类的构造函数。
|
|
|
mChildren = new ArrayList<Task>();//将 mChildren 初始化为一个新的 ArrayList,用于存储 Task 对象
|
|
|
mIndex = 1;//将 mIndex 初始化为 1。
|
|
|
}
|
|
|
|
|
|
public JSONObject getCreateAction(int actionId) {//公共方法 getCreateAction,接收一个整型参数 actionId,返回一个 JSONObject 对象。
|
|
|
JSONObject js = new JSONObject();//创建一个新的 JSONObject 对象 js。
|
|
|
|
|
|
try {//尝试执行以下代码块。
|
|
|
// action_type
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);//向 JSONObject 对象 js 中添加键值对,键为‘action_type’,值为 GTaskStringUtils 类中常量 GTASK_JSON_ACTION_TYPE_CREATE。
|
|
|
|
|
|
// action_id
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);//向 JSONObject 对象 js 中添加键值对,键为‘action_id’,值为传入的参数 actionId。
|
|
|
|
|
|
// index
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex);//向 JSONObject 对象 js 中添加键值对,键为‘index’,值为 mIndex 变量的值。
|
|
|
|
|
|
// entity_delta
|
|
|
JSONObject entity = new JSONObject();//创建一个新的 JSONObject 对象 entity。
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());//向 entity 对象中添加键值对,键为‘name’,值为调用 getName 方法返回的值。
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null");//向 entity 对象中添加键值对,键为‘creator_id’,值为字符串‘null’。
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE,
|
|
|
GTaskStringUtils.GTASK_JSON_TYPE_GROUP);//向 entity 对象中添加键值对,键为‘entity_type’,值为 GTaskStringUtils 类中常量 GTASK_JSON_TYPE_GROUP。
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);//向 js 对象中添加键值对,键为‘entity_delta’,值为 entity 对象。
|
|
|
|
|
|
} catch (JSONException e) {//捕获 JSONException 异常。
|
|
|
Log.e(TAG, e.toString());//使用日志记录错误信息,TAG 为类名的简单名称,输出异常的字符串表示。
|
|
|
e.printStackTrace();//打印异常堆栈跟踪。
|
|
|
throw new ActionFailureException("fail to generate tasklist-create jsonobject");//抛出一个 ActionFailureException 异常,异常信息为‘fail to generate tasklist-create jsonobject’。
|
|
|
}
|
|
|
|
|
|
return js;//返回 JSONObject 对象 js。
|
|
|
}
|
|
|
|
|
|
public JSONObject getUpdateAction(int actionId) {//公共的 JSONObject 方法 getUpdateAction,接收一个动作 id。
|
|
|
JSONObject js = new JSONObject();//创建一个新的 JSONObject 对象 js
|
|
|
|
|
|
try {//尝试执行以下代码块
|
|
|
// action_type
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
|
|
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);//在 js 对象中添加键值对,action_type 为指定的值。
|
|
|
|
|
|
// action_id
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);//在 js 对象中添加键值对,action_id 为传入的动作 id。
|
|
|
|
|
|
// id
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());//在 js 对象中添加键值对,id 为获取的 gid。
|
|
|
|
|
|
// entity_delta
|
|
|
JSONObject entity = new JSONObject();//创建一个新的 JSONObject 对象 entity。
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());//在 entity 对象中添加键值对,name 为获取的名称。
|
|
|
entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted());//在 entity 对象中添加键值对,deleted 为获取的已删除状态。
|
|
|
js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);//在 js 对象中添加键值对,entity_delta 为 entity 对象。
|
|
|
|
|
|
} catch (JSONException e) {//捕获 JSONException 异常。
|
|
|
Log.e(TAG, e.toString());//记录错误日志,输出异常信息的字符串表示。
|
|
|
e.printStackTrace();//打印异常的堆栈跟踪信息。
|
|
|
throw new ActionFailureException("fail to generate tasklist-update jsonobject");//抛出一个 ActionFailureException 异常,提示生成任务列表更新 JSONObject 对象失败。
|
|
|
}
|
|
|
|
|
|
return js;//返回 js 对象。
|
|
|
}
|
|
|
|
|
|
public void setContentByRemoteJSON(JSONObject js) {//公共的方法 setContentByRemoteJSON,接收一个 JSONObject 对象 js。
|
|
|
if (js != null) {//如果 js 对象不为空,则执行以下代码块。
|
|
|
try {//尝试执行以下代码块。
|
|
|
// id
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_ID)) {//如果 js 对象包含键 GTaskStringUtils.GTASK_JSON_ID,则执行以下代码块。
|
|
|
setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID));//设置 gid 为 js 对象中键 GTaskStringUtils.GTASK_JSON_ID 的对应的值。
|
|
|
}
|
|
|
|
|
|
// last_modified
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) {//如果 js 对象包含键 GTaskStringUtils.GTASK_JSON_LAST_MODIFIED,则执行以下代码块。
|
|
|
setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED));//设置最后修改时间为 js 对象中键 GTaskStringUtils.GTASK_JSON_LAST_MODIFIED 的对应的值。
|
|
|
}
|
|
|
|
|
|
// name
|
|
|
if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) {//如果 js 对象包含键 GTaskStringUtils.GTASK_JSON_NAME,则执行以下代码块。
|
|
|
setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME));//设置名称为 js 对象中键 GTaskStringUtils.GTASK_JSON_NAME 的对应的值。
|
|
|
}
|
|
|
|
|
|
} catch (JSONException e) {//捕获 JSONException 异常。
|
|
|
Log.e(TAG, e.toString());//记录错误日志,输出异常信息的字符串表示。
|
|
|
e.printStackTrace();//打印异常的堆栈跟踪信息。
|
|
|
throw new ActionFailureException("fail to get tasklist content from jsonobject");//抛出一个 ActionFailureException 异常,提示从 JSONObject 对象获取任务列表内容失败。
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void setContentByLocalJSON(JSONObject js) {//公共的方法 setContentByLocalJSON,接收一个 JSONObject 对象 js。
|
|
|
if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) {//如果 js 对象为空或者不包含键 GTaskStringUtils.META_HEAD_NOTE,则执行以下代码块。
|
|
|
Log.w(TAG, "setContentByLocalJSON: nothing is avaiable");//记录警告日志,提示在 setContentByLocalJSON 方法中没有可用内容。
|
|
|
}
|
|
|
|
|
|
try {//尝试执行以下代码块。
|
|
|
JSONObject folder = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);//从 js 对象中获取键 GTaskStringUtils.META_HEAD_NOTE 对应的 JSONObject 对象 folder。
|
|
|
|
|
|
if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) {//如果 folder 对象中键 NoteColumns.TYPE 的对应的值为 Notes.TYPE_FOLDER,则执行以下代码块。
|
|
|
String name = folder.getString(NoteColumns.SNIPPET);//获取名称为 folder 对象中键 NoteColumns.SNIPPET 对应的值。
|
|
|
setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name);//设置名称为指定的前缀加上获取的名称。
|
|
|
} else if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {//否则如果 folder 对象中键 NoteColumns.TYPE 的对应的值为 Notes.TYPE_SYSTEM,则执行以下代码块。
|
|
|
if (folder.getLong(NoteColumns.ID) == Notes.ID_ROOT_FOLDER)//如果 folder 对象中键 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)//否则如果 folder 对象中键 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) {//捕获 JSONException 异常。
|
|
|
Log.e(TAG, e.toString());//记录错误日志,输出异常信息的字符串表示。
|
|
|
e.printStackTrace();//打印异常的堆栈跟踪信息。
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public JSONObject getLocalJSONFromContent() {//公共的 JSONObject 方法,用于从内容中获取本地 JSON 对象。
|
|
|
try {
|
|
|
JSONObject js = new JSONObject();//创建一个新的 JSONObject 对象 js。
|
|
|
JSONObject folder = new JSONObject();//创建一个新的 JSONObject 对象 folder。
|
|
|
|
|
|
|
|
|
String folderName = getName();//获取名称并赋值给变量 folderName。
|
|
|
if (getName().startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX))//如果名称以指定前缀开头。
|
|
|
folderName = folderName.substring(GTaskStringUtils.MIUI_FOLDER_PREFFIX.length(),
|
|
|
folderName.length());//截取名称,去除前缀部分。
|
|
|
folder.put(NoteColumns.SNIPPET, folderName);//在 folder 对象中添加键值对。
|
|
|
if (folderName.equals(GTaskStringUtils.FOLDER_DEFAULT)
|
|
|
|| folderName.equals(GTaskStringUtils.FOLDER_CALL_NOTE))//如果名称等于指定的默认文件夹名或电话笔记文件夹名。
|
|
|
folder.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);//在 folder 对象中设置类型为系统类型。
|
|
|
else
|
|
|
folder.put(NoteColumns.TYPE, Notes.TYPE_FOLDER);//在 folder 对象中设置类型为文件夹类型。
|
|
|
|
|
|
js.put(GTaskStringUtils.META_HEAD_NOTE, folder);//在 js 对象中添加键值对。
|
|
|
|
|
|
return js;
|
|
|
} catch (JSONException e) {//捕获 JSONException 异常。
|
|
|
Log.e(TAG, e.toString());//记录错误日志。
|
|
|
e.printStackTrace();//打印异常堆栈信息。
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public int getSyncAction(Cursor c) {//公共的获取同步操作方法。
|
|
|
try {//尝试执行以下代码块。
|
|
|
if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) {//如果游标中本地修改列的值为 0。
|
|
|
// there is no local update
|
|
|
if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {//如果同步 ID 列的值等于最后修改的值。
|
|
|
// 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())) {//如果游标中任务 ID 列的值不等于当前任务的 ID。
|
|
|
Log.e(TAG, "gtask id doesn't match");//记录错误日志(任务 ID 不匹配)。
|
|
|
return SYNC_ACTION_ERROR;//返回错误操作。
|
|
|
}
|
|
|
if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {//如果同步 ID 列的值等于最后修改的值。
|
|
|
// 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) {//如果任务不为空且索引为 - 1(即子任务列表中不包含该任务)。
|
|
|
mChildren.add(index, task);//在指定索引处添加任务。
|
|
|
|
|
|
// update the task list
|
|
|
Task preTask = null;//初始化前一个任务为 null。
|
|
|
Task afterTask = null;//初始化后一个任务为 null
|
|
|
if (index != 0)
|
|
|
preTask = mChildren.get(index - 1);//如果索引不为 0,获取前一个任务。
|
|
|
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) {//如果索引不为 - 1(即子任务列表中包含该任务)。
|
|
|
ret = mChildren.remove(task);//从子任务列表中移除任务并更新返回值。
|
|
|
|
|
|
if (ret) {//如果移除成功。
|
|
|
// reset prior sibling and parent
|
|
|
task.setPriorSibling(null);//设置任务的前一个兄弟任务为 null。
|
|
|
task.setParent(null);//设置任务的父任务为 null。
|
|
|
|
|
|
// update the task list
|
|
|
if (index != mChildren.size()) {//如果索引不是子任务列表最后一个索引。
|
|
|
mChildren.get(index).setPriorSibling(//设置当前索引处任务的前一个兄弟任务。
|
|
|
index == 0 ? null : mChildren.get(index - 1));//如果索引为 0,则前一个兄弟任务为 null,否则为前一个索引处的任务。
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
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) {//如果索引为 - 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)) {//如果子任务的任务 ID 等于要查找的任务 ID。
|
|
|
return t;
|
|
|
}
|
|
|
}
|
|
|
return null;//如果遍历完子任务列表都没有找到,则返回 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))//如果子任务的任务 ID 等于要查找的任务 ID。
|
|
|
return task;//返回该子任务。
|
|
|
}
|
|
|
return null;//如果遍历完子任务列表都没有找到,则返回 null。
|
|
|
}
|
|
|
|
|
|
public ArrayList<Task> getChildTaskList() {//公共方法,用于获取子任务列表。
|
|
|
return this.mChildren;//返回成员变量mChildren,即子任务列表。
|
|
|
}
|
|
|
|
|
|
public void setIndex(int index) {//公共方法,用于设置索引
|
|
|
this.mIndex = index;//将传入的索引值赋给成员变量mIndex。
|
|
|
}
|
|
|
|
|
|
public int getIndex() {//公共方法,用于获取索引。
|
|
|
return this.mIndex;//返回成员变量mIndex的值,即当前的索引。
|
|
|
}
|
|
|
}
|