当前位置:   article > 正文

小米便签源码分析——gtask包_micode notes源码

micode notes源码

目录

一、data包

1、MetaData.java

2、Node.java

3、SqlData.java

4、SqlNote.java

5、Task.java

6、TaskList.java

二、exception包

1、ActionFailureException.java

2、NetworkFailureException.java

三、remote包

1、GTaskASyncTask.java

2、GTaskClient.java

3、GTaskManager.java

4、GTaskSyncService.java


一、data

1、MetaData.java

  1. package net.micode.notes.gtask.data;
  2. public class MetaData extends Task {
  3. /*
  4. * 功能描述:得到类的简写名称存入字符串TAG中
  5. * 实现过程:调用getSimpleName ()函数
  6. */
  7. private final static String TAG = MetaData.class.getSimpleName();
  8. private String mRelatedGid = null;
  9. /*
  10. * 功能描述:设置数据,即生成元数据库
  11. * 实现过程:调用JSONObject库函数put (),Task类中的setNotes ()和setName ()函数
  12. * 参数注解:
  13. */
  14. public void setMeta(String gid, JSONObject metaInfo)
  15. {
  16. //对函数块进行注释
  17. try {
  18. metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid);
  19. /*
  20. * 将这对键值放入metaInfo这个jsonobject对象中
  21. */
  22. } catch (JSONException e) {
  23. Log.e(TAG, "failed to put related gid");
  24. /*
  25. * 输出错误信息
  26. */
  27. }
  28. setNotes(metaInfo.toString());
  29. setName(GTaskStringUtils.META_NOTE_NAME);
  30. }
  31. /*
  32. * 功能描述:获取相关联的Gid
  33. */
  34. public String getRelatedGid() {
  35. return mRelatedGid;
  36. }
  37. /*
  38. * 功能描述:判断当前数据是否为空,若为空则返回真即值得保存
  39. * Made By CuiCan
  40. */
  41. @Override
  42. public boolean isWorthSaving() {
  43. return getNotes() != null;
  44. }
  45. /*
  46. * 功能描述:使用远程json数据对象设置元数据内容
  47. * 实现过程:调用父类Task中的setContentByRemoteJSON ()函数,并
  48. * 参数注解:
  49. */
  50. @Override
  51. public void setContentByRemoteJSON(JSONObject js) {
  52. super.setContentByRemoteJSON(js);
  53. if (getNotes() != null) {
  54. try {
  55. JSONObject metaInfo = new JSONObject(getNotes().trim());
  56. mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID);
  57. } catch (JSONException e) {
  58. Log.w(TAG, "failed to get related gid");
  59. /*
  60. * 输出警告信息
  61. */
  62. mRelatedGid = null;
  63. }
  64. }
  65. }
  66. /*
  67. * 功能描述:使用本地json数据对象设置元数据内容,一般不会用到,若用到,则抛出异常
  68. * Made By CuiCan
  69. */
  70. @Override
  71. public void setContentByLocalJSON(JSONObject js) {
  72. // this function should not be called
  73. throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");
  74. /*
  75. * 传递非法参数异常
  76. */
  77. }
  78. /*
  79. * 功能描述:从元数据内容中获取本地json对象,一般不会用到,若用到,则抛出异常
  80. * Made By CuiCan
  81. */
  82. @Override
  83. public JSONObject getLocalJSONFromContent() {
  84. throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called");
  85. /*
  86. * 传递非法参数异常
  87. * Made By Cui Can
  88. */
  89. }
  90. /*
  91. * 功能描述:获取同步动作状态,一般不会用到,若用到,则抛出异常
  92. * Made By CuiCan
  93. */
  94. @Override
  95. public int getSyncAction(Cursor c) {
  96. throw new IllegalAccessError("MetaData:getSyncAction should not be called");
  97. /*
  98. * 传递非法参数异常
  99. * Made By Cui Can
  100. */
  101. }
  102. }

2、Node.java

  1. package net.micode.notes.gtask.data;
  2. import android.database.Cursor;
  3. import org.json.JSONObject;
  4. /**
  5. * 应该是同步操作的基础数据类型,定义了相关指示同步操作的常量
  6. * 关键字:abstract
  7. */
  8. public abstract class Node {
  9. //定义了各种用于表征同步状态的常量
  10. public static final int SYNC_ACTION_NONE = 0;// 本地和云端都无可更新内容(即本地和云端内容一致)
  11. public static final int SYNC_ACTION_ADD_REMOTE = 1;// 需要在远程云端增加内容
  12. public static final int SYNC_ACTION_ADD_LOCAL = 2;// 需要在本地增加内容
  13. public static final int SYNC_ACTION_DEL_REMOTE = 3;// 需要在远程云端删除内容
  14. public static final int SYNC_ACTION_DEL_LOCAL = 4;// 需要在本地删除内容
  15. public static final int SYNC_ACTION_UPDATE_REMOTE = 5;// 需要将本地内容更新到远程云端
  16. public static final int SYNC_ACTION_UPDATE_LOCAL = 6;// 需要将远程云端内容更新到本地
  17. public static final int SYNC_ACTION_UPDATE_CONFLICT = 7;// 同步出现冲突
  18. public static final int SYNC_ACTION_ERROR = 8;// 同步出现错误
  19. private String mGid;
  20. private String mName;
  21. private long mLastModified;//记录最后一次修改时间
  22. private boolean mDeleted;//表征是否被删除
  23. public Node() {
  24. mGid = null;
  25. mName = "";
  26. mLastModified = 0;
  27. mDeleted = false;
  28. }
  29. public abstract JSONObject getCreateAction(int actionId);
  30. public abstract JSONObject getUpdateAction(int actionId);
  31. public abstract void setContentByRemoteJSON(JSONObject js);
  32. public abstract void setContentByLocalJSON(JSONObject js);
  33. public abstract JSONObject getLocalJSONFromContent();
  34. public abstract int getSyncAction(Cursor c);
  35. public void setGid(String gid) {
  36. this.mGid = gid;
  37. }
  38. public void setName(String name) {
  39. this.mName = name;
  40. }
  41. public void setLastModified(long lastModified) {
  42. this.mLastModified = lastModified;
  43. }
  44. public void setDeleted(boolean deleted) {
  45. this.mDeleted = deleted;
  46. }
  47. public String getGid() {
  48. return this.mGid;
  49. }
  50. public String getName() {
  51. return this.mName;
  52. }
  53. public long getLastModified() {
  54. return this.mLastModified;
  55. }
  56. public boolean getDeleted() {
  57. return this.mDeleted;
  58. }
  59. }

3、SqlData.java

  1. /*
  2. * Description:用于支持小米便签最底层的数据库相关操作,和sqlnote的关系上是子集关系,即data是note的子集(节点)。
  3. * SqlData其实就是也就是所谓数据中的数据
  4. */
  5. package net.micode.notes.gtask.data;
  6. /*
  7. * 功能描述:
  8. * 实现过程:
  9. * 参数注解:
  10. * Made By CuiCan
  11. */
  12. public class SqlData {
  13. /*
  14. * 功能描述:得到类的简写名称存入字符串TAG中
  15. * 实现过程:调用getSimpleName ()函数
  16. * Made By CuiCan
  17. */
  18. private static final String TAG = SqlData.class.getSimpleName();
  19. private static final int INVALID_ID = -99999;//为mDataId置初始值-99999
  20. /**
  21. * 来自Notes类中定义的DataColumn中的一些常量
  22. */
  23. // 集合了interface DataColumns中所有SF常量
  24. public static final String[] PROJECTION_DATA = new String[] {
  25. DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1,
  26. DataColumns.DATA3
  27. };
  28. /**
  29. * 以下五个变量作为sql表中5列的编号
  30. */
  31. public static final int DATA_ID_COLUMN = 0;
  32. public static final int DATA_MIME_TYPE_COLUMN = 1;
  33. public static final int DATA_CONTENT_COLUMN = 2;
  34. public static final int DATA_CONTENT_DATA_1_COLUMN = 3;
  35. public static final int DATA_CONTENT_DATA_3_COLUMN = 4;
  36. private ContentResolver mContentResolver;
  37. //判断是否直接用Content生成,是为true,否则为false
  38. private boolean mIsCreate;
  39. private long mDataId;
  40. private String mDataMimeType;
  41. private String mDataContent;
  42. private long mDataContentData1;
  43. private String mDataContentData3;
  44. private ContentValues mDiffDataValues;
  45. /*
  46. * 功能描述:构造函数,用于初始化数据
  47. * 参数注解:mContentResolver用于获取ContentProvider提供的数据
  48. * 参数注解: mIsCreate表征当前数据是用哪种方式创建(两种构造函数的参数不同)
  49. * 参数注解:
  50. * Made By CuiCan
  51. */
  52. public SqlData(Context context) {
  53. mContentResolver = context.getContentResolver();
  54. mIsCreate = true;
  55. mDataId = INVALID_ID;//mDataId置初始值-99999
  56. mDataMimeType = DataConstants.NOTE;
  57. mDataContent = "";
  58. mDataContentData1 = 0;
  59. mDataContentData3 = "";
  60. mDiffDataValues = new ContentValues();
  61. }
  62. /*
  63. * 功能描述:构造函数,初始化数据
  64. * 参数注解:mContentResolver用于获取ContentProvider提供的数据
  65. * 参数注解: mIsCreate表征当前数据是用哪种方式创建(两种构造函数的参数不同)
  66. * 参数注解:
  67. * Made By CuiCan
  68. */
  69. public SqlData(Context context, Cursor c) {
  70. mContentResolver = context.getContentResolver();
  71. mIsCreate = false;
  72. loadFromCursor(c);
  73. mDiffDataValues = new ContentValues();
  74. }
  75. /*
  76. * 功能描述:从光标处加载数据
  77. * 从当前的光标处将五列的数据加载到该类的对象
  78. * Made By CuiCan
  79. */
  80. private void loadFromCursor(Cursor c) {
  81. mDataId = c.getLong(DATA_ID_COLUMN);
  82. mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN);
  83. mDataContent = c.getString(DATA_CONTENT_COLUMN);
  84. mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN);
  85. mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN);
  86. }
  87. /*
  88. * 功能描述:设置用于共享的数据,并提供异常抛出与处理机制
  89. * 参数注解:
  90. * Made By CuiCan
  91. */
  92. public void setContent(JSONObject js) throws JSONException {
  93. //如果传入的JSONObject对象中有DataColumns.ID这一项,则设置,否则设为INVALID_ID
  94. long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID;
  95. if (mIsCreate || mDataId != dataId) {
  96. mDiffDataValues.put(DataColumns.ID, dataId);
  97. }
  98. mDataId = dataId;
  99. String dataMimeType = js.has(DataColumns.MIME_TYPE) ? js.getString(DataColumns.MIME_TYPE)
  100. : DataConstants.NOTE;
  101. if (mIsCreate || !mDataMimeType.equals(dataMimeType)) {
  102. mDiffDataValues.put(DataColumns.MIME_TYPE, dataMimeType);
  103. }
  104. mDataMimeType = dataMimeType;
  105. String dataContent = js.has(DataColumns.CONTENT) ? js.getString(DataColumns.CONTENT) : "";
  106. if (mIsCreate || !mDataContent.equals(dataContent)) {
  107. mDiffDataValues.put(DataColumns.CONTENT, dataContent);
  108. }
  109. mDataContent = dataContent;
  110. long dataContentData1 = js.has(DataColumns.DATA1) ? js.getLong(DataColumns.DATA1) : 0;
  111. if (mIsCreate || mDataContentData1 != dataContentData1) {
  112. mDiffDataValues.put(DataColumns.DATA1, dataContentData1);
  113. }
  114. mDataContentData1 = dataContentData1;
  115. String dataContentData3 = js.has(DataColumns.DATA3) ? js.getString(DataColumns.DATA3) : "";
  116. if (mIsCreate || !mDataContentData3.equals(dataContentData3)) {
  117. mDiffDataValues.put(DataColumns.DATA3, dataContentData3);
  118. }
  119. mDataContentData3 = dataContentData3;
  120. }
  121. /*
  122. * 功能描述:获取共享的数据内容,并提供异常抛出与处理机制
  123. * 参数注解:
  124. * Made By CuiCan
  125. */
  126. public JSONObject getContent() throws JSONException {
  127. if (mIsCreate) {
  128. Log.e(TAG, "it seems that we haven't created this in database yet");
  129. return null;
  130. }
  131. //创建JSONObject对象。并将相关数据放入其中,并返回。
  132. JSONObject js = new JSONObject();
  133. js.put(DataColumns.ID, mDataId);
  134. js.put(DataColumns.MIME_TYPE, mDataMimeType);
  135. js.put(DataColumns.CONTENT, mDataContent);
  136. js.put(DataColumns.DATA1, mDataContentData1);
  137. js.put(DataColumns.DATA3, mDataContentData3);
  138. return js;
  139. }
  140. /*
  141. * 功能描述:commit函数用于把当前造作所做的修改保存到数据库
  142. * 参数注解:
  143. * Made By CuiCan
  144. */
  145. public void commit(long noteId, boolean validateVersion, long version) {
  146. if (mIsCreate) {
  147. if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) {
  148. mDiffDataValues.remove(DataColumns.ID);
  149. }
  150. mDiffDataValues.put(DataColumns.NOTE_ID, noteId);
  151. Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues);
  152. try {
  153. mDataId = Long.valueOf(uri.getPathSegments().get(1));
  154. } catch (NumberFormatException e) {
  155. Log.e(TAG, "Get note id error :" + e.toString());
  156. throw new ActionFailureException("create note failed");
  157. }
  158. } else {
  159. if (mDiffDataValues.size() > 0) {
  160. int result = 0;
  161. if (!validateVersion) {
  162. result = mContentResolver.update(ContentUris.withAppendedId(
  163. Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null);
  164. } else {
  165. result = mContentResolver.update(ContentUris.withAppendedId(
  166. Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues,
  167. " ? in (SELECT " + NoteColumns.ID + " FROM " + TABLE.NOTE
  168. + " WHERE " + NoteColumns.VERSION + "=?)", new String[] {
  169. String.valueOf(noteId), String.valueOf(version)
  170. });
  171. }
  172. if (result == 0) {
  173. Log.w(TAG, "there is no update. maybe user updates note when syncing");
  174. }
  175. }
  176. }
  177. mDiffDataValues.clear();
  178. mIsCreate = false;
  179. }
  180. /*
  181. * 功能描述:获取当前id
  182. * 实现过程:
  183. * 参数注解:
  184. * Made By CuiCan
  185. */
  186. public long getId() {
  187. return mDataId;
  188. }
  189. }

