|
|
@ -2,6 +2,16 @@
|
|
|
|
* 该类是Notes应用的内容提供者,负责管理Notes的数据,包括查询、插入、更新和删除操作。
|
|
|
|
* 该类是Notes应用的内容提供者,负责管理Notes的数据,包括查询、插入、更新和删除操作。
|
|
|
|
* 它与数据库交互,将操作转换为对数据库的相应操作。
|
|
|
|
* 它与数据库交互,将操作转换为对数据库的相应操作。
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* @Package: net.micode.notes.data
|
|
|
|
|
|
|
|
* @ClassName: NotesDatabaseHelper
|
|
|
|
|
|
|
|
* @Description:
|
|
|
|
|
|
|
|
* 该类是Notes应用的内容提供者,负责管理Notes的数据,包括查询、插入、更新和删除操作。
|
|
|
|
|
|
|
|
* 它与数据库交互,将操作转换为对数据库的相应操作。该类继承自ContentProvider类,并实现了其抽象方法。
|
|
|
|
|
|
|
|
* @Author: Xinqi Qin
|
|
|
|
|
|
|
|
* @CreateDate: 12/22/2024 22:53 AM
|
|
|
|
|
|
|
|
*/
|
|
|
|
package net.micode.notes.data;
|
|
|
|
package net.micode.notes.data;
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.SearchManager;
|
|
|
|
import android.app.SearchManager;
|
|
|
@ -17,13 +27,13 @@ import android.text.TextUtils;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
import net.micode.notes.R;
|
|
|
|
import net.micode.notes.data.Notes.DataColumns;
|
|
|
|
import net.micode.notes.data.Notes.DataColumns;//定义了一些常量,表示数据库表的列名。
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;//定义了一些常量,表示笔记表的列名。
|
|
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
|
|
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;//定义了一个枚举类型,表示数据库表的名称。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
|
public class NotesProvider extends ContentProvider {//android中的ContentProvider类,用于提供数据给其他应用程序访问。
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
private static final UriMatcher mMatcher;//UriMatcher是Android中的一个类,用于匹配URI。
|
|
|
|
|
|
|
|
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
|
|
|
|
|
|
|
@ -40,14 +50,17 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
// 初始化UriMatcher,用于匹配不同的URI请求
|
|
|
|
// 初始化UriMatcher,用于匹配不同的URI请求
|
|
|
|
static {
|
|
|
|
static {
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
|
|
|
|
//创建一个UriMatcher对象mMatcher,初始值为UriMatcher.NO_MATCH,表示没有任何匹配规则。
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
|
|
|
|
|
//第一个参数是authority(授权),第二个参数是路径,第三个参数是匹配的URI类型。
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH);
|
|
|
|
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);
|
|
|
|
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路径,确定应该调用哪个处理程序来处理请求
|
|
|
|
|
|
|
|
//例如,当用户访问"http://example.com/note"时,mMatcher会匹配到第一个规则
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* 在搜索结果中,为了显示更多信息,我们会去除标题和内容中的'\n'和空白字符。
|
|
|
|
* 在搜索结果中,为了显示更多信息,我们会去除标题和内容中的'\n'和空白字符。
|
|
|
@ -60,7 +73,8 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
+ "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + ","
|
|
|
|
+ "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + ","
|
|
|
|
+ "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA;
|
|
|
|
+ "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA;
|
|
|
|
|
|
|
|
|
|
|
|
// 用于搜索查询的SQL语句
|
|
|
|
//是一个SQL查询语句,用于从数据库中检索与给定搜索条件匹配的笔记
|
|
|
|
|
|
|
|
//用于搜索笔记的投影字段列表。这个投影字段列表可以用于构建SQL查询语句,以便在数据库中搜索和检索相关笔记。
|
|
|
|
private static String NOTES_SNIPPET_SEARCH_QUERY = "SELECT " + NOTES_SEARCH_PROJECTION
|
|
|
|
private static String NOTES_SNIPPET_SEARCH_QUERY = "SELECT " + NOTES_SEARCH_PROJECTION
|
|
|
|
+ " FROM " + TABLE.NOTE
|
|
|
|
+ " FROM " + TABLE.NOTE
|
|
|
|
+ " WHERE " + NoteColumns.SNIPPET + " LIKE ?"
|
|
|
|
+ " WHERE " + NoteColumns.SNIPPET + " LIKE ?"
|
|
|
@ -92,10 +106,10 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
String sortOrder) {
|
|
|
|
String sortOrder) {
|
|
|
|
Cursor c = null;
|
|
|
|
Cursor c = null;
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();//获取可读数据库
|
|
|
|
String id = null;
|
|
|
|
String id = null;
|
|
|
|
// 根据URI匹配查询类型
|
|
|
|
// 根据URI匹配查询类型
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {//匹配数据库中相应条目
|
|
|
|
case URI_NOTE:
|
|
|
|
case URI_NOTE:
|
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
|
sortOrder);
|
|
|
|
sortOrder);
|
|
|
@ -118,7 +132,7 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
// 处理搜索建议的特殊逻辑
|
|
|
|
// 处理搜索建议的特殊逻辑
|
|
|
|
if (sortOrder != null || projection != null) {
|
|
|
|
if (sortOrder != null || projection != null) {
|
|
|
|
throw new IllegalArgumentException(
|
|
|
|
throw new IllegalArgumentException(//异常情况
|
|
|
|
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
|
|
|
|
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -189,7 +203,10 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//它通过调用getContentResolver().notifyChange()方法来实现这个功能。
|
|
|
|
|
|
|
|
//在这段代码中,首先检查noteId和dataId是否大于0,以确保它们是有效的ID。然后,使用ContentUris.withAppendedId()方法来构建对应的URI,
|
|
|
|
|
|
|
|
// 其中Notes.CONTENT_NOTE_URI和Notes.CONTENT_DATA_URI分别表示笔记和数据的URI。
|
|
|
|
|
|
|
|
// 最后,调用getContext().getContentResolver().notifyChange()方法并传入构建好的URI,以通知系统该URI所对应的资源已经发生了变化。
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -237,7 +254,7 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 通知URI改变
|
|
|
|
// 通知URI改变,即系统相关数据已经发生变化,以便进行相应的更新或处理。
|
|
|
|
if (count > 0) {
|
|
|
|
if (count > 0) {
|
|
|
|
if (deleteData) {
|
|
|
|
if (deleteData) {
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|