From 7177ed6448cf882377d554eb34444ece08b4182e Mon Sep 17 00:00:00 2001 From: lyl <2649575172@qq.com> Date: Fri, 22 Nov 2024 00:27:26 +0800 Subject: [PATCH] zuizhongjieguo.1 --- src1/net/micode/notes/data/Notes.java | 335 ++++-------------- src1/net/micode/notes/data/NotesProvider.java | 284 ++++++++------- 2 files changed, 229 insertions(+), 390 deletions(-) diff --git a/src1/net/micode/notes/data/Notes.java b/src1/net/micode/notes/data/Notes.java index f240604..820295d 100644 --- a/src1/net/micode/notes/data/Notes.java +++ b/src1/net/micode/notes/data/Notes.java @@ -1,279 +1,100 @@ -/* - * 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; // 定义包名 -package net.micode.notes.data; +import android.content.ContentUris; // 用于操作URI,如添加ID到URI中 +import android.net.Uri; // 用于表示URI的类 -import android.net.Uri; +// Notes 类定义了与便签应用相关的常量,包括URI、类型、ID等 public class Notes { - public static final String AUTHORITY = "micode_notes"; - public static final String TAG = "Notes"; - public static final int TYPE_NOTE = 0; - public static final int TYPE_FOLDER = 1; - public static final int TYPE_SYSTEM = 2; - - /** - * Following IDs are system folders' identifiers - * {@link Notes#ID_ROOT_FOLDER } is default folder - * {@link Notes#ID_TEMPARAY_FOLDER } is for notes belonging no folder - * {@link Notes#ID_CALL_RECORD_FOLDER} is to store call records - */ - public static final int ID_ROOT_FOLDER = 0; - public static final int ID_TEMPARAY_FOLDER = -1; - public static final int ID_CALL_RECORD_FOLDER = -2; - public static final int ID_TRASH_FOLER = -3; - - public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date"; - public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id"; - public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id"; - public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type"; - public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id"; - public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date"; - - public static final int TYPE_WIDGET_INVALIDE = -1; - public static final int TYPE_WIDGET_2X = 0; - public static final int TYPE_WIDGET_4X = 1; - + public static final String AUTHORITY = "micode_notes"; // 内容提供者的权限标识 + public static final String TAG = "Notes"; // 日志标签 + + // 定义笔记、文件夹和系统类型的常量 + public static final int TYPE_NOTE = 0; // 笔记类型 + public static final int TYPE_FOLDER = 1; // 文件夹类型 + public static final int TYPE_SYSTEM = 2; // 系统类型 + + // 系统文件夹的ID常量 + public static final int ID_ROOT_FOLDER = 0; // 根文件夹ID + public static final int ID_TEMPARAY_FOLDER = -1; // 临时文件夹ID + public static final int ID_CALL_RECORD_FOLDER = -2; // 电话记录文件夹ID + public static final int ID_TRASH_FOLER = -3; // 垃圾桶文件夹ID + + // 意图(Intent)中使用的额外数据键 + public static final String INTENT_EXTRA_ALERT_DATE = "net.micode.notes.alert_date"; // 警告日期 + public static final String INTENT_EXTRA_BACKGROUND_ID = "net.micode.notes.background_color_id"; // 背景颜色ID + public static final String INTENT_EXTRA_WIDGET_ID = "net.micode.notes.widget_id"; // 小部件ID + public static final String INTENT_EXTRA_WIDGET_TYPE = "net.micode.notes.widget_type"; // 小部件类型 + public static final String INTENT_EXTRA_FOLDER_ID = "net.micode.notes.folder_id"; // 文件夹ID + public static final String INTENT_EXTRA_CALL_DATE = "net.micode.notes.call_date"; // 电话日期 + + // 小部件类型的常量 + public static final int TYPE_WIDGET_INVALIDE = -1; // 无效的小部件类型 + public static final int TYPE_WIDGET_2X = 0; // 2x2小部件 + public static final int TYPE_WIDGET_4X = 1; // 4x4小部件 + + // 数据常量类,包含笔记和电话记录的MIME类型 public static class DataConstants { - public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; - public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; + public static final String NOTE = TextNote.CONTENT_ITEM_TYPE; // 文本笔记MIME类型 + public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; // 电话记录MIME类型 } - /** - * Uri to query all notes and folders - */ + // 查询所有笔记和文件夹的URI public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); - /** - * Uri to query data - */ + // 查询数据的URI public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); + // NoteColumns接口定义了笔记相关字段 public interface NoteColumns { - /** - * The unique ID for a row - *

