|
|
|
|
@ -91,6 +91,38 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
* Uri匹配类型:提供搜索建议(适配安卓SearchManager)
|
|
|
|
|
*/
|
|
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;
|
|
|
|
|
/**
|
|
|
|
|
* Uri匹配类型:查询/操作tag表的所有数据
|
|
|
|
|
*/
|
|
|
|
|
private static final int URI_TAG = 7;
|
|
|
|
|
/**
|
|
|
|
|
* Uri匹配类型:查询/操作tag表的单条数据(通过ID,如tag/1)
|
|
|
|
|
*/
|
|
|
|
|
private static final int URI_TAG_ITEM = 8;
|
|
|
|
|
/**
|
|
|
|
|
* Uri匹配类型:查询/操作note_tag_relation表的所有数据
|
|
|
|
|
*/
|
|
|
|
|
private static final int URI_NOTE_TAG_RELATION = 9;
|
|
|
|
|
/**
|
|
|
|
|
* Uri匹配类型:查询/操作note_tag_relation表的单条数据(通过ID,如note_tag_relation/1)
|
|
|
|
|
*/
|
|
|
|
|
private static final int URI_NOTE_TAG_RELATION_ITEM = 10;
|
|
|
|
|
/**
|
|
|
|
|
* Uri匹配类型:查询/操作加密信息表的所有数据
|
|
|
|
|
*/
|
|
|
|
|
private static final int URI_ENCRYPTION = 11;
|
|
|
|
|
/**
|
|
|
|
|
* Uri匹配类型:查询/操作加密信息表的单条数据(通过ID,如encryption/1)
|
|
|
|
|
*/
|
|
|
|
|
private static final int URI_ENCRYPTION_ITEM = 12;
|
|
|
|
|
/**
|
|
|
|
|
* Uri匹配类型:查询/操作密保问题表的所有数据
|
|
|
|
|
*/
|
|
|
|
|
private static final int URI_SECURITY_QUESTION = 13;
|
|
|
|
|
/**
|
|
|
|
|
* Uri匹配类型:查询/操作密保问题表的单条数据(通过ID,如security_question/1)
|
|
|
|
|
*/
|
|
|
|
|
private static final int URI_SECURITY_QUESTION_ITEM = 14;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
@ -114,6 +146,24 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST);
|
|
|
|
|
// 匹配搜索建议(带关键词):content://micode_notes/suggestions/query/关键词 -> URI_SEARCH_SUGGEST
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST);
|
|
|
|
|
// 匹配tag表所有数据:content://micode_notes/tag -> URI_TAG
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "tag", URI_TAG);
|
|
|
|
|
// 匹配tag表单条数据:content://micode_notes/tag/# -> URI_TAG_ITEM
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "tag/#", URI_TAG_ITEM);
|
|
|
|
|
// 匹配note_tag_relation表所有数据:content://micode_notes/note_tag_relation -> URI_NOTE_TAG_RELATION
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note_tag_relation", URI_NOTE_TAG_RELATION);
|
|
|
|
|
// 匹配note_tag_relation表单条数据:content://micode_notes/note_tag_relation/# -> URI_NOTE_TAG_RELATION_ITEM
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note_tag_relation/#", URI_NOTE_TAG_RELATION_ITEM);
|
|
|
|
|
|
|
|
|
|
// 匹配加密信息表所有数据:content://micode_notes/encryption -> URI_ENCRYPTION
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "encryption", URI_ENCRYPTION);
|
|
|
|
|
// 匹配加密信息表单条数据:content://micode_notes/encryption/# -> URI_ENCRYPTION_ITEM
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "encryption/#", URI_ENCRYPTION_ITEM);
|
|
|
|
|
|
|
|
|
|
// 匹配密保问题表所有数据:content://micode_notes/security_question -> URI_SECURITY_QUESTION
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "security_question", URI_SECURITY_QUESTION);
|
|
|
|
|
// 匹配密保问题表单条数据:content://micode_notes/security_question/# -> URI_SECURITY_QUESTION_ITEM
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "security_question/#", URI_SECURITY_QUESTION_ITEM);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -152,6 +202,15 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
public boolean onCreate() {
|
|
|
|
|
// 获取NotesDatabaseHelper的单例实例,上下文使用ContentProvider的上下文
|
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
|
|
|
|
|
|
// 确保标签相关的表结构存在
|
|
|
|
|
try {
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
mHelper.ensureTablesExist(db);
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
// 忽略异常,继续运行
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -295,6 +354,54 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_TAG:
|
|
|
|
|
// 查询tag表的所有数据
|
|
|
|
|
c = db.query(TABLE.TAG, projection, selection, selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_TAG_ITEM:
|
|
|
|
|
// 获取Uri中的ID
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 查询tag表的单条数据
|
|
|
|
|
c = db.query(TABLE.TAG, projection, "_id=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_NOTE_TAG_RELATION:
|
|
|
|
|
// 查询note_tag_relation表的所有数据
|
|
|
|
|
c = db.query(TABLE.NOTE_TAG_RELATION, projection, selection, selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_NOTE_TAG_RELATION_ITEM:
|
|
|
|
|
// 获取Uri中的ID
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 查询note_tag_relation表的单条数据
|
|
|
|
|
c = db.query(TABLE.NOTE_TAG_RELATION, projection, "_id=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_ENCRYPTION:
|
|
|
|
|
// 查询加密信息表的所有数据
|
|
|
|
|
c = db.query(TABLE.ENCRYPTION, projection, selection, selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_ENCRYPTION_ITEM:
|
|
|
|
|
// 获取Uri中的ID
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 查询加密信息表的单条数据
|
|
|
|
|
c = db.query(TABLE.ENCRYPTION, projection, "_id=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_SECURITY_QUESTION:
|
|
|
|
|
// 查询密保问题表的所有数据
|
|
|
|
|
c = db.query(TABLE.SECURITY_QUESTION, projection, selection, selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_SECURITY_QUESTION_ITEM:
|
|
|
|
|
// 获取Uri中的ID
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 查询密保问题表的单条数据
|
|
|
|
|
c = db.query(TABLE.SECURITY_QUESTION, projection, "_id=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// 未知Uri,抛出异常
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
@ -318,7 +425,7 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
|
// 获取可写的SQLiteDatabase对象(插入操作需要写权限)
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
long dataId = 0, noteId = 0, insertedId = 0; // 存储插入的ID
|
|
|
|
|
long dataId = 0, noteId = 0, insertedId = 0, tagId = 0, relationId = 0; // 存储插入的ID
|
|
|
|
|
|
|
|
|
|
// 根据Uri匹配的类型执行插入逻辑
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
@ -337,6 +444,39 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
// 插入data表,获取插入的ID
|
|
|
|
|
insertedId = dataId = db.insert(TABLE.DATA, null, values);
|
|
|
|
|
break;
|
|
|
|
|
case URI_TAG:
|
|
|
|
|
// 插入tag表,获取插入的ID
|
|
|
|
|
insertedId = tagId = db.insert(TABLE.TAG, null, values);
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_TAG_RELATION:
|
|
|
|
|
// 插入note_tag_relation表时,获取关联的noteId和tagId
|
|
|
|
|
if (values.containsKey(Notes.NoteTagRelationColumns.NOTE_ID)) {
|
|
|
|
|
noteId = values.getAsLong(Notes.NoteTagRelationColumns.NOTE_ID);
|
|
|
|
|
}
|
|
|
|
|
if (values.containsKey(Notes.NoteTagRelationColumns.TAG_ID)) {
|
|
|
|
|
tagId = values.getAsLong(Notes.NoteTagRelationColumns.TAG_ID);
|
|
|
|
|
}
|
|
|
|
|
// 插入note_tag_relation表,获取插入的ID
|
|
|
|
|
insertedId = relationId = db.insert(TABLE.NOTE_TAG_RELATION, null, values);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_ENCRYPTION:
|
|
|
|
|
// 插入加密信息表时,获取关联的noteId
|
|
|
|
|
if (values.containsKey(Notes.EncryptionColumns.NOTE_ID)) {
|
|
|
|
|
noteId = values.getAsLong(Notes.EncryptionColumns.NOTE_ID);
|
|
|
|
|
}
|
|
|
|
|
// 插入加密信息表,获取插入的ID
|
|
|
|
|
insertedId = db.insert(TABLE.ENCRYPTION, null, values);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_SECURITY_QUESTION:
|
|
|
|
|
// 插入密保问题表时,获取关联的noteId
|
|
|
|
|
if (values.containsKey(Notes.SecurityQuestionColumns.NOTE_ID)) {
|
|
|
|
|
noteId = values.getAsLong(Notes.SecurityQuestionColumns.NOTE_ID);
|
|
|
|
|
}
|
|
|
|
|
// 插入密保问题表,获取插入的ID
|
|
|
|
|
insertedId = db.insert(TABLE.SECURITY_QUESTION, null, values);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// 未知Uri,抛出异常
|
|
|
|
|
@ -355,7 +495,17 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送通知:tag表数据变更,通知对应的Uri
|
|
|
|
|
if (tagId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_TAG_URI, tagId), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送通知:note_tag_relation表数据变更,通知对应的Uri
|
|
|
|
|
if (relationId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_TAG_RELATION_URI, relationId), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回包含插入ID的新Uri
|
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
|
@ -377,6 +527,7 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
boolean deleteData = false; // 标记是否删除的是data表数据
|
|
|
|
|
long noteId = 0; // 用于存储便签ID,以便发送通知
|
|
|
|
|
long tagId = 0; // 用于存储标签ID,以便发送通知
|
|
|
|
|
|
|
|
|
|
// 根据Uri匹配的类型执行删除逻辑
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
@ -410,8 +561,47 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
deleteData = true;
|
|
|
|
|
break;
|
|
|
|
|
case URI_TAG:
|
|
|
|
|
// 删除tag表数据
|
|
|
|
|
count = db.delete(TABLE.TAG, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_TAG_ITEM:
|
|
|
|
|
// 获取Uri中的ID,删除tag表单条数据
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
tagId = Long.valueOf(id);
|
|
|
|
|
count = db.delete(TABLE.TAG, "_id=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_TAG_RELATION:
|
|
|
|
|
// 删除note_tag_relation表数据
|
|
|
|
|
count = db.delete(TABLE.NOTE_TAG_RELATION, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_TAG_RELATION_ITEM:
|
|
|
|
|
// 获取Uri中的ID,删除note_tag_relation表单条数据
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
count = db.delete(TABLE.NOTE_TAG_RELATION, "_id=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_ENCRYPTION:
|
|
|
|
|
// 删除加密信息表数据
|
|
|
|
|
count = db.delete(TABLE.ENCRYPTION, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_ENCRYPTION_ITEM:
|
|
|
|
|
// 获取Uri中的ID,删除加密信息表单条数据
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
count = db.delete(TABLE.ENCRYPTION, "_id=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_SECURITY_QUESTION:
|
|
|
|
|
// 删除密保问题表数据
|
|
|
|
|
count = db.delete(TABLE.SECURITY_QUESTION, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_SECURITY_QUESTION_ITEM:
|
|
|
|
|
// 获取Uri中的ID,删除密保问题表单条数据
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
count = db.delete(TABLE.SECURITY_QUESTION, "_id=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
@ -428,6 +618,11 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
|
|
|
}
|
|
|
|
|
// 如果是标签相关操作,通知对应的标签Uri
|
|
|
|
|
if (tagId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_TAG_URI, tagId), null);
|
|
|
|
|
}
|
|
|
|
|
// 通知当前Uri的数据变更
|
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
}
|
|
|
|
|
@ -451,6 +646,7 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
boolean updateData = false; // 标记是否更新的是data表数据
|
|
|
|
|
long noteId = 0; // 用于存储便签ID,以便发送通知
|
|
|
|
|
long tagId = 0; // 用于存储标签ID,以便发送通知
|
|
|
|
|
|
|
|
|
|
// 根据Uri匹配的类型执行更新逻辑
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
@ -479,8 +675,47 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
|
updateData = true;
|
|
|
|
|
break;
|
|
|
|
|
case URI_TAG:
|
|
|
|
|
// 更新tag表数据
|
|
|
|
|
count = db.update(TABLE.TAG, values, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_TAG_ITEM:
|
|
|
|
|
// 获取Uri中的ID,更新tag表单条数据
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
tagId = Long.valueOf(id);
|
|
|
|
|
count = db.update(TABLE.TAG, values, "_id=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_TAG_RELATION:
|
|
|
|
|
// 更新note_tag_relation表数据
|
|
|
|
|
count = db.update(TABLE.NOTE_TAG_RELATION, values, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_TAG_RELATION_ITEM:
|
|
|
|
|
// 获取Uri中的ID,更新note_tag_relation表单条数据
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
count = db.update(TABLE.NOTE_TAG_RELATION, values, "_id=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_ENCRYPTION:
|
|
|
|
|
// 更新加密信息表数据
|
|
|
|
|
count = db.update(TABLE.ENCRYPTION, values, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_ENCRYPTION_ITEM:
|
|
|
|
|
// 获取Uri中的ID,更新加密信息表单条数据
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
count = db.update(TABLE.ENCRYPTION, values, "_id=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_SECURITY_QUESTION:
|
|
|
|
|
// 更新密保问题表数据
|
|
|
|
|
count = db.update(TABLE.SECURITY_QUESTION, values, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case URI_SECURITY_QUESTION_ITEM:
|
|
|
|
|
// 获取Uri中的ID,更新密保问题表单条数据
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
count = db.update(TABLE.SECURITY_QUESTION, values, "_id=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
@ -497,6 +732,11 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
|
|
|
}
|
|
|
|
|
// 如果是标签相关操作,通知对应的标签Uri
|
|
|
|
|
if (tagId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_TAG_URI, tagId), null);
|
|
|
|
|
}
|
|
|
|
|
// 通知当前Uri的数据变更
|
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
}
|