|
|
|
@ -48,45 +48,34 @@ import java.util.Iterator;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Google任务管理器类,负责同步逻辑
|
|
|
|
|
public class GTaskManager {
|
|
|
|
|
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_SYNC_IN_PROGRESS = 3;
|
|
|
|
|
|
|
|
|
|
public static final int STATE_SYNC_CANCELLED = 4;
|
|
|
|
|
|
|
|
|
|
private static GTaskManager mInstance = null;
|
|
|
|
|
|
|
|
|
|
private Activity mActivity;
|
|
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
|
|
private ContentResolver mContentResolver;
|
|
|
|
|
|
|
|
|
|
private boolean mSyncing;
|
|
|
|
|
|
|
|
|
|
private boolean mCancelled;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 同步状态常量
|
|
|
|
|
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; // 同步已取消
|
|
|
|
|
|
|
|
|
|
private static GTaskManager mInstance = null; // 单例实例
|
|
|
|
|
private Activity mActivity; // Activity上下文(用于获取认证token)
|
|
|
|
|
private Context mContext; // 应用上下文
|
|
|
|
|
private ContentResolver mContentResolver; // 内容解析器
|
|
|
|
|
private boolean mSyncing; // 同步状态标志
|
|
|
|
|
private boolean mCancelled; // 取消同步标志
|
|
|
|
|
|
|
|
|
|
// 数据存储相关HashMap
|
|
|
|
|
private HashMap<String, TaskList> mGTaskListHashMap; // 任务列表HashMap
|
|
|
|
|
private HashMap<String, Node> mGTaskHashMap; // 节点HashMap
|
|
|
|
|
private HashMap<String, MetaData> mMetaHashMap; // 元数据HashMap
|
|
|
|
|
private TaskList mMetaList; // 元数据列表
|
|
|
|
|
private HashSet<Long> mLocalDeleteIdMap; // 本地删除ID集合
|
|
|
|
|
private HashMap<String, Long> mGidToNid; // Google ID到本地ID的映射
|
|
|
|
|
private HashMap<Long, String> mNidToGid; // 本地ID到Google ID的映射
|
|
|
|
|
|
|
|
|
|
// 私有构造函数
|
|
|
|
|
private GTaskManager() {
|
|
|
|
|
mSyncing = false;
|
|
|
|
|
mCancelled = false;
|
|
|
|
@ -99,6 +88,7 @@ public class GTaskManager {
|
|
|
|
|
mNidToGid = new HashMap<Long, String>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取单例实例
|
|
|
|
|
public static synchronized GTaskManager getInstance() {
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new GTaskManager();
|
|
|
|
@ -106,11 +96,13 @@ public class GTaskManager {
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置Activity上下文
|
|
|
|
|
public synchronized void setActivityContext(Activity activity) {
|
|
|
|
|
// used for getting authtoken
|
|
|
|
|
// 用于获取认证token
|
|
|
|
|
mActivity = activity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 执行同步操作
|
|
|
|
|
public int sync(Context context, GTaskASyncTask asyncTask) {
|
|
|
|
|
if (mSyncing) {
|
|
|
|
|
Log.d(TAG, "Sync is in progress");
|
|
|
|
@ -120,6 +112,7 @@ public class GTaskManager {
|
|
|
|
|
mContentResolver = mContext.getContentResolver();
|
|
|
|
|
mSyncing = true;
|
|
|
|
|
mCancelled = false;
|
|
|
|
|
// 清空所有临时数据存储
|
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
@ -131,18 +124,18 @@ public class GTaskManager {
|
|
|
|
|
GTaskClient client = GTaskClient.getInstance();
|
|
|
|
|
client.resetUpdateArray();
|
|
|
|
|
|
|
|
|
|
// login google task
|
|
|
|
|
// 1. 登录Google任务服务
|
|
|
|
|
if (!mCancelled) {
|
|
|
|
|
if (!client.login(mActivity)) {
|
|
|
|
|
throw new NetworkFailureException("login google task failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the task list from google
|
|
|
|
|
// 2. 初始化Google任务列表
|
|
|
|
|
asyncTask.publishProgess(mContext.getString(R.string.sync_progress_init_list));
|
|
|
|
|
initGTaskList();
|
|
|
|
|
|
|
|
|
|
// do content sync work
|
|
|
|
|
// 3. 同步内容
|
|
|
|
|
asyncTask.publishProgess(mContext.getString(R.string.sync_progress_syncing));
|
|
|
|
|
syncContent();
|
|
|
|
|
} catch (NetworkFailureException e) {
|
|
|
|
@ -156,6 +149,7 @@ public class GTaskManager {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return STATE_INTERNAL_ERROR;
|
|
|
|
|
} finally {
|
|
|
|
|
// 清理临时数据
|
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
@ -168,6 +162,7 @@ public class GTaskManager {
|
|
|
|
|
return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化Google任务列表
|
|
|
|
|
private void initGTaskList() throws NetworkFailureException {
|
|
|
|
|
if (mCancelled)
|
|
|
|
|
return;
|
|
|
|
@ -175,7 +170,7 @@ public class GTaskManager {
|
|
|
|
|
try {
|
|
|
|
|
JSONArray jsTaskLists = client.getTaskLists();
|
|
|
|
|
|
|
|
|
|
// init meta list first
|
|
|
|
|
// 1. 首先初始化元数据列表
|
|
|
|
|
mMetaList = null;
|
|
|
|
|
for (int i = 0; i < jsTaskLists.length(); i++) {
|
|
|
|
|
JSONObject object = jsTaskLists.getJSONObject(i);
|
|
|
|
@ -187,7 +182,7 @@ public class GTaskManager {
|
|
|
|
|
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);
|
|
|
|
@ -203,7 +198,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create meta list if not existed
|
|
|
|
|
// 2. 如果元数据列表不存在,则创建
|
|
|
|
|
if (mMetaList == null) {
|
|
|
|
|
mMetaList = new TaskList();
|
|
|
|
|
mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
@ -211,7 +206,7 @@ public class GTaskManager {
|
|
|
|
|
GTaskClient.getInstance().createTaskList(mMetaList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// init task list
|
|
|
|
|
// 3. 初始化其他任务列表
|
|
|
|
|
for (int i = 0; i < jsTaskLists.length(); i++) {
|
|
|
|
|
JSONObject object = jsTaskLists.getJSONObject(i);
|
|
|
|
|
String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
|
|
|
|
@ -225,7 +220,7 @@ public class GTaskManager {
|
|
|
|
|
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);
|
|
|
|
@ -247,6 +242,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 同步内容
|
|
|
|
|
private void syncContent() throws NetworkFailureException {
|
|
|
|
|
int syncType;
|
|
|
|
|
Cursor c = null;
|
|
|
|
@ -259,7 +255,7 @@ public class GTaskManager {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for local deleted note
|
|
|
|
|
// 1. 同步本地已删除的笔记
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
|
"(type<>? AND parent_id=?)", new String[] {
|
|
|
|
@ -286,10 +282,10 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sync folder first
|
|
|
|
|
// 2. 首先同步文件夹
|
|
|
|
|
syncFolder();
|
|
|
|
|
|
|
|
|
|
// for note existing in database
|
|
|
|
|
// 3. 同步数据库中存在的笔记
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
|
"(type=? AND parent_id<>?)", new String[] {
|
|
|
|
@ -306,10 +302,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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -326,7 +322,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// go through remaining items
|
|
|
|
|
// 4. 处理剩余的远程新增项
|
|
|
|
|
Iterator<Map.Entry<String, Node>> iter = mGTaskHashMap.entrySet().iterator();
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
|
Map.Entry<String, Node> entry = iter.next();
|
|
|
|
@ -334,23 +330,21 @@ 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
|
|
|
|
|
// 5. 清除本地删除表
|
|
|
|
|
if (!mCancelled) {
|
|
|
|
|
if (!DataUtils.batchDeleteNotes(mContentResolver, mLocalDeleteIdMap)) {
|
|
|
|
|
throw new ActionFailureException("failed to batch-delete local deleted notes");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// refresh local sync id
|
|
|
|
|
// 6. 刷新本地同步ID
|
|
|
|
|
if (!mCancelled) {
|
|
|
|
|
GTaskClient.getInstance().commitUpdate();
|
|
|
|
|
refreshLocalSyncId();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 同步文件夹
|
|
|
|
|
private void syncFolder() throws NetworkFailureException {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
String gid;
|
|
|
|
@ -361,7 +355,7 @@ public class GTaskManager {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for root folder
|
|
|
|
|
// 1. 同步根文件夹
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
Notes.ID_ROOT_FOLDER), SqlNote.PROJECTION_NOTE, null, null, null);
|
|
|
|
@ -373,7 +367,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);
|
|
|
|
@ -390,7 +384,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for call-note folder
|
|
|
|
|
// 2. 同步通话记录文件夹
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(_id=?)",
|
|
|
|
|
new String[] {
|
|
|
|
@ -404,8 +398,7 @@ 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))
|
|
|
|
@ -424,7 +417,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for local existing folders
|
|
|
|
|
// 3. 同步本地存在的文件夹
|
|
|
|
|
try {
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
|
"(type=? AND parent_id<>?)", new String[] {
|
|
|
|
@ -441,10 +434,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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -460,7 +453,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for remote add folders
|
|
|
|
|
// 4. 处理远程新增的文件夹
|
|
|
|
|
Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
|
Map.Entry<String, TaskList> entry = iter.next();
|
|
|
|
@ -476,6 +469,7 @@ public class GTaskManager {
|
|
|
|
|
GTaskClient.getInstance().commitUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 执行内容同步操作
|
|
|
|
|
private void doContentSync(int syncType, Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
@ -510,8 +504,8 @@ 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:
|
|
|
|
@ -522,6 +516,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加本地节点
|
|
|
|
|
private void addLocalNode(Node node) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
@ -529,6 +524,7 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
@ -541,15 +537,17 @@ public class GTaskManager {
|
|
|
|
|
sqlNote.setParentId(Notes.ID_ROOT_FOLDER);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 处理笔记
|
|
|
|
|
sqlNote = new SqlNote(mContext);
|
|
|
|
|
JSONObject js = node.getLocalJSONFromContent();
|
|
|
|
|
try {
|
|
|
|
|
// 处理元数据
|
|
|
|
|
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)) {
|
|
|
|
|
// the id is not available, have to create a new one
|
|
|
|
|
// ID不可用,需要创建新的
|
|
|
|
|
note.remove(NoteColumns.ID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -562,13 +560,11 @@ public class GTaskManager {
|
|
|
|
|
if (data.has(DataColumns.ID)) {
|
|
|
|
|
long dataId = data.getLong(DataColumns.ID);
|
|
|
|
|
if (DataUtils.existInDataDatabase(mContentResolver, dataId)) {
|
|
|
|
|
// the data id is not available, have to create
|
|
|
|
|
// a new one
|
|
|
|
|
// 数据ID不可用,需要创建新的
|
|
|
|
|
data.remove(DataColumns.ID);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
Log.w(TAG, e.toString());
|
|
|
|
@ -576,6 +572,7 @@ 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");
|
|
|
|
@ -584,28 +581,30 @@ public class GTaskManager {
|
|
|
|
|
sqlNote.setParentId(parentId.longValue());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create the local node
|
|
|
|
|
// 创建本地节点
|
|
|
|
|
sqlNote.setGtaskId(node.getGid());
|
|
|
|
|
sqlNote.commit(false);
|
|
|
|
|
|
|
|
|
|
// update gid-nid mapping
|
|
|
|
|
// 更新ID映射
|
|
|
|
|
mGidToNid.put(node.getGid(), sqlNote.getId());
|
|
|
|
|
mNidToGid.put(sqlNote.getId(), node.getGid());
|
|
|
|
|
|
|
|
|
|
// update meta
|
|
|
|
|
// 更新元数据
|
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新本地节点
|
|
|
|
|
private void updateLocalNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
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) {
|
|
|
|
@ -615,10 +614,11 @@ public class GTaskManager {
|
|
|
|
|
sqlNote.setParentId(parentId.longValue());
|
|
|
|
|
sqlNote.commit(true);
|
|
|
|
|
|
|
|
|
|
// update meta info
|
|
|
|
|
// 更新元数据
|
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加远程节点
|
|
|
|
|
private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
@ -627,11 +627,13 @@ public class GTaskManager {
|
|
|
|
|
SqlNote sqlNote = new SqlNote(mContext, c);
|
|
|
|
|
Node n;
|
|
|
|
|
|
|
|
|
|
// update remotely
|
|
|
|
|
// 远程更新
|
|
|
|
|
if (sqlNote.isNoteType()) {
|
|
|
|
|
// 处理笔记
|
|
|
|
|
Task task = new Task();
|
|
|
|
|
task.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
|
|
|
|
|
|
// 获取父文件夹ID
|
|
|
|
|
String parentGid = mNidToGid.get(sqlNote.getParentId());
|
|
|
|
|
if (parentGid == null) {
|
|
|
|
|
Log.e(TAG, "cannot find task's parent tasklist");
|
|
|
|
@ -639,15 +641,17 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
mGTaskListHashMap.get(parentGid).addChildTask(task);
|
|
|
|
|
|
|
|
|
|
// 创建远程任务
|
|
|
|
|
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;
|
|
|
|
@ -656,6 +660,7 @@ 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();
|
|
|
|
@ -671,7 +676,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// no match we can add now
|
|
|
|
|
// 没有匹配项则创建新文件夹
|
|
|
|
|
if (tasklist == null) {
|
|
|
|
|
tasklist = new TaskList();
|
|
|
|
|
tasklist.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
@ -681,17 +686,18 @@ public class GTaskManager {
|
|
|
|
|
n = (Node) tasklist;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update local note
|
|
|
|
|
// 更新本地笔记
|
|
|
|
|
sqlNote.setGtaskId(n.getGid());
|
|
|
|
|
sqlNote.commit(false);
|
|
|
|
|
sqlNote.resetLocalModified();
|
|
|
|
|
sqlNote.commit(true);
|
|
|
|
|
|
|
|
|
|
// gid-id mapping
|
|
|
|
|
// 更新ID映射
|
|
|
|
|
mGidToNid.put(n.getGid(), sqlNote.getId());
|
|
|
|
|
mNidToGid.put(sqlNote.getId(), n.getGid());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新远程节点
|
|
|
|
|
private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
@ -699,18 +705,19 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
// 获取当前父文件夹ID
|
|
|
|
|
String curParentGid = mNidToGid.get(sqlNote.getParentId());
|
|
|
|
|
if (curParentGid == null) {
|
|
|
|
|
Log.e(TAG, "cannot find task's parent tasklist");
|
|
|
|
@ -718,6 +725,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
TaskList curParentList = mGTaskListHashMap.get(curParentGid);
|
|
|
|
|
|
|
|
|
|
// 如果父文件夹发生变化,移动任务
|
|
|
|
|
if (preParentList != curParentList) {
|
|
|
|
|
preParentList.removeChildTask(task);
|
|
|
|
|
curParentList.addChildTask(task);
|
|
|
|
@ -725,18 +733,21 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// clear local modified flag
|
|
|
|
|
// 清除本地修改标志
|
|
|
|
|
sqlNote.resetLocalModified();
|
|
|
|
|
sqlNote.commit(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新远程元数据
|
|
|
|
|
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);
|
|
|
|
@ -746,12 +757,13 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 刷新本地同步ID
|
|
|
|
|
private void refreshLocalSyncId() throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the latest gtask list
|
|
|
|
|
// 重新获取Google任务列表
|
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
@ -759,6 +771,7 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
try {
|
|
|
|
|
// 查询所有非系统笔记
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
|
"(type<>? AND parent_id<>?)", new String[] {
|
|
|
|
|
String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER)
|
|
|
|
@ -769,6 +782,7 @@ public class GTaskManager {
|
|
|
|
|
Node node = mGTaskHashMap.get(gid);
|
|
|
|
|
if (node != null) {
|
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
|
// 更新同步时间戳
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.SYNC_ID, node.getLastModified());
|
|
|
|
|
mContentResolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI,
|
|
|
|
@ -790,10 +804,12 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前同步账户
|
|
|
|
|
public String getSyncAccount() {
|
|
|
|
|
return GTaskClient.getInstance().getSyncAccount().name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 取消同步
|
|
|
|
|
public void cancelSync() {
|
|
|
|
|
mCancelled = true;
|
|
|
|
|
}
|
|
|
|
|