Type: INTEGER (long)

- */ - public static final String ID = "_id"; - - /** - * The parent's id for note or folder - *

Type: INTEGER (long)

- */ - public static final String PARENT_ID = "parent_id"; - - /** - * Created data for note or folder - *

Type: INTEGER (long)

- */ - public static final String CREATED_DATE = "created_date"; - - /** - * Latest modified date - *

Type: INTEGER (long)

- */ - public static final String MODIFIED_DATE = "modified_date"; - - - /** - * Alert date - *

Type: INTEGER (long)

- */ - public static final String ALERTED_DATE = "alert_date"; - - /** - * Folder's name or text content of note - *

Type: TEXT

- */ - public static final String SNIPPET = "snippet"; - - /** - * Note's widget id - *

Type: INTEGER (long)

- */ - public static final String WIDGET_ID = "widget_id"; - - /** - * Note's widget type - *

Type: INTEGER (long)

- */ - public static final String WIDGET_TYPE = "widget_type"; - - /** - * Note's background color's id - *

Type: INTEGER (long)

- */ - public static final String BG_COLOR_ID = "bg_color_id"; - - /** - * For text note, it doesn't has attachment, for multi-media - * note, it has at least one attachment - *

Type: INTEGER

- */ - public static final String HAS_ATTACHMENT = "has_attachment"; - - /** - * Folder's count of notes - *

Type: INTEGER (long)

- */ - public static final String NOTES_COUNT = "notes_count"; - - /** - * The file type: folder or note - *

Type: INTEGER

- */ - public static final String TYPE = "type"; - - /** - * The last sync id - *

Type: INTEGER (long)

- */ - public static final String SYNC_ID = "sync_id"; - - /** - * Sign to indicate local modified or not - *

Type: INTEGER

- */ - public static final String LOCAL_MODIFIED = "local_modified"; - - /** - * Original parent id before moving into temporary folder - *

Type : INTEGER

- */ - public static final String ORIGIN_PARENT_ID = "origin_parent_id"; - - /** - * The gtask id - *

Type : TEXT

- */ - public static final String GTASK_ID = "gtask_id"; - - /** - * The version code - *

Type : INTEGER (long)

- */ - public static final String VERSION = "version"; + public static final String ID = "_id"; // 唯一ID + public static final String PARENT_ID = "parent_id"; // 父ID,用于文件夹结构 + public static final String CREATED_DATE = "created_date"; // 创建日期 + public static final String MODIFIED_DATE = "modified_date"; // 修改日期 + public static final String ALERTED_DATE = "alert_date"; // 警告日期 + public static final String SNIPPET = "snippet"; // 文件夹名或笔记内容摘要 + public static final String WIDGET_ID = "widget_id"; // 小部件ID + public static final String WIDGET_TYPE = "widget_type"; // 小部件类型 + public static final String BG_COLOR_ID = "bg_color_id"; // 背景颜色ID + public static final String HAS_ATTACHMENT = "has_attachment"; // 是否有附件 + public static final String NOTES_COUNT = "notes_count"; // 文件夹中的笔记数量 + public static final String TYPE = "type"; // 类型(笔记或文件夹) + public static final String SYNC_ID = "sync_id"; // 同步ID + public static final String LOCAL_MODIFIED = "local_modified"; // 本地修改标志 + public static final String ORIGIN_PARENT_ID = "origin_parent_id"; // 原始父ID + public static final String GTASK_ID = "gtask_id"; // Google任务ID + public static final String VERSION = "version"; // 版本号 } + // DataColumns接口定义了数据表相关字段 public interface DataColumns { - /** - * The unique ID for a row - *

