|
|
@ -16,32 +16,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.data;
|
|
|
|
package net.micode.notes.data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//导入Android系统提供的搜索管理器,用于处理搜索相关功能
|
|
|
|
import android.app.SearchManager;
|
|
|
|
import android.app.SearchManager;
|
|
|
|
|
|
|
|
//导入Android内容提供者类,用于管理数据的访问
|
|
|
|
import android.content.ContentProvider;
|
|
|
|
import android.content.ContentProvider;
|
|
|
|
|
|
|
|
//导入Android内容URI工具类,用于创建和处理URI
|
|
|
|
import android.content.ContentUris;
|
|
|
|
import android.content.ContentUris;
|
|
|
|
|
|
|
|
//导入Android内容值类,用于存储要插入或更新的数据
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.ContentValues;
|
|
|
|
|
|
|
|
//导入Android意图类,用于启动活动、广播等
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.Intent;
|
|
|
|
|
|
|
|
//导入Android URI匹配器,用于匹配URI以确定操作类型
|
|
|
|
import android.content.UriMatcher;
|
|
|
|
import android.content.UriMatcher;
|
|
|
|
|
|
|
|
//导入Android数据库游标,用于查询数据库后获取数据
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
|
|
|
//导入Android SQLite数据库类,用于操作SQLite数据库
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
|
|
|
//导入Android网络URI类,用于表示网络路径
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
|
|
//导入Android文本工具类,用于处理文本相关操作
|
|
|
|
import android.text.TextUtils;
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
//导入Android日志工具类,用于输出日志信息
|
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
//导入笔记应用所需的资源类,包含应用所需的各种资源ID
|
|
|
|
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 {
|
|
|
|
|
|
|
|
//UriMatcher用于匹配传入的URI,以便确定如何处理请求
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
|
|
|
|
// NotesDatabaseHelper是与数据库交互的助手类,用于执行数据库操作
|
|
|
|
|
|
|
|
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
|
|
|
|
//TAG常量用于日志记录,方便在日志输出时标识来源
|
|
|
|
private static final String TAG = "NotesProvider";
|
|
|
|
private static final String TAG = "NotesProvider";
|
|
|
|
|
|
|
|
//以下常量定义了不同的URI匹配码,每个代码对应一种特定的URI请求类型
|
|
|
|
private static final int URI_NOTE = 1;
|
|
|
|
private static final int URI_NOTE = 1;
|
|
|
|
private static final int URI_NOTE_ITEM = 2;
|
|
|
|
private static final int URI_NOTE_ITEM = 2;
|
|
|
|
private static final int URI_DATA = 3;
|
|
|
|
private static final int URI_DATA = 3;
|
|
|
@ -51,12 +66,17 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;
|
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;
|
|
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
static {
|
|
|
|
|
|
|
|
//初始化UriMatcher对象,用于后续匹配不同的URI
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
|
|
|
|
//为Notes实体的各个操作添加URI匹配规则
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM);
|
|
|
|
|
|
|
|
//为Data实体的各个操作添加URI匹配规则
|
|
|
|
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);
|
|
|
|
|
|
|
|
//添加用于搜索操作的URI匹配规则
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH);
|
|
|
|
|
|
|
|
//添加用于搜索建议操作的URI匹配规则,支持模糊查询
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -66,13 +86,19 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
* we will trim '\n' and white space in order to show more information.
|
|
|
|
* we will trim '\n' and white space in order to show more information.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + ","
|
|
|
|
private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + ","
|
|
|
|
|
|
|
|
//将ID列别名设置为SUGGEST_COLUMN_INTENT_EXTRA_DATA,用于搜索建议的额外数据
|
|
|
|
+ NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + ","
|
|
|
|
+ NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + ","
|
|
|
|
|
|
|
|
//使用TRIM和REPLACE函数处理SNIPPET列,移除换行符,用于搜索建议的文本显示
|
|
|
|
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_1 + ","
|
|
|
|
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_1 + ","
|
|
|
|
|
|
|
|
//再次处理SNIPPET列,用于搜索建议的第二个文本显示
|
|
|
|
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + ","
|
|
|
|
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + ","
|
|
|
|
|
|
|
|
//将一个drawable资源作为图标用于搜索建议
|
|
|
|
+ R.drawable.search_result + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1 + ","
|
|
|
|
+ R.drawable.search_result + " AS " + SearchManager.SUGGEST_COLUMN_ICON_1 + ","
|
|
|
|
|
|
|
|
//设置搜索建议的意图行为为ACTION_VIEW
|
|
|
|
+ "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + ","
|
|
|
|
+ "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + ","
|
|
|
|
|
|
|
|
//设置搜索建议的意图数据类型为Notes.TextNote.CONTENT_TYPE
|
|
|
|
+ "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA;
|
|
|
|
+ "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA;
|
|
|
|
|
|
|
|
//定义一个用于搜索笔记片段的查询字符串
|
|
|
|
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 ?"
|
|
|
@ -81,23 +107,32 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
|
|
|
public boolean onCreate() {
|
|
|
|
|
|
|
|
//初始化NotesDatabaseHelper实例,用于后续的数据库操作
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
|
|
|
|
//表示该方法已成功创建,返回true
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
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) {
|
|
|
|
|
|
|
|
// 初始化游标为null
|
|
|
|
Cursor c = null;
|
|
|
|
Cursor c = null;
|
|
|
|
|
|
|
|
//获取可读的数据库实例
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
|
|
|
|
|
|
//初始化ID为null
|
|
|
|
String id = null;
|
|
|
|
String id = null;
|
|
|
|
|
|
|
|
// 根据URI匹配类型执行不同的查询操作
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
case URI_NOTE:
|
|
|
|
case URI_NOTE:
|
|
|
|
|
|
|
|
//查询整个NOTE表
|
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
|
sortOrder);
|
|
|
|
sortOrder);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
|
|
|
//获取URI路径中的ID,查询特定的NOTE项
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
|
|
|
//查询整个DATA表
|
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
|
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
@ -106,74 +141,102 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
sortOrder);
|
|
|
|
sortOrder);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
|
|
|
//获取URI路径中的ID,查询特定的DATA项
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
|
|
|
|
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_SEARCH:
|
|
|
|
case URI_SEARCH:
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
|
|
|
|
//对于搜索和搜索建议的查询,限制使用sortOrder和projection参数
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//初始化searchString为null,用于存储后续可能提取的搜索字符串
|
|
|
|
String searchString = null;
|
|
|
|
String searchString = null;
|
|
|
|
|
|
|
|
//检查uri是否匹配预定义的URI模式,以确定是否为搜索建议请求
|
|
|
|
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
|
|
|
|
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
|
|
|
|
|
|
|
|
//如果uri的路径段数大于1,说明包含足够的路径信息
|
|
|
|
if (uri.getPathSegments().size() > 1) {
|
|
|
|
if (uri.getPathSegments().size() > 1) {
|
|
|
|
|
|
|
|
//从路径段中提取搜索字符串
|
|
|
|
searchString = uri.getPathSegments().get(1);
|
|
|
|
searchString = uri.getPathSegments().get(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
|
|
|
|
//如果不是搜索建议请求,尝试从查询参数中获取搜索字符串
|
|
|
|
searchString = uri.getQueryParameter("pattern");
|
|
|
|
searchString = uri.getQueryParameter("pattern");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(searchString)) {
|
|
|
|
if (TextUtils.isEmpty(searchString)) {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} //# 检查搜索字符串是否为空
|
|
|
|
|
|
|
|
//# 如果搜索字符串为空,则返回null,表示没有找到匹配的项目
|
|
|
|
|
|
|
|
//尝试执行SQL查询以搜索笔记片段
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
//格式化搜索字符串,以便在SQL查询中使用
|
|
|
|
searchString = String.format("%%%s%%", searchString);
|
|
|
|
searchString = String.format("%%%s%%", searchString);
|
|
|
|
|
|
|
|
//执行SQL查询
|
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
|
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
|
|
|
|
new String[] { searchString });
|
|
|
|
new String[] { searchString });
|
|
|
|
} catch (IllegalStateException ex) {
|
|
|
|
} catch (IllegalStateException ex) {
|
|
|
|
|
|
|
|
//捕获异常并记录错误信息
|
|
|
|
Log.e(TAG, "got exception: " + ex.toString());
|
|
|
|
Log.e(TAG, "got exception: " + ex.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//如果URI不匹配任何已知的情况,则抛出异常
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
|
|
|
|
//抛出IllegalArgumentException,指出URI未知
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//如果c不为空,则设置通知URI,以便在内容发生变化时通知
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
|
|
|
|
|
//使用getContext().getContentResolver()获取内容解析器,与uri关联通知
|
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//返回对象c,以便进一步处理或使用
|
|
|
|
return c;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
|
|
|
|
//获取可写的数据库实例
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
//初始化数据ID和笔记ID
|
|
|
|
long dataId = 0, noteId = 0, insertedId = 0;
|
|
|
|
long dataId = 0, noteId = 0, insertedId = 0;
|
|
|
|
|
|
|
|
//根据URI匹配类型执行不同的插入操作
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
case URI_NOTE:
|
|
|
|
case URI_NOTE:
|
|
|
|
|
|
|
|
//插入笔记,同时更新插入ID和笔记ID
|
|
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
|
|
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_DATA:
|
|
|
|
case URI_DATA:
|
|
|
|
|
|
|
|
//检查数据是否包含笔记ID,如果没有则记录日志
|
|
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {
|
|
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {
|
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);
|
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
Log.d(TAG, "Wrong data format without note id:" + values.toString());
|
|
|
|
Log.d(TAG, "Wrong data format without note id:" + values.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
insertedId = dataId = db.insert(TABLE.DATA, null, values);
|
|
|
|
insertedId = dataId = db.insert(TABLE.DATA, null, values);
|
|
|
|
|
|
|
|
//插入数据,同时更新插入ID和数据ID
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//默认情况下,如果URI不匹配任何已知的情况,则抛出异常
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
|
|
|
|
//抛出IllegalArgumentException异常,指出URI未知
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Notify the note uri
|
|
|
|
// Notify the note uri
|
|
|
|
|
|
|
|
// 如果noteId大于0,则通知内容提供者有笔记内容发生变化
|
|
|
|
if (noteId > 0) {
|
|
|
|
if (noteId > 0) {
|
|
|
|
|
|
|
|
//通过ContentUris.withAppendedId方法构建特定笔记的URI
|
|
|
|
|
|
|
|
// 使用notifyChange方法通知内容提供者指定URI的数据发生了改变
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Notify the data uri
|
|
|
|
// Notify the data uri
|
|
|
|
|
|
|
|
//如果数据ID大于0,则通知内容提供者数据已更改
|
|
|
|
if (dataId > 0) {
|
|
|
|
if (dataId > 0) {
|
|
|
|
|
|
|
|
//通知注册监听指定数据URI的组件数据已更改,以便它们可以重新查询数据
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -183,119 +246,196 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
|
|
|
|
//初始化删除的行数为0
|
|
|
|
int count = 0;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
//初始化ID为null,用于可能的单个数据项的删除
|
|
|
|
String id = null;
|
|
|
|
String id = null;
|
|
|
|
|
|
|
|
//获取可写的数据库实例
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
//初始化deleteData标志为false,用于判断是否需要删除数据
|
|
|
|
boolean deleteData = false;
|
|
|
|
boolean deleteData = false;
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
|
|
|
//根据URI匹配结果执行相应的操作
|
|
|
|
case URI_NOTE:
|
|
|
|
case URI_NOTE:
|
|
|
|
|
|
|
|
//当匹配到URI_NOTE时,更新selection以限定删除条件
|
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";
|
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";
|
|
|
|
|
|
|
|
//执行删除操作,仅删除ID大于0的笔记记录
|
|
|
|
count = db.delete(TABLE.NOTE, selection, selectionArgs);
|
|
|
|
count = db.delete(TABLE.NOTE, selection, selectionArgs);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
|
|
|
//获取URI路径中的第二段作为笔记ID
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* ID that smaller than 0 is system folder which is not allowed to
|
|
|
|
* ID that smaller than 0 is system folder which is not allowed to
|
|
|
|
* trash
|
|
|
|
* trash
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
//将路径段中的ID转换为长整型
|
|
|
|
long noteId = Long.valueOf(id);
|
|
|
|
long noteId = Long.valueOf(id);
|
|
|
|
|
|
|
|
//如果noteId的值小于等于0,则退出循环
|
|
|
|
if (noteId <= 0) {
|
|
|
|
if (noteId <= 0) {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//执行数据库中的数据删除操作
|
|
|
|
|
|
|
|
//# 这里使用了delete方法从指定表中删除符合条件的记录
|
|
|
|
|
|
|
|
//# 参数 TABLE.NOTE 指定了要操作的数据库表
|
|
|
|
|
|
|
|
//# NoteColumns.ID + "=" + id + parseSelection(selection) 构造了删除条件,即指定要删除的记录的ID
|
|
|
|
|
|
|
|
//# selection 和 selectionArgs 进一步细化了删除条件,确保只删除符合特定要求的记录
|
|
|
|
|
|
|
|
//# 该方法没有返回值,但会根据删除的记录数更新数据库状态
|
|
|
|
count = db.delete(TABLE.NOTE,
|
|
|
|
count = db.delete(TABLE.NOTE,
|
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
|
|
|
//结束删除操作
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//处理URI_DATA情况
|
|
|
|
case URI_DATA:
|
|
|
|
case URI_DATA:
|
|
|
|
|
|
|
|
//执行数据表中的数据删除操作
|
|
|
|
count = db.delete(TABLE.DATA, selection, selectionArgs);
|
|
|
|
count = db.delete(TABLE.DATA, selection, selectionArgs);
|
|
|
|
|
|
|
|
//标记已删除数据
|
|
|
|
deleteData = true;
|
|
|
|
deleteData = true;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//处理URI_DATA_ITEM情况
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
|
|
|
//获取路径段中的ID
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
|
|
|
// 执行数据删除操作
|
|
|
|
count = db.delete(TABLE.DATA,
|
|
|
|
count = db.delete(TABLE.DATA,
|
|
|
|
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
|
|
|
////标记数据已删除
|
|
|
|
deleteData = true;
|
|
|
|
deleteData = true;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//如果URI不匹配任何已知的情况,则抛出异常
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
|
|
|
|
//抛出IllegalArgumentException,指出URI未知
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//如果计数大于0,表明有需要处理的数据
|
|
|
|
if (count > 0) {
|
|
|
|
if (count > 0) {
|
|
|
|
|
|
|
|
//如果deleteData标志为真,则需要删除数据
|
|
|
|
if (deleteData) {
|
|
|
|
if (deleteData) {
|
|
|
|
|
|
|
|
//通知内容提供者数据已更改,以便其他观察者可以响应这个更改
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//通知内容解析器(ContentResolver)数据已更改
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//返回计数结果
|
|
|
|
return count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
|
|
|
|
// 初始化更新计数为0
|
|
|
|
int count = 0;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
// 初始化ID为null,用于后续可能的单条目更新
|
|
|
|
String id = null;
|
|
|
|
String id = null;
|
|
|
|
|
|
|
|
// 获取可写的数据库实例
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
|
|
|
// 标记是否需要更新数据
|
|
|
|
boolean updateData = false;
|
|
|
|
boolean updateData = false;
|
|
|
|
|
|
|
|
// 根据URI匹配操作类型
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
case URI_NOTE:
|
|
|
|
case URI_NOTE:
|
|
|
|
|
|
|
|
//处理URI为URI_NOTE的情况,主要进行以下操作:
|
|
|
|
|
|
|
|
// 1. 调用increaseNoteVersion函数,将笔记版本号增加-1(可能是为了表示版本回退或删除)。
|
|
|
|
|
|
|
|
// 此处不涉及函数调用的详细逻辑,但表明了对笔记版本号的操作意图。
|
|
|
|
|
|
|
|
// 2. 使用从内容提供者传入的selection和selectionArgs参数来更新数据库中的笔记内容。
|
|
|
|
|
|
|
|
// 这里的db.update方法调用,说明了更新操作是在NOTE表上进行,更新的条件和新值由selection和values参数指定。
|
|
|
|
|
|
|
|
// 最后,将更新的行数赋值给count变量,以便后续处理或响应。
|
|
|
|
|
|
|
|
// """
|
|
|
|
increaseNoteVersion(-1, selection, selectionArgs);
|
|
|
|
increaseNoteVersion(-1, selection, selectionArgs);
|
|
|
|
count = db.update(TABLE.NOTE, values, selection, selectionArgs);
|
|
|
|
count = db.update(TABLE.NOTE, values, selection, selectionArgs);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//处理URI为NOTE_ITEM的情况
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
|
|
|
//获取URI路径中的ID段
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
|
|
|
//调用方法增加笔记版本,参数包括笔记ID、选择条件和选择条件参数
|
|
|
|
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
|
|
|
|
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
|
|
|
|
|
|
|
|
//更新数据库中的笔记记录,设置更新的值、条件和条件参数
|
|
|
|
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
|
|
|
|
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//处理URI_DATA情况
|
|
|
|
case URI_DATA:
|
|
|
|
case URI_DATA:
|
|
|
|
|
|
|
|
//更新数据库中的数据
|
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs);
|
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs);
|
|
|
|
|
|
|
|
//设置更新标志为true
|
|
|
|
updateData = true;
|
|
|
|
updateData = true;
|
|
|
|
|
|
|
|
//结束switch-case语句
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//处理URI_DATA_ITEM情况
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
|
|
|
//获取路径段中的ID
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
|
|
|
//根据ID更新数据库中的数据项
|
|
|
|
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
|
|
|
|
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
|
|
|
|
//设置更新标志为true
|
|
|
|
updateData = true;
|
|
|
|
updateData = true;
|
|
|
|
|
|
|
|
//结束处理
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
|
|
|
|
//默认情况下,如果URI不匹配任何已知的情况,则抛出异常
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
|
|
|
|
//抛出IllegalArgumentException异常,指出URI未知
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//如果存在要更新的数据
|
|
|
|
if (count > 0) {
|
|
|
|
if (count > 0) {
|
|
|
|
|
|
|
|
//如果updateData标志为真,则通知数据集发生变化,以便刷新显示笔记列表的界面
|
|
|
|
if (updateData) {
|
|
|
|
if (updateData) {
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//通知数据集发生变化,以便刷新显示特定笔记的界面
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//返回更新的笔记数量
|
|
|
|
return count;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//根据提供的选择条件字符串,生成附加的SQL查询条件。
|
|
|
|
|
|
|
|
// * 如果选择条件字符串不为空,则将其包裹在" AND ("和")"之间,以适应SQL查询的语法结构;
|
|
|
|
|
|
|
|
// * 否则,返回空字符串,表示不添加任何查询条件。
|
|
|
|
|
|
|
|
// * @param selection SQL查询的选择条件字符串,可能为空。
|
|
|
|
|
|
|
|
// * @return 返回格式化后的查询条件字符串,用于拼接SQL查询语句。
|
|
|
|
private String parseSelection(String selection) {
|
|
|
|
private String parseSelection(String selection) {
|
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//@param id 笔记的唯一标识符,用于定位要更新的笔记记录
|
|
|
|
|
|
|
|
// selection SQL查询的WHERE子句,用于指定更新条件
|
|
|
|
|
|
|
|
// selectionArgs 替换WHERE子句中问号占位符的实际值数组
|
|
|
|
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
|
|
|
|
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
|
|
|
|
|
|
|
|
//初始化一个StringBuilder对象,用于构建SQL语句
|
|
|
|
StringBuilder sql = new StringBuilder(120);
|
|
|
|
StringBuilder sql = new StringBuilder(120);
|
|
|
|
|
|
|
|
//构建UPDATE SQL语句的起始部分
|
|
|
|
sql.append("UPDATE ");
|
|
|
|
sql.append("UPDATE ");
|
|
|
|
sql.append(TABLE.NOTE);
|
|
|
|
sql.append(TABLE.NOTE);
|
|
|
|
sql.append(" SET ");
|
|
|
|
sql.append(" SET ");
|
|
|
|
sql.append(NoteColumns.VERSION);
|
|
|
|
sql.append(NoteColumns.VERSION);
|
|
|
|
sql.append("=" + NoteColumns.VERSION + "+1 ");
|
|
|
|
sql.append("=" + NoteColumns.VERSION + "+1 ");
|
|
|
|
|
|
|
|
//如果id大于0或者selection不为空,则在SQL语句中添加WHERE子句
|
|
|
|
if (id > 0 || !TextUtils.isEmpty(selection)) {
|
|
|
|
if (id > 0 || !TextUtils.isEmpty(selection)) {
|
|
|
|
sql.append(" WHERE ");
|
|
|
|
sql.append(" WHERE ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//如果id大于0,则构建一个SQL查询条件,指定笔记的ID等于给定的id值
|
|
|
|
if (id > 0) {
|
|
|
|
if (id > 0) {
|
|
|
|
sql.append(NoteColumns.ID + "=" + String.valueOf(id));
|
|
|
|
sql.append(NoteColumns.ID + "=" + String.valueOf(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//如果selection不为空,则进行查询条件的处理
|
|
|
|
if (!TextUtils.isEmpty(selection)) {
|
|
|
|
if (!TextUtils.isEmpty(selection)) {
|
|
|
|
|
|
|
|
//根据id是否大于0来决定是否解析selection,以适应不同的查询条件构建需求
|
|
|
|
String selectString = id > 0 ? parseSelection(selection) : selection;
|
|
|
|
String selectString = id > 0 ? parseSelection(selection) : selection;
|
|
|
|
|
|
|
|
//遍历selectionArgs中的每个参数,依次替换查询条件中的问号占位符
|
|
|
|
for (String args : selectionArgs) {
|
|
|
|
for (String args : selectionArgs) {
|
|
|
|
selectString = selectString.replaceFirst("\\?", args);
|
|
|
|
selectString = selectString.replaceFirst("\\?", args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 将处理后的查询条件字符串append到sql StringBuilder中,以构建最终的SQL查询语句
|
|
|
|
sql.append(selectString);
|
|
|
|
sql.append(selectString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//执行SQL语句
|
|
|
|
|
|
|
|
//# 该方法用于执行非查询操作的SQL语句,如CREATE TABLE、INSERT、UPDATE、DELETE等
|
|
|
|
|
|
|
|
//# 通过getWritableDatabase()方法获取SQLiteDatabase对象,该对象用于对数据库进行读写操作
|
|
|
|
|
|
|
|
//# 使用execSQL()方法执行SQL语句,该方法不返回查询结果,适用于不需要返回结果的操作
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//重写getType方法
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|