/** * 功能:更新远端的Node,包含meta更新(updateRemoteMeta) * @author TTS * @param node * ----同步操作的基础数据类型 * @param c * --Cursor * @throws NetworkFailureException */ private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException { if (mCancelled) { return; } SqlNote sqlNote = new SqlNote(mContext, c); // update remotely node.setContentByLocalJSON(sqlNote.getContent()); GTaskClient.getInstance().addUpdateNode(node); //GTaskClient用途为从本地登陆远端服务器 // update meta updateRemoteMeta(node.getGid(), sqlNote); // move task if necessary if (sqlNote.isNoteType()) { Task task = (Task) node; TaskList preParentList = task.getParent(); //preParentList为通过node获取的父节点列表 String curParentGid = mNidToGid.get(sqlNote.getParentId()); //curParentGid为通过光标在数据库中找到sqlNote的mParentId,再通过mNidToGid由long类型转为String类型的Gid if (curParentGid == null) { Log.e(TAG, "cannot find task's parent tasklist"); throw new ActionFailureException("cannot update remote task"); } TaskList curParentList = mGTaskListHashMap.get(curParentGid); //通过HashMap找到对应Gid的TaskList if (preParentList != curParentList) { //????????????? preParentList.removeChildTask(task); curParentList.addChildTask(task); GTaskClient.getInstance().moveTask(task, preParentList, curParentList); } } // clear local modified flag sqlNote.resetLocalModified(); //commit到本地数据库 sqlNote.commit(true); } /** * 功能:升级远程meta。 meta---元数据----计算机文件系统管理数据---管理数据的数据。 * @author TTS * @param gid * ---GoogleID为String类型 * @param sqlNote * ---同步前的数据库操作,故使用类SqlNote * @throws NetworkFailureException */ 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); mMetaHashMap.put(gid, metaData); GTaskClient.getInstance().createTask(metaData); } } } /** * 功能:刷新本地,给sync的ID对应上最后更改过的对象 * @author TTS * @return void * @throws NetworkFailureException */ private void refreshLocalSyncId() throws NetworkFailureException { if (mCancelled) { return; } // get the latest gtask list //获取最近的(最晚的)gtask list mGTaskHashMap.clear(); mGTaskListHashMap.clear(); mMetaHashMap.clear(); initGTaskList(); 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) }, NoteColumns.TYPE + " DESC"); //query语句:五个参数,NoteColumns.TYPE + " DESC"-----为按类型递减顺序返回查询结果。new String[] {String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER)}------为选择参数。"(type<>? AND parent_id<>?)"-------指明返回行过滤器。SqlNote.PROJECTION_NOTE--------应返回的数据列的名字。Notes.CONTENT_NOTE_URI--------contentProvider包含所有数据集所对应的uri if (c != null) { while (c.moveToNext()) { String gid = c.getString(SqlNote.GTASK_ID_COLUMN); Node node = mGTaskHashMap.get(gid); if (node != null) { mGTaskHashMap.remove(gid); ContentValues values = new ContentValues(); //在ContentValues中创建键值对。准备通过contentResolver写入数据 values.put(NoteColumns.SYNC_ID, node.getLastModified()); mContentResolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, //进行批量更改,选择参数为NULL,应该可以用insert替换,参数分别为表名和需要更新的value对象。 c.getLong(SqlNote.ID_COLUMN)), values, null, null); } else { Log.e(TAG, "something is missed"); throw new ActionFailureException( "some local items don't have gid after sync"); } } } else { Log.w(TAG, "failed to query local note to refresh sync id"); } } finally { if (c != null) { c.close(); c = null; } } } /** * 功能:获取同步账号,mAccount.name * @author TTS * @return String */ public String getSyncAccount() { return GTaskClient.getInstance().getSyncAccount().name; } /** * 功能:取消同步,置mCancelled为true * @author TTS */ public void cancelSync() { mCancelled = true; } }