diff --git a/src/net/micode/notes/gtask/exception/ActionFailureException.java b/src/net/micode/notes/gtask/exception/ActionFailureException.java index 15504be..1a5cb46 100644 --- a/src/net/micode/notes/gtask/exception/ActionFailureException.java +++ b/src/net/micode/notes/gtask/exception/ActionFailureException.java @@ -16,18 +16,36 @@ package net.micode.notes.gtask.exception; +/** + * 操作失败异常类 + * 用于表示Google任务同步过程中发生的操作失败情况 + * 继承自RuntimeException,属于非检查异常 + */ public class ActionFailureException extends RuntimeException { + // 序列化版本UID,用于版本控制 private static final long serialVersionUID = 4425249765923293627L; + /** + * 默认构造函数 + */ public ActionFailureException() { super(); } + /** + * 带错误信息的构造函数 + * @param paramString 错误信息字符串 + */ public ActionFailureException(String paramString) { super(paramString); } + /** + * 带错误信息和原始异常的构造函数 + * @param paramString 错误信息字符串 + * @param paramThrowable 引发此异常的原始异常 + */ public ActionFailureException(String paramString, Throwable paramThrowable) { super(paramString, paramThrowable); } -} +} \ No newline at end of file diff --git a/src/net/micode/notes/gtask/exception/NetworkFailureException.java b/src/net/micode/notes/gtask/exception/NetworkFailureException.java index b08cfb1..0269ee2 100644 --- a/src/net/micode/notes/gtask/exception/NetworkFailureException.java +++ b/src/net/micode/notes/gtask/exception/NetworkFailureException.java @@ -16,18 +16,36 @@ package net.micode.notes.gtask.exception; +/** + * 网络失败异常类 + * 用于表示在与Google任务服务器通信过程中发生的网络连接相关错误 + * 继承自Exception,属于受检查异常,需要显式处理 + */ public class NetworkFailureException extends Exception { + // 序列化版本UID,用于版本控制 private static final long serialVersionUID = 2107610287180234136L; + /** + * 默认构造函数 + */ public NetworkFailureException() { super(); } + /** + * 带错误信息的构造函数 + * @param paramString 错误信息字符串 + */ public NetworkFailureException(String paramString) { super(paramString); } + /** + * 带错误信息和原始异常的构造函数 + * @param paramString 错误信息字符串 + * @param paramThrowable 引发此异常的原始异常 + */ public NetworkFailureException(String paramString, Throwable paramThrowable) { super(paramString, paramThrowable); } -} +} \ No newline at end of file diff --git a/src/net/micode/notes/tool/BackupUtils.java b/src/net/micode/notes/tool/BackupUtils.java index 39f6ec4..f81a26a 100644 --- a/src/net/micode/notes/tool/BackupUtils.java +++ b/src/net/micode/notes/tool/BackupUtils.java @@ -35,12 +35,23 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; - +/** + * 备份工具类,提供数据备份相关的功能,如将数据导出为文本文件等 + * 此类主要用于将笔记数据(包括文件夹和笔记内容)导出到外部存储设备上的文本文件中 + */ public class BackupUtils { + // 日志标签,用于在日志中标识此工具类的相关操作 private static final String TAG = "BackupUtils"; - // Singleton stuff + // 单例实例,用于确保BackupUtils类只有一个实例存在 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 +60,74 @@ public class BackupUtils { } /** - * Following states are signs to represents backup or restore - * status + * 备份或恢复状态标识 + * 以下状态用于表示备份或恢复操作的不同阶段或结果 */ - // Currently, the sdcard is not mounted - 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; - + // 外部存储设备(如SD卡)未挂载 + public static final int STATE_SD_CARD_UNMOUONTED = 0; + // 备份文件不存在 + 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 上下文对象,用于创建TextExport实例 + */ private BackupUtils(Context context) { mTextExport = new TextExport(context); } + /** + * 检查外部存储设备是否可用(已挂载) + * + * @return 如果外部存储设备已挂载返回true,否则返回false + */ private static boolean externalStorageAvailable() { return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } + /** + * 将数据导出为文本文件 + * 调用TextExport类的exportToText方法进行实际的导出操作 + * + * @return 导出操作的状态码,如成功则返回STATE_SUCCESS,否则返回其他状态码 + */ 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 +135,16 @@ public class BackupUtils { NoteColumns.TYPE }; + // 笔记ID列在NOTE_PROJECTION数组中的索引 private static final int NOTE_COLUMN_ID = 0; + // 笔记修改日期列在NOTE_PROJECTION数组中的索引 private static final int NOTE_COLUMN_MODIFIED_DATE = 1; + // 笔记摘要列在NOTE_PROJECTION数组中的索引 private static final int NOTE_COLUMN_SNIPPET = 2; + // 数据投影,指定查询笔记相关数据时需要返回的列 private static final String[] DATA_PROJECTION = { DataColumns.CONTENT, DataColumns.MIME_TYPE, @@ -108,23 +154,39 @@ public class BackupUtils { DataColumns.DATA4, }; + // 数据内容列在DATA_PROJECTION数组中的索引 private static final int DATA_COLUMN_CONTENT = 0; + // 数据MIME类型列在DATA_PROJECTION数组中的索引 private static final int DATA_COLUMN_MIME_TYPE = 1; + // 数据调用日期列在DATA_PROJECTION数组中的索引 private static final int DATA_COLUMN_CALL_DATE = 2; + // 数据电话号码列在DATA_PROJECTION数组中的索引 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 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,28 +194,36 @@ public class BackupUtils { mFileDirectory = ""; } + /** + * 根据索引获取文本格式字符串 + * + * @param id 格式字符串在TEXT_FORMAT数组中的索引 + * @return 对应的文本格式字符串 + */ private String getFormat(int id) { return TEXT_FORMAT[id]; } /** - * Export the folder identified by folder id to text + * 将指定文件夹及其子笔记导出为文本 + * 先查询该文件夹下的所有笔记,然后对每个笔记调用exportNoteToText方法进行导出 + * + * @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 - }, null); + NOTE_PROJECTION, NoteColumns.PARENT_ID + "=?", new String[]{folderId}, null); 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,20 +233,22 @@ public class BackupUtils { } /** - * Export note identified by id to a print stream + * 将指定ID的笔记导出为文本 + * 根据笔记的MIME类型,处理不同类型的笔记内容并写入打印流 + * + * @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); + 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)) { - // 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 +257,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,9 +277,9 @@ public class BackupUtils { } dataCursor.close(); } - // print a line separator between note + // 打印笔记之间的分隔符 try { - ps.write(new byte[] { + ps.write(new byte[]{ Character.LINE_SEPARATOR, Character.LETTER_NUMBER }); } catch (IOException e) { @@ -216,7 +288,10 @@ public class BackupUtils { } /** - * Note will be exported as text which is user readable + * 将数据导出为文本文件 + * 检查外部存储设备是否可用,创建打印流,导出文件夹和笔记数据,最后关闭打印流 + * + * @return 导出操作的状态码,如成功则返回STATE_SUCCESS,否则返回其他状态码 */ public int exportToText() { if (!externalStorageAvailable()) { @@ -229,7 +304,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,9 +315,9 @@ 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) { + if (folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { folderName = mContext.getString(R.string.call_record_folder_name); } else { folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); @@ -257,11 +332,11 @@ public class BackupUtils { folderCursor.close(); } - // Export notes in root's folder + // 导出根文件夹中的笔记 Cursor noteCursor = mContext.getContentResolver().query( Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, - NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID + NoteColumns.TYPE + "=" + Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID + "=0", null, null); if (noteCursor != null) { @@ -270,7 +345,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 +358,10 @@ 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 +388,13 @@ public class BackupUtils { } /** - * Generate the text file to store imported data + * 在外部存储设备上生成用于存储导入数据的文件 + * 拼接文件路径和名称,创建文件夹(如果不存在)和文件(如果不存在) + * + * @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 +423,4 @@ public class BackupUtils { return null; } -} - - +} \ No newline at end of file