|
|
/*
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 用于生成一个 JSONObject 对象,该对象表示一个用于创建当前 TaskList 对象的 Action
|
|
|
* @param actionId
|
|
|
* @return
|
|
|
*/
|
|
|
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());
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 用于生成一个 JSONObject 对象,该对象表示一个用于更新当前 TaskList 对象的 Action
|
|
|
* @param actionId
|
|
|
* @return
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将从网络返回的JSONObject对象解析出任务列表的ID、最后修改时间和名称,并根据解析结果设置相应属性的值
|
|
|
* @param js
|
|
|
*/
|
|
|
public void setContentByRemoteJSON(JSONObject js) {
|
|
|
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");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将从本地存储的JSONObject对象解析出便签的类型和名称,并根据解析结果设置相应任务列表的名称
|
|
|
* @param js
|
|
|
*/
|
|
|
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);
|
|
|
} 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();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将任务列表的名称转换为本地存储JSONObject对象并返回。如果转换过程中出现异常,将返回null。
|
|
|
* @return
|
|
|
*/
|
|
|
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(),
|
|
|
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;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据传入的游标Cursor c判断需要同步的操作:
|
|
|
* 如果本地和远程都没有进行更新,则返回“无需同步”状态;
|
|
|
* 如果只有远程数据更新了,则返回“将远程更新应用到本地”状态;
|
|
|
* 如果既有本地数据更新又有远程数据更新,则返回“将本地更新应用到远程”状态;
|
|
|
* 如果出现错误,则返回“同步错误”状态。
|
|
|
* @param c
|
|
|
* @return
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 返回当前任务列表下子任务的数量
|
|
|
* @return
|
|
|
*/
|
|
|
public int getChildTaskCount() {
|
|
|
return mChildren.size();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 向当前任务列表中添加一个子任务,如果添加成功则返回true。
|
|
|
* @param task
|
|
|
* @return
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 向当前任务列表中特定位置添加一个子任务。如果添加成功,则返回true。如果指定的索引不合法,则返回false。
|
|
|
* @param task
|
|
|
* @param index
|
|
|
* @return
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 从当前任务列表中删除一个子任务,并将该子任务的前一个同级任务的后继任务设置为该子任务的后继任务。如果删除成功则返回true,否则返回false。
|
|
|
* @param task
|
|
|
* @return
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将指定的子任务移至当前任务列表中特定位置。如果移动成功,则返回true,否则返回false。
|
|
|
* @param task
|
|
|
* @param index
|
|
|
* @return
|
|
|
*/
|
|
|
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));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查找并返回与指定任务ID相同的子任务。如果找到则返回该子任务,否则返回null。
|
|
|
* @param gid
|
|
|
* @return
|
|
|
*/
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 返回当前任务列表中指定子任务的索引。
|
|
|
* @param task
|
|
|
* @return
|
|
|
*/
|
|
|
public int getChildTaskIndex(Task task) {
|
|
|
return mChildren.indexOf(task);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 返回当前任务列表中指定索引下标的子任务对象。
|
|
|
* @param index
|
|
|
* @return
|
|
|
*/
|
|
|
public Task getChildTaskByIndex(int index) {
|
|
|
if (index < 0 || index >= mChildren.size()) {
|
|
|
Log.e(TAG, "getTaskByIndex: invalid index");
|
|
|
return null;
|
|
|
}
|
|
|
return mChildren.get(index);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据指定任务ID查找并返回与之匹配的子任务。
|
|
|
* @param gid
|
|
|
* @return
|
|
|
*/
|
|
|
public Task getChilTaskByGid(String gid) {
|
|
|
for (Task task : mChildren) {
|
|
|
if (task.getGid().equals(gid))
|
|
|
return task;
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 返回当前任务列表中包含的所有子任务。
|
|
|
* @return
|
|
|
*/
|
|
|
public ArrayList<Task> getChildTaskList() {
|
|
|
return this.mChildren;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将当前任务列表的索引设置为指定值。
|
|
|
* @param index
|
|
|
*/
|
|
|
public void setIndex(int index) {
|
|
|
this.mIndex = index;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 返回当前任务列表的索引值。
|
|
|
* @return
|
|
|
*/
|
|
|
public int getIndex() {
|
|
|
return this.mIndex;
|
|
|
}
|
|
|
}
|