Type: INTEGER (long)

- */ - public static final String ID = "_id"; - - /** - * The MIME type of the item represented by this row. - *

Type: Text

- */ - public static final String MIME_TYPE = "mime_type"; - - /** - * The reference id to note that this data belongs to - *

Type: INTEGER (long)

- */ - public static final String NOTE_ID = "note_id"; - - /** - * Created data for note or folder - *

Type: INTEGER (long)

- */ - public static final String CREATED_DATE = "created_date"; - - /** - * Latest modified date - *

Type: INTEGER (long)

- */ - public static final String MODIFIED_DATE = "modified_date"; - - /** - * Data's content - *

Type: TEXT

- */ - public static final String CONTENT = "content"; - - - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * integer data type - *

Type: INTEGER

- */ - public static final String DATA1 = "data1"; - - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * integer data type - *

Type: INTEGER

- */ - public static final String DATA2 = "data2"; - - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * TEXT data type - *

Type: TEXT

- */ - public static final String DATA3 = "data3"; - - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * TEXT data type - *

Type: TEXT

- */ - public static final String DATA4 = "data4"; - - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * TEXT data type - *

Type: TEXT

- */ - public static final String DATA5 = "data5"; + public static final String ID = "_id"; // 唯一ID + public static final String MIME_TYPE = "mime_type"; // MIME类型 + public static final String NOTE_ID = "note_id"; // 对应的笔记ID + public static final String CREATED_DATE = "created_date"; // 创建日期 + public static final String MODIFIED_DATE = "modified_date"; // 修改日期 + public static final String CONTENT = "content"; // 数据内容 + public static final String DATA1 = "data1"; // 通用数据列1 + public static final String DATA2 = "data2"; // 通用数据列2 + public static final String DATA3 = "data3"; // 通用数据列3 + public static final String DATA4 = "data4"; // 通用数据列4 + public static final String DATA5 = "data5"; // 通用数据列5 } + // TextNote类定义了文本笔记相关的常量 public static final class TextNote implements DataColumns { - /** - * Mode to indicate the text in check list mode or not - *

Type: Integer 1:check list mode 0: normal mode

- */ - public static final String MODE = DATA1; - - public static final int MODE_CHECK_LIST = 1; - - public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note"; - - public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note"; - - public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); + public static final String MODE = DATA1; // 模式,检查列表模式或普通模式 + public static final int MODE_CHECK_LIST = 1; // 检查列表模式值 + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/text_note"; // 目录MIME类型 + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/text_note"; // 项目MIME类型 + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); // 文本笔记URI } + // CallNote类定义了电话记录相关的常量 public static final class CallNote implements DataColumns { - /** - * Call date for this record - *

Type: INTEGER (long)

