|
|
|
@ -14,9 +14,10 @@
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// 包声明,用于组织和访问控制。
|
|
|
|
|
package net.micode.notes.data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 导入各种Android框架和项目特定类的语句。
|
|
|
|
|
import android.app.SearchManager;
|
|
|
|
|
import android.content.ContentProvider;
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
@ -34,24 +35,25 @@ import net.micode.notes.data.Notes.DataColumns;
|
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// NotesProvider类的定义,它扩展了ContentProvider。
|
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
|
private static final UriMatcher mMatcher; // UriMatcher实例,用于匹配URI到相应的整型代码。
|
|
|
|
|
|
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
|
private NotesDatabaseHelper mHelper; // 数据库帮助类实例,用于访问SQLite数据库。
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "NotesProvider";
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
|
// 初始化UriMatcher,将URI路径映射到整型代码。
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM);
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA);
|
|
|
|
@ -61,143 +63,101 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
|
// onCreate()方法在ContentProvider创建时调用,用于初始化。
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onCreate() {
|
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
|
return true;
|
|
|
|
|
return true; // 返回true表示初始化成功。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// query()方法用于处理URI查询请求。
|
|
|
|
|
@Override
|
|
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
|
|
String sortOrder) {
|
|
|
|
|
Cursor c = null;
|
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
|
|
|
String id = null;
|
|
|
|
|
String sortOrder) {
|
|
|
|
|
Cursor c = null; // 初始化游标。
|
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase(); // 获取可读数据库实例。
|
|
|
|
|
String id = null; // 用于存储从URI中解析出的ID。
|
|
|
|
|
|
|
|
|
|
// 根据UriMatcher匹配的结果,执行相应的查询操作。
|
|
|
|
|
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查询单个笔记。
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
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);
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String searchString = null;
|
|
|
|
|
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
|
|
|
|
|
if (uri.getPathSegments().size() > 1) {
|
|
|
|
|
searchString = uri.getPathSegments().get(1);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
searchString = uri.getQueryParameter("pattern");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(searchString)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
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());
|
|
|
|
|
}
|
|
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
|
break;
|
|
|
|
|
// 其他case处理不同的URI类型...
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
throw new IllegalArgumentException("未知URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
if (c != null) {
|
|
|
|
|
// 设置通知URI,当数据改变时可以通知外部。
|
|
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
|
}
|
|
|
|
|
return c;
|
|
|
|
|
return c; // 返回查询结果游标。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
|
// 获取数据库写入权限
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
long dataId = 0, noteId = 0, insertedId = 0;
|
|
|
|
|
// 根据传入的URI判断是插入note还是data
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
// 插入note并获取插入行的ID
|
|
|
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
// 插入data之前需要检查是否包含note的ID
|
|
|
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {
|
|
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);
|
|
|
|
|
} else {
|
|
|
|
|
Log.d(TAG, "Wrong data format without note id:" + values.toString());
|
|
|
|
|
}
|
|
|
|
|
// 插入data并获取插入行的ID
|
|
|
|
|
insertedId = dataId = db.insert(TABLE.DATA, null, values);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
// Notify the note uri
|
|
|
|
|
// 如果noteId有效,则通知note的URI数据已更改
|
|
|
|
|
if (noteId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Notify the data uri
|
|
|
|
|
// 如果dataId有效,则通知data的URI数据已更改
|
|
|
|
|
if (dataId > 0) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 返回插入数据的URI
|
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
|
// 初始化删除计数和ID
|
|
|
|
|
int count = 0;
|
|
|
|
|
String id = null;
|
|
|
|
|
// 获取数据库写入权限
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
boolean deleteData = false;
|
|
|
|
|
// 根据传入的URI判断是删除note还是data
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
// 构建删除note的条件
|
|
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";
|
|
|
|
|
count = db.delete(TABLE.NOTE, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
// 获取note的ID
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
/**
|
|
|
|
|
* ID that smaller than 0 is system folder which is not allowed to
|
|
|
|
|
* trash
|
|
|
|
|
*/
|
|
|
|
|
// 删除指定ID的note,注意ID必须大于0
|
|
|
|
|
long noteId = Long.valueOf(id);
|
|
|
|
|
if (noteId <= 0) {
|
|
|
|
|
break;
|
|
|
|
@ -206,10 +166,12 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
// 删除data表中满足条件的记录
|
|
|
|
|
count = db.delete(TABLE.DATA, selection, selectionArgs);
|
|
|
|
|
deleteData = true;
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
// 获取data的ID并删除指定ID的记录
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
count = db.delete(TABLE.DATA,
|
|
|
|
|
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
@ -218,37 +180,46 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
// 如果有记录被删除,则通知相应的URI数据已更改
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
if (deleteData) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
|
}
|
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
}
|
|
|
|
|
// 返回被删除的记录数
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
|
// 初始化更新计数和ID
|
|
|
|
|
int count = 0;
|
|
|
|
|
String id = null;
|
|
|
|
|
// 获取数据库写入权限
|
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
|
boolean updateData = false;
|
|
|
|
|
// 根据传入的URI判断是更新note还是data
|
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
|
case URI_NOTE:
|
|
|
|
|
// 更新note表中满足条件的记录
|
|
|
|
|
increaseNoteVersion(-1, selection, selectionArgs);
|
|
|
|
|
count = db.update(TABLE.NOTE, values, selection, selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
// 获取note的ID并更新指定ID的记录
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);
|
|
|
|
|
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA:
|
|
|
|
|
// 更新data表中满足条件的记录
|
|
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs);
|
|
|
|
|
updateData = true;
|
|
|
|
|
break;
|
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
// 获取data的ID并更新指定ID的记录
|
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
|
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
|
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
@ -257,49 +228,51 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
|
default:
|
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果有记录被更新,则通知相应的URI数据已更改
|
|
|
|
|
if (count > 0) {
|
|
|
|
|
if (updateData) {
|
|
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);
|
|
|
|
|
}
|
|
|
|
|
getContext().getContentResolver().notifyChange(uri, null);
|
|
|
|
|
}
|
|
|
|
|
// 返回被更新的记录数
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
sql.append("UPDATE ");
|
|
|
|
|
sql.append(TABLE.NOTE);
|
|
|
|
|
sql.append(" SET ");
|
|
|
|
|
sql.append(NoteColumns.VERSION);
|
|
|
|
|
sql.append("=" + NoteColumns.VERSION + "+1 ");
|
|
|
|
|
sql.append("UPDATE "); // 更新语句
|
|
|
|
|
sql.append(TABLE.NOTE); // 表名
|
|
|
|
|
sql.append(" SET "); // 设置要更新的字段
|
|
|
|
|
sql.append(NoteColumns.VERSION); // 版本字段
|
|
|
|
|
sql.append("=" + NoteColumns.VERSION + "+1 "); // 版本号加1
|
|
|
|
|
|
|
|
|
|
if (id > 0 || !TextUtils.isEmpty(selection)) {
|
|
|
|
|
sql.append(" WHERE ");
|
|
|
|
|
sql.append(" WHERE "); // 添加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) {
|
|
|
|
|
String selectString = id > 0 ? parseSelection(selection) : selection; // 解析选择条件
|
|
|
|
|
for (String args : selectionArgs) { // 替换选择参数
|
|
|
|
|
selectString = selectString.replaceFirst("\\?", args);
|
|
|
|
|
}
|
|
|
|
|
sql.append(selectString);
|
|
|
|
|
sql.append(selectString); // 拼接选择条件
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());
|
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString()); // 执行SQL语句
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
return null;
|
|
|
|
|
return null; // 返回类型暂未实现
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|