|
|
|
|
@ -16,7 +16,6 @@
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.SearchManager;
|
|
|
|
|
import android.content.ContentProvider;
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
|
@ -34,123 +33,144 @@ import net.micode.notes.data.Notes.DataColumns;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
|
|
// 定义 URI 匹配器,用来将传入的 URI 路径与预定义的常量匹配
|
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
|
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_SEARCH = 5;
|
|
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;
|
|
|
|
|
/**
|
|
|
|
|
* 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 + ","
|
|
|
|
|
+ "TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + ","
|
|
|
|
|
+ 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;
|
|
|
|
|
|
|
|
|
|
// 定义常量,用于标识不同类型的 URI 请求
|
|
|
|
|
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_SEARCH = 5; // 搜索笔记
|
|
|
|
|
private static final int URI_SEARCH_SUGGEST = 6; // 搜索建议
|
|
|
|
|
|
|
|
|
|
// 定义搜索时返回的列,主要包括笔记 ID、笔记内容片段等
|
|
|
|
|
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 + "," +
|
|
|
|
|
"TRIM(REPLACE(" + NoteColumns.SNIPPET + ", x'0A','')) AS " + SearchManager.SUGGEST_COLUMN_TEXT_2 + "," +
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
// 定义搜索时的 SQL 查询语句,用于匹配包含特定关键词的笔记内容
|
|
|
|
|
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; // 只查询普通笔记类型
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM);
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA);
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM);
|
|
|
|
|
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 = new UriMatcher(UriMatcher.NO_MATCH); // 初始化 URI 匹配器
|
|
|
|
|
|
|
|
|
|
// 为不同的 URI 路径添加匹配规则
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE); // 查询所有笔记
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM); // 查询单个笔记
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA); // 查询所有数据
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM); // 查询单个数据
|
|
|
|
|
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); // 搜索建议
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NotesDatabaseHelper 是数据库的辅助类,用来管理数据库的创建和版本更新
|
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
|
|
|
|
|
|
// onCreate 方法,在 ContentProvider 创建时调用,用来初始化数据库助手
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onCreate() {
|
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
|
return true;
|
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext()); // 获取数据库助手实例
|
|
|
|
|
return true; // 表示 ContentProvider 成功创建
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询操作,根据 URI 的不同,查询不同的数据
|
|
|
|
|
@Override
|
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
|
String sortOrder) {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
|
|
|
Cursor c = null; // 定义一个 Cursor,用于返回查询结果
|
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase(); // 获取可读的 SQLite 数据库
|
|
|
|
|
String id = null;
|
|
|
|
|
|
|
|
|
|
// 根据匹配的 URI 执行不同的查询操作
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
|
|
sortOrder);
|
|
|
|
|
// 查询所有笔记
|
|
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 查询单个笔记
|
|
|
|
|
id = uri.getPathSegments().get(1); // 获取 URI 中的笔记 ID
|
|
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
|
|
|
|
|
sortOrder);
|
|
|
|
|
// 查询所有数据
|
|
|
|
|
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 查询单个数据
|
|
|
|
|
id = uri.getPathSegments().get(1); // 获取 URI 中的数据 ID
|
|
|
|
|
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
+ 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");
|
|
|
|
|
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String searchString = null;
|
|
|
|
|
String searchString = null; // 搜索的关键词
|
|
|
|
|
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
|
|
|
|
|
// 如果是搜索建议,获取 URI 中的搜索词
|
|
|
|
|
if (uri.getPathSegments().size() > 1) {
|
|
|
|
|
searchString = uri.getPathSegments().get(1);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 如果是普通搜索,获取查询参数中的 "pattern"
|
|
|
|
|
searchString = uri.getQueryParameter("pattern");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果搜索字符串为空,返回 null
|
|
|
|
|
if (TextUtils.isEmpty(searchString)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
searchString = String.format("%%%s%%", searchString);
|
|
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
|
|
|
|
|
new String[]{searchString});
|
|
|
|
|
searchString = String.format("%%%s%%", searchString); // 模糊匹配搜索词
|
|
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY, new String[]{searchString});
|
|
|
|
|
} catch (IllegalStateException ex) {
|
|
|
|
|
Log.e(TAG, "got exception: " + ex.toString());
|
|
|
|
|
Log.e(TAG, "got exception: " + ex.toString()); // 如果查询出错,打印错误日志
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
return c; // 返回查询结果
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 插入数据操作,根据 URI 判断插入的表
|
|
|
|
|
@Override
|
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase(); // 获取可写的 SQLite 数据库
|
|
|
|
|
long dataId = 0, noteId = 0, insertedId = 0;
|
|
|
|
|
|
|
|
|
|
// 根据 URI 匹配的结果执行不同的插入操作
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
// 插入一条笔记数据
|
|
|
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
// 插入数据,首先确保 values 中包含 Note ID
|
|
|
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {
|
|
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);
|
|
|
|
|
} else {
|
|
|
|
|
@ -159,142 +179,157 @@ 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) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Notify the data uri
|
|
|
|
|
// 通知更新数据
|
|
|
|
|
if (dataId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId); // 返回插入数据的 URI
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除数据操作,根据 URI 匹配执行不同的删除操作
|
|
|
|
|
@Override
|
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
|
int count = 0;
|
|
|
|
|
int count = 0; // 记录删除的行数
|
|
|
|
|
String id = null;
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase(); // 获取可写的 SQLite 数据库
|
|
|
|
|
boolean deleteData = false;
|
|
|
|
|
|
|
|
|
|
// 根据 URI 匹配结果执行不同的删除操作
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";
|
|
|
|
|
count = db.delete(TABLE.NOTE, selection, selectionArgs);
|
|
|
|
|
// 删除所有笔记
|
|
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 "; // 添加 ID 限制条件
|
|
|
|
|
count = db.delete(TABLE.NOTE, selection, selectionArgs); // 删除数据
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
/**
|
|
|
|
|
* ID that smaller than 0 is system folder which is not allowed to
|
|
|
|
|
* trash
|
|
|
|
|
*/
|
|
|
|
|
// 删除单个笔记
|
|
|
|
|
id = uri.getPathSegments().get(1); // 获取笔记 ID
|
|
|
|
|
long noteId = Long.valueOf(id);
|
|
|
|
|
if (noteId <= 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
count = db.delete(TABLE.NOTE,
|
|
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs); // 删除指定笔记
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
count = db.delete(TABLE.DATA, selection, selectionArgs);
|
|
|
|
|
// 删除所有数据
|
|
|
|
|
count = db.delete(TABLE.DATA, selection, selectionArgs); // 删除数据
|
|
|
|
|
deleteData = true;
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 删除单个数据
|
|
|
|
|
id = uri.getPathSegments().get(1); // 获取数据 ID
|
|
|
|
|
count = db.delete(TABLE.DATA,
|
|
|
|
|
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs); // 删除指定数据
|
|
|
|
|
deleteData = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri); // 无效 URI,抛出异常
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 删除成功后,通知相应的数据变化
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
if (deleteData) {
|
|
|
|
|
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; // 返回删除的行数
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新数据操作,根据 URI 匹配执行不同的更新操作
|
|
|
|
|
@Override
|
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
|
int count = 0;
|
|
|
|
|
String id = null;
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase(); // 获取可写的 SQLite 数据库
|
|
|
|
|
boolean updateData = false;
|
|
|
|
|
|
|
|
|
|
// 根据 URI 匹配的结果执行不同的更新操作
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
increaseNoteVersion(-1, selection, selectionArgs);
|
|
|
|
|
count = db.update(TABLE.NOTE, values, selection, selectionArgs);
|
|
|
|
|
// 更新所有笔记
|
|
|
|
|
increaseNoteVersion(-1, selection, selectionArgs); // 增加版本号
|
|
|
|
|
count = db.update(TABLE.NOTE, values, selection, selectionArgs); // 更新笔记数据
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
|
|
|
|
|
// 更新单个笔记
|
|
|
|
|
id = uri.getPathSegments().get(1); // 获取笔记 ID
|
|
|
|
|
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs); // 增加指定笔记的版本号
|
|
|
|
|
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
|
+ parseSelection(selection), selectionArgs); // 更新指定笔记
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs);
|
|
|
|
|
// 更新所有数据
|
|
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs); // 更新数据
|
|
|
|
|
updateData = true;
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 更新单个数据
|
|
|
|
|
id = uri.getPathSegments().get(1); // 获取数据 ID
|
|
|
|
|
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
|
+ parseSelection(selection), selectionArgs); // 更新指定数据
|
|
|
|
|
updateData = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri); // 无效 URI,抛出异常
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 更新成功后,通知相应的数据变化
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
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 查询的 WHERE 子句
|
|
|
|
|
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);
|
|
|
|
|
StringBuilder sql = new StringBuilder(120); // 使用 StringBuilder 构建 SQL 查询
|
|
|
|
|
sql.append("UPDATE ");
|
|
|
|
|
sql.append(TABLE.NOTE);
|
|
|
|
|
sql.append(TABLE.NOTE); // 更新笔记表
|
|
|
|
|
sql.append(" SET ");
|
|
|
|
|
sql.append(NoteColumns.VERSION);
|
|
|
|
|
sql.append("=" + NoteColumns.VERSION + "+1 ");
|
|
|
|
|
sql.append(NoteColumns.VERSION); // 更新版本号字段
|
|
|
|
|
sql.append("=" + NoteColumns.VERSION + "+1 "); // 将版本号加1
|
|
|
|
|
|
|
|
|
|
if (id > 0 || !TextUtils.isEmpty(selection)) {
|
|
|
|
|
sql.append(" WHERE ");
|
|
|
|
|
sql.append(" WHERE "); // 如果指定了 ID 或者其他选择条件,添加 WHERE 子句
|
|
|
|
|
}
|
|
|
|
|
if (id > 0) {
|
|
|
|
|
sql.append(NoteColumns.ID + "=" + String.valueOf(id));
|
|
|
|
|
sql.append(NoteColumns.ID + "=" + String.valueOf(id)); // 如果指定了笔记 ID,更新对应笔记
|
|
|
|
|
}
|
|
|
|
|
if (!TextUtils.isEmpty(selection)) {
|
|
|
|
|
String selectString = id > 0 ? parseSelection(selection) : selection;
|
|
|
|
|
for (String args : selectionArgs) {
|
|
|
|
|
selectString = selectString.replaceFirst("\\?", args);
|
|
|
|
|
selectString = selectString.replaceFirst("\\?", args); // 替换选择条件中的占位符
|
|
|
|
|
}
|
|
|
|
|
sql.append(selectString);
|
|
|
|
|
sql.append(selectString); // 添加选择条件
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());
|
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString()); // 执行 SQL 更新语句
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取 MIME 类型,当前未实现,直接返回 null
|
|
|
|
|
@Override
|
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|