4、SqlNote.java

  1. /*
  2. * Description:用于支持小米便签最底层的数据库相关操作,和sqldata的关系上是父集关系,即note是data的子父集。
  3. * 和SqlData相比,SqlNote算是真正意义上的数据了。
  4. */
  5. package net.micode.notes.gtask.data;
  6. /*
  7. * 功能描述:
  8. * 实现过程:
  9. * 参数注解:
  10. * Made By CuiCan
  11. */
  12. public class SqlNote {
  13. /*
  14. * 功能描述:得到类的简写名称存入字符串TAG中
  15. * 实现过程:调用getSimpleName ()函数
  16. * Made By CuiCan
  17. */
  18. private static final String TAG = SqlNote.class.getSimpleName();
  19. private static final int INVALID_ID = -99999;
  20. // 集合了interface NoteColumns中所有SF常量(17个)
  21. public static final String[] PROJECTION_NOTE = new String[] {
  22. NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID,
  23. NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE,
  24. NoteColumns.NOTES_COUNT, NoteColumns.PARENT_ID, NoteColumns.SNIPPET, NoteColumns.TYPE,
  25. NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE, NoteColumns.SYNC_ID,
  26. NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID,
  27. NoteColumns.VERSION
  28. };
  29. //以下设置17个列的编号
  30. public static final int ID_COLUMN = 0;
  31. public static final int ALERTED_DATE_COLUMN = 1;
  32. public static final int BG_COLOR_ID_COLUMN = 2;
  33. public static final int CREATED_DATE_COLUMN = 3;
  34. public static final int HAS_ATTACHMENT_COLUMN = 4;
  35. public static final int MODIFIED_DATE_COLUMN = 5;
  36. public static final int NOTES_COUNT_COLUMN = 6;
  37. public static final int PARENT_ID_COLUMN = 7;
  38. public static final int SNIPPET_COLUMN = 8;
  39. public static final int TYPE_COLUMN = 9;
  40. public static final int WIDGET_ID_COLUMN = 10;
  41. public static final int WIDGET_TYPE_COLUMN = 11;
  42. public static final int SYNC_ID_COLUMN = 12;
  43. public static final int LOCAL_MODIFIED_COLUMN = 13;
  44. public static final int ORIGIN_PARENT_ID_COLUMN = 14;
  45. public static final int GTASK_ID_COLUMN = 15;
  46. public static final int VERSION_COLUMN = 16;
  47. //一下定义了17个内部的变量,其中12个可以由content中获得,5个需要初始化为0或者new
  48. private Context mContext;
  49. private ContentResolver mContentResolver;
  50. private boolean mIsCreate;
  51. private long mId;
  52. private long mAlertDate;
  53. private int mBgColorId;
  54. private long mCreatedDate;
  55. private int mHasAttachment;
  56. private long mModifiedDate;
  57. private long mParentId;
  58. private String mSnippet;
  59. private int mType;
  60. private int mWidgetId;
  61. private int mWidgetType;
  62. private long mOriginParent;
  63. private long mVersion;
  64. private ContentValues mDiffNoteValues;
  65. private ArrayList<SqlData> mDataList;
  66. /*
  67. * 功能描述:构造函数
  68. * 参数注解: mIsCreate用于标示构造方式
  69. * 参数注解:
  70. * Made By CuiCan
  71. */
  72. //构造函数只有context,对所有的变量进行初始化
  73. public SqlNote(Context context) {
  74. mContext = context;
  75. mContentResolver = context.getContentResolver();
  76. mIsCreate = true;
  77. mId = INVALID_ID;
  78. mAlertDate = 0;
  79. mBgColorId = ResourceParser.getDefaultBgId(context);
  80. mCreatedDate = System.currentTimeMillis();//调用系统函数获得创建时间
  81. mHasAttachment = 0;
  82. mModifiedDate = System.currentTimeMillis();//最后一次修改时间初始化为创建时间
  83. mParentId = 0;
  84. mSnippet = "";
  85. mType = Notes.TYPE_NOTE;
  86. mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
  87. mWidgetType = Notes.TYPE_WIDGET_INVALIDE;
  88. mOriginParent = 0;
  89. mVersion = 0;
  90. mDiffNoteValues = new ContentValues();
  91. mDataList = new ArrayList<SqlData>();
  92. }
  93. /*
  94. * 功能描述:构造函数
  95. * 参数注解: mIsCreate用于标示构造方式
  96. * 参数注解:
  97. * Made By CuiCan
  98. */
  99. //构造函数有context和一个数据库的cursor,多数变量通过cursor指向的一条记录直接进行初始化
  100. public SqlNote(Context context, Cursor c) {
  101. mContext = context;
  102. mContentResolver = context.getContentResolver();
  103. mIsCreate = false;
  104. loadFromCursor(c);
  105. mDataList = new ArrayList<SqlData>();
  106. //
  107. if (mType == Notes.TYPE_NOTE)
  108. loadDataContent();
  109. mDiffNoteValues = new ContentValues();
  110. }
  111. /*
  112. * 功能描述:构造函数
  113. * 参数注解: mIsCreate用于标示构造方式
  114. * 参数注解:
  115. * Made By CuiCan
  116. */
  117. public SqlNote(Context context, long id) {
  118. mContext = context;
  119. mContentResolver = context.getContentResolver();
  120. mIsCreate = false;
  121. loadFromCursor(id);
  122. mDataList = new ArrayList<SqlData>();
  123. if (mType == Notes.TYPE_NOTE)
  124. loadDataContent();
  125. mDiffNoteValues = new ContentValues();
  126. }
  127. /*
  128. * 功能描述:通过id从光标处加载数据
  129. * Made By CuiCan
  130. */
  131. private void loadFromCursor(long id) {
  132. Cursor c = null;
  133. try {
  134. c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)",
  135. new String[] {
  136. String.valueOf(id)
  137. }, null);//通过id获得对应的ContentResolver中的cursor
  138. if (c != null) {
  139. c.moveToNext();
  140. loadFromCursor(c);//然后加载数据进行初始化,这样函数
  141. //SqlNote(Context context, long id)与SqlNote(Context context, long id)的实现方式基本相同
  142. } else {
  143. Log.w(TAG, "loadFromCursor: cursor = null");
  144. }
  145. } finally {
  146. if (c != null)
  147. c.close();
  148. }
  149. }
  150. /*
  151. * 功能描述:通过游标从光标处加载数据
  152. * Made By CuiCan
  153. */
  154. private void loadFromCursor(Cursor c) {
  155. //直接从一条记录中的获得以下变量的初始值
  156. mId = c.getLong(ID_COLUMN);
  157. mAlertDate = c.getLong(ALERTED_DATE_COLUMN);
  158. mBgColorId = c.getInt(BG_COLOR_ID_COLUMN);
  159. mCreatedDate = c.getLong(CREATED_DATE_COLUMN);
  160. mHasAttachment = c.getInt(HAS_ATTACHMENT_COLUMN);
  161. mModifiedDate = c.getLong(MODIFIED_DATE_COLUMN);
  162. mParentId = c.getLong(PARENT_ID_COLUMN);
  163. mSnippet = c.getString(SNIPPET_COLUMN);
  164. mType = c.getInt(TYPE_COLUMN);
  165. mWidgetId = c.getInt(WIDGET_ID_COLUMN);
  166. mWidgetType = c.getInt(WIDGET_TYPE_COLUMN);
  167. mVersion = c.getLong(VERSION_COLUMN);
  168. }
  169. /*
  170. * 功能描述:通过content机制获取共享数据并加载到数据库当前游标处
  171. * 参数注解:
  172. * Made By CuiCan
  173. */
  174. private void loadDataContent() {
  175. Cursor c = null;
  176. mDataList.clear();
  177. try {
  178. c = mContentResolver.query(Notes.CONTENT_DATA_URI, SqlData.PROJECTION_DATA,
  179. "(note_id=?)", new String[] {
  180. String.valueOf(mId)
  181. }, null);
  182. if (c != null) {
  183. if (c.getCount() == 0) {
  184. Log.w(TAG, "it seems that the note has not data");
  185. return;
  186. }
  187. while (c.moveToNext()) {
  188. SqlData data = new SqlData(mContext, c);
  189. mDataList.add(data);
  190. }
  191. } else {
  192. Log.w(TAG, "loadDataContent: cursor = null");
  193. }
  194. } finally {
  195. if (c != null)
  196. c.close();
  197. }
  198. }
  199. /*
  200. * 功能描述:设置通过content机制用于共享的数据信息
  201. * 参数注解:
  202. * Made By CuiCan
  203. */
  204. public boolean setContent(JSONObject js) {
  205. try {
  206. JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
  207. if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
  208. Log.w(TAG, "cannot set system folder");
  209. } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) {
  210. // for folder we can only update the snnipet and type
  211. String snippet = note.has(NoteColumns.SNIPPET) ? note
  212. .getString(NoteColumns.SNIPPET) : "";
  213. if (mIsCreate || !mSnippet.equals(snippet)) {
  214. mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
  215. }
  216. mSnippet = snippet;
  217. int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
  218. : Notes.TYPE_NOTE;
  219. if (mIsCreate || mType != type) {
  220. mDiffNoteValues.put(NoteColumns.TYPE, type);
  221. }
  222. mType = type;
  223. } else if (note.getInt(NoteColumns.TYPE) == Notes.TYPE_NOTE) {
  224. JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
  225. long id = note.has(NoteColumns.ID) ? note.getLong(NoteColumns.ID) : INVALID_ID;
  226. if (mIsCreate || mId != id) {
  227. mDiffNoteValues.put(NoteColumns.ID, id);
  228. }
  229. mId = id;
  230. long alertDate = note.has(NoteColumns.ALERTED_DATE) ? note
  231. .getLong(NoteColumns.ALERTED_DATE) : 0;
  232. if (mIsCreate || mAlertDate != alertDate) {
  233. mDiffNoteValues.put(NoteColumns.ALERTED_DATE, alertDate);
  234. }
  235. mAlertDate = alertDate;
  236. int bgColorId = note.has(NoteColumns.BG_COLOR_ID) ? note
  237. .getInt(NoteColumns.BG_COLOR_ID) : ResourceParser.getDefaultBgId(mContext);
  238. if (mIsCreate || mBgColorId != bgColorId) {
  239. mDiffNoteValues.put(NoteColumns.BG_COLOR_ID, bgColorId);
  240. }
  241. mBgColorId = bgColorId;
  242. long createDate = note.has(NoteColumns.CREATED_DATE) ? note
  243. .getLong(NoteColumns.CREATED_DATE) : System.currentTimeMillis();
  244. if (mIsCreate || mCreatedDate != createDate) {
  245. mDiffNoteValues.put(NoteColumns.CREATED_DATE, createDate);
  246. }
  247. mCreatedDate = createDate;
  248. int hasAttachment = note.has(NoteColumns.HAS_ATTACHMENT) ? note
  249. .getInt(NoteColumns.HAS_ATTACHMENT) : 0;
  250. if (mIsCreate || mHasAttachment != hasAttachment) {
  251. mDiffNoteValues.put(NoteColumns.HAS_ATTACHMENT, hasAttachment);
  252. }
  253. mHasAttachment = hasAttachment;
  254. long modifiedDate = note.has(NoteColumns.MODIFIED_DATE) ? note
  255. .getLong(NoteColumns.MODIFIED_DATE) : System.currentTimeMillis();
  256. if (mIsCreate || mModifiedDate != modifiedDate) {
  257. mDiffNoteValues.put(NoteColumns.MODIFIED_DATE, modifiedDate);
  258. }
  259. mModifiedDate = modifiedDate;
  260. long parentId = note.has(NoteColumns.PARENT_ID) ? note
  261. .getLong(NoteColumns.PARENT_ID) : 0;
  262. if (mIsCreate || mParentId != parentId) {
  263. mDiffNoteValues.put(NoteColumns.PARENT_ID, parentId);
  264. }
  265. mParentId = parentId;
  266. String snippet = note.has(NoteColumns.SNIPPET) ? note
  267. .getString(NoteColumns.SNIPPET) : "";
  268. if (mIsCreate || !mSnippet.equals(snippet)) {
  269. mDiffNoteValues.put(NoteColumns.SNIPPET, snippet);
  270. }
  271. mSnippet = snippet;
  272. int type = note.has(NoteColumns.TYPE) ? note.getInt(NoteColumns.TYPE)
  273. : Notes.TYPE_NOTE;
  274. if (mIsCreate || mType != type) {
  275. mDiffNoteValues.put(NoteColumns.TYPE, type);
  276. }
  277. mType = type;
  278. int widgetId = note.has(NoteColumns.WIDGET_ID) ? note.getInt(NoteColumns.WIDGET_ID)
  279. : AppWidgetManager.INVALID_APPWIDGET_ID;
  280. if (mIsCreate || mWidgetId != widgetId) {
  281. mDiffNoteValues.put(NoteColumns.WIDGET_ID, widgetId);
  282. }
  283. mWidgetId = widgetId;
  284. int widgetType = note.has(NoteColumns.WIDGET_TYPE) ? note
  285. .getInt(NoteColumns.WIDGET_TYPE) : Notes.TYPE_WIDGET_INVALIDE;
  286. if (mIsCreate || mWidgetType != widgetType) {
  287. mDiffNoteValues.put(NoteColumns.WIDGET_TYPE, widgetType);
  288. }
  289. mWidgetType = widgetType;
  290. long originParent = note.has(NoteColumns.ORIGIN_PARENT_ID) ? note
  291. .getLong(NoteColumns.ORIGIN_PARENT_ID) : 0;
  292. if (mIsCreate || mOriginParent != originParent) {
  293. mDiffNoteValues.put(NoteColumns.ORIGIN_PARENT_ID, originParent);
  294. }
  295. mOriginParent = originParent;
  296. for (int i = 0; i < dataArray.length(); i++) {
  297. JSONObject data = dataArray.getJSONObject(i);
  298. SqlData sqlData = null;
  299. if (data.has(DataColumns.ID)) {
  300. long dataId = data.getLong(DataColumns.ID);
  301. for (SqlData temp : mDataList) {
  302. if (dataId == temp.getId()) {
  303. sqlData = temp;
  304. }
  305. }
  306. }
  307. if (sqlData == null) {
  308. sqlData = new SqlData(mContext);
  309. mDataList.add(sqlData);
  310. }
  311. sqlData.setContent(data);
  312. }
  313. }
  314. } catch (JSONException e) {
  315. Log.e(TAG, e.toString());
  316. e.printStackTrace();
  317. return false;
  318. }
  319. return true;
  320. }
  321. /*
  322. * 功能描述:获取content机制提供的数据并加载到note中
  323. * 参数注解:
  324. * Made By CuiCan
  325. */
  326. public JSONObject getContent() {
  327. try {
  328. JSONObject js = new JSONObject();
  329. if (mIsCreate) {
  330. Log.e(TAG, "it seems that we haven't created this in database yet");
  331. return null;
  332. }
  333. JSONObject note = new JSONObject();
  334. if (mType == Notes.TYPE_NOTE) {//类型为note时
  335. note.put(NoteColumns.ID, mId);
  336. note.put(NoteColumns.ALERTED_DATE, mAlertDate);
  337. note.put(NoteColumns.BG_COLOR_ID, mBgColorId);
  338. note.put(NoteColumns.CREATED_DATE, mCreatedDate);
  339. note.put(NoteColumns.HAS_ATTACHMENT, mHasAttachment);
  340. note.put(NoteColumns.MODIFIED_DATE, mModifiedDate);
  341. note.put(NoteColumns.PARENT_ID, mParentId);
  342. note.put(NoteColumns.SNIPPET, mSnippet);
  343. note.put(NoteColumns.TYPE, mType);
  344. note.put(NoteColumns.WIDGET_ID, mWidgetId);
  345. note.put(NoteColumns.WIDGET_TYPE, mWidgetType);
  346. note.put(NoteColumns.ORIGIN_PARENT_ID, mOriginParent);
  347. js.put(GTaskStringUtils.META_HEAD_NOTE, note);
  348. JSONArray dataArray = new JSONArray();
  349. for (SqlData sqlData : mDataList) {
  350. JSONObject data = sqlData.getContent();
  351. if (data != null) {
  352. dataArray.put(data);
  353. }
  354. }
  355. js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
  356. } else if (mType == Notes.TYPE_FOLDER || mType == Notes.TYPE_SYSTEM) {//类型为文件夹或者
  357. note.put(NoteColumns.ID, mId);
  358. note.put(NoteColumns.TYPE, mType);
  359. note.put(NoteColumns.SNIPPET, mSnippet);
  360. js.put(GTaskStringUtils.META_HEAD_NOTE, note);
  361. }
  362. return js;
  363. } catch (JSONException e) {
  364. Log.e(TAG, e.toString());
  365. e.printStackTrace();
  366. }
  367. return null;
  368. }
  369. /*
  370. * 功能描述:给当前id设置父id
  371. * 参数注解:
  372. * Made By CuiCan
  373. */
  374. public void setParentId(long id) {
  375. mParentId = id;
  376. mDiffNoteValues.put(NoteColumns.PARENT_ID, id);
  377. }
  378. /*
  379. * 功能描述:给当前id设置Gtaskid
  380. * 参数注解:
  381. * Made By CuiCan
  382. */
  383. public void setGtaskId(String gid) {
  384. mDiffNoteValues.put(NoteColumns.GTASK_ID, gid);
  385. }
  386. /*
  387. * 功能描述:给当前id设置同步id
  388. * 参数注解:
  389. * Made By CuiCan
  390. */
  391. public void setSyncId(long syncId) {
  392. mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId);
  393. }
  394. /*
  395. * 功能描述:初始化本地修改,即撤销所有当前修改
  396. * 参数注解:
  397. * Made By CuiCan
  398. */
  399. public void resetLocalModified() {
  400. mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0);
  401. }
  402. /*
  403. * 功能描述:获得当前id
  404. * 参数注解:
  405. * Made By CuiCan
  406. */
  407. public long getId() {
  408. return mId;
  409. }
  410. /*
  411. * 功能描述:获得当前id的父id
  412. * 参数注解:
  413. * Made By CuiCan
  414. */
  415. public long getParentId() {
  416. return mParentId;
  417. }
  418. /*
  419. * 功能描述:获取小片段即用于显示的部分便签内容
  420. * 参数注解:
  421. * Made By CuiCan
  422. */
  423. public String getSnippet() {
  424. return mSnippet;
  425. }
  426. /*
  427. * 功能描述:判断是否为便签类型
  428. * 参数注解:
  429. * Made By CuiCan
  430. */
  431. public boolean isNoteType() {
  432. return mType == Notes.TYPE_NOTE;
  433. }
  434. /*
  435. * 功能描述:commit函数用于把当前造作所做的修改保存到数据库
  436. * 参数注解:
  437. * Made By CuiCan
  438. */
  439. public void commit(boolean validateVersion) {
  440. if (mIsCreate) {
  441. if (mId == INVALID_ID && mDiffNoteValues.containsKey(NoteColumns.ID)) {
  442. mDiffNoteValues.remove(NoteColumns.ID);
  443. }
  444. Uri uri = mContentResolver.insert(Notes.CONTENT_NOTE_URI, mDiffNoteValues);
  445. try {
  446. mId = Long.valueOf(uri.getPathSegments().get(1));
  447. } catch (NumberFormatException e) {
  448. Log.e(TAG, "Get note id error :" + e.toString());
  449. throw new ActionFailureException("create note failed");
  450. }
  451. if (mId == 0) {
  452. throw new IllegalStateException("Create thread id failed");
  453. }
  454. if (mType == Notes.TYPE_NOTE) {
  455. for (SqlData sqlData : mDataList) {//直接使用sqldata中的实现
  456. sqlData.commit(mId, false, -1);
  457. }
  458. }
  459. } else {
  460. if (mId <= 0 && mId != Notes.ID_ROOT_FOLDER && mId != Notes.ID_CALL_RECORD_FOLDER) {
  461. Log.e(TAG, "No such note");
  462. throw new IllegalStateException("Try to update note with invalid id");
  463. }
  464. if (mDiffNoteValues.size() > 0) {
  465. mVersion ++;
  466. int result = 0;
  467. if (!validateVersion) {//构造字符串
  468. result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
  469. + NoteColumns.ID + "=?)", new String[] {
  470. String.valueOf(mId)
  471. });
  472. } else {
  473. result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "("
  474. + NoteColumns.ID + "=?) AND (" + NoteColumns.VERSION + "<=?)",
  475. new String[] {
  476. String.valueOf(mId), String.valueOf(mVersion)
  477. });
  478. }
  479. if (result == 0) {
  480. Log.w(TAG, "there is no update. maybe user updates note when syncing");
  481. }
  482. }
  483. if (mType == Notes.TYPE_NOTE) {
  484. for (SqlData sqlData : mDataList) {
  485. sqlData.commit(mId, validateVersion, mVersion);
  486. }
  487. }
  488. }
  489. // refresh local info
  490. loadFromCursor(mId);
  491. if (mType == Notes.TYPE_NOTE)
  492. loadDataContent();
  493. mDiffNoteValues.clear();
  494. mIsCreate = false;
  495. }
  496. }

