diff --git a/doc/小米便签开源代码全文泛读报告 .docx b/doc/小米便签开源代码全文泛读报告 .docx deleted file mode 100644 index f063739..0000000 Binary files a/doc/小米便签开源代码全文泛读报告 .docx and /dev/null differ diff --git a/src/BackupUtils.java b/src/BackupUtils.java deleted file mode 100644 index 821cbd6..0000000 --- a/src/BackupUtils.java +++ /dev/null @@ -1,279 +0,0 @@ - -/* - * BackupUtils 工具类 - * 提供了笔记备份到文本文件的功能。 - */ - -package net.micode.notes.tool; - -// 引入需要用到的 Android 和 Java 标准库类 -import android.content.Context; -import android.database.Cursor; -import android.os.Environment; -import android.text.TextUtils; -import android.text.format.DateFormat; -import android.util.Log; - -import net.micode.notes.R; -import net.micode.notes.data.Notes; -import net.micode.notes.data.Notes.DataColumns; -import net.micode.notes.data.Notes.DataConstants; -import net.micode.notes.data.Notes.NoteColumns; - -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.PrintStream; - -/** - * BackupUtils 是一个工具类,用于实现笔记的备份功能。 - */ -public class BackupUtils { - - // 日志标志,用于记录类中的日志 - private static final String TAG = "BackupUtils"; - - // 单例模式:存储唯一实例 - private static BackupUtils sInstance; - - /** - * 获取 BackupUtils 类的单例实例。 - * - * @param context 上下文对象,用于初始化内部组件。 - * @return 返回 BackupUtils 实例。 - */ - public static synchronized BackupUtils getInstance(Context context) { - if (sInstance == null) { // 如果实例不存在,则创建新实例 - sInstance = new BackupUtils(context); - } - return sInstance; // 返回单例实例 - } - - /** - * 定义备份操作的状态常量 - */ - public static final int STATE_SD_CARD_UNMOUONTED = 0; // SD卡未挂载 - public static final int STATE_BACKUP_FILE_NOT_EXIST = 1; // 备份文件不存在 - public static final int STATE_DATA_DESTROIED = 2; // 数据已损坏 - public static final int STATE_SYSTEM_ERROR = 3; // 系统错误 - public static final int STATE_SUCCESS = 4; // 备份成功 - - // 用于文本导出的工具类 - private TextExport mTextExport; - - /** - * 私有化构造函数,防止直接实例化。 - * - * @param context 上下文对象,用于初始化导出工具类。 - */ - private BackupUtils(Context context) { - mTextExport = new TextExport(context); // 初始化文本导出工具 - } - - /** - * 检查 SD 卡是否可用。 - * - * @return 如果 SD 卡已挂载,则返回 true,否则返回 false。 - */ - private static boolean externalStorageAvailable() { - return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); - } - - /** - * 导出笔记到文本文件。 - * - * @return 返回导出操作的状态码。 - */ - public int exportToText() { - return mTextExport.exportToText(); // 调用 TextExport 类中的导出方法 - } - - /** - * 获取导出文本的文件名。 - * - * @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, // 笔记 ID - NoteColumns.MODIFIED_DATE, // 修改时间 - NoteColumns.SNIPPET, // 摘要 - NoteColumns.TYPE // 类型 - }; - - // 定义笔记列的索引 - private static final int NOTE_COLUMN_ID = 0; // ID 列索引 - 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, // MIME 类型 - DataColumns.DATA1, // 数据 1 - DataColumns.DATA2, // 数据 2 - DataColumns.DATA3, // 数据 3 - DataColumns.DATA4, // 数据 4 - }; - - // 定义数据列的索引 - private static final int DATA_COLUMN_CONTENT = 0; // 内容列索引 - private static final int DATA_COLUMN_MIME_TYPE = 1; // MIME 类型列索引 - - // 上下文对象,用于访问系统资源 - private Context mContext; - private String mFileName; // 文件名 - private String mFileDirectory; // 文件目录 - - // 初始化文本导出格式 - private final String[] TEXT_FORMAT; - - public TextExport(Context context) { - TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); // 导出格式 - mContext = context; - mFileName = ""; - mFileDirectory = ""; - } - - /** - * 根据文件夹 ID 将笔记导出为文本。 - * - * @param folderId 文件夹 ID。 - * @param ps 打印流,用于写入文本内容。 - */ - private void exportFolderToText(String folderId, PrintStream ps) { - Cursor notesCursor = mContext.getContentResolver().query(Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, - NoteColumns.PARENT_ID + "=?", new String[]{folderId}, null); - - if (notesCursor != null) { - if (notesCursor.moveToFirst()) { - do { - // 输出笔记的修改时间 - ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( - mContext.getString(R.string.format_datetime_mdhm), - notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); - // 导出笔记内容 - String noteId = notesCursor.getString(NOTE_COLUMN_ID); - exportNoteToText(noteId, ps); - } while (notesCursor.moveToNext()); - } - notesCursor.close(); - } - } - - /** - * 导出指定笔记 ID 的内容到文本。 - * - * @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}, null); - - if (dataCursor != null) { - if (dataCursor.moveToFirst()) { - do { - String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); - if (DataConstants.CALL_NOTE.equals(mimeType)) { - // 导出通话笔记 - } else if (DataConstants.NOTE.equals(mimeType)) { - // 导出普通笔记 - } - } while (dataCursor.moveToNext()); - } - dataCursor.close(); - } - } - - /** - * 获取指向文件的打印流,用于导出笔记内容。 - * - * @return 返回指向生成的文本文件的打印流对象,如果失败则返回 null。 - */ - private PrintStream getExportToTextPrintStream() { - // 调用辅助方法,生成导出文件 - File file = generateFileMountedOnSDcard(mContext, R.string.file_path, R.string.file_name_txt_format); - if (file == null) { // 如果文件生成失败 - Log.e(TAG, "create file to exported failed"); // 输出错误日志 - return null; // 返回 null,表示打印流创建失败 - } - // 更新文件名和目录信息 - mFileName = file.getName(); // 设置文件名 - mFileDirectory = mContext.getString(R.string.file_path); // 设置文件目录 - - PrintStream ps = null; // 初始化打印流为 null - try { - // 创建文件输出流并包装为打印流 - FileOutputStream fos = new FileOutputStream(file); - ps = new PrintStream(fos); - } catch (FileNotFoundException e) { // 捕获文件未找到异常 - e.printStackTrace(); - return null; // 返回 null,表示打印流创建失败 - } catch (NullPointerException e) { // 捕获空指针异常 - e.printStackTrace(); - return null; // 返回 null - } - return ps; // 返回成功创建的打印流对象 - } - - /** - * 在 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(); // 使用 StringBuilder 拼接路径和文件名 - - // 获取 SD 卡的根目录并拼接路径 - sb.append(Environment.getExternalStorageDirectory()); - sb.append(context.getString(filePathResId)); // 从资源中获取路径字符串 - - // 创建文件目录对象 - File filedir = new File(sb.toString()); - // 生成文件名并拼接到路径中,文件名包含日期信息 - sb.append(context.getString(fileNameFormatResId, - DateFormat.format(context.getString(R.string.format_date_ymd), System.currentTimeMillis()))); - File file = new File(sb.toString()); // 创建文件对象 - - try { - if (!filedir.exists()) { // 如果目录不存在 - filedir.mkdir(); // 创建目录 - } - if (!file.exists()) { // 如果文件不存在 - file.createNewFile(); // 创建文件 - } - return file; // 返回成功生成的文件对象 - } catch (SecurityException e) { // 捕获安全异常 - e.printStackTrace(); - } catch (IOException e) { // 捕获 I/O 异常 - e.printStackTrace(); - } - return null; // 如果发生异常,返回 null - } - } -} - - diff --git a/src/DataUtils.java b/src/DataUtils.java deleted file mode 100644 index 977ad28..0000000 --- a/src/DataUtils.java +++ /dev/null @@ -1,406 +0,0 @@ -/* - * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) - * Licensed under the Apache License, Version 2.0 - * 该文件定义了用于管理和操作笔记数据的实用工具类 DataUtils。 - */ - -package net.micode.notes.tool; // 声明该类所属的包路径 - -// 导入需要的类,用于操作内容提供器、数据库、日志和其他功能 -import android.content.ContentProviderOperation; -import android.content.ContentProviderResult; -import android.content.ContentResolver; -import android.content.ContentUris; -import android.content.ContentValues; -import android.content.OperationApplicationException; -import android.database.Cursor; -import android.os.RemoteException; -import android.util.Log; - -import net.micode.notes.data.Notes; // 导入笔记数据定义 -import net.micode.notes.data.Notes.CallNote; // 导入与通话笔记相关的定义 -import net.micode.notes.data.Notes.NoteColumns; // 导入与笔记列相关的定义 -import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; // 导入笔记小部件属性定义 - -import java.util.ArrayList; // 用于创建动态数组 -import java.util.HashSet; // 用于存储无重复的集合 - -// 定义 DataUtils 工具类 -public class DataUtils { - public static final String TAG = "DataUtils"; // 定义日志标记常量 - - /** - * 批量删除笔记 - * - * @param resolver 内容解析器 - * @param ids 要删除的笔记ID集合 - * @return 如果删除成功或集合为空或为null,则返回true,否则返回false - */ - public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { - if (ids == null) { // 如果传入的ID集合为null - Log.d(TAG, "the ids is null"); // 输出调试日志 - return true; // 返回true,表示没有需要删除的内容 - } - if (ids.size() == 0) { // 如果ID集合为空 - Log.d(TAG, "no id is in the hashset"); // 输出调试日志 - return true; // 返回true - } - - // 创建一个操作列表,用于批量删除操作 - ArrayList operationList = new ArrayList(); - for (long id : ids) { // 遍历需要删除的ID集合 - if (id == Notes.ID_ROOT_FOLDER) { // 如果ID是系统根文件夹 - Log.e(TAG, "Don't delete system folder root"); // 输出错误日志 - continue; // 跳过此ID - } - // 创建删除操作 - ContentProviderOperation.Builder builder = ContentProviderOperation - .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); - operationList.add(builder.build()); // 将删除操作添加到列表 - } - try { - // 批量执行删除操作 - ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); - if (results == null || results.length == 0 || results[0] == null) { // 检查删除结果 - Log.d(TAG, "delete notes failed, ids:" + ids.toString()); // 输出调试日志 - return false; // 删除失败,返回false - } - return true; // 删除成功,返回true - } catch (RemoteException e) { // 捕获远程异常 - Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); // 输出错误日志 - } catch (OperationApplicationException e) { // 捕获操作应用异常 - Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); // 输出错误日志 - } - return false; // 出现异常时,返回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(); // 创建一个ContentValues对象 - values.put(NoteColumns.PARENT_ID, desFolderId); // 设置目标文件夹ID - values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); // 设置原始文件夹ID - values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改 - // 更新指定笔记的文件夹信息 - resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); - } - - /** - * 批量将笔记移动到指定文件夹 - * - * @param resolver 内容解析器 - * @param ids 要移动的笔记ID集合 - * @param folderId 目标文件夹ID - * @return 如果移动成功或集合为空或为null,则返回true,否则返回false - */ - public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { - if (ids == null) { // 如果ID集合为null - Log.d(TAG, "the ids is null"); // 输出调试日志 - return true; // 返回true - } - - // 构建更新操作的列表 - ArrayList operationList = new ArrayList(); - for (long id : ids) { // 遍历ID集合 - // 创建更新操作 - ContentProviderOperation.Builder builder = ContentProviderOperation - .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); - builder.withValue(NoteColumns.PARENT_ID, folderId); // 设置目标文件夹ID - builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); // 标记为本地已修改 - operationList.add(builder.build()); // 将操作添加到列表 - } - - try { - // 批量执行更新操作 - ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); - if (results == null || results.length == 0 || results[0] == null) { // 检查操作结果 - Log.d(TAG, "move notes failed, ids:" + ids.toString()); // 输出调试日志 - return false; // 操作失败 - } - return true; // 操作成功 - } catch (RemoteException e) { // 捕获远程异常 - Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); // 输出错误日志 - } catch (OperationApplicationException e) { // 捕获操作应用异常 - Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); // 输出错误日志 - } - return false; // 出现异常时,返回false - } - - /** - * 获取除系统文件夹外的所有用户文件夹数量 - * - * @param resolver 内容解析器 - * @return 用户文件夹数量 - */ - public static int getUserFolderCount(ContentResolver resolver) { - // 查询数据库获取用户文件夹数量,排除系统垃圾箱文件夹 - Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, - new String[]{"COUNT(*)"}, // 查询列:计数 - NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", // 条件:文件夹类型且不是垃圾箱 - new String[]{String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, // 条件值 - null); // 排序:无 - - int count = 0; // 初始化文件夹计数 - if (cursor != null) { // 如果查询结果不为空 - if (cursor.moveToFirst()) { // 移动到第一条记录 - try { - count = cursor.getInt(0); // 获取计数值 - } catch (IndexOutOfBoundsException e) { // 捕获索引越界异常 - Log.e(TAG, "get folder count failed:" + e.toString()); // 输出错误日志 - } finally { - cursor.close(); // 关闭游标 - } - } - } - 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), // 查询的URI - null, // 查询的列,null表示查询所有列 - NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, // 条件 - new String[]{String.valueOf(type)}, // 条件值 - null); // 排序:无 - - boolean exist = false; // 初始化结果为false - if (cursor != null) { // 如果查询结果不为空 - if (cursor.getCount() > 0) { // 如果查询结果有记录 - exist = true; // 设置结果为true - } - cursor.close(); // 关闭游标 - } - return exist; // 返回结果 - } - - /** - * 检查指定的笔记ID在数据库中是否存在 - * - * @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), // 查询的URI - null, // 查询所有列 - null, // 无条件 - null, // 无条件值 - null); // 无排序 - - boolean exist = false; // 初始化结果为false - if (cursor != null) { // 如果查询结果不为空 - if (cursor.getCount() > 0) { // 如果查询结果有记录 - exist = true; // 设置结果为true - } - cursor.close(); // 关闭游标 - } - return exist; // 返回结果 - } - - /** - * 检查指定的数据ID在数据库中是否存在 - * - * @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), // 查询的URI - null, // 查询所有列 - null, // 无条件 - null, // 无条件值 - null); // 无排序 - - boolean exist = false; // 初始化结果为false - if (cursor != null) { // 如果查询结果不为空 - if (cursor.getCount() > 0) { // 如果查询结果有记录 - exist = true; // 设置结果为true - } - cursor.close(); // 关闭游标 - } - 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, // 查询的URI - null, // 查询所有列 - NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + // 条件:类型为文件夹 - " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + // 且不在垃圾箱 - " AND " + NoteColumns.SNIPPET + "=?", // 且名称匹配 - new String[]{name}, // 条件值 - null); // 无排序 - - boolean exist = false; // 初始化结果为false - if (cursor != null) { // 如果查询结果不为空 - if (cursor.getCount() > 0) { // 如果查询结果有记录 - exist = true; // 设置结果为true - } - cursor.close(); // 关闭游标 - } - return exist; // 返回结果 - } - - /** - * 获取指定文件夹中的笔记小部件信息集合 - * - * @param resolver 内容解析器 - * @param folderId 文件夹ID - * @return 笔记小部件信息集合 - */ - public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { - // 查询数据库,获取文件夹中的小部件信息 - Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, // 查询的URI - new String[]{NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE}, // 查询的列 - NoteColumns.PARENT_ID + "=?", // 条件:父ID为指定文件夹 - new String[]{String.valueOf(folderId)}, // 条件值 - null); // 无排序 - - HashSet set = null; // 初始化小部件集合为null - if (c != null) { // 如果查询结果不为空 - if (c.moveToFirst()) { // 移动到第一条记录 - set = new HashSet(); // 初始化小部件集合 - do { // 遍历查询结果 - try { - AppWidgetAttribute widget = new AppWidgetAttribute(); // 创建小部件属性对象 - widget.widgetId = c.getInt(0); // 获取小部件ID - widget.widgetType = c.getInt(1); // 获取小部件类型 - set.add(widget); // 将小部件添加到集合 - } catch (IndexOutOfBoundsException e) { // 捕获索引越界异常 - Log.e(TAG, e.toString()); // 输出错误日志 - } - } while (c.moveToNext()); // 移动到下一条记录 - } - c.close(); // 关闭游标 - } - return set; // 返回小部件集合 - } - - - /** - * 通过笔记ID获取关联的通话号码 - * - * @param resolver 内容解析器 - * @param noteId 笔记ID - * @return 通话号码,如果未找到则返回空字符串 - */ - public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { - // 查询数据库,获取与笔记ID关联的通话号码 - Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, // 查询的URI - new String[]{CallNote.PHONE_NUMBER}, // 查询的列:电话号码 - CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", // 条件:匹配笔记ID和MIME类型 - new String[]{String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE}, // 条件值 - null); // 无排序 - - if (cursor != null && cursor.moveToFirst()) { // 如果查询结果不为空且有记录 - try { - return cursor.getString(0); // 返回查询结果中的电话号码 - } catch (IndexOutOfBoundsException e) { // 捕获索引越界异常 - Log.e(TAG, "Get call number fails " + e.toString()); // 输出错误日志 - } finally { - cursor.close(); // 关闭游标 - } - } - return ""; // 未找到记录时返回空字符串 - } - - /** - * 根据电话号码和通话日期获取对应的笔记ID - * - * @param resolver 内容解析器 - * @param phoneNumber 电话号码 - * @param callDate 通话日期 - * @return 笔记ID,未找到则返回0 - */ - public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { - // 查询数据库,根据电话号码和通话日期获取笔记ID - Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, // 查询的URI - new String[]{CallNote.NOTE_ID}, // 查询的列:笔记ID - CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" - + CallNote.PHONE_NUMBER + ",?)", // 条件:匹配通话日期、MIME类型和电话号码 - new String[]{String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber}, // 条件值 - null); // 无排序 - - if (cursor != null) { // 如果查询结果不为空 - if (cursor.moveToFirst()) { // 如果有记录 - try { - return cursor.getLong(0); // 返回笔记ID - } catch (IndexOutOfBoundsException e) { // 捕获索引越界异常 - Log.e(TAG, "Get call note id fails " + e.toString()); // 输出错误日志 - } - } - cursor.close(); // 关闭游标 - } - return 0; // 未找到记录时返回0 - } - - /** - * 根据笔记ID从数据库中获取笔记的摘要 - * - * @param resolver 内容解析器 - * @param noteId 笔记的ID - * @return 笔记的摘要字符串。如果找不到对应的笔记,将抛出IllegalArgumentException。 - */ - public static String getSnippetById(ContentResolver resolver, long noteId) { - // 查询数据库,获取指定ID笔记的摘要 - Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, // 查询的URI - new String[]{NoteColumns.SNIPPET}, // 查询的列:摘要 - NoteColumns.ID + "=?", // 条件:匹配笔记ID - new String[]{String.valueOf(noteId)}, // 条件值 - null); // 无排序 - - if (cursor != null) { // 如果查询结果不为空 - String snippet = ""; // 初始化摘要为空字符串 - if (cursor.moveToFirst()) { // 如果有记录 - snippet = cursor.getString(0); // 获取摘要字符串 - } - cursor.close(); // 关闭游标 - return snippet; // 返回摘要 - } - // 如果未找到记录,抛出异常 - 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(); // 去除两端的空白字符 - int index = snippet.indexOf('\n'); // 查找第一个换行符的位置 - if (index != -1) { // 如果存在换行符 - snippet = snippet.substring(0, index); // 截取至第一个换行符之前的内容 - } - } - return snippet; // 返回格式化后的摘要字符串 - } -} - diff --git a/src/GTaskStringUtils.java b/src/GTaskStringUtils.java deleted file mode 100644 index aeaa30d..0000000 --- a/src/GTaskStringUtils.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * GTaskStringUtils 类定义 - * 该类提供了一系列与 GTask 相关的字符串常量,用于在操作 GTask 数据时标识各种 JSON 属性。 - */ -package net.micode.notes.tool; // 定义该类所在的包路径 - - -// 定义 GTaskStringUtils 类 -public class GTaskStringUtils { - - // GTask JSON 对象中各种属性的键名 - public final static String GTASK_JSON_ACTION_ID = "action_id"; // 动作 ID - public final static String GTASK_JSON_ACTION_LIST = "action_list"; // 动作列表 - public final static String GTASK_JSON_ACTION_TYPE = "action_type"; // 动作类型 - public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; // 创建动作类型 - public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; // 获取所有动作类型 - public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; // 移动动作类型 - public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; // 更新动作类型 - public final static String GTASK_JSON_CREATOR_ID = "creator_id"; // 创建者 ID - public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; // 子实体 - public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; // 客户端版本 - public final static String GTASK_JSON_COMPLETED = "completed"; // 完成状态 - public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; // 当前列表 ID - public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; // 默认列表 ID - public final static String GTASK_JSON_DELETED = "deleted"; // 删除状态 - public final static String GTASK_JSON_DEST_LIST = "dest_list"; // 目标列表 - public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; // 目标父实体 - public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; // 目标父实体类型 - public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; // 实体增量 - public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; // 实体类型 - public final static String GTASK_JSON_GET_DELETED = "get_deleted"; // 获取已删除项 - public final static String GTASK_JSON_ID = "id"; // ID - public final static String GTASK_JSON_INDEX = "index"; // 索引 - public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; // 最后修改时间 - public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; // 最新同步点 - public final static String GTASK_JSON_LIST_ID = "list_id"; // 列表 ID - public final static String GTASK_JSON_LISTS = "lists"; // 列表集合 - public final static String GTASK_JSON_NAME = "name"; // 名称 - public final static String GTASK_JSON_NEW_ID = "new_id"; // 新 ID - public final static String GTASK_JSON_NOTES = "notes"; // 备注 - public final static String GTASK_JSON_PARENT_ID = "parent_id"; // 父 ID - public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; // 前一个兄弟 ID - public final static String GTASK_JSON_RESULTS = "results"; // 结果 - public final static String GTASK_JSON_SOURCE_LIST = "source_list"; // 源列表 - public final static String GTASK_JSON_TASKS = "tasks"; // 任务集合 - public final static String GTASK_JSON_TYPE = "type"; // 类型 - public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; // 类型:组 - public final static String GTASK_JSON_TYPE_TASK = "TASK"; // 类型:任务 - public final static String GTASK_JSON_USER = "user"; // 用户 - - // MIUI 笔记相关的文件夹前缀和元数据键名 - public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; // MIUI 笔记文件夹前缀 - public final static String FOLDER_DEFAULT = "Default"; // 默认文件夹名 - public final static String FOLDER_CALL_NOTE = "Call_Note"; // 通话笔记文件夹名 - public final static String FOLDER_META = "METADATA"; // 元数据文件夹名 - - // 元数据头部键名 - public final static String META_HEAD_GTASK_ID = "meta_gid"; // 元数据头部:GTask ID - 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"; // 不可更新和删除的元数据名称 -} diff --git a/src/NoteWidgetProvider.java b/src/NoteWidgetProvider.java new file mode 100644 index 0000000..c746e27 --- /dev/null +++ b/src/NoteWidgetProvider.java @@ -0,0 +1,159 @@ +package net.micode.notes.widget; + +// 导入必要的 Android 和应用相关的类和包 +import android.app.PendingIntent; +import android.appwidget.AppWidgetManager; +import android.appwidget.AppWidgetProvider; +import android.content.ContentValues; +import android.content.Context; +import android.content.Intent; +import android.database.Cursor; +import android.util.Log; +import android.widget.RemoteViews; + +import net.micode.notes.R; +import net.micode.notes.data.Notes; +import net.micode.notes.data.Notes.NoteColumns; +import net.micode.notes.tool.ResourceParser; +import net.micode.notes.ui.NoteEditActivity; +import net.micode.notes.ui.NotesListActivity; + +// 抽象类 NoteWidgetProvider,继承自 AppWidgetProvider,用于定义笔记小部件的逻辑 +public abstract class NoteWidgetProvider extends AppWidgetProvider { + + // 定义数据库查询时所需的列名数组 + public static final String[] PROJECTION = new String[]{ + NoteColumns.ID, // 笔记的唯一 ID + NoteColumns.BG_COLOR_ID, // 笔记的背景颜色 ID + NoteColumns.SNIPPET // 笔记的摘要(内容片段) + }; + + // 定义列索引的常量,以便访问查询结果时更加清晰和方便 + public static final int COLUMN_ID = 0; + public static final int COLUMN_BG_COLOR_ID = 1; + public static final int COLUMN_SNIPPET = 2; + + // 定义日志标签,便于在调试和记录日志时标识日志来源 + private static final String TAG = "NoteWidgetProvider"; + + // 覆写 onDeleted 方法,当小部件被用户移除时调用 + @Override + public void onDeleted(Context context, int[] appWidgetIds) { + // 创建一个 ContentValues 对象,用于更新数据库 + ContentValues values = new ContentValues(); + // 将小部件 ID 设置为无效 ID,以便从数据库中移除相关关联 + values.put(NoteColumns.WIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID); + // 遍历被删除的小部件 ID 数组 + for (int i = 0; i < appWidgetIds.length; i++) { + // 更新数据库中与该小部件关联的记录 + context.getContentResolver().update(Notes.CONTENT_NOTE_URI, + values, + NoteColumns.WIDGET_ID + "=?", // WHERE 子句:匹配指定小部件 ID + new String[]{String.valueOf(appWidgetIds[i])}); // WHERE 参数 + } + } + + // 私有方法:根据小部件 ID 查询对应的笔记信息 + private Cursor getNoteWidgetInfo(Context context, int widgetId) { + // 执行数据库查询,返回与指定小部件 ID 相关的笔记 + return context.getContentResolver().query(Notes.CONTENT_NOTE_URI, + PROJECTION, // 查询的列 + NoteColumns.WIDGET_ID + "=? AND " + NoteColumns.PARENT_ID + "<>?", // 条件:小部件 ID 匹配且不在垃圾箱中 + new String[]{String.valueOf(widgetId), String.valueOf(Notes.ID_TRASH_FOLER)}, // 参数值 + null); // 排序方式(此处为 null,即不排序) + } + + // 受保护方法:更新小部件显示内容的公共接口 + protected void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 默认调用私有方法,并将隐私模式设置为 false + update(context, appWidgetManager, appWidgetIds, false); + } + + // 私有方法:根据隐私模式,更新小部件显示内容 + private void update(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, + boolean privacyMode) { + // 遍历所有小部件 ID + for (int i = 0; i < appWidgetIds.length; i++) { + // 如果小部件 ID 无效,则跳过 + if (appWidgetIds[i] != AppWidgetManager.INVALID_APPWIDGET_ID) { + // 获取默认的背景颜色 ID + int bgId = ResourceParser.getDefaultBgId(context); + // 初始化笔记摘要为空字符串 + String snippet = ""; + // 创建一个 Intent,指定跳转到 NoteEditActivity + Intent intent = new Intent(context, NoteEditActivity.class); + // 设置 Intent 的标志,确保活动以单一实例模式启动 + intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); + // 将小部件 ID 和类型作为额外数据加入 Intent + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_ID, appWidgetIds[i]); + intent.putExtra(Notes.INTENT_EXTRA_WIDGET_TYPE, getWidgetType()); + + // 查询数据库,获取与当前小部件 ID 关联的笔记信息 + Cursor c = getNoteWidgetInfo(context, appWidgetIds[i]); + // 如果查询结果不为空且有数据 + if (c != null && c.moveToFirst()) { + // 如果查询结果超过一条记录,则记录日志并返回 + if (c.getCount() > 1) { + Log.e(TAG, "Multiple message with same widget id:" + appWidgetIds[i]); + c.close(); + return; + } + // 从查询结果中获取笔记摘要和背景颜色 ID + snippet = c.getString(COLUMN_SNIPPET); + bgId = c.getInt(COLUMN_BG_COLOR_ID); + // 将笔记 ID 添加到 Intent 的额外数据中 + intent.putExtra(Intent.EXTRA_UID, c.getLong(COLUMN_ID)); + // 设置 Intent 的操作类型为查看 + intent.setAction(Intent.ACTION_VIEW); + } else { + // 如果查询结果为空,则设置默认的内容和操作类型 + snippet = context.getResources().getString(R.string.widget_havenot_content); + intent.setAction(Intent.ACTION_INSERT_OR_EDIT); + } + + // 如果 Cursor 对象不为空,则关闭以释放资源 + if (c != null) { + c.close(); + } + + // 创建 RemoteViews 对象,用于更新小部件的布局 + RemoteViews rv = new RemoteViews(context.getPackageName(), getLayoutId()); + // 设置小部件的背景图片资源 + rv.setImageViewResource(R.id.widget_bg_image, getBgResourceId(bgId)); + // 将背景颜色 ID 添加到 Intent 的额外数据中 + intent.putExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, bgId); + + // 声明 PendingIntent 对象,用于处理小部件的点击事件 + PendingIntent pendingIntent = null; + if (privacyMode) { + // 如果处于隐私模式,则显示隐私模式文本 + rv.setTextViewText(R.id.widget_text, + context.getString(R.string.widget_under_visit_mode)); + // 设置 PendingIntent,点击时跳转到 NotesListActivity + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], new Intent( + context, NotesListActivity.class), PendingIntent.FLAG_UPDATE_CURRENT); + } else { + // 非隐私模式,显示笔记摘要 + rv.setTextViewText(R.id.widget_text, snippet); + // 设置 PendingIntent,点击时跳转到笔记编辑页面 + pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, + PendingIntent.FLAG_UPDATE_CURRENT); + } + + // 为小部件的文本区域设置点击事件 + rv.setOnClickPendingIntent(R.id.widget_text, pendingIntent); + // 更新小部件显示内容 + appWidgetManager.updateAppWidget(appWidgetIds[i], rv); + } + } + } + + // 抽象方法:获取背景资源的 ID,具体实现由子类提供 + protected abstract int getBgResourceId(int bgId); + + // 抽象方法:获取小部件布局的资源 ID,具体实现由子类提供 + protected abstract int getLayoutId(); + + // 抽象方法:获取小部件的类型,具体实现由子类提供 + protected abstract int getWidgetType(); +} diff --git a/src/NoteWidgetProvider_2x.java b/src/NoteWidgetProvider_2x.java new file mode 100644 index 0000000..9a547d2 --- /dev/null +++ b/src/NoteWidgetProvider_2x.java @@ -0,0 +1,76 @@ +/* + * 版权声明:MiCode开源社区(www.micode.net) + * + * 本代码遵循Apache 2.0开源协议 + * 如需获取完整的授权条款,请访问:http://www.apache.org/licenses/LICENSE-2.0.html + * + * 代码开始 + */ + +// 定义包名,组织代码模块化管理 +package net.micode.notes.widget; + +// 导入Android小部件相关类和应用的资源类 +import android.appwidget.AppWidgetManager; +import android.content.Context; + +import net.micode.notes.R; // 导入资源文件R类,用于访问布局和其他资源 +import net.micode.notes.data.Notes; // 导入笔记相关的数据类 +import net.micode.notes.tool.ResourceParser; // 导入资源解析工具类 + +/** + * 2x版本的小部件提供者类。 + * 该类继承自 NoteWidgetProvider,专门处理 2x2 尺寸的笔记小部件。 + */ +public class NoteWidgetProvider_2x extends NoteWidgetProvider { + /** + * 当系统需要更新小部件时,会调用此方法。 + * 此方法通过调用父类的 `update` 方法来处理小部件更新的逻辑。 + * + * @param context 上下文环境,用于获取应用程序资源和操作。 + * @param appWidgetManager 管理当前应用中所有小部件的 AppWidgetManager 实例。 + * @param appWidgetIds 当前需要更新的小部件 ID 数组。 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类的 update 方法,完成小部件的更新逻辑 + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取小部件布局的资源 ID。 + * 此方法返回当前小部件所使用的布局文件的资源 ID。 + * 该布局文件定义了小部件在屏幕上的外观。 + * + * @return 布局资源的 ID,对应 `res/layout/widget_2x.xml` 文件。 + */ + @Override + protected int getLayoutId() { + return R.layout.widget_2x; // 返回 2x 小部件的布局资源 ID + } + + /** + * 根据背景 ID 获取对应的背景资源 ID。 + * 小部件有不同的背景样式(如不同颜色或主题),通过背景 ID 选择对应的资源。 + * + * @param bgId 背景资源的索引 ID,表示选择哪一种背景样式。 + * @return 背景资源的 ID,用于设置小部件的背景。 + */ + @Override + protected int getBgResourceId(int bgId) { + // 使用 ResourceParser 工具类,根据背景 ID 获取 2x 小部件的背景资源 ID + return ResourceParser.WidgetBgResources.getWidget2xBgResource(bgId); + } + + /** + * 获取当前小部件的类型。 + * 该方法返回一个常量,用于标识当前小部件的类型。 + * 不同尺寸的小部件有不同的类型常量,例如 2x 和 4x 小部件。 + * + * @return 小部件类型的常量值,表示当前为 2x 尺寸的小部件。 + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_2X; // 返回表示 2x 小部件类型的常量 + } +} diff --git a/src/NoteWidgetProvider_4x.java b/src/NoteWidgetProvider_4x.java new file mode 100644 index 0000000..29c55e0 --- /dev/null +++ b/src/NoteWidgetProvider_4x.java @@ -0,0 +1,74 @@ +/* + * 版权声明:MiCode开源社区(www.micode.net) + * + * 本代码遵循Apache 2.0开源协议 + * 详细授权信息请访问:http://www.apache.org/licenses/LICENSE-2.0 + */ + +// 定义代码所在的包,用于组织代码模块化 +package net.micode.notes.widget; + +// 导入小部件和应用程序相关的类 +import android.appwidget.AppWidgetManager; +import android.content.Context; + +import net.micode.notes.R; // 导入应用资源文件 R,访问布局和其他资源 +import net.micode.notes.data.Notes; // 导入笔记相关的常量和数据类 +import net.micode.notes.tool.ResourceParser; // 导入工具类,用于解析小部件资源 + +/** + * 4x大小的便签小部件提供者类,继承自 NoteWidgetProvider。 + * 专门用于处理 4x 大小的便签小部件的更新和相关操作。 + */ +public class NoteWidgetProvider_4x extends NoteWidgetProvider { + + /** + * 当系统请求更新小部件时,会调用此方法。 + * 该方法通过调用父类的 `update` 方法来处理小部件的更新逻辑。 + * + * @param context 上下文环境,提供应用程序的全局信息和操作能力。 + * @param appWidgetManager 小部件管理器,用于管理当前应用中所有的小部件。 + * @param appWidgetIds 需要更新的小部件 ID 数组,包含多个小部件的唯一标识符。 + */ + @Override + public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { + // 调用父类的 update 方法,完成通用的更新逻辑 + super.update(context, appWidgetManager, appWidgetIds); + } + + /** + * 获取小部件的布局资源 ID。 + * 布局资源定义了小部件的界面结构,例如显示的文本和背景图片等。 + * + * @return 返回 4x 小部件的布局资源 ID,对应 `res/layout/widget_4x.xml` 文件。 + */ + @Override + protected int getLayoutId() { + return R.layout.widget_4x; // 指定用于 4x 小部件的布局资源 + } + + /** + * 根据背景 ID 获取对应的背景资源 ID。 + * 小部件可以有不同的背景样式,例如不同的颜色或主题,通过背景 ID 来选择。 + * + * @param bgId 背景的索引 ID,表示不同的背景样式。 + * @return 返回与背景 ID 对应的背景资源 ID,用于设置小部件的背景。 + */ + @Override + protected int getBgResourceId(int bgId) { + // 使用 ResourceParser 工具类,根据背景 ID 获取 4x 小部件的背景资源 ID + return ResourceParser.WidgetBgResources.getWidget4xBgResource(bgId); + } + + /** + * 获取当前小部件的类型标识。 + * 该方法返回一个整型常量,用于标识当前小部件的类型。 + * 在应用中可以通过该标识区分不同的小部件类型,例如 4x 和 2x 小部件。 + * + * @return 返回小部件类型的整型标识,表示这是一个 4x 大小的小部件。 + */ + @Override + protected int getWidgetType() { + return Notes.TYPE_WIDGET_4X; // 返回 4x 小部件类型的常量 + } +} diff --git a/src/ResourceParser.java b/src/ResourceParser.java deleted file mode 100644 index 8a4f353..0000000 --- a/src/ResourceParser.java +++ /dev/null @@ -1,212 +0,0 @@ -/* - * ResourceParser 类用于管理与应用资源相关的各种静态方法和常量。 - */ -package net.micode.notes.tool; - -// 导入 Android 的 Context 和 PreferenceManager 类,用于访问应用的上下文和首选项 -import android.content.Context; -import android.preference.PreferenceManager; - -// 导入应用资源和偏好设置相关的类 -import net.micode.notes.R; -import net.micode.notes.ui.NotesPreferenceActivity; - -// 定义 ResourceParser 类 -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, // 蓝色背景资源 - R.drawable.edit_white, // 白色背景资源 - R.drawable.edit_green, // 绿色背景资源 - R.drawable.edit_red // 红色背景资源 - }; - - // 编辑状态下的标题背景资源数组 - private final static int[] BG_EDIT_TITLE_RESOURCES = new int[]{ - R.drawable.edit_title_yellow, // 黄色标题背景资源 - R.drawable.edit_title_blue, // 蓝色标题背景资源 - R.drawable.edit_title_white, // 白色标题背景资源 - R.drawable.edit_title_green, // 绿色标题背景资源 - R.drawable.edit_title_red // 红色标题背景资源 - }; - - // 根据id获取编辑状态下的背景资源 - public static int getNoteBgResource(int id) { - return BG_EDIT_RESOURCES[id]; - } - - // 根据id获取编辑状态下的标题背景资源 - public static int getNoteTitleBgResource(int id) { - return BG_EDIT_TITLE_RESOURCES[id]; - } - } - - /** - * 获取默认笔记背景id。 - * - * @param context 上下文对象,用于访问SharedPreferences。 - * @return 如果用户设置了背景颜色,则返回一个随机背景颜色id;否则返回默认背景颜色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, // 蓝色第一个项背景 - R.drawable.list_white_up, // 白色第一个项背景 - R.drawable.list_green_up, // 绿色第一个项背景 - R.drawable.list_red_up // 红色第一个项背景 - }; - - // 普通列表项的背景资源数组 - private final static int[] BG_NORMAL_RESOURCES = new int[]{ - R.drawable.list_yellow_middle, // 黄色普通项背景 - R.drawable.list_blue_middle, // 蓝色普通项背景 - R.drawable.list_white_middle, // 白色普通项背景 - R.drawable.list_green_middle, // 绿色普通项背景 - R.drawable.list_red_middle // 红色普通项背景 - }; - - // 最后一个列表项的背景资源数组 - private final static int[] BG_LAST_RESOURCES = new int[]{ - R.drawable.list_yellow_down, // 黄色最后项背景 - R.drawable.list_blue_down, // 蓝色最后项背景 - R.drawable.list_white_down, // 白色最后项背景 - R.drawable.list_green_down, // 绿色最后项背景 - R.drawable.list_red_down // 红色最后项背景 - }; - - // 单个列表项的背景资源数组 - private final static int[] BG_SINGLE_RESOURCES = new int[]{ - R.drawable.list_yellow_single, // 黄色单项背景 - R.drawable.list_blue_single, // 蓝色单项背景 - R.drawable.list_white_single, // 白色单项背景 - R.drawable.list_green_single, // 绿色单项背景 - R.drawable.list_red_single // 红色单项背景 - }; - - // 获取第一个列表项的背景资源 - public static int getNoteBgFirstRes(int id) { - return BG_FIRST_RESOURCES[id]; - } - - // 获取最后一个列表项的背景资源 - public static int getNoteBgLastRes(int id) { - return BG_LAST_RESOURCES[id]; - } - - // 获取单个列表项的背景资源 - public static int getNoteBgSingleRes(int id) { - return BG_SINGLE_RESOURCES[id]; - } - - // 获取普通列表项的背景资源 - public static int getNoteBgNormalRes(int id) { - return BG_NORMAL_RESOURCES[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, // 黄色2x小部件背景 - R.drawable.widget_2x_blue, // 蓝色2x小部件背景 - R.drawable.widget_2x_white, // 白色2x小部件背景 - R.drawable.widget_2x_green, // 绿色2x小部件背景 - R.drawable.widget_2x_red // 红色2x小部件背景 - }; - - // 根据id获取2x小部件的背景资源 - 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, // 黄色4x小部件背景 - R.drawable.widget_4x_blue, // 蓝色4x小部件背景 - R.drawable.widget_4x_white, // 白色4x小部件背景 - R.drawable.widget_4x_green, // 绿色4x小部件背景 - R.drawable.widget_4x_red // 红色4x小部件背景 - }; - - // 根据id获取4x小部件的背景资源 - 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, // 中等文本样式 - R.style.TextAppearanceLarge, // 大号文本样式 - R.style.TextAppearanceSuper // 特大号文本样式 - }; - - // 根据id获取文本外观资源 - public static int getTexAppearanceResource(int id) { - // 如果id超出资源数组范围,返回默认字体大小 - if (id >= TEXTAPPEARANCE_RESOURCES.length) { - return BG_DEFAULT_FONT_SIZE; - } - return TEXTAPPEARANCE_RESOURCES[id]; - } - - // 获取文本外观资源的数量 - public static int getResourcesSize() { - return TEXTAPPEARANCE_RESOURCES.length; - } - } -}