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..640c0a4 100644 --- a/app/src/main/java/net/micode/notes/data/NotesProvider.java +++ b/app/src/main/java/net/micode/notes/data/NotesProvider.java @@ -36,6 +36,7 @@ import net.micode.notes.data.NotesDatabaseHelper.TABLE; public class NotesProvider extends ContentProvider { + //为存储和获取数据提供接口 private static final UriMatcher mMatcher; private NotesDatabaseHelper mHelper; @@ -59,7 +60,7 @@ public class NotesProvider extends ContentProvider { mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH); mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST); mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST); - } + }// 把需要匹配Uri路径全部给注册上 /** * x'0A' represents the '\n' character in sqlite. For title and content in the search result, @@ -72,24 +73,26 @@ 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 ?" + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE; + //定义常量 @Override public boolean onCreate() { mHelper = NotesDatabaseHelper.getInstance(getContext()); return true; - } + }//实例化mHelper @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { Cursor c = null; - SQLiteDatabase db = mHelper.getReadableDatabase(); + SQLiteDatabase db = mHelper.getReadableDatabase();//get数据库 String id = null; switch (mMatcher.match(uri)) { case URI_NOTE: @@ -115,7 +118,7 @@ public class NotesProvider extends ContentProvider { if (sortOrder != null || projection != null) { throw new IllegalArgumentException( "do not specify sortOrder, selection, selectionArgs, or projection" + "with this query"); - } + }//抛出数据异常 String searchString = null; if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) { @@ -139,17 +142,17 @@ public class NotesProvider extends ContentProvider { } break; default: - throw new IllegalArgumentException("Unknown URI " + uri); - } + throw new IllegalArgumentException("Unknown URI " + uri);//抛出URI异常 + }//查找Uri if (c != null) { c.setNotificationUri(getContext().getContentResolver(), uri); } return c; - } + }// 查询uri在数据库中对应的位置 @Override public Uri insert(Uri uri, ContentValues values) { - SQLiteDatabase db = mHelper.getWritableDatabase(); + SQLiteDatabase db = mHelper.getWritableDatabase();//get数据库 long dataId = 0, noteId = 0, insertedId = 0; switch (mMatcher.match(uri)) { case URI_NOTE: @@ -164,7 +167,7 @@ public class NotesProvider extends ContentProvider { insertedId = dataId = db.insert(TABLE.DATA, null, values); break; default: - throw new IllegalArgumentException("Unknown URI " + uri); + throw new IllegalArgumentException("Unknown URI " + uri);//抛出URI异常 } // Notify the note uri if (noteId > 0) { @@ -178,14 +181,14 @@ public class NotesProvider extends ContentProvider { 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) { + public int delete(Uri uri, String selection, String[] selectionArgs) {//删除URI int count = 0; String id = null; - SQLiteDatabase db = mHelper.getWritableDatabase(); + SQLiteDatabase db = mHelper.getWritableDatabase();//get数据库 boolean deleteData = false; switch (mMatcher.match(uri)) { case URI_NOTE: @@ -216,7 +219,7 @@ public class NotesProvider extends ContentProvider { deleteData = true; break; default: - throw new IllegalArgumentException("Unknown URI " + uri); + throw new IllegalArgumentException("Unknown URI " + uri);//抛出URI异常 } if (count > 0) { if (deleteData) { @@ -228,7 +231,7 @@ public class NotesProvider extends ContentProvider { } @Override - public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {//更新 int count = 0; String id = null; SQLiteDatabase db = mHelper.getWritableDatabase(); @@ -269,7 +272,7 @@ public class NotesProvider extends ContentProvider { private String parseSelection(String selection) { return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); - } + }//字符串变成规定格式 private void increaseNoteVersion(long id, String selection, String[] selectionArgs) { StringBuilder sql = new StringBuilder(120); @@ -294,7 +297,7 @@ public class NotesProvider extends ContentProvider { } mHelper.getWritableDatabase().execSQL(sql.toString()); - } + } //升级 @Override public String getType(Uri uri) {