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 edb0a60..69e72d2 100644 --- a/app/src/main/java/net/micode/notes/data/NotesProvider.java +++ b/app/src/main/java/net/micode/notes/data/NotesProvider.java @@ -36,20 +36,23 @@ import net.micode.notes.data.NotesDatabaseHelper.TABLE; public class NotesProvider extends ContentProvider { - private static final UriMatcher mMatcher; + private static final UriMatcher mMatcher;// 定义UriMatcher,用于匹配URI - private NotesDatabaseHelper mHelper; + private NotesDatabaseHelper mHelper;// 数据库帮助类 - private static final String TAG = "NotesProvider"; + private static final String TAG = "NotesProvider";// 日志输出标签 - private static final int URI_NOTE = 1; - private static final int URI_NOTE_ITEM = 2; - private static final int URI_DATA = 3; - private static final int URI_DATA_ITEM = 4; + private static final int URI_NOTE = 1; // 笔记URI + private static final int URI_NOTE_ITEM = 2;// 单个笔记URI + private static final int URI_DATA = 3; // 笔记数据URI + private static final int URI_DATA_ITEM = 4;// 单个笔记数据URI - private static final int URI_SEARCH = 5; - private static final int URI_SEARCH_SUGGEST = 6; + private static final int URI_SEARCH = 5;// 搜索URI + private static final int URI_SEARCH_SUGGEST = 6;// 搜索建议URI + + +// 初始化UriMatcher static { mMatcher = new UriMatcher(UriMatcher.NO_MATCH); mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE); @@ -65,6 +68,7 @@ public class NotesProvider extends ContentProvider { * x'0A' represents the '\n' character in sqlite. For title and content in the search result, * we will trim '\n' and white space in order to show more information. */ + // 搜索结果显示的投影 private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + "," + NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + "," + "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_1 + "," @@ -72,7 +76,7 @@ public class NotesProvider extends ContentProvider { + R.drawable.search_result + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1 + "," + "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + "," + "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA; - + // 搜索语句 private static String NOTES_SNIPPET_SEARCH_QUERY = "SELECT " + NOTES_SEARCH_PROJECTION + " FROM " + TABLE.NOTE + " WHERE " + NoteColumns.SNIPPET + " LIKE ?" @@ -81,37 +85,37 @@ public class NotesProvider extends ContentProvider { @Override public boolean onCreate() { - mHelper = NotesDatabaseHelper.getInstance(getContext()); + mHelper = NotesDatabaseHelper.getInstance(getContext());// 创建数据库帮助类实例 return true; } - + // 查询数据 @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor c = null; - SQLiteDatabase db = mHelper.getReadableDatabase(); - String id = null; + SQLiteDatabase db = mHelper.getReadableDatabase(); // 获取可读数据库实例 + String id = null; // ID值 switch (mMatcher.match(uri)) { - case URI_NOTE: + case URI_NOTE: // 笔记查询 c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null, sortOrder); break; - case URI_NOTE_ITEM: + case URI_NOTE_ITEM:// 单个笔记查询 id = uri.getPathSegments().get(1); c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder); break; - case URI_DATA: + case URI_DATA:// 笔记数据查询 c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null, sortOrder); break; - case URI_DATA_ITEM: + case URI_DATA_ITEM: // 单个笔记数据查询 id = uri.getPathSegments().get(1); c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder); break; case URI_SEARCH: - case URI_SEARCH_SUGGEST: + case URI_SEARCH_SUGGEST:// 搜索和搜索建议查询 if (sortOrder != null || projection != null) { throw new IllegalArgumentException( "do not specify sortOrder, selection, selectionArgs, or projection" + "with this query"); @@ -146,16 +150,16 @@ public class NotesProvider extends ContentProvider { } return c; } - +// 插入数据 @Override public Uri insert(Uri uri, ContentValues values) { - SQLiteDatabase db = mHelper.getWritableDatabase(); + SQLiteDatabase db = mHelper.getWritableDatabase();// 获取可写数据库实例 long dataId = 0, noteId = 0, insertedId = 0; switch (mMatcher.match(uri)) { - case URI_NOTE: + case URI_NOTE:// 插入笔记 insertedId = noteId = db.insert(TABLE.NOTE, null, values); break; - case URI_DATA: + case URI_DATA:// 插入笔记数据 if (values.containsKey(DataColumns.NOTE_ID)) { noteId = values.getAsLong(DataColumns.NOTE_ID); } else { @@ -166,33 +170,33 @@ public class NotesProvider extends ContentProvider { default: throw new IllegalArgumentException("Unknown URI " + uri); } - // Notify the note uri + // 通知笔记URI if (noteId > 0) { getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); } - // Notify the data uri + // 通知笔记数据URI if (dataId > 0) { getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null); } - return ContentUris.withAppendedId(uri, insertedId); + return ContentUris.withAppendedId(uri, insertedId);// 返回插入数据的URI } - + // 删除数据 @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int count = 0; String id = null; - SQLiteDatabase db = mHelper.getWritableDatabase(); - boolean deleteData = false; + SQLiteDatabase db = mHelper.getWritableDatabase();// 获取可写数据库实例 + boolean deleteData = false; // 是否删除数据 switch (mMatcher.match(uri)) { - case URI_NOTE: + case URI_NOTE:// 删除笔记 selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 "; count = db.delete(TABLE.NOTE, selection, selectionArgs); break; - case URI_NOTE_ITEM: + case URI_NOTE_ITEM:// 删除单个笔记 id = uri.getPathSegments().get(1); /** * ID that smaller than 0 is system folder which is not allowed to