5、Task.java

  1. package net.micode.notes.gtask.data;
  2. public class Task extends Node {
  3. private static final String TAG = Task.class.getSimpleName();
  4. private boolean mCompleted;//是否完成
  5. private String mNotes;
  6. private JSONObject mMetaInfo;//将在实例中存储数据的类型
  7. private Task mPriorSibling;//对应的优先兄弟Task的指针(待完善)
  8. private TaskList mParent;//所在的任务列表的指针
  9. public Task() {
  10. super();
  11. mCompleted = false;
  12. mNotes = null;
  13. mPriorSibling = null;//TaskList中当前Task前面的Task的指针
  14. mParent = null;//当前Task所在的TaskList
  15. mMetaInfo = null;
  16. }
  17. public JSONObject getCreateAction(int actionId) {
  18. JSONObject js = new JSONObject();
  19. try {
  20. // action_type
  21. js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
  22. GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);
  23. // action_id
  24. js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
  25. // index
  26. js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this));
  27. // entity_delta
  28. JSONObject entity = new JSONObject();
  29. entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
  30. entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null");
  31. entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE,
  32. GTaskStringUtils.GTASK_JSON_TYPE_TASK);
  33. if (getNotes() != null) {
  34. entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes());
  35. }
  36. js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);
  37. // parent_id
  38. if (mParent!= null) {
  39. js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid());
  40. }
  41. // dest_parent_type
  42. js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE,
  43. GTaskStringUtils.GTASK_JSON_TYPE_GROUP);
  44. // list_id
  45. if (mParent!= null) {
  46. js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid());
  47. }
  48. // prior_sibling_id
  49. if (mPriorSibling != null) {
  50. js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid());
  51. }
  52. } catch (JSONException e) {
  53. Log.e(TAG, e.toString());
  54. e.printStackTrace();
  55. throw new ActionFailureException("fail to generate task-create jsonobject");
  56. }
  57. return js;
  58. }
  59. public JSONObject getUpdateAction(int actionId) {
  60. JSONObject js = new JSONObject();
  61. try {
  62. // action_type
  63. js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
  64. GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);
  65. // action_id
  66. js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
  67. // id
  68. js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());
  69. // entity_delta
  70. JSONObject entity = new JSONObject();
  71. entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
  72. if (getNotes() != null) {
  73. entity.put(GTaskStringUtils.GTASK_JSON_NOTES, getNotes());
  74. }
  75. entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted());
  76. js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);
  77. } catch (JSONException e) {
  78. Log.e(TAG, e.toString());
  79. e.printStackTrace();
  80. throw new ActionFailureException("fail to generate task-update jsonobject");
  81. }
  82. return js;
  83. }
  84. public void setContentByRemoteJSON(JSONObject js) {
  85. if (js != null) {
  86. try {
  87. // id
  88. if (js.has(GTaskStringUtils.GTASK_JSON_ID)) {
  89. setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID));
  90. }
  91. // last_modified
  92. if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) {
  93. setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED));
  94. }
  95. // name
  96. if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) {
  97. setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME));
  98. }
  99. // notes
  100. if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) {
  101. setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES));
  102. }
  103. // deleted
  104. if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) {
  105. setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED));
  106. }
  107. // completed
  108. if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) {
  109. setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED));
  110. }
  111. } catch (JSONException e) {
  112. Log.e(TAG, e.toString());
  113. e.printStackTrace();
  114. throw new ActionFailureException("fail to get task content from jsonobject");
  115. }
  116. }
  117. }
  118. public void setContentByLocalJSON(JSONObject js) { //��metadata����ʵʩ
  119. if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)
  120. || !js.has(GTaskStringUtils.META_HEAD_DATA)) {
  121. Log.w(TAG, "setContentByLocalJSON: nothing is avaiable");
  122. }
  123. try {
  124. JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
  125. JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
  126. if (note.getInt(NoteColumns.TYPE) != Notes.TYPE_NOTE) {
  127. Log.e(TAG, "invalid type");
  128. return;
  129. }
  130. for (int i = 0; i < dataArray.length(); i++) {
  131. JSONObject data = dataArray.getJSONObject(i);
  132. if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) {
  133. setName(data.getString(DataColumns.CONTENT));
  134. break;
  135. }
  136. }
  137. } catch (JSONException e) {
  138. Log.e(TAG, e.toString());
  139. e.printStackTrace();
  140. }
  141. }
  142. public JSONObject getLocalJSONFromContent() {
  143. String name = getName();
  144. try {
  145. if (mMetaInfo == null) {
  146. // new task created from web
  147. if (name == null) {
  148. Log.w(TAG, "the note seems to be an empty one");
  149. return null;
  150. }
  151. JSONObject js = new JSONObject();
  152. JSONObject note = new JSONObject();
  153. JSONArray dataArray = new JSONArray();
  154. JSONObject data = new JSONObject();
  155. data.put(DataColumns.CONTENT, name);
  156. dataArray.put(data);
  157. js.put(GTaskStringUtils.META_HEAD_DATA, dataArray);
  158. note.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
  159. js.put(GTaskStringUtils.META_HEAD_NOTE, note);
  160. return js;
  161. } else {
  162. // synced task
  163. JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
  164. JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
  165. for (int i = 0; i < dataArray.length(); i++) {
  166. JSONObject data = dataArray.getJSONObject(i);
  167. if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) {
  168. data.put(DataColumns.CONTENT, getName());
  169. break;
  170. }
  171. }
  172. note.put(NoteColumns.TYPE, Notes.TYPE_NOTE);
  173. return mMetaInfo;
  174. }
  175. } catch (JSONException e) {
  176. Log.e(TAG, e.toString());
  177. e.printStackTrace();
  178. return null;
  179. }
  180. }
  181. public void setMetaInfo(MetaData metaData) {
  182. if (metaData != null && metaData.getNotes() != null) {
  183. try {
  184. mMetaInfo = new JSONObject(metaData.getNotes());
  185. } catch (JSONException e) {
  186. Log.w(TAG, e.toString());
  187. mMetaInfo = null;
  188. }
  189. }
  190. }
  191. public int getSyncAction(Cursor c) {
  192. try {
  193. JSONObject noteInfo = null;
  194. if (mMetaInfo != null && mMetaInfo.has(GTaskStringUtils.META_HEAD_NOTE)) {
  195. noteInfo = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
  196. }
  197. if (noteInfo == null) {
  198. Log.w(TAG, "it seems that note meta has been deleted");
  199. return SYNC_ACTION_UPDATE_REMOTE;
  200. }
  201. if (!noteInfo.has(NoteColumns.ID)) {
  202. Log.w(TAG, "remote note id seems to be deleted");
  203. return SYNC_ACTION_UPDATE_LOCAL;
  204. }
  205. // validate the note id now
  206. if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) {
  207. Log.w(TAG, "note id doesn't match");
  208. return SYNC_ACTION_UPDATE_LOCAL;
  209. }
  210. if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) {
  211. // there is no local update
  212. if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
  213. // no update both side
  214. return SYNC_ACTION_NONE;
  215. } else {
  216. // apply remote to local
  217. return SYNC_ACTION_UPDATE_LOCAL;
  218. }
  219. } else {
  220. // validate gtask id
  221. if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) {
  222. Log.e(TAG, "gtask id doesn't match");
  223. return SYNC_ACTION_ERROR;
  224. }
  225. if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
  226. // local modification only
  227. return SYNC_ACTION_UPDATE_REMOTE;
  228. } else {
  229. return SYNC_ACTION_UPDATE_CONFLICT;
  230. }
  231. }
  232. } catch (Exception e) {
  233. Log.e(TAG, e.toString());
  234. e.printStackTrace();
  235. }
  236. return SYNC_ACTION_ERROR;
  237. }
  238. public boolean isWorthSaving() {
  239. return mMetaInfo != null || (getName() != null && getName().trim().length() > 0)
  240. || (getNotes() != null && getNotes().trim().length() > 0);
  241. }
  242. public void setCompleted(boolean completed) {
  243. this.mCompleted = completed;
  244. }
  245. public void setNotes(String notes) {
  246. this.mNotes = notes;
  247. }
  248. public void setPriorSibling(Task priorSibling) {
  249. this.mPriorSibling = priorSibling;
  250. }
  251. public void setParent(TaskList parent) {
  252. this.mParent = parent;
  253. }
  254. public boolean getCompleted() {
  255. return this.mCompleted;
  256. }
  257. public String getNotes() {
  258. return this.mNotes;
  259. }
  260. public Task getPriorSibling() {
  261. return this.mPriorSibling;
  262. }
  263. public TaskList getParent() {
  264. return this.mParent;
  265. }
  266. }

