|
|
|
|
@ -34,36 +34,48 @@ import net.micode.notes.data.Notes.DataColumns;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 便签内容提供者类
|
|
|
|
|
*
|
|
|
|
|
* 实现ContentProvider接口,负责管理便签应用的数据访问和操作。
|
|
|
|
|
* 提供URI匹配、CRUD操作和搜索功能。是应用数据层的关键组件,
|
|
|
|
|
* 使其他组件和应用能够以统一方式访问便签数据。
|
|
|
|
|
*/
|
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
|
|
// URI匹配器,用于区分不同的URI请求
|
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
|
|
|
|
|
|
// 数据库帮助类实例
|
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
// 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; // 搜索建议
|
|
|
|
|
|
|
|
|
|
// 静态初始化块,设置URI匹配规则
|
|
|
|
|
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);
|
|
|
|
|
// 添加URI匹配规则
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE); // 匹配所有便签
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM); // 匹配特定ID的便签
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA); // 匹配所有数据
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM); // 匹配特定ID的数据
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH); // 匹配搜索URI
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST); // 匹配搜索建议URI
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST); // 匹配带参数的搜索建议URI
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
* 搜索结果的投影列
|
|
|
|
|
*
|
|
|
|
|
* x'0A' 表示SQLite中的'\n'字符。对于搜索结果中的标题和内容,
|
|
|
|
|
* 我们将去除'\n'和空格以显示更多信息。
|
|
|
|
|
*/
|
|
|
|
|
private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + ","
|
|
|
|
|
+ NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + ","
|
|
|
|
|
@ -73,45 +85,76 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
+ "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + ","
|
|
|
|
|
+ "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 便签片段搜索查询SQL
|
|
|
|
|
*
|
|
|
|
|
* 构建搜索便签内容的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;
|
|
|
|
|
+ " WHERE " + NoteColumns.SNIPPET + " LIKE ?" // 使用LIKE进行模糊匹配
|
|
|
|
|
+ " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER // 排除回收站中的便签
|
|
|
|
|
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE; // 只搜索便签类型
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ContentProvider创建时调用
|
|
|
|
|
* 初始化数据库帮助类
|
|
|
|
|
*
|
|
|
|
|
* @return 初始化是否成功
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onCreate() {
|
|
|
|
|
// 获取数据库帮助类单例
|
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询数据方法
|
|
|
|
|
* 处理不同类型URI的查询请求
|
|
|
|
|
*
|
|
|
|
|
* @param uri 查询URI
|
|
|
|
|
* @param projection 要返回的列
|
|
|
|
|
* @param selection 查询条件
|
|
|
|
|
* @param selectionArgs 查询参数
|
|
|
|
|
* @param sortOrder 排序方式
|
|
|
|
|
* @return 查询结果游标
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
|
String sortOrder) {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
// 获取可读数据库
|
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
|
|
|
String id = null;
|
|
|
|
|
|
|
|
|
|
// 根据URI类型执行不同的查询
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
// 查询所有便签
|
|
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
|
|
sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 查询特定ID的便签
|
|
|
|
|
id = uri.getPathSegments().get(1); // 从URI路径获取ID
|
|
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
// 查询所有便签数据
|
|
|
|
|
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
|
|
|
|
|
sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
// 查询特定ID的便签数据
|
|
|
|
|
id = uri.getPathSegments().get(1); // 从URI路径获取ID
|
|
|
|
|
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");
|
|
|
|
|
@ -119,19 +162,23 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
|
|
|
|
|
String searchString = null;
|
|
|
|
|
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);
|
|
|
|
|
// 格式化搜索字符串为SQL LIKE模式
|
|
|
|
|
searchString = String.format("%%%s%%", searchString); // 添加%用于模糊匹配
|
|
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
|
|
|
|
|
new String[] { searchString });
|
|
|
|
|
} catch (IllegalStateException ex) {
|
|
|
|
|
@ -141,23 +188,38 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 设置内容变更通知
|
|
|
|
|
if (c != null) {
|
|
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
|
}
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 插入数据方法
|
|
|
|
|
* 处理便签和便签数据的插入操作
|
|
|
|
|
*
|
|
|
|
|
* @param uri 插入URI
|
|
|
|
|
* @param values 要插入的值
|
|
|
|
|
* @return 插入后的URI
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
|
// 获取可写数据库
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
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:
|
|
|
|
|
// 插入便签数据,需要关联到便签
|
|
|
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {
|
|
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);
|
|
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID); // 获取关联的便签ID
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "Wrong data format without note id:" + values.toString());
|
|
|
|
|
}
|
|
|
|
|
@ -166,140 +228,209 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
// Notify the note uri
|
|
|
|
|
|
|
|
|
|
// 通知便签内容变更
|
|
|
|
|
if (noteId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回插入项的URI
|
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除数据方法
|
|
|
|
|
* 处理便签和便签数据的删除操作
|
|
|
|
|
*
|
|
|
|
|
* @param uri 删除URI
|
|
|
|
|
* @param selection 删除条件
|
|
|
|
|
* @param selectionArgs 删除参数
|
|
|
|
|
* @return 删除的行数
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
|
int count = 0;
|
|
|
|
|
String id = null;
|
|
|
|
|
// 获取可写数据库
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
boolean deleteData = false;
|
|
|
|
|
|
|
|
|
|
// 根据URI类型执行不同的删除操作
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
// 删除便签,ID>0是为了保护系统文件夹
|
|
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";
|
|
|
|
|
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小于0的是系统文件夹,不允许删除
|
|
|
|
|
*/
|
|
|
|
|
long noteId = Long.valueOf(id);
|
|
|
|
|
if (noteId <= 0) {
|
|
|
|
|
break;
|
|
|
|
|
Long noteId = Long.valueOf(id);
|
|
|
|
|
if (noteId > 0) {
|
|
|
|
|
count = db.delete(TABLE.NOTE,
|
|
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "Skip delete system folder");
|
|
|
|
|
}
|
|
|
|
|
count = db.delete(TABLE.NOTE,
|
|
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
// 删除便签数据
|
|
|
|
|
count = db.delete(TABLE.DATA, selection, selectionArgs);
|
|
|
|
|
deleteData = true;
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
count = db.delete(TABLE.DATA,
|
|
|
|
|
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
deleteData = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 通知内容变更
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
if (deleteData) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
|
}
|
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新数据方法
|
|
|
|
|
* 处理便签和便签数据的更新操作
|
|
|
|
|
*
|
|
|
|
|
* @param uri 更新URI
|
|
|
|
|
* @param values 要更新的值
|
|
|
|
|
* @param selection 更新条件
|
|
|
|
|
* @param selectionArgs 更新参数
|
|
|
|
|
* @return 更新的行数
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
|
int count = 0;
|
|
|
|
|
String id = null;
|
|
|
|
|
// 获取可写数据库
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
boolean updateData = false;
|
|
|
|
|
|
|
|
|
|
// 根据URI类型执行不同的更新操作
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
increaseNoteVersion(-1, 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);
|
|
|
|
|
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 = uri.getPathSegments().get(1);
|
|
|
|
|
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
|
updateData = true;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 通知内容变更
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
if (updateData) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
|
}
|
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 解析选择条件
|
|
|
|
|
* 将已有条件和新条件合并
|
|
|
|
|
*
|
|
|
|
|
* @param selection 原始选择条件
|
|
|
|
|
* @return 合并后的选择条件
|
|
|
|
|
*/
|
|
|
|
|
private String parseSelection(String selection) {
|
|
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
|
|
return TextUtils.isEmpty(selection) ? "" : " AND (" + selection + ")";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 增加便签版本号
|
|
|
|
|
* 便签变更时更新版本号,用于同步
|
|
|
|
|
*
|
|
|
|
|
* @param id 便签ID,-1表示多个便签
|
|
|
|
|
* @param selection 选择条件
|
|
|
|
|
* @param selectionArgs 选择参数
|
|
|
|
|
*/
|
|
|
|
|
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
|
|
|
|
|
// 获取可写数据库
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
StringBuilder sql = new StringBuilder(120);
|
|
|
|
|
|
|
|
|
|
// 构建更新SQL语句
|
|
|
|
|
sql.append("UPDATE ");
|
|
|
|
|
sql.append(TABLE.NOTE);
|
|
|
|
|
sql.append(" SET ");
|
|
|
|
|
sql.append(NoteColumns.VERSION);
|
|
|
|
|
sql.append("=" + NoteColumns.VERSION + "+1 ");
|
|
|
|
|
|
|
|
|
|
if (id > 0 || !TextUtils.isEmpty(selection)) {
|
|
|
|
|
sql.append("=");
|
|
|
|
|
sql.append(NoteColumns.VERSION);
|
|
|
|
|
sql.append("+1 ");
|
|
|
|
|
|
|
|
|
|
// 根据ID或条件构建WHERE子句
|
|
|
|
|
if (id != -1) {
|
|
|
|
|
sql.append(" WHERE ");
|
|
|
|
|
}
|
|
|
|
|
if (id > 0) {
|
|
|
|
|
sql.append(NoteColumns.ID + "=" + String.valueOf(id));
|
|
|
|
|
}
|
|
|
|
|
if (!TextUtils.isEmpty(selection)) {
|
|
|
|
|
String selectString = id > 0 ? parseSelection(selection) : selection;
|
|
|
|
|
for (String args : selectionArgs) {
|
|
|
|
|
selectString = selectString.replaceFirst("\\?", args);
|
|
|
|
|
sql.append(NoteColumns.ID);
|
|
|
|
|
sql.append("=");
|
|
|
|
|
sql.append(String.valueOf(id));
|
|
|
|
|
if (!TextUtils.isEmpty(selection)) {
|
|
|
|
|
sql.append(parseSelection(selection));
|
|
|
|
|
}
|
|
|
|
|
db.execSQL(sql.toString());
|
|
|
|
|
} else if (!TextUtils.isEmpty(selection)) {
|
|
|
|
|
sql.append(" WHERE ");
|
|
|
|
|
sql.append("(");
|
|
|
|
|
sql.append(selection);
|
|
|
|
|
sql.append(")");
|
|
|
|
|
if (selectionArgs == null) {
|
|
|
|
|
db.execSQL(sql.toString());
|
|
|
|
|
} else {
|
|
|
|
|
db.execSQL(sql.toString(), selectionArgs);
|
|
|
|
|
}
|
|
|
|
|
sql.append(selectString);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取内容类型
|
|
|
|
|
* 根据URI返回MIME类型
|
|
|
|
|
*
|
|
|
|
|
* @param uri 请求的URI
|
|
|
|
|
* @return MIME类型字符串
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
return null;
|
|
|
|
|
// 根据URI类型返回不同的MIME类型
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
return Notes.NoteColumns.CONTENT_TYPE;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
return Notes.NoteColumns.CONTENT_ITEM_TYPE;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
return Notes.DataColumns.CONTENT_TYPE;
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
return Notes.DataColumns.CONTENT_ITEM_TYPE;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|