From 06e19e87373a3b0be297615e3dd2c46d841847b7 Mon Sep 17 00:00:00 2001 From: STRIV1 <26755173368@qq.com> Date: Mon, 30 Dec 2024 16:56:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Contact.java | 28 ++++-- Notes.java | 243 +++++++++++++++++++-------------------------------- 2 files changed, 109 insertions(+), 162 deletions(-) diff --git a/Contact.java b/Contact.java index d97ac5d..e81a5ad 100644 --- a/Contact.java +++ b/Contact.java @@ -26,9 +26,11 @@ import android.util.Log; import java.util.HashMap; public class Contact { + // 用于缓存联系人信息,避免多次查询同一个号码 private static HashMap sContactCache; - private static final String TAG = "Contact"; + private static final String TAG = "Contact"; // 用于Log输出的TAG + // 用于查询联系人名称的SQL条件,依赖于手机号的最小匹配格式 private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER + ",?) AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'" + " AND " + Data.RAW_CONTACT_ID + " IN " @@ -36,38 +38,50 @@ public class Contact { + " FROM phone_lookup" + " WHERE min_match = '+')"; + // 获取某个电话号对应的联系人名称 public static String getContact(Context context, String phoneNumber) { + // 如果缓存为空,初始化缓存 if(sContactCache == null) { sContactCache = new HashMap(); } + // 如果缓存中已有该电话号码的联系人信息,直接返回 if(sContactCache.containsKey(phoneNumber)) { return sContactCache.get(phoneNumber); } + // 通过PhoneNumberUtils将电话号码转为符合最小匹配格式的号码 String selection = CALLER_ID_SELECTION.replace("+", PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); + + // 查询联系人数据库,根据号码查找联系人名称 Cursor cursor = context.getContentResolver().query( - Data.CONTENT_URI, - new String [] { Phone.DISPLAY_NAME }, - selection, - new String[] { phoneNumber }, - null); + Data.CONTENT_URI, // 数据源:联系人数据 + new String[] { Phone.DISPLAY_NAME }, // 查询字段:联系人名称 + selection, // 查询条件:号码与条件匹配 + new String[] { phoneNumber }, // 查询参数:电话号码 + null); // 排序 + // 如果查询成功,处理返回结果 if (cursor != null && cursor.moveToFirst()) { try { + // 获取查询到的联系人名称 String name = cursor.getString(0); + // 将查询到的联系人名称缓存起来 sContactCache.put(phoneNumber, name); return name; } catch (IndexOutOfBoundsException e) { + // 如果查询结果异常,记录日志 Log.e(TAG, " Cursor get string error " + e.toString()); return null; } finally { + // 最后关闭游标,释放资源 cursor.close(); } } else { + // 如果没有找到匹配的联系人,记录日志并返回null Log.d(TAG, "No contact matched with number:" + phoneNumber); return null; } } -} +} \ No newline at end of file diff --git a/Notes.java b/Notes.java index f240604..2ba5674 100644 --- a/Notes.java +++ b/Notes.java @@ -14,211 +14,143 @@ * limitations under the License. */ -package net.micode.notes.data; +package net.micode.notes.data; // 定义了包名,表示这个类属于 net.micode.notes.data 包 -import android.net.Uri; -public class Notes { +import android.net.Uri; // 导入 Android 的 Uri 类,用于处理内容提供者的 URI + +public class Notes { // Notes 类,主要用于定义与笔记相关的常量和 URI 配置 + + // 内容提供者的 authority 字符串,用于唯一标识此内容提供者 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; + + // 定义了笔记、文件夹、系统三种类型的常量 + 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 + * 以下常量是系统文件夹的标识符 + * {@link Notes#ID_ROOT_FOLDER} 是默认文件夹 + * {@link Notes#ID_TEMPARAY_FOLDER} 是存储不属于任何文件夹的笔记 + * {@link Notes#ID_CALL_RECORD_FOLDER} 用于存储通话记录 */ - 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 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; // 2x 小部件类型 + public static final int TYPE_WIDGET_4X = 1; // 4x 小部件类型 + + // 数据常量类,定义了笔记和通话记录的内容类型 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; // 笔记的内容类型 + public static final String CALL_NOTE = CallNote.CONTENT_ITEM_TYPE; // 通话记录的内容类型 } /** - * Uri to query all notes and folders + * 用于查询所有笔记和文件夹的 URI */ - public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); + public static final Uri CONTENT_NOTE_URI = Uri.parse("content://" + AUTHORITY + "/note"); // 查询笔记的 URI /** - * Uri to query data + * 用于查询数据的 URI */ - public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); + public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); // 查询数据的 URI + // 笔记相关的列定义接口,包含笔记的数据列 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)

+ * 每一行的唯一 ID + *

类型: INTEGER (long)

*/ - public static final String PARENT_ID = "parent_id"; + public static final String ID = "_id"; // 唯一标识符 /** - * Created data for note or folder - *

Type: INTEGER (long)

+ * 笔记或文件夹的父 ID + *

类型: INTEGER (long)

*/ - public static final String CREATED_DATE = "created_date"; + public static final String PARENT_ID = "parent_id"; // 父级 ID /** - * Latest modified date - *

Type: INTEGER (long)

+ * 笔记或文件夹的创建日期 + *

类型: INTEGER (long)

*/ - public static final String MODIFIED_DATE = "modified_date"; - + public static final String CREATED_DATE = "created_date"; // 创建日期 /** - * Alert date - *

Type: INTEGER (long)

+ * 笔记或文件夹的最后修改日期 + *

类型: INTEGER (long)

*/ - public static final String ALERTED_DATE = "alert_date"; + public static final String MODIFIED_DATE = "modified_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"; + /** + * The constant fields represent various columns of a note-taking application database. + * These constants are used to define column names and their data types within the database schema. + */ +public static final String ALERTED_DATE = "alert_date"; // 记录提醒日期,数据类型:INTEGER(长整型) - /** - * Note's widget type - *

Type: INTEGER (long)

- */ - public static final String WIDGET_TYPE = "widget_type"; +public static final String SNIPPET = "snippet"; // 记录文件夹名称或笔记的文本内容,数据类型:TEXT(字符串) - /** - * Note's background color's id - *

Type: INTEGER (long)

- */ - public static final String BG_COLOR_ID = "bg_color_id"; +public static final String WIDGET_ID = "widget_id"; // 记录笔记的小部件ID,数据类型:INTEGER(长整型) - /** - * 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"; +public static final String WIDGET_TYPE = "widget_type"; // 记录笔记的小部件类型,数据类型:INTEGER(长整型) - /** - * Folder's count of notes - *

Type: INTEGER (long)

- */ - public static final String NOTES_COUNT = "notes_count"; +public static final String BG_COLOR_ID = "bg_color_id"; // 记录笔记的背景颜色ID,数据类型:INTEGER(长整型) - /** - * The file type: folder or note - *

Type: INTEGER

- */ - public static final String TYPE = "type"; +public static final String HAS_ATTACHMENT = "has_attachment"; // 标记笔记是否有附件,文本笔记没有附件,多媒体笔记至少有一个附件,数据类型:INTEGER(整型) - /** - * The last sync id - *

Type: INTEGER (long)

- */ - public static final String SYNC_ID = "sync_id"; +public static final String NOTES_COUNT = "notes_count"; // 文件夹中笔记的数量,数据类型:INTEGER(长整型) - /** - * Sign to indicate local modified or not - *

Type: INTEGER

- */ - public static final String LOCAL_MODIFIED = "local_modified"; +public static final String TYPE = "type"; // 文件类型,标识为文件夹或笔记,数据类型:INTEGER(整型) - /** - * Original parent id before moving into temporary folder - *

Type : INTEGER

- */ - public static final String ORIGIN_PARENT_ID = "origin_parent_id"; +public static final String SYNC_ID = "sync_id"; // 记录上次同步的ID,数据类型:INTEGER(长整型) - /** - * The gtask id - *

Type : TEXT

- */ - public static final String GTASK_ID = "gtask_id"; +public static final String LOCAL_MODIFIED = "local_modified"; // 本地修改标识,用来表示该项是否被本地修改过,数据类型:INTEGER(整型) - /** - * The version code - *

Type : INTEGER (long)

- */ - public static final String VERSION = "version"; - } +public static final String ORIGIN_PARENT_ID = "origin_parent_id"; // 记录笔记或文件夹在移动到临时文件夹之前的原始父级ID,数据类型:INTEGER(整型) - public interface DataColumns { - /** - * The unique ID for a row - *

