|
|
|
@ -14,6 +14,8 @@
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 实现同步功能的主函数
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.gtask.remote;
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
@ -47,18 +49,20 @@ import java.util.HashSet;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 【1】定义:GTask管理类
|
|
|
|
|
* 【2】功能:提供同步本地和远端的任务,初始化任务列表,同步内容、文件夹,添加、更新本地和远端结点,刷新本地同步任务ID等功能
|
|
|
|
|
*/
|
|
|
|
|
public class GTaskManager {
|
|
|
|
|
// 定义了一系列静态变量来显示GTask当前的状态
|
|
|
|
|
//(1)设置GTask的TAG
|
|
|
|
|
private static final String TAG = GTaskManager.class.getSimpleName();
|
|
|
|
|
|
|
|
|
|
//(2)进程状态:用0、1、2、3、4分别表示成功、网络错误、内部错误、同步中、取消同步
|
|
|
|
|
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;
|
|
|
|
@ -87,18 +91,32 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
private HashMap<Long, String> mNidToGid;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 类的构造函数,对其内部变量进行初始化
|
|
|
|
|
*/
|
|
|
|
|
private GTaskManager() {
|
|
|
|
|
// 正在同步标识,false代表未同步
|
|
|
|
|
mSyncing = false;
|
|
|
|
|
// 取消同步标识,false代表可以取消
|
|
|
|
|
mCancelled = false;
|
|
|
|
|
// 任务列表以哈希表的形式创立
|
|
|
|
|
mGTaskListHashMap = new HashMap<String, TaskList>();
|
|
|
|
|
// 创建一个节点表
|
|
|
|
|
mGTaskHashMap = new HashMap<String, Node>();
|
|
|
|
|
// 创建一个删除本地ID的map
|
|
|
|
|
mMetaHashMap = new HashMap<String, MetaData>();
|
|
|
|
|
mMetaList = null;
|
|
|
|
|
mLocalDeleteIdMap = new HashSet<Long>();
|
|
|
|
|
// 创建Gid(google id)到Nid(节点 id)的映射
|
|
|
|
|
mGidToNid = new HashMap<String, Long>();
|
|
|
|
|
// 与mGidToNid相反的映射
|
|
|
|
|
mNidToGid = new HashMap<Long, String>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取一个实例,通过GTaskManager()新建一个mInstance
|
|
|
|
|
*(注:synchronized指明该函数可以运行在多线程下)
|
|
|
|
|
*/
|
|
|
|
|
public static synchronized GTaskManager getInstance() {
|
|
|
|
|
if (mInstance == null) {
|
|
|
|
|
mInstance = new GTaskManager();
|
|
|
|
@ -106,20 +124,29 @@ public class GTaskManager {
|
|
|
|
|
return mInstance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取当前的操作并更新至GTask中
|
|
|
|
|
*/
|
|
|
|
|
public synchronized void setActivityContext(Activity activity) {
|
|
|
|
|
// used for getting authtoken
|
|
|
|
|
mActivity = activity;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:实现同步操作,包括同步前设置环境,进行同步,处理异常,同步结束清空缓存
|
|
|
|
|
*/
|
|
|
|
|
public int sync(Context context, GTaskASyncTask asyncTask) {
|
|
|
|
|
// 当前状态是正在同步中
|
|
|
|
|
if (mSyncing) {
|
|
|
|
|
Log.d(TAG, "Sync is in progress");
|
|
|
|
|
return STATE_SYNC_IN_PROGRESS;
|
|
|
|
|
Log.d(TAG, "Sync is in progress"); // 声明同步正在运行
|
|
|
|
|
return STATE_SYNC_IN_PROGRESS; // 返回同步状态
|
|
|
|
|
}
|
|
|
|
|
// 对同步时GTaskManager的属性进行更新
|
|
|
|
|
mContext = context;
|
|
|
|
|
mContentResolver = mContext.getContentResolver();
|
|
|
|
|
mSyncing = true;
|
|
|
|
|
mCancelled = false;
|
|
|
|
|
// 对各种结构进行清空
|
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
@ -127,35 +154,42 @@ public class GTaskManager {
|
|
|
|
|
mGidToNid.clear();
|
|
|
|
|
mNidToGid.clear();
|
|
|
|
|
|
|
|
|
|
// 异常处理程序
|
|
|
|
|
try {
|
|
|
|
|
// 实例化一个GTask用户对象
|
|
|
|
|
GTaskClient client = GTaskClient.getInstance();
|
|
|
|
|
// 重置更新数组
|
|
|
|
|
client.resetUpdateArray();
|
|
|
|
|
|
|
|
|
|
// login google task
|
|
|
|
|
// 若此时未取消同步操作,进行登录操作,尝试登录到google task
|
|
|
|
|
if (!mCancelled) {
|
|
|
|
|
// 如果mActivity登录不上则抛出一个异常
|
|
|
|
|
if (!client.login(mActivity)) {
|
|
|
|
|
throw new NetworkFailureException("login google task failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the task list from google
|
|
|
|
|
// 从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) {
|
|
|
|
|
} catch (NetworkFailureException e) { // 网络异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
return STATE_NETWORK_ERROR;
|
|
|
|
|
} catch (ActionFailureException e) {
|
|
|
|
|
} catch (ActionFailureException e) { //操作未完成
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
return STATE_INTERNAL_ERROR;
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
} catch (Exception e) { //内部错误
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return STATE_INTERNAL_ERROR;
|
|
|
|
|
} finally {
|
|
|
|
|
} finally { //在同步操作结束后,更新GTaskManager的属性
|
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
@ -165,37 +199,53 @@ public class GTaskManager {
|
|
|
|
|
mSyncing = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 若在同步时操作未取消,则返回同步成功,否则返回同步操作取消
|
|
|
|
|
return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:初始化GTask列表,将google上的JSONTaskList转为本地任务列表
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
*/
|
|
|
|
|
private void initGTaskList() throws NetworkFailureException {
|
|
|
|
|
// 判断是否取消该次操作
|
|
|
|
|
if (mCancelled)
|
|
|
|
|
return;
|
|
|
|
|
// 实例化一个GTask用户对象
|
|
|
|
|
GTaskClient client = GTaskClient.getInstance();
|
|
|
|
|
try {
|
|
|
|
|
// 客户端获取任务列表jsTaskLists
|
|
|
|
|
JSONArray jsTaskLists = client.getTaskLists();
|
|
|
|
|
|
|
|
|
|
// init meta list first
|
|
|
|
|
// 初始化元数据列表
|
|
|
|
|
mMetaList = null;
|
|
|
|
|
// 不断把任务列表里的每个任务通过远程JSON设置内容并把元数据都放到哈希表中
|
|
|
|
|
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);
|
|
|
|
|
JSONObject object = jsTaskLists.getJSONObject(i); // 取出单个JSON对象
|
|
|
|
|
String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID); // 获取它的id
|
|
|
|
|
String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME); // 获取它的名字
|
|
|
|
|
|
|
|
|
|
// 新建数组,并为新建的数组设定内容
|
|
|
|
|
if (name
|
|
|
|
|
.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
|
|
|
|
|
mMetaList = new TaskList();
|
|
|
|
|
mMetaList.setContentByRemoteJSON(object);
|
|
|
|
|
|
|
|
|
|
// load meta data
|
|
|
|
|
// 获取元数据
|
|
|
|
|
JSONArray jsMetas = client.getTaskList(gid);
|
|
|
|
|
// 把jsMetas里的每一个有识别码的metaData都放到哈希表中
|
|
|
|
|
for (int j = 0; j < jsMetas.length(); j++) {
|
|
|
|
|
object = (JSONObject) jsMetas.getJSONObject(j);
|
|
|
|
|
MetaData metaData = new MetaData();
|
|
|
|
|
metaData.setContentByRemoteJSON(object);
|
|
|
|
|
// 判断前面获取的元数据有无价值加入列表中
|
|
|
|
|
if (metaData.isWorthSaving()) {
|
|
|
|
|
mMetaList.addChildTask(metaData);
|
|
|
|
|
// 操作getGid:取得组识别码函数
|
|
|
|
|
if (metaData.getGid() != null) {
|
|
|
|
|
// 把元数据放到哈希表中
|
|
|
|
|
mMetaHashMap.put(metaData.getRelatedGid(), metaData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -204,6 +254,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create meta list if not existed
|
|
|
|
|
// 如果元数据列表不存在,则在客户端创建一个
|
|
|
|
|
if (mMetaList == null) {
|
|
|
|
|
mMetaList = new TaskList();
|
|
|
|
|
mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
@ -212,7 +263,9 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
@ -221,11 +274,13 @@ public class GTaskManager {
|
|
|
|
|
&& !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
|
|
|
|
|
// 获取任务id号
|
|
|
|
|
JSONArray jsTasks = client.getTaskList(gid);
|
|
|
|
|
for (int j = 0; j < jsTasks.length(); j++) {
|
|
|
|
|
object = (JSONObject) jsTasks.getJSONObject(j);
|
|
|
|
@ -240,46 +295,60 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
} catch (JSONException e) { // 初始化时捕捉异常
|
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
// 抛出异常,提交 JSONObject 失败
|
|
|
|
|
throw new ActionFailureException("initGTaskList: handing JSONObject failed");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:实现内容同步的操作
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
*/
|
|
|
|
|
private void syncContent() throws NetworkFailureException {
|
|
|
|
|
int syncType;
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
String gid;
|
|
|
|
|
Node node;
|
|
|
|
|
|
|
|
|
|
// 初始化本地删除列表
|
|
|
|
|
mLocalDeleteIdMap.clear();
|
|
|
|
|
|
|
|
|
|
// 判断该操作是否已被取消
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for local deleted note
|
|
|
|
|
// 对于删除本地便签的操作的同步
|
|
|
|
|
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)
|
|
|
|
|
}, null);
|
|
|
|
|
// 若获取到的待删除便签不为空,则进行同步操作
|
|
|
|
|
if (c != null) {
|
|
|
|
|
// 方法:如果节点位置不为空,用while循环把节点一个一个的往后移动,并不多更新gid和哈希表,直到最后移动到列表尾不能再移动,把c指向的note放到本地删除节点ID的图里。
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
gid = c.getString(SqlNote.GTASK_ID_COLUMN);
|
|
|
|
|
node = mGTaskHashMap.get(gid);
|
|
|
|
|
if (node != null) {
|
|
|
|
|
// 将待删除的节点对应的google id从映射表中移除
|
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
|
// 在远程删除对应节点
|
|
|
|
|
doContentSync(Node.SYNC_ACTION_DEL_REMOTE, node, c);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在本地删除记录中添加这一项记录
|
|
|
|
|
mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
} else { // 或者发现没有待删除的节点,报错
|
|
|
|
|
Log.w(TAG, "failed to query trash folder");
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
} finally { // 最后把c关闭并重置
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
c = null;
|
|
|
|
@ -287,10 +356,13 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// sync folder first
|
|
|
|
|
// 对文件夹进行同步
|
|
|
|
|
syncFolder();
|
|
|
|
|
|
|
|
|
|
// for note existing in database
|
|
|
|
|
// 对已经存在与数据库的节点进行同步
|
|
|
|
|
try {
|
|
|
|
|
// c指针指向待操作的便签位置
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
|
"(type=? AND parent_id<>?)", new String[] {
|
|
|
|
|
String.valueOf(Notes.TYPE_NOTE), String.valueOf(Notes.ID_TRASH_FOLER)
|
|
|
|
@ -301,25 +373,31 @@ public class GTaskManager {
|
|
|
|
|
node = mGTaskHashMap.get(gid);
|
|
|
|
|
if (node != null) {
|
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
|
// 建立google id到节点id的映射
|
|
|
|
|
mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));
|
|
|
|
|
// 建立节点id到google id的映射
|
|
|
|
|
mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
|
|
|
|
|
// 更新此时的同步类型
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
doContentSync(syncType, node, c);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 警告,询问数据库中已创建的便签失败
|
|
|
|
|
Log.w(TAG, "failed to query existing note in database");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
} finally { // 最后关闭c并重置
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
c = null;
|
|
|
|
@ -327,16 +405,19 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// go through remaining items
|
|
|
|
|
// 用迭代器扫描剩下的项目,逐个进行同步
|
|
|
|
|
Iterator<Map.Entry<String, Node>> iter = mGTaskHashMap.entrySet().iterator();
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
|
Map.Entry<String, Node> entry = iter.next();
|
|
|
|
|
node = entry.getValue();
|
|
|
|
|
// 在本地增加这些节点
|
|
|
|
|
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");
|
|
|
|
@ -344,6 +425,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// refresh local sync id
|
|
|
|
|
// 更新同步表
|
|
|
|
|
if (!mCancelled) {
|
|
|
|
|
GTaskClient.getInstance().commitUpdate();
|
|
|
|
|
refreshLocalSyncId();
|
|
|
|
@ -351,6 +433,10 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:同步文件夹
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
*/
|
|
|
|
|
private void syncFolder() throws NetworkFailureException {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
String gid;
|
|
|
|
@ -363,27 +449,32 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
// for root folder
|
|
|
|
|
try {
|
|
|
|
|
// 使指针指向根文件夹的位置
|
|
|
|
|
c = mContentResolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI,
|
|
|
|
|
Notes.ID_ROOT_FOLDER), SqlNote.PROJECTION_NOTE, null, null, null);
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.moveToNext();
|
|
|
|
|
// 获取指针指向内容对应的gid
|
|
|
|
|
gid = c.getString(SqlNote.GTASK_ID_COLUMN);
|
|
|
|
|
// 获取该gid所代表的节点
|
|
|
|
|
node = mGTaskHashMap.get(gid);
|
|
|
|
|
if (node != null) {
|
|
|
|
|
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);
|
|
|
|
|
} else {
|
|
|
|
|
} else { // 若非系统文件夹则在远程进行增加节点操作
|
|
|
|
|
doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
} else { // 警告,询问根目录文件夹失败
|
|
|
|
|
Log.w(TAG, "failed to query root folder");
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
} finally { // 结束操作之后,将指针指向内容关闭并将指针置空
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
c = null;
|
|
|
|
@ -391,8 +482,11 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for call-note folder
|
|
|
|
|
// 对存放电话号码便签的文件夹的同步操作
|
|
|
|
|
try {
|
|
|
|
|
// 使指针指向文件夹的位置
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(_id=?)",
|
|
|
|
|
// 添加MIUI文件前缀
|
|
|
|
|
new String[] {
|
|
|
|
|
String.valueOf(Notes.ID_CALL_RECORD_FOLDER)
|
|
|
|
|
}, null);
|
|
|
|
@ -406,18 +500,19 @@ public class GTaskManager {
|
|
|
|
|
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))
|
|
|
|
|
doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c);
|
|
|
|
|
} else {
|
|
|
|
|
} else { // 若非系统文件夹则在远程进行增加节点操作
|
|
|
|
|
doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
} else { // 警告,询问通讯便签文件夹失败
|
|
|
|
|
Log.w(TAG, "failed to query call note folder");
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
} finally { // 结束操作之后,将指针指向内容关闭并将指针置空
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
c = null;
|
|
|
|
@ -425,12 +520,15 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// for local existing folders
|
|
|
|
|
// 对于本地已存在的文件的操作
|
|
|
|
|
try {
|
|
|
|
|
// 使指针指向第一个文件夹的位置
|
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
|
"(type=? AND parent_id<>?)", new String[] {
|
|
|
|
|
String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)
|
|
|
|
|
}, NoteColumns.TYPE + " DESC");
|
|
|
|
|
if (c != null) {
|
|
|
|
|
// 使指针遍历所有的文件夹
|
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
|
gid = c.getString(SqlNote.GTASK_ID_COLUMN);
|
|
|
|
|
node = mGTaskHashMap.get(gid);
|
|
|
|
@ -438,19 +536,21 @@ public class GTaskManager {
|
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
|
mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));
|
|
|
|
|
mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
|
|
|
|
|
// 更新同步类型
|
|
|
|
|
syncType = node.getSyncAction(c);
|
|
|
|
|
} else {
|
|
|
|
|
} else { // 若本地增加了内容,则远程也要增加内容
|
|
|
|
|
if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
|
|
|
|
|
// local add
|
|
|
|
|
syncType = Node.SYNC_ACTION_ADD_REMOTE;
|
|
|
|
|
} else {
|
|
|
|
|
} else { // 若远程删除了内容,则本地也要删除内容
|
|
|
|
|
// remote delete
|
|
|
|
|
syncType = Node.SYNC_ACTION_DEL_LOCAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 进行同步操作
|
|
|
|
|
doContentSync(syncType, node, c);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
} else { // 警告,询问已创建的文件夹失败
|
|
|
|
|
Log.w(TAG, "failed to query existing folder");
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
@ -461,6 +561,8 @@ 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();
|
|
|
|
@ -468,14 +570,21 @@ public class GTaskManager {
|
|
|
|
|
node = entry.getValue();
|
|
|
|
|
if (mGTaskHashMap.containsKey(gid)) {
|
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
|
// 进行本地增添操作
|
|
|
|
|
doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!mCancelled)
|
|
|
|
|
// 如果没有取消,在GTsk的客户端进行实例的提交更新
|
|
|
|
|
GTaskClient.getInstance().commitUpdate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:内容同步
|
|
|
|
|
* 方法:依据不同的同步类型去调用不同的函数,以达到本地与远程同步的实际操作
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
*/
|
|
|
|
|
private void doContentSync(int syncType, Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
@ -483,13 +592,17 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
MetaData meta;
|
|
|
|
|
if (c != null) {
|
|
|
|
|
// 根据不同的同步类型来选择不同的操作
|
|
|
|
|
switch (syncType) {
|
|
|
|
|
// 本地添加
|
|
|
|
|
case Node.SYNC_ACTION_ADD_LOCAL:
|
|
|
|
|
addLocalNode(node);
|
|
|
|
|
break;
|
|
|
|
|
// 远程添加
|
|
|
|
|
case Node.SYNC_ACTION_ADD_REMOTE:
|
|
|
|
|
addRemoteNode(node, c);
|
|
|
|
|
break;
|
|
|
|
|
// 本地删除
|
|
|
|
|
case Node.SYNC_ACTION_DEL_LOCAL:
|
|
|
|
|
meta = mMetaHashMap.get(c.getString(SqlNote.GTASK_ID_COLUMN));
|
|
|
|
|
if (meta != null) {
|
|
|
|
@ -497,6 +610,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN));
|
|
|
|
|
break;
|
|
|
|
|
// 远程删除
|
|
|
|
|
case Node.SYNC_ACTION_DEL_REMOTE:
|
|
|
|
|
meta = mMetaHashMap.get(node.getGid());
|
|
|
|
|
if (meta != null) {
|
|
|
|
@ -504,52 +618,72 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
GTaskClient.getInstance().deleteNode(node);
|
|
|
|
|
break;
|
|
|
|
|
// 本地更新
|
|
|
|
|
case Node.SYNC_ACTION_UPDATE_LOCAL:
|
|
|
|
|
updateLocalNode(node, c);
|
|
|
|
|
break;
|
|
|
|
|
// 远程更新
|
|
|
|
|
case Node.SYNC_ACTION_UPDATE_REMOTE:
|
|
|
|
|
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("unkown sync action type");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:添加本地节点
|
|
|
|
|
* 参数:待添加的本地节点
|
|
|
|
|
*/
|
|
|
|
|
private void addLocalNode(Node node) throws NetworkFailureException {
|
|
|
|
|
// 检查是否取消这次操作
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SqlNote sqlNote;
|
|
|
|
|
// 若待增添节点为任务列表中的节点,进一步操作
|
|
|
|
|
//(instanceof 运算符是用来在运行时指出对象是否是特定类的一个实例。instanceof通过返回一个布尔值来指出,这个对象是否是这个特定类或者是它的子类的一个实例。)
|
|
|
|
|
if (node instanceof TaskList) {
|
|
|
|
|
// 在根目录中增加节点
|
|
|
|
|
if (node.getName().equals(
|
|
|
|
|
// 判断node节点的名字是否是MIUI系统文件夹的默认文件夹:preffix是访问页面的前缀,用来指定页面存放的文件夹,然后加上文件夹默认值。
|
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT)) {
|
|
|
|
|
sqlNote = new SqlNote(mContext, Notes.ID_ROOT_FOLDER);
|
|
|
|
|
} else if (node.getName().equals(
|
|
|
|
|
} else if (node.getName().equals( // 判断是否在存电话的文件夹里
|
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE)) {
|
|
|
|
|
sqlNote = new SqlNote(mContext, Notes.ID_CALL_RECORD_FOLDER);
|
|
|
|
|
} else {
|
|
|
|
|
} else { //若没有存放的文件夹,则将其放在根文件夹中
|
|
|
|
|
sqlNote = new SqlNote(mContext);
|
|
|
|
|
// 从本地任务列表中获取内容
|
|
|
|
|
sqlNote.setContent(node.getLocalJSONFromContent());
|
|
|
|
|
sqlNote.setParentId(Notes.ID_ROOT_FOLDER);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
} else { // 如果Node不在任务列表中,则删除不合法的节点和数据
|
|
|
|
|
sqlNote = new SqlNote(mContext);
|
|
|
|
|
// 从待增添节点中获取jsonobject对象
|
|
|
|
|
JSONObject js = node.getLocalJSONFromContent();
|
|
|
|
|
try {
|
|
|
|
|
try { //异常判断
|
|
|
|
|
if (js.has(GTaskStringUtils.META_HEAD_NOTE)) {
|
|
|
|
|
// 获取对应便签的jsonobject对象
|
|
|
|
|
JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
|
|
|
|
|
// 判断便签中是否有条目
|
|
|
|
|
if (note.has(NoteColumns.ID)) {
|
|
|
|
|
long id = note.getLong(NoteColumns.ID);
|
|
|
|
|
// 若便签条目的id已无效,在便签中将其删除
|
|
|
|
|
if (DataUtils.existInNoteDatabase(mContentResolver, id)) {
|
|
|
|
|
// the id is not available, have to create a new one
|
|
|
|
|
note.remove(NoteColumns.ID);
|
|
|
|
@ -557,6 +691,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 以下为判断便签中的数据条目
|
|
|
|
|
if (js.has(GTaskStringUtils.META_HEAD_DATA)) {
|
|
|
|
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
@ -572,12 +707,15 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
} catch (JSONException e) {
|
|
|
|
|
} catch (JSONException e) { // 对于异常进行处理
|
|
|
|
|
// 获取异常类型和异常详细消息
|
|
|
|
|
Log.w(TAG, e.toString());
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
// 删除不合法的节点后,把js的内容更新到sqlNote中
|
|
|
|
|
sqlNote.setContent(js);
|
|
|
|
|
|
|
|
|
|
// 找到父任务的ID号,并作为sqlNote的父任务的ID,没有父任务则报错
|
|
|
|
|
Long parentId = mGidToNid.get(((Task) node).getParent().getGid());
|
|
|
|
|
if (parentId == null) {
|
|
|
|
|
Log.e(TAG, "cannot find task's parent id locally");
|
|
|
|
@ -587,40 +725,59 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// create the local node
|
|
|
|
|
// 用getGid函数获取node节点的Gid,用于设置本地Gtask的ID
|
|
|
|
|
sqlNote.setGtaskId(node.getGid());
|
|
|
|
|
sqlNote.commit(false);
|
|
|
|
|
|
|
|
|
|
// update gid-nid mapping
|
|
|
|
|
// 更新gid与nid的映射表
|
|
|
|
|
mGidToNid.put(node.getGid(), sqlNote.getId());
|
|
|
|
|
mNidToGid.put(sqlNote.getId(), node.getGid());
|
|
|
|
|
|
|
|
|
|
// update meta
|
|
|
|
|
//更新远程的数据
|
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:更新本地节点
|
|
|
|
|
* 参数:一个是待更新的节点,一个是指向待增加位置的指针
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
*/
|
|
|
|
|
private void updateLocalNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 新建一个sql节点并将内容存储进Node中
|
|
|
|
|
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);
|
|
|
|
|
// 当不能找到父任务id时报错
|
|
|
|
|
if (parentId == null) {
|
|
|
|
|
Log.e(TAG, "cannot find task's parent id locally");
|
|
|
|
|
throw new ActionFailureException("cannot update local node");
|
|
|
|
|
}
|
|
|
|
|
// 把上面更新的父任务ID赋值给sqlNote
|
|
|
|
|
sqlNote.setParentId(parentId.longValue());
|
|
|
|
|
sqlNote.commit(true);
|
|
|
|
|
|
|
|
|
|
// update meta info
|
|
|
|
|
// 更新远程的节点信息
|
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:增加远程节点
|
|
|
|
|
* 参数:一个是待更新的节点,一个是指向待增加位置的指针
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
*/
|
|
|
|
|
private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
@ -630,6 +787,7 @@ public class GTaskManager {
|
|
|
|
|
Node n;
|
|
|
|
|
|
|
|
|
|
// update remotely
|
|
|
|
|
// 如果sqlNote是节点类型,则设置好它的参数并更新哈希表,再把节点更新到远程数据里
|
|
|
|
|
if (sqlNote.isNoteType()) {
|
|
|
|
|
Task task = new Task();
|
|
|
|
|
task.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
@ -650,6 +808,7 @@ public class GTaskManager {
|
|
|
|
|
TaskList tasklist = null;
|
|
|
|
|
|
|
|
|
|
// we need to skip folder if it has already existed
|
|
|
|
|
// 如果文件夹已经存在则需要跳过,否则根据sqlNote的ID创建新的文件夹
|
|
|
|
|
String folderName = GTaskStringUtils.MIUI_FOLDER_PREFFIX;
|
|
|
|
|
if (sqlNote.getId() == Notes.ID_ROOT_FOLDER)
|
|
|
|
|
folderName += GTaskStringUtils.FOLDER_DEFAULT;
|
|
|
|
@ -658,12 +817,14 @@ public class GTaskManager {
|
|
|
|
|
else
|
|
|
|
|
folderName += sqlNote.getSnippet();
|
|
|
|
|
|
|
|
|
|
// 使用迭代器对gtasklist进行遍历
|
|
|
|
|
Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
|
Map.Entry<String, TaskList> entry = iter.next();
|
|
|
|
|
String gid = entry.getKey();
|
|
|
|
|
TaskList list = entry.getValue();
|
|
|
|
|
|
|
|
|
|
// 若寻找到的任务列表已存在,则直接在里面更新
|
|
|
|
|
if (list.getName().equals(folderName)) {
|
|
|
|
|
tasklist = list;
|
|
|
|
|
if (mGTaskHashMap.containsKey(gid)) {
|
|
|
|
@ -674,6 +835,7 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// no match we can add now
|
|
|
|
|
// 若找不到任务列表,则创建一个新的任务列表
|
|
|
|
|
if (tasklist == null) {
|
|
|
|
|
tasklist = new TaskList();
|
|
|
|
|
tasklist.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
@ -684,42 +846,57 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// update local note
|
|
|
|
|
// 更新本地节点
|
|
|
|
|
sqlNote.setGtaskId(n.getGid());
|
|
|
|
|
sqlNote.commit(false);
|
|
|
|
|
sqlNote.resetLocalModified();
|
|
|
|
|
sqlNote.commit(true);
|
|
|
|
|
|
|
|
|
|
// gid-id mapping
|
|
|
|
|
// 更新gid与nid的映射表
|
|
|
|
|
mGidToNid.put(n.getGid(), sqlNote.getId());
|
|
|
|
|
mNidToGid.put(sqlNote.getId(), n.getGid());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:更新远程结点
|
|
|
|
|
* 参数:node是要更新的结点,c是数据库的指针
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
*/
|
|
|
|
|
private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 在指针指向处创建一个新的节点
|
|
|
|
|
SqlNote sqlNote = new SqlNote(mContext, c);
|
|
|
|
|
|
|
|
|
|
// update remotely
|
|
|
|
|
// 对远程GTask的节点进行更新
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
// 获取上一级任务列表的gid
|
|
|
|
|
String curParentGid = mNidToGid.get(sqlNote.getParentId());
|
|
|
|
|
if (curParentGid == null) {
|
|
|
|
|
Log.e(TAG, "cannot find task's parent tasklist");
|
|
|
|
|
throw new ActionFailureException("cannot update remote task");
|
|
|
|
|
}
|
|
|
|
|
// 通过HashMap找到对应Gid的TaskList
|
|
|
|
|
TaskList curParentList = mGTaskListHashMap.get(curParentGid);
|
|
|
|
|
|
|
|
|
|
// 若两个上一级任务列表不一致,进行任务的移动,从之前的任务列表中移动到该列表中
|
|
|
|
|
if (preParentList != curParentList) {
|
|
|
|
|
preParentList.removeChildTask(task);
|
|
|
|
|
curParentList.addChildTask(task);
|
|
|
|
@ -728,13 +905,23 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// clear local modified flag
|
|
|
|
|
// 清空本地已经被修改的标志
|
|
|
|
|
sqlNote.resetLocalModified();
|
|
|
|
|
sqlNote.commit(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:更新远程结点的数据(与上一个函数不同的是这里只更新数据)
|
|
|
|
|
* 参数:gid是要更新的数据对应的在数据库中的结点id,sqlnote用于获得数据内容
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
*/
|
|
|
|
|
private void updateRemoteMeta(String gid, SqlNote sqlNote) throws NetworkFailureException {
|
|
|
|
|
// 对节点的类型进行判断,只有节点类型符合要求才进一步操作
|
|
|
|
|
if (sqlNote != null && sqlNote.isNoteType()) {
|
|
|
|
|
// 通过gid获取元数据
|
|
|
|
|
MetaData metaData = mMetaHashMap.get(gid);
|
|
|
|
|
// 如果数据不为空,则设置好元,从客户端获取更新元节点后的信息内容
|
|
|
|
|
// 否则新建一个元并设置好参数,设置好meta链表的子节点,更新哈希表,用户端获取用元数据创建任务后的信息内容
|
|
|
|
|
if (metaData != null) {
|
|
|
|
|
metaData.setMeta(gid, sqlNote.getContent());
|
|
|
|
|
GTaskClient.getInstance().addUpdateNode(metaData);
|
|
|
|
@ -748,12 +935,17 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:刷新本地同步的ID
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
*/
|
|
|
|
|
private void refreshLocalSyncId() throws NetworkFailureException {
|
|
|
|
|
if (mCancelled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// get the latest gtask list
|
|
|
|
|
// 获取最新的gtask list
|
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
@ -761,6 +953,7 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
try {
|
|
|
|
|
// c作为光标定位,用query进行查询
|
|
|
|
|
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)
|
|
|
|
@ -782,9 +975,10 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 警告,询问本地待同步更新的便签 id 失败
|
|
|
|
|
Log.w(TAG, "failed to query local note to refresh sync id");
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
} finally { // 结束操作之后,将指针指向内容关闭并将指针置空
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.close();
|
|
|
|
|
c = null;
|
|
|
|
@ -792,10 +986,16 @@ public class GTaskManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:获取同步账户,通过客户端调用函数获取名字作为信息返回
|
|
|
|
|
*/
|
|
|
|
|
public String getSyncAccount() {
|
|
|
|
|
return GTaskClient.getInstance().getSyncAccount().name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 功能:取消同步,置mCancelled为true
|
|
|
|
|
*/
|
|
|
|
|
public void cancelSync() {
|
|
|
|
|
mCancelled = true;
|
|
|
|
|
}
|
|
|
|
|