6、TaskList.java

  1. package net.micode.notes.gtask.data;
  2. public class TaskList extends Node {
  3. private static final String TAG = TaskList.class.getSimpleName();//tag标记
  4. private int mIndex;//当前TaskList的指针
  5. private ArrayList<Task> mChildren;//类中主要的保存数据的单元,用来实现一个以Task为元素的ArrayList
  6. public TaskList() {
  7. super();
  8. mChildren = new ArrayList<Task>();
  9. mIndex = 1;
  10. }
  11. /* (non-Javadoc)
  12. * @see net.micode.notes.gtask.data.Node#getCreateAction(int)
  13. * 生成并返回一个包含了一定数据的JSONObject实体
  14. */
  15. public JSONObject getCreateAction(int actionId) {
  16. JSONObject js = new JSONObject();
  17. try {
  18. // action_type
  19. js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
  20. GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE);
  21. // action_id
  22. js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
  23. // index
  24. js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex);
  25. // entity_delta
  26. JSONObject entity = new JSONObject();//entity实体
  27. entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
  28. entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null");
  29. entity.put(GTaskStringUtils.GTASK_JSON_ENTITY_TYPE,
  30. GTaskStringUtils.GTASK_JSON_TYPE_GROUP);
  31. js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);
  32. } catch (JSONException e) {
  33. Log.e(TAG, e.toString());
  34. e.printStackTrace();
  35. throw new ActionFailureException("fail to generate tasklist-create jsonobject");
  36. }
  37. return js;
  38. }
  39. /* (non-Javadoc)
  40. * @see net.micode.notes.gtask.data.Node#getUpdateAction(int)
  41. * 生成并返回一个包含了一定数据的JSONObject实体
  42. */
  43. public JSONObject getUpdateAction(int actionId) {
  44. JSONObject js = new JSONObject();
  45. try {
  46. // action_type
  47. js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
  48. GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE);
  49. // action_id
  50. js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId);
  51. // id
  52. js.put(GTaskStringUtils.GTASK_JSON_ID, getGid());
  53. // entity_delta
  54. JSONObject entity = new JSONObject();
  55. entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName());
  56. entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted());
  57. js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity);
  58. } catch (JSONException e) {
  59. Log.e(TAG, e.toString());
  60. e.printStackTrace();
  61. throw new ActionFailureException("fail to generate tasklist-update jsonobject");
  62. }
  63. return js;
  64. }
  65. public void setContentByRemoteJSON(JSONObject js) {
  66. if (js != null) {
  67. try {
  68. // id
  69. if (js.has(GTaskStringUtils.GTASK_JSON_ID)) {
  70. setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID));
  71. }
  72. // last_modified
  73. if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) {
  74. setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED));
  75. }
  76. // name
  77. if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) {
  78. setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME));
  79. }
  80. } catch (JSONException e) {
  81. Log.e(TAG, e.toString());
  82. e.printStackTrace();
  83. throw new ActionFailureException("fail to get tasklist content from jsonobject");
  84. }
  85. }
  86. }
  87. public void setContentByLocalJSON(JSONObject js) {
  88. if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) {
  89. Log.w(TAG, "setContentByLocalJSON: nothing is avaiable");
  90. }
  91. try {
  92. JSONObject folder = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
  93. if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) {
  94. String name = folder.getString(NoteColumns.SNIPPET);
  95. setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name);
  96. } else if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) {
  97. if (folder.getLong(NoteColumns.ID) == Notes.ID_ROOT_FOLDER)
  98. setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT);
  99. else if (folder.getLong(NoteColumns.ID) == Notes.ID_CALL_RECORD_FOLDER)
  100. setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX
  101. + GTaskStringUtils.FOLDER_CALL_NOTE);
  102. else
  103. Log.e(TAG, "invalid system folder");
  104. } else {
  105. Log.e(TAG, "error type");
  106. }
  107. } catch (JSONException e) {
  108. Log.e(TAG, e.toString());
  109. e.printStackTrace();
  110. }
  111. }
  112. public JSONObject getLocalJSONFromContent() {
  113. try {
  114. JSONObject js = new JSONObject();
  115. JSONObject folder = new JSONObject();
  116. String folderName = getName();
  117. if (getName().startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX))
  118. folderName = folderName.substring(GTaskStringUtils.MIUI_FOLDER_PREFFIX.length(),
  119. folderName.length());
  120. folder.put(NoteColumns.SNIPPET, folderName);
  121. if (folderName.equals(GTaskStringUtils.FOLDER_DEFAULT)
  122. || folderName.equals(GTaskStringUtils.FOLDER_CALL_NOTE))
  123. folder.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM);
  124. else
  125. folder.put(NoteColumns.TYPE, Notes.TYPE_FOLDER);
  126. js.put(GTaskStringUtils.META_HEAD_NOTE, folder);
  127. return js;
  128. } catch (JSONException e) {
  129. Log.e(TAG, e.toString());
  130. e.printStackTrace();
  131. return null;
  132. }
  133. }
  134. public int getSyncAction(Cursor c) {
  135. try {
  136. if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) {
  137. // there is no local update
  138. if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
  139. // no update both side
  140. return SYNC_ACTION_NONE;
  141. } else {
  142. // apply remote to local
  143. return SYNC_ACTION_UPDATE_LOCAL;
  144. }
  145. } else {
  146. // validate gtask id
  147. if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) {
  148. Log.e(TAG, "gtask id doesn't match");
  149. return SYNC_ACTION_ERROR;
  150. }
  151. if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) {
  152. // local modification only
  153. return SYNC_ACTION_UPDATE_REMOTE;
  154. } else {
  155. // for folder conflicts, just apply local modification
  156. return SYNC_ACTION_UPDATE_REMOTE;
  157. }
  158. }
  159. } catch (Exception e) {
  160. Log.e(TAG, e.toString());
  161. e.printStackTrace();
  162. }
  163. return SYNC_ACTION_ERROR;
  164. }
  165. /**
  166. * @return
  167. * 功能:获得TaskList的大小,即mChildren的大小
  168. */
  169. public int getChildTaskCount() {
  170. return mChildren.size();
  171. }
  172. /**
  173. * @param task
  174. * @return 返回值为是否成功添加任务。
  175. * 功能:在当前任务表末尾添加新的任务。
  176. */
  177. public boolean addChildTask(Task task) {
  178. boolean ret = false;
  179. if (task != null && !mChildren.contains(task)) {
  180. ret = mChildren.add(task);
  181. if (ret) {
  182. // need to set prior sibling and parent
  183. task.setPriorSibling(mChildren.isEmpty() ? null : mChildren
  184. .get(mChildren.size() - 1));
  185. task.setParent(this);
  186. //注意:每一次ArrayList的变化都要紧跟相关Task中PriorSibling的更改
  187. //,接下来几个函数都有相关操作
  188. }
  189. }
  190. return ret;
  191. }
  192. /**
  193. * @param task
  194. * @param index
  195. * @return
  196. * 功能:在当前任务表的指定位置添加新的任务。
  197. */
  198. public boolean addChildTask(Task task, int index) {
  199. if (index < 0 || index > mChildren.size()) {
  200. Log.e(TAG, "add child task: invalid index");
  201. return false;
  202. }
  203. int pos = mChildren.indexOf(task);
  204. if (task != null && pos == -1) {
  205. mChildren.add(index, task);
  206. // update the task list
  207. Task preTask = null;
  208. Task afterTask = null;
  209. if (index != 0)
  210. preTask = mChildren.get(index - 1);
  211. if (index != mChildren.size() - 1)
  212. afterTask = mChildren.get(index + 1);
  213. task.setPriorSibling(preTask);
  214. if (afterTask != null)
  215. afterTask.setPriorSibling(task);
  216. }
  217. return true;
  218. }
  219. /**
  220. * @param task
  221. * @return 返回删除是否成功
  222. * 功能:删除TaskList中的一个Task
  223. */
  224. public boolean removeChildTask(Task task) {
  225. boolean ret = false;
  226. int index = mChildren.indexOf(task);
  227. if (index != -1) {
  228. ret = mChildren.remove(task);
  229. if (ret) {
  230. // reset prior sibling and parent
  231. task.setPriorSibling(null);
  232. task.setParent(null);
  233. // update the task list
  234. if (index != mChildren.size()) {
  235. mChildren.get(index).setPriorSibling(
  236. index == 0 ? null : mChildren.get(index - 1));
  237. }
  238. }
  239. }
  240. return ret;
  241. }
  242. /**
  243. * @param task
  244. * @param index
  245. * @return
  246. * 功能:将当前TaskList中含有的某个Task移到index位置
  247. */
  248. public boolean moveChildTask(Task task, int index) {
  249. if (index < 0 || index >= mChildren.size()) {
  250. Log.e(TAG, "move child task: invalid index");
  251. return false;
  252. }
  253. int pos = mChildren.indexOf(task);
  254. if (pos == -1) {
  255. Log.e(TAG, "move child task: the task should in the list");
  256. return false;
  257. }
  258. if (pos == index)
  259. return true;
  260. return (removeChildTask(task) && addChildTask(task, index));
  261. //利用已实现好的功能完成当下功能;
  262. }
  263. /**
  264. * @param gid
  265. * @return返回寻找结果
  266. * 功能:按gid寻找Task
  267. */
  268. public Task findChildTaskByGid(String gid) {
  269. for (int i = 0; i < mChildren.size(); i++) {
  270. Task t = mChildren.get(i);
  271. if (t.getGid().equals(gid)) {
  272. return t;
  273. }
  274. }
  275. return null;
  276. }
  277. /**
  278. * @param task
  279. * @return
  280. * 功能:返回指定Task的index
  281. */
  282. public int getChildTaskIndex(Task task) {
  283. return mChildren.indexOf(task);
  284. }
  285. /**
  286. * @param index
  287. * @return
  288. * 功能:返回指定index的Task
  289. */
  290. public Task getChildTaskByIndex(int index) {
  291. if (index < 0 || index >= mChildren.size()) {
  292. Log.e(TAG, "getTaskByIndex: invalid index");
  293. return null;
  294. }
  295. return mChildren.get(index);
  296. }
  297. /**
  298. * @param gid
  299. * @return
  300. * 功能:返回指定gid的Task
  301. */
  302. public Task getChilTaskByGid(String gid) {
  303. for (Task task : mChildren) {//一种常见的ArrayList的遍历方法(四种,见精读笔记)
  304. if (task.getGid().equals(gid))
  305. return task;
  306. }
  307. return null;
  308. }
  309. public ArrayList<Task> getChildTaskList() {
  310. return this.mChildren;
  311. }
  312. public void setIndex(int index) {
  313. this.mIndex = index;
  314. }
  315. public int getIndex() {
  316. return this.mIndex;
  317. }
  318. }

二、exception

1、ActionFailureException.java

  1. /*
  2. * Description:支持小米便签运行过程中的运行异常处理。
  3. */
  4. package net.micode.notes.gtask.exception;
  5. public class ActionFailureException extends RuntimeException {
  6. private static final long serialVersionUID = 4425249765923293627L;
  7. /*
  8. * serialVersionUID相当于java类的身份证。主要用于版本控制。
  9. * serialVersionUID作用是序列化时保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。
  10. * Made By Cuican
  11. */
  12. public ActionFailureException() {
  13. super();
  14. }
  15. /*
  16. * 在JAVA类中使用super来引用父类的成分,用this来引用当前对象.
  17. * 如果一个类从另外一个类继承,我们new这个子类的实例对象的时候,这个子类对象里面会有一个父类对象。
  18. * 怎么去引用里面的父类对象呢?使用super来引用
  19. * 也就是说,此处super()以及super (paramString)可认为是Exception ()和Exception (paramString)
  20. * Made By Cuican
  21. */
  22. public ActionFailureException(String paramString) {
  23. super(paramString);
  24. }
  25. public ActionFailureException(String paramString, Throwable paramThrowable) {
  26. super(paramString, paramThrowable);
  27. }
  28. }

2、NetworkFailureException.java

  1. /*
  2. * Description:支持小米便签运行过程中的网络异常处理。
  3. */
  4. package net.micode.notes.gtask.exception;
  5. public class NetworkFailureException extends Exception {
  6. private static final long serialVersionUID = 2107610287180234136L;
  7. /*
  8. * serialVersionUID相当于java类的身份证。主要用于版本控制。
  9. * serialVersionUID作用是序列化时保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性。
  10. * Made By Cuican
  11. */
  12. public NetworkFailureException() {
  13. super();
  14. }
  15. /*
  16. * 在JAVA类中使用super来引用父类的成分,用this来引用当前对象.
  17. * 如果一个类从另外一个类继承,我们new这个子类的实例对象的时候,这个子类对象里面会有一个父类对象。
  18. * 怎么去引用里面的父类对象呢?使用super来引用
  19. * 也就是说,此处super()以及super (paramString)可认为是Exception ()和Exception (paramString)
  20. * Made By Cuican
  21. */
  22. public NetworkFailureException(String paramString) {
  23. super(paramString);
  24. }
  25. public NetworkFailureException(String paramString, Throwable paramThrowable) {
  26. super(paramString, paramThrowable);
  27. }
  28. }

三、remote

1、GTaskASyncTask.java

  1. package net.micode.notes.gtask.remote;
  2. /*异步操作类,实现GTask的异步操作过程
  3. * 主要方法:
  4. * private void showNotification(int tickerId, String content) 向用户提示当前同步的状态,是一个用于交互的方法
  5. * protected Integer doInBackground(Void... unused) 此方法在后台线程执行,完成任务的主要工作,通常需要较长的时间
  6. * protected void onProgressUpdate(String... progress) 可以使用进度条增加用户体验度。 此方法在主线程执行,用于显示任务执行的进度。
  7. * protected void onPostExecute(Integer result) 相当于Handler 处理UI的方式,在这里面可以使用在doInBackground 得到的结果处理操作UI
  8. */
  9. public class GTaskASyncTask extends AsyncTask<Void, String, Integer> {
  10. private static int GTASK_SYNC_NOTIFICATION_ID = 5234235;
  11. public interface OnCompleteListener {
  12. void onComplete();
  13. }
  14. private Context mContext;
  15. private NotificationManager mNotifiManager;
  16. private GTaskManager mTaskManager;
  17. private OnCompleteListener mOnCompleteListener;
  18. public GTaskASyncTask(Context context, OnCompleteListener listener) {
  19. mContext = context;
  20. mOnCompleteListener = listener;
  21. mNotifiManager = (NotificationManager) mContext
  22. .getSystemService(Context.NOTIFICATION_SERVICE);
  23. mTaskManager = GTaskManager.getInstance();
  24. }
  25. public void cancelSync() {
  26. mTaskManager.cancelSync();
  27. }
  28. public void publishProgess(String message) { // 发布进度单位,系统将会调用onProgressUpdate()方法更新这些值
  29. publishProgress(new String[] {
  30. message
  31. });
  32. }
  33. private void showNotification(int tickerId, String content) {
  34. Notification notification = new Notification(R.drawable.notification, mContext
  35. .getString(tickerId), System.currentTimeMillis());
  36. notification.defaults = Notification.DEFAULT_LIGHTS; // 调用系统自带灯光
  37. notification.flags = Notification.FLAG_AUTO_CANCEL; // 点击清除按钮或点击通知后会自动消失
  38. PendingIntent pendingIntent; //一个描述了想要启动一个Activity、Broadcast或是Service的意图
  39. if (tickerId != R.string.ticker_success) {
  40. pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
  41. NotesPreferenceActivity.class), 0); //如果同步不成功,那么从系统取得一个用于启动一个NotesPreferenceActivity的PendingIntent对象
  42. } else {
  43. pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext,
  44. NotesListActivity.class), 0); //如果同步成功,那么从系统取得一个用于启动一个NotesListActivity的PendingIntent对象
  45. }
  46. notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content,
  47. pendingIntent);
  48. mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification);//通过NotificationManager对象的notify()方法来执行一个notification的消息
  49. }
  50. @Override
  51. protected Integer doInBackground(Void... unused) {
  52. publishProgess(mContext.getString(R.string.sync_progress_login, NotesPreferenceActivity
  53. .getSyncAccountName(mContext))); //利用getString,将把 NotesPreferenceActivity.getSyncAccountName(mContext))的字符串内容传进sync_progress_login中
  54. return mTaskManager.sync(mContext, this); //进行后台同步具体操作
  55. }
  56. @Override
  57. protected void onProgressUpdate(String... progress) {
  58. showNotification(R.string.ticker_syncing, progress[0]);
  59. if (mContext instanceof GTaskSyncService) { //instanceof 判断mContext是否是GTaskSyncService的实例
  60. ((GTaskSyncService) mContext).sendBroadcast(progress[0]);
  61. }
  62. }
  63. @Override
  64. protected void onPostExecute(Integer result) { //用于在执行完后台任务后更新UI,显示结果
  65. if (result == GTaskManager.STATE_SUCCESS) {
  66. showNotification(R.string.ticker_success, mContext.getString(
  67. R.string.success_sync_account, mTaskManager.getSyncAccount()));
  68. NotesPreferenceActivity.setLastSyncTime(mContext, System.currentTimeMillis()); //设置最新同步的时间
  69. } else if (result == GTaskManager.STATE_NETWORK_ERROR) {
  70. showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_network));
  71. } else if (result == GTaskManager.STATE_INTERNAL_ERROR) {
  72. showNotification(R.string.ticker_fail, mContext.getString(R.string.error_sync_internal));
  73. } else if (result == GTaskManager.STATE_SYNC_CANCELLED) {
  74. showNotification(R.string.ticker_cancel, mContext
  75. .getString(R.string.error_sync_cancelled));
  76. } //几种不同情况下的结果显示
  77. if (mOnCompleteListener != null) {
  78. new Thread(new Runnable() { //这里好像是方法内的一个线程,但是并不太懂什么意思
  79. public void run() { //完成后的操作,使用onComplete()将所有值都重新初始化,相当于完成一次操作
  80. mOnCompleteListener.onComplete();
  81. }
  82. }).start();
  83. }
  84. }
  85. }

