|
|
/*
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
*
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
* You may obtain a copy of the License at
|
|
|
*
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
*
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
* See the License for the specific language governing permissions and
|
|
|
* limitations under the License.
|
|
|
*/
|
|
|
|
|
|
package net.micode.notes.data;// 包名:net.micode.notes.data
|
|
|
|
|
|
|
|
|
import android.app.SearchManager;// 导入 Android 的搜索管理器类
|
|
|
import android.content.ContentProvider;// 导入 Android 的内容提供器类
|
|
|
import android.content.ContentUris;// 导入 Android 的内容 URI 工具类
|
|
|
import android.content.ContentValues;// 导入 Android 的内容值类
|
|
|
import android.content.Intent;
|
|
|
import android.content.UriMatcher;// 导入 Android 的 URI 匹配器类
|
|
|
import android.database.Cursor;// 导入 Android 的游标类
|
|
|
import android.database.sqlite.SQLiteDatabase;// 导入 Android 的 SQLite 数据库类
|
|
|
import android.net.Uri;// 导入 Android 的统一资源标识符类
|
|
|
import android.text.TextUtils;// 导入 Android 的文本工具类
|
|
|
import android.util.Log;// 导入 Android 的日志工具类
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
import net.micode.notes.data.Notes.DataColumns;// 导入 net.micode.notes.data.Notes 类中的 DataColumns 内部类
|
|
|
import net.micode.notes.data.Notes.NoteColumns;
|
|
|
import net.micode.notes.data.NotesDatabaseHelper.TABLE;// 导入 net.micode.notes.data.NotesDatabaseHelper 类中的 TABLE 静态成员
|
|
|
|
|
|
|
|
|
public class NotesProvider extends ContentProvider {// 定义一个名为 NotesProvider 的公共类,它继承自 ContentProvider。
|
|
|
private static final UriMatcher mMatcher;// 定义一个私有的静态常量 mMatcher,类型为 UriMatcher。
|
|
|
|
|
|
private NotesDatabaseHelper mHelper;// 定义一个私有的静态常量 mMatcher,类型为 UriMatcher。
|
|
|
|
|
|
private static final String TAG = "NotesProvider";// 定义一个私有的静态常量 TAG,值为"NotesProvider"。
|
|
|
|
|
|
private static final int URI_NOTE = 1;// 定义一个私有的静态常量 URI_NOTE,值为 1。
|
|
|
private static final int URI_NOTE_ITEM = 2;// 定义一个私有的静态常量 URI_NOTE_ITEM,值为 2。
|
|
|
private static final int URI_DATA = 3;// 定义一个私有的静态常量 URI_DATA,值为 3。
|
|
|
private static final int URI_DATA_ITEM = 4;// 定义一个私有的静态常量 URI_DATA_ITEM,值为 4。
|
|
|
|
|
|
private static final int URI_SEARCH = 5;// 定义一个私有的静态常量 URI_SEARCH,值为 5。
|
|
|
private static final int URI_SEARCH_SUGGEST = 6;// 定义一个私有的静态常量 URI_SEARCH_SUGGEST,值为 6。
|
|
|
|
|
|
static {// 静态代码块。
|
|
|
mMatcher = new UriMatcher(UriMatcher.NO_MATCH);// 创建一个新的 UriMatcher 对象并赋值给 mMatcher,初始匹配结果为 UriMatcher.NO_MATCH。
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note", URI_NOTE);// 向 mMatcher 添加一个 URI 匹配规则,当 authority 为 Notes.AUTHORITY,路径为"note"时,匹配结果为 URI_NOTE。
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "note/#", URI_NOTE_ITEM);// 向 mMatcher 添加一个 URI 匹配规则,当 authority 为 Notes.AUTHORITY,路径为"note/任意数字"时,匹配结果为 URI_NOTE_ITEM。
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data", URI_DATA);// 向 mMatcher 添加一个 URI 匹配规则,当 authority 为 Notes.AUTHORITY,路径为"note/任意数字"时,匹配结果为 URI_NOTE_ITEM。
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "data/#", URI_DATA_ITEM);// 向 mMatcher 添加一个 URI 匹配规则,当 authority 为 Notes.AUTHORITY,路径为"data/任意数字"时,匹配结果为 URI_DATA_ITEM。
|
|
|
mMatcher.addURI(Notes.AUTHORITY, "search", URI_SEARCH);// 向 mMatcher 添加一个 URI 匹配规则,当 authority 为 Notes.AUTHORITY,路径为"search"时,匹配结果为 URI_SEARCH。
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY, URI_SEARCH_SUGGEST);// 向 mMatcher 添加一个 URI 匹配规则,当 authority 为 Notes.AUTHORITY,路径为 SearchManager.SUGGEST_URI_PATH_QUERY 时,匹配结果为 URI_SEARCH_SUGGEST。
|
|
|
mMatcher.addURI(Notes.AUTHORITY, SearchManager.SUGGEST_URI_PATH_QUERY + "/*", URI_SEARCH_SUGGEST);// 向 mMatcher 添加一个 URI 匹配规则,当 authority 为 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;
|
|
|
// 定义一个私有的静态常量 NOTES_SEARCH_PROJECTION,它是一个字符串,由多个部分组成,分别是 NoteColumns.ID(可能是笔记的 ID)、NoteColumns.ID 作为 SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA、对 NoteColumns.SNIPPET(可能是笔记片段)进行处理后的结果作为 SearchManager.SUGGEST_COLUMN_TEXT_1 和 SearchManager.SUGGEST_COLUMN_TEXT_2、R.drawable.search_result(可能是搜索结果的图标资源)作为 SearchManager.SUGGEST_COLUMN_ICON_1、Intent.ACTION_VIEW(可能是查看的动作)作为 SearchManager.SUGGEST_COLUMN_INTENT_ACTION、Notes.TextNote.CONTENT_TYPE(可能是文本笔记的内容类型)作为 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;
|
|
|
SNIPPET_SEARCH_QUERY,它是一个 SQL 查询语句,包含选择 NOTES_SEARCH_PROJECTION 指定的列,从 TABLE.NOTE(可能是一个表名)中选择,有条件判断 NoteColumns.SNIPPET(笔记片段)类似某个值,NoteColumns.PARENT_ID(可能是父 ID)不等于 Notes.ID_TRASH_FOLER(可能是垃圾文件夹的 ID),以及 NoteColumns.TYPE(可能是类型)等于 Notes.TYPE_NOTE(可能是笔记类型)。
|
|
|
|
|
|
@Override
|
|
|
public boolean onCreate() {
|
|
|
mHelper = NotesDatabaseHelper.getInstance(getContext());
|
|
|
return true;
|
|
|
}// 重写 onCreate 方法。在这个方法中,获取 NotesDatabaseHelper 的实例并赋值给 mHelper,然后返回 true 表示创建成功。
|
|
|
|
|
|
@Override
|
|
|
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
|
|
|
String sortOrder) {// 重写 query 方法,该方法接收一个 Uri(统一资源标识符)、一个字符串数组(projection,指定要返回的列)、一个字符串(selection,用于筛选数据的条件)、一个字符串数组(selectionArgs,用于填充 selection 中的占位符)和一个字符串(sortOrder,用于指定排序方式),并返回一个 Cursor(游标)对象。
|
|
|
Cursor c = null;// 初始化一个 Cursor 对象为 null。
|
|
|
SQLiteDatabase db = mHelper.getReadableDatabase();// 获取一个可读的 SQLiteDatabase 对象。
|
|
|
String id = null;// 初始化一个字符串 id 为 null。
|
|
|
switch (mMatcher.match(uri)) {// 根据 Uri 匹配器 mMatcher 对传入的 Uri 进行匹配,并根据匹配结果进行不同的处理。
|
|
|
case URI_NOTE:// 如果匹配到 URI_NOTE。
|
|
|
c = db.query(TABLE.NOTE, projection, selection, selectionArgs, null, null,
|
|
|
sortOrder);// 使用数据库对象 db 执行查询操作,从名为 TABLE.NOTE 的表中查询指定的列(projection),根据条件(selection 和 selectionArgs)进行筛选,并按照指定的排序方式(sortOrder)进行排序,结果存储在 Cursor 对象 c 中。
|
|
|
break;// 跳出 switch 语句。
|
|
|
case URI_NOTE_ITEM:// 如果匹配到 URI_NOTE_ITEM。
|
|
|
id = uri.getPathSegments().get(1);// 从 Uri 的路径片段中获取第二个元素(索引为 1),并赋值给 id。
|
|
|
c = db.query(TABLE.NOTE, projection, NoteColumns.ID + "=" + id
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);// 使用数据库对象 db 执行查询操作,从名为 TABLE.NOTE 的表中查询指定的列(projection),根据条件(NoteColumns.ID 等于 id 加上经过 parseSelection 方法处理后的 selection)进行筛选,并按照指定的排序方式(sortOrder)进行排序,结果存储在 Cursor 对象 c 中。
|
|
|
break;// 跳出 switch 语句。
|
|
|
case URI_DATA:// 如果匹配到 URI_DATA。
|
|
|
c = db.query(TABLE.DATA, projection, selection, selectionArgs, null, null,
|
|
|
sortOrder);// 使用数据库对象 db 执行查询操作,从名为 TABLE.DATA 的表中查询指定的列(projection),根据条件(selection 和 selectionArgs)进行筛选,并按照指定的排序方式(sortOrder)进行排序,结果存储在 Cursor 对象 c 中。
|
|
|
break;// 跳出 switch 语句。
|
|
|
case URI_DATA_ITEM:// 如果匹配到 URI_DATA_ITEM。
|
|
|
id = uri.getPathSegments().get(1);// 从 Uri 的路径片段中获取第二个元素(索引为 1),并赋值给 id。
|
|
|
c = db.query(TABLE.DATA, projection, DataColumns.ID + "=" + id
|
|
|
+ parseSelection(selection), selectionArgs, null, null, sortOrder);// 使用数据库对象 db 执行查询操作,从名为 TABLE.DATA 的表中查询指定的列(projection),根据条件(DataColumns.ID 等于 id 加上经过 parseSelection 方法处理后的 selection)进行筛选,并按照指定的排序方式(sortOrder)进行排序,结果存储在 Cursor 对象 c 中。
|
|
|
break;// 跳出 switch 语句。
|
|
|
case URI_SEARCH:// 如果匹配到 URI_SEARCH。
|
|
|
case URI_SEARCH_SUGGEST:// 如果匹配到 URI_SEARCH_SUGGEST。
|
|
|
if (sortOrder != null || projection != null) {// 如果 sortOrder 不为 null 或者 projection 不为 null。
|
|
|
throw new IllegalArgumentException(
|
|
|
"do not specify sortOrder, selection, selectionArgs, or projection" + "with this query");// 抛出一个非法参数异常,提示在这个查询中不要指定 sortOrder、selection、selectionArgs 或 projection。
|
|
|
}
|
|
|
|
|
|
String searchString = null;// 定义一个字符串 searchString 并初始化为 null。
|
|
|
if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) {// 如果传入的 Uri 与 URI_SEARCH_SUGGEST 匹配。
|
|
|
if (uri.getPathSegments().size() > 1) {// 如果 Uri 的路径片段数量大于 1。
|
|
|
searchString = uri.getPathSegments().get(1);// 获取 Uri 的第二个路径片段并赋值给 searchString。
|
|
|
}
|
|
|
} else {
|
|
|
// 如果传入的 Uri 不与 URI_SEARCH_SUGGEST 匹配。
|
|
|
searchString = uri.getQueryParameter("pattern");// 从 Uri 的查询参数中获取名为 "pattern" 的参数值并赋值给 searchString。
|
|
|
}
|
|
|
|
|
|
if (TextUtils.isEmpty(searchString)) {// 如果 searchString 为空。
|
|
|
return null;// 返回 null。
|
|
|
}
|
|
|
|
|
|
try {// 尝试执行以下代码块。
|
|
|
searchString = String.format("%%%s%%", searchString);// 使用 String.format 方法格式化 searchString,在其前后加上 "%%"。
|
|
|
c = db.rawQuery(NOTES_SNIPPET_SEARCH_QUERY,
|
|
|
new String[] { searchString });// 使用数据库对象 db 执行原始 SQL 查询 NOTES_SNIPPET_SEARCH_QUERY,并将 searchString 作为参数传入,结果存储在 Cursor 对象 c 中。
|
|
|
} catch (IllegalStateException ex) {// 如果发生非法状态异常。
|
|
|
Log.e(TAG, "got exception: " + ex.toString());// 使用日志记录错误信息,包括标签 TAG 和异常的字符串表示。
|
|
|
}
|
|
|
break;// 跳出 switch 语句。
|
|
|
default:// 如果没有匹配到任何已知的 Uri。
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);// 抛出非法参数异常,提示未知的 Uri。
|
|
|
}
|
|
|
if (c != null) {// 抛出非法参数异常,提示未知的 Uri。
|
|
|
c.setNotificationUri(getContext().getContentResolver(), uri);// 设置 Cursor 对象的通知 Uri,以便在数据发生变化时通知内容解析器。
|
|
|
}
|
|
|
return c;// 返回 Cursor 对象 c。
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public Uri insert(Uri uri, ContentValues values) {// 重写 insert 方法,该方法接收一个 Uri 和一个 ContentValues 对象,用于插入数据,并返回插入数据的 Uri。
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();// 获取一个可写的 SQLiteDatabase 对象。
|
|
|
long dataId = 0, noteId = 0, insertedId = 0;// 获取一个可写的 SQLiteDatabase 对象。
|
|
|
switch (mMatcher.match(uri)) {// 根据 Uri 匹配器 mMatcher 对传入的 Uri 进行匹配,并根据匹配结果进行不同的处理。
|
|
|
case URI_NOTE:// 如果匹配到 URI_NOTE。
|
|
|
insertedId = noteId = db.insert(TABLE.NOTE, null, values);// 使用数据库对象 db 将 values 插入到名为 TABLE.NOTE 的表中,返回插入的行 ID,并将其同时赋值给 insertedId 和 noteId。
|
|
|
break;// 跳出 switch 语句。
|
|
|
case URI_DATA:// 如果匹配到 URI_DATA。
|
|
|
if (values.containsKey(DataColumns.NOTE_ID)) {// 如果 values 中包含 DataColumns.NOTE_ID 键。
|
|
|
noteId = values.getAsLong(DataColumns.NOTE_ID);// 获取 values 中对应 DataColumns.NOTE_ID 的长整型值,并赋值给 noteId。
|
|
|
} else {// 如果 values 中不包含 DataColumns.NOTE_ID 键。
|
|
|
Log.d(TAG, "Wrong data format without note id:" + values.toString());// 使用日志记录错误信息,提示数据格式错误,没有 note id,并打印 values 的字符串表示。
|
|
|
}
|
|
|
insertedId = dataId = db.insert(TABLE.DATA, null, values);// 使用数据库对象 db 将 values 插入到名为 TABLE.DATA 的表中,返回插入的行 ID,并将其同时赋值给 insertedId 和 dataId。
|
|
|
break;// 跳出 switch 语句。
|
|
|
default:// 如果没有匹配到任何已知的 Uri。
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);// 抛出非法参数异常,提示未知的 Uri。
|
|
|
}
|
|
|
// Notify the note uri
|
|
|
// 注释:通知 note 的 Uri。
|
|
|
if (noteId > 0) {// 如果 noteId 大于 0。
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null);// 使用上下文的内容解析器通知 Note 的 Uri 发生了变化,传入带有 noteId 的 Uri 和 null。
|
|
|
}
|
|
|
|
|
|
// Notify the data uri
|
|
|
if (dataId > 0) {// 如果 dataId 大于 0。
|
|
|
getContext().getContentResolver().notifyChange(
|
|
|
ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null);// 使用上下文的内容解析器通知 Data 的 Uri 发生了变化,传入带有 dataId 的 Uri 和 null。
|
|
|
}
|
|
|
|
|
|
return ContentUris.withAppendedId(uri, insertedId);// 返回带有 insertedId 的 Uri。
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int delete(Uri uri, String selection, String[] selectionArgs) {// 重写 delete 方法,该方法接收一个 Uri、一个筛选条件字符串和一个筛选参数数组,用于删除数据,并返回删除的行数。
|
|
|
int count = 0;// 定义一个整数变量 count 并初始化为 0,表示删除的行数。
|
|
|
String id = null;// 定义一个字符串变量 id 并初始化为 null。
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();// 获取一个可写的 SQLiteDatabase 对象。
|
|
|
boolean deleteData = false;// 定义一个布尔变量 deleteData 并初始化为 false,表示是否删除了数据。
|
|
|
switch (mMatcher.match(uri)) {// 根据 Uri 匹配器 mMatcher 对传入的 Uri 进行匹配,并根据匹配结果进行不同的处理。
|
|
|
case URI_NOTE:// 如果匹配到 URI_NOTE。
|
|
|
selection = "(" + selection + ") AND " + NoteColumns.ID + ">0 ";// 重新构建筛选条件,在原有的筛选条件上加上 NoteColumns.ID 大于 0 的条件。
|
|
|
count = db.delete(TABLE.NOTE, selection, selectionArgs);// 使用数据库对象 db 删除名为 TABLE.NOTE 的表中符合筛选条件的行,并将删除的行数赋值给 count。
|
|
|
break;// 跳出 switch 语句。
|
|
|
case URI_NOTE_ITEM:// 如果匹配到 URI_NOTE_ITEM。
|
|
|
id = uri.getPathSegments().get(1);// 从 Uri 的路径片段中获取第二个元素(索引为 1),并赋值给 id。
|
|
|
/**
|
|
|
* ID that smaller than 0 is system folder which is not allowed to
|
|
|
* trash
|
|
|
*/
|
|
|
long noteId = Long.valueOf(id);// 将 id 转换为长整型并赋值给 noteId。
|
|
|
if (noteId <= 0) {// 如果 noteId 小于等于 0。
|
|
|
break;// 跳出 switch 语句。
|
|
|
}
|
|
|
count = db.delete(TABLE.NOTE,
|
|
|
NoteColumns.ID + "=" + id + parseSelection(selection), selectionArgs);// 使用数据库对象 db 删除名为 TABLE.NOTE 的表中符合筛选条件的行,并将删除的行数赋值给 count。
|
|
|
break;
|
|
|
case URI_DATA:// 如果匹配到 URI_DATA。
|
|
|
count = db.delete(TABLE.DATA, selection, selectionArgs);// 使用数据库对象 db 删除名为 TABLE.DATA 的表中符合筛选条件的行,并将删除的行数赋值给 count。
|
|
|
deleteData = true;// 将 deleteData 设置为 true,表示删除了数据。
|
|
|
break;
|
|
|
case URI_DATA_ITEM:// 将 deleteData 设置为 true,表示删除了数据。
|
|
|
id = uri.getPathSegments().get(1);// 从 Uri 的路径片段中获取第二个元素(索引为 1),并赋值给 id。
|
|
|
count = db.delete(TABLE.DATA,
|
|
|
DataColumns.ID + "=" + id + parseSelection(selection), selectionArgs);// 使用数据库对象 db 删除名为 TABLE.DATA 的表中符合筛选条件的行,并将删除的行数赋值给 count。
|
|
|
deleteData = true;// 将 deleteData 设置为 true,表示删除了数据。
|
|
|
break;
|
|
|
default:// 如果没有匹配到任何已知的 Uri。
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);// 抛出非法参数异常,提示未知的 Uri。
|
|
|
}
|
|
|
if (count > 0) {// 如果删除的行数大于 0。
|
|
|
if (deleteData) {// 如果 deleteData 为 true,表示删除了数据。
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);// 使用上下文的内容解析器通知 Notes.CONTENT_NOTE_URI 发生了变化,传入 null。
|
|
|
}
|
|
|
getContext().getContentResolver().notifyChange(uri, null);// 使用上下文的内容解析器通知传入的 Uri 发生了变化,传入 null。
|
|
|
}
|
|
|
return count;// 返回删除的行数。
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {// 重写 update 方法,该方法接收一个 Uri、一个 ContentValues 对象、一个筛选条件字符串和一个筛选参数数组,用于更新数据,并返回更新的行数。
|
|
|
int count = 0;// 定义一个整数变量 count 并初始化为 0,表示更新的行数。
|
|
|
String id = null;// 定义一个字符串变量 id 并初始化为 null。
|
|
|
SQLiteDatabase db = mHelper.getWritableDatabase();// 获取一个可写的 SQLiteDatabase 对象
|
|
|
boolean updateData = false;// 定义一个布尔变量 updateData 并初始化为 false,表示是否更新了数据。
|
|
|
switch (mMatcher.match(uri)) {// 根据 Uri 匹配器 mMatcher 对传入的 Uri 进行匹配,并根据匹配结果进行不同的处理。
|
|
|
case URI_NOTE:// 如果匹配到 URI_NOTE。
|
|
|
increaseNoteVersion(-1, selection, selectionArgs);// 调用 increaseNoteVersion 方法,传入 -1、筛选条件和筛选参数。
|
|
|
count = db.update(TABLE.NOTE, values, selection, selectionArgs);// 使用数据库对象 db 更新名为 TABLE.NOTE 的表中符合筛选条件的行,并将更新的行数赋值给 count。
|
|
|
break;
|
|
|
case URI_NOTE_ITEM:// 使用数据库对象 db 更新名为 TABLE.NOTE 的表中符合筛选条件的行,并将更新的行数赋值给 count。
|
|
|
id = uri.getPathSegments().get(1);// 从 Uri 的路径片段中获取第二个元素(索引为 1),并赋值给 id。
|
|
|
increaseNoteVersion(Long.valueOf(id), selection, selectionArgs);// 从 Uri 的路径片段中获取第二个元素(索引为 1),并赋值给 id。
|
|
|
count = db.update(TABLE.NOTE, values, NoteColumns.ID + "=" + id// 使用数据库对象 db 更新名为 TABLE.NOTE 的表中符合筛选条件的行,并将更新的行数赋值给 count。
|
|
|
+ parseSelection(selection), selectionArgs);
|
|
|
break;
|
|
|
case URI_DATA:// 如果匹配到 URI_DATA。
|
|
|
count = db.update(TABLE.DATA, values, selection, selectionArgs);// 使用数据库对象 db 更新名为 TABLE.DATA 的表中符合筛选条件的行,并将更新的行数赋值给 count。
|
|
|
updateData = true;// 将 updateData 设置为 true,表示更新了数据。
|
|
|
break;
|
|
|
case URI_DATA_ITEM:// 如果匹配到 URI_DATA_ITEM。
|
|
|
id = uri.getPathSegments().get(1);// 从 Uri 的路径片段中获取第二个元素(索引为 1),并赋值给 id。
|
|
|
count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id
|
|
|
+ parseSelection(selection), selectionArgs);// 使用数据库对象 db 更新名为 TABLE.DATA 的表中符合筛选条件的行,并将更新的行数赋值给 count。
|
|
|
updateData = true;// 将 updateData 设置为 true,表示更新了数据。
|
|
|
break;
|
|
|
default:// 如果没有匹配到任何已知的 Uri。
|
|
|
throw new IllegalArgumentException("Unknown URI " + uri);// 抛出非法参数异常,提示未知的 Uri。
|
|
|
}
|
|
|
|
|
|
if (count > 0) {// 如果更新的行数大于 0。
|
|
|
if (updateData) {// 如果 updateData 为 true,表示更新了数据。
|
|
|
getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null);// 使用上下文的内容解析器通知 Notes.CONTENT_NOTE_URI 发生了变化,传入 null。
|
|
|
}
|
|
|
getContext().getContentResolver().notifyChange(uri, null);// 使用上下文的内容解析器通知传入的 Uri 发生了变化,传入 null。
|
|
|
}
|
|
|
return count;// 返回更新的行数。
|
|
|
}
|
|
|
|
|
|
private String parseSelection(String selection) {// 定义一个私有方法 parseSelection,该方法接收一个筛选条件字符串,用于处理筛选条件。
|
|
|
return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : "");// 如果筛选条件不为空,返回 " AND (" + selection + ")",否则返回空字符串。
|
|
|
}
|
|
|
|
|
|
private void increaseNoteVersion(long id, String selection, String[] selectionArgs) {// 定义一个私有方法 increaseNoteVersion,该方法接收一个长整型 id、一个筛选条件字符串和一个筛选参数数组,用于增加笔记的版本号。
|
|
|
StringBuilder sql = new StringBuilder(120);// 创建一个 StringBuilder 对象,初始容量为 120。
|
|
|
sql.append("UPDATE ");// 将 "UPDATE " 添加到 StringBuilder 对象中。
|
|
|
sql.append(TABLE.NOTE);// 将 TABLE.NOTE(可能是表名)添加到 StringBuilder 对象中。
|
|
|
sql.append(" SET ");// 将 " SET " 添加到 StringBuilder 对象中。
|
|
|
sql.append(NoteColumns.VERSION);// 将 NoteColumns.VERSION(可能是版本号列名)添加到 StringBuilder 对象中。
|
|
|
sql.append("=" + NoteColumns.VERSION + "+1 ");// 将 "=" + NoteColumns.VERSION + "+1 " 添加到 StringBuilder 对象中,表示将版本号设置为当前版本号加 1。
|
|
|
|
|
|
if (id > 0 || !TextUtils.isEmpty(selection)) {// 如果 id 大于 0 或者筛选条件不为空。
|
|
|
sql.append(" WHERE ");
|
|
|
// 将 " WHERE " 添加到 StringBuilder 对象中。
|
|
|
sql.append(" WHERE ");
|
|
|
}
|
|
|
if (id > 0) {
|
|
|
sql.append(NoteColumns.ID + "=" + String.valueOf(id));// 将 NoteColumns.ID + "=" + id 的字符串表示添加到 StringBuilder 对象中,表示根据 id 进行筛选。
|
|
|
}
|
|
|
if (!TextUtils.isEmpty(selection)) {// 如果筛选条件不为空。
|
|
|
String selectString = id > 0 ? parseSelection(selection) : selection;// 如果 id 大于 0,调用 parseSelection 方法处理筛选条件,否则直接使用筛选条件。
|
|
|
for (String args : selectionArgs) {// 遍历筛选参数数组。
|
|
|
selectString = selectString.replaceFirst("\\?", args);// 将筛选条件中的第一个问号替换为当前的筛选参数。
|
|
|
}
|
|
|
sql.append(selectString);// 将处理后的筛选条件添加到
|
|
|
}
|
|
|
|
|
|
mHelper.getWritableDatabase().execSQL(sql.toString());// 使用数据库助手对象 mHelper 获取可写数据库,并执行 StringBuilder 对象中的 SQL 语句。
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public String getType(Uri uri) {// 重写 getType 方法,该方法接收一个 Uri,用于获取数据的 MIME 类型。
|
|
|
// TODO Auto-generated method stub
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
}
|