Compare commits

..

No commits in common. 'main' and 'Yu_branch' have entirely different histories.

@ -1,3 +1,19 @@
/*
* 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;
@ -8,99 +24,59 @@ import net.micode.notes.tool.GTaskStringUtils;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Task
* Google Task
*
*/
public class MetaData extends Task {
// 日志标签(使用类名简化)
private final static String TAG = MetaData.class.getSimpleName();
// 关联的Google Task服务端ID
private String mRelatedGid = null;
/**
*
* @param gid Google Task ID
* @param metaInfo JSON
*/
public void setMeta(String gid, JSONObject metaInfo) {
try {
// 将Google Task ID注入元数据头部
metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
} catch (JSONException e) {
Log.e(TAG, "failed to put related gid"); // 理论上不会触发因META_HEAD_GTASK_ID是合法key
Log.e(TAG, "failed to put related gid");
}
// 将元数据存储到父类的notes字段
setNotes(metaInfo.toString());
// 设置固定名称标识
setName(GTaskStringUtils.META_NOTE_NAME);
}
/**
* Google Task ID
*/
public String getRelatedGid() {
return mRelatedGid;
}
/**
*
* @return true
*/
@Override
public boolean isWorthSaving() {
return getNotes() != null; // 仅当存在元数据时需要保存
return getNotes() != null;
}
/**
* JSON
* @param js JSON
*/
@Override
public void setContentByRemoteJSON(JSONObject js) {
super.setContentByRemoteJSON(js);
if (getNotes() != null) {
try {
// 解析notes字段中的元数据去除前后空格确保格式正确
JSONObject metaInfo = new JSONObject(getNotes().trim());
// 提取关联的Google Task ID
mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
} catch (JSONException e) {
Log.w(TAG, "failed to get related gid"); // 处理元数据格式错误
Log.w(TAG, "failed to get related gid");
mRelatedGid = null;
}
}
}
//------------------------ 以下方法禁用 ------------------------
// 设计约束:此类仅用于处理服务端元数据,禁止本地操作
/**
* JSON
* @throws IllegalAccessError
*/
@Override
public void setContentByLocalJSON(JSONObject js) {
// this function should not be called
throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
}
/**
* JSON
* @throws IllegalAccessError
*/
@Override
public JSONObject getLocalJSONFromContent() {
throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
}
/**
*
* @throws IllegalAccessError
*/
@Override
public int getSyncAction(Cursor c) {
throw new IllegalAccessError("MetaData:getSyncAction should not be called");
}
}
}

@ -1,143 +1,101 @@
/*
* 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 org.json.JSONObject;
/**
*
*
*
* 1. GID
* 2.
* 3. /
* 4. 访
*
* Note/Task
*/
public abstract class Node {
/* 同步动作类型常量定义 */
/** 无需同步操作 */
public static final int SYNC_ACTION_NONE = 0;
/** 需要在远程服务端创建新节点 */
public static final int SYNC_ACTION_ADD_REMOTE = 1;
/** 需要在本地创建新节点 */
public static final int SYNC_ACTION_ADD_LOCAL = 2;
/** 需要删除远程节点 */
public static final int SYNC_ACTION_DEL_REMOTE = 3;
/** 需要删除本地节点 */
public static final int SYNC_ACTION_DEL_LOCAL = 4;
/** 需要更新远程节点数据 */
public static final int SYNC_ACTION_UPDATE_REMOTE = 5;
/** 需要更新本地节点数据 */
public static final int SYNC_ACTION_UPDATE_LOCAL = 6;
/** 数据冲突需要解决(本地和远程版本都有修改) */
public static final int SYNC_ACTION_UPDATE_CONFLICT = 7;
/** 同步过程中发生错误 */
public static final int SYNC_ACTION_ERROR = 8;
// 节点基础属性
private String mGid; // 全局唯一标识(服务端分配)
private String mName; // 节点显示名称
private long mLastModified; // 最后修改时间戳(毫秒)
private boolean mDeleted; // 软删除标记
private String mGid;
private String mName;
private long mLastModified;
private boolean mDeleted;
/** 初始化节点默认属性 */
public Node() {
mGid = null; // 新建节点尚未分配GID
mName = ""; // 空名称初始化
mLastModified = 0; // 时间戳初始为0表示未同步
mDeleted = false; // 初始为非删除状态
mGid = null;
mName = "";
mLastModified = 0;
mDeleted = false;
}
/* 抽象方法定义(需子类实现) */
/**
* JSON
* @param actionId
* @return JSON
*/
public abstract JSONObject getCreateAction(int actionId);
/**
* JSON
* @param actionId
* @return JSON
*/
public abstract JSONObject getUpdateAction(int actionId);
/**
* JSON
* @param js JSON
*/
public abstract void setContentByRemoteJSON(JSONObject js);
/**
* JSON
* @param js JSON
*/
public abstract void setContentByLocalJSON(JSONObject js);
/**
* JSON
* @return JSON
*/
public abstract JSONObject getLocalJSONFromContent();
/**
*
* @param c
* @return
*/
public abstract int getSyncAction(Cursor c);
/* 基础属性访问方法 */
/** 设置全局唯一标识(通常由服务端分配) */
public void setGid(String gid) {
this.mGid = gid;
}
/** 设置节点显示名称 */
public void setName(String name) {
this.mName = name;
}
/** 设置最后修改时间戳UTC毫秒数 */
public void setLastModified(long lastModified) {
this.mLastModified = lastModified;
}
/** 标记节点删除状态true=已删除) */
public void setDeleted(boolean deleted) {
this.mDeleted = deleted;
}
/** 获取全局唯一标识 */
public String getGid() {
return this.mGid;
}
/** 获取节点显示名称 */
public String getName() {
return this.mName;
}
/** 获取最后修改时间戳 */
public long getLastModified() {
return this.mLastModified;
}
/** 获取删除状态 */
public boolean getDeleted() {
return this.mDeleted;
}
}
}

