From afc01b235c8c975656ca8a31b2bb9e66724dbc84 Mon Sep 17 00:00:00 2001 From: hzy <1915048504@qq.com> Date: Thu, 12 Jun 2025 15:39:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BAtool=E4=BB=A3=E7=A0=81=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E5=A4=B9=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 --- .../net/micode/notes/tool/BackupUtils.java | 121 ++++++++++++++---- .../java/net/micode/notes/tool/DataUtils.java | 86 ++++++++++++- .../micode/notes/tool/GTaskStringUtils.java | 52 +++++++- .../net/micode/notes/tool/ResourceParser.java | 99 +++++++++++++- 4 files changed, 324 insertions(+), 34 deletions(-) diff --git a/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/BackupUtils.java b/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/BackupUtils.java index 39f6ec4..f6e2cf6 100644 --- a/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/BackupUtils.java +++ b/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/BackupUtils.java @@ -35,12 +35,19 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; - +/** + * 该类用于实现笔记数据的备份功能,将笔记数据导出为文本文件。 + */ public class BackupUtils { private static final String TAG = "BackupUtils"; - // Singleton stuff + // 单例实例 private static BackupUtils sInstance; + /** + * 获取BackupUtils的单例实例 + * @param context 上下文对象 + * @return BackupUtils的单例实例 + */ public static synchronized BackupUtils getInstance(Context context) { if (sInstance == null) { sInstance = new BackupUtils(context); @@ -49,43 +56,67 @@ public class BackupUtils { } /** - * Following states are signs to represents backup or restore - * status + * 以下状态常量用于表示备份或恢复操作的状态 */ - // Currently, the sdcard is not mounted + // 当前SD卡未挂载 public static final int STATE_SD_CARD_UNMOUONTED = 0; - // The backup file not exist + // 备份文件不存在 public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; - // The data is not well formated, may be changed by other programs + // 数据格式不正确,可能被其他程序修改 public static final int STATE_DATA_DESTROIED = 2; - // Some run-time exception which causes restore or backup fails + // 运行时异常导致备份或恢复失败 public static final int STATE_SYSTEM_ERROR = 3; - // Backup or restore success + // 备份或恢复成功 public static final int STATE_SUCCESS = 4; + // 文本导出工具类实例 private TextExport mTextExport; + /** + * 构造函数,初始化文本导出工具类 + * @param context 上下文对象 + */ private BackupUtils(Context context) { mTextExport = new TextExport(context); } + /** + * 检查外部存储是否可用 + * @return 如果外部存储已挂载返回true,否则返回false + */ private static boolean externalStorageAvailable() { return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } + /** + * 导出笔记数据为文本文件 + * @return 导出操作的状态码 + */ public int exportToText() { return mTextExport.exportToText(); } + /** + * 获取导出的文本文件的文件名 + * @return 文件名 + */ public String getExportedTextFileName() { return mTextExport.mFileName; } + /** + * 获取导出的文本文件的存储目录 + * @return 存储目录 + */ public String getExportedTextFileDir() { return mTextExport.mFileDirectory; } + /** + * 内部类,用于实现文本导出功能 + */ private static class TextExport { + // 查询笔记的投影列 private static final String[] NOTE_PROJECTION = { NoteColumns.ID, NoteColumns.MODIFIED_DATE, @@ -93,12 +124,16 @@ public class BackupUtils { NoteColumns.TYPE }; + // 笔记ID列的索引 private static final int NOTE_COLUMN_ID = 0; + // 笔记修改日期列的索引 private static final int NOTE_COLUMN_MODIFIED_DATE = 1; + // 笔记摘要列的索引 private static final int NOTE_COLUMN_SNIPPET = 2; + // 查询数据的投影列 private static final String[] DATA_PROJECTION = { DataColumns.CONTENT, DataColumns.MIME_TYPE, @@ -108,23 +143,38 @@ public class BackupUtils { DataColumns.DATA4, }; + // 数据内容列的索引 private static final int DATA_COLUMN_CONTENT = 0; + // 数据MIME类型列的索引 private static final int DATA_COLUMN_MIME_TYPE = 1; + // 通话日期列的索引 private static final int DATA_COLUMN_CALL_DATE = 2; + // 电话号码列的索引 private static final int DATA_COLUMN_PHONE_NUMBER = 4; + // 文本格式数组 private final String [] TEXT_FORMAT; + // 文件夹名称格式的索引 private static final int FORMAT_FOLDER_NAME = 0; + // 笔记日期格式的索引 private static final int FORMAT_NOTE_DATE = 1; + // 笔记内容格式的索引 private static final int FORMAT_NOTE_CONTENT = 2; + // 上下文对象 private Context mContext; + // 导出的文件名 private String mFileName; + // 导出文件的存储目录 private String mFileDirectory; + /** + * 构造函数,初始化上下文、文本格式、文件名和存储目录 + * @param context 上下文对象 + */ public TextExport(Context context) { TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); mContext = context; @@ -132,15 +182,22 @@ public class BackupUtils { mFileDirectory = ""; } + /** + * 根据索引获取文本格式 + * @param id 格式索引 + * @return 对应的文本格式字符串 + */ private String getFormat(int id) { return TEXT_FORMAT[id]; } /** - * Export the folder identified by folder id to text + * 将指定文件夹下的笔记导出为文本 + * @param folderId 文件夹ID + * @param ps 打印流 */ private void exportFolderToText(String folderId, PrintStream ps) { - // Query notes belong to this folder + // 查询该文件夹下的笔记 Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[] { folderId @@ -149,11 +206,11 @@ public class BackupUtils { if (notesCursor != null) { if (notesCursor.moveToFirst()) { do { - // Print note's last modified date + // 打印笔记的最后修改日期 ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( mContext.getString(R.string.format_datetime_mdhm), notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); - // Query data belong to this note + // 查询该笔记的数据 String noteId = notesCursor.getString(NOTE_COLUMN_ID); exportNoteToText(noteId, ps); } while (notesCursor.moveToNext()); @@ -163,9 +220,12 @@ public class BackupUtils { } /** - * Export note identified by id to a print stream + * 将指定笔记导出为文本 + * @param noteId 笔记ID + * @param ps 打印流 */ private void exportNoteToText(String noteId, PrintStream ps) { + // 查询该笔记的数据 Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { noteId @@ -176,7 +236,7 @@ public class BackupUtils { do { String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); if (DataConstants.CALL_NOTE.equals(mimeType)) { - // Print phone number + // 打印电话号码 String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); String location = dataCursor.getString(DATA_COLUMN_CONTENT); @@ -185,11 +245,11 @@ public class BackupUtils { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), phoneNumber)); } - // Print call date + // 打印通话日期 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat .format(mContext.getString(R.string.format_datetime_mdhm), callDate))); - // Print call attachment location + // 打印通话附件位置 if (!TextUtils.isEmpty(location)) { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), location)); @@ -205,7 +265,7 @@ public class BackupUtils { } dataCursor.close(); } - // print a line separator between note + // 打印笔记之间的分隔符 try { ps.write(new byte[] { Character.LINE_SEPARATOR, Character.LETTER_NUMBER @@ -216,9 +276,11 @@ public class BackupUtils { } /** - * Note will be exported as text which is user readable + * 将笔记导出为用户可读的文本文件 + * @return 导出操作的状态码 */ public int exportToText() { + // 检查外部存储是否可用 if (!externalStorageAvailable()) { Log.d(TAG, "Media was not mounted"); return STATE_SD_CARD_UNMOUONTED; @@ -229,7 +291,7 @@ public class BackupUtils { Log.e(TAG, "get print stream error"); return STATE_SYSTEM_ERROR; } - // First export folder and its notes + // 首先导出文件夹及其笔记 Cursor folderCursor = mContext.getContentResolver().query( Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, @@ -240,7 +302,7 @@ public class BackupUtils { if (folderCursor != null) { if (folderCursor.moveToFirst()) { do { - // Print folder's name + // 打印文件夹名称 String folderName = ""; if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { folderName = mContext.getString(R.string.call_record_folder_name); @@ -257,7 +319,7 @@ public class BackupUtils { folderCursor.close(); } - // Export notes in root's folder + // 导出根文件夹下的笔记 Cursor noteCursor = mContext.getContentResolver().query( Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, @@ -270,7 +332,7 @@ public class BackupUtils { ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( mContext.getString(R.string.format_datetime_mdhm), noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); - // Query data belong to this note + // 查询该笔记的数据 String noteId = noteCursor.getString(NOTE_COLUMN_ID); exportNoteToText(noteId, ps); } while (noteCursor.moveToNext()); @@ -283,7 +345,8 @@ public class BackupUtils { } /** - * Get a print stream pointed to the file {@generateExportedTextFile} + * 获取指向导出文本文件的打印流 + * @return 打印流对象,如果获取失败返回null */ private PrintStream getExportToTextPrintStream() { File file = generateFileMountedOnSDcard(mContext, R.string.file_path, @@ -310,7 +373,11 @@ public class BackupUtils { } /** - * Generate the text file to store imported data + * 在SD卡上生成用于存储导入数据的文本文件 + * @param context 上下文对象 + * @param filePathResId 文件路径资源ID + * @param fileNameFormatResId 文件名格式资源ID + * @return 生成的文件对象,如果生成失败返回null */ private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { StringBuilder sb = new StringBuilder(); @@ -339,6 +406,4 @@ public class BackupUtils { return null; } -} - - +} \ No newline at end of file diff --git a/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/DataUtils.java b/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/DataUtils.java index 2a14982..9cc819e 100644 --- a/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/DataUtils.java +++ b/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/DataUtils.java @@ -34,9 +34,18 @@ import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; import java.util.ArrayList; import java.util.HashSet; - +/** + * 该类提供了一系列与笔记数据操作相关的工具方法,如批量删除、移动笔记等。 + */ public class DataUtils { public static final String TAG = "DataUtils"; + + /** + * 批量删除笔记 + * @param resolver 内容解析器 + * @param ids 要删除的笔记ID集合 + * @return 如果删除成功返回true,否则返回false + */ public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { if (ids == null) { Log.d(TAG, "the ids is null"); @@ -72,6 +81,13 @@ public class DataUtils { return false; } + /** + * 将笔记移动到指定文件夹 + * @param resolver 内容解析器 + * @param id 笔记ID + * @param srcFolderId 源文件夹ID + * @param desFolderId 目标文件夹ID + */ public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { ContentValues values = new ContentValues(); values.put(NoteColumns.PARENT_ID, desFolderId); @@ -80,6 +96,13 @@ public class DataUtils { resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); } + /** + * 批量将笔记移动到指定文件夹 + * @param resolver 内容解析器 + * @param ids 要移动的笔记ID集合 + * @param folderId 目标文件夹ID + * @return 如果移动成功返回true,否则返回false + */ public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { if (ids == null) { @@ -112,7 +135,9 @@ public class DataUtils { } /** - * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} + * 获取用户文件夹的数量(不包括系统文件夹) + * @param resolver 内容解析器 + * @return 用户文件夹的数量 */ public static int getUserFolderCount(ContentResolver resolver) { Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, @@ -136,6 +161,13 @@ public class DataUtils { return count; } + /** + * 检查笔记是否在数据库中可见 + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @param type 笔记类型 + * @return 如果笔记可见返回true,否则返回false + */ public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, @@ -153,6 +185,12 @@ public class DataUtils { return exist; } + /** + * 检查笔记是否存在于数据库中 + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @return 如果笔记存在返回true,否则返回false + */ public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, null, null, null); @@ -167,6 +205,12 @@ public class DataUtils { return exist; } + /** + * 检查数据是否存在于数据库中 + * @param resolver 内容解析器 + * @param dataId 数据ID + * @return 如果数据存在返回true,否则返回false + */ public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null, null, null, null); @@ -181,6 +225,12 @@ public class DataUtils { return exist; } + /** + * 检查可见文件夹名称是否已存在 + * @param resolver 内容解析器 + * @param name 文件夹名称 + * @return 如果文件夹名称已存在返回true,否则返回false + */ public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + @@ -197,6 +247,12 @@ public class DataUtils { return exist; } + /** + * 获取指定文件夹下的笔记小部件集合 + * @param resolver 内容解析器 + * @param folderId 文件夹ID + * @return 笔记小部件集合 + */ public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, @@ -224,6 +280,12 @@ public class DataUtils { return set; } + /** + * 根据笔记ID获取通话号码 + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @return 通话号码,如果未找到返回空字符串 + */ public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.PHONE_NUMBER }, @@ -243,6 +305,13 @@ public class DataUtils { return ""; } + /** + * 根据电话号码和通话日期获取笔记ID + * @param resolver 内容解析器 + * @param phoneNumber 电话号码 + * @param callDate 通话日期 + * @return 笔记ID,如果未找到返回0 + */ public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.NOTE_ID }, @@ -264,6 +333,12 @@ public class DataUtils { return 0; } + /** + * 根据笔记ID获取笔记摘要 + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @return 笔记摘要,如果未找到抛出异常 + */ public static String getSnippetById(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, new String [] { NoteColumns.SNIPPET }, @@ -282,6 +357,11 @@ public class DataUtils { throw new IllegalArgumentException("Note is not found with id: " + noteId); } + /** + * 格式化笔记摘要,去除前后空格并截取第一行 + * @param snippet 笔记摘要 + * @return 格式化后的笔记摘要 + */ public static String getFormattedSnippet(String snippet) { if (snippet != null) { snippet = snippet.trim(); @@ -292,4 +372,4 @@ public class DataUtils { } return snippet; } -} +} \ No newline at end of file diff --git a/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java b/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java index 666b729..e65090e 100644 --- a/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java +++ b/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/GTaskStringUtils.java @@ -16,98 +16,146 @@ package net.micode.notes.tool; +/** + * 该类包含与Google任务相关的JSON字符串常量,用于处理Google任务相关的数据交互。 + */ public class GTaskStringUtils { + // Google任务JSON中的动作ID字段 public final static String GTASK_JSON_ACTION_ID = "action_id"; + // Google任务JSON中的动作列表字段 public final static String GTASK_JSON_ACTION_LIST = "action_list"; + // Google任务JSON中的动作类型字段 public final static String GTASK_JSON_ACTION_TYPE = "action_type"; + // Google任务JSON中创建动作的类型值 public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; + // Google任务JSON中获取所有任务动作的类型值 public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; + // Google任务JSON中移动任务动作的类型值 public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; + // Google任务JSON中更新任务动作的类型值 public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; + // Google任务JSON中的创建者ID字段 public final static String GTASK_JSON_CREATOR_ID = "creator_id"; + // Google任务JSON中的子实体字段 public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; + // Google任务JSON中的客户端版本字段 public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; + // Google任务JSON中的完成状态字段 public final static String GTASK_JSON_COMPLETED = "completed"; + // Google任务JSON中的当前列表ID字段 public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; + // Google任务JSON中的默认列表ID字段 public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; + // Google任务JSON中的删除状态字段 public final static String GTASK_JSON_DELETED = "deleted"; + // Google任务JSON中的目标列表字段 public final static String GTASK_JSON_DEST_LIST = "dest_list"; + // Google任务JSON中的目标父级字段 public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; + // Google任务JSON中的目标父级类型字段 public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; + // Google任务JSON中的实体增量字段 public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; + // Google任务JSON中的实体类型字段 public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; + // Google任务JSON中获取已删除任务的字段 public final static String GTASK_JSON_GET_DELETED = "get_deleted"; + // Google任务JSON中的ID字段 public final static String GTASK_JSON_ID = "id"; + // Google任务JSON中的索引字段 public final static String GTASK_JSON_INDEX = "index"; + // Google任务JSON中的最后修改时间字段 public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; + // Google任务JSON中的最新同步点字段 public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; + // Google任务JSON中的列表ID字段 public final static String GTASK_JSON_LIST_ID = "list_id"; + // Google任务JSON中的列表集合字段 public final static String GTASK_JSON_LISTS = "lists"; + // Google任务JSON中的名称字段 public final static String GTASK_JSON_NAME = "name"; + // Google任务JSON中的新ID字段 public final static String GTASK_JSON_NEW_ID = "new_id"; + // Google任务JSON中的备注字段 public final static String GTASK_JSON_NOTES = "notes"; + // Google任务JSON中的父级ID字段 public final static String GTASK_JSON_PARENT_ID = "parent_id"; + // Google任务JSON中的前一个兄弟ID字段 public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; + // Google任务JSON中的结果字段 public final static String GTASK_JSON_RESULTS = "results"; + // Google任务JSON中的源列表字段 public final static String GTASK_JSON_SOURCE_LIST = "source_list"; + // Google任务JSON中的任务集合字段 public final static String GTASK_JSON_TASKS = "tasks"; + // Google任务JSON中的类型字段 public final static String GTASK_JSON_TYPE = "type"; + // Google任务JSON中组类型的值 public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; + // Google任务JSON中任务类型的值 public final static String GTASK_JSON_TYPE_TASK = "TASK"; + // Google任务JSON中的用户字段 public final static String GTASK_JSON_USER = "user"; + // MIUI文件夹前缀 public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; + // 默认文件夹名称 public final static String FOLDER_DEFAULT = "Default"; + // 通话记录文件夹名称 public final static String FOLDER_CALL_NOTE = "Call_Note"; + // 元数据文件夹名称 public final static String FOLDER_META = "METADATA"; + // 元数据头部的Google任务ID字段 public final static String META_HEAD_GTASK_ID = "meta_gid"; + // 元数据头部的备注字段 public final static String META_HEAD_NOTE = "meta_note"; + // 元数据头部的数据字段 public final static String META_HEAD_DATA = "meta_data"; + // 元数据备注名称 public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; - -} +} \ No newline at end of file diff --git a/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/ResourceParser.java b/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/ResourceParser.java index 1ad3ad6..dcf7869 100644 --- a/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/ResourceParser.java +++ b/src/Notes/Notes/app/src/main/java/net/micode/notes/tool/ResourceParser.java @@ -22,24 +22,42 @@ import android.preference.PreferenceManager; import net.micode.notes.R; import net.micode.notes.ui.NotesPreferenceActivity; +/** + * 该类用于解析和管理应用中的资源,如背景颜色、字体大小等。 + */ public class ResourceParser { + // 黄色背景常量 public static final int YELLOW = 0; + // 蓝色背景常量 public static final int BLUE = 1; + // 白色背景常量 public static final int WHITE = 2; + // 绿色背景常量 public static final int GREEN = 3; + // 红色背景常量 public static final int RED = 4; + // 默认背景颜色为黄色 public static final int BG_DEFAULT_COLOR = YELLOW; + // 小字体常量 public static final int TEXT_SMALL = 0; + // 中等字体常量 public static final int TEXT_MEDIUM = 1; + // 大字体常量 public static final int TEXT_LARGE = 2; + // 超大字体常量 public static final int TEXT_SUPER = 3; + // 默认字体大小为中等 public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; + /** + * 笔记编辑时的背景资源管理类 + */ public static class NoteBgResources { + // 笔记编辑时的背景资源数组 private final static int [] BG_EDIT_RESOURCES = new int [] { R.drawable.edit_yellow, R.drawable.edit_blue, @@ -48,6 +66,7 @@ public class ResourceParser { R.drawable.edit_red }; + // 笔记编辑时标题的背景资源数组 private final static int [] BG_EDIT_TITLE_RESOURCES = new int [] { R.drawable.edit_title_yellow, R.drawable.edit_title_blue, @@ -56,25 +75,47 @@ public class ResourceParser { R.drawable.edit_title_red }; + /** + * 根据背景ID获取笔记编辑时的背景资源 + * @param id 背景ID + * @return 对应的背景资源ID + */ public static int getNoteBgResource(int id) { return BG_EDIT_RESOURCES[id]; } + /** + * 根据背景ID获取笔记编辑时标题的背景资源 + * @param id 背景ID + * @return 对应的标题背景资源ID + */ public static int getNoteTitleBgResource(int id) { return BG_EDIT_TITLE_RESOURCES[id]; } } + /** + * 获取默认的背景ID + * @param context 上下文对象 + * @return 默认背景ID + */ public static int getDefaultBgId(Context context) { + // 检查是否设置了自定义背景颜色 if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { + // 如果设置了,随机选择一个背景ID return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); } else { + // 如果未设置,返回默认背景ID return BG_DEFAULT_COLOR; } } + /** + * 笔记列表项的背景资源管理类 + */ public static class NoteItemBgResources { + // 笔记列表项第一个的背景资源数组 private final static int [] BG_FIRST_RESOURCES = new int [] { R.drawable.list_yellow_up, R.drawable.list_blue_up, @@ -83,6 +124,7 @@ public class ResourceParser { R.drawable.list_red_up }; + // 笔记列表项中间的背景资源数组 private final static int [] BG_NORMAL_RESOURCES = new int [] { R.drawable.list_yellow_middle, R.drawable.list_blue_middle, @@ -91,6 +133,7 @@ public class ResourceParser { R.drawable.list_red_middle }; + // 笔记列表项最后一个的背景资源数组 private final static int [] BG_LAST_RESOURCES = new int [] { R.drawable.list_yellow_down, R.drawable.list_blue_down, @@ -99,6 +142,7 @@ public class ResourceParser { R.drawable.list_red_down, }; + // 笔记列表项单个的背景资源数组 private final static int [] BG_SINGLE_RESOURCES = new int [] { R.drawable.list_yellow_single, R.drawable.list_blue_single, @@ -107,28 +151,56 @@ public class ResourceParser { R.drawable.list_red_single }; + /** + * 根据背景ID获取笔记列表项第一个的背景资源 + * @param id 背景ID + * @return 对应的背景资源ID + */ public static int getNoteBgFirstRes(int id) { return BG_FIRST_RESOURCES[id]; } + /** + * 根据背景ID获取笔记列表项最后一个的背景资源 + * @param id 背景ID + * @return 对应的背景资源ID + */ public static int getNoteBgLastRes(int id) { return BG_LAST_RESOURCES[id]; } + /** + * 根据背景ID获取笔记列表项单个的背景资源 + * @param id 背景ID + * @return 对应的背景资源ID + */ public static int getNoteBgSingleRes(int id) { return BG_SINGLE_RESOURCES[id]; } + /** + * 根据背景ID获取笔记列表项中间的背景资源 + * @param id 背景ID + * @return 对应的背景资源ID + */ public static int getNoteBgNormalRes(int id) { return BG_NORMAL_RESOURCES[id]; } + /** + * 获取文件夹的背景资源 + * @return 文件夹背景资源ID + */ public static int getFolderBgRes() { return R.drawable.list_folder; } } + /** + * 小部件的背景资源管理类 + */ public static class WidgetBgResources { + // 2x小部件的背景资源数组 private final static int [] BG_2X_RESOURCES = new int [] { R.drawable.widget_2x_yellow, R.drawable.widget_2x_blue, @@ -137,10 +209,16 @@ public class ResourceParser { R.drawable.widget_2x_red, }; + /** + * 根据背景ID获取2x小部件的背景资源 + * @param id 背景ID + * @return 对应的背景资源ID + */ public static int getWidget2xBgResource(int id) { return BG_2X_RESOURCES[id]; } + // 4x小部件的背景资源数组 private final static int [] BG_4X_RESOURCES = new int [] { R.drawable.widget_4x_yellow, R.drawable.widget_4x_blue, @@ -149,12 +227,21 @@ public class ResourceParser { R.drawable.widget_4x_red }; + /** + * 根据背景ID获取4x小部件的背景资源 + * @param id 背景ID + * @return 对应的背景资源ID + */ public static int getWidget4xBgResource(int id) { return BG_4X_RESOURCES[id]; } } + /** + * 文本外观资源管理类 + */ public static class TextAppearanceResources { + // 文本外观资源数组 private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { R.style.TextAppearanceNormal, R.style.TextAppearanceMedium, @@ -162,20 +249,30 @@ public class ResourceParser { R.style.TextAppearanceSuper }; + /** + * 根据ID获取文本外观资源 + * @param id 文本外观ID + * @return 对应的文本外观资源ID + */ public static int getTexAppearanceResource(int id) { /** * HACKME: Fix bug of store the resource id in shared preference. * The id may larger than the length of resources, in this case, * return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE} */ + // 检查ID是否超出资源数组长度,如果超出则返回默认字体大小对应的资源ID if (id >= TEXTAPPEARANCE_RESOURCES.length) { return BG_DEFAULT_FONT_SIZE; } return TEXTAPPEARANCE_RESOURCES[id]; } + /** + * 获取文本外观资源数组的长度 + * @return 资源数组长度 + */ public static int getResourcesSize() { return TEXTAPPEARANCE_RESOURCES.length; } } -} +} \ No newline at end of file