Type: INTEGER (long)

- */ - public static final String ID = "_id"; +public static final String GTASK_ID = "gtask_id"; // 记录Google任务的ID,数据类型:TEXT(字符串) - /** - * The MIME type of the item represented by this row. - *

Type: Text

- */ - public static final String MIME_TYPE = "mime_type"; +public static final String VERSION = "version"; // 记录笔记或文件夹的版本号,数据类型:INTEGER(长整型) - /** - * The reference id to note that this data belongs to - *

Type: INTEGER (long)

- */ - public static final String NOTE_ID = "note_id"; +/** + * DataColumns接口用于定义存储在数据表中的列字段,通常用于数据相关的记录。 + */ +public interface DataColumns { + public static final String ID = "_id"; // 记录行的唯一标识符,数据类型:INTEGER(长整型) - /** - * Created data for note or folder - *

Type: INTEGER (long)

- */ - public static final String CREATED_DATE = "created_date"; + public static final String MIME_TYPE = "mime_type"; // 记录数据项的MIME类型,数据类型:TEXT(字符串) - /** - * Latest modified date - *

Type: INTEGER (long)

- */ - public static final String MODIFIED_DATE = "modified_date"; + public static final String NOTE_ID = "note_id"; // 记录数据所对应的笔记ID,数据类型:INTEGER(长整型) - /** - * Data's content - *

Type: TEXT

- */ - public static final String CONTENT = "content"; + public static final String CREATED_DATE = "created_date"; // 记录数据创建的日期,数据类型:INTEGER(长整型) + public static final String MODIFIED_DATE = "modified_date"; // 记录数据最后修改的日期,数据类型:INTEGER(长整型) - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * integer data type - *

Type: INTEGER

- */ - public static final String DATA1 = "data1"; + public static final String CONTENT = "content"; // 记录数据的具体内容,数据类型:TEXT(字符串) - /** - * Generic data column, the meaning is {@link #MIMETYPE} specific, used for - * integer data type - *

Type: INTEGER

- */ - public static final String DATA2 = "data2"; + public static final String DATA1 = "data1"; // 一个通用数据列,具体意义根据MIME类型而定,数据类型:INTEGER(整型) + public static final String DATA2 = "data2"; // 另一个通用数据列,具体意义根据MIME类型而定,数据类型:INTEGER(整型) +} /** * Generic data column, the meaning is {@link #MIMETYPE} specific, used for * TEXT data type @@ -277,3 +209,4 @@ public class Notes { public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note"); } } +