diff --git a/5fa24a884b b/5fa24a884b new file mode 100644 index 0000000..e69de29 diff --git a/Notes-master2/src/net/micode/notes/data/Contact.java b/Notes-master2/src/net/micode/notes/data/Contact.java index d97ac5d..aa79c49 100644 --- a/Notes-master2/src/net/micode/notes/data/Contact.java +++ b/Notes-master2/src/net/micode/notes/data/Contact.java @@ -25,49 +25,38 @@ import android.util.Log; import java.util.HashMap; -public class Contact { - private static HashMap sContactCache; - private static final String TAG = "Contact"; - - private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER - + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" - + " AND " + Data.RAW_CONTACT_ID + " IN " - + "(SELECT raw_contact_id " - + " FROM phone_lookup" - + " WHERE min_match = '+')"; - - public static String getContact(Context context, String phoneNumber) { - if(sContactCache == null) { - sContactCache = new HashMap(); - } - - if(sContactCache.containsKey(phoneNumber)) { - return sContactCache.get(phoneNumber); - } +public static String getContact(Context context, String phoneNumber) { + if(sContactCache == null) { //如果缓存为空则创建一个新的 HashMap + sContactCache = new HashMap(); + } - String selection = CALLER_ID_SELECTION.replace("+", - PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); - Cursor cursor = context.getContentResolver().query( - Data.CONTENT_URI, - new String [] { Phone.DISPLAY_NAME }, - selection, - new String[] { phoneNumber }, - null); + if(sContactCache.containsKey(phoneNumber)) { //如果缓存中已存在对应的电话号码则返回其对应的联系人姓名 + return sContactCache.get(phoneNumber); + } - if (cursor != null && cursor.moveToFirst()) { - try { - String name = cursor.getString(0); - sContactCache.put(phoneNumber, name); - return name; - } catch (IndexOutOfBoundsException e) { - Log.e(TAG, " Cursor get string error " + e.toString()); - return null; - } finally { - cursor.close(); - } - } else { - Log.d(TAG, "No contact matched with number:" + phoneNumber); - return null; + //否则根据电话号码查询联系人姓名 + String selection = CALLER_ID_SELECTION.replace("+", + PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); //将 "+" 替换为待查询电话号码的最小完全匹配 + Cursor cursor = context.getContentResolver().query( + Data.CONTENT_URI, //uri为ContentProvider提供的数据查询接口的位置(Data.CONTENT_URI) + new String [] { Phone.DISPLAY_NAME }, //需要查询的数据项,查询联系人姓名 + selection, //查询条件 + new String[] { phoneNumber }, //占位符对应的实际参数 + null); //排序方式为空 + + if (cursor != null && cursor.moveToFirst()) { //如果查询结果非空,获取查询结果中的第一条记录 + try { + String name = cursor.getString(0); //获取联系人姓名 + sContactCache.put(phoneNumber, name); //将电话号码及其联系人姓名加入 HashMap 缓存中 + return name; //返回联系人姓名 + } catch (IndexOutOfBoundsException e) { //处理异常 + Log.e(TAG, " Cursor get string error " + e.toString()); + return null; //返回null + } finally { + cursor.close(); //释放cursor } + } else { //如果返回为空,打印调试信息并返回null + Log.d(TAG, "No contact matched with number:" + phoneNumber); + return null; //返回null } } diff --git a/Notes-master2/src/net/micode/notes/data/Notes.java b/Notes-master2/src/net/micode/notes/data/Notes.java index f240604..e5bebb8 100644 --- a/Notes-master2/src/net/micode/notes/data/Notes.java +++ b/Notes-master2/src/net/micode/notes/data/Notes.java @@ -18,262 +18,175 @@ package net.micode.notes.data; import android.net.Uri; public class Notes { - public static final String AUTHORITY = "micode_notes"; - public static final String TAG = "Notes"; - public static final int TYPE_NOTE = 0; - public static final int TYPE_FOLDER = 1; - public static final int TYPE_SYSTEM = 2; - - /** - * Following IDs are system folders' identifiers - * {@link Notes#ID_ROOT_FOLDER } is default folder - * {@link Notes#ID_TEMPARAY_FOLDER } is for notes belonging no folder - * {@link Notes#ID_CALL_RECORD_FOLDER} is to store call records - */ - public static final int ID_ROOT_FOLDER = 0; - public static final int ID_TEMPARAY_FOLDER = -1; - public static final int ID_CALL_RECORD_FOLDER = -2; - public static final int ID_TRASH_FOLER = -3; - - public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date"; - public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id"; - public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id"; - public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type"; - public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id"; - public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date"; - - public static final int TYPE_WIDGET_INVALIDE = -1; - public static final int TYPE_WIDGET_2X = 0; - public static final int TYPE_WIDGET_4X = 1; - - public static class DataConstants { - public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; - public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; - } - - /** - * Uri to query all notes and folders - */ - public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); - - /** - * Uri to query data - */ - public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); - - public interface NoteColumns { - /** - * The unique ID for a row - *

Type: INTEGER (long)

- */ - public static final String ID = "_id"; - - /** - * The parent's id for note or folder - *

Type: INTEGER (long)

- */ - public static final String PARENT_ID = "parent_id"; - - /** - * Created data for note or folder - *

Type: INTEGER (long)

- */ - public static final String CREATED_DATE = "created_date"; - - /** - * Latest modified date - *

Type: INTEGER (long)

- */ - public static final String MODIFIED_DATE = "modified_date"; - - - /** - * Alert date - *

Type: INTEGER (long)

- */ - public static final String ALERTED_DATE = "alert_date"; - - /** - * Folder's name or text content of note - *

Type: TEXT

- */ - public static final String SNIPPET = "snippet"; - - /** - * Note's widget id - *

Type: INTEGER (long)

- */ - public static final String WIDGET_ID = "widget_id"; - - /** - * Note's widget type - *

Type: INTEGER (long)

- */ - public static final String WIDGET_TYPE = "widget_type"; - - /** - * Note's background color's id - *

Type: INTEGER (long)

- */ - public static final String BG_COLOR_ID = "bg_color_id"; - - /** - * For text note, it doesn't has attachment, for multi-media - * note, it has at least one attachment - *

Type: INTEGER

- */ - public static final String HAS_ATTACHMENT = "has_attachment"; - - /** - * Folder's count of notes - *

Type: INTEGER (long)

- */ - public static final String NOTES_COUNT = "notes_count"; - - /** - * The file type: folder or note - *

Type: INTEGER

- */ - public static final String TYPE = "type"; - - /** - * The last sync id - *

Type: INTEGER (long)

- */ - public static final String SYNC_ID = "sync_id"; - - /** - * Sign to indicate local modified or not - *

Type: INTEGER

- */ - public static final String LOCAL_MODIFIED = "local_modified"; - - /** - * Original parent id before moving into temporary folder - *

Type : INTEGER

- */ - public static final String ORIGIN_PARENT_ID = "origin_parent_id"; - - /** - * The gtask id - *

Type : TEXT

- */ - public static final String GTASK_ID = "gtask_id"; - - /** - * The version code - *

Type : INTEGER (long)

- */ - public static final String VERSION = "version"; - } - - public interface DataColumns { - /** - * The unique ID for a row - *

Type: INTEGER (long)

- */ - public static final String ID = "_id"; - - /** - * The MIME type of the item represented by this row. - *

Type: Text

- */ - public static final String MIME_TYPE = "mime_type"; - - /** - * The reference id to note that this data belongs to - *

Type: INTEGER (long)

- */ - public static final String NOTE_ID = "note_id"; - - /** - * Created data for note or folder - *

Type: INTEGER (long)

- */ - public static final String CREATED_DATE = "created_date"; - - /** - * Latest modified date - *

Type: INTEGER (long)

- */ - public static final String MODIFIED_DATE = "modified_date"; - - /** - * Data's content - *

Type: TEXT

- */ - public static final String CONTENT = "content"; - - - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * integer data type - *

Type: INTEGER

- */ - public static final String DATA1 = "data1"; - - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * integer data type - *

Type: INTEGER

- */ - public static final String DATA2 = "data2"; - - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * TEXT data type - *

Type: TEXT

- */ - public static final String DATA3 = "data3"; - - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * TEXT data type - *

Type: TEXT

- */ - public static final String DATA4 = "data4"; - - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * TEXT data type - *

Type: TEXT

- */ - public static final String DATA5 = "data5"; - } - - public static final class TextNote implements DataColumns { - /** - * Mode to indicate the text in check list mode or not - *

Type: Integer 1:check list mode 0: normal mode

- */ - public static final String MODE = DATA1; - - public static final int MODE_CHECK_LIST = 1; - - public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note"; - - public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note"; - - public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); - } - - public static final class CallNote implements DataColumns { - /** - * Call date for this record - *

Type: INTEGER (long)

- */ - public static final String CALL_DATE = DATA1; - - /** - * Phone number for this record - *

Type: TEXT

- */ - public static final String PHONE_NUMBER = DATA3; - - public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note"; - - public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note"; - - public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note"); - } + // 笔记应用的权限 +public static final String AUTHORITY = "micode_notes"; + +// 日志TAG +public static final String TAG = "Notes"; + +// 笔记和文件夹的类型 +public static final int TYPE_NOTE = 0; +public static final int TYPE_FOLDER = 1; +public static final int TYPE_SYSTEM = 2; + +/** + * 下面的ID都是系统文件夹的标识符 + * {@link Notes#ID_ROOT_FOLDER} 是默认文件夹, + * {@link Notes#ID_TEMPARAY_FOLDER} 是属于无文件夹的笔记, + * {@link Notes#ID_CALL_RECORD_FOLDER} 是用于存储通话记录。 + */ +public static final int ID_ROOT_FOLDER = 0; +public static final int ID_TEMPARAY_FOLDER = -1; +public static final int ID_CALL_RECORD_FOLDER = -2; +public static final int ID_TRASH_FOLER = -3; + +// intent附加的警报日期名字 +public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date"; + +// intent附加的背景颜色名字 +public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id"; + +// intent附加的小部件标识符名字 +public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id"; + +// intent附加的小部件类型名字 +public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type"; + +// intent附加的文件夹标识符名字 +public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id"; + +// intent 附加的通话日期名字 +public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date"; + +// 小部件类型的无效值 +public static final int TYPE_WIDGET_INVALIDE = -1; + +// 小部件类型(2X2 or 4X4) +public static final int TYPE_WIDGET_2X = 0; +public static final int TYPE_WIDGET_4X = 1; + +public static class DataConstants { + public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; + public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; +} + +// 通过这个URI查询所有笔记和文件夹 +public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); + +// 通过这个URI查询数据 +public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); + +public interface NoteColumns { + + // 笔记或文件夹的唯一ID + public static final String ID = "_id"; + + // 笔记或文件夹的父ID + public static final String PARENT_ID = "parent_id"; + + // 笔记或文件夹创建的日期 + public static final String CREATED_DATE = "created_date"; + + // 最近修改日期 + public static final String MODIFIED_DATE = "modified_date"; + + // 提醒日期 + public static final String ALERTED_DATE = "alert_date"; + + // 文件夹的名字或者笔记的内容 + public static final String SNIPPET = "snippet"; + + // 笔记的小部件ID + public static final String WIDGET_ID = "widget_id"; + + // 笔记的小部件类型 + public static final String WIDGET_TYPE = "widget_type"; + + // 笔记的背景颜色ID + public static final String BG_COLOR_ID = "bg_color_id"; + + // 如果是文本笔记没有附件的(如图片、音频),则为0,否则为1 + public static final String HAS_ATTACHMENT = "has_attachment"; + + // 文件夹包含的笔记数量 + public static final String NOTES_COUNT = "notes_count"; + + // 文件类型:文件夹或笔记 + public static final String TYPE = "type"; + + // 最后一个同步的ID + public static final String SYNC_ID = "sync_id"; + + // 注明本地是否被修改过 + public static final String LOCAL_MODIFIED = "local_modified"; + + // 移到临时文件夹之前的原始父ID + public static final String ORIGIN_PARENT_ID = "origin_parent_id"; + + // gtask的ID + public static final String GTASK_ID = "gtask_id"; + + // 版本号 + public static final String VERSION = "version"; +} + +public interface DataColumns { + + // 数据表唯一ID + public static final String ID = "_id"; + + // 描述该行数据的MIME类型 + public static final String MIME_TYPE = "mime_type"; + + // 笔记所属的引用ID + public static final String NOTE_ID = "note_id"; + + // 笔记或文件夹创建的日期 + public static final String CREATED_DATE = "created_date"; + + // 最近修改日期 + public static final String MODIFIED_DATE = "modified_date"; + + // 数据的内容 + public static final String CONTENT = "content"; + + // 通用的数据列,由{@link#MIMETYPE}具体说明,用于整数数据类型 + public static final String DATA1 = "data1"; + + // 通用的数据列,由{@link#MIMETYPE}具体说明,用于整数数据类型 + public static final String DATA2 = "data2"; + + // 通用的数据列,由{@link#MIMETYPE}具体说明,用于TEXT数据类型 + public static final String DATA3 = "data3"; + + // 通用的数据列,由{@link#MIMETYPE}具体说明,用于TEXT数据类型 + public static final String DATA4 = "data4"; + + // 通用的数据列,由{@link#MIMETYPE}具体说明,用于TEXT数据类型 + public static final String DATA5 = "data5"; +} + +public static final class TextNote implements DataColumns { + + // 是否为检查清单文本模式 + public static final String MODE = DATA1; + public static final int MODE_CHECK_LIST = 1; + + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note"; + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note"; + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); +} + +public static final class CallNote implements DataColumns { + + // 通话记录日期 + public static final String CALL_DATE = DATA1; + + // 电话号码 + public static final String PHONE_NUMBER = DATA3; + + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note"; + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note"; + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note"); +} } diff --git a/Notes-master2/src/net/micode/notes/gtask/data/MetaData.java b/Notes-master2/src/net/micode/notes/gtask/data/MetaData.java index 3a2050b..8b51954 100644 --- a/Notes-master2/src/net/micode/notes/gtask/data/MetaData.java +++ b/Notes-master2/src/net/micode/notes/gtask/data/MetaData.java @@ -1,19 +1,3 @@ -/* - * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package net.micode.notes.gtask.data; import android.database.Cursor; @@ -27,36 +11,40 @@ import org.json.JSONObject; public class MetaData extends Task { private final static String TAG = MetaData.class.getSimpleName(); - + // 类的简写名称存入字符串TAG中,通过调用getSimpleName ()函数实现 private String mRelatedGid = null; - + // 设置数据,即生成元数据库 public void setMeta(String gid, JSONObject metaInfo) { - try { + try { //将这对键值放入metaInfo这个jsonobject对象中 metaInfo.put(GTaskStringUtils.META_HEAD_GTASK_ID, gid); - } catch (JSONException e) { + } catch (JSONException e) { //输出错误信息 Log.e(TAG, "failed to put related gid"); } setNotes(metaInfo.toString()); setName(GTaskStringUtils.META_NOTE_NAME); + //调用Task类中的setNotes ()和setName ()函数 } - public String getRelatedGid() { + //getRelatedGid函数定义 return mRelatedGid; } @Override public boolean isWorthSaving() { + //isWorthSaving函数定义 return getNotes() != null; } @Override public void setContentByRemoteJSON(JSONObject js) { + //定义setContentByRemoteJSON函数 super.setContentByRemoteJSON(js); - if (getNotes() != null) { + if (getNotes() != null) {//判断当前数据是否为空,若为空则返回真即值得保存 try { JSONObject metaInfo = new JSONObject(getNotes().trim()); mRelatedGid = metaInfo.getString(GTaskStringUtils.META_HEAD_GTASK_ID); } catch (JSONException e) { + //输出警告信息 Log.w(TAG, "failed to get related gid"); mRelatedGid = null; } @@ -65,17 +53,19 @@ public class MetaData extends Task { @Override public void setContentByLocalJSON(JSONObject js) { - // this function should not be called - throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called"); + //使用本地json数据对象设置元数据内容,一般不会用到,若用到,则抛出异常 + throw new IllegalAccessError("MetaData:setContentByLocalJSON should not be called");//传递非法参数异常 } @Override public JSONObject getLocalJSONFromContent() { + //从元数据内容中获取本地json对象,一般不会用到,若用到,则抛出异常 throw new IllegalAccessError("MetaData:getLocalJSONFromContent should not be called"); } @Override public int getSyncAction(Cursor c) { + //获取同步动作状态,一般不会用到,若用到,则抛出异常 throw new IllegalAccessError("MetaData:getSyncAction should not be called"); } diff --git a/Notes-master2/src/net/micode/notes/gtask/data/Node.java b/Notes-master2/src/net/micode/notes/gtask/data/Node.java index 63950e0..efa8801 100644 --- a/Notes-master2/src/net/micode/notes/gtask/data/Node.java +++ b/Notes-master2/src/net/micode/notes/gtask/data/Node.java @@ -1,51 +1,35 @@ -/* - * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package net.micode.notes.gtask.data; import android.database.Cursor; import org.json.JSONObject; -public abstract class Node { - public static final int SYNC_ACTION_NONE = 0; +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_REMOTE = 1;// 在远程云端增加内容 - public static final int SYNC_ACTION_ADD_LOCAL = 2; + 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_REMOTE = 3;// 在远程云端删除内容 - public static final int SYNC_ACTION_DEL_LOCAL = 4; + 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_REMOTE = 5;// 将本地内容更新到远程云端 - public static final int SYNC_ACTION_UPDATE_LOCAL = 6; + public static final int SYNC_ACTION_UPDATE_LOCAL = 6;// 将远程云端内容更新到本地 - public static final int SYNC_ACTION_UPDATE_CONFLICT = 7; + public static final int SYNC_ACTION_UPDATE_CONFLICT = 7;// 同步出现冲突 - public static final int SYNC_ACTION_ERROR = 8; + public static final int SYNC_ACTION_ERROR = 8;// 同步出现错误 private String mGid; private String mName; - private long mLastModified; + private long mLastModified;//记录最后一次修改时间 - private boolean mDeleted; + private boolean mDeleted;//表征是否被删除 public Node() { mGid = null; diff --git a/Notes-master2/src/net/micode/notes/gtask/data/SqlData.java b/Notes-master2/src/net/micode/notes/gtask/data/SqlData.java index d3ec3be..f9fb6b5 100644 --- a/Notes-master2/src/net/micode/notes/gtask/data/SqlData.java +++ b/Notes-master2/src/net/micode/notes/gtask/data/SqlData.java @@ -1,19 +1,3 @@ -/* - * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package net.micode.notes.gtask.data; import android.content.ContentResolver; @@ -37,14 +21,13 @@ import org.json.JSONObject; public class SqlData { private static final String TAG = SqlData.class.getSimpleName(); - - private static final int INVALID_ID = -99999; - + //得到类的简写名称存入字符串TAG中,通过调用getSimpleName ()函数实现 + private static final int INVALID_ID = -99999;//将mDataId初始值置为-99999 public static final String[] PROJECTION_DATA = new String[] { DataColumns.ID, DataColumns.MIME_TYPE, DataColumns.CONTENT, DataColumns.DATA1, DataColumns.DATA3 }; - + //以下五个变量作为sql表中5列的编号 public static final int DATA_ID_COLUMN = 0; public static final int DATA_MIME_TYPE_COLUMN = 1; @@ -54,7 +37,8 @@ public class SqlData { public static final int DATA_CONTENT_DATA_1_COLUMN = 3; public static final int DATA_CONTENT_DATA_3_COLUMN = 4; - + //定义以下八个变量 + //构造函数,用于初始化数据 private ContentResolver mContentResolver; private boolean mIsCreate; @@ -71,8 +55,8 @@ public class SqlData { private ContentValues mDiffDataValues; - public SqlData(Context context) { - mContentResolver = context.getContentResolver(); + public SqlData(Context context) { //判断是否直接用Content生成,是为true,否则为false + mContentResolver = context.getContentResolver();//mContentResolver用于获取ContentProvider提供的数据 mIsCreate = true; mDataId = INVALID_ID; mDataMimeType = DataConstants.NOTE; @@ -82,7 +66,7 @@ public class SqlData { mDiffDataValues = new ContentValues(); } - public SqlData(Context context, Cursor c) { + public SqlData(Context context, Cursor c) { //判断是否直接用Content生成,是为true,否则为false mContentResolver = context.getContentResolver(); mIsCreate = false; loadFromCursor(c); @@ -98,8 +82,9 @@ public class SqlData { } public void setContent(JSONObject js) throws JSONException { + //如果传入的JSONObject对象中有DataColumns.ID这一项,则设置,否则设为INVALID_ID long dataId = js.has(DataColumns.ID) ? js.getLong(DataColumns.ID) : INVALID_ID; - if (mIsCreate || mDataId != dataId) { + if (mIsCreate || mDataId != dataId) { mDiffDataValues.put(DataColumns.ID, dataId); } mDataId = dataId; @@ -130,7 +115,7 @@ public class SqlData { mDataContentData3 = dataContentData3; } - public JSONObject getContent() throws JSONException { + public JSONObject getContent() throws JSONException {//创建JSONObject对象。并将相关数据放入其中,并返回 if (mIsCreate) { Log.e(TAG, "it seems that we haven't created this in database yet"); return null; @@ -144,7 +129,7 @@ public class SqlData { return js; } - public void commit(long noteId, boolean validateVersion, long version) { + public void commit(long noteId, boolean validateVersion, long version) {//commit函数用于把当前造作所做的修改保存到数据库 if (mIsCreate) { if (mDataId == INVALID_ID && mDiffDataValues.containsKey(DataColumns.ID)) { @@ -183,7 +168,7 @@ public class SqlData { mIsCreate = false; } - public long getId() { + public long getId() {//获取当前id return mDataId; } } diff --git a/Notes-master2/src/net/micode/notes/gtask/data/SqlNote.java b/Notes-master2/src/net/micode/notes/gtask/data/SqlNote.java index 79a4095..b1c3f07 100644 --- a/Notes-master2/src/net/micode/notes/gtask/data/SqlNote.java +++ b/Notes-master2/src/net/micode/notes/gtask/data/SqlNote.java @@ -1,19 +1,3 @@ -/* - * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package net.micode.notes.gtask.data; import android.appwidget.AppWidgetManager; @@ -41,8 +25,8 @@ import java.util.ArrayList; public class SqlNote { private static final String TAG = SqlNote.class.getSimpleName(); - private static final int INVALID_ID = -99999; - + private static final int INVALID_ID = -99999;//将其初始值-99999 + // 集合了interface NoteColumns中所有SF常量(17个) public static final String[] PROJECTION_NOTE = new String[] { NoteColumns.ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID, NoteColumns.CREATED_DATE, NoteColumns.HAS_ATTACHMENT, NoteColumns.MODIFIED_DATE, @@ -51,7 +35,7 @@ public class SqlNote { NoteColumns.LOCAL_MODIFIED, NoteColumns.ORIGIN_PARENT_ID, NoteColumns.GTASK_ID, NoteColumns.VERSION }; - + //以下设置17个列的编号 public static final int ID_COLUMN = 0; public static final int ALERTED_DATE_COLUMN = 1; @@ -85,7 +69,7 @@ public class SqlNote { public static final int GTASK_ID_COLUMN = 15; public static final int VERSION_COLUMN = 16; - + //定义了17个内部的变量,其中12个可以由content中获得,5个需要初始化为0或者new private Context mContext; private ContentResolver mContentResolver; @@ -122,17 +106,17 @@ public class SqlNote { private ArrayList mDataList; - public SqlNote(Context context) { - mContext = context; + public SqlNote(Context context) {//构造函数只有context,对所有的变量进行初始化 + mContext = context;//变量赋值 mContentResolver = context.getContentResolver(); - mIsCreate = true; + mIsCreate = true;//初始化 mId = INVALID_ID; - mAlertDate = 0; + mAlertDate = 0;//初始化 mBgColorId = ResourceParser.getDefaultBgId(context); - mCreatedDate = System.currentTimeMillis(); + mCreatedDate = System.currentTimeMillis();//调用系统函数获得创建时间 mHasAttachment = 0; - mModifiedDate = System.currentTimeMillis(); - mParentId = 0; + mModifiedDate = System.currentTimeMillis();//最后一次修改时间初始化为创建时间 + mParentId = 0;//初始化 mSnippet = ""; mType = Notes.TYPE_NOTE; mWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; @@ -143,7 +127,7 @@ public class SqlNote { mDataList = new ArrayList(); } - public SqlNote(Context context, Cursor c) { + public SqlNote(Context context, Cursor c) {//构造函数有context和一个数据库的cursor,多数变量通过cursor指向的一条记录直接进行初始化 mContext = context; mContentResolver = context.getContentResolver(); mIsCreate = false; @@ -153,8 +137,8 @@ public class SqlNote { loadDataContent(); mDiffNoteValues = new ContentValues(); } - - public SqlNote(Context context, long id) { + //SqlNote(Context context, long id)与SqlNote(Context context, long id)的实现方式基本相同 + public SqlNote(Context context, long id) {//构造函数,对获取的id进行初始化 mContext = context; mContentResolver = context.getContentResolver(); mIsCreate = false; @@ -166,7 +150,7 @@ public class SqlNote { } - private void loadFromCursor(long id) { + private void loadFromCursor(long id) {//通过id获得对应的ContentResolver中的cursor Cursor c = null; try { c = mContentResolver.query(Notes.CONTENT_NOTE_URI, PROJECTION_NOTE, "(_id=?)", diff --git a/Notes-master2/src/net/micode/notes/model/Note.java b/Notes-master2/src/net/micode/notes/model/Note.java index 6706cf6..b28b5dd 100644 --- a/Notes-master2/src/net/micode/notes/model/Note.java +++ b/Notes-master2/src/net/micode/notes/model/Note.java @@ -15,28 +15,28 @@ */ package net.micode.notes.model; -import android.content.ContentProviderOperation; -import android.content.ContentProviderResult; -import android.content.ContentUris; -import android.content.ContentValues; -import android.content.Context; -import android.content.OperationApplicationException; -import android.net.Uri; -import android.os.RemoteException; -import android.util.Log; +import android.content.ContentProviderOperation;//批量的更新、插入、删除数据。 +import android.content.ContentProviderResult;//操作的结果 +import android.content.ContentUris;//添加和获取Uri后面的ID +import android.content.ContentValues;//一种用来存储基本数据类型数据的存储机制 +import android.content.Context;//该类用于弄清调用者的实例 +import android.content.OperationApplicationException;//操作应用程序容错 +import android.net.Uri;//待操作的数据 +import android.os.RemoteException;//远程容错 +import android.util.Log;//输出日志,出错、警告等 -import net.micode.notes.data.Notes; -import net.micode.notes.data.Notes.CallNote; -import net.micode.notes.data.Notes.DataColumns; -import net.micode.notes.data.Notes.NoteColumns; -import net.micode.notes.data.Notes.TextNote; +import net.micode.notes.data.Notes;//导入Note +import net.micode.notes.data.Notes.CallNote;//导入Notes.CallNote +import net.micode.notes.data.Notes.DataColumns;//导入Notes.DataColumns +import net.micode.notes.data.Notes.NoteColumns;//导入Notes.NoteColumns +import net.micode.notes.data.Notes.TextNote;//导入Notes.TextNote -import java.util.ArrayList; +import java.util.ArrayList;//输入Scanner public class Note { - private ContentValues mNoteDiffValues; - private NoteData mNoteData; + private ContentValues mNoteDiffValues;//属性 + private NoteData mNoteData;//属性 private static final String TAG = "Note"; /** * Create a new note id for adding a new note to databases @@ -49,8 +49,10 @@ public class Note { values.put(NoteColumns.MODIFIED_DATE, createdTime); values.put(NoteColumns.TYPE, Notes.TYPE_NOTE); values.put(NoteColumns.LOCAL_MODIFIED, 1); - values.put(NoteColumns.PARENT_ID, folderId); + values.put(NoteColumns.PARENT_ID, folderId);//将数据写入数据库表格 Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); + //ContentResolver()主要是实现外部应用对ContentProvider中的数据 + //进行添加、删除、修改和查询操作 long noteId = 0; try { @@ -58,49 +60,49 @@ public class Note { } catch (NumberFormatException e) { Log.e(TAG, "Get note id error :" + e.toString()); noteId = 0; - } + }//try-catch异常处理 if (noteId == -1) { throw new IllegalStateException("Wrong note id:" + noteId); } return noteId; } - public Note() { + public Note() {//定义两个变量用来存储便签的数据,一个是存储便签属性、一个是存储便签内容 mNoteDiffValues = new ContentValues(); mNoteData = new NoteData(); } - public void setNoteValue(String key, String value) { + public void setNoteValue(String key, String value) {//设置数据库表格的标签属性数据 mNoteDiffValues.put(key, value); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); } - public void setTextData(String key, String value) { + public void setTextData(String key, String value) {//设置数据库表格的标签文本内容的数据 mNoteData.setTextData(key, value); } - public void setTextDataId(long id) { + public void setTextDataId(long id) {//设置文本数据的ID mNoteData.setTextDataId(id); } - public long getTextDataId() { + public long getTextDataId() {//得到文本数据的ID return mNoteData.mTextDataId; } - public void setCallDataId(long id) { + public void setCallDataId(long id) {//设置电话号码数据的ID mNoteData.setCallDataId(id); } - public void setCallData(String key, String value) { + public void setCallData(String key, String value) {//得到电话号码数据的ID mNoteData.setCallData(key, value); } - public boolean isLocalModified() { + public boolean isLocalModified() {//判断是否是本地修改 return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); } - public boolean syncNote(Context context, long noteId) { + public boolean syncNote(Context context, long noteId) {//判断数据是否同步 if (noteId <= 0) { throw new IllegalArgumentException("Wrong note id:" + noteId); } @@ -130,17 +132,17 @@ public class Note { return true; } - private class NoteData { + private class NoteData {//定义一个基本的便签内容的数据类,主要包含文本数据和电话号码数据 private long mTextDataId; - private ContentValues mTextDataValues; + private ContentValues mTextDataValues;//文本数据 private long mCallDataId; - private ContentValues mCallDataValues; + private ContentValues mCallDataValues;//电话号码数据 private static final String TAG = "NoteData"; - + //下面是上述几个函数的具体实现 public NoteData() { mTextDataValues = new ContentValues(); mCallDataValues = new ContentValues(); @@ -177,19 +179,19 @@ public class Note { mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); } - + //下面函数的作用是将新的数据通过Uri的操作存储到数据库 Uri pushIntoContentResolver(Context context, long noteId) { /** * Check for safety */ - if (noteId <= 0) { + if (noteId <= 0) {//判断数据是否合法 throw new IllegalArgumentException("Wrong note id:" + noteId); } ArrayList operationList = new ArrayList(); - ContentProviderOperation.Builder builder = null; + ContentProviderOperation.Builder builder = null;//数据库的操作列表 - if(mTextDataValues.size() > 0) { + if(mTextDataValues.size() > 0) {//把文本数据存入DataColumns mTextDataValues.put(DataColumns.NOTE_ID, noteId); if (mTextDataId == 0) { mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); @@ -211,7 +213,7 @@ public class Note { mTextDataValues.clear(); } - if(mCallDataValues.size() > 0) { + if(mCallDataValues.size() > 0) {//把电话号码数据存入DataColumns mCallDataValues.put(DataColumns.NOTE_ID, noteId); if (mCallDataId == 0) { mCallDataValues.put(DataColumns.MIME_TYPE, CallNote.CONTENT_ITEM_TYPE); @@ -233,7 +235,7 @@ public class Note { mCallDataValues.clear(); } - if (operationList.size() > 0) { + if (operationList.size() > 0) {//存储过程中的异常处理 try { ContentProviderResult[] results = context.getContentResolver().applyBatch( Notes.AUTHORITY, operationList); diff --git a/Notes-master2/src/net/micode/notes/model/WorkingNote.java b/Notes-master2/src/net/micode/notes/model/WorkingNote.java index be081e4..d39231a 100644 --- a/Notes-master2/src/net/micode/notes/model/WorkingNote.java +++ b/Notes-master2/src/net/micode/notes/model/WorkingNote.java @@ -62,7 +62,7 @@ public class WorkingNote { private NoteSettingChangedListener mNoteSettingStatusListener; - public static final String[] DATA_PROJECTION = new String[] { + public static final String[] DATA_PROJECTION = new String[] {// 声明 DATA_PROJECTION字符串数组 DataColumns.ID, DataColumns.CONTENT, DataColumns.MIME_TYPE, @@ -72,7 +72,7 @@ public class WorkingNote { DataColumns.DATA4, }; - public static final String[] NOTE_PROJECTION = new String[] { + public static final String[] NOTE_PROJECTION = new String[] { // 声明 NOTE_PROJECTION字符串数组 NoteColumns.PARENT_ID, NoteColumns.ALERTED_DATE, NoteColumns.BG_COLOR_ID, @@ -113,7 +113,7 @@ public class WorkingNote { mMode = 0; mWidgetType = Notes.TYPE_WIDGET_INVALIDE; } - + // WorkingNote的构造函数 // Existing note construct private WorkingNote(Context context, long noteId, long folderId) { mContext = context; diff --git a/测试.txt b/测试.txt new file mode 100644 index 0000000..7284ab4 --- /dev/null +++ b/测试.txt @@ -0,0 +1 @@ +aaaa \ No newline at end of file