|
|
|
|
@ -48,47 +48,45 @@ import java.util.Iterator;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @Package: net.micode.notes.gtask.remote
|
|
|
|
|
* @ClassName: GTaskManager
|
|
|
|
|
* @Description:
|
|
|
|
|
* 1. 协调本地SQLite数据库与Google Task之间的双向同步
|
|
|
|
|
* 2. 管理同步状态和冲突解决策略
|
|
|
|
|
* 3. 维护本地ID与Google Task ID之间的映射关系
|
|
|
|
|
* 4. 处理文件夹、便签和元数据的完整同步逻辑
|
|
|
|
|
*/
|
|
|
|
|
public class GTaskManager {
|
|
|
|
|
private static final String TAG = GTaskManager.class.getSimpleName(); // 日志标签
|
|
|
|
|
private static final String TAG = GTaskManager.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
public static final int STATE_SUCCESS = 0;
|
|
|
|
|
|
|
|
|
|
public static final int STATE_NETWORK_ERROR = 1;
|
|
|
|
|
|
|
|
|
|
public static final int STATE_INTERNAL_ERROR = 2;
|
|
|
|
|
|
|
|
|
|
// 同步状态常量
|
|
|
|
|
public static final int STATE_SUCCESS = 0; // 同步成功
|
|
|
|
|
public static final int STATE_NETWORK_ERROR = 1; // 网络错误
|
|
|
|
|
public static final int STATE_INTERNAL_ERROR = 2; // 内部错误
|
|
|
|
|
public static final int STATE_SYNC_IN_PROGRESS = 3; // 同步进行中
|
|
|
|
|
public static final int STATE_SYNC_CANCELLED = 4; // 同步已取消
|
|
|
|
|
public static final int STATE_SYNC_IN_PROGRESS = 3;
|
|
|
|
|
|
|
|
|
|
public static final int STATE_SYNC_CANCELLED = 4;
|
|
|
|
|
|
|
|
|
|
private static GTaskManager mInstance = null;
|
|
|
|
|
|
|
|
|
|
private Activity mActivity; // Activity上下文,用于获取授权令牌
|
|
|
|
|
private Context mContext; // 应用上下文
|
|
|
|
|
private ContentResolver mContentResolver; // ContentResolver,用于数据库操作
|
|
|
|
|
private boolean mSyncing; // 同步状态标记
|
|
|
|
|
private boolean mCancelled; // 取消同步标记
|
|
|
|
|
private Activity mActivity;
|
|
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
|
|
private ContentResolver mContentResolver;
|
|
|
|
|
|
|
|
|
|
private boolean mSyncing;
|
|
|
|
|
|
|
|
|
|
// Google Task相关数据结构
|
|
|
|
|
private HashMap<String, TaskList> mGTaskListHashMap; // Google Task列表映射(GID -> TaskList)
|
|
|
|
|
private HashMap<String, Node> mGTaskHashMap; // Google Task节点映射(GID -> Node)
|
|
|
|
|
private HashMap<String, MetaData> mMetaHashMap; // 元数据映射(相关GID -> MetaData)
|
|
|
|
|
private TaskList mMetaList; // 元数据任务列表
|
|
|
|
|
private boolean mCancelled;
|
|
|
|
|
|
|
|
|
|
// 本地数据相关
|
|
|
|
|
private HashSet<Long> mLocalDeleteIdMap; // 本地待删除的便签ID集合
|
|
|
|
|
private HashMap<String, Long> mGidToNid; // GID到本地ID的映射
|
|
|
|
|
private HashMap<Long, String> mNidToGid; // 本地ID到GID的映射
|
|
|
|
|
private HashMap<String, TaskList> mGTaskListHashMap;
|
|
|
|
|
|
|
|
|
|
private HashMap<String, Node> mGTaskHashMap;
|
|
|
|
|
|
|
|
|
|
private HashMap<String, MetaData> mMetaHashMap;
|
|
|
|
|
|
|
|
|
|
private TaskList mMetaList;
|
|
|
|
|
|
|
|
|
|
private HashSet<Long> mLocalDeleteIdMap;
|
|
|
|
|
|
|
|
|
|
private HashMap<String, Long> mGidToNid;
|
|
|
|
|
|
|
|
|
|
private HashMap<Long, String> mNidToGid;
|
|
|
|
|
|
|
|
|
|
//初始化所有数据结构和状态变量
|
|
|
|
|
private GTaskManager() {
|
|
|
|
|
mSyncing = false;
|
|
|
|
|
mCancelled = false;
|
|
|
|
|
@ -101,7 +99,6 @@ public class GTaskManager {
|
|
|
|
|
mNidToGid = new HashMap<Long, String>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static synchronized GTaskManager getInstance() {
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new GTaskManager();
|
|
|
|
|
@ -109,41 +106,20 @@ public class GTaskManager {
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//设置Activity上下文
|
|
|
|
|
public synchronized void setActivityContext(Activity activity) {
|
|
|
|
|
// used for getting authtoken
|
|
|
|
|
mActivity = activity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method sync
|
|
|
|
|
* @description 执行同步操作
|
|
|
|
|
* 1. 检查是否已经在同步中
|
|
|
|
|
* 2. 初始化同步环境和数据结构
|
|
|
|
|
* 3. 登录Google Task
|
|
|
|
|
* 4. 初始化任务列表
|
|
|
|
|
* 5. 同步内容(包括文件夹和便签)
|
|
|
|
|
* 6. 清理资源
|
|
|
|
|
* 7. 返回同步结果
|
|
|
|
|
*
|
|
|
|
|
* @param context Android上下文
|
|
|
|
|
* @param asyncTask 异步任务,用于更新进度
|
|
|
|
|
* @return 同步结果状态码
|
|
|
|
|
*/
|
|
|
|
|
public int sync(Context context, GTaskASyncTask asyncTask) {
|
|
|
|
|
// 检查是否已经在同步中
|
|
|
|
|
if (mSyncing) {
|
|
|
|
|
Log.d(TAG, "Sync is in progress");
|
|
|
|
|
return STATE_SYNC_IN_PROGRESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化同步环境
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = mContext.getContentResolver();
|
|
|
|
|
mSyncing = true;
|
|
|
|
|
mCancelled = false;
|
|
|
|
|
|
|
|
|
|
// 清空数据结构,准备新的同步
|
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
|
@ -153,20 +129,20 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
GTaskClient client = GTaskClient.getInstance();
|
|
|
|
|
client.resetUpdateArray(); // 重置客户端更新数组
|
|
|
|
|
client.resetUpdateArray();
|
|
|
|
|
|
|
|
|
|
// 登录Google Task
|
|
|
|
|
// login google task
|
|
|
|
|
if (!mCancelled) {
|
|
|
|
|
if (!client.login(mActivity)) {
|
|
|
|
|
throw new NetworkFailureException("login google task failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化任务列表
|
|
|
|
|
// get the task list from google
|
|
|
|
|
asyncTask.publishProgess(mContext.getString(R.string.sync_progress_init_list));
|
|
|
|
|
initGTaskList();
|
|
|
|
|
|
|
|
|
|
// 执行内容同步
|
|
|
|
|
// do content sync work
|
|
|
|
|
asyncTask.publishProgess(mContext.getString(R.string.sync_progress_syncing));
|
|
|
|
|
syncContent();
|
|
|
|
|
} catch (NetworkFailureException e) {
|
|
|
|
|
@ -180,7 +156,6 @@ public class GTaskManager {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return STATE_INTERNAL_ERROR;
|
|
|
|
|
} finally {
|
|
|
|
|
// 无论成功失败,都清理资源
|
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
|
@ -193,36 +168,26 @@ public class GTaskManager {
|
|
|
|
|
return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method initGTaskList
|
|
|
|
|
* @description 初始化Google Task列表
|
|
|
|
|
* 执行流程:
|
|
|
|
|
* 1. 从Google Task获取所有任务列表
|
|
|
|
|
* 2. 查找并初始化元数据列表
|
|
|
|
|
* 3. 加载元数据列表中的所有元数据
|
|
|
|
|
* 4. 如果元数据列表不存在则创建
|
|
|
|
|
* 5. 初始化普通任务列表
|
|
|
|
|
*/
|
|
|
|
|
private void initGTaskList() throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) return;
|
|
|
|
|
|
|
|
|
|
if (mCancelled)
|
|
|
|
|
return;
|
|
|
|
|
GTaskClient client = GTaskClient.getInstance();
|
|
|
|
|
try {
|
|
|
|
|
JSONArray jsTaskLists = client.getTaskLists();
|
|
|
|
|
|
|
|
|
|
// 首先初始化元数据列表
|
|
|
|
|
// init meta list first
|
|
|
|
|
mMetaList = null;
|
|
|
|
|
for (int i = 0; i < jsTaskLists.length(); i++) {
|
|
|
|
|
JSONObject object = jsTaskLists.getJSONObject(i);
|
|
|
|
|
String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
|
|
|
|
|
String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);
|
|
|
|
|
|
|
|
|
|
// 查找元数据列表(名称以特定前缀开头)
|
|
|
|
|
if (name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
|
|
|
|
|
if (name
|
|
|
|
|
.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
|
|
|
|
|
mMetaList = new TaskList();
|
|
|
|
|
mMetaList.setContentByRemoteJSON(object);
|
|
|
|
|
|
|
|
|
|
// 加载元数据列表中的所有元数据任务
|
|
|
|
|
// load meta data
|
|
|
|
|
JSONArray jsMetas = client.getTaskList(gid);
|
|
|
|
|
for (int j = 0; j < jsMetas.length(); j++) {
|
|
|
|
|
object = (JSONObject) jsMetas.getJSONObject(j);
|
|
|
|
|
@ -238,28 +203,29 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果元数据列表不存在,创建新的
|
|
|
|
|
// create meta list if not existed
|
|
|
|
|
if (mMetaList == null) {
|
|
|
|
|
mMetaList = new TaskList();
|
|
|
|
|
mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META);
|
|
|
|
|
mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
|
+ GTaskStringUtils.FOLDER_META);
|
|
|
|
|
GTaskClient.getInstance().createTaskList(mMetaList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化普通任务列表
|
|
|
|
|
// init task list
|
|
|
|
|
for (int i = 0; i < jsTaskLists.length(); i++) {
|
|
|
|
|
JSONObject object = jsTaskLists.getJSONObject(i);
|
|
|
|
|
String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
|
|
|
|
|
String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);
|
|
|
|
|
|
|
|
|
|
// 只处理以MIUI前缀开头的任务列表(排除元数据列表)
|
|
|
|
|
if (name.startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX)
|
|
|
|
|
&& !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
|
|
|
|
|
&& !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
|
+ GTaskStringUtils.FOLDER_META)) {
|
|
|
|
|
TaskList tasklist = new TaskList();
|
|
|
|
|
tasklist.setContentByRemoteJSON(object);
|
|
|
|
|
mGTaskListHashMap.put(gid, tasklist);
|
|
|
|
|
mGTaskHashMap.put(gid, tasklist);
|
|
|
|
|
|
|
|
|
|
// 加载任务列表中的所有任务
|
|
|
|
|
// load tasks
|
|
|
|
|
JSONArray jsTasks = client.getTaskList(gid);
|
|
|
|
|
for (int j = 0; j < jsTasks.length(); j++) {
|
|
|
|
|
object = (JSONObject) jsTasks.getJSONObject(j);
|
|
|
|
|
@ -281,17 +247,6 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method syncContent
|
|
|
|
|
* @description 同步内容(便签和文件夹的具体数据)
|
|
|
|
|
* 执行流程:
|
|
|
|
|
* 1. 同步本地已删除的便签
|
|
|
|
|
* 2. 同步文件夹(先同步文件夹,再同步便签)
|
|
|
|
|
* 3. 同步现有的便签
|
|
|
|
|
* 4. 处理远程新增的项目
|
|
|
|
|
* 5. 批量删除本地已删除的便签
|
|
|
|
|
* 6. 刷新本地同步ID
|
|
|
|
|
*/
|
|
|
|
|
private void syncContent() throws NetworkFailureException {
|
|
|
|
|
int syncType;
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
@ -300,9 +255,11 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
mLocalDeleteIdMap.clear();
|
|
|
|
|
|
|
|
|
|
if (mCancelled) return;
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第一步:处理本地已删除的便签(在回收站中的便签)
|
|
|
|
|
// for local deleted note
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
|
"(type<>? AND parent_id=?)", new String[] {
|
|
|
|
|
@ -329,10 +286,10 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第二步:同步文件夹(必须先同步文件夹,因为便签需要知道父文件夹的映射关系)
|
|
|
|
|
// sync folder first
|
|
|
|
|
syncFolder();
|
|
|
|
|
|
|
|
|
|
// 第三步:同步现有的便签
|
|
|
|
|
// for note existing in database
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
|
"(type=? AND parent_id<>?)", new String[] {
|
|
|
|
|
@ -349,10 +306,10 @@ public class GTaskManager {
|
|
|
|
|
syncType = node.getSyncAction(c);
|
|
|
|
|
} else {
|
|
|
|
|
if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
|
|
|
|
|
// 本地新增:本地有GID,但远程没有对应节点
|
|
|
|
|
// local add
|
|
|
|
|
syncType = Node.SYNC_ACTION_ADD_REMOTE;
|
|
|
|
|
} else {
|
|
|
|
|
// 远程删除:本地有GID,但远程节点不存在
|
|
|
|
|
// remote delete
|
|
|
|
|
syncType = Node.SYNC_ACTION_DEL_LOCAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -361,6 +318,7 @@ public class GTaskManager {
|
|
|
|
|
} else {
|
|
|
|
|
Log.w(TAG, "failed to query existing note in database");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
@ -368,7 +326,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第四步:处理远程新增的项目(遍历剩余的Google Task节点)
|
|
|
|
|
// go through remaining items
|
|
|
|
|
Iterator<Map.Entry<String, Node>> iter = mGTaskHashMap.entrySet().iterator();
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
|
Map.Entry<String, Node> entry = iter.next();
|
|
|
|
|
@ -376,38 +334,34 @@ public class GTaskManager {
|
|
|
|
|
doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第五步:批量删除本地已删除的便签
|
|
|
|
|
// mCancelled can be set by another thread, so we neet to check one by
|
|
|
|
|
// one
|
|
|
|
|
// clear local delete table
|
|
|
|
|
if (!mCancelled) {
|
|
|
|
|
if (!DataUtils.batchDeleteNotes(mContentResolver, mLocalDeleteIdMap)) {
|
|
|
|
|
throw new ActionFailureException("failed to batch-delete local deleted notes");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 第六步:刷新本地同步ID
|
|
|
|
|
// refresh local sync id
|
|
|
|
|
if (!mCancelled) {
|
|
|
|
|
GTaskClient.getInstance().commitUpdate();
|
|
|
|
|
refreshLocalSyncId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method syncFolder
|
|
|
|
|
* @description 同步文件夹
|
|
|
|
|
* 执行流程:
|
|
|
|
|
* 1. 同步根文件夹
|
|
|
|
|
* 2. 同步通话记录文件夹
|
|
|
|
|
* 3. 同步现有的普通文件夹
|
|
|
|
|
* 4. 处理远程新增的文件夹
|
|
|
|
|
*/
|
|
|
|
|
private void syncFolder() throws NetworkFailureException {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
String gid;
|
|
|
|
|
Node node;
|
|
|
|
|
int syncType;
|
|
|
|
|
|
|
|
|
|
if (mCancelled) return;
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 同步根文件夹
|
|
|
|
|
// for root folder
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
Notes.ID_ROOT_FOLDER), SqlNote.PROJECTION_NOTE, null, null, null);
|
|
|
|
|
@ -419,7 +373,7 @@ public class GTaskManager {
|
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
|
mGidToNid.put(gid, (long) Notes.ID_ROOT_FOLDER);
|
|
|
|
|
mNidToGid.put((long) Notes.ID_ROOT_FOLDER, gid);
|
|
|
|
|
// 系统文件夹:只有在名称不同时才更新远程
|
|
|
|
|
// for system folder, only update remote name if necessary
|
|
|
|
|
if (!node.getName().equals(
|
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT))
|
|
|
|
|
doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c);
|
|
|
|
|
@ -436,11 +390,11 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 同步通话记录文件夹
|
|
|
|
|
// for call-note folder
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(_id=?)",
|
|
|
|
|
new String[] {
|
|
|
|
|
String.valueOf(Notes.ID_CALL_RECORD_FOLDER)
|
|
|
|
|
String.valueOf(Notes.ID_CALL_RECORD_FOLDER)
|
|
|
|
|
}, null);
|
|
|
|
|
if (c != null) {
|
|
|
|
|
if (c.moveToNext()) {
|
|
|
|
|
@ -450,9 +404,11 @@ public class GTaskManager {
|
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
|
mGidToNid.put(gid, (long) Notes.ID_CALL_RECORD_FOLDER);
|
|
|
|
|
mNidToGid.put((long) Notes.ID_CALL_RECORD_FOLDER, gid);
|
|
|
|
|
// 系统文件夹:只有在名称不同时才更新远程
|
|
|
|
|
// for system folder, only update remote name if
|
|
|
|
|
// necessary
|
|
|
|
|
if (!node.getName().equals(
|
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE))
|
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
|
+ GTaskStringUtils.FOLDER_CALL_NOTE))
|
|
|
|
|
doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c);
|
|
|
|
|
} else {
|
|
|
|
|
doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);
|
|
|
|
|
@ -468,7 +424,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 同步现有的普通文件夹
|
|
|
|
|
// for local existing folders
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
|
"(type=? AND parent_id<>?)", new String[] {
|
|
|
|
|
@ -485,10 +441,10 @@ public class GTaskManager {
|
|
|
|
|
syncType = node.getSyncAction(c);
|
|
|
|
|
} else {
|
|
|
|
|
if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
|
|
|
|
|
// 本地新增
|
|
|
|
|
// local add
|
|
|
|
|
syncType = Node.SYNC_ACTION_ADD_REMOTE;
|
|
|
|
|
} else {
|
|
|
|
|
// 远程删除
|
|
|
|
|
// remote delete
|
|
|
|
|
syncType = Node.SYNC_ACTION_DEL_LOCAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
@ -504,7 +460,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理远程新增的文件夹
|
|
|
|
|
// for remote add folders
|
|
|
|
|
Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
|
Map.Entry<String, TaskList> entry = iter.next();
|
|
|
|
|
@ -520,15 +476,10 @@ public class GTaskManager {
|
|
|
|
|
GTaskClient.getInstance().commitUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method doContentSync
|
|
|
|
|
* @description 根据同步类型执行相应的同步动作
|
|
|
|
|
* @param syncType 同步类型
|
|
|
|
|
* @param node Google Task节点
|
|
|
|
|
* @param c 数据库游标
|
|
|
|
|
*/
|
|
|
|
|
private void doContentSync(int syncType, Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) return;
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MetaData meta;
|
|
|
|
|
switch (syncType) {
|
|
|
|
|
@ -559,36 +510,25 @@ public class GTaskManager {
|
|
|
|
|
updateRemoteNode(node, c);
|
|
|
|
|
break;
|
|
|
|
|
case Node.SYNC_ACTION_UPDATE_CONFLICT:
|
|
|
|
|
// 冲突处理:简单策略,使用本地更新远程
|
|
|
|
|
// merging both modifications maybe a good idea
|
|
|
|
|
// right now just use local update simply
|
|
|
|
|
updateRemoteNode(node, c);
|
|
|
|
|
break;
|
|
|
|
|
case Node.SYNC_ACTION_NONE:
|
|
|
|
|
break;
|
|
|
|
|
case Node.SYNC_ACTION_ERROR:
|
|
|
|
|
default:
|
|
|
|
|
throw new ActionFailureException("unknown sync action type");
|
|
|
|
|
throw new ActionFailureException("unkown sync action type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method addLocalNode
|
|
|
|
|
* @description 在本地添加节点
|
|
|
|
|
* 执行流程:
|
|
|
|
|
* 1. 根据节点类型创建SqlNote对象
|
|
|
|
|
* 2. 处理可能存在的ID冲突
|
|
|
|
|
* 3. 设置父文件夹ID
|
|
|
|
|
* 4. 提交到本地数据库
|
|
|
|
|
* 5. 更新ID映射关系
|
|
|
|
|
* 6. 更新远程元数据
|
|
|
|
|
*
|
|
|
|
|
* @param node 要在本地添加的节点
|
|
|
|
|
*/
|
|
|
|
|
private void addLocalNode(Node node) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) return;
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqlNote sqlNote;
|
|
|
|
|
if (node instanceof TaskList) {
|
|
|
|
|
// 处理任务列表(文件夹)
|
|
|
|
|
if (node.getName().equals(
|
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT)) {
|
|
|
|
|
sqlNote = new SqlNote(mContext, Notes.ID_ROOT_FOLDER);
|
|
|
|
|
@ -601,23 +541,20 @@ public class GTaskManager {
|
|
|
|
|
sqlNote.setParentId(Notes.ID_ROOT_FOLDER);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 处理任务(便签)
|
|
|
|
|
sqlNote = new SqlNote(mContext);
|
|
|
|
|
JSONObject js = node.getLocalJSONFromContent();
|
|
|
|
|
try {
|
|
|
|
|
// 检查便签ID是否可用
|
|
|
|
|
if (js.has(GTaskStringUtils.META_HEAD_NOTE)) {
|
|
|
|
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
if (note.has(NoteColumns.ID)) {
|
|
|
|
|
long id = note.getLong(NoteColumns.ID);
|
|
|
|
|
if (DataUtils.existInNoteDatabase(mContentResolver, id)) {
|
|
|
|
|
// ID已被占用,移除ID让数据库自动生成新ID
|
|
|
|
|
// the id is not available, have to create a new one
|
|
|
|
|
note.remove(NoteColumns.ID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 检查数据ID是否可用
|
|
|
|
|
if (js.has(GTaskStringUtils.META_HEAD_DATA)) {
|
|
|
|
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
|
@ -625,11 +562,13 @@ public class GTaskManager {
|
|
|
|
|
if (data.has(DataColumns.ID)) {
|
|
|
|
|
long dataId = data.getLong(DataColumns.ID);
|
|
|
|
|
if (DataUtils.existInDataDatabase(mContentResolver, dataId)) {
|
|
|
|
|
// 数据ID已被占用,移除ID
|
|
|
|
|
// the data id is not available, have to create
|
|
|
|
|
// a new one
|
|
|
|
|
data.remove(DataColumns.ID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.w(TAG, e.toString());
|
|
|
|
|
@ -637,7 +576,6 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
sqlNote.setContent(js);
|
|
|
|
|
|
|
|
|
|
// 设置父文件夹ID
|
|
|
|
|
Long parentId = mGidToNid.get(((Task) node).getParent().getGid());
|
|
|
|
|
if (parentId == null) {
|
|
|
|
|
Log.e(TAG, "cannot find task's parent id locally");
|
|
|
|
|
@ -646,40 +584,28 @@ public class GTaskManager {
|
|
|
|
|
sqlNote.setParentId(parentId.longValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建本地节点
|
|
|
|
|
// create the local node
|
|
|
|
|
sqlNote.setGtaskId(node.getGid());
|
|
|
|
|
sqlNote.commit(false);
|
|
|
|
|
|
|
|
|
|
// 更新ID映射
|
|
|
|
|
// update gid-nid mapping
|
|
|
|
|
mGidToNid.put(node.getGid(), sqlNote.getId());
|
|
|
|
|
mNidToGid.put(sqlNote.getId(), node.getGid());
|
|
|
|
|
|
|
|
|
|
// 更新元数据
|
|
|
|
|
// update meta
|
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method updateLocalNode
|
|
|
|
|
* @description 更新本地节点(远程更新)
|
|
|
|
|
* 执行流程:
|
|
|
|
|
* 1. 从游标创建SqlNote对象
|
|
|
|
|
* 2. 设置新的内容
|
|
|
|
|
* 3. 更新父文件夹ID
|
|
|
|
|
* 4. 提交更新(带版本验证)
|
|
|
|
|
* 5. 更新远程元数据
|
|
|
|
|
*
|
|
|
|
|
* @param node 更新后的节点
|
|
|
|
|
* @param c 数据库游标,指向要更新的本地记录
|
|
|
|
|
*/
|
|
|
|
|
private void updateLocalNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) return;
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqlNote sqlNote;
|
|
|
|
|
// 更新本地便签
|
|
|
|
|
// update the note locally
|
|
|
|
|
sqlNote = new SqlNote(mContext, c);
|
|
|
|
|
sqlNote.setContent(node.getLocalJSONFromContent());
|
|
|
|
|
|
|
|
|
|
// 更新父文件夹ID
|
|
|
|
|
Long parentId = (node instanceof Task) ? mGidToNid.get(((Task) node).getParent().getGid())
|
|
|
|
|
: new Long(Notes.ID_ROOT_FOLDER);
|
|
|
|
|
if (parentId == null) {
|
|
|
|
|
@ -687,42 +613,25 @@ public class GTaskManager {
|
|
|
|
|
throw new ActionFailureException("cannot update local node");
|
|
|
|
|
}
|
|
|
|
|
sqlNote.setParentId(parentId.longValue());
|
|
|
|
|
|
|
|
|
|
// 提交更新(带版本验证)
|
|
|
|
|
sqlNote.commit(true);
|
|
|
|
|
|
|
|
|
|
// 更新元数据
|
|
|
|
|
// update meta info
|
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method addRemoteNode
|
|
|
|
|
* @description 在远程添加节点(本地新增)
|
|
|
|
|
*
|
|
|
|
|
* 执行流程:
|
|
|
|
|
* 1. 根据本地数据创建Google Task节点
|
|
|
|
|
* 2. 添加到相应的父任务列表
|
|
|
|
|
* 3. 在远程创建节点
|
|
|
|
|
* 4. 更新本地节点的GID
|
|
|
|
|
* 5. 更新ID映射关系
|
|
|
|
|
* 6. 重置本地修改标记
|
|
|
|
|
*
|
|
|
|
|
* @param node 要在远程添加的节点
|
|
|
|
|
* @param c 数据库游标,指向本地记录
|
|
|
|
|
*/
|
|
|
|
|
private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) return;
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqlNote sqlNote = new SqlNote(mContext, c);
|
|
|
|
|
Node n;
|
|
|
|
|
|
|
|
|
|
// 在远程创建节点
|
|
|
|
|
// update remotely
|
|
|
|
|
if (sqlNote.isNoteType()) {
|
|
|
|
|
// 创建任务(便签)
|
|
|
|
|
Task task = new Task();
|
|
|
|
|
task.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
|
|
|
|
|
|
// 查找父任务列表
|
|
|
|
|
String parentGid = mNidToGid.get(sqlNote.getParentId());
|
|
|
|
|
if (parentGid == null) {
|
|
|
|
|
Log.e(TAG, "cannot find task's parent tasklist");
|
|
|
|
|
@ -733,13 +642,12 @@ public class GTaskManager {
|
|
|
|
|
GTaskClient.getInstance().createTask(task);
|
|
|
|
|
n = (Node) task;
|
|
|
|
|
|
|
|
|
|
// 添加元数据
|
|
|
|
|
// add meta
|
|
|
|
|
updateRemoteMeta(task.getGid(), sqlNote);
|
|
|
|
|
} else {
|
|
|
|
|
// 创建任务列表(文件夹)
|
|
|
|
|
TaskList tasklist = null;
|
|
|
|
|
|
|
|
|
|
// 检查是否已存在同名文件夹
|
|
|
|
|
// we need to skip folder if it has already existed
|
|
|
|
|
String folderName = GTaskStringUtils.MIUI_FOLDER_PREFFIX;
|
|
|
|
|
if (sqlNote.getId() == Notes.ID_ROOT_FOLDER)
|
|
|
|
|
folderName += GTaskStringUtils.FOLDER_DEFAULT;
|
|
|
|
|
@ -748,7 +656,6 @@ public class GTaskManager {
|
|
|
|
|
else
|
|
|
|
|
folderName += sqlNote.getSnippet();
|
|
|
|
|
|
|
|
|
|
// 查找已存在的同名文件夹
|
|
|
|
|
Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
|
Map.Entry<String, TaskList> entry = iter.next();
|
|
|
|
|
@ -764,7 +671,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果没有匹配的文件夹,创建新的
|
|
|
|
|
// no match we can add now
|
|
|
|
|
if (tasklist == null) {
|
|
|
|
|
tasklist = new TaskList();
|
|
|
|
|
tasklist.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
|
@ -774,43 +681,32 @@ public class GTaskManager {
|
|
|
|
|
n = (Node) tasklist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新本地节点
|
|
|
|
|
// update local note
|
|
|
|
|
sqlNote.setGtaskId(n.getGid());
|
|
|
|
|
sqlNote.commit(false);
|
|
|
|
|
sqlNote.resetLocalModified(); // 重置本地修改标记
|
|
|
|
|
sqlNote.resetLocalModified();
|
|
|
|
|
sqlNote.commit(true);
|
|
|
|
|
|
|
|
|
|
// 更新ID映射
|
|
|
|
|
// gid-id mapping
|
|
|
|
|
mGidToNid.put(n.getGid(), sqlNote.getId());
|
|
|
|
|
mNidToGid.put(sqlNote.getId(), n.getGid());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method updateRemoteNode
|
|
|
|
|
* @description 更新远程节点(本地更新)
|
|
|
|
|
*
|
|
|
|
|
* 执行流程:
|
|
|
|
|
* 1. 更新远程节点内容
|
|
|
|
|
* 2. 更新远程元数据
|
|
|
|
|
* 3. 如果需要,移动任务到其他任务列表
|
|
|
|
|
* 4. 重置本地修改标记
|
|
|
|
|
*
|
|
|
|
|
* @param node 要更新的节点
|
|
|
|
|
* @param c 数据库游标,指向本地记录
|
|
|
|
|
*/
|
|
|
|
|
private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) return;
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqlNote sqlNote = new SqlNote(mContext, c);
|
|
|
|
|
|
|
|
|
|
// 更新远程节点
|
|
|
|
|
// update remotely
|
|
|
|
|
node.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
|
GTaskClient.getInstance().addUpdateNode(node);
|
|
|
|
|
|
|
|
|
|
// 更新元数据
|
|
|
|
|
// update meta
|
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
|
|
|
|
|
|
// 如果便签被移动到其他文件夹,处理移动操作
|
|
|
|
|
// move task if necessary
|
|
|
|
|
if (sqlNote.isNoteType()) {
|
|
|
|
|
Task task = (Task) node;
|
|
|
|
|
TaskList preParentList = task.getParent();
|
|
|
|
|
@ -829,30 +725,18 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重置本地修改标记
|
|
|
|
|
// clear local modified flag
|
|
|
|
|
sqlNote.resetLocalModified();
|
|
|
|
|
sqlNote.commit(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method updateRemoteMeta
|
|
|
|
|
* @description 更新远程元数据
|
|
|
|
|
*
|
|
|
|
|
* 功能:为任务创建或更新关联的元数据
|
|
|
|
|
* 元数据包含任务ID和便签内容的关联信息
|
|
|
|
|
*
|
|
|
|
|
* @param gid 任务的Google Task ID
|
|
|
|
|
* @param sqlNote 本地便签对象
|
|
|
|
|
*/
|
|
|
|
|
private void updateRemoteMeta(String gid, SqlNote sqlNote) throws NetworkFailureException {
|
|
|
|
|
if (sqlNote != null && sqlNote.isNoteType()) {
|
|
|
|
|
MetaData metaData = mMetaHashMap.get(gid);
|
|
|
|
|
if (metaData != null) {
|
|
|
|
|
// 更新现有元数据
|
|
|
|
|
metaData.setMeta(gid, sqlNote.getContent());
|
|
|
|
|
GTaskClient.getInstance().addUpdateNode(metaData);
|
|
|
|
|
} else {
|
|
|
|
|
// 创建新的元数据
|
|
|
|
|
metaData = new MetaData();
|
|
|
|
|
metaData.setMeta(gid, sqlNote.getContent());
|
|
|
|
|
mMetaList.addChildTask(metaData);
|
|
|
|
|
@ -862,18 +746,17 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//同步完成后,更新本地便签的同步ID
|
|
|
|
|
private void refreshLocalSyncId() throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) return;
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 重新获取最新的任务列表
|
|
|
|
|
// get the latest gtask list
|
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
|
initGTaskList();
|
|
|
|
|
|
|
|
|
|
// 更新本地同步ID
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
|
@ -907,13 +790,11 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//获取同步账户名称
|
|
|
|
|
public String getSyncAccount() {
|
|
|
|
|
return GTaskClient.getInstance().getSyncAccount().name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void cancelSync() {
|
|
|
|
|
mCancelled = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|