|
|
|
@ -48,46 +48,46 @@ import java.util.Iterator;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @Author: 林迪文
|
|
|
|
|
|
|
|
* @Updator: 林迪文
|
|
|
|
|
|
|
|
* @Date 2025/12/22 21:30
|
|
|
|
|
|
|
|
* @Description 这个是同步功能的核心管理者。它负责整个同步流程的调度,比如登录、拉取云端数据、比对本地和云端的数据差异,然后决定哪些需要上传,哪些需要下载。
|
|
|
|
|
|
|
|
*/
|
|
|
|
public class GTaskManager {
|
|
|
|
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;
|
|
|
|
// 定义了一堆同步结果的状态码,方便给调用方(比如Service)返回结果
|
|
|
|
|
|
|
|
public static final int STATE_SUCCESS = 0; // 0代表成功
|
|
|
|
public static final int STATE_NETWORK_ERROR = 1;
|
|
|
|
public static final int STATE_NETWORK_ERROR = 1; // 1是网络问题
|
|
|
|
|
|
|
|
public static final int STATE_INTERNAL_ERROR = 2; // 2是程序内部错误
|
|
|
|
public static final int STATE_INTERNAL_ERROR = 2;
|
|
|
|
public static final int STATE_SYNC_IN_PROGRESS = 3; // 3是正在同步中,防止重复启动
|
|
|
|
|
|
|
|
public static final int STATE_SYNC_CANCELLED = 4; // 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;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private ContentResolver mContentResolver;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mSyncing;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private boolean mCancelled;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private HashMap<String, TaskList> mGTaskListHashMap;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private HashMap<String, Node> mGTaskHashMap;
|
|
|
|
private static GTaskManager mInstance = null; // 单例模式
|
|
|
|
|
|
|
|
|
|
|
|
private HashMap<String, MetaData> mMetaHashMap;
|
|
|
|
private Activity mActivity; // Activity的上下文,主要给GTaskClient登录用
|
|
|
|
|
|
|
|
private Context mContext; // 全局的上下文
|
|
|
|
|
|
|
|
private ContentResolver mContentResolver; // 内容提供者,可以理解成咱们App访问便签数据库的“管家”
|
|
|
|
|
|
|
|
|
|
|
|
private TaskList mMetaList;
|
|
|
|
private boolean mSyncing; // 同步状态的标记
|
|
|
|
|
|
|
|
private boolean mCancelled; // 取消同步的标记
|
|
|
|
|
|
|
|
|
|
|
|
private HashSet<Long> mLocalDeleteIdMap;
|
|
|
|
// HashMap是整个同步算法的核心,用来在内存里缓存和映射数据
|
|
|
|
|
|
|
|
private HashMap<String, TaskList> mGTaskListHashMap; // Google云端任务清单的缓存,key是清单的gid
|
|
|
|
|
|
|
|
private HashMap<String, Node> mGTaskHashMap; // Google云端所有节点的缓存(包括清单和任务),key是gid
|
|
|
|
|
|
|
|
private HashMap<String, MetaData> mMetaHashMap; // Google云端元数据的缓存,key是被关联任务的gid
|
|
|
|
|
|
|
|
private TaskList mMetaList; // 专门存meta数据的那个清单对象
|
|
|
|
|
|
|
|
|
|
|
|
private HashMap<String, Long> mGidToNid;
|
|
|
|
private HashSet<Long> mLocalDeleteIdMap; // 存放在本地被删除了的笔记ID
|
|
|
|
|
|
|
|
|
|
|
|
private HashMap<Long, String> mNidToGid;
|
|
|
|
// 这两个是关键,用来建立Google ID和本地数据库ID之间的对应关系
|
|
|
|
|
|
|
|
private HashMap<String, Long> mGidToNid; // Google ID到本地笔记ID的映射
|
|
|
|
|
|
|
|
private HashMap<Long, String> mNidToGid; // 本地笔记ID到Google ID的映射
|
|
|
|
|
|
|
|
|
|
|
|
private GTaskManager() {
|
|
|
|
private GTaskManager() {
|
|
|
|
|
|
|
|
// 构造函数里做一些初始化,防止空指针
|
|
|
|
mSyncing = false;
|
|
|
|
mSyncing = false;
|
|
|
|
mCancelled = false;
|
|
|
|
mCancelled = false;
|
|
|
|
mGTaskListHashMap = new HashMap<String, TaskList>();
|
|
|
|
mGTaskListHashMap = new HashMap<String, TaskList>();
|
|
|
|
@ -106,20 +106,29 @@ public class GTaskManager {
|
|
|
|
return mInstance;
|
|
|
|
return mInstance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 设置Activity上下文,因为登录Google账户需要一个Activity
|
|
|
|
public synchronized void setActivityContext(Activity activity) {
|
|
|
|
public synchronized void setActivityContext(Activity activity) {
|
|
|
|
// used for getting authtoken
|
|
|
|
|
|
|
|
mActivity = activity;
|
|
|
|
mActivity = activity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 这是同步操作的总入口,所有同步逻辑都从这里开始。
|
|
|
|
|
|
|
|
* @param context 上下文
|
|
|
|
|
|
|
|
* @param asyncTask 异步任务的实例,用来在同步过程中更新界面上的进度提示
|
|
|
|
|
|
|
|
* @return 返回上面定义的状态码,告诉调用者同步结果
|
|
|
|
|
|
|
|
*/
|
|
|
|
public int sync(Context context, GTaskASyncTask asyncTask) {
|
|
|
|
public int sync(Context context, GTaskASyncTask asyncTask) {
|
|
|
|
|
|
|
|
// 先检查是不是已经在同步了,是的话就直接返回,避免重复执行
|
|
|
|
if (mSyncing) {
|
|
|
|
if (mSyncing) {
|
|
|
|
Log.d(TAG, "Sync is in progress");
|
|
|
|
Log.d(TAG, "Sync is in progress");
|
|
|
|
return STATE_SYNC_IN_PROGRESS;
|
|
|
|
return STATE_SYNC_IN_PROGRESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mContext = context;
|
|
|
|
mContext = context;
|
|
|
|
mContentResolver = mContext.getContentResolver();
|
|
|
|
mContentResolver = mContext.getContentResolver(); // 拿到数据库“管家”
|
|
|
|
mSyncing = true;
|
|
|
|
mSyncing = true; // 标记开始同步
|
|
|
|
mCancelled = false;
|
|
|
|
mCancelled = false; // 重置取消标记
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 每次同步前,先把上次的缓存清空,保证拿到的是最新数据
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
@ -129,33 +138,35 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
GTaskClient client = GTaskClient.getInstance();
|
|
|
|
GTaskClient client = GTaskClient.getInstance();
|
|
|
|
client.resetUpdateArray();
|
|
|
|
client.resetUpdateArray(); // 万一上次同步失败有残留,先把待提交队列清一下
|
|
|
|
|
|
|
|
|
|
|
|
// login google task
|
|
|
|
// 登录
|
|
|
|
if (!mCancelled) {
|
|
|
|
if (!mCancelled) {
|
|
|
|
if (!client.login(mActivity)) {
|
|
|
|
if (!client.login(mActivity)) {
|
|
|
|
throw new NetworkFailureException("login google task failed");
|
|
|
|
throw new NetworkFailureException("login google task failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the task list from google
|
|
|
|
// 从Google服务器上把所有任务清单和任务都拉下来,放到内存的HashMap里
|
|
|
|
asyncTask.publishProgess(mContext.getString(R.string.sync_progress_init_list));
|
|
|
|
asyncTask.publishProgess(mContext.getString(R.string.sync_progress_init_list)); // 通知界面,“正在初始化列表...”
|
|
|
|
initGTaskList();
|
|
|
|
initGTaskList();
|
|
|
|
|
|
|
|
|
|
|
|
// do content sync work
|
|
|
|
// 开始比对和同步本地与云端的内容
|
|
|
|
asyncTask.publishProgess(mContext.getString(R.string.sync_progress_syncing));
|
|
|
|
asyncTask.publishProgess(mContext.getString(R.string.sync_progress_syncing)); // 通知界面,“正在同步...”
|
|
|
|
syncContent();
|
|
|
|
syncContent();
|
|
|
|
|
|
|
|
|
|
|
|
} catch (NetworkFailureException e) {
|
|
|
|
} catch (NetworkFailureException e) {
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
return STATE_NETWORK_ERROR;
|
|
|
|
return STATE_NETWORK_ERROR; // 网络异常
|
|
|
|
} catch (ActionFailureException e) {
|
|
|
|
} catch (ActionFailureException e) {
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
return STATE_INTERNAL_ERROR;
|
|
|
|
return STATE_INTERNAL_ERROR; // 操作失败,比如JSON解析错了
|
|
|
|
} catch (Exception e) {
|
|
|
|
} catch (Exception e) {
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
Log.e(TAG, e.toString());
|
|
|
|
e.printStackTrace();
|
|
|
|
e.printStackTrace();
|
|
|
|
return STATE_INTERNAL_ERROR;
|
|
|
|
return STATE_INTERNAL_ERROR; // 其他未知错误
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
// 关键的收尾工作,不管同步成功、失败还是取消,都要把这些缓存清掉,把状态改回去,保证下次能正常同步
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
@ -165,37 +176,43 @@ public class GTaskManager {
|
|
|
|
mSyncing = false;
|
|
|
|
mSyncing = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 最后根据取消标志返回最终状态
|
|
|
|
return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS;
|
|
|
|
return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 这个方法负责从Google Tasks服务器拉取所有的数据,并初始化到内存中的各个HashMap里,为后续的数据比对做准备。
|
|
|
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
|
|
|
*/
|
|
|
|
private void initGTaskList() throws NetworkFailureException {
|
|
|
|
private void initGTaskList() throws NetworkFailureException {
|
|
|
|
if (mCancelled)
|
|
|
|
if (mCancelled) // 同步过程中随时检查是否被取消了
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
GTaskClient client = GTaskClient.getInstance();
|
|
|
|
GTaskClient client = GTaskClient.getInstance();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
JSONArray jsTaskLists = client.getTaskLists();
|
|
|
|
JSONArray jsTaskLists = client.getTaskLists(); // 从服务器拿到所有清单的JSON数组
|
|
|
|
|
|
|
|
|
|
|
|
// init meta list first
|
|
|
|
// 优先处理meta清单。这个清单是用来存一些便签的附加信息(比如颜色、提醒时间)的,在Google Tasks界面上看不到,是咱们App自己用的
|
|
|
|
mMetaList = null;
|
|
|
|
mMetaList = null;
|
|
|
|
for (int i = 0; i < jsTaskLists.length(); i++) {
|
|
|
|
for (int i = 0; i < jsTaskLists.length(); i++) {
|
|
|
|
JSONObject object = jsTaskLists.getJSONObject(i);
|
|
|
|
JSONObject object = jsTaskLists.getJSONObject(i);
|
|
|
|
String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
|
|
|
|
String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID); // 清单的gid
|
|
|
|
String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);
|
|
|
|
String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME); // 清单的名字
|
|
|
|
|
|
|
|
|
|
|
|
if (name
|
|
|
|
// 通过一个特殊的前缀名来识别出哪个是meta清单
|
|
|
|
.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
|
|
|
|
if (name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
|
|
|
|
mMetaList = new TaskList();
|
|
|
|
mMetaList = new TaskList();
|
|
|
|
mMetaList.setContentByRemoteJSON(object);
|
|
|
|
mMetaList.setContentByRemoteJSON(object); // 把JSON数据填到TaskList对象里
|
|
|
|
|
|
|
|
|
|
|
|
// load meta data
|
|
|
|
// 找到meta清单后,再去拉这个清单下面所有的meta数据
|
|
|
|
JSONArray jsMetas = client.getTaskList(gid);
|
|
|
|
JSONArray jsMetas = client.getTaskList(gid);
|
|
|
|
for (int j = 0; j < jsMetas.length(); j++) {
|
|
|
|
for (int j = 0; j < jsMetas.length(); j++) {
|
|
|
|
object = (JSONObject) jsMetas.getJSONObject(j);
|
|
|
|
object = (JSONObject) jsMetas.getJSONObject(j);
|
|
|
|
MetaData metaData = new MetaData();
|
|
|
|
MetaData metaData = new MetaData();
|
|
|
|
metaData.setContentByRemoteJSON(object);
|
|
|
|
metaData.setContentByRemoteJSON(object);
|
|
|
|
if (metaData.isWorthSaving()) {
|
|
|
|
if (metaData.isWorthSaving()) { // 检查一下数据是不是有效
|
|
|
|
mMetaList.addChildTask(metaData);
|
|
|
|
mMetaList.addChildTask(metaData); // 加到meta清单的子任务列表里
|
|
|
|
if (metaData.getGid() != null) {
|
|
|
|
if (metaData.getGid() != null) {
|
|
|
|
|
|
|
|
// 把meta数据存到缓存里,key是它关联的那个笔记的gid,方便后面查找
|
|
|
|
mMetaHashMap.put(metaData.getRelatedGid(), metaData);
|
|
|
|
mMetaHashMap.put(metaData.getRelatedGid(), metaData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -203,7 +220,7 @@ public class GTaskManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// create meta list if not existed
|
|
|
|
// 如果服务器上没有meta清单,说明是第一次同步,那咱们就在云端给它创建一个
|
|
|
|
if (mMetaList == null) {
|
|
|
|
if (mMetaList == null) {
|
|
|
|
mMetaList = new TaskList();
|
|
|
|
mMetaList = new TaskList();
|
|
|
|
mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
@ -211,21 +228,23 @@ public class GTaskManager {
|
|
|
|
GTaskClient.getInstance().createTaskList(mMetaList);
|
|
|
|
GTaskClient.getInstance().createTaskList(mMetaList);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// init task list
|
|
|
|
// 处理正常的便签夹(任务清单)
|
|
|
|
for (int i = 0; i < jsTaskLists.length(); i++) {
|
|
|
|
for (int i = 0; i < jsTaskLists.length(); i++) {
|
|
|
|
JSONObject object = jsTaskLists.getJSONObject(i);
|
|
|
|
JSONObject object = jsTaskLists.getJSONObject(i);
|
|
|
|
String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
|
|
|
|
String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
|
|
|
|
String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);
|
|
|
|
String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 通过前缀名过滤出我们自己App创建的清单,忽略用户在Google Tasks上创建的其他清单
|
|
|
|
if (name.startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX)
|
|
|
|
if (name.startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX)
|
|
|
|
&& !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
&& !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
+ GTaskStringUtils.FOLDER_META)) {
|
|
|
|
+ GTaskStringUtils.FOLDER_META)) {
|
|
|
|
TaskList tasklist = new TaskList();
|
|
|
|
TaskList tasklist = new TaskList();
|
|
|
|
tasklist.setContentByRemoteJSON(object);
|
|
|
|
tasklist.setContentByRemoteJSON(object);
|
|
|
|
|
|
|
|
// 放到清单缓存和总节点缓存里
|
|
|
|
mGTaskListHashMap.put(gid, tasklist);
|
|
|
|
mGTaskListHashMap.put(gid, tasklist);
|
|
|
|
mGTaskHashMap.put(gid, tasklist);
|
|
|
|
mGTaskHashMap.put(gid, tasklist);
|
|
|
|
|
|
|
|
|
|
|
|
// load tasks
|
|
|
|
// 拉取这个清单下的所有任务(便签)
|
|
|
|
JSONArray jsTasks = client.getTaskList(gid);
|
|
|
|
JSONArray jsTasks = client.getTaskList(gid);
|
|
|
|
for (int j = 0; j < jsTasks.length(); j++) {
|
|
|
|
for (int j = 0; j < jsTasks.length(); j++) {
|
|
|
|
object = (JSONObject) jsTasks.getJSONObject(j);
|
|
|
|
object = (JSONObject) jsTasks.getJSONObject(j);
|
|
|
|
@ -233,9 +252,10 @@ public class GTaskManager {
|
|
|
|
Task task = new Task();
|
|
|
|
Task task = new Task();
|
|
|
|
task.setContentByRemoteJSON(object);
|
|
|
|
task.setContentByRemoteJSON(object);
|
|
|
|
if (task.isWorthSaving()) {
|
|
|
|
if (task.isWorthSaving()) {
|
|
|
|
|
|
|
|
// 从meta缓存里找到这条便签对应的meta数据,然后关联起来
|
|
|
|
task.setMetaInfo(mMetaHashMap.get(gid));
|
|
|
|
task.setMetaInfo(mMetaHashMap.get(gid));
|
|
|
|
tasklist.addChildTask(task);
|
|
|
|
tasklist.addChildTask(task); // 把任务加到清单的子节点里
|
|
|
|
mGTaskHashMap.put(gid, task);
|
|
|
|
mGTaskHashMap.put(gid, task); // 也放到总节点缓存里
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -247,11 +267,15 @@ public class GTaskManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 这个方法是数据同步的核心。它会遍历本地数据库和从云端拉下来的数据,找出两边的差异,然后调用doContentSync来决定具体执行哪种同步操作。
|
|
|
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
|
|
|
*/
|
|
|
|
private void syncContent() throws NetworkFailureException {
|
|
|
|
private void syncContent() throws NetworkFailureException {
|
|
|
|
int syncType;
|
|
|
|
int syncType; // 用来存同步操作的类型,比如是本地新增、还是远程删除
|
|
|
|
Cursor c = null;
|
|
|
|
Cursor c = null; // 数据库查询用的游标
|
|
|
|
String gid;
|
|
|
|
String gid; // Google ID
|
|
|
|
Node node;
|
|
|
|
Node node; // 云端节点对象
|
|
|
|
|
|
|
|
|
|
|
|
mLocalDeleteIdMap.clear();
|
|
|
|
mLocalDeleteIdMap.clear();
|
|
|
|
|
|
|
|
|
|
|
|
@ -259,7 +283,7 @@ public class GTaskManager {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// for local deleted note
|
|
|
|
// 处理本地已经删除的笔记。这些笔记在回收站里,需要通知云端也删除。
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
"(type<>? AND parent_id=?)", new String[] {
|
|
|
|
"(type<>? AND parent_id=?)", new String[] {
|
|
|
|
@ -267,13 +291,14 @@ public class GTaskManager {
|
|
|
|
}, null);
|
|
|
|
}, null);
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
gid = c.getString(SqlNote.GTASK_ID_COLUMN);
|
|
|
|
gid = c.getString(SqlNote.GTASK_ID_COLUMN); // 拿到本地笔记对应的Google ID
|
|
|
|
node = mGTaskHashMap.get(gid);
|
|
|
|
node = mGTaskHashMap.get(gid); // 去云端数据缓存里找找,看云上还有没有
|
|
|
|
if (node != null) {
|
|
|
|
if (node != null) {
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
// 如果云上还有,说明这是一个“本地删除,云端保留”的情况
|
|
|
|
doContentSync(Node.SYNC_ACTION_DEL_REMOTE, node, c);
|
|
|
|
mGTaskHashMap.remove(gid); // 从待处理的云端数据里把它移除,因为它已经被处理了
|
|
|
|
|
|
|
|
doContentSync(Node.SYNC_ACTION_DEL_REMOTE, node, c); // 告诉云端也要删除
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 把这个本地ID记下来,最后统一从本地数据库的data表里删掉
|
|
|
|
mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN));
|
|
|
|
mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
@ -281,16 +306,17 @@ public class GTaskManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
|
c.close();
|
|
|
|
c.close(); // 确保游标一定被关闭,防止内存泄漏
|
|
|
|
c = null;
|
|
|
|
c = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// sync folder first
|
|
|
|
// 同步文件夹。文件夹的层级关系比较重要,所以要优先同步
|
|
|
|
syncFolder();
|
|
|
|
syncFolder();
|
|
|
|
|
|
|
|
|
|
|
|
// for note existing in database
|
|
|
|
// 处理本地数据库里还存在的笔记
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 查询所有不在回收站里的普通笔记
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
"(type=? AND parent_id<>?)", new String[] {
|
|
|
|
"(type=? AND parent_id<>?)", new String[] {
|
|
|
|
String.valueOf(Notes.TYPE_NOTE), String.valueOf(Notes.ID_TRASH_FOLER)
|
|
|
|
String.valueOf(Notes.TYPE_NOTE), String.valueOf(Notes.ID_TRASH_FOLER)
|
|
|
|
@ -298,22 +324,23 @@ public class GTaskManager {
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
gid = c.getString(SqlNote.GTASK_ID_COLUMN);
|
|
|
|
gid = c.getString(SqlNote.GTASK_ID_COLUMN);
|
|
|
|
node = mGTaskHashMap.get(gid);
|
|
|
|
node = mGTaskHashMap.get(gid); // 拿本地笔记的gid去云端缓存里找
|
|
|
|
if (node != null) {
|
|
|
|
if (node != null) { // 在云端找到了对应的笔记
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
mGTaskHashMap.remove(gid); // 从待处理的云端数据里移除
|
|
|
|
mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));
|
|
|
|
mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN)); // 再次确认一下ID映射关系
|
|
|
|
mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
|
|
|
|
mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
|
|
|
|
|
|
|
|
// 调用node自己的方法来比对内容,判断是谁更新了,还是都更新了(冲突)
|
|
|
|
syncType = node.getSyncAction(c);
|
|
|
|
syncType = node.getSyncAction(c);
|
|
|
|
} else {
|
|
|
|
} else { // 在云端没找到对应的笔记
|
|
|
|
if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
|
|
|
|
if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
|
|
|
|
// local add
|
|
|
|
// 如果本地笔记的gid是空的,说明这是在本地新建的,还没同步过
|
|
|
|
syncType = Node.SYNC_ACTION_ADD_REMOTE;
|
|
|
|
syncType = Node.SYNC_ACTION_ADD_REMOTE;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// remote delete
|
|
|
|
// 如果本地有gid,但云端没有,说明这个笔记在云端被删掉了
|
|
|
|
syncType = Node.SYNC_ACTION_DEL_LOCAL;
|
|
|
|
syncType = Node.SYNC_ACTION_DEL_LOCAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
doContentSync(syncType, node, c);
|
|
|
|
doContentSync(syncType, node, c); // 根据比对结果执行相应的同步操作
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "failed to query existing note in database");
|
|
|
|
Log.w(TAG, "failed to query existing note in database");
|
|
|
|
@ -326,31 +353,35 @@ public class GTaskManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// go through remaining items
|
|
|
|
// 处理云端数据缓存(mGTaskHashMap)里剩下的东西
|
|
|
|
|
|
|
|
// 经过上面几步,缓存里剩下的就是那些“云端有,本地没有”的笔记了,说明是需要下载到本地的
|
|
|
|
Iterator<Map.Entry<String, Node>> iter = mGTaskHashMap.entrySet().iterator();
|
|
|
|
Iterator<Map.Entry<String, Node>> iter = mGTaskHashMap.entrySet().iterator();
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
Map.Entry<String, Node> entry = iter.next();
|
|
|
|
Map.Entry<String, Node> entry = iter.next();
|
|
|
|
node = entry.getValue();
|
|
|
|
node = entry.getValue();
|
|
|
|
doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null);
|
|
|
|
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 (!mCancelled) {
|
|
|
|
|
|
|
|
// 把之前记录的、在本地被删除的笔记,批量从数据库里彻底清理掉
|
|
|
|
if (!DataUtils.batchDeleteNotes(mContentResolver, mLocalDeleteIdMap)) {
|
|
|
|
if (!DataUtils.batchDeleteNotes(mContentResolver, mLocalDeleteIdMap)) {
|
|
|
|
throw new ActionFailureException("failed to batch-delete local deleted notes");
|
|
|
|
throw new ActionFailureException("failed to batch-delete local deleted notes");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// refresh local sync id
|
|
|
|
|
|
|
|
if (!mCancelled) {
|
|
|
|
if (!mCancelled) {
|
|
|
|
GTaskClient.getInstance().commitUpdate();
|
|
|
|
GTaskClient.getInstance().commitUpdate(); // 提交所有攒着的更新操作
|
|
|
|
refreshLocalSyncId();
|
|
|
|
refreshLocalSyncId(); // 最后再刷新一下本地笔记的sync_id,保证和云端一致
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 这个是专门同步文件夹的逻辑,和同步笔记的流程基本一样,只是查询条件和处理的对象不同。
|
|
|
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
|
|
|
*/
|
|
|
|
private void syncFolder() throws NetworkFailureException {
|
|
|
|
private void syncFolder() throws NetworkFailureException {
|
|
|
|
Cursor c = null;
|
|
|
|
Cursor c = null;
|
|
|
|
String gid;
|
|
|
|
String gid;
|
|
|
|
@ -361,7 +392,7 @@ public class GTaskManager {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// for root folder
|
|
|
|
// 先处理“默认便签夹”(根目录),这是个系统文件夹
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
c = mContentResolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI,
|
|
|
|
c = mContentResolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI,
|
|
|
|
Notes.ID_ROOT_FOLDER), SqlNote.PROJECTION_NOTE, null, null, null);
|
|
|
|
Notes.ID_ROOT_FOLDER), SqlNote.PROJECTION_NOTE, null, null, null);
|
|
|
|
@ -370,14 +401,15 @@ public class GTaskManager {
|
|
|
|
gid = c.getString(SqlNote.GTASK_ID_COLUMN);
|
|
|
|
gid = c.getString(SqlNote.GTASK_ID_COLUMN);
|
|
|
|
node = mGTaskHashMap.get(gid);
|
|
|
|
node = mGTaskHashMap.get(gid);
|
|
|
|
if (node != null) {
|
|
|
|
if (node != null) {
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
mGTaskHashMap.remove(gid); // 从待处理缓存中移除
|
|
|
|
mGidToNid.put(gid, (long) Notes.ID_ROOT_FOLDER);
|
|
|
|
mGidToNid.put(gid, (long) Notes.ID_ROOT_FOLDER); // 建立ID映射
|
|
|
|
mNidToGid.put((long) Notes.ID_ROOT_FOLDER, gid);
|
|
|
|
mNidToGid.put((long) Notes.ID_ROOT_FOLDER, gid);
|
|
|
|
// for system folder, only update remote name if necessary
|
|
|
|
// 系统文件夹只检查名字对不对,不对的话就更新云端的名字
|
|
|
|
if (!node.getName().equals(
|
|
|
|
if (!node.getName().equals(
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT))
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT))
|
|
|
|
doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c);
|
|
|
|
doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 云端没有这个文件夹,就创建一个
|
|
|
|
doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);
|
|
|
|
doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
@ -390,11 +422,12 @@ public class GTaskManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// for call-note folder
|
|
|
|
// 再处理“通话便签”文件夹,也是系统文件夹
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 这块逻辑和上面处理根目录的完全一样,只是ID和名字换了
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(_id=?)",
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(_id=?)",
|
|
|
|
new String[] {
|
|
|
|
new String[] {
|
|
|
|
String.valueOf(Notes.ID_CALL_RECORD_FOLDER)
|
|
|
|
String.valueOf(Notes.ID_CALL_RECORD_FOLDER)
|
|
|
|
}, null);
|
|
|
|
}, null);
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
|
if (c.moveToNext()) {
|
|
|
|
if (c.moveToNext()) {
|
|
|
|
@ -404,8 +437,7 @@ public class GTaskManager {
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
mGidToNid.put(gid, (long) Notes.ID_CALL_RECORD_FOLDER);
|
|
|
|
mGidToNid.put(gid, (long) Notes.ID_CALL_RECORD_FOLDER);
|
|
|
|
mNidToGid.put((long) Notes.ID_CALL_RECORD_FOLDER, gid);
|
|
|
|
mNidToGid.put((long) Notes.ID_CALL_RECORD_FOLDER, gid);
|
|
|
|
// for system folder, only update remote name if
|
|
|
|
|
|
|
|
// necessary
|
|
|
|
|
|
|
|
if (!node.getName().equals(
|
|
|
|
if (!node.getName().equals(
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX
|
|
|
|
+ GTaskStringUtils.FOLDER_CALL_NOTE))
|
|
|
|
+ GTaskStringUtils.FOLDER_CALL_NOTE))
|
|
|
|
@ -424,8 +456,9 @@ public class GTaskManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// for local existing folders
|
|
|
|
// 处理本地存在的其他普通文件夹
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 这里的比对逻辑和syncContent里处理普通笔记的逻辑是一样的
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
"(type=? AND parent_id<>?)", new String[] {
|
|
|
|
"(type=? AND parent_id<>?)", new String[] {
|
|
|
|
String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)
|
|
|
|
String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)
|
|
|
|
@ -439,12 +472,10 @@ public class GTaskManager {
|
|
|
|
mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));
|
|
|
|
mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));
|
|
|
|
mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
|
|
|
|
mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
|
|
|
|
syncType = node.getSyncAction(c);
|
|
|
|
syncType = node.getSyncAction(c);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
|
|
|
|
if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
|
|
|
|
// local add
|
|
|
|
|
|
|
|
syncType = Node.SYNC_ACTION_ADD_REMOTE;
|
|
|
|
syncType = Node.SYNC_ACTION_ADD_REMOTE;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// remote delete
|
|
|
|
|
|
|
|
syncType = Node.SYNC_ACTION_DEL_LOCAL;
|
|
|
|
syncType = Node.SYNC_ACTION_DEL_LOCAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -460,61 +491,77 @@ public class GTaskManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// for remote add folders
|
|
|
|
// 处理云端新增的文件夹
|
|
|
|
|
|
|
|
// mGTaskListHashMap里是所有云端文件夹,经过上面的处理,还在mGTaskHashMap里的就是“云端有,本地没有”的
|
|
|
|
Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
|
|
|
|
Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
Map.Entry<String, TaskList> entry = iter.next();
|
|
|
|
Map.Entry<String, TaskList> entry = iter.next();
|
|
|
|
gid = entry.getKey();
|
|
|
|
gid = entry.getKey();
|
|
|
|
node = entry.getValue();
|
|
|
|
node = entry.getValue();
|
|
|
|
if (mGTaskHashMap.containsKey(gid)) {
|
|
|
|
if (mGTaskHashMap.containsKey(gid)) { // 再次确认一下
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null);
|
|
|
|
doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!mCancelled)
|
|
|
|
if (!mCancelled)
|
|
|
|
GTaskClient.getInstance().commitUpdate();
|
|
|
|
GTaskClient.getInstance().commitUpdate(); // 同步完文件夹就提交一次,免得操作太多
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 这是一个分发器。根据传入的同步类型,决定具体是调用本地新增、远程删除还是其他方法。
|
|
|
|
|
|
|
|
* @param syncType 同步操作的类型
|
|
|
|
|
|
|
|
* @param node 云端节点对象
|
|
|
|
|
|
|
|
* @param c 本地数据库查询的游标
|
|
|
|
|
|
|
|
* @throws NetworkFailureException
|
|
|
|
|
|
|
|
*/
|
|
|
|
private void doContentSync(int syncType, Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
private void doContentSync(int syncType, Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
if (mCancelled) {
|
|
|
|
if (mCancelled) { // 最后一层检查,确保取消指令能及时生效
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
MetaData meta;
|
|
|
|
MetaData meta;
|
|
|
|
switch (syncType) {
|
|
|
|
switch (syncType) {
|
|
|
|
case Node.SYNC_ACTION_ADD_LOCAL:
|
|
|
|
case Node.SYNC_ACTION_ADD_LOCAL:
|
|
|
|
|
|
|
|
// 云端有,本地没有 -> 在本地数据库里新建
|
|
|
|
addLocalNode(node);
|
|
|
|
addLocalNode(node);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case Node.SYNC_ACTION_ADD_REMOTE:
|
|
|
|
case Node.SYNC_ACTION_ADD_REMOTE:
|
|
|
|
|
|
|
|
// 本地有,云端没有 -> 上传到Google服务器
|
|
|
|
addRemoteNode(node, c);
|
|
|
|
addRemoteNode(node, c);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case Node.SYNC_ACTION_DEL_LOCAL:
|
|
|
|
case Node.SYNC_ACTION_DEL_LOCAL:
|
|
|
|
|
|
|
|
// 本地有,云端没有(但本地有gid,说明之前同步过) -> 在本地删除
|
|
|
|
|
|
|
|
// 删之前,先看看它有没有关联的meta数据,有的话也要一起删掉
|
|
|
|
meta = mMetaHashMap.get(c.getString(SqlNote.GTASK_ID_COLUMN));
|
|
|
|
meta = mMetaHashMap.get(c.getString(SqlNote.GTASK_ID_COLUMN));
|
|
|
|
if (meta != null) {
|
|
|
|
if (meta != null) {
|
|
|
|
GTaskClient.getInstance().deleteNode(meta);
|
|
|
|
GTaskClient.getInstance().deleteNode(meta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN));
|
|
|
|
mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN)); // 记下ID,最后统一删
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case Node.SYNC_ACTION_DEL_REMOTE:
|
|
|
|
case Node.SYNC_ACTION_DEL_REMOTE:
|
|
|
|
|
|
|
|
// 本地删了,云端还有 -> 通知云端删除
|
|
|
|
meta = mMetaHashMap.get(node.getGid());
|
|
|
|
meta = mMetaHashMap.get(node.getGid());
|
|
|
|
if (meta != null) {
|
|
|
|
if (meta != null) {
|
|
|
|
GTaskClient.getInstance().deleteNode(meta);
|
|
|
|
GTaskClient.getInstance().deleteNode(meta); // 同样,连meta数据一起删
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GTaskClient.getInstance().deleteNode(node);
|
|
|
|
GTaskClient.getInstance().deleteNode(node);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case Node.SYNC_ACTION_UPDATE_LOCAL:
|
|
|
|
case Node.SYNC_ACTION_UPDATE_LOCAL:
|
|
|
|
|
|
|
|
// 云端更新了 -> 把云端的数据更新到本地数据库
|
|
|
|
updateLocalNode(node, c);
|
|
|
|
updateLocalNode(node, c);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case Node.SYNC_ACTION_UPDATE_REMOTE:
|
|
|
|
case Node.SYNC_ACTION_UPDATE_REMOTE:
|
|
|
|
|
|
|
|
// 本地更新了 -> 把本地的数据上传到云端
|
|
|
|
updateRemoteNode(node, c);
|
|
|
|
updateRemoteNode(node, c);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case Node.SYNC_ACTION_UPDATE_CONFLICT:
|
|
|
|
case Node.SYNC_ACTION_UPDATE_CONFLICT:
|
|
|
|
// merging both modifications maybe a good idea
|
|
|
|
// 两边都更新了,冲突了
|
|
|
|
// right now just use local update simply
|
|
|
|
// 直接用本地的覆盖云端的
|
|
|
|
updateRemoteNode(node, c);
|
|
|
|
updateRemoteNode(node, c);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case Node.SYNC_ACTION_NONE:
|
|
|
|
case Node.SYNC_ACTION_NONE:
|
|
|
|
|
|
|
|
// 两边数据一样,啥也不用干
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case Node.SYNC_ACTION_ERROR:
|
|
|
|
case Node.SYNC_ACTION_ERROR:
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
@ -522,26 +569,30 @@ public class GTaskManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 在本地数据库里添加一个节点(文件夹或便签)
|
|
|
|
private void addLocalNode(Node node) throws NetworkFailureException {
|
|
|
|
private void addLocalNode(Node node) throws NetworkFailureException {
|
|
|
|
if (mCancelled) {
|
|
|
|
if (mCancelled) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SqlNote sqlNote;
|
|
|
|
SqlNote sqlNote;
|
|
|
|
if (node instanceof TaskList) {
|
|
|
|
if (node instanceof TaskList) { // 判断是文件夹还是便签
|
|
|
|
|
|
|
|
// 对系统文件夹做特殊处理,直接关联到固定的ID上,而不是新建
|
|
|
|
if (node.getName().equals(
|
|
|
|
if (node.getName().equals(
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT)) {
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT)) {
|
|
|
|
sqlNote = new SqlNote(mContext, Notes.ID_ROOT_FOLDER);
|
|
|
|
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)) {
|
|
|
|
GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE)) {
|
|
|
|
sqlNote = new SqlNote(mContext, Notes.ID_CALL_RECORD_FOLDER);
|
|
|
|
sqlNote = new SqlNote(mContext, Notes.ID_CALL_RECORD_FOLDER);
|
|
|
|
} else {
|
|
|
|
} else { // 普通文件夹
|
|
|
|
sqlNote = new SqlNote(mContext);
|
|
|
|
sqlNote = new SqlNote(mContext);
|
|
|
|
sqlNote.setContent(node.getLocalJSONFromContent());
|
|
|
|
sqlNote.setContent(node.getLocalJSONFromContent()); // 把云端节点内容转成本地格式
|
|
|
|
sqlNote.setParentId(Notes.ID_ROOT_FOLDER);
|
|
|
|
sqlNote.setParentId(Notes.ID_ROOT_FOLDER); // 普通文件夹都放在根目录下
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else { // 是便签
|
|
|
|
sqlNote = new SqlNote(mContext);
|
|
|
|
sqlNote = new SqlNote(mContext);
|
|
|
|
|
|
|
|
// 这里有一段防御性代码,检查下载下来的笔记ID和dataID在本地是不是已经被占用了
|
|
|
|
|
|
|
|
// 如果被占用了,就把它ID去掉,让数据库自己生成新的,避免主键冲突
|
|
|
|
JSONObject js = node.getLocalJSONFromContent();
|
|
|
|
JSONObject js = node.getLocalJSONFromContent();
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (js.has(GTaskStringUtils.META_HEAD_NOTE)) {
|
|
|
|
if (js.has(GTaskStringUtils.META_HEAD_NOTE)) {
|
|
|
|
@ -549,12 +600,11 @@ public class GTaskManager {
|
|
|
|
if (note.has(NoteColumns.ID)) {
|
|
|
|
if (note.has(NoteColumns.ID)) {
|
|
|
|
long id = note.getLong(NoteColumns.ID);
|
|
|
|
long id = note.getLong(NoteColumns.ID);
|
|
|
|
if (DataUtils.existInNoteDatabase(mContentResolver, id)) {
|
|
|
|
if (DataUtils.existInNoteDatabase(mContentResolver, id)) {
|
|
|
|
// the id is not available, have to create a new one
|
|
|
|
|
|
|
|
note.remove(NoteColumns.ID);
|
|
|
|
note.remove(NoteColumns.ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// data表里的ID也要检查
|
|
|
|
if (js.has(GTaskStringUtils.META_HEAD_DATA)) {
|
|
|
|
if (js.has(GTaskStringUtils.META_HEAD_DATA)) {
|
|
|
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
for (int i = 0; i < dataArray.length(); i++) {
|
|
|
|
@ -562,13 +612,10 @@ public class GTaskManager {
|
|
|
|
if (data.has(DataColumns.ID)) {
|
|
|
|
if (data.has(DataColumns.ID)) {
|
|
|
|
long dataId = data.getLong(DataColumns.ID);
|
|
|
|
long dataId = data.getLong(DataColumns.ID);
|
|
|
|
if (DataUtils.existInDataDatabase(mContentResolver, dataId)) {
|
|
|
|
if (DataUtils.existInDataDatabase(mContentResolver, dataId)) {
|
|
|
|
// the data id is not available, have to create
|
|
|
|
|
|
|
|
// a new one
|
|
|
|
|
|
|
|
data.remove(DataColumns.ID);
|
|
|
|
data.remove(DataColumns.ID);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (JSONException e) {
|
|
|
|
} catch (JSONException e) {
|
|
|
|
Log.w(TAG, e.toString());
|
|
|
|
Log.w(TAG, e.toString());
|
|
|
|
@ -576,6 +623,7 @@ public class GTaskManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sqlNote.setContent(js);
|
|
|
|
sqlNote.setContent(js);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 从ID映射表里找到它的parent(父文件夹)在本地的ID
|
|
|
|
Long parentId = mGidToNid.get(((Task) node).getParent().getGid());
|
|
|
|
Long parentId = mGidToNid.get(((Task) node).getParent().getGid());
|
|
|
|
if (parentId == null) {
|
|
|
|
if (parentId == null) {
|
|
|
|
Log.e(TAG, "cannot find task's parent id locally");
|
|
|
|
Log.e(TAG, "cannot find task's parent id locally");
|
|
|
|
@ -584,28 +632,30 @@ public class GTaskManager {
|
|
|
|
sqlNote.setParentId(parentId.longValue());
|
|
|
|
sqlNote.setParentId(parentId.longValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// create the local node
|
|
|
|
sqlNote.setGtaskId(node.getGid()); // 关联Google ID
|
|
|
|
sqlNote.setGtaskId(node.getGid());
|
|
|
|
sqlNote.commit(false); // 提交到数据库
|
|
|
|
sqlNote.commit(false);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// update gid-nid mapping
|
|
|
|
// 关键一步:在ID映射表里把这个新节点的对应关系加上
|
|
|
|
mGidToNid.put(node.getGid(), sqlNote.getId());
|
|
|
|
mGidToNid.put(node.getGid(), sqlNote.getId());
|
|
|
|
mNidToGid.put(sqlNote.getId(), node.getGid());
|
|
|
|
mNidToGid.put(sqlNote.getId(), node.getGid());
|
|
|
|
|
|
|
|
|
|
|
|
// update meta
|
|
|
|
// 顺便把meta数据也更新一下
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新本地数据库里的一个节点
|
|
|
|
private void updateLocalNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
private void updateLocalNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
if (mCancelled) {
|
|
|
|
if (mCancelled) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SqlNote sqlNote;
|
|
|
|
SqlNote sqlNote;
|
|
|
|
// update the note locally
|
|
|
|
// 先用游标把数据库里现有的数据读出来
|
|
|
|
sqlNote = new SqlNote(mContext, c);
|
|
|
|
sqlNote = new SqlNote(mContext, c);
|
|
|
|
|
|
|
|
// 然后把云端的数据盖上去
|
|
|
|
sqlNote.setContent(node.getLocalJSONFromContent());
|
|
|
|
sqlNote.setContent(node.getLocalJSONFromContent());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 找到它的parent在本地的ID
|
|
|
|
Long parentId = (node instanceof Task) ? mGidToNid.get(((Task) node).getParent().getGid())
|
|
|
|
Long parentId = (node instanceof Task) ? mGidToNid.get(((Task) node).getParent().getGid())
|
|
|
|
: new Long(Notes.ID_ROOT_FOLDER);
|
|
|
|
: new Long(Notes.ID_ROOT_FOLDER);
|
|
|
|
if (parentId == null) {
|
|
|
|
if (parentId == null) {
|
|
|
|
@ -613,41 +663,39 @@ public class GTaskManager {
|
|
|
|
throw new ActionFailureException("cannot update local node");
|
|
|
|
throw new ActionFailureException("cannot update local node");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sqlNote.setParentId(parentId.longValue());
|
|
|
|
sqlNote.setParentId(parentId.longValue());
|
|
|
|
sqlNote.commit(true);
|
|
|
|
sqlNote.commit(true); // 提交更新
|
|
|
|
|
|
|
|
|
|
|
|
// update meta info
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote); // 更新meta
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 把本地新建的节点上传到云端
|
|
|
|
private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
if (mCancelled) {
|
|
|
|
if (mCancelled) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SqlNote sqlNote = new SqlNote(mContext, c);
|
|
|
|
SqlNote sqlNote = new SqlNote(mContext, c);
|
|
|
|
Node n;
|
|
|
|
Node n; // 用来存创建成功后的云端节点对象
|
|
|
|
|
|
|
|
|
|
|
|
// update remotely
|
|
|
|
if (sqlNote.isNoteType()) { // 是便签
|
|
|
|
if (sqlNote.isNoteType()) {
|
|
|
|
|
|
|
|
Task task = new Task();
|
|
|
|
Task task = new Task();
|
|
|
|
task.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
task.setContentByLocalJSON(sqlNote.getContent()); // 用本地数据填充Task对象
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 从映射表里找到父文件夹的gid
|
|
|
|
String parentGid = mNidToGid.get(sqlNote.getParentId());
|
|
|
|
String parentGid = mNidToGid.get(sqlNote.getParentId());
|
|
|
|
if (parentGid == null) {
|
|
|
|
if (parentGid == null) {
|
|
|
|
Log.e(TAG, "cannot find task's parent tasklist");
|
|
|
|
Log.e(TAG, "cannot find task's parent tasklist");
|
|
|
|
throw new ActionFailureException("cannot add remote task");
|
|
|
|
throw new ActionFailureException("cannot add remote task");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mGTaskListHashMap.get(parentGid).addChildTask(task);
|
|
|
|
mGTaskListHashMap.get(parentGid).addChildTask(task); // 在内存里也维护一下父子关系
|
|
|
|
|
|
|
|
|
|
|
|
GTaskClient.getInstance().createTask(task);
|
|
|
|
GTaskClient.getInstance().createTask(task); // 发请求创建
|
|
|
|
n = (Node) task;
|
|
|
|
n = (Node) task;
|
|
|
|
|
|
|
|
|
|
|
|
// add meta
|
|
|
|
|
|
|
|
updateRemoteMeta(task.getGid(), sqlNote);
|
|
|
|
updateRemoteMeta(task.getGid(), sqlNote);
|
|
|
|
} else {
|
|
|
|
} else { // 是文件夹
|
|
|
|
TaskList tasklist = null;
|
|
|
|
TaskList tasklist = null;
|
|
|
|
|
|
|
|
// 上传前先检查一下,云端是不是已经有同名的文件夹了,有的话就直接复用,不再创建新的
|
|
|
|
// we need to skip folder if it has already existed
|
|
|
|
|
|
|
|
String folderName = GTaskStringUtils.MIUI_FOLDER_PREFFIX;
|
|
|
|
String folderName = GTaskStringUtils.MIUI_FOLDER_PREFFIX;
|
|
|
|
if (sqlNote.getId() == Notes.ID_ROOT_FOLDER)
|
|
|
|
if (sqlNote.getId() == Notes.ID_ROOT_FOLDER)
|
|
|
|
folderName += GTaskStringUtils.FOLDER_DEFAULT;
|
|
|
|
folderName += GTaskStringUtils.FOLDER_DEFAULT;
|
|
|
|
@ -663,7 +711,7 @@ public class GTaskManager {
|
|
|
|
TaskList list = entry.getValue();
|
|
|
|
TaskList list = entry.getValue();
|
|
|
|
|
|
|
|
|
|
|
|
if (list.getName().equals(folderName)) {
|
|
|
|
if (list.getName().equals(folderName)) {
|
|
|
|
tasklist = list;
|
|
|
|
tasklist = list; // 找到了同名的
|
|
|
|
if (mGTaskHashMap.containsKey(gid)) {
|
|
|
|
if (mGTaskHashMap.containsKey(gid)) {
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -671,7 +719,7 @@ public class GTaskManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// no match we can add now
|
|
|
|
// 如果没找到同名的,才真的去创建
|
|
|
|
if (tasklist == null) {
|
|
|
|
if (tasklist == null) {
|
|
|
|
tasklist = new TaskList();
|
|
|
|
tasklist = new TaskList();
|
|
|
|
tasklist.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
tasklist.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
@ -681,17 +729,18 @@ public class GTaskManager {
|
|
|
|
n = (Node) tasklist;
|
|
|
|
n = (Node) tasklist;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// update local note
|
|
|
|
// 上传成功后,把服务器返回的gid写回本地数据库
|
|
|
|
sqlNote.setGtaskId(n.getGid());
|
|
|
|
sqlNote.setGtaskId(n.getGid());
|
|
|
|
sqlNote.commit(false);
|
|
|
|
sqlNote.commit(false);
|
|
|
|
sqlNote.resetLocalModified();
|
|
|
|
sqlNote.resetLocalModified(); // 清除'本地已修改'的标记
|
|
|
|
sqlNote.commit(true);
|
|
|
|
sqlNote.commit(true);
|
|
|
|
|
|
|
|
|
|
|
|
// gid-id mapping
|
|
|
|
// 更新ID映射表
|
|
|
|
mGidToNid.put(n.getGid(), sqlNote.getId());
|
|
|
|
mGidToNid.put(n.getGid(), sqlNote.getId());
|
|
|
|
mNidToGid.put(sqlNote.getId(), n.getGid());
|
|
|
|
mNidToGid.put(sqlNote.getId(), n.getGid());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 把本地修改过的节点上传到云端
|
|
|
|
private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException {
|
|
|
|
if (mCancelled) {
|
|
|
|
if (mCancelled) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
@ -699,59 +748,61 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
|
|
SqlNote sqlNote = new SqlNote(mContext, c);
|
|
|
|
SqlNote sqlNote = new SqlNote(mContext, c);
|
|
|
|
|
|
|
|
|
|
|
|
// update remotely
|
|
|
|
// 用本地数据更新云端节点对象的内容
|
|
|
|
node.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
node.setContentByLocalJSON(sqlNote.getContent());
|
|
|
|
GTaskClient.getInstance().addUpdateNode(node);
|
|
|
|
GTaskClient.getInstance().addUpdateNode(node); // 把这个更新操作加到待提交队列
|
|
|
|
|
|
|
|
|
|
|
|
// update meta
|
|
|
|
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
updateRemoteMeta(node.getGid(), sqlNote);
|
|
|
|
|
|
|
|
|
|
|
|
// move task if necessary
|
|
|
|
// 如果是便签,还要检查一下它的父文件夹有没有变,变了的话就要发一个'move'指令
|
|
|
|
if (sqlNote.isNoteType()) {
|
|
|
|
if (sqlNote.isNoteType()) {
|
|
|
|
Task task = (Task) node;
|
|
|
|
Task task = (Task) node;
|
|
|
|
TaskList preParentList = task.getParent();
|
|
|
|
TaskList preParentList = task.getParent(); // 之前的parent
|
|
|
|
|
|
|
|
|
|
|
|
String curParentGid = mNidToGid.get(sqlNote.getParentId());
|
|
|
|
String curParentGid = mNidToGid.get(sqlNote.getParentId());
|
|
|
|
if (curParentGid == null) {
|
|
|
|
if (curParentGid == null) {
|
|
|
|
Log.e(TAG, "cannot find task's parent tasklist");
|
|
|
|
Log.e(TAG, "cannot find task's parent tasklist");
|
|
|
|
throw new ActionFailureException("cannot update remote task");
|
|
|
|
throw new ActionFailureException("cannot update remote task");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TaskList curParentList = mGTaskListHashMap.get(curParentGid);
|
|
|
|
TaskList curParentList = mGTaskListHashMap.get(curParentGid);// parent变了
|
|
|
|
|
|
|
|
|
|
|
|
if (preParentList != curParentList) {
|
|
|
|
if (preParentList != curParentList) {
|
|
|
|
|
|
|
|
// 在内存里维护父子关系
|
|
|
|
preParentList.removeChildTask(task);
|
|
|
|
preParentList.removeChildTask(task);
|
|
|
|
curParentList.addChildTask(task);
|
|
|
|
curParentList.addChildTask(task);
|
|
|
|
GTaskClient.getInstance().moveTask(task, preParentList, curParentList);
|
|
|
|
GTaskClient.getInstance().moveTask(task, preParentList, curParentList); // 发送移动请求
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// clear local modified flag
|
|
|
|
// 上传成功后,清除'本地已修改'的标记
|
|
|
|
sqlNote.resetLocalModified();
|
|
|
|
sqlNote.resetLocalModified();
|
|
|
|
sqlNote.commit(true);
|
|
|
|
sqlNote.commit(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新云端的meta数据
|
|
|
|
private void updateRemoteMeta(String gid, SqlNote sqlNote) throws NetworkFailureException {
|
|
|
|
private void updateRemoteMeta(String gid, SqlNote sqlNote) throws NetworkFailureException {
|
|
|
|
if (sqlNote != null && sqlNote.isNoteType()) {
|
|
|
|
if (sqlNote != null && sqlNote.isNoteType()) {
|
|
|
|
MetaData metaData = mMetaHashMap.get(gid);
|
|
|
|
MetaData metaData = mMetaHashMap.get(gid); // 先看看这个便签之前有没有meta数据
|
|
|
|
if (metaData != null) {
|
|
|
|
if (metaData != null) { // 有的话,直接更新
|
|
|
|
metaData.setMeta(gid, sqlNote.getContent());
|
|
|
|
metaData.setMeta(gid, sqlNote.getContent());
|
|
|
|
GTaskClient.getInstance().addUpdateNode(metaData);
|
|
|
|
GTaskClient.getInstance().addUpdateNode(metaData);
|
|
|
|
} else {
|
|
|
|
} else { // 没有的话,就新建一个
|
|
|
|
metaData = new MetaData();
|
|
|
|
metaData = new MetaData();
|
|
|
|
metaData.setMeta(gid, sqlNote.getContent());
|
|
|
|
metaData.setMeta(gid, sqlNote.getContent());
|
|
|
|
mMetaList.addChildTask(metaData);
|
|
|
|
mMetaList.addChildTask(metaData); // 关联到meta清单下
|
|
|
|
mMetaHashMap.put(gid, metaData);
|
|
|
|
mMetaHashMap.put(gid, metaData); // 加到缓存
|
|
|
|
GTaskClient.getInstance().createTask(metaData);
|
|
|
|
GTaskClient.getInstance().createTask(metaData); // 发请求创建
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 同步完成后,刷新本地数据库里所有笔记的sync_id,让它和云端的lastModified时间戳保持一致
|
|
|
|
private void refreshLocalSyncId() throws NetworkFailureException {
|
|
|
|
private void refreshLocalSyncId() throws NetworkFailureException {
|
|
|
|
if (mCancelled) {
|
|
|
|
if (mCancelled) {
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// get the latest gtask list
|
|
|
|
// 重新从服务器拉一遍最新的数据,因为commitUpdate之后,云端的lastModified可能变了
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
mGTaskHashMap.clear();
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
mGTaskListHashMap.clear();
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
mMetaHashMap.clear();
|
|
|
|
@ -759,6 +810,7 @@ public class GTaskManager {
|
|
|
|
|
|
|
|
|
|
|
|
Cursor c = null;
|
|
|
|
Cursor c = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 遍历本地所有笔记
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
|
|
|
|
"(type<>? AND parent_id<>?)", new String[] {
|
|
|
|
"(type<>? AND parent_id<>?)", new String[] {
|
|
|
|
String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER)
|
|
|
|
String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER)
|
|
|
|
@ -767,13 +819,15 @@ public class GTaskManager {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
String gid = c.getString(SqlNote.GTASK_ID_COLUMN);
|
|
|
|
String gid = c.getString(SqlNote.GTASK_ID_COLUMN);
|
|
|
|
Node node = mGTaskHashMap.get(gid);
|
|
|
|
Node node = mGTaskHashMap.get(gid);
|
|
|
|
if (node != null) {
|
|
|
|
if (node != null) { // 找到对应的云端节点
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
mGTaskHashMap.remove(gid);
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
|
|
|
// 把云端节点的时间戳更新到本地的sync_id字段
|
|
|
|
values.put(NoteColumns.SYNC_ID, node.getLastModified());
|
|
|
|
values.put(NoteColumns.SYNC_ID, node.getLastModified());
|
|
|
|
mContentResolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI,
|
|
|
|
mContentResolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI,
|
|
|
|
c.getLong(SqlNote.ID_COLUMN)), values, null, null);
|
|
|
|
c.getLong(SqlNote.ID_COLUMN)), values, null, null);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
// 如果同步完,本地有的笔记在云端居然找不到了,说明出错了
|
|
|
|
Log.e(TAG, "something is missed");
|
|
|
|
Log.e(TAG, "something is missed");
|
|
|
|
throw new ActionFailureException(
|
|
|
|
throw new ActionFailureException(
|
|
|
|
"some local items don't have gid after sync");
|
|
|
|
"some local items don't have gid after sync");
|
|
|
|
@ -790,10 +844,12 @@ public class GTaskManager {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 获取当前同步的账户名
|
|
|
|
public String getSyncAccount() {
|
|
|
|
public String getSyncAccount() {
|
|
|
|
return GTaskClient.getInstance().getSyncAccount().name;
|
|
|
|
return GTaskClient.getInstance().getSyncAccount().name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 从外部取消同步
|
|
|
|
public void cancelSync() {
|
|
|
|
public void cancelSync() {
|
|
|
|
mCancelled = true;
|
|
|
|
mCancelled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|