forked from poz2c78vw/read
Compare commits
47 Commits
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,374 +0,0 @@
|
||||
/*
|
||||
* 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.appwidget.AppWidgetManager; // 处理小部件的类
|
||||
import android.content.ContentResolver; // 用于访问内容提供者的类
|
||||
import android.content.ContentValues; // 用于存储内容数据的类
|
||||
import android.content.Context; // Android上下文类
|
||||
import android.database.Cursor; // 数据库操作的游标类
|
||||
import android.net.Uri; // URI的类
|
||||
import android.util.Log; // 日志记录类
|
||||
|
||||
// 导入笔记和相关数据列的类
|
||||
import net.micode.notes.data.Notes;
|
||||
import net.micode.notes.data.Notes.DataColumns;
|
||||
import net.micode.notes.data.Notes.NoteColumns;
|
||||
import net.micode.notes.gtask.exception.ActionFailureException; // 自定义异常类
|
||||
import net.micode.notes.tool.GTaskStringUtils; // 字符串工具类
|
||||
import net.micode.notes.tool.ResourceParser; // 资源解析工具类
|
||||
|
||||
import org.json.JSONArray; // JSON数组类
|
||||
import org.json.JSONException; // JSON异常类
|
||||
import org.json.JSONObject; // JSON对象类
|
||||
|
||||
import java.util.ArrayList; // 动态数组类
|
||||
|
||||
// Task类用于表示一个任务
|
||||
public class Task {
|
||||
private static final String TAG = Task.class.getSimpleName(); // 日志标识
|
||||
|
||||
private boolean mCompleted; // 标识任务是否已完成
|
||||
private String mNotes; // 任务的备注
|
||||
private Task mPriorSibling; // 优先兄弟任务
|
||||
private TaskList mParent; // 父任务列表
|
||||
private JSONObject mMetaInfo; // 任务的元信息
|
||||
|
||||
// 构造函数
|
||||
public Task() {
|
||||
super(); // 调用父类构造函数
|
||||
mCompleted = false; // 默认任务未完成
|
||||
mNotes = null; // 备注初始化为null
|
||||
mPriorSibling = null; // 优先兄弟任务初始化为null
|
||||
mParent = null; // 父任务列表初始化为null
|
||||
mMetaInfo = null; // 元信息初始化为null
|
||||
}
|
||||
|
||||
// 获取创建操作的JSON对象
|
||||
public JSONObject getCreateAction(int actionId) {
|
||||
JSONObject js = new JSONObject(); // 创建新的JSON对象
|
||||
|
||||
try {
|
||||
// 设置操作类型
|
||||
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
||||
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);
|
||||
|
||||
// 设置操作ID
|
||||
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
|
||||
|
||||
// 设置索引
|
||||
js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this));
|
||||
|
||||
// 创建实体增量对象
|
||||
JSONObject entity = new JSONObject();
|
||||
entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); // 设置任务名称
|
||||
entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); // 创建者ID
|
||||
entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE,
|
||||
GTaskStringUtils.GTASK_JSON_TYPE_TASK); // 设置实体类型
|
||||
if (getNotes() != null) {
|
||||
entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes()); // 设置任务备注
|
||||
}
|
||||
js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); // 将增量对象添加至主对象
|
||||
|
||||
// 设置父任务ID
|
||||
js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid());
|
||||
|
||||
// 设置目标父级类型
|
||||
js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE,
|
||||
GTaskStringUtils.GTASK_JSON_TYPE_GROUP);
|
||||
|
||||
// 设置列表ID
|
||||
js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid());
|
||||
|
||||
// 设置优先兄弟任务ID
|
||||
if (mPriorSibling != null) {
|
||||
js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid());
|
||||
}
|
||||
|
||||
} catch (JSONException e) { // 捕获JSON异常
|
||||
Log.e(TAG, e.toString()); // 输出错误日志
|
||||
e.printStackTrace(); // 打印堆栈信息
|
||||
throw new ActionFailureException("fail to generate task-create jsonobject"); // 抛出异常
|
||||
}
|
||||
|
||||
return js; // 返回生成的JSON对象
|
||||
}
|
||||
|
||||
// 获取更新操作的JSON对象
|
||||
public JSONObject getUpdateAction(int actionId) {
|
||||
JSONObject js = new JSONObject(); // 创建新的JSON对象
|
||||
|
||||
try {
|
||||
// 设置操作类型
|
||||
js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
|
||||
GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);
|
||||
|
||||
// 设置操作ID
|
||||
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
|
||||
|
||||
// 设置任务ID
|
||||
js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());
|
||||
|
||||
// 创建实体增量对象
|
||||
JSONObject entity = new JSONObject();
|
||||
entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); // 设置任务名称
|
||||
if (getNotes() != null) {
|
||||
entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes()); // 设置任务备注
|
||||
}
|
||||
entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted()); // 设置删除标志
|
||||
js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); // 将增量对象添加至主对象
|
||||
|
||||
} catch (JSONException e) { // 捕获JSON异常
|
||||
Log.e(TAG, e.toString()); // 输出错误日志
|
||||
e.printStackTrace(); // 打印堆栈信息
|
||||
throw new ActionFailureException("fail to generate task-update jsonobject"); // 抛出异常
|
||||
}
|
||||
|
||||
return js; // 返回生成的JSON对象
|
||||
}
|
||||
|
||||
// 根据远程JSON设置任务内容
|
||||
public void setContentByRemoteJSON(JSONObject js) {
|
||||
if (js != null) { // 如果JSON对象不为空
|
||||
try {
|
||||
// 设置任务ID
|
||||
if (js.has(GTaskStringUtils.GTASK_JSON_ID)) {
|
||||
setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID));
|
||||
}
|
||||
|
||||
// 设置最后修改时间
|
||||
if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) {
|
||||
setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED));
|
||||
}
|
||||
|
||||
// 设置任务名称
|
||||
if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) {
|
||||
setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME));
|
||||
}
|
||||
|
||||
// 设置任务备注
|
||||
if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) {
|
||||
setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES));
|
||||
}
|
||||
|
||||
// 设置删除标志
|
||||
if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) {
|
||||
setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED));
|
||||
}
|
||||
|
||||
// 设置完成状态
|
||||
if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) {
|
||||
setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED));
|
||||
}
|
||||
} catch (JSONException e) { // 捕获JSON异常
|
||||
Log.e(TAG, e.toString()); // 输出错误日志
|
||||
e.printStackTrace(); // 打印堆栈信息
|
||||
throw new ActionFailureException("fail to get task content from jsonobject"); // 抛出异常
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 根据本地JSON设置任务内容
|
||||
public void setContentByLocalJSON(JSONObject js) {
|
||||
if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)
|
||||
|| !js.has(GTaskStringUtils.META_HEAD_DATA)) { // 检查必需字段
|
||||
Log.w(TAG, "setContentByLocalJSON: nothing is avaiable"); // 无数据警告
|
||||
}
|
||||
|
||||
try {
|
||||
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); // 获取笔记对象
|
||||
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); // 获取数据数组
|
||||
|
||||
if (note.getInt(NoteColumns.TYPE) != Notes.TYPE_NOTE) { // 检查任务类型
|
||||
Log.e(TAG, "invalid type"); // 输出错误日志
|
||||
return; // 返回
|
||||
}
|
||||
|
||||
// 遍历数据数组
|
||||
for (int i = 0; i < dataArray.length(); i++) {
|
||||
JSONObject data = dataArray.getJSONObject(i); // 获取每个数据对象
|
||||
if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { // 检查MIME类型
|
||||
setName(data.getString(DataColumns.CONTENT)); // 设置任务名称
|
||||
break; // 退出循环
|
||||
}
|
||||
}
|
||||
|
||||
} catch (JSONException e) { // 捕获JSON异常
|
||||
Log.e(TAG, e.toString()); // 输出错误日志
|
||||
e.printStackTrace(); // 打印堆栈信息
|
||||
}
|
||||
}
|
||||
|
||||
// 从内容获取本地JSON
|
||||
public JSONObject getLocalJSONFromContent() {
|
||||
String name = getName(); // 获取任务名称
|
||||
try {
|
||||
if (mMetaInfo == null) { // 如果元信息为null
|
||||
// 新任务从网络创建
|
||||
if (name == null) { // 如果名称为空
|
||||
Log.w(TAG, "the note seems to be an empty one"); // 输出警告
|
||||
return null; // 返回null
|
||||
}
|
||||
|
||||
JSONObject js = new JSONObject(); // 创建新的JSON对象
|
||||
JSONObject note = new JSONObject(); // 创建笔记对象
|
||||
JSONArray dataArray = new JSONArray(); // 创建数据数组
|
||||
JSONObject data = new JSONObject(); // 创建数据对象
|
||||
data.put(DataColumns.CONTENT, name); // 设置内容
|
||||
dataArray.put(data); // 添加数据对象到数组
|
||||
js.put(GTaskStringUtils.META_HEAD_DATA, dataArray); // 将数据数组添加到主对象
|
||||
note.put(NoteColumns.TYPE, Notes.TYPE_NOTE); // 设置任务类型
|
||||
js.put(GTaskStringUtils.META_HEAD_NOTE, note); // 将笔记对象添加到主对象
|
||||
return js; // 返回生成的JSON对象
|
||||
} else {
|
||||
// 已同步的任务
|
||||
JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); // 获取元信息中的笔记对象
|
||||
JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA); // 获取数据数组
|
||||
|
||||
for (int i = 0; i < dataArray.length(); i++) {
|
||||
JSONObject data = dataArray.getJSONObject(i); // 获取数据对象
|
||||
if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) {
|
||||
data.put(DataColumns.CONTENT, getName()); // 更新内容
|
||||
break; // 退出循环
|
||||
}
|
||||
}
|
||||
|
||||
note.put(NoteColumns.TYPE, Notes.TYPE_NOTE); // 确保类型为笔记
|
||||
return mMetaInfo; // 返回元信息
|
||||
}
|
||||
} catch (JSONException e) { // 捕获JSON异常
|
||||
Log.e(TAG, e.toString()); // 输出错误日志
|
||||
e.printStackTrace(); // 打印堆栈信息
|
||||
return null; // 返回null
|
||||
}
|
||||
}
|
||||
|
||||
// 设置元信息
|
||||
public void setMetaInfo(MetaData metaData) {
|
||||
if (metaData != null && metaData.getNotes() != null) { // 检查元数据及笔记是否为空
|
||||
try {
|
||||
mMetaInfo = new JSONObject(metaData.getNotes()); // 从元数据中获取笔记信息
|
||||
} catch (JSONException e) { // 捕获JSON异常
|
||||
Log.w(TAG, e.toString()); // 输出警告日志
|
||||
mMetaInfo = null; // 将元信息设为null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取同步操作
|
||||
public int getSyncAction(Cursor c) {
|
||||
try {
|
||||
JSONObject noteInfo = null; // 笔记信息对象
|
||||
// 检查元信息是否存在
|
||||
if (mMetaInfo != null && mMetaInfo.has(GTaskStringUtils.META_HEAD_NOTE)) {
|
||||
noteInfo = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); // 获取笔记信息
|
||||
}
|
||||
|
||||
if (noteInfo == null) { // 如果笔记信息为空
|
||||
Log.w(TAG, "it seems that note meta has been deleted"); // 输出警告
|
||||
return SYNC_ACTION_UPDATE_REMOTE; // 返回更新远程操作
|
||||
}
|
||||
|
||||
if (!noteInfo.has(NoteColumns.ID)) { // 如果笔记ID不存在
|
||||
Log.w(TAG, "remote note id seems to be deleted"); // 输出警告
|
||||
return SYNC_ACTION_UPDATE_LOCAL; // 返回更新本地操作
|
||||
}
|
||||
|
||||
// 验证笔记ID
|
||||
if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) {
|
||||
Log.w(TAG, "note id doesn't match"); // 输出警告
|
||||
return SYNC_ACTION_UPDATE_LOCAL; // 返回更新本地操作
|
||||
}
|
||||
|
||||
if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { // 如果没有本地修改
|
||||
// 检查同步ID
|
||||
if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
|
||||
// 双方没有更新
|
||||
return SYNC_ACTION_NONE; // 返回无操作
|
||||
} else {
|
||||
// 应用远程到本地
|
||||
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_CONFLICT; // 返回更新冲突操作
|
||||
}
|
||||
}
|
||||
} catch (Exception e) { // 捕获所有异常
|
||||
Log.e(TAG, e.toString()); // 输出错误日志
|
||||
e.printStackTrace(); // 打印堆栈信息
|
||||
}
|
||||
|
||||
return SYNC_ACTION_ERROR; // 返回错误操作
|
||||
}
|
||||
|
||||
// 判断任务是否值得保存
|
||||
public boolean isWorthSaving() {
|
||||
return mMetaInfo != null || (getName() != null && getName().trim().length() > 0)
|
||||
|| (getNotes() != null && getNotes().trim().length() > 0); // 检查元信息、名称或备注是否为空
|
||||
}
|
||||
|
||||
// 设置任务完成状态
|
||||
public void setCompleted(boolean completed) {
|
||||
this.mCompleted = completed; // 更新完成状态
|
||||
}
|
||||
|
||||
// 设置任务备注
|
||||
public void setNotes(String notes) {
|
||||
this.mNotes = notes; // 更新备注
|
||||
}
|
||||
|
||||
// 设置优先兄弟任务
|
||||
public void setPriorSibling(Task priorSibling) {
|
||||
this.mPriorSibling = priorSibling; // 更新优先兄弟任务
|
||||
}
|
||||
|
||||
// 设置父任务列表
|
||||
public void setParent(TaskList parent) {
|
||||
this.mParent = parent; // 更新父任务列表
|
||||
}
|
||||
|
||||
// 获取完成状态
|
||||
public boolean getCompleted() {
|
||||
return this.mCompleted; // 返回完成状态
|
||||
}
|
||||
|
||||
// 获取备注
|
||||
public String getNotes() {
|
||||
return this.mNotes; // 返回备注
|
||||
}
|
||||
|
||||
// 获取优先兄弟任务
|
||||
public Task getPriorSibling() {
|
||||
return this.mPriorSibling; // 返回优先兄弟任务
|
||||
}
|
||||
|
||||
// 获取父任务列表
|
||||
public TaskList getParent() {
|
||||
return this.mParent; // 返回父任务列表
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue