diff --git a/app/src/main/java/net/micode/notes/data/Contact.java b/app/src/main/java/net/micode/notes/data/Contact.java index 9c6fa62..b132d43 100644 --- a/app/src/main/java/net/micode/notes/data/Contact.java +++ b/app/src/main/java/net/micode/notes/data/Contact.java @@ -36,15 +36,23 @@ public class Contact { + " FROM phone_lookup" + " WHERE min_match = '+')"; + + /** + * 判断是否存在映射关系 + * 若不存在,创建并存储在哈希表中 + */ public static String getContact(Context context, String phoneNumber) { if (sContactCache == null) { sContactCache = new HashMap(); } - + // 查找HashMap中是否已有phoneNumber信息,如果存在则返回phoneNumber信息 if (sContactCache.containsKey(phoneNumber)) { return sContactCache.get(phoneNumber); } + /** + * 查找数据库中phoneNumber的信息 + */ String selection = CALLER_ID_SELECTION.replace("+", PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); Cursor cursor = context.getContentResolver().query( @@ -54,12 +62,22 @@ public class Contact { new String[]{phoneNumber}, null); + /** + * 判定查询结果,若不为空,moveToFirst()返回第一条;否则返回空 + */ if (cursor != null && cursor.moveToFirst()) { + /** + * 找到相关情况时 + */ try { String name = cursor.getString(0); sContactCache.put(phoneNumber, name); return name; - } catch (IndexOutOfBoundsException e) { + } + /** + * 异常 + */ + catch (IndexOutOfBoundsException e) { Log.e(TAG, " Cursor get string error " + e.toString()); return null; } finally { diff --git a/app/src/main/java/net/micode/notes/data/Notes.java b/app/src/main/java/net/micode/notes/data/Notes.java index 87e60c3..af04c10 100644 --- a/app/src/main/java/net/micode/notes/data/Notes.java +++ b/app/src/main/java/net/micode/notes/data/Notes.java @@ -67,6 +67,9 @@ public class Notes { */ public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); + /** + * 定义了NoteColumns中的常量,将用于后面创建数据库的表头 + */ public interface NoteColumns { /** * The unique ID for a row @@ -172,6 +175,9 @@ public class Notes { */ public static final String VERSION = "version"; } + /** + * 以上这些常量主要是用于定义便签的属性 + */ public interface DataColumns { /** @@ -245,7 +251,9 @@ public class Notes { *

Type: TEXT

*/ public static final String DATA5 = "data5"; - } + }/** + * 以上DATA主要是定义存储便签内容数据 + */ public static final class TextNote implements DataColumns { /** @@ -261,7 +269,9 @@ public class Notes { 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 { /** @@ -281,5 +291,7 @@ public class Notes { 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/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java b/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java index c7d668e..ef7bb4e 100644 --- a/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java +++ b/app/src/main/java/net/micode/notes/data/NotesDatabaseHelper.java @@ -26,7 +26,9 @@ import net.micode.notes.data.Notes.DataColumns; import net.micode.notes.data.Notes.DataConstants; import net.micode.notes.data.Notes.NoteColumns; - +/** + *数据库操作,对一些note和文件进行数据库的操作。 + */ public class NotesDatabaseHelper extends SQLiteOpenHelper { private static final String DB_NAME = "note.db"; @@ -42,6 +44,9 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { private static NotesDatabaseHelper mInstance; + /** + * 创建数据库中需要存储的项目的名称 + */ private static final String CREATE_NOTE_TABLE_SQL = "CREATE TABLE " + TABLE.NOTE + "(" + NoteColumns.ID + " INTEGER PRIMARY KEY," + @@ -63,6 +68,9 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { NoteColumns.VERSION + " INTEGER NOT NULL DEFAULT 0" + ")"; + /** + * 功能同上,但存储的项目类型不同 + */ private static final String CREATE_DATA_TABLE_SQL = "CREATE TABLE " + TABLE.DATA + "(" + DataColumns.ID + " INTEGER PRIMARY KEY," + @@ -78,12 +86,16 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { DataColumns.DATA5 + " TEXT NOT NULL DEFAULT ''" + ")"; + /** + * 创建用来存储便签编号的数据表格 + */ private static final String CREATE_DATA_NOTE_ID_INDEX_SQL = "CREATE INDEX IF NOT EXISTS note_id_index ON " + TABLE.DATA + "(" + DataColumns.NOTE_ID + ");"; /** * Increase folder's note count when move note to the folder + * 在文件夹中移入一个Note之后需要更改的数据的表格。 */ private static final String NOTE_INCREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = "CREATE TRIGGER increase_folder_count_on_update " + @@ -96,6 +108,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { /** * Decrease folder's note count when move note from folder + * 在文件夹中移出一个Note之后需要更改的数据的表格。 */ private static final String NOTE_DECREASE_FOLDER_COUNT_ON_UPDATE_TRIGGER = "CREATE TRIGGER decrease_folder_count_on_update " + @@ -109,6 +122,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { /** * Increase folder's note count when insert new note to the folder + * 在文件夹中插入一个Note之后需要更改的数据的表格。 */ private static final String NOTE_INCREASE_FOLDER_COUNT_ON_INSERT_TRIGGER = "CREATE TRIGGER increase_folder_count_on_insert " + @@ -121,6 +135,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { /** * Decrease folder's note count when delete note from the folder + * 在文件夹中删除一个Note之后需要更改的数据的表格。 */ private static final String NOTE_DECREASE_FOLDER_COUNT_ON_DELETE_TRIGGER = "CREATE TRIGGER decrease_folder_count_on_delete " + @@ -134,6 +149,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { /** * Update note's content when insert data with type {@link DataConstants#NOTE} + * 在文件夹中对一个Note导入新的数据之后需要更改的数据的表格。 */ private static final String DATA_UPDATE_NOTE_CONTENT_ON_INSERT_TRIGGER = "CREATE TRIGGER update_note_content_on_insert " + @@ -147,6 +163,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { /** * Update note's content when data with {@link DataConstants#NOTE} type has changed + * Note数据被修改后需要更改的数据的表格。 */ private static final String DATA_UPDATE_NOTE_CONTENT_ON_UPDATE_TRIGGER = "CREATE TRIGGER update_note_content_on_update " + @@ -160,6 +177,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { /** * Update note's content when data with {@link DataConstants#NOTE} type has deleted + * Note数据被删除后需要更改的数据的表格 */ private static final String DATA_UPDATE_NOTE_CONTENT_ON_DELETE_TRIGGER = "CREATE TRIGGER update_note_content_on_delete " + @@ -173,6 +191,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { /** * Delete datas belong to note which has been deleted + * 删除已删除的便签的数据后需要更改的数据的表格。 */ private static final String NOTE_DELETE_DATA_ON_DELETE_TRIGGER = "CREATE TRIGGER delete_data_on_delete " + @@ -184,6 +203,7 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { /** * Delete notes belong to folder which has been deleted + * 删除已删除的文件夹的便签后需要更改的数据的表格。 */ private static final String FOLDER_DELETE_NOTES_ON_DELETE_TRIGGER = "CREATE TRIGGER folder_delete_notes_on_delete " + @@ -208,8 +228,11 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { public NotesDatabaseHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); - } + }/**构造函数,传入数据库的名称和版本*/ + /** + *创建表格(用来存储标签属性) + */ public void createNoteTable(SQLiteDatabase db) { db.execSQL(CREATE_NOTE_TABLE_SQL); reCreateNoteTableTriggers(db); @@ -217,6 +240,9 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { Log.d(TAG, "note table has been created"); } + /** + * 用来重新创建上述定义的表格用的,先删除原来有的数据库的触发器再重新创建新的数据库 + */ private void reCreateNoteTableTriggers(SQLiteDatabase db) { db.execSQL("DROP TRIGGER IF EXISTS increase_folder_count_on_update"); db.execSQL("DROP TRIGGER IF EXISTS decrease_folder_count_on_update"); @@ -294,6 +320,9 @@ public class NotesDatabaseHelper extends SQLiteOpenHelper { return mInstance; } + /** + * 实现上面创建的两个表格 + */ @Override public void onCreate(SQLiteDatabase db) { createNoteTable(db); diff --git a/app/src/main/java/net/micode/notes/data/NotesProvider.java b/app/src/main/java/net/micode/notes/data/NotesProvider.java index 51d2ff9..b0f43a0 100644 --- a/app/src/main/java/net/micode/notes/data/NotesProvider.java +++ b/app/src/main/java/net/micode/notes/data/NotesProvider.java @@ -35,6 +35,15 @@ import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.NotesDatabaseHelper.TABLE; +/** + * 为存储和获取数据提供接口。可以在不同的应用程序之间共享数据 + * ContentProvider提供的方法 + * query:查询 + * insert:插入 + * update:更新 + * delete:删除 + * getType:得到数据类型 + */ public class NotesProvider extends ContentProvider { private static final UriMatcher mMatcher; @@ -79,12 +88,19 @@ public class NotesProvider extends ContentProvider { + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE; + /** + * 只在onCreate()中初始化Context + * 对mHelper进行实例化 + */ @Override public boolean onCreate() { mHelper = NotesDatabaseHelper.getInstance(getContext()); return true; } + /** + *查询uri在数据库中对应的位置 + */ @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { @@ -112,6 +128,9 @@ public class NotesProvider extends ContentProvider { break; case URI_SEARCH: case URI_SEARCH_SUGGEST: + /** + * 不合法的参数异常 + */ if (sortOrder != null || projection != null) { throw new IllegalArgumentException( "do not specify sortOrder, selection, selectionArgs, or projection" + "with this query"); @@ -147,6 +166,9 @@ public class NotesProvider extends ContentProvider { return c; } + /** + * 插入一个uri + */ @Override public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = mHelper.getWritableDatabase(); @@ -181,6 +203,9 @@ public class NotesProvider extends ContentProvider { return ContentUris.withAppendedId(uri, insertedId); } + /** + * 删除一个uri + */ @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int count = 0; @@ -227,6 +252,9 @@ public class NotesProvider extends ContentProvider { return count; } + /** + * 更新一个uri + */ @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { int count = 0; @@ -267,6 +295,9 @@ public class NotesProvider extends ContentProvider { return count; } + /** + *将字符串解析成规定格式 + */ private String parseSelection(String selection) { return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); } @@ -292,7 +323,7 @@ public class NotesProvider extends ContentProvider { } sql.append(selectString); } - + /** execSQL()方法可以执行insert、delete、update和CREATE TABLE之类有更改行为的SQL语句 */ mHelper.getWritableDatabase().execSQL(sql.toString()); } diff --git a/app/src/main/java/net/micode/notes/model/Note.java b/app/src/main/java/net/micode/notes/model/Note.java index 027ef91..8bc27c8 100644 --- a/app/src/main/java/net/micode/notes/model/Note.java +++ b/app/src/main/java/net/micode/notes/model/Note.java @@ -53,7 +53,9 @@ public class Note { values.put(NoteColumns.LOCAL_MODIFIED, 1); values.put(NoteColumns.PARENT_ID, folderId); Uri uri = context.getContentResolver().insert(Notes.CONTENT_NOTE_URI, values); - + /** + ContentResolver()主要是实现外部应用对ContentProvider中的数据进行添加、删除、修改和查询操作 + */ long noteId = 0; try { noteId = Long.valueOf(uri.getPathSegments().get(1)); @@ -67,6 +69,9 @@ public class Note { return noteId; } + /** + 用来存储便签的数据,一个是存储便签属性、一个是存储便签内容 + */ public Note() { mNoteDiffValues = new ContentValues(); mNoteData = new NoteData(); @@ -81,18 +86,30 @@ public class Note { public void setTextData(String key, String value) { mNoteData.setTextData(key, value); } + /** + 设置文本数据的ID + */ public void setTextDataId(long id) { mNoteData.setTextDataId(id); } + /** + 得到文本数据的ID + */ public long getTextDataId() { return mNoteData.mTextDataId; } + /** + 设置电话号码数据的ID + */ public void setCallDataId(long id) { mNoteData.setCallDataId(id); } + /** + 得到电话号码数据的ID + */ public void setCallData(String key, String value) { mNoteData.setCallData(key, value); @@ -132,6 +149,9 @@ public class Note { return true; } + /** + * 定义一个基本的便签内容的数据类,主要包含文本数据和电话号码数据 + */ private class NoteData { private long mTextDataId; @@ -187,6 +207,9 @@ public class Note { if (noteId <= 0) { throw new IllegalArgumentException("Wrong note id:" + noteId); } + /** + * 判断数据是否合法 + */ ArrayList operationList = new ArrayList(); ContentProviderOperation.Builder builder = null; @@ -212,6 +235,9 @@ public class Note { } mTextDataValues.clear(); } + /** + * 把文本数据存入DataColumns + */ if (mCallDataValues.size() > 0) { mCallDataValues.put(DataColumns.NOTE_ID, noteId); diff --git a/app/src/main/java/net/micode/notes/model/WorkingNote.java b/app/src/main/java/net/micode/notes/model/WorkingNote.java index 12079de..e999d6a 100644 --- a/app/src/main/java/net/micode/notes/model/WorkingNote.java +++ b/app/src/main/java/net/micode/notes/model/WorkingNote.java @@ -62,6 +62,9 @@ public class WorkingNote { private NoteSettingChangedListener mNoteSettingStatusListener; + /** + * 声明 DATA_PROJECTION字符串数组 + */ public static final String[] DATA_PROJECTION = new String[]{ DataColumns.ID, DataColumns.CONTENT, @@ -102,6 +105,10 @@ public class WorkingNote { private static final int NOTE_MODIFIED_DATE_COLUMN = 5; // New note construct + + /** + * WorkingNote的构造函数 + * */ private WorkingNote(Context context, long folderId) { mContext = context; mAlertDate = 0; @@ -124,6 +131,9 @@ public class WorkingNote { loadNote(); } + /** + * 通过数据库调用query函数找到第一个条目 + */ private void loadNote() { Cursor cursor = mContext.getContentResolver().query( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null, @@ -151,7 +161,9 @@ public class WorkingNote { DataColumns.NOTE_ID + "=?", new String[]{ String.valueOf(mNoteId) }, null); - +/** + * 查看第一项是否存在 + */ if (cursor != null) { if (cursor.moveToFirst()) { do { @@ -174,6 +186,9 @@ public class WorkingNote { } } + /** + * 创建空的Note 传参:context,文件夹id,widget,背景颜色 + */ public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, int widgetType, int defaultBgColorId) { WorkingNote note = new WorkingNote(context, folderId); @@ -229,6 +244,10 @@ public class WorkingNote { mNoteSettingStatusListener = l; } + /** + * 设置AlertDate + * 若 mAlertDate与data不同,则更改mAlertDate并设定NoteValue + */ public void setAlertDate(long date, boolean set) { if (date != mAlertDate) { mAlertDate = date; @@ -239,6 +258,9 @@ public class WorkingNote { } } + /** + * 设定删除标记 + */ public void markDeleted(boolean mark) { mIsDeleted = mark; if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID @@ -247,6 +269,9 @@ public class WorkingNote { } } + /** + *设定背景颜色 + */ public void setBgColorId(int id) { if (id != mBgColorId) { mBgColorId = id; @@ -257,6 +282,9 @@ public class WorkingNote { } } + /** + *设定检查列表模式 + */ public void setCheckListMode(int mode) { if (mMode != mode) { if (mNoteSettingStatusListener != null) { @@ -267,6 +295,9 @@ public class WorkingNote { } } + /** + *设定WidgetType + */ public void setWidgetType(int type) { if (type != mWidgetType) { mWidgetType = type; @@ -274,6 +305,9 @@ public class WorkingNote { } } + /** + *设定WidgetId + */ public void setWidgetId(int id) { if (id != mWidgetId) { mWidgetId = id; @@ -281,6 +315,9 @@ public class WorkingNote { } } + /** + *设定setWorkingText + */ public void setWorkingText(String text) { if (!TextUtils.equals(mContent, text)) { mContent = text;