From 74a663ea95b88fb7d87169a3546f06d0c0935f4d Mon Sep 17 00:00:00 2001 From: HZXhuang Date: Sat, 28 Oct 2023 12:11:41 +0800 Subject: [PATCH] =?UTF-8?q?=E9=98=85=E8=AF=BBContact=E5=92=8CNotes?= =?UTF-8?q?=E5=B9=B6=E6=B7=BB=E5=8A=A0=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mi_note | 1 - .../java/net/micode/notes/data/Contact.java | 24 ++++- .../java/net/micode/notes/data/Notes.java | 94 ++++++++++++++++--- 3 files changed, 101 insertions(+), 18 deletions(-) delete mode 160000 mi_note diff --git a/mi_note b/mi_note deleted file mode 160000 index d48acec..0000000 --- a/mi_note +++ /dev/null @@ -1 +0,0 @@ -Subproject commit d48acec0b0cae90d95d289c02545fcac5e7a1485 diff --git a/src/mi_note/app/src/main/java/net/micode/notes/data/Contact.java b/src/mi_note/app/src/main/java/net/micode/notes/data/Contact.java index d97ac5d..f7583b5 100644 --- a/src/mi_note/app/src/main/java/net/micode/notes/data/Contact.java +++ b/src/mi_note/app/src/main/java/net/micode/notes/data/Contact.java @@ -25,8 +25,14 @@ import android.util.Log; import java.util.HashMap; +/** + * @author hzx + * 版本:1.0 + * 创建日期:2023/10/28 + * 描述:Contact 联系人类 封装通过电话号码获取联系人姓名的接口 + */ public class Contact { - private static HashMap sContactCache; + private static HashMap sContactCache; // 缓存联系人号码和姓名的映射关系 private static final String TAG = "Contact"; private static final String CALLER_ID_SELECTION = "PHONE_NUMBERS_EQUAL(" + Phone.NUMBER @@ -34,19 +40,29 @@ public class Contact { + " AND " + Data.RAW_CONTACT_ID + " IN " + "(SELECT raw_contact_id " + " FROM phone_lookup" - + " WHERE min_match = '+')"; + + " WHERE min_match = '+')"; // 查询条件 + /** + * 根据电话号码获取联系人姓名 + * @param context 应用上下文 + * @param phoneNumber 电话号码 + * @return 联系人姓名 + */ public static String getContact(Context context, String phoneNumber) { if(sContactCache == null) { + // 初始化map缓存 sContactCache = new HashMap(); } if(sContactCache.containsKey(phoneNumber)) { + // 如果map缓存中的键包含要查询的电话号码,直接从缓存中读取 return sContactCache.get(phoneNumber); } + // 获取电话号码的min-match,然后替换原始查询条件中的“+” String selection = CALLER_ID_SELECTION.replace("+", PhoneNumberUtils.toCallerIDMinMatch(phoneNumber)); + // 从联系人内容提供者中查询联系人信息 Cursor cursor = context.getContentResolver().query( Data.CONTENT_URI, new String [] { Phone.DISPLAY_NAME }, @@ -54,18 +70,22 @@ public class Contact { new String[] { phoneNumber }, null); + // 读取查询结果 if (cursor != null && cursor.moveToFirst()) { try { String name = cursor.getString(0); + // 将这次电话号码和联系人姓名的匹配关系缓存起来 sContactCache.put(phoneNumber, name); return name; } catch (IndexOutOfBoundsException e) { + // 数组越界异常,从cursor取不到指定字段,输出日志信息 Log.e(TAG, " Cursor get string error " + e.toString()); return null; } finally { cursor.close(); } } else { + // 查询结果为空,输出日志信息 Log.d(TAG, "No contact matched with number:" + phoneNumber); return null; } diff --git a/src/mi_note/app/src/main/java/net/micode/notes/data/Notes.java b/src/mi_note/app/src/main/java/net/micode/notes/data/Notes.java index f240604..9acf62b 100644 --- a/src/mi_note/app/src/main/java/net/micode/notes/data/Notes.java +++ b/src/mi_note/app/src/main/java/net/micode/notes/data/Notes.java @@ -17,12 +17,19 @@ package net.micode.notes.data; import android.net.Uri; + +/** + * @author hzx + * 版本:1.0 + * 创建日期:2023/10/28 + * 描述:Notes 便签类,封装便签相关的属性和方法 + */ 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; + 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 @@ -30,10 +37,10 @@ public class Notes { * {@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 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"; @@ -46,42 +53,51 @@ public class Notes { public static final int TYPE_WIDGET_2X = 0; public static final int TYPE_WIDGET_4X = 1; + /** + * 封装一些数据常量 + */ public static class DataConstants { 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"); /** - * Uri to query data + * 查询所有数据的Uri */ public static final Uri CONTENT_DATA_URI = Uri.parse("content://" + AUTHORITY + "/data"); + /** + * 便签字段接口,封装每一个字段实际存储的名字 + */ public interface NoteColumns { /** - * The unique ID for a row + * 每一行的唯一ID *

Type: INTEGER (long)

*/ public static final String ID = "_id"; /** * The parent's id for note or folder + * 便签或文件夹的父ID *

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"; @@ -95,24 +111,28 @@ public class Notes { /** * Folder's name or text content of note + * 文件夹名或便签的文本内容 *

Type: TEXT

*/ public static final String SNIPPET = "snippet"; /** * Note's widget id + * 便签的小部件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 + * 便签的背景颜色ID *

Type: INTEGER (long)

*/ public static final String BG_COLOR_ID = "bg_color_id"; @@ -120,127 +140,149 @@ public class Notes { /** * 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 + * 最后一次云同步的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 + * 在移动到临时文件夹之前,文件夹或便签的父ID *

Type : INTEGER

*/ public static final String ORIGIN_PARENT_ID = "origin_parent_id"; /** * The gtask id + * 云同步ID *

Type : TEXT

*/ public static final String GTASK_ID = "gtask_id"; /** * The version code + * 版本号 *

Type : INTEGER (long)

*/ public static final String VERSION = "version"; } + /** + * 数据字段接口 + */ public interface DataColumns { /** * The unique ID for a row + * 行的唯一ID *

Type: INTEGER (long)

*/ public static final String ID = "_id"; /** * The MIME type of the item represented by this row. + * 这一行代表的项的MIME类型 *

Type: Text

*/ public static final String MIME_TYPE = "mime_type"; /** * The reference id to note that this data belongs to + * 这一行数据所属的便签的ID *

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 + * Generic data column, the meaning is {@link #MIME_TYPE} 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 + * Generic data column, the meaning is {@link #MIME_TYPE} 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 + * Generic data column, the meaning is {@link #MIME_TYPE} 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 + * Generic data column, the meaning is {@link #MIME_TYPE} 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 + * Generic data column, the meaning is {@link #MIME_TYPE} specific, used for * TEXT data type *

Type: TEXT

*/ public static final String DATA5 = "data5"; } + /** + * 数据字段接口的实现,文本便签 + */ public static final class TextNote implements DataColumns { /** * Mode to indicate the text in check list mode or not @@ -250,16 +292,29 @@ public class Notes { 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"; + /** + * 获取内容的Uri + */ public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/text_note"); } + /** + * 数据字段接口的实现,调用便签 + */ public static final class CallNote implements DataColumns { /** * Call date for this record + * 调用日期 *

Type: INTEGER (long)

*/ public static final String CALL_DATE = DATA1; @@ -270,10 +325,19 @@ public class Notes { */ 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"; + /** + * 获取内容的Uri + */ public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/call_note"); } }