|
|
@ -14,42 +14,51 @@
|
|
|
|
* limitations under the License.
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.data;
|
|
|
|
package net.micode.notes.data; // 定义这个Java类的包名,表示这个类属于net.micode.notes.data包
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.SearchManager; // 导入Android的SearchManager类,用于管理搜索功能
|
|
|
|
|
|
|
|
import android.content.ContentProvider; // 导入Android的ContentProvider类,用于提供数据
|
|
|
|
|
|
|
|
import android.content.ContentUris; // 导入Android的ContentUris类,用于处理Uri
|
|
|
|
|
|
|
|
import android.content.ContentValues; // 导入Android的ContentValues类,用于存储键值对的数据
|
|
|
|
|
|
|
|
import android.content.Intent; // 导入Android的Intent类,用于处理应用程序之间的消息传递
|
|
|
|
|
|
|
|
import android.content.UriMatcher; // 导入Android的UriMatcher类,用于匹配Uri
|
|
|
|
|
|
|
|
import android.database.Cursor; // 导入Android的Cursor类,用于处理数据库查询结果
|
|
|
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase; // 导入Android的SQLiteDatabase类,用于操作SQLite数据库
|
|
|
|
|
|
|
|
import android.net.Uri; // 导入Android的Uri类,用于表示统一资源标识符
|
|
|
|
|
|
|
|
import android.text.TextUtils; // 导入Android的TextUtils类,用于处理字符串
|
|
|
|
|
|
|
|
import android.util.Log; // 导入Android的Log类,用于记录日志信息
|
|
|
|
|
|
|
|
|
|
|
|
import android.app.SearchManager;
|
|
|
|
import net.micode.notes.R; // 导入应用程序的资源类R
|
|
|
|
import android.content.ContentProvider;
|
|
|
|
import net.micode.notes.data.Notes.DataColumns; // 导入Notes类中的DataColumns接口
|
|
|
|
import android.content.ContentUris;
|
|
|
|
import net.micode.notes.data.Notes.NoteColumns; // 导入Notes类中的NoteColumns接口
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE; // 导入NotesDatabaseHelper类中的TABLE接口
|
|
|
|
import android.content.Intent;
|
|
|
|
|
|
|
|
import android.content.UriMatcher;
|
|
|
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
|
|
|
import android.net.Uri;
|
|
|
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
public class NotesProvider extends ContentProvider {
|
|
|
|
|
|
|
|
// 定义一个静态的UriMatcher对象mMatcher,用于匹配Uri
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
private static final UriMatcher mMatcher;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个NotesDatabaseHelper对象mHelper,用于数据库操作
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
private NotesDatabaseHelper mHelper;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个静态的字符串TAG,用于日志标记
|
|
|
|
private static final String TAG = "NotesProvider";
|
|
|
|
private static final String TAG = "NotesProvider";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义静态常量URI_NOTE,表示笔记表的Uri
|
|
|
|
private static final int URI_NOTE = 1;
|
|
|
|
private static final int URI_NOTE = 1;
|
|
|
|
|
|
|
|
// 定义静态常量URI_NOTE_ITEM,表示单个笔记项的Uri
|
|
|
|
private static final int URI_NOTE_ITEM = 2;
|
|
|
|
private static final int URI_NOTE_ITEM = 2;
|
|
|
|
|
|
|
|
// 定义静态常量URI_DATA,表示数据表的Uri
|
|
|
|
private static final int URI_DATA = 3;
|
|
|
|
private static final int URI_DATA = 3;
|
|
|
|
|
|
|
|
// 定义静态常量URI_DATA_ITEM,表示单个数据项的Uri
|
|
|
|
private static final int URI_DATA_ITEM = 4;
|
|
|
|
private static final int URI_DATA_ITEM = 4;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义静态常量URI_SEARCH,表示搜索的Uri
|
|
|
|
private static final int URI_SEARCH = 5;
|
|
|
|
private static final int URI_SEARCH = 5;
|
|
|
|
|
|
|
|
// 定义静态常量URI_SEARCH_SUGGEST,表示搜索建议的Uri
|
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;
|
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 静态代码块,初始化UriMatcher对象
|
|
|
|
static {
|
|
|
|
static {
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);
|
|
|
@ -64,7 +73,9 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* x'0A' represents the '\n' character in sqlite. For title and content in the search result,
|
|
|
|
* 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.
|
|
|
|
* we will trim '\n' and white space in order to show more information.
|
|
|
|
|
|
|
|
* x'0A' 在SQLite中表示换行符。在搜索结果中,我们将去除换行符和空白字符以显示更多信息。
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
// 定义静态字符串NOTES_SEARCH_PROJECTION,用于搜索查询的投影
|
|
|
|
private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + ","
|
|
|
|
private static final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + ","
|
|
|
|
+ NoteColumns.ID + " AS " + SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA + ","
|
|
|
|
+ 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_1 + ","
|
|
|
@ -73,218 +84,280 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
+ "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + ","
|
|
|
|
+ "'" + Intent.ACTION_VIEW + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_ACTION + ","
|
|
|
|
+ "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA;
|
|
|
|
+ "'" + Notes.TextNote.CONTENT_TYPE + "' AS " + SearchManager.SUGGEST_COLUMN_INTENT_DATA;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义静态字符串NOTES_SNIPPET_SEARCH_QUERY,用于搜索查询的SQL语句
|
|
|
|
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 ?"
|
|
|
|
+ " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER
|
|
|
|
+ " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER
|
|
|
|
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE;
|
|
|
|
+ " AND " + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重写父类ContentProvider的onCreate方法,用于初始化数据库帮助类
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public boolean onCreate() {
|
|
|
|
public boolean onCreate() {
|
|
|
|
|
|
|
|
// 获取NotesDatabaseHelper的单例实例
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
|
|
|
|
|
// 返回true表示初始化成功
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重写父类ContentProvider的query方法,用于根据Uri查询数据
|
|
|
|
@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) {
|
|
|
|
Cursor c = null;
|
|
|
|
Cursor c = null; // 定义一个Cursor对象c,用于存储查询结果
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase(); // 获取可读的SQLiteDatabase对象
|
|
|
|
String id = null;
|
|
|
|
String id = null; // 定义一个字符串id,用于存储Uri中的ID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 根据Uri匹配不同的查询逻辑
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
case URI_NOTE:
|
|
|
|
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;
|
|
|
|
break;
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
|
|
|
// 获取Uri中的ID
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
|
|
|
|
// 查询单个笔记项
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_DATA:
|
|
|
|
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;
|
|
|
|
break;
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
|
|
|
// 获取Uri中的ID
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
|
|
|
|
// 查询单个数据项
|
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs, null, null, sortOrder);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_SEARCH:
|
|
|
|
case URI_SEARCH:
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
case URI_SEARCH_SUGGEST:
|
|
|
|
|
|
|
|
// 检查是否指定了sortOrder、selection、selectionArgs或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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
String searchString = null;
|
|
|
|
String searchString = null; // 定义一个字符串searchString,用于存储搜索字符串
|
|
|
|
|
|
|
|
// 如果是搜索建议Uri,则获取搜索字符串
|
|
|
|
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
|
|
|
|
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果搜索字符串为空,则返回null
|
|
|
|
if (TextUtils.isEmpty(searchString)) {
|
|
|
|
if (TextUtils.isEmpty(searchString)) {
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
|
|
|
|
// 格式化搜索字符串,添加通配符
|
|
|
|
searchString = String.format("%%%s%%", searchString);
|
|
|
|
searchString = String.format("%%%s%%", searchString);
|
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
|
|
|
|
// 执行搜索查询
|
|
|
|
new String[] { searchString });
|
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY, 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;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
|
|
|
|
// 如果Uri不匹配,则抛出异常
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果查询结果不为空,则设置通知Uri
|
|
|
|
if (c != null) {
|
|
|
|
if (c != null) {
|
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 返回查询结果
|
|
|
|
return c;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重写父类ContentProvider的insert方法,用于插入数据
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
public Uri insert(Uri uri, ContentValues values) {
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase(); // 获取可写的SQLiteDatabase对象
|
|
|
|
long dataId = 0, noteId = 0, insertedId = 0;
|
|
|
|
long dataId = 0, noteId = 0, insertedId = 0; // 定义长整型变量dataId、noteId和insertedId,用于存储插入的数据ID和笔记ID
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 根据Uri匹配不同的插入逻辑
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
case URI_NOTE:
|
|
|
|
case URI_NOTE:
|
|
|
|
|
|
|
|
// 插入笔记
|
|
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
|
|
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_DATA:
|
|
|
|
case URI_DATA:
|
|
|
|
|
|
|
|
// 检查ContentValues中是否包含note_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 {
|
|
|
|
|
|
|
|
// 如果没有note_id,则记录日志信息
|
|
|
|
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);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
|
|
|
|
// 如果Uri不匹配,则抛出异常
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Notify the note uri
|
|
|
|
|
|
|
|
|
|
|
|
// 通知笔记Uri的变化
|
|
|
|
if (noteId > 0) {
|
|
|
|
if (noteId > 0) {
|
|
|
|
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
|
|
|
|
// 通知数据Uri的变化
|
|
|
|
if (dataId > 0) {
|
|
|
|
if (dataId > 0) {
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 返回插入数据的Uri
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重写父类ContentProvider的delete方法,用于删除数据
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {
|
|
|
|
int count = 0;
|
|
|
|
int count = 0; // 定义整型变量count,用于存储删除的行数
|
|
|
|
String id = null;
|
|
|
|
String id = null; // 定义一个字符串id,用于存储Uri中的ID
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase(); // 获取可写的SQLiteDatabase对象
|
|
|
|
boolean deleteData = false;
|
|
|
|
boolean deleteData = false; // 定义布尔型变量deleteData,用于标记是否删除数据项
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 根据Uri匹配不同的删除逻辑
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
case URI_NOTE:
|
|
|
|
case URI_NOTE:
|
|
|
|
|
|
|
|
// 添加条件以确保ID大于0
|
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";
|
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.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 小于 0 表示系统文件夹,不允许移动到回收站
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
long noteId = Long.valueOf(id);
|
|
|
|
long noteId = Long.valueOf(id);
|
|
|
|
if (noteId <= 0) {
|
|
|
|
if (noteId <= 0) {
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
count = db.delete(TABLE.NOTE,
|
|
|
|
// 删除单个笔记项
|
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
count = db.delete(TABLE.NOTE, NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
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;
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
|
|
|
// 获取Uri中的ID
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
count = db.delete(TABLE.DATA,
|
|
|
|
// 删除单个数据项
|
|
|
|
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
count = db.delete(TABLE.DATA, DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
deleteData = true;
|
|
|
|
deleteData = true;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
|
|
|
|
// 如果Uri不匹配,则抛出异常
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果删除的行数大于0,则通知Uri的变化
|
|
|
|
if (count > 0) {
|
|
|
|
if (count > 0) {
|
|
|
|
if (deleteData) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重写父类ContentProvider的update方法,用于更新数据
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
|
|
|
|
int count = 0;
|
|
|
|
int count = 0; // 定义整型变量count,用于存储更新的行数
|
|
|
|
String id = null;
|
|
|
|
String id = null; // 定义一个字符串id,用于存储Uri中的ID
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();
|
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase(); // 获取可写的SQLiteDatabase对象
|
|
|
|
boolean updateData = false;
|
|
|
|
boolean updateData = false; // 定义布尔型变量updateData,用于标记是否更新数据项
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 根据Uri匹配不同的更新逻辑
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
switch (mMatcher.match(uri)) {
|
|
|
|
case URI_NOTE:
|
|
|
|
case URI_NOTE:
|
|
|
|
|
|
|
|
// 增加笔记版本号
|
|
|
|
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;
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
case URI_NOTE_ITEM:
|
|
|
|
|
|
|
|
// 获取Uri中的ID
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
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);
|
|
|
|
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_DATA:
|
|
|
|
case URI_DATA:
|
|
|
|
|
|
|
|
// 更新数据项
|
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs);
|
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs);
|
|
|
|
updateData = true;
|
|
|
|
updateData = true;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
case URI_DATA_ITEM:
|
|
|
|
|
|
|
|
// 获取Uri中的ID
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
id = uri.getPathSegments().get(1);
|
|
|
|
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
|
|
|
|
// 更新单个数据项
|
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
|
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);
|
|
|
|
updateData = true;
|
|
|
|
updateData = true;
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
|
|
|
|
// 如果Uri不匹配,则抛出异常
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 如果更新的行数大于0,则通知Uri的变化
|
|
|
|
if (count > 0) {
|
|
|
|
if (count > 0) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个方法parseSelection,用于处理selection字符串
|
|
|
|
private String parseSelection(String selection) {
|
|
|
|
private String parseSelection(String selection) {
|
|
|
|
|
|
|
|
// 如果selection不为空,则添加AND条件
|
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 定义一个方法increaseNoteVersion,用于增加笔记的版本号
|
|
|
|
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
|
|
|
|
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {
|
|
|
|
StringBuilder sql = new StringBuilder(120);
|
|
|
|
StringBuilder sql = new StringBuilder(120); // 定义一个StringBuilder对象sql,用于构建SQL语句
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 构建更新笔记版本号的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不为空,则添加WHERE条件
|
|
|
|
if (id > 0 || !TextUtils.isEmpty(selection)) {
|
|
|
|
if (id > 0 || !TextUtils.isEmpty(selection)) {
|
|
|
|
sql.append(" WHERE ");
|
|
|
|
sql.append(" WHERE ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果id大于0,则添加ID条件
|
|
|
|
if (id > 0) {
|
|
|
|
if (id > 0) {
|
|
|
|
sql.append(NoteColumns.ID + "=" + String.valueOf(id));
|
|
|
|
sql.append(NoteColumns.ID + "=" + String.valueOf(id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 如果selection不为空,则添加selection条件
|
|
|
|
if (!TextUtils.isEmpty(selection)) {
|
|
|
|
if (!TextUtils.isEmpty(selection)) {
|
|
|
|
String selectString = id > 0 ? parseSelection(selection) : selection;
|
|
|
|
String selectString = id > 0 ? parseSelection(selection) : selection;
|
|
|
|
for (String args : selectionArgs) {
|
|
|
|
for (String args : selectionArgs) {
|
|
|
@ -293,13 +366,14 @@ public class NotesProvider extends ContentProvider {
|
|
|
|
sql.append(selectString);
|
|
|
|
sql.append(selectString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 执行SQL语句以增加笔记版本号
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 重写父类ContentProvider的getType方法,用于返回Uri的数据类型
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
public String getType(Uri uri) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|