diff --git a/src/Notes-master/src/net/micode/notes/tool/BackupUtils.java b/src/Notes-master/src/net/micode/notes/tool/BackupUtils.java index 39f6ec4..3932b45 100644 --- a/src/Notes-master/src/net/micode/notes/tool/BackupUtils.java +++ b/src/Notes-master/src/net/micode/notes/tool/BackupUtils.java @@ -2,234 +2,277 @@ * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * 您可以在遵守 License 的前提下使用本文件。 + * 您可以从以下地址获取 License 副本: * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 除非适用法律要求或书面同意,本软件根据 License 分发时“按现状”提供, + * 不附带任何明示或暗示的保证或条件。请参阅 License 了解具体的权限和限制。 */ -package net.micode.notes.tool; - -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; - - +package net.micode.notes.tool; // 指定代码所属的包名 + +import android.content.Context; // 导入Context类,提供应用环境信息 +import android.database.Cursor; // 导入Cursor类,用于查询数据库结果集 +import android.os.Environment; // 导入Environment类,获取外部存储信息 +import android.text.TextUtils; // 导入TextUtils类,提供文本处理工具 +import android.text.format.DateFormat; // 导入DateFormat类,用于日期格式化 +import android.util.Log; // 导入Log类,用于日志记录 + +import net.micode.notes.R; // 导入R资源类,访问应用资源 +import net.micode.notes.data.Notes; // 导入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; // 导入File类,用于文件操作 +import java.io.FileNotFoundException; // 导入文件未找到异常类 +import java.io.FileOutputStream; // 导入文件输出流类 +import java.io.IOException; // 导入IO异常类 +import java.io.PrintStream; // 导入打印流类,用于文本输出 + +/** + * 备份工具类,用于将笔记数据导出为文本文件 + * 支持导出到SD卡,提供备份状态反馈和文件管理功能 + */ public class BackupUtils { - private static final String TAG = "BackupUtils"; - // Singleton stuff + 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); + if (sInstance == null) { // 检查实例是否已创建 + sInstance = new BackupUtils(context); // 创建新实例 } - return sInstance; + return sInstance; // 返回实例 } /** - * 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; + private TextExport mTextExport; // 文本导出器实例 + /** + * 私有构造函数,初始化文本导出器 + * @param context 应用上下文 + */ private BackupUtils(Context context) { - mTextExport = new TextExport(context); + mTextExport = new TextExport(context); // 初始化文本导出器 } + /** + * 检查外部存储是否可用 + * @return 可用返回true,否则返回false + */ private static boolean externalStorageAvailable() { - return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); // 检查SD卡挂载状态 } + /** + * 导出笔记到文本文件 + * @return 导出状态码 + */ public int exportToText() { - return mTextExport.exportToText(); + return mTextExport.exportToText(); // 调用文本导出器的导出方法 } + /** + * 获取导出的文本文件名 + * @return 文件名 + */ public String getExportedTextFileName() { - return mTextExport.mFileName; + return mTextExport.mFileName; // 返回导出的文件名 } + /** + * 获取导出的文本文件目录 + * @return 文件目录路径 + */ public String getExportedTextFileDir() { - return mTextExport.mFileDirectory; + return mTextExport.mFileDirectory; // 返回导出的文件目录 } + /** + * 内部类:文本导出器,负责将笔记导出为文本格式 + */ private static class TextExport { + // 笔记查询投影,指定要查询的列 private static final String[] NOTE_PROJECTION = { - NoteColumns.ID, - NoteColumns.MODIFIED_DATE, - NoteColumns.SNIPPET, - NoteColumns.TYPE + NoteColumns.ID, // 笔记ID + NoteColumns.MODIFIED_DATE, // 修改日期 + NoteColumns.SNIPPET, // 摘要 + NoteColumns.TYPE // 类型 }; - 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 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, - DataColumns.DATA1, - DataColumns.DATA2, - DataColumns.DATA3, - DataColumns.DATA4, + 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; - - private static final int DATA_COLUMN_CALL_DATE = 2; - - private static final int DATA_COLUMN_PHONE_NUMBER = 4; + // 数据列索引常量 + private static final int DATA_COLUMN_CONTENT = 0; // 内容列索引 + private static final int DATA_COLUMN_MIME_TYPE = 1; // MIME类型列索引 + 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 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; + 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; - mFileName = ""; - mFileDirectory = ""; + TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); // 获取文本格式数组 + mContext = context; // 保存上下文 + mFileName = ""; // 初始化文件名 + mFileDirectory = ""; // 初始化文件目录 } + /** + * 获取指定索引的文本格式 + * @param id 格式索引 + * @return 格式字符串 + */ private String getFormat(int id) { - return TEXT_FORMAT[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 + folderId }, null); - if (notesCursor != null) { - if (notesCursor.moveToFirst()) { + 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()); + exportNoteToText(noteId, ps); // 导出笔记内容 + } while (notesCursor.moveToNext()); // 移动到下一条记录 } - notesCursor.close(); + notesCursor.close(); // 关闭游标 } } /** - * Export note identified by id to a print stream + * 将指定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 + noteId }, null); - if (dataCursor != null) { - if (dataCursor.moveToFirst()) { + 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 mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); // 获取MIME类型 + if (DataConstants.CALL_NOTE.equals(mimeType)) { // 处理通话记录类型 + // 打印电话号码 String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); String location = dataCursor.getString(DATA_COLUMN_CONTENT); - if (!TextUtils.isEmpty(phoneNumber)) { + if (!TextUtils.isEmpty(phoneNumber)) { // 检查电话号码是否为空 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), - phoneNumber)); + 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)) { + // 打印通话附件位置 + if (!TextUtils.isEmpty(location)) { // 检查位置信息是否为空 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), - location)); + location)); // 打印位置信息 } - } else if (DataConstants.NOTE.equals(mimeType)) { - String content = dataCursor.getString(DATA_COLUMN_CONTENT); - if (!TextUtils.isEmpty(content)) { + } else if (DataConstants.NOTE.equals(mimeType)) { // 处理普通笔记类型 + String content = dataCursor.getString(DATA_COLUMN_CONTENT); // 获取笔记内容 + if (!TextUtils.isEmpty(content)) { // 检查内容是否为空 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), - content)); + content)); // 打印笔记内容 } } - } while (dataCursor.moveToNext()); + } while (dataCursor.moveToNext()); // 移动到下一条记录 } - dataCursor.close(); + dataCursor.close(); // 关闭游标 } - // print a line separator between note + // 在笔记之间打印行分隔符 try { ps.write(new byte[] { Character.LINE_SEPARATOR, Character.LETTER_NUMBER }); } catch (IOException e) { - Log.e(TAG, e.toString()); + Log.e(TAG, e.toString()); // 记录异常信息 } } /** - * 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; + if (!externalStorageAvailable()) { // 检查外部存储是否可用 + Log.d(TAG, "Media was not mounted"); // 记录日志 + return STATE_SD_CARD_UNMOUONTED; // 返回SD卡未挂载状态 } - PrintStream ps = getExportToTextPrintStream(); - if (ps == null) { - Log.e(TAG, "get print stream error"); - return STATE_SYSTEM_ERROR; + PrintStream ps = getExportToTextPrintStream(); // 获取打印流 + if (ps == null) { // 检查打印流是否获取成功 + 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, @@ -237,108 +280,112 @@ public class BackupUtils { + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); - if (folderCursor != null) { - if (folderCursor.moveToFirst()) { + 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); + folderName = mContext.getString(R.string.call_record_folder_name); // 获取通话记录文件夹名称 } else { - folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); + folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); // 获取文件夹摘要作为名称 } - if (!TextUtils.isEmpty(folderName)) { - ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); + if (!TextUtils.isEmpty(folderName)) { // 检查文件夹名称是否为空 + ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); // 打印文件夹名称 } - String folderId = folderCursor.getString(NOTE_COLUMN_ID); - exportFolderToText(folderId, ps); - } while (folderCursor.moveToNext()); + String folderId = folderCursor.getString(NOTE_COLUMN_ID); // 获取文件夹ID + exportFolderToText(folderId, ps); // 导出文件夹中的笔记 + } while (folderCursor.moveToNext()); // 移动到下一条记录 } - folderCursor.close(); + 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 + "=0", null, null); - if (noteCursor != null) { - if (noteCursor.moveToFirst()) { + if (noteCursor != null) { // 检查游标是否有效 + if (noteCursor.moveToFirst()) { // 移动到第一条记录 do { + // 打印笔记的最后修改日期 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()); + exportNoteToText(noteId, ps); // 导出笔记内容 + } while (noteCursor.moveToNext()); // 移动到下一条记录 } - noteCursor.close(); + noteCursor.close(); // 关闭游标 } - ps.close(); + ps.close(); // 关闭打印流 - return STATE_SUCCESS; + return STATE_SUCCESS; // 返回成功状态 } /** - * Get a print stream pointed to the file {@generateExportedTextFile} + * 获取指向导出文本文件的打印流 + * @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; + R.string.file_name_txt_format); // 生成SD卡上的文件 + 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; + mFileName = file.getName(); // 保存文件名 + mFileDirectory = mContext.getString(R.string.file_path); // 保存文件目录 + PrintStream ps = null; // 初始化打印流 try { - FileOutputStream fos = new FileOutputStream(file); - ps = new PrintStream(fos); - } catch (FileNotFoundException e) { - e.printStackTrace(); - return null; - } catch (NullPointerException e) { - e.printStackTrace(); - return null; + 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; + return ps; // 返回打印流 } } /** - * 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(); - sb.append(Environment.getExternalStorageDirectory()); - sb.append(context.getString(filePathResId)); - File filedir = new File(sb.toString()); + StringBuilder sb = new StringBuilder(); // 创建字符串构建器 + 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()); + System.currentTimeMillis()))); // 添加带日期的文件名 + File file = new File(sb.toString()); // 创建文件对象 try { - if (!filedir.exists()) { - filedir.mkdir(); + if (!filedir.exists()) { // 检查目录是否存在 + filedir.mkdir(); // 创建目录 } - if (!file.exists()) { - file.createNewFile(); + if (!file.exists()) { // 检查文件是否存在 + file.createNewFile(); // 创建文件 } - return file; - } catch (SecurityException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); + return file; // 返回文件对象 + } catch (SecurityException e) { // 处理安全异常 + e.printStackTrace(); // 打印异常堆栈 + } catch (IOException e) { // 处理IO异常 + e.printStackTrace(); // 打印异常堆栈 } - return null; + return null; // 返回null表示失败 } -} - - +} \ No newline at end of file diff --git a/src/Notes-master/src/net/micode/notes/tool/DataUtils.java b/src/Notes-master/src/net/micode/notes/tool/DataUtils.java index 2a14982..652740b 100644 --- a/src/Notes-master/src/net/micode/notes/tool/DataUtils.java +++ b/src/Notes-master/src/net/micode/notes/tool/DataUtils.java @@ -2,294 +2,379 @@ * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * 您可以在遵守 License 的前提下使用本文件。 + * 您可以从以下地址获取 License 副本: * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 除非适用法律要求或书面同意,本软件根据 License 分发时“按现状”提供, + * 不附带任何明示或暗示的保证或条件。请参阅 License 了解具体的权限和限制。 */ -package net.micode.notes.tool; +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 android.content.ContentProviderOperation; // 导入内容提供者操作类 +import android.content.ContentProviderResult; // 导入内容提供者结果类 +import android.content.ContentResolver; // 导入内容解析器类 +import android.content.ContentUris; // 导入内容URI工具类 +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; +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; // 导入ArrayList类 +import java.util.HashSet; // 导入HashSet类 +/** + * 数据工具类,提供与笔记数据操作相关的实用方法 + * 包括批量删除、移动笔记、查询文件夹和笔记信息等功能 + */ public class DataUtils { - public static final String TAG = "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) { + if (ids == null) { // 检查ID集合是否为空 Log.d(TAG, "the ids is null"); - return true; + return true; // 空集合视为操作成功 } - if (ids.size() == 0) { + if (ids.size() == 0) { // 检查ID集合是否为空 Log.d(TAG, "no id is in the hashset"); - return true; + return true; // 空集合视为操作成功 } - ArrayList operationList = new ArrayList(); - for (long id : ids) { - if(id == Notes.ID_ROOT_FOLDER) { + ArrayList operationList = new ArrayList<>(); // 创建操作列表 + for (long id : ids) { // 遍历ID集合 + if(id == Notes.ID_ROOT_FOLDER) { // 检查是否为根文件夹 Log.e(TAG, "Don't delete system folder root"); - continue; + continue; // 跳过根文件夹,不删除 } ContentProviderOperation.Builder builder = ContentProviderOperation - .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); - operationList.add(builder.build()); + .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) { + 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; + return false; // 操作失败 } - return true; - } catch (RemoteException e) { + return true; // 操作成功 + } catch (RemoteException e) { // 处理远程异常 Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); - } catch (OperationApplicationException e) { + } catch (OperationApplicationException e) { // 处理操作应用异常 Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } - return false; + 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); - values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); - values.put(NoteColumns.LOCAL_MODIFIED, 1); - resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); + ContentValues values = new 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 移动成功返回true,失败返回false + */ public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, - long folderId) { - if (ids == null) { + long folderId) { + if (ids == null) { // 检查ID集合是否为空 Log.d(TAG, "the ids is null"); - return true; + return true; // 空集合视为操作成功 } - ArrayList operationList = new ArrayList(); - for (long id : ids) { + 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); - builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); - operationList.add(builder.build()); + .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) { + 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; + return false; // 操作失败 } - return true; - } catch (RemoteException e) { + return true; // 操作成功 + } catch (RemoteException e) { // 处理远程异常 Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); - } catch (OperationApplicationException e) { + } catch (OperationApplicationException e) { // 处理操作应用异常 Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } - return false; + return false; // 发生异常,操作失败 } /** - * 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, - new String[] { "COUNT(*)" }, - NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", - new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, - null); + 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()) { + int count = 0; // 初始化数量为0 + if(cursor != null) { // 检查游标是否有效 + if(cursor.moveToFirst()) { // 移动到第一条记录 try { - count = cursor.getInt(0); - } catch (IndexOutOfBoundsException e) { + count = cursor.getInt(0); // 获取数量值 + } catch (IndexOutOfBoundsException e) { // 处理索引越界异常 Log.e(TAG, "get folder count failed:" + e.toString()); } finally { - cursor.close(); + cursor.close(); // 关闭游标 } } } - return count; + 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, - NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, - new String [] {String.valueOf(type)}, - null); + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), // 查询指定笔记 + null, // 查询所有列 + NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, // 查询条件:类型匹配且不在回收站 + new String [] {String.valueOf(type)}, // 查询参数 + null); // 排序方式 - boolean exist = false; - if (cursor != null) { - if (cursor.getCount() > 0) { - exist = true; + boolean exist = false; // 初始化存在标志为false + if (cursor != null) { // 检查游标是否有效 + if (cursor.getCount() > 0) { // 检查记录数量 + exist = true; // 存在记录,设置标志为true } - cursor.close(); + cursor.close(); // 关闭游标 } - return exist; + 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); + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), // 查询指定笔记 + null, // 查询所有列 + null, // 无查询条件 + null, // 无查询参数 + null); // 排序方式 - boolean exist = false; - if (cursor != null) { - if (cursor.getCount() > 0) { - exist = true; + boolean exist = false; // 初始化存在标志为false + if (cursor != null) { // 检查游标是否有效 + if (cursor.getCount() > 0) { // 检查记录数量 + exist = true; // 存在记录,设置标志为true } - cursor.close(); + cursor.close(); // 关闭游标 } - return exist; + 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); + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), // 查询指定数据项 + null, // 查询所有列 + null, // 无查询条件 + null, // 无查询参数 + null); // 排序方式 - boolean exist = false; - if (cursor != null) { - if (cursor.getCount() > 0) { - exist = true; + boolean exist = false; // 初始化存在标志为false + if (cursor != null) { // 检查游标是否有效 + if (cursor.getCount() > 0) { // 检查记录数量 + exist = true; // 存在记录,设置标志为true } - cursor.close(); + cursor.close(); // 关闭游标 } - return exist; + 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 + - " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + - " AND " + NoteColumns.SNIPPET + "=?", - new String[] { name }, null); - boolean exist = false; - if(cursor != null) { - if(cursor.getCount() > 0) { - exist = true; + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_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(); + cursor.close(); // 关闭游标 } - return exist; + 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 }, - NoteColumns.PARENT_ID + "=?", - new String[] { String.valueOf(folderId) }, - null); + Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, // 查询笔记表 + new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, // 查询小部件ID和类型 + NoteColumns.PARENT_ID + "=?", // 查询条件:父文件夹ID匹配 + new String[] { String.valueOf(folderId) }, // 查询参数 + null); // 排序方式 - HashSet set = null; - if (c != null) { - if (c.moveToFirst()) { - set = new HashSet(); + HashSet set = null; // 初始化集合为null + if (c != null) { // 检查游标是否有效 + if (c.moveToFirst()) { // 移动到第一条记录 + set = new HashSet<>(); // 创建集合 do { try { - AppWidgetAttribute widget = new AppWidgetAttribute(); - widget.widgetId = c.getInt(0); - widget.widgetType = c.getInt(1); - set.add(widget); - } catch (IndexOutOfBoundsException e) { + 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()); + } while (c.moveToNext()); // 移动到下一条记录 } - c.close(); + c.close(); // 关闭游标 } - return set; + 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 }, - CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", - new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, - null); + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, // 查询数据表 + new String [] { CallNote.PHONE_NUMBER }, // 查询电话号码列 + CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", // 查询条件:笔记ID匹配且类型为通话记录 + new String [] { String.valueOf(noteId), CallNote.CONTENT_ITEM_TYPE }, // 查询参数 + null); // 排序方式 - if (cursor != null && cursor.moveToFirst()) { + if (cursor != null && cursor.moveToFirst()) { // 检查游标是否有效并移动到第一条记录 try { - return cursor.getString(0); - } catch (IndexOutOfBoundsException e) { + return cursor.getString(0); // 返回电话号码 + } catch (IndexOutOfBoundsException e) { // 处理索引越界异常 Log.e(TAG, "Get call number fails " + e.toString()); } finally { - cursor.close(); + cursor.close(); // 关闭游标 } } - return ""; + 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 }, - CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" - + CallNote.PHONE_NUMBER + ",?)", - new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, - null); + Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, // 查询数据表 + new String [] { CallNote.NOTE_ID }, // 查询笔记ID列 + CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" // 查询条件:日期匹配、类型为通话记录且电话号码相等 + + CallNote.PHONE_NUMBER + ",?)", + new String [] { String.valueOf(callDate), CallNote.CONTENT_ITEM_TYPE, phoneNumber }, // 查询参数 + null); // 排序方式 - if (cursor != null) { - if (cursor.moveToFirst()) { + if (cursor != null) { // 检查游标是否有效 + if (cursor.moveToFirst()) { // 移动到第一条记录 try { - return cursor.getLong(0); - } catch (IndexOutOfBoundsException e) { + return cursor.getLong(0); // 返回笔记ID + } catch (IndexOutOfBoundsException e) { // 处理索引越界异常 Log.e(TAG, "Get call note id fails " + e.toString()); } } - cursor.close(); + cursor.close(); // 关闭游标 } - return 0; + return 0; // 未找到返回0 } + /** + * 根据笔记ID获取摘要 + * @param resolver 内容解析器 + * @param noteId 笔记ID + * @return 摘要内容 + * @throws IllegalArgumentException 笔记不存在时抛出异常 + */ public static String getSnippetById(ContentResolver resolver, long noteId) { - Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, - new String [] { NoteColumns.SNIPPET }, - NoteColumns.ID + "=?", - new String [] { String.valueOf(noteId)}, - null); + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_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); + if (cursor != null) { // 检查游标是否有效 + String snippet = ""; // 初始化摘要为空字符串 + if (cursor.moveToFirst()) { // 移动到第一条记录 + snippet = cursor.getString(0); // 获取摘要内容 } - cursor.close(); - return snippet; + cursor.close(); // 关闭游标 + return snippet; // 返回摘要 } - throw new IllegalArgumentException("Note is not found with id: " + noteId); + 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); + if (snippet != null) { // 检查摘要是否为空 + snippet = snippet.trim(); // 去除前后空格 + int index = snippet.indexOf('\n'); // 查找换行符位置 + if (index != -1) { // 如果存在换行符 + snippet = snippet.substring(0, index); // 截取第一行 } } - return snippet; + return snippet; // 返回格式化后的摘要 } -} +} \ No newline at end of file diff --git a/src/Notes-master/src/net/micode/notes/tool/GTaskStringUtils.java b/src/Notes-master/src/net/micode/notes/tool/GTaskStringUtils.java index 666b729..38f639b 100644 --- a/src/Notes-master/src/net/micode/notes/tool/GTaskStringUtils.java +++ b/src/Notes-master/src/net/micode/notes/tool/GTaskStringUtils.java @@ -2,112 +2,118 @@ * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * 您可以在遵守 License 的前提下使用本文件。 + * 您可以从以下地址获取 License 副本: * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 除非适用法律要求或书面同意,本软件根据 License 分发时“按现状”提供, + * 不附带任何明示或暗示的保证或条件。请参阅 License 了解具体的权限和限制。 */ -package net.micode.notes.tool; +package net.micode.notes.tool; // 指定代码所属的包名 +/** + * Google Tasks 相关字符串常量工具类 + * 定义与Google Tasks服务交互时使用的JSON字段名、文件夹名称、元数据标识等常量 + */ public class GTaskStringUtils { + // --------------------------- JSON 字段常量 --------------------------- // + // 操作ID字段名 public final static String GTASK_JSON_ACTION_ID = "action_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"; - + // 创建者ID字段名 public final static String GTASK_JSON_CREATOR_ID = "creator_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"; - + // 当前列表ID字段名 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"; - + // 已删除状态字段名 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"; - + // ID字段名 public final static String GTASK_JSON_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"; - + // 列表ID字段名 public final static String GTASK_JSON_LIST_ID = "list_id"; - + // 列表集合字段名 public final static String GTASK_JSON_LISTS = "lists"; - + // 名称字段名 public final static String GTASK_JSON_NAME = "name"; - + // 新ID字段名(创建操作返回) public final static String GTASK_JSON_NEW_ID = "new_id"; - + // 备注字段名 public final static String GTASK_JSON_NOTES = "notes"; - + // 父级ID字段名 public final static String GTASK_JSON_PARENT_ID = "parent_id"; - + // 前一个兄弟ID字段名(排序用) public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_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]"; - + // 默认文件夹名称 public final static String FOLDER_DEFAULT = "Default"; - + // 通话记录文件夹名称 public final static String FOLDER_CALL_NOTE = "Call_Note"; - + // 元数据文件夹名称(存储同步元数据) public final static String FOLDER_META = "METADATA"; + // --------------------------- 元数据相关常量 --------------------------- // + // 元数据头部:Google Tasks 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-master/src/net/micode/notes/tool/ResourceParser.java b/src/Notes-master/src/net/micode/notes/tool/ResourceParser.java index 1ad3ad6..ebb3dbd 100644 --- a/src/Notes-master/src/net/micode/notes/tool/ResourceParser.java +++ b/src/Notes-master/src/net/micode/notes/tool/ResourceParser.java @@ -2,180 +2,270 @@ * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * 您可以在遵守 License 的前提下使用本文件。 + * 您可以从以下地址获取 License 副本: * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * 除非适用法律要求或书面同意,本软件根据 License 分发时“按现状”提供, + * 不附带任何明示或暗示的保证或条件。请参阅 License 了解具体的权限和限制。 */ -package net.micode.notes.tool; +package net.micode.notes.tool; // 指定代码所属的包名 -import android.content.Context; -import android.preference.PreferenceManager; +import android.content.Context; // 导入Context类,提供应用环境信息 +import android.preference.PreferenceManager; // 导入偏好设置管理类 -import net.micode.notes.R; -import net.micode.notes.ui.NotesPreferenceActivity; +import net.micode.notes.R; // 导入R资源类,访问应用资源 +import net.micode.notes.ui.NotesPreferenceActivity; // 导入笔记偏好设置活动类 +/** + * 资源解析工具类,用于管理和获取笔记应用中的各种资源 + * 包括背景颜色、文本大小、布局样式等资源的ID映射和解析 + */ 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 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 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; + /** + * 笔记编辑界面背景资源内部类 + * 管理笔记编辑界面的背景和标题栏背景资源ID + */ 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 + 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 + 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获取笔记编辑界面背景资源ID + * @param id 颜色ID(0-4) + * @return 对应的背景资源ID + */ public static int getNoteBgResource(int id) { - return BG_EDIT_RESOURCES[id]; + return BG_EDIT_RESOURCES[id]; // 返回对应颜色的背景资源ID } + /** + * 根据颜色ID获取笔记编辑界面标题栏背景资源ID + * @param id 颜色ID(0-4) + * @return 对应的标题栏背景资源ID + */ public static int getNoteTitleBgResource(int id) { - return BG_EDIT_TITLE_RESOURCES[id]; + return BG_EDIT_TITLE_RESOURCES[id]; // 返回对应颜色的标题栏背景资源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; } } + /** + * 笔记列表项背景资源内部类 + * 管理笔记在列表中不同位置(首项、中间项、末项、单项)的背景资源ID + */ 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 + 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 + 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, + 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 + 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 // 红色单项背景 }; + /** + * 根据颜色ID获取列表首项背景资源ID + * @param id 颜色ID(0-4) + * @return 对应的首项背景资源ID + */ public static int getNoteBgFirstRes(int id) { - return BG_FIRST_RESOURCES[id]; + return BG_FIRST_RESOURCES[id]; // 返回对应颜色的首项背景资源ID } + /** + * 根据颜色ID获取列表末项背景资源ID + * @param id 颜色ID(0-4) + * @return 对应的末项背景资源ID + */ public static int getNoteBgLastRes(int id) { - return BG_LAST_RESOURCES[id]; + return BG_LAST_RESOURCES[id]; // 返回对应颜色的末项背景资源ID } + /** + * 根据颜色ID获取列表单项背景资源ID + * @param id 颜色ID(0-4) + * @return 对应的单项背景资源ID + */ public static int getNoteBgSingleRes(int id) { - return BG_SINGLE_RESOURCES[id]; + return BG_SINGLE_RESOURCES[id]; // 返回对应颜色的单项背景资源ID } + /** + * 根据颜色ID获取列表中间项背景资源ID + * @param id 颜色ID(0-4) + * @return 对应的中间项背景资源ID + */ public static int getNoteBgNormalRes(int id) { - return BG_NORMAL_RESOURCES[id]; + return BG_NORMAL_RESOURCES[id]; // 返回对应颜色的中间项背景资源ID } + /** + * 获取文件夹列表项的背景资源ID + * @return 文件夹背景资源ID + */ public static int getFolderBgRes() { - return R.drawable.list_folder; + return R.drawable.list_folder; // 返回文件夹背景资源ID } } + /** + * 桌面小部件背景资源内部类 + * 管理不同尺寸(2x和4x)桌面小部件的背景资源ID + */ public static class WidgetBgResources { + // 2x尺寸桌面小部件背景资源数组 private final static int [] BG_2X_RESOURCES = new int [] { - R.drawable.widget_2x_yellow, - R.drawable.widget_2x_blue, - R.drawable.widget_2x_white, - R.drawable.widget_2x_green, - R.drawable.widget_2x_red, + 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尺寸桌面小部件背景资源ID + * @param id 颜色ID(0-4) + * @return 对应的2x小部件背景资源ID + */ public static int getWidget2xBgResource(int id) { - return BG_2X_RESOURCES[id]; + return BG_2X_RESOURCES[id]; // 返回对应颜色的2x小部件背景资源ID } + // 4x尺寸桌面小部件背景资源数组 private final static int [] BG_4X_RESOURCES = new int [] { - R.drawable.widget_4x_yellow, - R.drawable.widget_4x_blue, - R.drawable.widget_4x_white, - R.drawable.widget_4x_green, - R.drawable.widget_4x_red + 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尺寸桌面小部件背景资源ID + * @param id 颜色ID(0-4) + * @return 对应的4x小部件背景资源ID + */ public static int getWidget4xBgResource(int id) { - return BG_4X_RESOURCES[id]; + return BG_4X_RESOURCES[id]; // 返回对应颜色的4x小部件背景资源ID } } + /** + * 文本样式资源内部类 + * 管理不同文本大小的样式资源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 + R.style.TextAppearanceNormal, // 小号文本样式 + R.style.TextAppearanceMedium, // 中号文本样式 + R.style.TextAppearanceLarge, // 大号文本样式 + R.style.TextAppearanceSuper // 超大号文本样式 }; + /** + * 根据文本大小ID获取对应的文本样式资源ID + * @param id 文本大小ID(0-3) + * @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} + * HACKME: 修复在共享偏好中存储资源ID可能导致的问题。 + * 如果ID大于资源数组长度,返回默认文本大小(中号) */ if (id >= TEXTAPPEARANCE_RESOURCES.length) { - return BG_DEFAULT_FONT_SIZE; + return BG_DEFAULT_FONT_SIZE; // ID越界时返回默认文本大小 } - return TEXTAPPEARANCE_RESOURCES[id]; + return TEXTAPPEARANCE_RESOURCES[id]; // 返回对应文本大小的样式资源ID } + /** + * 获取文本样式资源数组的长度 + * @return 资源数组长度 + */ public static int getResourcesSize() { - return TEXTAPPEARANCE_RESOURCES.length; + return TEXTAPPEARANCE_RESOURCES.length; // 返回资源数组长度 } } -} +} \ No newline at end of file