- */ - public static final String CALL_DATE = DATA1; - - /** - * Phone number for this record - *

Type: TEXT

- */ - public static final String PHONE_NUMBER = DATA3; - - public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note"; - - public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note"; - - public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note"); + public static final String CALL_DATE = DATA1; // 电话日期 + public static final String PHONE_NUMBER = DATA3; // 电话号码 + public static final String CONTENT_TYPE = "vnd.android.cursor.dir/call_note"; // 目录MIME类型 + public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/call_note"; // 项目MIME类型 + public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note"); // 电话记录URI } } diff --git a/src1/net/micode/notes/data/NotesProvider.java b/src1/net/micode/notes/data/NotesProvider.java index edb0a60..cf445c0 100644 --- a/src1/net/micode/notes/data/NotesProvider.java +++ b/src1/net/micode/notes/data/NotesProvider.java @@ -1,97 +1,84 @@ -/* - * 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; - - -import android.app.SearchManager; -import android.content.ContentProvider; -import android.content.ContentUris; -import android.content.ContentValues; -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; - +package net.micode.notes.data; // 定义包名 +import android.app.SearchManager; // 搜索管理器,用于处理搜索建议等 +import android.content.ContentProvider; // Android内容提供者基类 +import android.content.ContentUris; // 用于操作URI,如添加ID到URI中 +import android.content.ContentValues; // 用于存储数据的键值对,便于数据库操作 +import android.content.Intent; // 用于应用程序之间传递数据的意图 +import android.content.UriMatcher; // 用于匹配URI +import android.database.Cursor; // 用于数据库查询结果集的迭代 +import android.database.sqlite.SQLiteDatabase; // 用于执行SQL语句和进行数据库操作 +import android.net.Uri; // 用于表示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 { + // UriMatcher用于匹配URI private static final UriMatcher mMatcher; - private NotesDatabaseHelper mHelper; - - private static final String TAG = "NotesProvider"; + private NotesDatabaseHelper mHelper; // 数据库帮助类实例 - 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 String TAG = "NotesProvider"; // 日志标签 - 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 { + // 创建UriMatcher时,调用UriMatcher(UriMatcher.NO_MATCH)表示不匹配任何路径的返回码 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);//笔记表中的某条记录 + 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);//搜索建议 } - /** - * 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 final String NOTES_SEARCH_PROJECTION = NoteColumns.ID + ","//笔记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; + // 声明搜索查询 + 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;//笔记类型 @Override + // Context只有在onCreate()中才被初始化,对mHelper进行实例化 public boolean onCreate() { mHelper = NotesDatabaseHelper.getInstance(getContext()); return true; } @Override + // 查询uri在数据库中对应的位置 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); @@ -113,6 +100,7 @@ public class NotesProvider extends ContentProvider { 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"); } @@ -120,6 +108,8 @@ public class NotesProvider extends ContentProvider { String searchString = null; if (mMatcher.match(uri) == URI_SEARCH_SUGGEST) { if (uri.getPathSegments().size() > 1) { + // getPathSegments()方法得到一个String的List, + // 在uri.getPathSegments().get(1)为第2个元素 searchString = uri.getPathSegments().get(1); } } else { @@ -139,6 +129,7 @@ public class NotesProvider extends ContentProvider { } break; default: + // 抛出异常 throw new IllegalArgumentException("Unknown URI " + uri); } if (c != null) { @@ -148,13 +139,17 @@ public class NotesProvider extends ContentProvider { } @Override + // 插入一个uri public Uri insert(Uri uri, ContentValues values) { + // 获得可写的数据库 SQLiteDatabase db = mHelper.getWritableDatabase(); long dataId = 0, noteId = 0, insertedId = 0; switch (mMatcher.match(uri)) { + // 新增一个条目 case URI_NOTE: insertedId = noteId = db.insert(TABLE.NOTE, null, values); break; + // 如果存在,查找NOTE_ID case URI_DATA: if (values.containsKey(DataColumns.NOTE_ID)) { noteId = values.getAsLong(DataColumns.NOTE_ID); @@ -163,10 +158,11 @@ public class NotesProvider extends ContentProvider { } insertedId = dataId = db.insert(TABLE.DATA, null, values); break; - default: + default: throw new IllegalArgumentException("Unknown URI " + uri); } // Notify the note uri + // notifyChange获得一个ContextResolver对象并且更新里面的内容 if (noteId > 0) { getContext().getContentResolver().notifyChange( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null); @@ -178,13 +174,17 @@ public class NotesProvider extends ContentProvider { ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null); } + // 返回插入的uri的路径 return ContentUris.withAppendedId(uri, insertedId); } @Override + // 删除一个uri public int delete(Uri uri, String selection, String[] selectionArgs) { + // Uri代表要操作的数据,Android上可用的每种资源 -包括 图像、视频片段、音频资源等都可以用Uri来表示。 int count = 0; String id = null; + // 获得可写的数据库 SQLiteDatabase db = mHelper.getWritableDatabase(); boolean deleteData = false; switch (mMatcher.match(uri)) { @@ -228,78 +228,96 @@ public class NotesProvider extends ContentProvider { } @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; - switch (mMatcher.match(uri)) { - case URI_NOTE: - 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); - 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); - } +// 更新一个uri +public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { + int count = 0; // 记录更新的行数 + String id = null; // 用于存储URI中的ID部分 + SQLiteDatabase db = mHelper.getWritableDatabase(); // 获取可写的数据库实例 + boolean updateData = false; // 标记是否需要更新数据表 - if (count > 0) { - if (updateData) { - getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); - } - getContext().getContentResolver().notifyChange(uri, null); - } - return count; + // 根据URI匹配不同的操作 + switch (mMatcher.match(uri)) { + case URI_NOTE: + // 更新笔记表中的多条记录 + increaseNoteVersion(-1, selection, selectionArgs); // 增加笔记版本号 + count = db.update(TABLE.NOTE, values, selection, selectionArgs); // 执行更新操作 + break; + case URI_NOTE_ITEM: + // 更新笔记表中的单条记录 + id = uri.getPathSegments().get(1); // 从URI中获取ID + 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; // 设置更新数据表的标记为true + break; + case URI_DATA_ITEM: + // 更新数据表中的单条记录 + id = uri.getPathSegments().get(1); // 从URI中获取ID + count = db.update(TABLE.DATA, values, DataColumns.ID + "=" + id + + parseSelection(selection), selectionArgs); // 执行更新操作 + updateData = true; // 设置更新数据表的标记为true + break; + default: + // 如果URI不匹配任何已知类型,则抛出异常 + throw new IllegalArgumentException("Unknown URI " + uri); } - private String parseSelection(String selection) { - return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); + // 如果有行被更新,则通知相关URI的监听器 + if (count > 0) { + if (updateData) { + // 如果更新了数据表,则通知笔记表的URI + getContext().getContentResolver().notifyChange(Notes.CONTENT_NOTE_URI, null); + } + // 通知当前URI的监听器 + getContext().getContentResolver().notifyChange(uri, null); } + return count; // 返回更新的行数 +} - 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 "); +/// 将字符串解析成规定格式 +private String parseSelection(String selection) { + // 如果传入的选择字符串不为空,则在 selection 前后添加 " AND (" 和 ")",否则返回空字符串 + return (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')' : ""); +} - if (id > 0 || !TextUtils.isEmpty(selection)) { - 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(selectString); - } +// 增加一个noteVersion +private void increaseNoteVersion(long id, String selection, String[] selectionArgs) { + // 创建StringBuilder对象,用于构建SQL语句 + StringBuilder sql = new StringBuilder(120); + sql.append("UPDATE "); // 添加UPDATE关键字 + sql.append(TABLE.NOTE); // 添加表名NOTE + sql.append(" SET "); // 添加SET关键字 + sql.append(NoteColumns.VERSION); // 添加版本号列名 + sql.append("=" + NoteColumns.VERSION + "+1 "); // 版本号列值加1 - mHelper.getWritableDatabase().execSQL(sql.toString()); + // 如果ID大于0或者选择字符串不为空,则添加WHERE关键字 + if (id > 0 || !TextUtils.isEmpty(selection)) { + sql.append(" WHERE "); } - - @Override - public String getType(Uri uri) { - // TODO Auto-generated method stub - return null; + // 如果ID大于0,则添加ID等于指定值的条件 + 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(selectString); } + // 使用execSQL方法执行构建的SQL语句 + mHelper.getWritableDatabase().execSQL(sql.toString()); +} + +@Override +public String getType(Uri uri) { + // TODO: 需要实现该方法来返回给定URI的MIME类型,暂时返回null + return null; }