2、GTaskClient.java

  1. package net.micode.notes.gtask.remote;
  2. /*
  3. * 主要功能:实现GTASK的登录操作,进行GTASK任务的创建,创建任务列表,从网络上获取任务和任务列表的内容
  4. * 主要使用类或技术:accountManager JSONObject HttpParams authToken Gid
  5. */
  6. public class GTaskClient {
  7. private static final String TAG = GTaskClient.class.getSimpleName();
  8. private static final String GTASK_URL = "https://mail.google.com/tasks/"; //这个是指定的URL
  9. private static final String GTASK_GET_URL = "https://mail.google.com/tasks/ig";
  10. private static final String GTASK_POST_URL = "https://mail.google.com/tasks/r/ig";
  11. private static GTaskClient mInstance = null;
  12. private DefaultHttpClient mHttpClient;
  13. private String mGetUrl;
  14. private String mPostUrl;
  15. private long mClientVersion;
  16. private boolean mLoggedin;
  17. private long mLastLoginTime;
  18. private int mActionId;
  19. private Account mAccount;
  20. private JSONArray mUpdateArray;
  21. private GTaskClient() {
  22. mHttpClient = null;
  23. mGetUrl = GTASK_GET_URL;
  24. mPostUrl = GTASK_POST_URL;
  25. mClientVersion = -1;
  26. mLoggedin = false;
  27. mLastLoginTime = 0;
  28. mActionId = 1;
  29. mAccount = null;
  30. mUpdateArray = null;
  31. }
  32. /*用来获取的实例化对象
  33. * 使用 getInstance()
  34. * 返回mInstance这个实例化对象
  35. */
  36. public static synchronized GTaskClient getInstance() {
  37. if (mInstance == null) {
  38. mInstance = new GTaskClient();
  39. }
  40. return mInstance;
  41. }
  42. /*用来实现登录操作的函数,传入的参数是一个Activity
  43. * 设置登录操作限制时间,如果超时则需要重新登录
  44. * 有两种登录方式,使用用户自己的URL登录或者使用谷歌官方的URL登录
  45. * 返回true或者false,即最后是否登陆成功
  46. */
  47. public boolean login(Activity activity) {
  48. // we suppose that the cookie would expire after 5 minutes
  49. // then we need to re-login
  50. //判断距离最后一次登录操作是否超过5分钟
  51. final long interval = 1000 * 60 * 5;
  52. if (mLastLoginTime + interval < System.currentTimeMillis()) {
  53. mLoggedin = false;
  54. }
  55. // need to re-login after account switch 重新登录操作
  56. if (mLoggedin
  57. && !TextUtils.equals(getSyncAccount().name, NotesPreferenceActivity
  58. .getSyncAccountName(activity))) {
  59. mLoggedin = false;
  60. }
  61. //如果没超过时间,则不需要重新登录
  62. if (mLoggedin) {
  63. Log.d(TAG, "already logged in");
  64. return true;
  65. }
  66. mLastLoginTime = System.currentTimeMillis();//更新最后登录时间,改为系统当前的时间
  67. String authToken = loginGoogleAccount(activity, false);//判断是否登录到谷歌账户
  68. if (authToken == null) {
  69. Log.e(TAG, "login google account failed");
  70. return false;
  71. }
  72. // login with custom domain if necessary
  73. //尝试使用用户自己的域名登录
  74. if (!(mAccount.name.toLowerCase().endsWith("gmail.com") || mAccount.name.toLowerCase() //将用户账号名改为统一格式(小写)后判断是否为一个谷歌账号地址
  75. .endsWith("googlemail.com"))) {
  76. StringBuilder url = new StringBuilder(GTASK_URL).append("a/");
  77. int index = mAccount.name.indexOf('@') + 1;
  78. String suffix = mAccount.name.substring(index);
  79. url.append(suffix + "/");
  80. mGetUrl = url.toString() + "ig"; //设置用户对应的getUrl
  81. mPostUrl = url.toString() + "r/ig"; //设置用户对应的postUrl
  82. if (tryToLoginGtask(activity, authToken)) {
  83. mLoggedin = true;
  84. }
  85. }
  86. // try to login with google official url
  87. //如果用户账户无法登录,则使用谷歌官方的URI进行登录
  88. if (!mLoggedin) {
  89. mGetUrl = GTASK_GET_URL;
  90. mPostUrl = GTASK_POST_URL;
  91. if (!tryToLoginGtask(activity, authToken)) {
  92. return false;
  93. }
  94. }
  95. mLoggedin = true;
  96. return true;
  97. }
  98. /*具体实现登录谷歌账户的方法
  99. * 使用令牌机制
  100. * 使用AccountManager来管理注册账号
  101. * 返回值是账号的令牌
  102. */
  103. private String loginGoogleAccount(Activity activity, boolean invalidateToken) {
  104. String authToken; //令牌,是登录操作保证安全性的一个方法
  105. AccountManager accountManager = AccountManager.get(activity);//AccountManager这个类给用户提供了集中注册账号的接口
  106. Account[] accounts = accountManager.getAccountsByType("com.google");//获取全部以com.google结尾的account
  107. if (accounts.length == 0) {
  108. Log.e(TAG, "there is no available google account");
  109. return null;
  110. }
  111. String accountName = NotesPreferenceActivity.getSyncAccountName(activity);
  112. Account account = null;
  113. //遍历获得的accounts信息,寻找已经记录过的账户信息
  114. for (Account a : accounts) {
  115. if (a.name.equals(accountName)) {
  116. account = a;
  117. break;
  118. }
  119. }
  120. if (account != null) {
  121. mAccount = account;
  122. } else {
  123. Log.e(TAG, "unable to get an account with the same name in the settings");
  124. return null;
  125. }
  126. // get the token now
  127. //获取选中账号的令牌
  128. AccountManagerFuture<Bundle> accountManagerFuture = accountManager.getAuthToken(account,
  129. "goanna_mobile", null, activity, null, null);
  130. try {
  131. Bundle authTokenBundle = accountManagerFuture.getResult();
  132. authToken = authTokenBundle.getString(AccountManager.KEY_AUTHTOKEN);
  133. //如果是invalidateToken,那么需要调用invalidateAuthToken(String, String)方法废除这个无效token
  134. if (invalidateToken) {
  135. accountManager.invalidateAuthToken("com.google", authToken);
  136. loginGoogleAccount(activity, false);
  137. }
  138. } catch (Exception e) {
  139. Log.e(TAG, "get auth token failed");
  140. authToken = null;
  141. }
  142. return authToken;
  143. }
  144. //尝试登陆Gtask,这只是一个预先判断令牌是否是有效以及是否能登上GTask的方法,而不是具体实现登陆的方法
  145. private boolean tryToLoginGtask(Activity activity, String authToken) {
  146. if (!loginGtask(authToken)) {
  147. // maybe the auth token is out of authTokedate, now let's invalidate the
  148. // token and try again
  149. //删除过一个无效的authToken,申请一个新的后再次尝试登陆
  150. authToken = loginGoogleAccount(activity, true);
  151. if (authToken == null) {
  152. Log.e(TAG, "login google account failed");
  153. return false;
  154. }
  155. if (!loginGtask(authToken)) {
  156. Log.e(TAG, "login gtask failed");
  157. return false;
  158. }
  159. }
  160. return true;
  161. }
  162. //实现登录GTask的具体操作
  163. private boolean loginGtask(String authToken) {
  164. int timeoutConnection = 10000;
  165. int timeoutSocket = 15000; //socket是一种通信连接实现数据的交换的端口
  166. HttpParams httpParameters = new BasicHttpParams(); //实例化一个新的HTTP参数类
  167. HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);//设置连接超时时间
  168. HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);//设置设置端口超时时间
  169. mHttpClient = new DefaultHttpClient(httpParameters);
  170. BasicCookieStore localBasicCookieStore = new BasicCookieStore(); //设置本地cookie
  171. mHttpClient.setCookieStore(localBasicCookieStore);
  172. HttpProtocolParams.setUseExpectContinue(mHttpClient.getParams(), false);
  173. // login gtask
  174. try {
  175. String loginUrl = mGetUrl + "?auth=" + authToken; //设置登录的url
  176. HttpGet httpGet = new HttpGet(loginUrl); //通过登录的uri实例化网页上资源的查找
  177. HttpResponse response = null;
  178. response = mHttpClient.execute(httpGet);
  179. // get the cookie now
  180. //获取CookieStore里存放的cookie,看如果存有“GTL(不知道什么意思)”,则说明有验证成功的有效的cookie
  181. List<Cookie> cookies = mHttpClient.getCookieStore().getCookies();
  182. boolean hasAuthCookie = false;
  183. for (Cookie cookie : cookies) {
  184. if (cookie.getName().contains("GTL")) {
  185. hasAuthCookie = true;
  186. }
  187. }
  188. if (!hasAuthCookie) {
  189. Log.w(TAG, "it seems that there is no auth cookie");
  190. }
  191. // get the client version
  192. //获取client的内容,具体操作是在返回的Content中截取从_setup(开始到)}</script>中间的字符串内容,也就是gtask_url的内容
  193. String resString = getResponseContent(response.getEntity());
  194. String jsBegin = "_setup(";
  195. String jsEnd = ")}</script>";
  196. int begin = resString.indexOf(jsBegin);
  197. int end = resString.lastIndexOf(jsEnd);
  198. String jsString = null;
  199. if (begin != -1 && end != -1 && begin < end) {
  200. jsString = resString.substring(begin + jsBegin.length(), end);
  201. }
  202. JSONObject js = new JSONObject(jsString);
  203. mClientVersion = js.getLong("v");
  204. } catch (JSONException e) {
  205. Log.e(TAG, e.toString());
  206. e.printStackTrace();
  207. return false;
  208. } catch (Exception e) {
  209. // simply catch all exceptions
  210. Log.e(TAG, "httpget gtask_url failed");
  211. return false;
  212. }
  213. return true;
  214. }
  215. private int getActionId() {
  216. return mActionId++;
  217. }
  218. /*实例化创建一个用于向网络传输数据的对象
  219. * 使用HttpPost类
  220. * 返回一个httpPost实例化对象,但里面还没有内容
  221. */
  222. private HttpPost createHttpPost() {
  223. HttpPost httpPost = new HttpPost(mPostUrl);
  224. httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
  225. httpPost.setHeader("AT", "1");
  226. return httpPost;
  227. }
  228. /*通过URL获取响应后返回的数据,也就是网络上的数据和资源
  229. * 使用getContentEncoding()获取网络上的资源和数据
  230. * 返回值就是获取到的资源
  231. */
  232. private String getResponseContent(HttpEntity entity) throws IOException {
  233. String contentEncoding = null;
  234. if (entity.getContentEncoding() != null) {//通过URL得到HttpEntity对象,如果不为空则使用getContent()方法创建一个流将数据从网络都过来
  235. contentEncoding = entity.getContentEncoding().getValue();
  236. Log.d(TAG, "encoding: " + contentEncoding);
  237. }
  238. InputStream input = entity.getContent();
  239. if (contentEncoding != null && contentEncoding.equalsIgnoreCase("gzip")) {//GZIP是使用DEFLATE进行压缩数据的另一个压缩库
  240. input = new GZIPInputStream(entity.getContent());
  241. } else if (contentEncoding != null && contentEncoding.equalsIgnoreCase("deflate")) {//DEFLATE是一个无专利的压缩算法,它可以实现无损数据压缩
  242. Inflater inflater = new Inflater(true);
  243. input = new InflaterInputStream(entity.getContent(), inflater);
  244. }
  245. try {
  246. InputStreamReader isr = new InputStreamReader(input);
  247. BufferedReader br = new BufferedReader(isr);//是一个包装类,它可以包装字符流,将字符流放入缓存里,先把字符读到缓存里,到缓存满了时候,再读入内存,是为了提供读的效率而设计的
  248. StringBuilder sb = new StringBuilder();
  249. while (true) {
  250. String buff = br.readLine();
  251. if (buff == null) {
  252. return sb.toString();
  253. }
  254. sb = sb.append(buff);
  255. }
  256. } finally {
  257. input.close();
  258. }
  259. }
  260. /*通过JSON发送请求
  261. * 请求的具体内容在json的实例化对象js中然后传入
  262. * 利用UrlEncodedFormEntity entity和httpPost.setEntity(entity)方法把js中的内容放置到httpPost中
  263. * 执行请求后使用getResponseContent方法得到返回的数据和资源
  264. * 将资源再次放入json后返回
  265. */
  266. private JSONObject postRequest(JSONObject js) throws NetworkFailureException {
  267. if (!mLoggedin) {//未登录
  268. Log.e(TAG, "please login first");
  269. throw new ActionFailureException("not logged in");
  270. }
  271. //实例化一个httpPost的对象用来向服务器传输数据,在这里就是发送请求,而请求的内容在js里
  272. HttpPost httpPost = createHttpPost();
  273. try {
  274. LinkedList<BasicNameValuePair> list = new LinkedList<BasicNameValuePair>();
  275. list.add(new BasicNameValuePair("r", js.toString()));
  276. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, "UTF-8"); //UrlEncodedFormEntity()的形式比较单一,是普通的键值对
  277. httpPost.setEntity(entity);
  278. // execute the post
  279. //执行这个请求
  280. HttpResponse response = mHttpClient.execute(httpPost);
  281. String jsString = getResponseContent(response.getEntity());
  282. return new JSONObject(jsString);
  283. } catch (ClientProtocolException e) {
  284. Log.e(TAG, e.toString());
  285. e.printStackTrace();
  286. throw new NetworkFailureException("postRequest failed");
  287. } catch (IOException e) {
  288. Log.e(TAG, e.toString());
  289. e.printStackTrace();
  290. throw new NetworkFailureException("postRequest failed");
  291. } catch (JSONException e) {
  292. Log.e(TAG, e.toString());
  293. e.printStackTrace();
  294. throw new ActionFailureException("unable to convert response content to jsonobject");
  295. } catch (Exception e) {
  296. Log.e(TAG, e.toString());
  297. e.printStackTrace();
  298. throw new ActionFailureException("error occurs when posting request");
  299. }
  300. }
  301. /*创建单个任务
  302. * 传入参数是一个.gtask.data.Task包里Task类的对象
  303. * 利用json获取Task里的内容,并且创建相应的jsPost
  304. * 利用postRequest得到任务的返回信息
  305. * 使用task.setGid设置task的new_ID
  306. */
  307. public void createTask(Task task) throws NetworkFailureException {
  308. commitUpdate();
  309. try {
  310. JSONObject jsPost = new JSONObject();
  311. JSONArray actionList = new JSONArray();
  312. // action_list
  313. actionList.put(task.getCreateAction(getActionId()));
  314. jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
  315. // client_version
  316. jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
  317. // post
  318. JSONObject jsResponse = postRequest(jsPost);
  319. JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
  320. GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
  321. task.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
  322. } catch (JSONException e) {
  323. Log.e(TAG, e.toString());
  324. e.printStackTrace();
  325. throw new ActionFailureException("create task: handing jsonobject failed");
  326. }
  327. }
  328. /*
  329. * 创建一个任务列表,与createTask几乎一样,区别就是最后设置的是tasklist的gid
  330. */
  331. public void createTaskList(TaskList tasklist) throws NetworkFailureException {
  332. commitUpdate();
  333. try {
  334. JSONObject jsPost = new JSONObject();
  335. JSONArray actionList = new JSONArray();
  336. // action_list
  337. actionList.put(tasklist.getCreateAction(getActionId()));
  338. jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
  339. // client version
  340. jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
  341. // post
  342. JSONObject jsResponse = postRequest(jsPost);
  343. JSONObject jsResult = (JSONObject) jsResponse.getJSONArray(
  344. GTaskStringUtils.GTASK_JSON_RESULTS).get(0);
  345. tasklist.setGid(jsResult.getString(GTaskStringUtils.GTASK_JSON_NEW_ID));
  346. } catch (JSONException e) {
  347. Log.e(TAG, e.toString());
  348. e.printStackTrace();
  349. throw new ActionFailureException("create tasklist: handing jsonobject failed");
  350. }
  351. }
  352. /*
  353. * 同步更新操作
  354. * 使用JSONObject进行数据存储,使用jsPost.put,Put的信息包括UpdateArray和ClientVersion
  355. * 使用postRequest发送这个jspost,进行处理
  356. */
  357. public void commitUpdate() throws NetworkFailureException {
  358. if (mUpdateArray != null) {
  359. try {
  360. JSONObject jsPost = new JSONObject();
  361. // action_list
  362. jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, mUpdateArray);
  363. // client_version
  364. jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
  365. postRequest(jsPost);
  366. mUpdateArray = null;
  367. } catch (JSONException e) {
  368. Log.e(TAG, e.toString());
  369. e.printStackTrace();
  370. throw new ActionFailureException("commit update: handing jsonobject failed");
  371. }
  372. }
  373. }
  374. /*
  375. * 添加更新的事项
  376. * 调用commitUpdate()实现
  377. */
  378. public void addUpdateNode(Node node) throws NetworkFailureException {
  379. if (node != null) {
  380. // too many update items may result in an error
  381. // set max to 10 items
  382. if (mUpdateArray != null && mUpdateArray.length() > 10) {
  383. commitUpdate();
  384. }
  385. if (mUpdateArray == null)
  386. mUpdateArray = new JSONArray();
  387. mUpdateArray.put(node.getUpdateAction(getActionId()));
  388. }
  389. }
  390. /*
  391. * 移动task,比如讲task移动到不同的task列表中去
  392. * 通过getGid获取task所属列表的gid
  393. * 通过JSONObject.put(String name, Object value)函数设置移动后的task的相关属性值,从而达到移动的目的
  394. * 最后还是通过postRequest进行更新后的发送
  395. */
  396. public void moveTask(Task task, TaskList preParent, TaskList curParent)
  397. throws NetworkFailureException {
  398. commitUpdate();
  399. try {
  400. JSONObject jsPost = new JSONObject();
  401. JSONArray actionList = new JSONArray();
  402. JSONObject action = new JSONObject();
  403. // action_list
  404. action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
  405. GTaskStringUtils.GTASK_JSON_ACTION_TYPE_MOVE);
  406. action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
  407. action.put(GTaskStringUtils.GTASK_JSON_ID, task.getGid());
  408. if (preParent == curParent && task.getPriorSibling() != null) {
  409. // put prioring_sibing_id only if moving within the tasklist and
  410. // it is not the first one
  411. //设置优先级ID,只有当移动是发生在文件中
  412. action.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, task.getPriorSibling());
  413. }
  414. action.put(GTaskStringUtils.GTASK_JSON_SOURCE_LIST, preParent.getGid()); //设置移动前所属列表
  415. action.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT, curParent.getGid()); //设置当前所属列表
  416. if (preParent != curParent) {
  417. // put the dest_list only if moving between tasklists
  418. action.put(GTaskStringUtils.GTASK_JSON_DEST_LIST, curParent.getGid());
  419. }
  420. actionList.put(action);
  421. //最后将ACTION_LIST加入到jsPost中
  422. jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
  423. // client_version
  424. jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
  425. postRequest(jsPost);
  426. } catch (JSONException e) {
  427. Log.e(TAG, e.toString());
  428. e.printStackTrace();
  429. throw new ActionFailureException("move task: handing jsonobject failed");
  430. }
  431. }
  432. /*
  433. * 删除操作节点
  434. * 还是利用JSON
  435. * 删除过后使用postRequest发送删除后的结果
  436. */
  437. public void deleteNode(Node node) throws NetworkFailureException {
  438. commitUpdate();
  439. try {
  440. JSONObject jsPost = new JSONObject();
  441. JSONArray actionList = new JSONArray();
  442. // action_list
  443. node.setDeleted(true);
  444. actionList.put(node.getUpdateAction(getActionId())); //这里会获取到删除操作的ID,加入到actionLiast中
  445. jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
  446. // client_version
  447. jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
  448. postRequest(jsPost);
  449. mUpdateArray = null;
  450. } catch (JSONException e) {
  451. Log.e(TAG, e.toString());
  452. e.printStackTrace();
  453. throw new ActionFailureException("delete node: handing jsonobject failed");
  454. }
  455. }
  456. /*
  457. * 获取任务列表
  458. * 首先通过GetURI使用getResponseContent从网上获取数据
  459. * 然后筛选出"_setup("到)}</script>的部分,并且从中获取GTASK_JSON_LISTS的内容返回
  460. */
  461. public JSONArray getTaskLists() throws NetworkFailureException {
  462. if (!mLoggedin) {
  463. Log.e(TAG, "please login first");
  464. throw new ActionFailureException("not logged in");
  465. }
  466. try {
  467. HttpGet httpGet = new HttpGet(mGetUrl);
  468. HttpResponse response = null;
  469. response = mHttpClient.execute(httpGet);
  470. // get the task list
  471. //筛选工作,把筛选出的字符串放入jsString
  472. String resString = getResponseContent(response.getEntity());
  473. String jsBegin = "_setup(";
  474. String jsEnd = ")}</script>";
  475. int begin = resString.indexOf(jsBegin);
  476. int end = resString.lastIndexOf(jsEnd);
  477. String jsString = null;
  478. if (begin != -1 && end != -1 && begin < end) {
  479. jsString = resString.substring(begin + jsBegin.length(), end);
  480. }
  481. JSONObject js = new JSONObject(jsString);
  482. //获取GTASK_JSON_LISTS
  483. return js.getJSONObject("t").getJSONArray(GTaskStringUtils.GTASK_JSON_LISTS);
  484. } catch (ClientProtocolException e) {
  485. Log.e(TAG, e.toString());
  486. e.printStackTrace();
  487. throw new NetworkFailureException("gettasklists: httpget failed");
  488. } catch (IOException e) {
  489. Log.e(TAG, e.toString());
  490. e.printStackTrace();
  491. throw new NetworkFailureException("gettasklists: httpget failed");
  492. } catch (JSONException e) {
  493. Log.e(TAG, e.toString());
  494. e.printStackTrace();
  495. throw new ActionFailureException("get task lists: handing jasonobject failed");
  496. }
  497. }
  498. /*
  499. * 通过传入的TASKList的gid,从网络上获取相应属于这个任务列表的任务
  500. */
  501. public JSONArray getTaskList(String listGid) throws NetworkFailureException {
  502. commitUpdate();
  503. try {
  504. JSONObject jsPost = new JSONObject();
  505. JSONArray actionList = new JSONArray();
  506. JSONObject action = new JSONObject();
  507. // action_list
  508. action.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE,
  509. GTaskStringUtils.GTASK_JSON_ACTION_TYPE_GETALL);
  510. action.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, getActionId());
  511. action.put(GTaskStringUtils.GTASK_JSON_LIST_ID, listGid); //这里设置为传入的listGid
  512. action.put(GTaskStringUtils.GTASK_JSON_GET_DELETED, false);
  513. actionList.put(action);
  514. jsPost.put(GTaskStringUtils.GTASK_JSON_ACTION_LIST, actionList);
  515. // client_version
  516. jsPost.put(GTaskStringUtils.GTASK_JSON_CLIENT_VERSION, mClientVersion);
  517. JSONObject jsResponse = postRequest(jsPost);
  518. return jsResponse.getJSONArray(GTaskStringUtils.GTASK_JSON_TASKS);
  519. } catch (JSONException e) {
  520. Log.e(TAG, e.toString());
  521. e.printStackTrace();
  522. throw new ActionFailureException("get task list: handing jsonobject failed");
  523. }
  524. }
  525. public Account getSyncAccount() {
  526. return mAccount;
  527. }
  528. //重置更新的内容
  529. public void resetUpdateArray() {
  530. mUpdateArray = null;
  531. }
  532. }

