From 8ab652dfbe7919bf3eb6e710f89104e77846845a Mon Sep 17 00:00:00 2001 From: zhangqing <1770666340@qq.com> Date: Tue, 23 Dec 2025 20:58:36 +0800 Subject: [PATCH] 1 --- .../net/micode/notes/tool/BackupUtils.java | 415 ++++++++++-------- .../src/net/micode/notes/tool/DataUtils.java | 393 ++++++++++------- .../micode/notes/tool/GTaskStringUtils.java | 178 ++++---- .../net/micode/notes/tool/ResourceParser.java | 194 +++++--- 4 files changed, 676 insertions(+), 504 deletions(-) diff --git a/src/小米标签代码/src/net/micode/notes/tool/BackupUtils.java b/src/小米标签代码/src/net/micode/notes/tool/BackupUtils.java index 39f6ec4..d6be559 100644 --- a/src/小米标签代码/src/net/micode/notes/tool/BackupUtils.java +++ b/src/小米标签代码/src/net/micode/notes/tool/BackupUtils.java @@ -1,344 +1,393 @@ +[file name]: BackupUtils.java +[file content begin] /* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * 版权声明:2010-2011年,MiCode开源社区 * * Licensed under the Apache License, Version 2.0 (the "License"); + * 基于Apache License 2.0许可证授权 * you may not use this file except in compliance with the License. + * 除非遵守许可证,否则不得使用此文件 * You may obtain a copy of the License at + * 可以在以下地址获取许可证副本 * * 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. + * 和限制 */ +// 包声明:工具类包 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; - - +// 导入Android相关类 +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; // 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; // 笔记列定义 + +// 导入Java IO类 +import java.io.File; // 文件类 +import java.io.FileNotFoundException; // 文件未找到异常 +import java.io.FileOutputStream; // 文件输出流 +import java.io.IOException; // IO异常 +import java.io.PrintStream; // 打印流 + +// 备份工具类:负责将笔记数据导出为文本文件 public class BackupUtils { - private static final String TAG = "BackupUtils"; - // Singleton stuff - private static BackupUtils sInstance; + private static final String TAG = "BackupUtils"; // 日志标签 + // 单例模式相关 + private static BackupUtils sInstance; // 静态单例实例 + // 获取单例实例的静态方法,使用synchronized确保线程安全 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 - 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; + // 状态常量定义 + 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; // 文本导出器实例 + // 私有构造函数,外部不能直接实例化 private BackupUtils(Context context) { - mTextExport = new TextExport(context); + mTextExport = new TextExport(context); // 创建文本导出器 } + // 检查外部存储是否可用的静态方法 private static boolean externalStorageAvailable() { + // 判断外部存储状态是否为已挂载 return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } + // 导出数据到文本文件的公共方法 public int exportToText() { - return mTextExport.exportToText(); + return mTextExport.exportToText(); // 调用文本导出器的导出方法 } + // 获取导出的文本文件名 public String getExportedTextFileName() { - return mTextExport.mFileName; + return mTextExport.mFileName; // 返回文件名 } + // 获取导出的文本文件目录 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; // 文件目录 + // 构造函数 public TextExport(Context context) { + // 从资源文件获取文本格式化数组 TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); - mContext = context; - mFileName = ""; - mFileDirectory = ""; + mContext = context; // 保存上下文 + mFileName = ""; // 初始化文件名为空 + mFileDirectory = ""; // 初始化文件目录为空 } + // 获取指定索引的格式化字符串 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 - }, null); + NOTE_PROJECTION, // 查询的列 + NoteColumns.PARENT_ID + "=?", // 查询条件:父ID等于指定文件夹ID + new String[] { 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()); + mContext.getString(R.string.format_datetime_mdhm), // 日期时间格式 + notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); // 修改日期 + // 查询属于该笔记的数据 + String noteId = notesCursor.getString(NOTE_COLUMN_ID); // 获取笔记ID + exportNoteToText(noteId, ps); // 导出该笔记的内容 + } while (notesCursor.moveToNext()); // 移动到下一行 } - notesCursor.close(); + notesCursor.close(); // 关闭游标 } } /** - * Export note identified by id to a print stream + * 导出指定笔记到打印流 + * @param noteId 笔记ID + * @param ps 打印流,用于输出文本 */ private void exportNoteToText(String noteId, PrintStream ps) { + // 查询属于该笔记的数据 Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, - DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { - noteId - }, null); + DATA_PROJECTION, // 查询的列 + DataColumns.NOTE_ID + "=?", // 查询条件:笔记ID等于指定笔记ID + new String[] { noteId }, // 查询参数 + null); // 排序方式(无) - if (dataCursor != null) { - if (dataCursor.moveToFirst()) { + if (dataCursor != null) { // 如果游标不为空 + if (dataCursor.moveToFirst()) { // 如果游标移动到第一行 do { + // 获取MIME类型 String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); - if (DataConstants.CALL_NOTE.equals(mimeType)) { - // Print phone number + 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)); } - // 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)); } - } 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)); } } - } 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 + ps.write(new byte[] { // 写入字节数组 + Character.LINE_SEPARATOR, // 行分隔符 + Character.LETTER_NUMBER // 字母数字字符 }); - } catch (IOException e) { - Log.e(TAG, e.toString()); + } catch (IOException e) { // 捕获IO异常 + 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, "(" + NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + " AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER + ") OR " - + NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER, null, null); + + 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 = ""; + // 打印文件夹名称 + String folderName = ""; // 初始化文件夹名称为空 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); } - if (!TextUtils.isEmpty(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 + // 导出根目录下的笔记(父ID为0的笔记) Cursor noteCursor = mContext.getContentResolver().query( Notes.CONTENT_NOTE_URI, NOTE_PROJECTION, NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID - + "=0", null, null); + + "=0", // 查询条件:类型为笔记且父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()); + mContext.getString(R.string.format_datetime_mdhm), // 日期格式 + noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); // 修改日期 + // 查询属于该笔记的数据 + String noteId = noteCursor.getString(NOTE_COLUMN_ID); // 获取笔记ID + 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; + 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 生成的File对象,如果创建失败返回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()); - sb.append(context.getString( - fileNameFormatResId, - DateFormat.format(context.getString(R.string.format_date_ymd), - System.currentTimeMillis()))); - File file = 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, // 文件名格式资源ID + DateFormat.format(context.getString(R.string.format_date_ymd), // 日期格式 + 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 } } - - +[file content end] \ No newline at end of file diff --git a/src/小米标签代码/src/net/micode/notes/tool/DataUtils.java b/src/小米标签代码/src/net/micode/notes/tool/DataUtils.java index 2a14982..b2d1aab 100644 --- a/src/小米标签代码/src/net/micode/notes/tool/DataUtils.java +++ b/src/小米标签代码/src/net/micode/notes/tool/DataUtils.java @@ -1,295 +1,374 @@ +[file name]: DataUtils.java +[file content begin] /* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * 版权声明:2010-2011年,MiCode开源社区 * * Licensed under the Apache License, Version 2.0 (the "License"); + * 基于Apache License 2.0许可证授权 * you may not use this file except in compliance with the License. + * 除非遵守许可证,否则不得使用此文件 * You may obtain a copy of the License at + * 可以在以下地址获取许可证副本 * * 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. + * 和限制 */ +// 包声明:工具类包 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; +// 导入Android相关类 +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; // 小部件属性类 +// 导入Java集合类 +import java.util.ArrayList; // 动态数组类 +import java.util.HashSet; // 哈希集合类 +// 数据库工具类,提供对笔记数据的各种操作 public class DataUtils { - public static final String TAG = "DataUtils"; + public static final String TAG = "DataUtils"; // 日志标签 + + // 批量删除笔记的方法 + // 参数:resolver - 内容解析器,ids - 要删除的笔记ID集合 + // 返回值:boolean - 删除是否成功 public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { - if (ids == null) { - Log.d(TAG, "the ids is null"); - return true; + if (ids == null) { // 如果ID集合为空 + Log.d(TAG, "the ids is null"); // 记录调试日志 + return true; // 返回成功(无需删除) } - if (ids.size() == 0) { - Log.d(TAG, "no id is in the hashset"); - return true; + if (ids.size() == 0) { // 如果ID集合大小为0 + Log.d(TAG, "no id is in the hashset"); // 记录调试日志 + return true; // 返回成功(无需删除) } + // 创建内容提供器操作列表 ArrayList operationList = new ArrayList(); - for (long id : ids) { - if(id == Notes.ID_ROOT_FOLDER) { - Log.e(TAG, "Don't delete system folder root"); - continue; + for (long id : ids) { // 遍历ID集合 + if(id == Notes.ID_ROOT_FOLDER) { // 如果是根文件夹ID + Log.e(TAG, "Don't delete system folder root"); // 记录错误日志 + continue; // 跳过,不删除系统根文件夹 } + // 创建删除操作 ContentProviderOperation.Builder builder = ContentProviderOperation .newDelete(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); - operationList.add(builder.build()); + 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; + // 如果结果为空或无效 + Log.d(TAG, "delete 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 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; + return false; // 返回失败 } + // 将笔记移动到其他文件夹的方法 + // 参数:resolver - 内容解析器,id - 笔记ID,srcFolderId - 源文件夹ID,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); + ContentValues values = new ContentValues(); // 创建内容值对象 + values.put(NoteColumns.PARENT_ID, desFolderId); // 设置父文件夹ID为目标文件夹ID + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); // 设置原始父文件夹ID + values.put(NoteColumns.LOCAL_MODIFIED, 1); // 设置本地修改标志为1(已修改) + // 更新笔记 resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); } + // 批量移动笔记到指定文件夹的方法 + // 参数:resolver - 内容解析器,ids - 笔记ID集合,folderId - 目标文件夹ID + // 返回值:boolean - 移动是否成功 public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { - if (ids == null) { - Log.d(TAG, "the ids is null"); - return true; + if (ids == null) { // 如果ID集合为空 + Log.d(TAG, "the ids is null"); // 记录调试日志 + return true; // 返回成功(无需移动) } + // 创建内容提供器操作列表 ArrayList operationList = new ArrayList(); - for (long id : ids) { + 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()); + 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, "delete notes failed, ids:" + ids.toString()); - return false; + // 如果结果为空或无效 + Log.d(TAG, "delete 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 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; + return false; // 返回失败 } /** - * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} + * 获取用户文件夹数量(排除系统文件夹) + * 参数:resolver - 内容解析器 + * 返回值:int - 用户文件夹数量 */ 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); + 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; // 初始化计数 + if(cursor != null) { // 如果游标不为空 + if(cursor.moveToFirst()) { // 如果游标移动到第一行 try { - count = cursor.getInt(0); - } catch (IndexOutOfBoundsException e) { - Log.e(TAG, "get folder count failed:" + e.toString()); + 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; // 返回计数 } + // 检查指定类型的笔记是否在数据库中可见(不在垃圾箱中) + // 参数:resolver - 内容解析器,noteId - 笔记ID,type - 笔记类型 + // 返回值:boolean - 是否可见 public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { + // 查询指定ID和类型的笔记 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); + 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; // 初始化存在标志 + if (cursor != null) { // 如果游标不为空 + if (cursor.getCount() > 0) { // 如果结果数大于0 + exist = true; // 设置存在标志为true } - cursor.close(); + cursor.close(); // 关闭游标 } - return exist; + return exist; // 返回存在标志 } + // 检查笔记是否存在于笔记数据库中 + // 参数:resolver - 内容解析器,noteId - 笔记ID + // 返回值:boolean - 是否存在 public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { + // 查询指定ID的笔记 Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), - null, null, null, null); + null, // 所有列 + null, // 无条件 + null, // 无参数 + null); // 无排序 - boolean exist = false; - if (cursor != null) { - if (cursor.getCount() > 0) { - exist = true; + boolean exist = false; // 初始化存在标志 + if (cursor != null) { // 如果游标不为空 + if (cursor.getCount() > 0) { // 如果结果数大于0 + exist = true; // 设置存在标志为true } - cursor.close(); + cursor.close(); // 关闭游标 } - return exist; + return exist; // 返回存在标志 } + // 检查数据是否存在于数据数据库中 + // 参数:resolver - 内容解析器,dataId - 数据ID + // 返回值:boolean - 是否存在 public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { + // 查询指定ID的数据 Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), - null, null, null, null); + null, // 所有列 + null, // 无条件 + null, // 无参数 + null); // 无排序 - boolean exist = false; - if (cursor != null) { - if (cursor.getCount() > 0) { - exist = true; + boolean exist = false; // 初始化存在标志 + if (cursor != null) { // 如果游标不为空 + if (cursor.getCount() > 0) { // 如果结果数大于0 + exist = true; // 设置存在标志为true } - cursor.close(); + cursor.close(); // 关闭游标 } - return exist; + return exist; // 返回存在标志 } + // 检查可见文件夹名称是否已存在 + // 参数:resolver - 内容解析器,name - 文件夹名称 + // 返回值:boolean - 是否存在 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; + 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) { // 如果结果数大于0 + exist = true; // 设置存在标志为true } - cursor.close(); + cursor.close(); // 关闭游标 } - return exist; + return exist; // 返回存在标志 } + // 获取文件夹中的笔记小部件属性 + // 参数:resolver - 内容解析器,folderId - 文件夹ID + // 返回值:HashSet - 小部件属性集合 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); + 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; // 初始化小部件属性集合 + 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) { - Log.e(TAG, e.toString()); + 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获取通话号码 + // 参数:resolver - 内容解析器,noteId - 笔记ID + // 返回值:String - 电话号码,如果不存在返回空字符串 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); + new String [] { CallNote.PHONE_NUMBER }, // 查询电话号码列 + CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", // 条件 + 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) { - Log.e(TAG, "Get call number fails " + e.toString()); + return cursor.getString(0); // 返回电话号码 + } catch (IndexOutOfBoundsException e) { // 捕获索引越界异常 + Log.e(TAG, "Get call number fails " + e.toString()); // 记录错误日志 } finally { - cursor.close(); + cursor.close(); // 关闭游标 } } - return ""; + return ""; // 返回空字符串 } + // 根据电话号码和通话日期获取笔记ID + // 参数:resolver - 内容解析器,phoneNumber - 电话号码,callDate - 通话日期 + // 返回值:long - 笔记ID,如果不存在返回0 public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { + // 查询通话笔记的笔记ID Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, - new String [] { CallNote.NOTE_ID }, + 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); + + 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) { - Log.e(TAG, "Get call note id fails " + e.toString()); + 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获取内容摘要 + // 参数:resolver - 内容解析器,noteId - 笔记ID + // 返回值:String - 内容摘要,如果不存在抛出异常 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); + new String [] { NoteColumns.SNIPPET }, // 查询内容摘要列 + NoteColumns.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); // 抛出异常 } + // 格式化内容摘要(去除换行符和多余空格) + // 参数:snippet - 原始内容摘要 + // 返回值:String - 格式化后的内容摘要 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; // 返回格式化后的内容摘要 } } +[file content end] \ No newline at end of file diff --git a/src/小米标签代码/src/net/micode/notes/tool/GTaskStringUtils.java b/src/小米标签代码/src/net/micode/notes/tool/GTaskStringUtils.java index 666b729..ff7ed6b 100644 --- a/src/小米标签代码/src/net/micode/notes/tool/GTaskStringUtils.java +++ b/src/小米标签代码/src/net/micode/notes/tool/GTaskStringUtils.java @@ -1,113 +1,109 @@ +[file name]: GTaskStringUtils.java +[file content begin] /* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * 版权声明:2010-2011年,MiCode开源社区 * * Licensed under the Apache License, Version 2.0 (the "License"); + * 基于Apache License 2.0许可证授权 * you may not use this file except in compliance with the License. + * 除非遵守许可证,否则不得使用此文件 * You may obtain a copy of the License at + * 可以在以下地址获取许可证副本 * * 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. + * 和限制 */ +// 包声明:工具类包 package net.micode.notes.tool; +// Google Task相关JSON字段和常量的工具类 public class GTaskStringUtils { - 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"; - - 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"; - - public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_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"; - - 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"; - - 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"; - - public final static String GTASK_JSON_NEW_ID = "new_id"; - - public final static String GTASK_JSON_NOTES = "notes"; - - public final static String GTASK_JSON_PARENT_ID = "parent_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"; - - 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"; - - 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"; + // 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_ENTITY_DELTA = "entity_delta"; // 实体增量字段 + public final static String GTASK_JSON_ENTITY_TYPE = "entity_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_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_DEST_LIST = "dest_list"; // 目标列表字段 + 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_SOURCE_LIST = "source_list"; // 源列表字段 + + // 删除相关字段 + public final static String GTASK_JSON_DELETED = "deleted"; // 删除状态字段 + public final static String GTASK_JSON_GET_DELETED = "get_deleted"; // 获取删除项字段 + + // 父级相关字段 + 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_PARENT_ID = "parent_id"; // 父级ID字段 + + // 通用字段 + 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_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_PRIOR_SIBLING_ID = "prior_sibling_id"; // 前兄弟ID字段 + public final static String GTASK_JSON_RESULTS = "results"; // 结果字段 + public final static String GTASK_JSON_TASKS = "tasks"; // 任务字段 + public final static String GTASK_JSON_TYPE = "type"; // 类型字段 + 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"; // 元数据笔记名称 } +[file content end] \ No newline at end of file diff --git a/src/小米标签代码/src/net/micode/notes/tool/ResourceParser.java b/src/小米标签代码/src/net/micode/notes/tool/ResourceParser.java index 1ad3ad6..308775f 100644 --- a/src/小米标签代码/src/net/micode/notes/tool/ResourceParser.java +++ b/src/小米标签代码/src/net/micode/notes/tool/ResourceParser.java @@ -1,181 +1,229 @@ +[file name]: ResourceParser.java +[file content begin] /* * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) + * 版权声明:2010-2011年,MiCode开源社区 * * Licensed under the Apache License, Version 2.0 (the "License"); + * 基于Apache License 2.0许可证授权 * you may not use this file except in compliance with the License. + * 除非遵守许可证,否则不得使用此文件 * You may obtain a copy of the License at + * 可以在以下地址获取许可证副本 * * 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. + * 和限制 */ +// 包声明:工具类包 package net.micode.notes.tool; -import android.content.Context; -import android.preference.PreferenceManager; +// 导入Android相关类 +import android.content.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; // 笔记偏好设置活动 +// 资源解析器,用于处理笔记的背景颜色、字体大小等资源 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 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; + // 默认字体大小 + 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 + R.drawable.edit_yellow, // 黄色编辑背景资源ID + R.drawable.edit_blue, // 蓝色编辑背景资源ID + R.drawable.edit_white, // 白色编辑背景资源ID + R.drawable.edit_green, // 绿色编辑背景资源ID + R.drawable.edit_red // 红色编辑背景资源ID }; + // 编辑标题背景资源数组,对应不同颜色的标题背景图片 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, // 黄色标题背景资源ID + R.drawable.edit_title_blue, // 蓝色标题背景资源ID + R.drawable.edit_title_white, // 白色标题背景资源ID + R.drawable.edit_title_green, // 绿色标题背景资源ID + R.drawable.edit_title_red // 红色标题背景资源ID }; + // 获取笔记背景资源ID的方法 public static int getNoteBgResource(int id) { - return BG_EDIT_RESOURCES[id]; + return BG_EDIT_RESOURCES[id]; // 返回指定ID的背景资源 } + // 获取笔记标题背景资源ID的方法 public static int getNoteTitleBgResource(int id) { - return BG_EDIT_TITLE_RESOURCES[id]; + return BG_EDIT_TITLE_RESOURCES[id]; // 返回指定ID的标题背景资源 } } + // 获取默认背景ID的方法 public static int getDefaultBgId(Context context) { + // 检查偏好设置中是否启用了随机背景颜色 if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { + // 如果启用了随机背景颜色,则随机选择一个背景颜色 return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); } else { + // 否则使用默认背景颜色 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 + 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 // 红色单个背景 }; + // 获取第一个列表项背景资源的方法 public static int getNoteBgFirstRes(int id) { - return BG_FIRST_RESOURCES[id]; + return BG_FIRST_RESOURCES[id]; // 返回指定ID的第一个列表项背景资源 } + // 获取最后一个列表项背景资源的方法 public static int getNoteBgLastRes(int id) { - return BG_LAST_RESOURCES[id]; + return BG_LAST_RESOURCES[id]; // 返回指定ID的最后一个列表项背景资源 } + // 获取单个列表项背景资源的方法 public static int getNoteBgSingleRes(int id) { - return BG_SINGLE_RESOURCES[id]; + return BG_SINGLE_RESOURCES[id]; // 返回指定ID的单个列表项背景资源 } + // 获取中间列表项背景资源的方法 public static int getNoteBgNormalRes(int id) { - return BG_NORMAL_RESOURCES[id]; + return BG_NORMAL_RESOURCES[id]; // 返回指定ID的中间列表项背景资源 } + // 获取文件夹背景资源的方法 public static int getFolderBgRes() { - return R.drawable.list_folder; + return R.drawable.list_folder; // 返回文件夹背景资源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小部件背景 }; + // 获取2x小部件背景资源的方法 public static int getWidget2xBgResource(int id) { - return BG_2X_RESOURCES[id]; + return BG_2X_RESOURCES[id]; // 返回指定ID的2x小部件背景资源 } + // 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小部件背景 }; + // 获取4x小部件背景资源的方法 public static int getWidget4xBgResource(int id) { - return BG_4X_RESOURCES[id]; + return BG_4X_RESOURCES[id]; // 返回指定ID的4x小部件背景资源 } } + // 文本外观资源类:处理文本样式资源 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 // 超大文本外观 }; + // 获取文本外观资源的方法 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: 修复在SharedPreference中存储资源ID的bug + * ID可能大于资源数组长度,这种情况下返回默认字体大小 */ if (id >= TEXTAPPEARANCE_RESOURCES.length) { + // 如果ID超出范围,返回默认字体大小 return BG_DEFAULT_FONT_SIZE; } - return TEXTAPPEARANCE_RESOURCES[id]; + return TEXTAPPEARANCE_RESOURCES[id]; // 返回指定ID的文本外观资源 } + // 获取资源数组大小的方法 public static int getResourcesSize() { - return TEXTAPPEARANCE_RESOURCES.length; + return TEXTAPPEARANCE_RESOURCES.length; // 返回文本外观资源数组的长度 } } } +[file content end] \ No newline at end of file