From 57762bd66ef4051a699e542e988c7bf308cd4301 Mon Sep 17 00:00:00 2001 From: dingxinnan <1175943266@qq.com> Date: Thu, 21 Dec 2023 00:31:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../net/micode/notes/data/NotesProvider.java | 63 +++++++++++++++++-- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/src/Notes-master1/app/src/main/java/net/micode/notes/data/NotesProvider.java b/src/Notes-master1/app/src/main/java/net/micode/notes/data/NotesProvider.java index edb0a60..d684cac 100644 --- a/src/Notes-master1/app/src/main/java/net/micode/notes/data/NotesProvider.java +++ b/src/Notes-master1/app/src/main/java/net/micode/notes/data/NotesProvider.java @@ -36,12 +36,16 @@ import net.micode.notes.data.NotesDatabaseHelper.TABLE; public class NotesProvider extends ContentProvider { + // 创建一个UriMatcher对象,用于匹配URI private static final UriMatcher mMatcher; + // 创建一个NotesDatabaseHelper对象,用于操作数据库 private NotesDatabaseHelper mHelper; + // 创建一个TAG,用于标记日志 private static final String TAG = "NotesProvider"; + // 创建一个URI码,用于表示不同的URI private static final int URI_NOTE = 1; private static final int URI_NOTE_ITEM = 2; private static final int URI_DATA = 3; @@ -50,6 +54,7 @@ public class NotesProvider extends ContentProvider { private static final int URI_SEARCH = 5; private static final int URI_SEARCH_SUGGEST = 6; + // 静态初始化,创建一个UriMatcher对象,用于匹配URI static { mMatcher = new UriMatcher(UriMatcher.NO_MATCH); mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE); @@ -81,6 +86,7 @@ public class NotesProvider extends ContentProvider { @Override public boolean onCreate() { + // 获取NotesDatabaseHelper实例 mHelper = NotesDatabaseHelper.getInstance(getContext()); return true; } @@ -88,99 +94,136 @@ public class NotesProvider extends ContentProvider { @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { + // 初始化游标 Cursor c = null; + // 获取可读的数据库 SQLiteDatabase db = mHelper.getReadableDatabase(); + // 初始化id String id = null; + // 根据uri匹配模式 switch (mMatcher.match(uri)) { + // 查询note表 case URI_NOTE: + // 查询note表,并设置查询条件 c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null, sortOrder); break; + // 查询note表中的某一行 case URI_NOTE_ITEM: + // 获取id id = uri.getPathSegments().get(1); + // 查询note表,并设置查询条件 c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder); break; + // 查询data表 case URI_DATA: + // 查询data表,并设置查询条件 c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null, sortOrder); break; + // 查询data表中的某一行 case URI_DATA_ITEM: + // 获取id id = uri.getPathSegments().get(1); + // 查询data表,并设置查询条件 c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder); 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"); } + // 初始化搜索字符串 String searchString = null; + // 根据uri匹配模式,获取搜索字符串 if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) { + // 如果uri中包含路径段,则获取路径段 if (uri.getPathSegments().size() > 1) { searchString = uri.getPathSegments().get(1); } } else { + // 从uri中获取查询参数 searchString = uri.getQueryParameter("pattern"); } + // 如果搜索字符串为空,则返回空游标 if (TextUtils.isEmpty(searchString)) { return null; } try { + // 将搜索字符串格式化 searchString = String.format("%%%s%%", searchString); + // 查询note表,并设置查询条件 c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY, new String[] { searchString }); } catch (IllegalStateException ex) { Log.e(TAG, "got exception: " + ex.toString()); } break; + // 未知uri default: throw new IllegalArgumentException("Unknown URI " + uri); } + // 如果游标不为空,则设置通知uri if (c != null) { c.setNotificationUri(getContext().getContentResolver(), uri); } + // 返回游标 return c; } - @Override + @Override public Uri insert(Uri uri, ContentValues values) { + // 获取可写的数据库 SQLiteDatabase db = mHelper.getWritableDatabase(); + // 定义变量 long dataId = 0, noteId = 0, insertedId = 0; + // 根据uri匹配 switch (mMatcher.match(uri)) { + // 匹配到note uri case URI_NOTE: + // 插入note insertedId = noteId = db.insert(TABLE.NOTE, null, values); break; + // 匹配到data uri case URI_DATA: + // 判断note id是否存在 if (values.containsKey(DataColumns.NOTE_ID)) { noteId = values.getAsLong(DataColumns.NOTE_ID); } else { Log.d(TAG, "Wrong data format without note id:" + values.toString()); } + // 插入data insertedId = dataId = db.insert(TABLE.DATA, null, values); break; + // 匹配到其他uri default: throw new IllegalArgumentException("Unknown URI " + uri); } // Notify the note uri + // 通知note uri if (noteId > 0) { getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); } // Notify the data uri + // 通知data uri if (dataId > 0) { getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null); } + // 返回插入的id return ContentUris.withAppendedId(uri, insertedId); } - @Override public int delete(Uri uri, String selection, String[] selectionArgs) { int count = 0; @@ -228,28 +271,34 @@ 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(); boolean updateData = false; switch (mMatcher.match(uri)) { case URI_NOTE: + // 增加版本号 increaseNoteVersion(-1, selection, selectionArgs); count = db.update(TABLE.NOTE, values, selection, selectionArgs); break; case URI_NOTE_ITEM: + // 获取id id = uri.getPathSegments().get(1); + // 增加版本号 increaseNoteVersion(Long.valueOf(id), selection, selectionArgs); count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs); break; case URI_DATA: + // 更新数据 count = db.update(TABLE.DATA, values, selection, selectionArgs); updateData = true; break; case URI_DATA_ITEM: + // 获取id id = uri.getPathSegments().get(1); + // 更新数据 count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs); updateData = true; @@ -260,18 +309,22 @@ public class NotesProvider extends ContentProvider { if (count > 0) { if (updateData) { + // 通知更新数据 getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); } + // 通知更新笔记 getContext().getContentResolver().notifyChange(uri, null); } return count; } - private String parseSelection(String selection) { + 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); sql.append("UPDATE "); sql.append(TABLE.NOTE); @@ -299,7 +352,7 @@ public class NotesProvider extends ContentProvider { @Override public String getType(Uri uri) { // TODO Auto-generated method stub + // 获取URI的类型 return null; } - }