3、GTaskManager.java

  1. package net.micode.notes.gtask.remote;
  2. public class GTaskManager {
  3. private static final String TAG = GTaskManager.class.getSimpleName();
  4. public static final int STATE_SUCCESS = 0;
  5. public static final int STATE_NETWORK_ERROR = 1;
  6. public static final int STATE_INTERNAL_ERROR = 2;
  7. public static final int STATE_SYNC_IN_PROGRESS = 3;
  8. public static final int STATE_SYNC_CANCELLED = 4;
  9. private static GTaskManager mInstance = null;
  10. private Activity mActivity;
  11. private Context mContext;
  12. private ContentResolver mContentResolver;
  13. private boolean mSyncing;
  14. private boolean mCancelled;
  15. private HashMap<String, TaskList> mGTaskListHashMap;
  16. private HashMap<String, Node> mGTaskHashMap;
  17. private HashMap<String, MetaData> mMetaHashMap;
  18. private TaskList mMetaList;
  19. private HashSet<Long> mLocalDeleteIdMap;
  20. private HashMap<String, Long> mGidToNid;
  21. private HashMap<Long, String> mNidToGid;
  22. private GTaskManager() { //对象初始化函数
  23. mSyncing = false; //正在同步,flase代表未执行
  24. mCancelled = false; //全局标识,flase代表可以执行
  25. mGTaskListHashMap = new HashMap<String, TaskList>(); //<>代表Java的泛型,就是创建一个用类型作为参数的类。
  26. mGTaskHashMap = new HashMap<String, Node>();
  27. mMetaHashMap = new HashMap<String, MetaData>();
  28. mMetaList = null;
  29. mLocalDeleteIdMap = new HashSet<Long>();
  30. mGidToNid = new HashMap<String, Long>(); //GoogleID to NodeID??
  31. mNidToGid = new HashMap<Long, String>(); //NodeID to GoogleID???通过hashmap散列表建立映射
  32. }
  33. /**
  34. * 包含关键字synchronized,语言级同步,指明该函数可能运行在多线程的环境下。
  35. * 功能:类初始化函数
  36. * @author TTS
  37. * @return GtaskManger
  38. */
  39. public static synchronized GTaskManager getInstance() { //可能运行在多线程环境下,使用语言级同步--synchronized
  40. if (mInstance == null) {
  41. mInstance = new GTaskManager();
  42. }
  43. return mInstance;
  44. }
  45. /**
  46. * 包含关键字synchronized,语言级同步,指明该函数可能运行在多线程的环境下。
  47. * @author TTS
  48. * @param activity
  49. */
  50. public synchronized void setActivityContext(Activity activity) {
  51. // used for getting auth token
  52. mActivity = activity;
  53. }
  54. /**
  55. * 核心函数
  56. * 功能:实现了本地同步操作和远端同步操作
  57. * @author TTS
  58. * @param context-----获取上下文
  59. * @param asyncTask-------用于同步的异步操作类
  60. * @return int
  61. */
  62. public int sync(Context context, GTaskASyncTask asyncTask) { //核心函数
  63. if (mSyncing) {
  64. Log.d(TAG, "Sync is in progress"); //创建日志文件(调试信息),debug
  65. return STATE_SYNC_IN_PROGRESS;
  66. }
  67. mContext = context;
  68. mContentResolver = mContext.getContentResolver();
  69. mSyncing = true;
  70. mCancelled = false;
  71. mGTaskListHashMap.clear();
  72. mGTaskHashMap.clear();
  73. mMetaHashMap.clear();
  74. mLocalDeleteIdMap.clear();
  75. mGidToNid.clear();
  76. mNidToGid.clear();
  77. try {
  78. GTaskClient client = GTaskClient.getInstance(); //getInstance即为创建一个实例,client--客户机
  79. client.resetUpdateArray(); //JSONArray类型,reset即置为NULL
  80. // login google task
  81. if (!mCancelled) {
  82. if (!client.login(mActivity)) {
  83. throw new NetworkFailureException("login google task failed");
  84. }
  85. }
  86. // get the task list from google
  87. asyncTask.publishProgess(mContext.getString(R.string.sync_progress_init_list));
  88. initGTaskList(); //获取Google上的JSONtasklist转为本地TaskList
  89. // do content sync work
  90. asyncTask.publishProgess(mContext.getString(R.string.sync_progress_syncing));
  91. syncContent();
  92. } catch (NetworkFailureException e) { //分为两种异常,此类异常为网络异常
  93. Log.e(TAG, e.toString()); //创建日志文件(调试信息),error
  94. return STATE_NETWORK_ERROR;
  95. } catch (ActionFailureException e) { //此类异常为操作异常
  96. Log.e(TAG, e.toString());
  97. return STATE_INTERNAL_ERROR;
  98. } catch (Exception e) {
  99. Log.e(TAG, e.toString());
  100. e.printStackTrace();
  101. return STATE_INTERNAL_ERROR;
  102. } finally {
  103. mGTaskListHashMap.clear();
  104. mGTaskHashMap.clear();
  105. mMetaHashMap.clear();
  106. mLocalDeleteIdMap.clear();
  107. mGidToNid.clear();
  108. mNidToGid.clear();
  109. mSyncing = false;
  110. }
  111. return mCancelled ? STATE_SYNC_CANCELLED : STATE_SUCCESS;
  112. }
  113. /**
  114. *功能:初始化GtaskList,获取Google上的JSONtasklist转为本地TaskList。
  115. *获得的数据存储在mMetaList,mGTaskListHashMap,mGTaskHashMap
  116. *@author TTS
  117. *@exception NetworkFailureException
  118. *@return void
  119. */
  120. private void initGTaskList() throws NetworkFailureException {
  121. if (mCancelled)
  122. return;
  123. GTaskClient client = GTaskClient.getInstance(); //getInstance即为创建一个实例,client应指远端客户机
  124. try {
  125. //Json对象是Name Value对(即子元素)的无序集合,相当于一个Map对象。JsonObject类是bantouyan-json库对Json对象的抽象,提供操纵Json对象的各种方法。
  126. //其格式为{"key1":value1,"key2",value2....};key 必须是字符串。
  127. //因为ajax请求不刷新页面,但配合js可以实现局部刷新,因此json常常被用来作为异步请求的返回对象使用。
  128. JSONArray jsTaskLists = client.getTaskLists(); //原注释为get task list------lists???
  129. // init meta list first
  130. mMetaList = null; //TaskList类型
  131. for (int i = 0; i < jsTaskLists.length(); i++) {
  132. JSONObject object = jsTaskLists.getJSONObject(i); //JSONObject与JSONArray一个为对象,一个为数组。此处取出单个JASONObject
  133. String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
  134. String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);
  135. if (name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_META)) {
  136. mMetaList = new TaskList(); //MetaList意为元表,Tasklist类型,此处为初始化
  137. mMetaList.setContentByRemoteJSON(object); //将JSON中部分数据复制到自己定义的对象中相对应的数据:name->mname...
  138. // load meta data
  139. JSONArray jsMetas = client.getTaskList(gid); //原注释为get action_list------list???
  140. for (int j = 0; j < jsMetas.length(); j++) {
  141. object = (JSONObject) jsMetas.getJSONObject(j);
  142. MetaData metaData = new MetaData(); //继承自Node
  143. metaData.setContentByRemoteJSON(object);
  144. if (metaData.isWorthSaving()) { //if not worth to save,metadata将不加入mMetaList
  145. mMetaList.addChildTask(metaData);
  146. if (metaData.getGid() != null) {
  147. mMetaHashMap.put(metaData.getRelatedGid(), metaData);
  148. }
  149. }
  150. }
  151. }
  152. }
  153. // create meta list if not existed
  154. if (mMetaList == null) {
  155. mMetaList = new TaskList();
  156. mMetaList.setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX
  157. + GTaskStringUtils.FOLDER_META);
  158. GTaskClient.getInstance().createTaskList(mMetaList);
  159. }
  160. // init task list
  161. for (int i = 0; i < jsTaskLists.length(); i++) {
  162. JSONObject object = jsTaskLists.getJSONObject(i);
  163. String gid = object.getString(GTaskStringUtils.GTASK_JSON_ID); //通过getString函数传入本地某个标志数据的名称,获取其在远端的名称。
  164. String name = object.getString(GTaskStringUtils.GTASK_JSON_NAME);
  165. if (name.startsWith(GTaskStringUtils.MIUI_FOLDER_PREFFIX)
  166. && !name.equals(GTaskStringUtils.MIUI_FOLDER_PREFFIX
  167. + GTaskStringUtils.FOLDER_META)) {
  168. TaskList tasklist = new TaskList(); //继承自Node
  169. tasklist.setContentByRemoteJSON(object);
  170. mGTaskListHashMap.put(gid, tasklist);
  171. mGTaskHashMap.put(gid, tasklist); //为什么加两遍???
  172. // load tasks
  173. JSONArray jsTasks = client.getTaskList(gid);
  174. for (int j = 0; j < jsTasks.length(); j++) {
  175. object = (JSONObject) jsTasks.getJSONObject(j);
  176. gid = object.getString(GTaskStringUtils.GTASK_JSON_ID);
  177. Task task = new Task();
  178. task.setContentByRemoteJSON(object);
  179. if (task.isWorthSaving()) {
  180. task.setMetaInfo(mMetaHashMap.get(gid));
  181. tasklist.addChildTask(task);
  182. mGTaskHashMap.put(gid, task);
  183. }
  184. }
  185. }
  186. }
  187. } catch (JSONException e) {
  188. Log.e(TAG, e.toString());
  189. e.printStackTrace();
  190. throw new ActionFailureException("initGTaskList: handing JSONObject failed");
  191. }
  192. }
  193. /**
  194. * 功能:本地内容同步操作
  195. * @throws NetworkFailureException
  196. * @return 无返回值
  197. */
  198. private void syncContent() throws NetworkFailureException { //本地内容同步操作
  199. int syncType;
  200. Cursor c = null; //数据库指针
  201. String gid; //GoogleID??
  202. Node node; //Node包含Sync_Action的不同类型
  203. mLocalDeleteIdMap.clear(); //HashSet<Long>类型
  204. if (mCancelled) {
  205. return;
  206. }
  207. // for local deleted note
  208. try {
  209. c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
  210. "(type<>? AND parent_id=?)", new String[] {
  211. String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER)
  212. }, null);
  213. if (c != null) {
  214. while (c.moveToNext()) {
  215. gid = c.getString(SqlNote.GTASK_ID_COLUMN);
  216. node = mGTaskHashMap.get(gid);
  217. if (node != null) {
  218. mGTaskHashMap.remove(gid);
  219. doContentSync(Node.SYNC_ACTION_DEL_REMOTE, node, c);
  220. }
  221. mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN));
  222. }
  223. } else {
  224. Log.w(TAG, "failed to query trash folder");
  225. }
  226. } finally {
  227. if (c != null) {
  228. c.close();
  229. c = null;
  230. }
  231. }
  232. // sync folder first
  233. syncFolder();
  234. // for note existing in database
  235. try {
  236. c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
  237. "(type=? AND parent_id<>?)", new String[] {
  238. String.valueOf(Notes.TYPE_NOTE), String.valueOf(Notes.ID_TRASH_FOLER)
  239. }, NoteColumns.TYPE + " DESC");
  240. if (c != null) {
  241. while (c.moveToNext()) {
  242. gid = c.getString(SqlNote.GTASK_ID_COLUMN);
  243. node = mGTaskHashMap.get(gid);
  244. if (node != null) {
  245. mGTaskHashMap.remove(gid);
  246. mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN)); //通过hashmap建立联系
  247. mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid); //通过hashmap建立联系
  248. syncType = node.getSyncAction(c);
  249. } else {
  250. if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
  251. // local add
  252. syncType = Node.SYNC_ACTION_ADD_REMOTE;
  253. } else {
  254. // remote delete
  255. syncType = Node.SYNC_ACTION_DEL_LOCAL;
  256. }
  257. }
  258. doContentSync(syncType, node, c);
  259. }
  260. } else {
  261. Log.w(TAG, "failed to query existing note in database");
  262. }
  263. } finally {
  264. if (c != null) {
  265. c.close();
  266. c = null;
  267. }
  268. }
  269. // go through remaining items
  270. Iterator<Map.Entry<String, Node>> iter = mGTaskHashMap.entrySet().iterator(); //Iterator迭代器
  271. while (iter.hasNext()) {
  272. Map.Entry<String, Node> entry = iter.next();
  273. node = entry.getValue();
  274. doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null);
  275. }
  276. // mCancelled can be set by another thread, so we neet to check one by //thread----线程
  277. // one
  278. // clear local delete table
  279. if (!mCancelled) {
  280. if (!DataUtils.batchDeleteNotes(mContentResolver, mLocalDeleteIdMap)) {
  281. throw new ActionFailureException("failed to batch-delete local deleted notes");
  282. }
  283. }
  284. // refresh local sync id
  285. if (!mCancelled) {
  286. GTaskClient.getInstance().commitUpdate();
  287. refreshLocalSyncId();
  288. }
  289. }
  290. /**
  291. * 功能:
  292. * @author TTS
  293. * @throws NetworkFailureException
  294. */
  295. private void syncFolder() throws NetworkFailureException {
  296. Cursor c = null;
  297. String gid;
  298. Node node;
  299. int syncType;
  300. if (mCancelled) {
  301. return;
  302. }
  303. // for root folder
  304. try {
  305. c = mContentResolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI,
  306. Notes.ID_ROOT_FOLDER), SqlNote.PROJECTION_NOTE, null, null, null);
  307. if (c != null) {
  308. c.moveToNext();
  309. gid = c.getString(SqlNote.GTASK_ID_COLUMN);
  310. node = mGTaskHashMap.get(gid);
  311. if (node != null) {
  312. mGTaskHashMap.remove(gid);
  313. mGidToNid.put(gid, (long) Notes.ID_ROOT_FOLDER);
  314. mNidToGid.put((long) Notes.ID_ROOT_FOLDER, gid);
  315. // for system folder, only update remote name if necessary
  316. if (!node.getName().equals(
  317. GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT))
  318. doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c);
  319. } else {
  320. doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);
  321. }
  322. } else {
  323. Log.w(TAG, "failed to query root folder");
  324. }
  325. } finally {
  326. if (c != null) {
  327. c.close();
  328. c = null;
  329. }
  330. }
  331. // for call-note folder
  332. try {
  333. c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE, "(_id=?)",
  334. new String[] {
  335. String.valueOf(Notes.ID_CALL_RECORD_FOLDER)
  336. }, null);
  337. if (c != null) {
  338. if (c.moveToNext()) {
  339. gid = c.getString(SqlNote.GTASK_ID_COLUMN);
  340. node = mGTaskHashMap.get(gid);
  341. if (node != null) {
  342. mGTaskHashMap.remove(gid);
  343. mGidToNid.put(gid, (long) Notes.ID_CALL_RECORD_FOLDER);
  344. mNidToGid.put((long) Notes.ID_CALL_RECORD_FOLDER, gid);
  345. // for system folder, only update remote name if
  346. // necessary
  347. if (!node.getName().equals(
  348. GTaskStringUtils.MIUI_FOLDER_PREFFIX
  349. + GTaskStringUtils.FOLDER_CALL_NOTE))
  350. doContentSync(Node.SYNC_ACTION_UPDATE_REMOTE, node, c);
  351. } else {
  352. doContentSync(Node.SYNC_ACTION_ADD_REMOTE, node, c);
  353. }
  354. }
  355. } else {
  356. Log.w(TAG, "failed to query call note folder");
  357. }
  358. } finally {
  359. if (c != null) {
  360. c.close();
  361. c = null;
  362. }
  363. }
  364. // for local existing folders
  365. try {
  366. c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
  367. "(type=? AND parent_id<>?)", new String[] {
  368. String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)
  369. }, NoteColumns.TYPE + " DESC");
  370. if (c != null) {
  371. while (c.moveToNext()) {
  372. gid = c.getString(SqlNote.GTASK_ID_COLUMN);
  373. node = mGTaskHashMap.get(gid);
  374. if (node != null) {
  375. mGTaskHashMap.remove(gid);
  376. mGidToNid.put(gid, c.getLong(SqlNote.ID_COLUMN));
  377. mNidToGid.put(c.getLong(SqlNote.ID_COLUMN), gid);
  378. syncType = node.getSyncAction(c);
  379. } else {
  380. if (c.getString(SqlNote.GTASK_ID_COLUMN).trim().length() == 0) {
  381. // local add
  382. syncType = Node.SYNC_ACTION_ADD_REMOTE;
  383. } else {
  384. // remote delete
  385. syncType = Node.SYNC_ACTION_DEL_LOCAL;
  386. }
  387. }
  388. doContentSync(syncType, node, c);
  389. }
  390. } else {
  391. Log.w(TAG, "failed to query existing folder");
  392. }
  393. } finally {
  394. if (c != null) {
  395. c.close();
  396. c = null;
  397. }
  398. }
  399. // for remote add folders
  400. Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
  401. while (iter.hasNext()) {
  402. Map.Entry<String, TaskList> entry = iter.next();
  403. gid = entry.getKey();
  404. node = entry.getValue();
  405. if (mGTaskHashMap.containsKey(gid)) {
  406. mGTaskHashMap.remove(gid);
  407. doContentSync(Node.SYNC_ACTION_ADD_LOCAL, node, null);
  408. }
  409. }
  410. if (!mCancelled)
  411. GTaskClient.getInstance().commitUpdate();
  412. }
  413. /**
  414. * 功能:syncType分类,addLocalNode,addRemoteNode,deleteNode,updateLocalNode,updateRemoteNode
  415. * @author TTS
  416. * @param syncType
  417. * @param node
  418. * @param c
  419. * @throws NetworkFailureException
  420. */
  421. private void doContentSync(int syncType, Node node, Cursor c) throws NetworkFailureException {
  422. if (mCancelled) {
  423. return;
  424. }
  425. MetaData meta;
  426. switch (syncType) {
  427. case Node.SYNC_ACTION_ADD_LOCAL:
  428. addLocalNode(node);
  429. break;
  430. case Node.SYNC_ACTION_ADD_REMOTE:
  431. addRemoteNode(node, c);
  432. break;
  433. case Node.SYNC_ACTION_DEL_LOCAL:
  434. meta = mMetaHashMap.get(c.getString(SqlNote.GTASK_ID_COLUMN));
  435. if (meta != null) {
  436. GTaskClient.getInstance().deleteNode(meta);
  437. }
  438. mLocalDeleteIdMap.add(c.getLong(SqlNote.ID_COLUMN));
  439. break;
  440. case Node.SYNC_ACTION_DEL_REMOTE:
  441. meta = mMetaHashMap.get(node.getGid());
  442. if (meta != null) {
  443. GTaskClient.getInstance().deleteNode(meta);
  444. }
  445. GTaskClient.getInstance().deleteNode(node);
  446. break;
  447. case Node.SYNC_ACTION_UPDATE_LOCAL:
  448. updateLocalNode(node, c);
  449. break;
  450. case Node.SYNC_ACTION_UPDATE_REMOTE:
  451. updateRemoteNode(node, c);
  452. break;
  453. case Node.SYNC_ACTION_UPDATE_CONFLICT:
  454. // merging both modifications maybe a good idea
  455. // right now just use local update simply
  456. updateRemoteNode(node, c);
  457. break;
  458. case Node.SYNC_ACTION_NONE:
  459. break;
  460. case Node.SYNC_ACTION_ERROR:
  461. default:
  462. throw new ActionFailureException("unkown sync action type");
  463. }
  464. }
  465. /**
  466. * 功能:本地增加Node
  467. * @author TTS
  468. * @param node
  469. * @throws NetworkFailureException
  470. */
  471. private void addLocalNode(Node node) throws NetworkFailureException {
  472. if (mCancelled) {
  473. return;
  474. }
  475. SqlNote sqlNote;
  476. if (node instanceof TaskList) {
  477. if (node.getName().equals(
  478. GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT)) {
  479. sqlNote = new SqlNote(mContext, Notes.ID_ROOT_FOLDER);
  480. } else if (node.getName().equals(
  481. GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE)) {
  482. sqlNote = new SqlNote(mContext, Notes.ID_CALL_RECORD_FOLDER);
  483. } else {
  484. sqlNote = new SqlNote(mContext);
  485. sqlNote.setContent(node.getLocalJSONFromContent());
  486. sqlNote.setParentId(Notes.ID_ROOT_FOLDER);
  487. }
  488. } else {
  489. sqlNote = new SqlNote(mContext);
  490. JSONObject js = node.getLocalJSONFromContent();
  491. try {
  492. if (js.has(GTaskStringUtils.META_HEAD_NOTE)) {
  493. JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE);
  494. if (note.has(NoteColumns.ID)) {
  495. long id = note.getLong(NoteColumns.ID);
  496. if (DataUtils.existInNoteDatabase(mContentResolver, id)) {
  497. // the id is not available, have to create a new one
  498. note.remove(NoteColumns.ID);
  499. }
  500. }
  501. }
  502. if (js.has(GTaskStringUtils.META_HEAD_DATA)) {
  503. JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA);
  504. for (int i = 0; i < dataArray.length(); i++) {
  505. JSONObject data = dataArray.getJSONObject(i);
  506. if (data.has(DataColumns.ID)) {
  507. long dataId = data.getLong(DataColumns.ID);
  508. if (DataUtils.existInDataDatabase(mContentResolver, dataId)) {
  509. // the data id is not available, have to create
  510. // a new one
  511. data.remove(DataColumns.ID);
  512. }
  513. }
  514. }
  515. }
  516. } catch (JSONException e) {
  517. Log.w(TAG, e.toString());
  518. e.printStackTrace();
  519. }
  520. sqlNote.setContent(js);
  521. Long parentId = mGidToNid.get(((Task) node).getParent().getGid());
  522. if (parentId == null) {
  523. Log.e(TAG, "cannot find task's parent id locally");
  524. throw new ActionFailureException("cannot add local node");
  525. }
  526. sqlNote.setParentId(parentId.longValue());
  527. }
  528. // create the local node
  529. sqlNote.setGtaskId(node.getGid());
  530. sqlNote.commit(false);
  531. // update gid-nid mapping
  532. mGidToNid.put(node.getGid(), sqlNote.getId());
  533. mNidToGid.put(sqlNote.getId(), node.getGid());
  534. // update meta
  535. updateRemoteMeta(node.getGid(), sqlNote);
  536. }
  537. /**
  538. * 功能:update本地node
  539. * @author TTS
  540. * @param node
  541. * ----同步操作的基础数据类型
  542. * @param c
  543. * ----Cursor
  544. * @throws NetworkFailureException
  545. */
  546. private void updateLocalNode(Node node, Cursor c) throws NetworkFailureException {
  547. if (mCancelled) {
  548. return;
  549. }
  550. SqlNote sqlNote;
  551. // update the note locally
  552. sqlNote = new SqlNote(mContext, c);
  553. sqlNote.setContent(node.getLocalJSONFromContent());
  554. Long parentId = (node instanceof Task) ? mGidToNid.get(((Task) node).getParent().getGid())
  555. : new Long(Notes.ID_ROOT_FOLDER);
  556. if (parentId == null) {
  557. Log.e(TAG, "cannot find task's parent id locally");
  558. throw new ActionFailureException("cannot update local node");
  559. }
  560. sqlNote.setParentId(parentId.longValue());
  561. sqlNote.commit(true);
  562. // update meta info
  563. updateRemoteMeta(node.getGid(), sqlNote);
  564. }
  565. /**
  566. * 功能:远程增加Node
  567. * 需要updateRemoteMeta
  568. * @author TTS
  569. * @param node
  570. * ----同步操作的基础数据类型
  571. * @param c
  572. * --Cursor
  573. * @throws NetworkFailureException
  574. */
  575. private void addRemoteNode(Node node, Cursor c) throws NetworkFailureException {
  576. if (mCancelled) {
  577. return;
  578. }
  579. SqlNote sqlNote = new SqlNote(mContext, c); //从本地mContext中获取内容
  580. Node n;
  581. // update remotely
  582. if (sqlNote.isNoteType()) {
  583. Task task = new Task();
  584. task.setContentByLocalJSON(sqlNote.getContent());
  585. String parentGid = mNidToGid.get(sqlNote.getParentId());
  586. if (parentGid == null) {
  587. Log.e(TAG, "cannot find task's parent tasklist"); //调试信息
  588. throw new ActionFailureException("cannot add remote task");
  589. }
  590. mGTaskListHashMap.get(parentGid).addChildTask(task); //在本地生成的GTaskList中增加子结点
  591. //登录远程服务器,创建Task
  592. GTaskClient.getInstance().createTask(task);
  593. n = (Node) task;
  594. // add meta
  595. updateRemoteMeta(task.getGid(), sqlNote);
  596. } else {
  597. TaskList tasklist = null;
  598. // we need to skip folder if it has already existed
  599. String folderName = GTaskStringUtils.MIUI_FOLDER_PREFFIX;
  600. if (sqlNote.getId() == Notes.ID_ROOT_FOLDER)
  601. folderName += GTaskStringUtils.FOLDER_DEFAULT;
  602. else if (sqlNote.getId() == Notes.ID_CALL_RECORD_FOLDER)
  603. folderName += GTaskStringUtils.FOLDER_CALL_NOTE;
  604. else
  605. folderName += sqlNote.getSnippet();
  606. //iterator迭代器,通过统一的接口迭代所有的map元素
  607. Iterator<Map.Entry<String, TaskList>> iter = mGTaskListHashMap.entrySet().iterator();
  608. while (iter.hasNext()) {
  609. Map.Entry<String, TaskList> entry = iter.next();
  610. String gid = entry.getKey();
  611. TaskList list = entry.getValue();
  612. if (list.getName().equals(folderName)) {
  613. tasklist = list;
  614. if (mGTaskHashMap.containsKey(gid)) {
  615. mGTaskHashMap.remove(gid);
  616. }
  617. break;
  618. }
  619. }
  620. // no match we can add now
  621. if (tasklist == null) {
  622. tasklist = new TaskList();
  623. tasklist.setContentByLocalJSON(sqlNote.getContent());
  624. GTaskClient.getInstance().createTaskList(tasklist);
  625. mGTaskListHashMap.put(tasklist.getGid(), tasklist);
  626. }
  627. n = (Node) tasklist;
  628. }
  629. // update local note
  630. sqlNote.setGtaskId(n.getGid());
  631. sqlNote.commit(false);
  632. sqlNote.resetLocalModified();
  633. sqlNote.commit(true);
  634. // gid-id mapping //创建id间的映射
  635. mGidToNid.put(n.getGid(), sqlNote.getId());
  636. mNidToGid.put(sqlNote.getId(), n.getGid());
  637. }
  638. /**
  639. * 功能:更新远端的Node,包含meta更新(updateRemoteMeta)
  640. * @author TTS
  641. * @param node
  642. * ----同步操作的基础数据类型
  643. * @param c
  644. * --Cursor
  645. * @throws NetworkFailureException
  646. */
  647. private void updateRemoteNode(Node node, Cursor c) throws NetworkFailureException {
  648. if (mCancelled) {
  649. return;
  650. }
  651. SqlNote sqlNote = new SqlNote(mContext, c);
  652. // update remotely
  653. node.setContentByLocalJSON(sqlNote.getContent());
  654. GTaskClient.getInstance().addUpdateNode(node); //GTaskClient用途为从本地登陆远端服务器
  655. // update meta
  656. updateRemoteMeta(node.getGid(), sqlNote);
  657. // move task if necessary
  658. if (sqlNote.isNoteType()) {
  659. Task task = (Task) node;
  660. TaskList preParentList = task.getParent();
  661. //preParentList为通过node获取的父节点列表
  662. String curParentGid = mNidToGid.get(sqlNote.getParentId());
  663. //curParentGid为通过光标在数据库中找到sqlNote的mParentId,再通过mNidToGid由long类型转为String类型的Gid
  664. if (curParentGid == null) {
  665. Log.e(TAG, "cannot find task's parent tasklist");
  666. throw new ActionFailureException("cannot update remote task");
  667. }
  668. TaskList curParentList = mGTaskListHashMap.get(curParentGid);
  669. //通过HashMap找到对应Gid的TaskList
  670. if (preParentList != curParentList) { //?????????????
  671. preParentList.removeChildTask(task);
  672. curParentList.addChildTask(task);
  673. GTaskClient.getInstance().moveTask(task, preParentList, curParentList);
  674. }
  675. }
  676. // clear local modified flag
  677. sqlNote.resetLocalModified();
  678. //commit到本地数据库
  679. sqlNote.commit(true);
  680. }
  681. /**
  682. * 功能:升级远程meta。 meta---元数据----计算机文件系统管理数据---管理数据的数据。
  683. * @author TTS
  684. * @param gid
  685. * ---GoogleID为String类型
  686. * @param sqlNote
  687. * ---同步前的数据库操作,故使用类SqlNote
  688. * @throws NetworkFailureException
  689. */
  690. private void updateRemoteMeta(String gid, SqlNote sqlNote) throws NetworkFailureException {
  691. if (sqlNote != null && sqlNote.isNoteType()) {
  692. MetaData metaData = mMetaHashMap.get(gid);
  693. if (metaData != null) {
  694. metaData.setMeta(gid, sqlNote.getContent());
  695. GTaskClient.getInstance().addUpdateNode(metaData);
  696. } else {
  697. metaData = new MetaData();
  698. metaData.setMeta(gid, sqlNote.getContent());
  699. mMetaList.addChildTask(metaData);
  700. mMetaHashMap.put(gid, metaData);
  701. GTaskClient.getInstance().createTask(metaData);
  702. }
  703. }
  704. }
  705. /**
  706. * 功能:刷新本地,给sync的ID对应上最后更改过的对象
  707. * @author TTS
  708. * @return void
  709. * @throws NetworkFailureException
  710. */
  711. private void refreshLocalSyncId() throws NetworkFailureException {
  712. if (mCancelled) {
  713. return;
  714. }
  715. // get the latest gtask list //获取最近的(最晚的)gtask list
  716. mGTaskHashMap.clear();
  717. mGTaskListHashMap.clear();
  718. mMetaHashMap.clear();
  719. initGTaskList();
  720. Cursor c = null;
  721. try {
  722. c = mContentResolver.query(Notes.CONTENT_NOTE_URI, SqlNote.PROJECTION_NOTE,
  723. "(type<>? AND parent_id<>?)", new String[] {
  724. String.valueOf(Notes.TYPE_SYSTEM), String.valueOf(Notes.ID_TRASH_FOLER)
  725. }, 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
  726. if (c != null) {
  727. while (c.moveToNext()) {
  728. String gid = c.getString(SqlNote.GTASK_ID_COLUMN);
  729. Node node = mGTaskHashMap.get(gid);
  730. if (node != null) {
  731. mGTaskHashMap.remove(gid);
  732. ContentValues values = new ContentValues(); //在ContentValues中创建键值对。准备通过contentResolver写入数据
  733. values.put(NoteColumns.SYNC_ID, node.getLastModified());
  734. mContentResolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, //进行批量更改,选择参数为NULL,应该可以用insert替换,参数分别为表名和需要更新的value对象。
  735. c.getLong(SqlNote.ID_COLUMN)), values, null, null);
  736. } else {
  737. Log.e(TAG, "something is missed");
  738. throw new ActionFailureException(
  739. "some local items don't have gid after sync");
  740. }
  741. }
  742. } else {
  743. Log.w(TAG, "failed to query local note to refresh sync id");
  744. }
  745. } finally {
  746. if (c != null) {
  747. c.close();
  748. c = null;
  749. }
  750. }
  751. }
  752. /**
  753. * 功能:获取同步账号,mAccount.name
  754. * @author TTS
  755. * @return String
  756. */
  757. public String getSyncAccount() {
  758. return GTaskClient.getInstance().getSyncAccount().name;
  759. }
  760. /**
  761. * 功能:取消同步,置mCancelled为true
  762. * @author TTS
  763. */
  764. public void cancelSync() {
  765. mCancelled = true;
  766. }
  767. }