@ -1,14 +1,29 @@
/*
* 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;
// 导入所需的Android包
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
// 导入项目中的自定义类和接口
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.DataColumns;
import net.micode.notes.data.Notes.DataConstants;
@ -16,42 +31,46 @@ import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
import net.micode.notes.gtask.exception.ActionFailureException;
// 导入JSON相关类
import org.json.JSONException;
import org.json.JSONObject;
public class SqlData {
// 定义日志标签,用于调试
private static final String TAG = SqlData.class.getSimpleName();
// 定义一个无效ID的常量
private static final int INVALID_ID = -99999;
// 定义查询投影,指定要查询的列
public static final String[] PROJECTION_DATA = new String[] {
DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1,
DataColumns.DATA3
};
// 定义投影中各列的索引
public static final int DATA_ID_COLUMN = 0;
public static final int DATA_MIME_TYPE_COLUMN = 1;
public static final int DATA_CONTENT_COLUMN = 2;
public static final int DATA_CONTENT_DATA_1_COLUMN = 3;
public static final int DATA_CONTENT_DATA_3_COLUMN = 4;
// 数据成员用于存储内容解析器、是否创建、数据ID、MIME类型、内容等信息
private ContentResolver mContentResolver;
private boolean mIsCreate;
private long mDataId;
private String mDataMimeType;
private String mDataContent;
private long mDataContentData1;
private String mDataContentData3;
private ContentValues mDiffDataValues;
// 构造函数用于创建新的SqlData对象
public SqlData(Context context) {
mContentResolver = context.getContentResolver();
mIsCreate = true;
@ -63,7 +82,6 @@ public class SqlData {
mDiffDataValues = new ContentValues();
}
// 构造函数,用于从游标加载现有数据
public SqlData(Context context, Cursor c) {
mContentResolver = context.getContentResolver();
mIsCreate = false;
@ -71,7 +89,6 @@ public class SqlData {
mDiffDataValues = new ContentValues();
}
// 从游标加载数据到SqlData对象
private void loadFromCursor(Cursor c) {
mDataId = c.getLong(DATA_ID_COLUMN);
mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN);
@ -80,7 +97,6 @@ public class SqlData {
mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN);
}
// 设置内容更新内部状态和ContentValues
public void setContent(JSONObject js) throws JSONException {
long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID;
if (mIsCreate || mDataId != dataId) {
@ -114,7 +130,6 @@ public class SqlData {
mDataContentData3 = dataContentData3;
}
// 获取当前数据的内容封装为JSONObject
public JSONObject getContent() throws JSONException {
if (mIsCreate) {
Log.e(TAG, "it seems that we haven't created this in database yet");
@ -129,7 +144,6 @@ public class SqlData {
return js;
}
// 提交更改到数据库
public void commit(long noteId, boolean validateVersion, long version) {
if (mIsCreate) {
@ -153,7 +167,7 @@ public class SqlData {
Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null);
} else {
result = mContentResolver.update(ContentUris.withAppendedId(
Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues,
Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues,
" ? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE
+ " WHERE " + NoteColumns.VERSION + "=?)", new String[] {
String.valueOf(noteId), String.valueOf(version)
@ -169,8 +183,7 @@ public class SqlData {
mIsCreate = false;
}
// 获取当前数据的ID
public long getId() {
return mDataId;
}
}
}

@ -1,3 +1,19 @@
/*
* 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;
@ -23,13 +39,10 @@ import java.util.ArrayList;
public class SqlNote {
// 定义日志标签,用于调试输出
private static final String TAG = SqlNote.class.getSimpleName();
// 定义一个表示无效ID的常量
private static final int INVALID_ID = -99999;
// 定义查询投影,指定要查询的列
public static final String[] PROJECTION_NOTE = new String[] {
NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID,
NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE,
@ -39,46 +52,76 @@ public class SqlNote {
NoteColumns.VERSION
};
// 定义投影中各列的索引
public static final int ID_COLUMN = 0;
public static final int ALERTED_DATE_COLUMN = 1;
public static final int BG_COLOR_ID_COLUMN = 2;
public static final int CREATED_DATE_COLUMN = 3;
public static final int HAS_ATTACHMENT_COLUMN = 4;
public static final int MODIFIED_DATE_COLUMN = 5;
public static final int NOTES_COUNT_COLUMN = 6;
public static final int PARENT_ID_COLUMN = 7;
public static final int SNIPPET_COLUMN = 8;
public static final int TYPE_COLUMN = 9;
public static final int WIDGET_ID_COLUMN = 10;
public static final int WIDGET_TYPE_COLUMN = 11;
public static final int SYNC_ID_COLUMN = 12;
public static final int LOCAL_MODIFIED_COLUMN = 13;
public static final int ORIGIN_PARENT_ID_COLUMN = 14;
public static final int GTASK_ID_COLUMN = 15;
public static final int VERSION_COLUMN = 16;
// 数据成员,用于存储与笔记相关的各种信息
private Context mContext;
private ContentResolver mContentResolver;
private boolean mIsCreate;
private long mId;
private long mAlertDate;
private int mBgColorId;
private long mCreatedDate;
private int mHasAttachment;
private long mModifiedDate;
private long mParentId;
private String mSnippet;
private int mType;
private int mWidgetId;
private int mWidgetType;
private long mOriginParent;
private long mVersion;
private ContentValues mDiffNoteValues;
private ArrayList<SqlData> mDataList;
// 构造函数用于创建新的SqlNote对象
public SqlNote(Context context) {
mContext = context;
mContentResolver = context.getContentResolver();
@ -100,7 +143,6 @@ public class SqlNote {
mDataList = new ArrayList<SqlData>();
}
// 构造函数,用于从游标加载现有数据
public SqlNote(Context context, Cursor c) {
mContext = context;
mContentResolver = context.getContentResolver();
@ -112,7 +154,6 @@ public class SqlNote {
mDiffNoteValues = new ContentValues();
}
// 构造函数用于根据ID加载现有数据
public SqlNote(Context context, long id) {
mContext = context;
mContentResolver = context.getContentResolver();
@ -122,15 +163,15 @@ public class SqlNote {
if (mType == Notes.TYPE_NOTE)
loadDataContent();
mDiffNoteValues = new ContentValues();
}
// 根据ID从数据库加载数据
private void loadFromCursor(long id) {
Cursor c = null;
try {
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)",
new String[] {
String.valueOf(id)
String.valueOf(id)
}, null);
if (c != null) {
c.moveToNext();
@ -144,7 +185,6 @@ public class SqlNote {
}
}
// 从游标加载数据到SqlNote对象
private void loadFromCursor(Cursor c) {
mId = c.getLong(ID_COLUMN);
mAlertDate = c.getLong(ALERTED_DATE_COLUMN);
@ -160,14 +200,13 @@ public class SqlNote {
mVersion = c.getLong(VERSION_COLUMN);
}
// 加载与笔记相关的数据内容
private void loadDataContent() {
Cursor c = null;
mDataList.clear();
try {
c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA,
"(note_id=?)", new String[] {
String.valueOf(mId)
String.valueOf(mId)
}, null);
if (c != null) {
if (c.getCount() == 0) {
@ -187,14 +226,13 @@ public class SqlNote {
}
}
// 设置笔记内容更新内部状态和ContentValues
public boolean setContent(JSONObject js) {
try {
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
Log.w(TAG, "cannot set system folder");
} else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) {
// 对于文件夹,只更新摘要和类型
// for folder we can only update the snnipet and type
String snippet = note.has(NoteColumns.SNIPPET) ? note
.getString(NoteColumns.SNIPPET) : "";
if (mIsCreate || !mSnippet.equals(snippet)) {
@ -321,7 +359,6 @@ public class SqlNote {
return true;
}
// 获取笔记内容封装为JSONObject
public JSONObject getContent() {
try {
JSONObject js = new JSONObject();
@ -370,48 +407,39 @@ public class SqlNote {
return null;
}
// 设置父ID
public void setParentId(long id) {
mParentId = id;
mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
}
// 设置GTask ID
public void setGtaskId(String gid) {
mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
}
// 设置同步ID
public void setSyncId(long syncId) {
mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
}
// 重置本地修改标志
public void resetLocalModified() {
mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
}
// 获取ID
public long getId() {
return mId;
}
// 获取父ID
public long getParentId() {
return mParentId;
}
// 获取摘要
public String getSnippet() {
return mSnippet;
}
// 检查是否为笔记类型
public boolean isNoteType() {
return mType == Notes.TYPE_NOTE;
}
// 提交更改到数据库
public void commit(boolean validateVersion) {
if (mIsCreate) {
if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
@ -440,16 +468,16 @@ public class SqlNote {
throw new IllegalStateException("Try to update note with invalid id");
}
if (mDiffNoteValues.size() > 0) {
mVersion++;
mVersion ++;
int result = 0;
if (!validateVersion) {
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
+ NoteColumns.ID + "=?)", new String[] {
String.valueOf(mId)
String.valueOf(mId)
});
} else {
result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
+ NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
+ NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
new String[] {
String.valueOf(mId), String.valueOf(mVersion)
});
@ -466,7 +494,7 @@ public class SqlNote {
}
}
// 刷新本地信息
// refresh local info
loadFromCursor(mId);
if (mType == Notes.TYPE_NOTE)
loadDataContent();
@ -474,4 +502,4 @@ public class SqlNote {
mDiffNoteValues.clear();
mIsCreate = false;
}
}
}

@ -33,17 +33,18 @@ import org.json.JSONObject;
public class Task extends Node {
// 定义日志标签,用于调试输出
private static final String TAG = Task.class.getSimpleName();
// 定义任务相关属性
private boolean mCompleted; // 是否完成
private String mNotes; // 任务笔记
private JSONObject mMetaInfo; // 元信息
private Task mPriorSibling; // 前置兄弟任务
private TaskList mParent; // 父任务列表
private boolean mCompleted;
private String mNotes;
private JSONObject mMetaInfo;
private Task mPriorSibling;
private TaskList mParent;
// 构造函数,初始化任务对象
public Task() {
super();
mCompleted = false;
@ -53,22 +54,21 @@ public class Task extends Node {
mMetaInfo = null;
}
// 生成创建任务的JSON对象
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);
// 设置动作ID
// action_id
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
// 设置任务在父任务列表中的索引
// index
js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this));
// 设置实体增量信息包含任务名称、创建者ID、实体类型和笔记
// entity_delta
JSONObject entity = new JSONObject();
entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null");
@ -79,17 +79,17 @@ public class Task extends Node {
}
js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);
// 设置父任务ID
// parent_id
js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid());
// 设置目标父类型为组
// dest_parent_type
js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE,
GTaskStringUtils.GTASK_JSON_TYPE_GROUP);
// 设置列表ID为父任务的GID
// list_id
js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid());
// 如果存在前置兄弟任务设置其ID
// prior_sibling_id
if (mPriorSibling != null) {
js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid());
}
@ -103,22 +103,21 @@ public class Task extends Node {
return js;
}
// 生成更新任务的JSON对象
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);
// 设置动作ID
// action_id
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
// 设置任务ID
// id
js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());
// 设置实体增量信息,包含任务名称、笔记和删除状态
// entity_delta
JSONObject entity = new JSONObject();
entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
if (getNotes() != null) {
@ -136,36 +135,35 @@ public class Task extends Node {
return js;
}
// 从远程JSON对象设置任务内容
public void setContentByRemoteJSON(JSONObject js) {
if (js != null) {
try {
// 设置任务ID
// 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));
}
// 设置任务笔记
// notes
if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) {
setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES));
}
// 设置任务是否被删除
// deleted
if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) {
setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED));
}
// 设置任务是否完成
// completed
if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) {
setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED));
}
@ -177,11 +175,10 @@ public class Task extends Node {
}
}
// 从本地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 available");
Log.w(TAG, "setContentByLocalJSON: nothing is avaiable");
}
try {
@ -207,12 +204,11 @@ public class Task extends Node {
}
}
// 从任务内容生成本地JSON对象
public JSONObject getLocalJSONFromContent() {
String name = getName();
try {
if (mMetaInfo == null) {
// 新任务从网页创建
// new task created from web
if (name == null) {
Log.w(TAG, "the note seems to be an empty one");
return null;
@ -229,7 +225,7 @@ public class Task extends Node {
js.put(GTaskStringUtils.META_HEAD_NOTE, note);
return js;
} else {
// 已同步的任务
// synced task
JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
@ -251,7 +247,6 @@ public class Task extends Node {
}
}
// 设置任务的元信息
public void setMetaInfo(MetaData metaData) {
if (metaData != null && metaData.getNotes() != null) {
try {
@ -263,7 +258,6 @@ public class Task extends Node {
}
}
// 获取同步操作类型
public int getSyncAction(Cursor c) {
try {
JSONObject noteInfo = null;
@ -281,29 +275,29 @@ public class Task extends Node {
return SYNC_ACTION_UPDATE_LOCAL;
}
// 验证任务ID是否匹配
// validate the note id now
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) {
// 本地没有更新
// 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是否匹配
// 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 {
return SYNC_ACTION_UPDATE_CONFLICT;
@ -317,49 +311,41 @@ public class Task extends Node {
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;
}
}
}

@ -31,36 +31,33 @@ 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;
}
// 生成创建任务列表的JSON对象
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);
// 设置动作ID
// action_id
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
// 设置任务列表的索引
// index
js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex);
// 设置实体增量信息包含任务列表名称和创建者ID
// entity_delta
JSONObject entity = new JSONObject();
entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null");
@ -77,22 +74,21 @@ public class TaskList extends Node {
return js;
}
// 生成更新任务列表的JSON对象
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);
// 设置动作ID
// action_id
js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
// 设置任务列表ID
// 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());
@ -107,21 +103,20 @@ public class TaskList extends Node {
return js;
}
// 从远程JSON对象设置任务列表内容
public void setContentByRemoteJSON(JSONObject js) {
if (js != null) {
try {
// 设置任务列表ID
// 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));
}
@ -134,25 +129,23 @@ public class TaskList extends Node {
}
}
// 从本地JSON对象设置任务列表内容
public void setContentByLocalJSON(JSONObject js) {
if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) {
Log.w(TAG, "setContentByLocalJSON: nothing is available");
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);
setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX
+ GTaskStringUtils.FOLDER_CALL_NOTE);
else
Log.e(TAG, "invalid system folder");
} else {
@ -164,7 +157,6 @@ public class TaskList extends Node {
}
}
// 从任务列表内容生成本地JSON对象
public JSONObject getLocalJSONFromContent() {
try {
JSONObject js = new JSONObject();
@ -191,29 +183,28 @@ public class TaskList extends Node {
}
}
// 获取同步操作类型
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 {
// 验证GTask ID是否匹配
// 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;
}
}
@ -225,18 +216,16 @@ public class TaskList extends Node {
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);
@ -245,7 +234,6 @@ public class TaskList extends Node {
return ret;
}
// 在指定位置添加子任务
public boolean addChildTask(Task task, int index) {
if (index < 0 || index > mChildren.size()) {
Log.e(TAG, "add child task: invalid index");
@ -256,7 +244,7 @@ public class TaskList extends Node {
if (task != null && pos == -1) {
mChildren.add(index, task);
// 更新任务列表
// update the task list
Task preTask = null;
Task afterTask = null;
if (index != 0)
@ -272,7 +260,6 @@ public class TaskList extends Node {
return true;
}
// 移除子任务
public boolean removeChildTask(Task task) {
boolean ret = false;
int index = mChildren.indexOf(task);
@ -280,11 +267,11 @@ public class TaskList extends Node {
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));
@ -294,7 +281,6 @@ public class TaskList extends Node {
return ret;
}
// 移动子任务
public boolean moveChildTask(Task task, int index) {
if (index < 0 || index >= mChildren.size()) {
@ -313,7 +299,6 @@ public class TaskList extends Node {
return (removeChildTask(task) && addChildTask(task, index));
}
// 根据GID查找子任务
public Task findChildTaskByGid(String gid) {
for (int i = 0; i < mChildren.size(); i++) {
Task t = mChildren.get(i);
@ -324,12 +309,10 @@ public class TaskList extends Node {
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");
@ -338,7 +321,6 @@ public class TaskList extends Node {
return mChildren.get(index);
}
// 根据GID获取子任务
public Task getChilTaskByGid(String gid) {
for (Task task : mChildren) {
if (task.getGid().equals(gid))
@ -347,18 +329,15 @@ public class TaskList extends Node {
return null;
}
// 获取子任务列表
public ArrayList<Task> getChildTaskList() {
return this.mChildren;
}
// 设置任务列表索引
public void setIndex(int index) {
this.mIndex = index;
}
// 获取任务列表索引
public int getIndex() {
return this.mIndex;
}
}
}

Loading…
Cancel
Save