diff --git a/src/app/src/main/java/net/micode/notes/gtask/data/MetaData.java b/src/app/src/main/java/net/micode/notes/gtask/data/MetaData.java index 3a2050b..32da50d 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/data/MetaData.java +++ b/src/app/src/main/java/net/micode/notes/gtask/data/MetaData.java @@ -24,7 +24,9 @@ import net.micode.notes.tool.GTaskStringUtils; import org.json.JSONException; import org.json.JSONObject; - +/* +关于同步任务的元数据 + */ public class MetaData extends Task { private final static String TAG = MetaData.class.getSimpleName(); @@ -32,11 +34,15 @@ public class MetaData extends Task { public void setMeta(String gid, JSONObject metaInfo) { try { + //put中第一个元素在tool包中 metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid); + //捕获json异常 } catch (JSONException e) { Log.e(TAG, "failed to put related gid"); } + //建立便签 setNotes(metaInfo.toString()); + //函数中的元素依然在tool包中,设置便签名 setName(GTaskStringUtils.META_NOTE_NAME); } @@ -50,10 +56,13 @@ public class MetaData extends Task { } @Override + public void setContentByRemoteJSON(JSONObject js) { + //super调用父类 super.setContentByRemoteJSON(js); if (getNotes() != null) { try { + //将trim()去掉两端无意义字符后的字符串转化为JSON格式 JSONObject metaInfo = new JSONObject(getNotes().trim()); mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); } catch (JSONException e) { @@ -62,7 +71,12 @@ public class MetaData extends Task { } } } - + /*override + 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变。 + 即外壳不变,核心重写! + 重写的好处在于子类可以根据需要,定义特定于自己的行为。 + 也就是说子类能够根据需要实现父类的方法。 + */ @Override public void setContentByLocalJSON(JSONObject js) { // this function should not be called diff --git a/src/app/src/main/java/net/micode/notes/gtask/data/Node.java b/src/app/src/main/java/net/micode/notes/gtask/data/Node.java index 63950e0..778d76b 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/data/Node.java +++ b/src/app/src/main/java/net/micode/notes/gtask/data/Node.java @@ -21,51 +21,53 @@ import android.database.Cursor; import org.json.JSONObject; public abstract class Node { + //定义结点的属性变量 public static final int SYNC_ACTION_NONE = 0; - + //添加远程同步动作 public static final int SYNC_ACTION_ADD_REMOTE = 1; - + //添加当地同步动作 public static final int SYNC_ACTION_ADD_LOCAL = 2; - + //删除远程同步动作 public static final int SYNC_ACTION_DEL_REMOTE = 3; - + //删除当地同步动作 public static final int SYNC_ACTION_DEL_LOCAL = 4; - + //更新远程同步动作 public static final int SYNC_ACTION_UPDATE_REMOTE = 5; - + //更新当地同步动作 public static final int SYNC_ACTION_UPDATE_LOCAL = 6; - + //远程同步动作冲突 public static final int SYNC_ACTION_UPDATE_CONFLICT = 7; - + //当地远程同步冲突 public static final int SYNC_ACTION_ERROR = 8; private String mGid; private String mName; - + //记录最后一次修改 private long mLastModified; private boolean mDeleted; + //构造Node public Node() { mGid = null; mName = ""; mLastModified = 0; mDeleted = false; } - + //创建 public abstract JSONObject getCreateAction(int actionId); - + //更新 public abstract JSONObject getUpdateAction(int actionId); - + //通过远程JSON设置内容 public abstract void setContentByRemoteJSON(JSONObject js); - + //通过本地JSON设置内容 public abstract void setContentByLocalJSON(JSONObject js); - + //从内容获得本地JSON public abstract JSONObject getLocalJSONFromContent(); - + //获得同步动作 public abstract int getSyncAction(Cursor c); - + //下面是一系列构造初始化 public void setGid(String gid) { this.mGid = gid; } diff --git a/src/app/src/main/java/net/micode/notes/gtask/data/SqlData.java b/src/app/src/main/java/net/micode/notes/gtask/data/SqlData.java index d3ec3be..3abbac0 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/data/SqlData.java +++ b/src/app/src/main/java/net/micode/notes/gtask/data/SqlData.java @@ -34,10 +34,12 @@ import net.micode.notes.gtask.exception.ActionFailureException; import org.json.JSONException; import org.json.JSONObject; - +/* +同步任务基本数据 + */ public class SqlData { private static final String TAG = SqlData.class.getSimpleName(); - + //设置初始ID private static final int INVALID_ID = -99999; public static final String[] PROJECTION_DATA = new String[] { @@ -45,10 +47,11 @@ public class SqlData { DataColumns.DATA3 }; + //数据列ID public static final int DATA_ID_COLUMN = 0; - + //数据列的mime_type public static final int DATA_MIME_TYPE_COLUMN = 1; - + //数据列内容 public static final int DATA_CONTENT_COLUMN = 2; public static final int DATA_CONTENT_DATA_1_COLUMN = 3; @@ -68,9 +71,15 @@ public class SqlData { private long mDataContentData1; private String mDataContentData3; - + /*ContentValues + 就是用于保存一些数据(string/boolean/byte/double/float/int/long/short …)信息, + 这些信息可以被数据库操作时方便地使用。 + ContentValues 和 HashTable 类似,都是一种存储的机制, + 但是两者最大的区别就在于:ContentValues 只能存储基本类型的数据, + 像string、int之类的,不能存储对象这种东西,而HashTable却可以存储对象。 + */ private ContentValues mDiffDataValues; - + //构造函数两种 public SqlData(Context context) { mContentResolver = context.getContentResolver(); mIsCreate = true; @@ -88,7 +97,7 @@ public class SqlData { loadFromCursor(c); mDiffDataValues = new ContentValues(); } - + //根据cursor确定属性参数 private void loadFromCursor(Cursor c) { mDataId = c.getLong(DATA_ID_COLUMN); mDataMimeType = c.getString(DATA_MIME_TYPE_COLUMN); @@ -96,7 +105,7 @@ public class SqlData { mDataContentData1 = c.getLong(DATA_CONTENT_DATA_1_COLUMN); mDataContentData3 = c.getString(DATA_CONTENT_DATA_3_COLUMN); } - + //根据JSON的内容设置属性 public void setContent(JSONObject js) throws JSONException { long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID; if (mIsCreate || mDataId != dataId) { @@ -129,13 +138,15 @@ public class SqlData { } mDataContentData3 = dataContentData3; } - + //利用JSON获取内容属性 public JSONObject getContent() throws JSONException { + //记录未在数据库建立 if (mIsCreate) { Log.e(TAG, "it seems that we haven't created this in database yet"); return null; } JSONObject js = new JSONObject(); + //利用JSON将一系列参数添加进来 js.put(DataColumns.ID, mDataId); js.put(DataColumns.MIME_TYPE, mDataMimeType); js.put(DataColumns.CONTENT, mDataContent); @@ -143,18 +154,23 @@ public class SqlData { js.put(DataColumns.DATA3, mDataContentData3); return js; } - + //传入noteid为建立便签准备 public void commit(long noteId, boolean validateVersion, long version) { if (mIsCreate) { if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) { mDiffDataValues.remove(DataColumns.ID); } - + //保存信息 mDiffDataValues.put(DataColumns.NOTE_ID, noteId); Uri uri = mContentResolver.insert(Notes.CONTENT_DATA_URI, mDiffDataValues); try { + /* + Uri的getPathSegments()方法返回的是一个元素为String的List, + 每个元素都是从Uri截取出来的一部分。 + */ mDataId = Long.valueOf(uri.getPathSegments().get(1)); + //捕获数字格式异常 } catch (NumberFormatException e) { Log.e(TAG, "Get note id error :" + e.toString()); throw new ActionFailureException("create note failed"); @@ -163,6 +179,10 @@ public class SqlData { if (mDiffDataValues.size() > 0) { int result = 0; if (!validateVersion) { + /* + ContentResolver.update方法ContentValue只更新指定的字段 + .update(Uri arg0,ContentValues arg1,String arg2,String[] arg3) + */ result = mContentResolver.update(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mDataId), mDiffDataValues, null, null); } else { @@ -173,12 +193,13 @@ public class SqlData { String.valueOf(noteId), String.valueOf(version) }); } + //未更新 if (result == 0) { Log.w(TAG, "there is no update. maybe user updates note when syncing"); } } } - + //清空记录,建立失败 mDiffDataValues.clear(); mIsCreate = false; } diff --git a/src/app/src/main/java/net/micode/notes/gtask/data/SqlNote.java b/src/app/src/main/java/net/micode/notes/gtask/data/SqlNote.java index 79a4095..476c307 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/data/SqlNote.java +++ b/src/app/src/main/java/net/micode/notes/gtask/data/SqlNote.java @@ -51,19 +51,19 @@ public class SqlNote { NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID, NoteColumns.VERSION }; - + //ID列 public static final int ID_COLUMN = 0; - + //警示日期列 public static final int ALERTED_DATE_COLUMN = 1; - + //背景颜色id列 public static final int BG_COLOR_ID_COLUMN = 2; - + //建立的日期列 public static final int CREATED_DATE_COLUMN = 3; - + //有连接列 public static final int HAS_ATTACHMENT_COLUMN = 4; - + //修改日期列 public static final int MODIFIED_DATE_COLUMN = 5; - + //便签数目列 public static final int NOTES_COUNT_COLUMN = 6; public static final int PARENT_ID_COLUMN = 7; @@ -121,7 +121,7 @@ public class SqlNote { private ContentValues mDiffNoteValues; private ArrayList mDataList; - + //三类构造函数 public SqlNote(Context context) { mContext = context; mContentResolver = context.getContentResolver(); @@ -165,7 +165,7 @@ public class SqlNote { mDiffNoteValues = new ContentValues(); } - + //递归查询符合的id private void loadFromCursor(long id) { Cursor c = null; try { @@ -184,7 +184,7 @@ public class SqlNote { c.close(); } } - + //利用cursor设置属性参数 private void loadFromCursor(Cursor c) { mId = c.getLong(ID_COLUMN); mAlertDate = c.getLong(ALERTED_DATE_COLUMN); @@ -199,7 +199,7 @@ public class SqlNote { mWidgetType = c.getInt(WIDGET_TYPE_COLUMN); mVersion = c.getLong(VERSION_COLUMN); } - + //根据数据库查询id private void loadDataContent() { Cursor c = null; mDataList.clear(); @@ -225,7 +225,7 @@ public class SqlNote { c.close(); } } - + //根据JSON的内容设置属性参数 public boolean setContent(JSONObject js) { try { JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); @@ -358,17 +358,18 @@ public class SqlNote { } return true; } - + //小米便签获取内容 public JSONObject getContent() { try { JSONObject js = new JSONObject(); - + //未创建内容加入数据库中 if (mIsCreate) { Log.e(TAG, "it seems that we haven't created this in database yet"); return null; } JSONObject note = new JSONObject(); + //如果便签类型匹配 if (mType == Notes.TYPE_NOTE) { note.put(NoteColumns.ID, mId); note.put(NoteColumns.ALERTED_DATE, mAlertDate); @@ -406,36 +407,36 @@ public class SqlNote { } return null; } - + //设置父ID public void setParentId(long id) { mParentId = id; mDiffNoteValues.put(NoteColumns.PARENT_ID, id); } - + //设置日程同步ID public void setGtaskId(String gid) { mDiffNoteValues.put(NoteColumns.GTASK_ID, gid); } - + //设置同步ID public void setSyncId(long syncId) { mDiffNoteValues.put(NoteColumns.SYNC_ID, syncId); } - + //重新设定本地修改 public void resetLocalModified() { mDiffNoteValues.put(NoteColumns.LOCAL_MODIFIED, 0); } - + //获取对象ID public long getId() { return mId; } - + //获取父ID public long getParentId() { return mParentId; } - + //获取对象片段 public String getSnippet() { return mSnippet; } - + //判断是否为便签ID public boolean isNoteType() { return mType == Notes.TYPE_NOTE; } @@ -453,6 +454,7 @@ public class SqlNote { Log.e(TAG, "Get note id error :" + e.toString()); throw new ActionFailureException("create note failed"); } + //建立线程ID失败 if (mId == 0) { throw new IllegalStateException("Create thread id failed"); } @@ -470,6 +472,7 @@ public class SqlNote { if (mDiffNoteValues.size() > 0) { mVersion ++; int result = 0; + //若有效 if (!validateVersion) { result = mContentResolver.update(Notes.CONTENT_NOTE_URI, mDiffNoteValues, "(" + NoteColumns.ID + "=?)", new String[] { @@ -482,6 +485,7 @@ public class SqlNote { String.valueOf(mId), String.valueOf(mVersion) }); } + //未更新,可能在同步时更新了 if (result == 0) { Log.w(TAG, "there is no update. maybe user updates note when syncing"); } @@ -494,7 +498,7 @@ public class SqlNote { } } - // refresh local info + // 刷新本地infof loadFromCursor(mId); if (mType == Notes.TYPE_NOTE) loadDataContent(); diff --git a/src/app/src/main/java/net/micode/notes/gtask/data/Task.java b/src/app/src/main/java/net/micode/notes/gtask/data/Task.java index 6a19454..d03f3bc 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/data/Task.java +++ b/src/app/src/main/java/net/micode/notes/gtask/data/Task.java @@ -44,7 +44,7 @@ public class Task extends Node { private Task mPriorSibling; private TaskList mParent; - + //构造函数 public Task() { super(); mCompleted = false; @@ -53,22 +53,22 @@ public class Task extends Node { mParent = null; mMetaInfo = null; } - + //获得创造的动作 public JSONObject getCreateAction(int actionId) { JSONObject js = new JSONObject(); try { - // action_type + // 动作类型 js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); - // action_id + // 动作ID js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); - // index + // 索引 js.put(GTaskStringUtils.GTASK_JSON_INDEX, mParent.getChildTaskIndex(this)); - // entity_delta + // JSON创建内容 JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); @@ -79,17 +79,17 @@ public class Task extends Node { } js.put(GTaskStringUtils.GTASK_JSON_ENTITY_DELTA, entity); - // parent_id + // 父ID js.put(GTaskStringUtils.GTASK_JSON_PARENT_ID, mParent.getGid()); - // dest_parent_type + // 目的父类型 js.put(GTaskStringUtils.GTASK_JSON_DEST_PARENT_TYPE, GTaskStringUtils.GTASK_JSON_TYPE_GROUP); - // list_id + // 列表ID js.put(GTaskStringUtils.GTASK_JSON_LIST_ID, mParent.getGid()); - // prior_sibling_id + // 优先同级ID if (mPriorSibling != null) { js.put(GTaskStringUtils.GTASK_JSON_PRIOR_SIBLING_ID, mPriorSibling.getGid()); } @@ -102,22 +102,22 @@ public class Task extends Node { return js; } - + //获得更新的动作 public JSONObject getUpdateAction(int actionId) { JSONObject js = new JSONObject(); try { - // action_type + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE); - // action_id + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); // id js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); - // entity_delta + JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); if (getNotes() != null) { @@ -134,7 +134,7 @@ public class Task extends Node { return js; } - + //通过远程JSON设置内容 public void setContentByRemoteJSON(JSONObject js) { if (js != null) { try { @@ -143,27 +143,27 @@ public class Task extends Node { setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); } - // last_modified + // 最后一次修改 if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)); } - // name + // 姓名 if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)); } - // notes + // 有便签 if (js.has(GTaskStringUtils.GTASK_JSON_NOTES)) { setNotes(js.getString(GTaskStringUtils.GTASK_JSON_NOTES)); } - // deleted + // 删除 if (js.has(GTaskStringUtils.GTASK_JSON_DELETED)) { setDeleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_DELETED)); } - // completed + // 完成 if (js.has(GTaskStringUtils.GTASK_JSON_COMPLETED)) { setCompleted(js.getBoolean(GTaskStringUtils.GTASK_JSON_COMPLETED)); } @@ -174,8 +174,9 @@ public class Task extends Node { } } } - + //通过本地JSON设置内容 public void setContentByLocalJSON(JSONObject js) { + //JSON空消息设置内容失败 if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE) || !js.has(GTaskStringUtils.META_HEAD_DATA)) { Log.w(TAG, "setContentByLocalJSON: nothing is avaiable"); @@ -184,12 +185,12 @@ public class Task extends Node { try { JSONObject note = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); JSONArray dataArray = js.getJSONArray(GTaskStringUtils.META_HEAD_DATA); - + //无效类型 if (note.getInt(NoteColumns.TYPE) != Notes.TYPE_NOTE) { Log.e(TAG, "invalid type"); return; } - + //如果文本备份找到一致就跳出 for (int i = 0; i < dataArray.length(); i++) { JSONObject data = dataArray.getJSONObject(i); if (TextUtils.equals(data.getString(DataColumns.MIME_TYPE), DataConstants.NOTE)) { @@ -208,7 +209,7 @@ public class Task extends Node { String name = getName(); try { if (mMetaInfo == null) { - // new task created from web + // 初始为空,从网页创建的新任务 if (name == null) { Log.w(TAG, "the note seems to be an empty one"); return null; @@ -225,7 +226,7 @@ public class Task extends Node { js.put(GTaskStringUtils.META_HEAD_NOTE, note); return js; } else { - // synced task + // 同步任务 JSONObject note = mMetaInfo.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); JSONArray dataArray = mMetaInfo.getJSONArray(GTaskStringUtils.META_HEAD_DATA); @@ -246,7 +247,7 @@ public class Task extends Node { return null; } } - + //设置元信息 public void setMetaInfo(MetaData metaData) { if (metaData != null && metaData.getNotes() != null) { try { @@ -275,29 +276,29 @@ public class Task extends Node { return SYNC_ACTION_UPDATE_LOCAL; } - // validate the note id now + // 现在验证便签 id if (c.getLong(SqlNote.ID_COLUMN) != noteInfo.getLong(NoteColumns.ID)) { Log.w(TAG, "note id doesn't match"); return SYNC_ACTION_UPDATE_LOCAL; } if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { - // there is no local update + // 没有本地更新 if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { // no update both side return SYNC_ACTION_NONE; } else { - // apply remote to local + // 将远程应用到本地 return SYNC_ACTION_UPDATE_LOCAL; } } else { - // validate gtask id + // 验证日程同步 id if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { Log.e(TAG, "gtask id doesn't match"); return SYNC_ACTION_ERROR; } if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { - // local modification only + // 只有本地修改 return SYNC_ACTION_UPDATE_REMOTE; } else { return SYNC_ACTION_UPDATE_CONFLICT; @@ -310,36 +311,36 @@ public class Task extends Node { return SYNC_ACTION_ERROR; } - + //值得保留 public boolean isWorthSaving() { return mMetaInfo != null || (getName() != null && getName().trim().length() > 0) || (getNotes() != null && getNotes().trim().length() > 0); } - + //设置完成 public void setCompleted(boolean completed) { this.mCompleted = completed; } - + //设置便签 public void setNotes(String notes) { this.mNotes = notes; } - + //设置优先级 public void setPriorSibling(Task priorSibling) { this.mPriorSibling = priorSibling; } - + //设置父列表 public void setParent(TaskList parent) { this.mParent = parent; } - + //获得完成项 public boolean getCompleted() { return this.mCompleted; } - + //获得便签 public String getNotes() { return this.mNotes; } - + //获得优先级 public Task getPriorSibling() { return this.mPriorSibling; } diff --git a/src/app/src/main/java/net/micode/notes/gtask/data/TaskList.java b/src/app/src/main/java/net/micode/notes/gtask/data/TaskList.java index 4ea21c5..51132db 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/data/TaskList.java +++ b/src/app/src/main/java/net/micode/notes/gtask/data/TaskList.java @@ -47,17 +47,17 @@ public class TaskList extends Node { JSONObject js = new JSONObject(); try { - // action_type + // 动作类型 js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_CREATE); - // action_id + // 动作ID js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); - // index + // 索引 js.put(GTaskStringUtils.GTASK_JSON_INDEX, mIndex); - // entity_delta + // JSON设置内容 JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); entity.put(GTaskStringUtils.GTASK_JSON_CREATOR_ID, "null"); @@ -78,17 +78,17 @@ public class TaskList extends Node { JSONObject js = new JSONObject(); try { - // action_type + js.put(GTaskStringUtils.GTASK_JSON_ACTION_TYPE, GTaskStringUtils.GTASK_JSON_ACTION_TYPE_UPDATE); - // action_id + js.put(GTaskStringUtils.GTASK_JSON_ACTION_ID, actionId); - // id + js.put(GTaskStringUtils.GTASK_JSON_ID, getGid()); - // entity_delta + JSONObject entity = new JSONObject(); entity.put(GTaskStringUtils.GTASK_JSON_NAME, getName()); entity.put(GTaskStringUtils.GTASK_JSON_DELETED, getDeleted()); @@ -102,21 +102,21 @@ public class TaskList extends Node { return js; } - + //通过远程JSON设置内容 public void setContentByRemoteJSON(JSONObject js) { if (js != null) { try { - // id + if (js.has(GTaskStringUtils.GTASK_JSON_ID)) { setGid(js.getString(GTaskStringUtils.GTASK_JSON_ID)); } - // last_modified + // 最后一次修改 if (js.has(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)) { setLastModified(js.getLong(GTaskStringUtils.GTASK_JSON_LAST_MODIFIED)); } - // name + // 姓名 if (js.has(GTaskStringUtils.GTASK_JSON_NAME)) { setName(js.getString(GTaskStringUtils.GTASK_JSON_NAME)); } @@ -128,24 +128,27 @@ public class TaskList extends Node { } } } - + //通过本地JSON设置内容(在Node中有涉及) public void setContentByLocalJSON(JSONObject js) { if (js == null || !js.has(GTaskStringUtils.META_HEAD_NOTE)) { Log.w(TAG, "setContentByLocalJSON: nothing is avaiable"); } - + //建立JSON文件夹加入元头便签 try { JSONObject folder = js.getJSONObject(GTaskStringUtils.META_HEAD_NOTE); if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_FOLDER) { String name = folder.getString(NoteColumns.SNIPPET); + //设置姓名 setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + name); + //获得类型与便签系统类型一样时 } else if (folder.getInt(NoteColumns.TYPE) == Notes.TYPE_SYSTEM) { if (folder.getLong(NoteColumns.ID) == Notes.ID_ROOT_FOLDER) setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_DEFAULT); else if (folder.getLong(NoteColumns.ID) == Notes.ID_CALL_RECORD_FOLDER) setName(GTaskStringUtils.MIUI_FOLDER_PREFFIX + GTaskStringUtils.FOLDER_CALL_NOTE); + //无效系统文件夹 else Log.e(TAG, "invalid system folder"); } else { @@ -156,7 +159,7 @@ public class TaskList extends Node { e.printStackTrace(); } } - + //通过内容获得本地JSON public JSONObject getLocalJSONFromContent() { try { JSONObject js = new JSONObject(); @@ -167,6 +170,7 @@ public class TaskList extends Node { folderName = folderName.substring(GTaskStringUtils.MIUI_FOLDER_PREFFIX.length(), folderName.length()); folder.put(NoteColumns.SNIPPET, folderName); + //文件夹名和默认文件夹名一致时 if (folderName.equals(GTaskStringUtils.FOLDER_DEFAULT) || folderName.equals(GTaskStringUtils.FOLDER_CALL_NOTE)) folder.put(NoteColumns.TYPE, Notes.TYPE_SYSTEM); @@ -186,25 +190,25 @@ public class TaskList extends Node { public int getSyncAction(Cursor c) { try { if (c.getInt(SqlNote.LOCAL_MODIFIED_COLUMN) == 0) { - // there is no local update + // t没有本地更新 if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { - // no update both side + // 双方无更新 return SYNC_ACTION_NONE; } else { - // apply remote to local + // 将远程应用到本地 return SYNC_ACTION_UPDATE_LOCAL; } } else { - // validate gtask id + // 验证日程同步 id if (!c.getString(SqlNote.GTASK_ID_COLUMN).equals(getGid())) { Log.e(TAG, "gtask id doesn't match"); return SYNC_ACTION_ERROR; } if (c.getLong(SqlNote.SYNC_ID_COLUMN) == getLastModified()) { - // local modification only + // 只有本地修改 return SYNC_ACTION_UPDATE_REMOTE; } else { - // for folder conflicts, just apply local modification + // 对于文件夹冲突,只需应用本地修改 return SYNC_ACTION_UPDATE_REMOTE; } } @@ -215,17 +219,17 @@ public class TaskList extends Node { return SYNC_ACTION_ERROR; } - + //获得子任务数量 public int getChildTaskCount() { return mChildren.size(); } - + //添加子任务 public boolean addChildTask(Task task) { boolean ret = false; if (task != null && !mChildren.contains(task)) { ret = mChildren.add(task); if (ret) { - // need to set prior sibling and parent + // 需要设置之前的父结点和兄节点 task.setPriorSibling(mChildren.isEmpty() ? null : mChildren .get(mChildren.size() - 1)); task.setParent(this); @@ -233,7 +237,7 @@ public class TaskList extends Node { } return ret; } - + //通过索引添加子任务 public boolean addChildTask(Task task, int index) { if (index < 0 || index > mChildren.size()) { Log.e(TAG, "add child task: invalid index"); @@ -244,14 +248,14 @@ public class TaskList extends Node { if (task != null && pos == -1) { mChildren.add(index, task); - // update the task list + // 更新任务列表 Task preTask = null; Task afterTask = null; if (index != 0) preTask = mChildren.get(index - 1); if (index != mChildren.size() - 1) afterTask = mChildren.get(index + 1); - + //设置同级 task.setPriorSibling(preTask); if (afterTask != null) afterTask.setPriorSibling(task); @@ -259,7 +263,7 @@ public class TaskList extends Node { return true; } - + //移除子任务 public boolean removeChildTask(Task task) { boolean ret = false; int index = mChildren.indexOf(task); @@ -267,11 +271,11 @@ public class TaskList extends Node { ret = mChildren.remove(task); if (ret) { - // reset prior sibling and parent + task.setPriorSibling(null); task.setParent(null); - // update the task list + // 更新任务列表 if (index != mChildren.size()) { mChildren.get(index).setPriorSibling( index == 0 ? null : mChildren.get(index - 1)); @@ -280,14 +284,14 @@ public class TaskList extends Node { } return ret; } - + //移动子任务 public boolean moveChildTask(Task task, int index) { if (index < 0 || index >= mChildren.size()) { Log.e(TAG, "move child task: invalid index"); return false; } - + //子任务索引位置 int pos = mChildren.indexOf(task); if (pos == -1) { Log.e(TAG, "move child task: the task should in the list"); @@ -298,7 +302,7 @@ public class TaskList extends Node { return true; return (removeChildTask(task) && addChildTask(task, index)); } - + //通过Gid找到子任务 public Task findChildTaskByGid(String gid) { for (int i = 0; i < mChildren.size(); i++) { Task t = mChildren.get(i); @@ -308,7 +312,7 @@ public class TaskList extends Node { } return null; } - + //获得子任务的索引 public int getChildTaskIndex(Task task) { return mChildren.indexOf(task); } @@ -320,7 +324,7 @@ public class TaskList extends Node { } return mChildren.get(index); } - + //通过Gid获取子任务 public Task getChilTaskByGid(String gid) { for (Task task : mChildren) { if (task.getGid().equals(gid)) @@ -332,7 +336,7 @@ public class TaskList extends Node { public ArrayList getChildTaskList() { return this.mChildren; } - + //设置索引 public void setIndex(int index) { this.mIndex = index; } diff --git a/src/app/src/main/java/net/micode/notes/gtask/exception/ActionFailureException.java b/src/app/src/main/java/net/micode/notes/gtask/exception/ActionFailureException.java index 15504be..b2bc8f1 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/exception/ActionFailureException.java +++ b/src/app/src/main/java/net/micode/notes/gtask/exception/ActionFailureException.java @@ -15,7 +15,9 @@ */ package net.micode.notes.gtask.exception; - +/* +动作失败异常 + */ public class ActionFailureException extends RuntimeException { private static final long serialVersionUID = 4425249765923293627L; diff --git a/src/app/src/main/java/net/micode/notes/gtask/exception/NetworkFailureException.java b/src/app/src/main/java/net/micode/notes/gtask/exception/NetworkFailureException.java index b08cfb1..e8b43be 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/exception/NetworkFailureException.java +++ b/src/app/src/main/java/net/micode/notes/gtask/exception/NetworkFailureException.java @@ -15,7 +15,9 @@ */ package net.micode.notes.gtask.exception; - +/* +网络失败异常 + */ public class NetworkFailureException extends Exception { private static final long serialVersionUID = 2107610287180234136L; diff --git a/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskASyncTask.java b/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskASyncTask.java index 2315ef1..9e7c689 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskASyncTask.java +++ b/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskASyncTask.java @@ -28,7 +28,9 @@ import net.micode.notes.R; import net.micode.notes.ui.NotesListActivity; import net.micode.notes.ui.NotesPreferenceActivity; - +/* +Gtask异步任务 + */ public class GTaskASyncTask extends AsyncTask { private static int GTASK_SYNC_NOTIFICATION_ID = 5234235; @@ -52,7 +54,7 @@ public class GTaskASyncTask extends AsyncTask { .getSystemService(Context.NOTIFICATION_SERVICE); mTaskManager = GTaskManager.getInstance(); } - + //取消同步 public void cancelSync() { mTaskManager.cancelSync(); } @@ -62,7 +64,7 @@ public class GTaskASyncTask extends AsyncTask { message }); } - + //展示提示 private void showNotification(int tickerId, String content) { Notification notification = new Notification(R.drawable.notification, mContext .getString(tickerId), System.currentTimeMillis()); @@ -77,8 +79,8 @@ public class GTaskASyncTask extends AsyncTask { pendingIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, NotesListActivity.class), 0); } - //notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, - // pendingIntent); + /* notification.setLatestEventInfo(mContext, mContext.getString(R.string.app_name), content, + pendingIntent);*/ mNotifiManager.notify(GTASK_SYNC_NOTIFICATION_ID, notification); } diff --git a/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskClient.java b/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskClient.java index c67dfdf..c3698de 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskClient.java +++ b/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskClient.java @@ -60,7 +60,10 @@ import java.util.zip.GZIPInputStream; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; - +/* +Gtask客户端类,提供登录Google账户,创建任务和任务列表 +添加和删除节点,提交、重置更新,获取任务列表等功能 + */ public class GTaskClient { private static final String TAG = GTaskClient.class.getSimpleName(); @@ -108,7 +111,7 @@ public class GTaskClient { } return mInstance; } - + //登录;注册 public boolean login(Activity activity) { // we suppose that the cookie would expire after 5 minutes // then we need to re-login @@ -163,7 +166,7 @@ public class GTaskClient { mLoggedin = true; return true; } - + //登录Google账户 private String loginGoogleAccount(Activity activity, boolean invalidateToken) { String authToken; AccountManager accountManager = AccountManager.get(activity); @@ -206,7 +209,7 @@ public class GTaskClient { return authToken; } - + //尝试登录日程同步 private boolean tryToLoginGtask(Activity activity, String authToken) { if (!loginGtask(authToken)) { // maybe the auth token is out of date, now let's invalidate the @@ -279,18 +282,18 @@ public class GTaskClient { return true; } - + //获得动作 private int getActionId() { return mActionId++; } - + //建立互联网请求 private HttpPost createHttpPost() { HttpPost httpPost = new HttpPost(mPostUrl); httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); httpPost.setHeader("AT", "1"); return httpPost; } - + //获得回复消息 private String getResponseContent(HttpEntity entity) throws IOException { String contentEncoding = null; if (entity.getContentEncoding() != null) { @@ -432,7 +435,7 @@ public class GTaskClient { } } } - + //添加更新节点 public void addUpdateNode(Node node) throws NetworkFailureException { if (node != null) { // too many update items may result in an error @@ -446,7 +449,7 @@ public class GTaskClient { mUpdateArray.put(node.getUpdateAction(getActionId())); } } - + //移动任务 public void moveTask(Task task, TaskList preParent, TaskList curParent) throws NetworkFailureException { commitUpdate(); @@ -485,7 +488,7 @@ public class GTaskClient { throw new ActionFailureException("move task: handing jsonobject failed"); } } - + //删除结点 public void deleteNode(Node node) throws NetworkFailureException { commitUpdate(); try { diff --git a/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskManager.java b/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskManager.java index d2b4082..91683b3 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskManager.java +++ b/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskManager.java @@ -47,7 +47,10 @@ import java.util.HashSet; import java.util.Iterator; import java.util.Map; - +/* +Gtask管理者类,提供初始化任务列表,同步便签内容和文件夹 +添加、更新本地和远端节点,更新本地同步任务ID等功能 + */ public class GTaskManager { private static final String TAG = GTaskManager.class.getSimpleName(); @@ -98,7 +101,7 @@ public class GTaskManager { mGidToNid = new HashMap(); mNidToGid = new HashMap(); } - + //获得实例 public static synchronized GTaskManager getInstance() { if (mInstance == null) { mInstance = new GTaskManager(); diff --git a/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskSyncService.java b/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskSyncService.java index cca36f7..404a180 100644 --- a/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskSyncService.java +++ b/src/app/src/main/java/net/micode/notes/gtask/remote/GTaskSyncService.java @@ -22,7 +22,9 @@ import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.os.IBinder; - +/* +Gtask同步服务 + */ public class GTaskSyncService extends Service { public final static String ACTION_STRING_NAME = "sync_action_type"; @@ -96,7 +98,7 @@ public class GTaskSyncService extends Service { public IBinder onBind(Intent intent) { return null; } - + //发送广播 public void sendBroadcast(String msg) { mSyncProgress = msg; Intent intent = new Intent(GTASK_SERVICE_BROADCAST_NAME); @@ -104,24 +106,24 @@ public class GTaskSyncService extends Service { intent.putExtra(GTASK_SERVICE_BROADCAST_PROGRESS_MSG, msg); sendBroadcast(intent); } - + //开始同步 public static void startSync(Activity activity) { GTaskManager.getInstance().setActivityContext(activity); Intent intent = new Intent(activity, GTaskSyncService.class); intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_START_SYNC); activity.startService(intent); } - + //取消同步 public static void cancelSync(Context context) { Intent intent = new Intent(context, GTaskSyncService.class); intent.putExtra(GTaskSyncService.ACTION_STRING_NAME, GTaskSyncService.ACTION_CANCEL_SYNC); context.startService(intent); } - + //判断同步 public static boolean isSyncing() { return mSyncTask != null; } - + //获取进程字符串 public static String getProgressString() { return mSyncProgress; } diff --git a/src/app/src/main/java/net/micode/notes/tool/BackupUtils.java b/src/app/src/main/java/net/micode/notes/tool/BackupUtils.java index 39f6ec4..8cc9e1b 100644 --- a/src/app/src/main/java/net/micode/notes/tool/BackupUtils.java +++ b/src/app/src/main/java/net/micode/notes/tool/BackupUtils.java @@ -35,12 +35,14 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; - +/* +备份工具类 + */ public class BackupUtils { private static final String TAG = "BackupUtils"; // Singleton stuff private static BackupUtils sInstance; - + //获得实例 public static synchronized BackupUtils getInstance(Context context) { if (sInstance == null) { sInstance = new BackupUtils(context); @@ -64,27 +66,32 @@ public class BackupUtils { public static final int STATE_SUCCESS = 4; private TextExport mTextExport; - + //构造函数 private BackupUtils(Context context) { mTextExport = new TextExport(context); } private static boolean externalStorageAvailable() { + /*Environment类中提供了几个静态常量用于标识外部存储的状态 + MEDIA_MOUNTED存储媒体已经挂载 + 并且挂载点可读/写 + 可以通过静态方法getExternalStorageState()来获取外部存储的状态 + 其中.equals表示的是二者的内容是否相等*/ return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } - + //导出文本 public int exportToText() { return mTextExport.exportToText(); } - + //获取导出文本名字 public String getExportedTextFileName() { return mTextExport.mFileName; } - + //获取导出文本目录 public String getExportedTextFileDir() { return mTextExport.mFileDirectory; } - + //引用内部类包含了之前data.Notes中的NoteColumns和DataColumns private static class TextExport { private static final String[] NOTE_PROJECTION = { NoteColumns.ID, @@ -92,13 +99,13 @@ public class BackupUtils { NoteColumns.SNIPPET, NoteColumns.TYPE }; - + //便签列id private static final int NOTE_COLUMN_ID = 0; - + //便签修改的日期 private static final int NOTE_COLUMN_MODIFIED_DATE = 1; - + //便签列片段 private static final int NOTE_COLUMN_SNIPPET = 2; - + //数据项 private static final String[] DATA_PROJECTION = { DataColumns.CONTENT, DataColumns.MIME_TYPE, @@ -107,15 +114,15 @@ public class BackupUtils { DataColumns.DATA3, DataColumns.DATA4, }; - + //数据列中内容 private static final int DATA_COLUMN_CONTENT = 0; - + //数据列中mime类型 private static final int DATA_COLUMN_MIME_TYPE = 1; - + //数据中拨打日期 private static final int DATA_COLUMN_CALL_DATE = 2; - + //数据列中的电话号码 private static final int DATA_COLUMN_PHONE_NUMBER = 4; - + //文本格式 private final String [] TEXT_FORMAT; private static final int FORMAT_FOLDER_NAME = 0; private static final int FORMAT_NOTE_DATE = 1; @@ -124,14 +131,14 @@ public class BackupUtils { private Context mContext; private String mFileName; private String mFileDirectory; - + //文本导出初始化 public TextExport(Context context) { TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); mContext = context; mFileName = ""; mFileDirectory = ""; } - + //获取文本格式类型 private String getFormat(int id) { return TEXT_FORMAT[id]; } @@ -140,13 +147,18 @@ public class BackupUtils { * Export the folder identified by folder id to text */ private void exportFolderToText(String folderId, PrintStream ps) { - // Query notes belong to this folder + // 查询属于此文件夹的Notes Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { folderId }, null); - + //下面有很多地方调用了android.database.Cursor类有一些固定用法 if (notesCursor != null) { + /* + 查询出来的cursor的初始位置是指向第一条记录的前一个位置的 + cursor.moveToFirst()指向查询结果的第一个位置。 + 一般通过判断cursor.moveToFirst()的值为true或false来确定查询结果是否为空。 + */ if (notesCursor.moveToFirst()) { do { // Print note's last modified date @@ -174,6 +186,7 @@ public class BackupUtils { if (dataCursor != null) { if (dataCursor.moveToFirst()) { do { + //获取mimeType String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); if (DataConstants.CALL_NOTE.equals(mimeType)) { // Print phone number @@ -185,11 +198,11 @@ public class BackupUtils { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), phoneNumber)); } - // Print call date + // 打印调用日期 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat .format(mContext.getString(R.string.format_datetime_mdhm), callDate))); - // Print call attachment location + // 打印调用位置 if (!TextUtils.isEmpty(location)) { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), location)); @@ -203,9 +216,10 @@ public class BackupUtils { } } while (dataCursor.moveToNext()); } + //及时关闭cursor dataCursor.close(); } - // print a line separator between note + // 在便签之间打印一个行分隔符 try { ps.write(new byte[] { Character.LINE_SEPARATOR, Character.LETTER_NUMBER @@ -215,21 +229,23 @@ public class BackupUtils { } } - /** - * Note will be exported as text which is user readable + /* + 便签将被导出为用户可读的文本 */ public int exportToText() { + //判断是否有外部存储空间 if (!externalStorageAvailable()) { Log.d(TAG, "Media was not mounted"); return STATE_SD_CARD_UNMOUONTED; } - + //定义输出流ps PrintStream ps = getExportToTextPrintStream(); + //检查null if (ps == null) { Log.e(TAG, "get print stream error"); return STATE_SYSTEM_ERROR; } - // First export folder and its notes + // 第一个导出文件夹及其注释 Cursor folderCursor = mContext.getContentResolver().query( Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, @@ -240,24 +256,28 @@ public class BackupUtils { if (folderCursor != null) { if (folderCursor.moveToFirst()) { do { - // Print folder's name + // 打印文件夹的名字 String folderName = ""; + //若id为最近记录里调用的文件中 if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { folderName = mContext.getString(R.string.call_record_folder_name); } else { folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); } + //若文本工具为空 if (!TextUtils.isEmpty(folderName)) { ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); } + //获得文件id String folderId = folderCursor.getString(NOTE_COLUMN_ID); exportFolderToText(folderId, ps); } while (folderCursor.moveToNext()); } + //及时关闭folderCursor folderCursor.close(); } - // Export notes in root's folder + // 在根目录下导出注释 Cursor noteCursor = mContext.getContentResolver().query( Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, @@ -265,13 +285,15 @@ public class BackupUtils { + "=0", null, null); if (noteCursor != null) { + //如果不为空 if (noteCursor.moveToFirst()) { do { ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( mContext.getString(R.string.format_datetime_mdhm), noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); - // Query data belong to this note + // 查询数据属于该便签 String noteId = noteCursor.getString(NOTE_COLUMN_ID); + //通过id导出文本 exportNoteToText(noteId, ps); } while (noteCursor.moveToNext()); } @@ -282,8 +304,8 @@ public class BackupUtils { return STATE_SUCCESS; } - /** - * Get a print stream pointed to the file {@generateExportedTextFile} + /* + 获取指向该文件的打印流 {@generateExportedTextFile} */ private PrintStream getExportToTextPrintStream() { File file = generateFileMountedOnSDcard(mContext, R.string.file_path, @@ -309,8 +331,8 @@ public class BackupUtils { } } - /** - * Generate the text file to store imported data + /* + 生成文本文件来存储导入的数据 */ private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { StringBuilder sb = new StringBuilder(); @@ -324,15 +346,19 @@ public class BackupUtils { File file = new File(sb.toString()); try { + //目录不存在就创建 if (!filedir.exists()) { filedir.mkdir(); } + //文件不存在也创建 if (!file.exists()) { file.createNewFile(); } return file; + //安全性异常 } catch (SecurityException e) { e.printStackTrace(); + //IO异常 } catch (IOException e) { e.printStackTrace(); } diff --git a/src/app/src/main/java/net/micode/notes/tool/DataUtils.java b/src/app/src/main/java/net/micode/notes/tool/DataUtils.java index 2a14982..d6d437a 100644 --- a/src/app/src/main/java/net/micode/notes/tool/DataUtils.java +++ b/src/app/src/main/java/net/micode/notes/tool/DataUtils.java @@ -34,14 +34,19 @@ import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; import java.util.ArrayList; import java.util.HashSet; - +/* +便签数据处理工具类 + */ public class DataUtils { public static final String TAG = "DataUtils"; + //批处理删除便签 public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + //id不存在 if (ids == null) { Log.d(TAG, "the ids is null"); return true; } + //id在哈希表里 if (ids.size() == 0) { Log.d(TAG, "no id is in the hashset"); return true; @@ -49,21 +54,25 @@ public class DataUtils { ArrayList operationList = new ArrayList(); for (long id : ids) { + //id是便签根文件夹id if(id == Notes.ID_ROOT_FOLDER) { Log.e(TAG, "Don't delete system folder root"); continue; } + //使用ArrayList来保存 ContentProviderOperation操作 ContentProviderOperation.Builder builder = ContentProviderOperation .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); operationList.add(builder.build()); } try { + //通过ContentResolver 的applyBatch()函数来应用批量操作: ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); if (results == null || results.length == 0 || results[0] == null) { Log.d(TAG, "delete notes failed, ids:" + ids.toString()); return false; } return true; + //捕捉远程异常 } catch (RemoteException e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } catch (OperationApplicationException e) { @@ -79,7 +88,7 @@ public class DataUtils { values.put(NoteColumns.LOCAL_MODIFIED, 1); resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); } - + //批量移动至文件夹 public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { if (ids == null) { @@ -111,10 +120,16 @@ public class DataUtils { return false; } - /** - * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} + /* + 获取除系统文件夹外的所有文件夹计数 {@link Notes#TYPE_SYSTEM}} */ public static int getUserFolderCount(ContentResolver resolver) { + /* + ContentResolver直译为内容解析器, + Android中程序间数据的共享是通过Provider/Resolver进行的。 + 提供数据(内容)的就叫Provider,Resovler提供接口对这个内容进行解读。 + 在这里,系统提供了联系人的Provider,那么我们就需要构建一个Resolver来读取联系人的内容。 + */ Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, new String[] { "COUNT(*)" }, NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", @@ -152,13 +167,14 @@ public class DataUtils { } return exist; } - + //查询该ID在数据库中是否存在 public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, null, null, null); boolean exist = false; if (cursor != null) { + //行数大于0 if (cursor.getCount() > 0) { exist = true; } @@ -166,7 +182,7 @@ public class DataUtils { } return exist; } - + //查询dataID在数据库中是否存在 public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null, null, null, null); @@ -180,7 +196,7 @@ public class DataUtils { } return exist; } - + //检查可见文件名是否存在 public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + @@ -196,7 +212,7 @@ public class DataUtils { } return exist; } - + //获取文件夹便签的widget返回的是哈希表 public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, @@ -207,6 +223,7 @@ public class DataUtils { HashSet set = null; if (c != null) { if (c.moveToFirst()) { + //定义AppWidgetAttribute类型的哈希表 set = new HashSet(); do { try { @@ -217,13 +234,14 @@ public class DataUtils { } catch (IndexOutOfBoundsException e) { Log.e(TAG, e.toString()); } + //这里循环是找到cursor不为空的时候 } while (c.moveToNext()); } c.close(); } return set; } - + //通过便签ID获得电话号码 public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.PHONE_NUMBER }, @@ -232,8 +250,10 @@ public class DataUtils { null); if (cursor != null && cursor.moveToFirst()) { + //获得第一行数据 try { return cursor.getString(0); + //捕捉超界异常 } catch (IndexOutOfBoundsException e) { Log.e(TAG, "Get call number fails " + e.toString()); } finally { @@ -242,7 +262,7 @@ public class DataUtils { } return ""; } - + //通过电话号码和拨打日期获得便签ID public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.NOTE_ID }, @@ -255,6 +275,7 @@ public class DataUtils { if (cursor.moveToFirst()) { try { return cursor.getLong(0); + //记录获取失败 } catch (IndexOutOfBoundsException e) { Log.e(TAG, "Get call note id fails " + e.toString()); } @@ -263,7 +284,7 @@ public class DataUtils { } return 0; } - + //通过ID获取NoteColumns.SNIPPET public static String getSnippetById(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, new String [] { NoteColumns.SNIPPET }, @@ -274,14 +295,16 @@ public class DataUtils { if (cursor != null) { String snippet = ""; if (cursor.moveToFirst()) { + //获取第一列字符串 snippet = cursor.getString(0); } cursor.close(); return snippet; } + //抛出无法利用id找到便签的异常 throw new IllegalArgumentException("Note is not found with id: " + noteId); } - + //获得格式化的snippet(主要是去除无用字符) public static String getFormattedSnippet(String snippet) { if (snippet != null) { snippet = snippet.trim(); diff --git a/src/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java b/src/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java index 666b729..3ea4d96 100644 --- a/src/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java +++ b/src/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java @@ -15,35 +15,37 @@ */ package net.micode.notes.tool; - +/* + 字符工具类 + */ public class GTaskStringUtils { - + //日程同步动作ID public final static String GTASK_JSON_ACTION_ID = "action_id"; - + //日程同步动作列表 public final static String GTASK_JSON_ACTION_LIST = "action_list"; - + //日程同步动作类型 public final static String GTASK_JSON_ACTION_TYPE = "action_type"; - + //日程同步动作类型构造 public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; - + //返回日程同步动作类型 public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; - + //日程同步动作类型移动 public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; - + //日程同步动作类型更新 public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; - + //日程同步创建id public final static String GTASK_JSON_CREATOR_ID = "creator_id"; - + //日程同步子体 public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; - + //日程同步用户版本 public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; - + //日程同步已完成 public final static String GTASK_JSON_COMPLETED = "completed"; - + //日程同步当前列表ID public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; - + //日程同步默认列表id public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; - + //日程同步删除 public final static String GTASK_JSON_DELETED = "deleted"; public final static String GTASK_JSON_DEST_LIST = "dest_list"; diff --git a/src/app/src/main/java/net/micode/notes/tool/ResourceParser.java b/src/app/src/main/java/net/micode/notes/tool/ResourceParser.java index 1ad3ad6..b15dccb 100644 --- a/src/app/src/main/java/net/micode/notes/tool/ResourceParser.java +++ b/src/app/src/main/java/net/micode/notes/tool/ResourceParser.java @@ -21,25 +21,28 @@ import android.preference.PreferenceManager; import net.micode.notes.R; import net.micode.notes.ui.NotesPreferenceActivity; - +/* +界面元素解析工具类 + */ public class ResourceParser { - + //定义各种颜色 public static final int YELLOW = 0; public static final int BLUE = 1; public static final int WHITE = 2; public static final int GREEN = 3; public static final int RED = 4; - + //背景默认色 public static final int BG_DEFAULT_COLOR = YELLOW; public static final int TEXT_SMALL = 0; public static final int TEXT_MEDIUM = 1; public static final int TEXT_LARGE = 2; public static final int TEXT_SUPER = 3; - + //背景默认字体大小 public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; - + //便签背景源 public static class NoteBgResources { + //颜色 private final static int [] BG_EDIT_RESOURCES = new int [] { R.drawable.edit_yellow, R.drawable.edit_blue, @@ -47,7 +50,7 @@ public class ResourceParser { R.drawable.edit_green, R.drawable.edit_red }; - + //标题色 private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] { R.drawable.edit_title_yellow, R.drawable.edit_title_blue, @@ -55,16 +58,16 @@ public class ResourceParser { R.drawable.edit_title_green, R.drawable.edit_title_red }; - + //获取便签背景源第ID项内容 public static int getNoteBgResource(int id) { return BG_EDIT_RESOURCES[id]; } - + //获取便签标题背景源第ID项内容 public static int getNoteTitleBgResource(int id) { return BG_EDIT_TITLE_RESOURCES[id]; } } - + //获得默认背景颜色ID public static int getDefaultBgId(Context context) { if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { @@ -73,7 +76,7 @@ public class ResourceParser { return BG_DEFAULT_COLOR; } } - + //便签不同区域的定色 public static class NoteItemBgResources { private final static int [] BG_FIRST_RESOURCES = new int [] { R.drawable.list_yellow_up, @@ -106,29 +109,30 @@ public class ResourceParser { R.drawable.list_green_single, R.drawable.list_red_single }; - + //获得便签背景首源 public static int getNoteBgFirstRes(int id) { return BG_FIRST_RESOURCES[id]; } - + //获得便签背景尾源 public static int getNoteBgLastRes(int id) { return BG_LAST_RESOURCES[id]; } - + //获得单个便签源 public static int getNoteBgSingleRes(int id) { return BG_SINGLE_RESOURCES[id]; } - + //获得便签正常源 public static int getNoteBgNormalRes(int id) { return BG_NORMAL_RESOURCES[id]; } - + //获得文件背景源 public static int getFolderBgRes() { return R.drawable.list_folder; } } public static class WidgetBgResources { + //2x大小背景 private final static int [] BG_2X_RESOURCES = new int [] { R.drawable.widget_2x_yellow, R.drawable.widget_2x_blue, @@ -140,7 +144,7 @@ public class ResourceParser { public static int getWidget2xBgResource(int id) { return BG_2X_RESOURCES[id]; } - + //4X大小背景 private final static int [] BG_4X_RESOURCES = new int [] { R.drawable.widget_4x_yellow, R.drawable.widget_4x_blue, @@ -153,7 +157,7 @@ public class ResourceParser { return BG_4X_RESOURCES[id]; } } - + //文本显示大小 public static class TextAppearanceResources { private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { R.style.TextAppearanceNormal, @@ -168,6 +172,7 @@ public class ResourceParser { * The id may larger than the length of resources, in this case, * return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE} */ + //如果索引超界 if (id >= TEXTAPPEARANCE_RESOURCES.length) { return BG_DEFAULT_FONT_SIZE; }