4、GTaskSyncService.java

  1. package net.micode.notes.gtask.remote;
  2. /*
  3. * Service是在一段不定的时间运行在后台,不和用户交互的应用组件
  4. * 主要方法:
  5. * private void startSync() 启动一个同步工作
  6. * private void cancelSync() 取消同步
  7. * public void onCreate()
  8. * public int onStartCommand(Intent intent, int flags, int startId) service生命周期的组成部分,相当于重启service(比如在被暂停之后),而不是创建一个新的service
  9. * public void onLowMemory() 在没有内存的情况下如果存在service则结束掉这的service
  10. * public IBinder onBind()
  11. * public void sendBroadcast(String msg) 发送同步的相关通知
  12. * public static void startSync(Activity activity)
  13. * public static void cancelSync(Context context)
  14. * public static boolean isSyncing() 判读是否在进行同步
  15. * public static String getProgressString() 获取当前进度的信息
  16. */
  17. public class GTaskSyncService extends Service {
  18. public final static String ACTION_STRING_NAME = "sync_action_type";
  19. public final static int ACTION_START_SYNC = 0;
  20. public final static int ACTION_CANCEL_SYNC = 1;
  21. public final static int ACTION_INVALID = 2;
  22. public final static String GTASK_SERVICE_BROADCAST_NAME = "net.micode.notes.gtask.remote.gtask_sync_service";
  23. public final static String GTASK_SERVICE_BROADCAST_IS_SYNCING = "isSyncing";
  24. public final static String GTASK_SERVICE_BROADCAST_PROGRESS_MSG = "progressMsg";
  25. private static GTaskASyncTask mSyncTask = null;
  26. private static String mSyncProgress = "";
  27. //开始一个同步的工作
  28. private void startSync() {
  29. if (mSyncTask == null) {
  30. mSyncTask = new GTaskASyncTask(this, new GTaskASyncTask.OnCompleteListener() {
  31. public void onComplete() {
  32. mSyncTask = null;
  33. sendBroadcast("");
  34. stopSelf();
  35. }
  36. });
  37. sendBroadcast("");
  38. mSyncTask.execute(); //这个函数让任务是以单线程队列方式或线程池队列方式运行
  39. }
  40. }
  41. private void cancelSync() {
  42. if (mSyncTask != null) {
  43. mSyncTask.cancelSync();
  44. }
  45. }
  46. @Override
  47. public void onCreate() { //初始化一个service
  48. mSyncTask = null;
  49. }
  50. @Override
  51. public int onStartCommand(Intent intent, int flags, int startId) {
  52. Bundle bundle = intent.getExtras();
  53. if (bundle != null && bundle.containsKey(ACTION_STRING_NAME)) {
  54. switch (bundle.getInt(ACTION_STRING_NAME, ACTION_INVALID)) {
  55. //两种情况,开始同步或者取消同步
  56. case ACTION_START_SYNC:
  57. startSync();
  58. break;
  59. case ACTION_CANCEL_SYNC:
  60. cancelSync();
  61. break;
  62. default:
  63. break;
  64. }
  65. return START_STICKY; //等待新的intent来是这个service继续运行
  66. }
  67. return super.onStartCommand(intent, flags, startId);
  68. }
  69. @Override
  70. public void onLowMemory() {
  71. if (mSyncTask != null) {
  72. mSyncTask.cancelSync();
  73. }
  74. }
  75. public IBinder onBind(Intent intent) { //不知道干吗用的
  76. return null;
  77. }
  78. public void sendBroadcast(String msg) {
  79. mSyncProgress = msg;
  80. Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME); //创建一个新的Intent
  81. intent.putExtra(GTASK_SERVICE_BROADCAST_IS_SYNCING, mSyncTask != null); //附加INTENT中的相应参数的值
  82. intent.putExtra(GTASK_SERVICE_BROADCAST_PROGRESS_MSG, msg);
  83. sendBroadcast(intent); //发送这个通知
  84. }
  85. public static void startSync(Activity activity) {//执行一个service,service的内容里的同步动作就是开始同步
  86. GTaskManager.getInstance().setActivityContext(activity);
  87. Intent intent = new Intent(activity, GTaskSyncService.class);
  88. intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC);
  89. activity.startService(intent);
  90. }
  91. public static void cancelSync(Context context) {//执行一个service,service的内容里的同步动作就是取消同步
  92. Intent intent = new Intent(context, GTaskSyncService.class);
  93. intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC);
  94. context.startService(intent);
  95. }
  96. public static boolean isSyncing() {
  97. return mSyncTask != null;
  98. }
  99. public static String getProgressString() {
  100. return mSyncProgress;
  101. }
  102. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/552828
推荐阅读
相关标签
  

闽ICP备14008679号