You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
java/zz_code/gtask/data/TaskList.java

382 lines
15 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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;
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;
// TaskList类继承自Node类
public class TaskList extends Node {
// 定义一个静态的字符串TAG用于调试
private static final String TAG = TaskList.class.getSimpleName();
// 定义一个整型变量mIndex用于存储索引
private int mIndex;
// 定义一个Task类型的ArrayList对象mChildren用于存储Task对象
private ArrayList<Task> mChildren;
// 定义一个TaskList构造函数无参数
public TaskList() {
super();
mChildren = new ArrayList<Task>();
mIndex = 1;
}
// 定义一个TaskList构造函数参数为String类型的name
public JSONObject getCreateAction(int actionId) {
JSONObject js = new JSONObject();
// 尝试
try {
// action_type
// 将键值对action_type和GTASK_JSON_ACTION_TYPE_CREATE放入js中
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);
// action_id
// 将键值对action_id和actionId放入js中
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
// index
// 将键值对index和mIndex放入js中
js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex);
// entity_delta
// 创建一个JSONObject对象entity
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);
// 捕获JSONException异常
} catch (JSONException e) {
// 打印异常信息
Log.e(TAG, e.toString());
// 打印异常堆栈信息
e.printStackTrace();
// 抛出ActionFailureException异常
throw new ActionFailureException("fail to generate tasklist-create jsonobject");
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("fail to generate tasklist-create jsonobject");
}
return js;
}
// 定义一个JSONObject类型的方法getUpdateAction参数为整型的actionId
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);
// 捕获JSONException异常
} catch (JSONException e) {
// 打印异常信息
Log.e(TAG, e.toString());
// 打印异常堆栈信息
e.printStackTrace();
// 抛出ActionFailureException异常
throw new ActionFailureException("fail to generate tasklist-update jsonobject");
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("fail to generate tasklist-update jsonobject");
}
// 返回js
return js;
}
// 定义一个JSONObject类型的方法getDeleteAction参数为整型的actionId
public void setContentByRemoteJSON(JSONObject js) {
if (js != null) {
try {
// id
if (js.has(GTaskStringUtils.GTASK_JSON_ID)) {
setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID));
}
// deleted
// 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));
}
// 捕获JSONException异常
} catch (JSONException e) {
// 打印异常信息
Log.e(TAG, e.toString());
// 打印异常堆栈信息
e.printStackTrace();
// 抛出ActionFailureException异常
throw new ActionFailureException("fail to get tasklist content from jsonobject");
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
throw new ActionFailureException("fail to get tasklist content from jsonobject");
}
}
}
// 定义一个JSONObject类型的方法getLocalJSONFromContent
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);
// name of the folder is the snippet of the 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");// invalid system folder
} else {// invalid type
Log.e(TAG, "error type");
}
// 捕获JSONException异常
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
}
// 定义一个JSONObject类型的方法getLocalJSONFromContent
public JSONObject getLocalJSONFromContent() {
// 尝试
try {
JSONObject js = new JSONObject();
JSONObject folder = new JSONObject();
// 文件夹的名称是注释的代码段
String folderName = getName();
// 将键值对id和getId放入folder中
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;
// 捕获JSONException异常
} catch (JSONException e) {
Log.e(TAG, e.toString());
e.printStackTrace();
return null;
}
}
// 定义一个整型的方法getSyncAction参数为Cursor类型的c
public int getSyncAction(Cursor c) {
try {
// 如果Cursor对象c为空
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 {
// 验证 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()) {
// 仅限本地修改
return SYNC_ACTION_UPDATE_REMOTE;
} else {
// 对于文件夹冲突,只需应用本地修改
return SYNC_ACTION_UPDATE_REMOTE;
}
}
} catch (Exception e) {
Log.e(TAG, e.toString());
e.printStackTrace();
}
return SYNC_ACTION_ERROR;
}
// 定义一个整型的方法getSyncAction参数为TaskList类型的t
public int getChildTaskCount() {
return mChildren.size();
}
// 定义一个布尔型的方法addChildTask参数为Task类型的task
public boolean addChildTask(Task task) {
boolean ret = false;
// 如果task不为空且mChildren不包含task
if (task != null && !mChildren.contains(task)) {
ret = mChildren.add(task);
if (ret) {
// 需要设置先前的兄弟姐妹和父级
task.setPriorSibling(mChildren.isEmpty() ? null : mChildren
.get(mChildren.size() - 1));
task.setParent(this);
}
}
return ret;
}
// 定义一个布尔型的方法addChildTask参数为Task类型的task和整型的index
public boolean addChildTask(Task task, int index) {
if (index < 0 || index > mChildren.size()) {
Log.e(TAG, "add child task: invalid index");
return false;
}
// 如果task不为空且mChildren不包含task
int pos = mChildren.indexOf(task);
if (task != null && pos == -1) {
mChildren.add(index, task);
// 更新任务列表
Task preTask = null;
Task afterTask = null;
// 如果index不等于0
if (index != 0)
preTask = mChildren.get(index - 1);
// 如果index不等于mChildren.size() - 1
if (index != mChildren.size() - 1)
afterTask = mChildren.get(index + 1);
// 需要设置先前的兄弟姐妹和父级
task.setPriorSibling(preTask);
if (afterTask != null)
afterTask.setPriorSibling(task);
}
return true;
}
// 定义一个布尔型的方法removeChildTask参数为Task类型的task
public boolean removeChildTask(Task task) {
boolean ret = false;
int index = mChildren.indexOf(task);
if (index != -1) {
ret = mChildren.remove(task);
// 如果ret为真
if (ret) {
// 重置先前的兄弟姐妹和父级
task.setPriorSibling(null);
task.setParent(null);
// 更新任务列表
if (index != mChildren.size()) {
mChildren.get(index).setPriorSibling(
index == 0 ? null : mChildren.get(index - 1));
}
}
}
return ret;
}
// 定义一个布尔型的方法moveChildTask参数为Task类型的task和整型的index
public boolean moveChildTask(Task task, int index) {
// 如果index小于0或者index大于等于mChildren.size()
if (index < 0 || index >= mChildren.size()) {
Log.e(TAG, "move child task: invalid index");
return false;
}
// 定义一个整型变量pos用于存储mChildren中task的位置
int pos = mChildren.indexOf(task);
if (pos == -1) {
Log.e(TAG, "move child task: the task should in the list");
return false;
}
// 如果pos等于index
if (pos == index)
return true;
return (removeChildTask(task) && addChildTask(task, index));
}
// 定义一个Task类型的方法findChildTaskByGid参数为String类型的gid
public Task findChildTaskByGid(String gid) {
for (int i = 0; i < mChildren.size(); i++) {
Task t = mChildren.get(i);
if (t.getGid().equals(gid)) {
return t;
}
}
return null;
}
// 定义一个整型的方法getChildTaskIndex参数为Task类型的task
public int getChildTaskIndex(Task task) {
return mChildren.indexOf(task);
}
// 定义一个Task类型的方法getChildTaskByIndex参数为整型的index
public Task getChildTaskByIndex(int index) {
if (index < 0 || index >= mChildren.size()) {
Log.e(TAG, "getTaskByIndex: invalid index");
return null;
}
// 返回mChildren中的index位置的Task对象
return mChildren.get(index);
}
// 定义一个Task类型的方法getChilTaskByGid参数为String类型的gid
public Task getChilTaskByGid(String gid) {
for (Task task : mChildren) {
if (task.getGid().equals(gid))
return task;
}
return null;
}
// 定义一个ArrayList类型的方法getChildTaskList
public ArrayList<Task> getChildTaskList() {
return this.mChildren;
}
// 定义一个整型的方法getIndex
public void setIndex(int index) {
this.mIndex = index;
}
// 定义一个整型的方法getIndex
public int getIndex() {
return this.mIndex;
}
}