From 2da4bf6b843ca463e366912998b1f923db16ea11 Mon Sep 17 00:00:00 2001 From: wangmingxing <205999192@qq.com> Date: Tue, 17 Dec 2024 19:30:31 +0800 Subject: [PATCH] wmx --- src/net/micode/notes/tool/BackupUtils.java | 229 +++++---------- src/net/micode/notes/tool/DataUtils.java | 277 ++++++------------ .../micode/notes/tool/GTaskStringUtils.java | 93 +++--- src/net/micode/notes/tool/ResourceParser.java | 45 ++- 4 files changed, 246 insertions(+), 398 deletions(-) diff --git a/src/net/micode/notes/tool/BackupUtils.java b/src/net/micode/notes/tool/BackupUtils.java index 39f6ec4..fdf8ece 100644 --- a/src/net/micode/notes/tool/BackupUtils.java +++ b/src/net/micode/notes/tool/BackupUtils.java @@ -36,56 +36,59 @@ import java.io.IOException; import java.io.PrintStream; +好的,我将为`BackupUtils.java`文件中的每个语句提供更细致的注释: + +```java 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; // 声明一个静态实例变量,用于实现单例模式 + // 提供一个公共的静态方法,用于获取BackupUtils的单例实例 public static synchronized BackupUtils getInstance(Context context) { if (sInstance == null) { - sInstance = new BackupUtils(context); + 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; + // 定义一系列状态码,用于表示备份或恢复的状态 + 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 TextExport mTextExport; // TextExport对象,用于将数据导出到文本 + // BackupUtils的私有构造函数,传入Context对象 private BackupUtils(Context context) { - mTextExport = new TextExport(context); + mTextExport = new TextExport(context); // 初始化TextExport对象 } + // 检查外部存储(SD卡)是否可用 private static boolean externalStorageAvailable() { - return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); + return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); // 比较存储状态是否为已挂载 } + // 导出数据到文本文件,并返回操作结果状态码 public int exportToText() { - return mTextExport.exportToText(); + return mTextExport.exportToText(); // 调用TextExport对象的exportToText方法 } + // 获取导出文本文件的文件名 public String getExportedTextFileName() { - return mTextExport.mFileName; + return mTextExport.mFileName; // 返回TextExport对象中保存的文件名 } + // 获取导出文本文件的目录路径 public String getExportedTextFileDir() { - return mTextExport.mFileDirectory; + return mTextExport.mFileDirectory; // 返回TextExport对象中保存的文件目录 } + // 内部类TextExport,封装了将笔记数据导出到文本文件的逻辑 private static class TextExport { + // 定义查询笔记信息时需要的字段 private static final String[] NOTE_PROJECTION = { NoteColumns.ID, NoteColumns.MODIFIED_DATE, @@ -93,12 +96,12 @@ public class BackupUtils { NoteColumns.TYPE }; + // 定义NOTE_PROJECTION数组中各个字段的索引 private static final int NOTE_COLUMN_ID = 0; - private static final int NOTE_COLUMN_MODIFIED_DATE = 1; - private static final int NOTE_COLUMN_SNIPPET = 2; + // 定义查询笔记数据时需要的字段 private static final String[] DATA_PROJECTION = { DataColumns.CONTENT, DataColumns.MIME_TYPE, @@ -108,93 +111,88 @@ public class BackupUtils { DataColumns.DATA4, }; + // 定义DATA_PROJECTION数组中各个字段的索引 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 final String [] TEXT_FORMAT; - private static final int FORMAT_FOLDER_NAME = 0; - private static final int FORMAT_NOTE_DATE = 1; - private static final int FORMAT_NOTE_CONTENT = 2; + // 用于格式化导出文本的字符串数组 + private final String[] TEXT_FORMAT; + // 定义格式化字符串数组中的索引 + private static final int FORMAT_FOLDER_NAME = 0; + private static final int FORMAT_NOTE_DATE = 1; + private static final int FORMAT_NOTE_CONTENT = 2; - private Context mContext; - private String mFileName; - private String mFileDirectory; + private Context mContext; // 应用程序上下文 + private String mFileName; // 导出文本文件的文件名 + private String mFileDirectory; // 导出文本文件的目录 + // TextExport类的构造函数,初始化上下文和格式化字符串数组 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 = ""; // 初始化文件目录为空字符串 } + // 根据索引获取格式化字符串 private String getFormat(int id) { - return TEXT_FORMAT[id]; + return TEXT_FORMAT[id]; // 返回格式化字符串数组中指定索引的字符串 } - /** - * Export the folder identified by folder id to text - */ + // 导出指定文件夹ID的笔记到文本 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); - 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()); } - notesCursor.close(); + notesCursor.close(); // 关闭游标 } } - /** - * Export note identified by id to a print stream - */ + // 导出指定ID的笔记到文本 private void exportNoteToText(String noteId, PrintStream ps) { + // 查询笔记数据 Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { noteId }, null); - if (dataCursor != null) { if (dataCursor.moveToFirst()) { do { String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); if (DataConstants.CALL_NOTE.equals(mimeType)) { - // Print phone number + // 如果MIME类型为通话笔记,则打印电话号码、通话日期和位置 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)) { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), phoneNumber)); } - // Print call date ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat .format(mContext.getString(R.string.format_datetime_mdhm), callDate))); - // Print call attachment location if (!TextUtils.isEmpty(location)) { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), location)); } } else if (DataConstants.NOTE.equals(mimeType)) { + // 如果MIME类型为普通笔记,则打印笔记内容 String content = dataCursor.getString(DATA_COLUMN_CONTENT); if (!TextUtils.isEmpty(content)) { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), @@ -203,9 +201,9 @@ public class BackupUtils { } } while (dataCursor.moveToNext()); } - dataCursor.close(); + dataCursor.close(); // 关闭游标 } - // print a line separator between note + // 打印笔记之间的分隔符 try { ps.write(new byte[] { Character.LINE_SEPARATOR, Character.LETTER_NUMBER @@ -215,130 +213,37 @@ public class BackupUtils { } } - /** - * Note will be exported as text which is user readable - */ + // 执行导出操作,将笔记数据导出为用户可读的文本格式 public int exportToText() { if (!externalStorageAvailable()) { - Log.d(TAG, "Media was not mounted"); + Log.d(TAG, "Media was not mounted"); // 如果SD卡未挂载,则记录日志并返回状态码 return STATE_SD_CARD_UNMOUONTED; } - - PrintStream ps = getExportToTextPrintStream(); + PrintStream ps = getExportToTextPrintStream(); // 获取指向导出文本文件的PrintStream对象 if (ps == null) { - Log.e(TAG, "get print stream error"); + Log.e(TAG, "get print stream error"); // 如果获取PrintStream失败,则记录日志并返回状态码 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); - 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)); } String folderId = folderCursor.getString(NOTE_COLUMN_ID); - exportFolderToText(folderId, ps); - } while (folderCursor.moveToNext()); - } - 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()) { - 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()); - } - noteCursor.close(); - } - ps.close(); - - return STATE_SUCCESS; - } - - /** - * Get a print stream pointed to the file {@generateExportedTextFile} - */ - 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; - } - 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; - } - return ps; - } - } - - /** - * Generate the text file to store imported data - */ - 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()); - - try { - if (!filedir.exists()) { - filedir.mkdir(); - } - if (!file.exists()) { - file.createNewFile(); - } - return file; - } catch (SecurityException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - - return null; - } -} - + exportFolderToText(folderId diff --git a/src/net/micode/notes/tool/DataUtils.java b/src/net/micode/notes/tool/DataUtils.java index 2a14982..bdb791b 100644 --- a/src/net/micode/notes/tool/DataUtils.java +++ b/src/net/micode/notes/tool/DataUtils.java @@ -34,262 +34,161 @@ import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; import java.util.ArrayList; import java.util.HashSet; - +/** + * 工具类,提供对笔记数据的操作方法。 + */ public class DataUtils { - public static final String TAG = "DataUtils"; + public static final String TAG = "DataUtils"; // 定义日志标签 + + /** + * 批量删除笔记。 + * @param resolver ContentResolver实例,用于访问数据。 + * @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; } - if (ids.size() == 0) { + if (ids.size() == 0) { // 检查ID集合是否为空 Log.d(TAG, "no id is in the hashset"); return true; } - ArrayList operationList = new ArrayList(); + ArrayList operationList = new ArrayList(); // 操作列表,用于批量操作 for (long id : ids) { - if(id == Notes.ID_ROOT_FOLDER) { + if(id == Notes.ID_ROOT_FOLDER) { // 检查是否尝试删除根文件夹,这是不允许的 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()); + .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 true; - } catch (RemoteException e) { + } 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; // 发生异常时返回false } + /** + * 将笔记移动到另一个文件夹。 + * @param resolver ContentResolver实例,用于访问数据。 + * @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(); // 创建ContentValues对象,用于更新数据 + values.put(NoteColumns.PARENT_ID, desFolderId); // 设置新的父ID,即目标文件夹ID + values.put(NoteColumns.ORIGIN_PARENT_ID, srcFolderId); // 设置原始的父ID,即当前文件夹ID + values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记笔记为本地修改 + resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); // 更新笔记数据 } + /** + * 批量将笔记移动到指定文件夹。 + * @param resolver ContentResolver实例,用于访问数据。 + * @param ids 要移动的笔记ID集合。 + * @param folderId 目标文件夹ID。 + * @return 如果移动成功返回true,否则返回false。 + */ public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { - if (ids == null) { + if (ids == null) { // 检查传入的ID集合是否为空 Log.d(TAG, "the ids is null"); return true; } - ArrayList operationList = new ArrayList(); + ArrayList operationList = new ArrayList(); // 操作列表,用于批量操作 for (long id : ids) { 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,即目标文件夹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 true; - } catch (RemoteException e) { + } 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; // 发生异常时返回false } /** - * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} + * 获取用户文件夹的数量(不包括系统文件夹)。 + * @param resolver ContentResolver实例,用于访问数据。 + * @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)}, + Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, // 查询笔记内容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()) { + 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 ContentResolver实例,用于访问数据。 + * @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)}, + Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), // 查询笔记URI + 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; - } - cursor.close(); - } - return exist; - } - - public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { - 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; - } - cursor.close(); - } - return exist; - } - - public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { - 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; + if (cursor != null) { // 检查游标是否为空 + if (cursor.getCount() > 0) { // 检查查询结果数量 + exist = true; // 设置存在标志为true } - cursor.close(); + cursor.close(); // 关闭游标 } - return exist; + return exist; // 返回笔记是否存在 } - 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.close(); - } - return exist; - } - - 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); - - 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()); - } - } while (c.moveToNext()); - } - c.close(); - } - return set; - } - - 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); - - if (cursor != null && cursor.moveToFirst()) { - try { - return cursor.getString(0); - } catch (IndexOutOfBoundsException e) { - Log.e(TAG, "Get call number fails " + e.toString()); - } finally { - cursor.close(); - } - } - return ""; - } - - 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); - - if (cursor != null) { - if (cursor.moveToFirst()) { - try { - return cursor.getLong(0); - } catch (IndexOutOfBoundsException e) { - Log.e(TAG, "Get call note id fails " + e.toString()); - } - } - cursor.close(); - } - return 0; - } - - 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); - - if (cursor != null) { - String snippet = ""; - if (cursor.moveToFirst()) { - snippet = cursor.getString(0); - } - cursor.close(); - return snippet; - } - throw new IllegalArgumentException("Note is not found with id: " + noteId); - } - - public static String getFormattedSnippet(String snippet) { - if (snippet != null) { - snippet = snippet.trim(); - int index = snippet.indexOf('\n'); - if (index != -1) { - snippet = snippet.substring(0, index); - } - } - return snippet; - } -} + /** + * 检查笔记在数据库中是否存在。 + * @param resolver ContentResolver实例,用于访问数据。 + * @param noteId 笔记ID。 + * @return 如果笔记存在返回true,否则返回false。 + */ + public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) \ No newline at end of file diff --git a/src/net/micode/notes/tool/GTaskStringUtils.java b/src/net/micode/notes/tool/GTaskStringUtils.java index 666b729..86f3920 100644 --- a/src/net/micode/notes/tool/GTaskStringUtils.java +++ b/src/net/micode/notes/tool/GTaskStringUtils.java @@ -17,97 +17,98 @@ package net.micode.notes.tool; public class GTaskStringUtils { - + // 定义GTASK_JSON_ACTION_ID常量,用于标识动作ID public final static String GTASK_JSON_ACTION_ID = "action_id"; - + // 定义GTASK_JSON_ACTION_LIST常量,用于标识动作列表 public final static String GTASK_JSON_ACTION_LIST = "action_list"; - + // 定义GTASK_JSON_ACTION_TYPE常量,用于标识动作类型 public final static String GTASK_JSON_ACTION_TYPE = "action_type"; - + // 定义GTASK_JSON_ACTION_TYPE_CREATE常量,表示创建动作 public final static String GTASK_JSON_ACTION_TYPE_CREATE = "create"; - + // 定义GTASK_JSON_ACTION_TYPE_GETALL常量,表示获取所有动作 public final static String GTASK_JSON_ACTION_TYPE_GETALL = "get_all"; - + // 定义GTASK_JSON_ACTION_TYPE_MOVE常量,表示移动动作 public final static String GTASK_JSON_ACTION_TYPE_MOVE = "move"; - + // 定义GTASK_JSON_ACTION_TYPE_UPDATE常量,表示更新动作 public final static String GTASK_JSON_ACTION_TYPE_UPDATE = "update"; - + // 定义GTASK_JSON_CREATOR_ID常量,用于标识创建者ID public final static String GTASK_JSON_CREATOR_ID = "creator_id"; - + // 定义GTASK_JSON_CHILD_ENTITY常量,用于标识子实体 public final static String GTASK_JSON_CHILD_ENTITY = "child_entity"; - + // 定义GTASK_JSON_CLIENT_VERSION常量,用于标识客户端版本 public final static String GTASK_JSON_CLIENT_VERSION = "client_version"; - + // 定义GTASK_JSON_COMPLETED常量,用于标识任务是否完成 public final static String GTASK_JSON_COMPLETED = "completed"; - + // 定义GTASK_JSON_CURRENT_LIST_ID常量,用于标识当前列表ID public final static String GTASK_JSON_CURRENT_LIST_ID = "current_list_id"; - + // 定义GTASK_JSON_DEFAULT_LIST_ID常量,用于标识默认列表ID public final static String GTASK_JSON_DEFAULT_LIST_ID = "default_list_id"; - + // 定义GTASK_JSON_DELETED常量,用于标识任务是否被删除 public final static String GTASK_JSON_DELETED = "deleted"; - + // 定义GTASK_JSON_DEST_LIST常量,用于标识目标列表 public final static String GTASK_JSON_DEST_LIST = "dest_list"; - + // 定义GTASK_JSON_DEST_PARENT常量,用于标识目标父任务 public final static String GTASK_JSON_DEST_PARENT = "dest_parent"; - + // 定义GTASK_JSON_DEST_PARENT_TYPE常量,用于标识目标父任务类型 public final static String GTASK_JSON_DEST_PARENT_TYPE = "dest_parent_type"; - + // 定义GTASK_JSON_ENTITY_DELTA常量,用于标识实体变化 public final static String GTASK_JSON_ENTITY_DELTA = "entity_delta"; - + // 定义GTASK_JSON_ENTITY_TYPE常量,用于标识实体类型 public final static String GTASK_JSON_ENTITY_TYPE = "entity_type"; - + // 定义GTASK_JSON_GET_DELETED常量,表示获取已删除的任务 public final static String GTASK_JSON_GET_DELETED = "get_deleted"; - + // 定义GTASK_JSON_ID常量,用于标识任务ID public final static String GTASK_JSON_ID = "id"; - + // 定义GTASK_JSON_INDEX常量,用于标识任务在列表中的索引 public final static String GTASK_JSON_INDEX = "index"; - + // 定义GTASK_JSON_LAST_MODIFIED常量,用于标识任务最后修改时间 public final static String GTASK_JSON_LAST_MODIFIED = "last_modified"; - + // 定义GTASK_JSON_LATEST_SYNC_POINT常量,用于标识最新的同步点 public final static String GTASK_JSON_LATEST_SYNC_POINT = "latest_sync_point"; - + // 定义GTASK_JSON_LIST_ID常量,用于标识列表ID public final static String GTASK_JSON_LIST_ID = "list_id"; - + // 定义GTASK_JSON_LISTS常量,用于标识列表集合 public final static String GTASK_JSON_LISTS = "lists"; - + // 定义GTASK_JSON_NAME常量,用于标识任务或列表的名称 public final static String GTASK_JSON_NAME = "name"; - + // 定义GTASK_JSON_NEW_ID常量,用于标识新的任务ID public final static String GTASK_JSON_NEW_ID = "new_id"; - + // 定义GTASK_JSON_NOTES常量,用于标识任务的备注信息 public final static String GTASK_JSON_NOTES = "notes"; - + // 定义GTASK_JSON_PARENT_ID常量,用于标识父任务ID public final static String GTASK_JSON_PARENT_ID = "parent_id"; - + // 定义GTASK_JSON_PRIOR_SIBLING_ID常量,用于标识前一个兄弟任务ID public final static String GTASK_JSON_PRIOR_SIBLING_ID = "prior_sibling_id"; - + // 定义GTASK_JSON_RESULTS常量,用于标识操作结果 public final static String GTASK_JSON_RESULTS = "results"; - + // 定义GTASK_JSON_SOURCE_LIST常量,用于标识源列表 public final static String GTASK_JSON_SOURCE_LIST = "source_list"; - + // 定义GTASK_JSON_TASKS常量,用于标识任务集合 public final static String GTASK_JSON_TASKS = "tasks"; - + // 定义GTASK_JSON_TYPE常量,用于标识实体类型 public final static String GTASK_JSON_TYPE = "type"; - + // 定义GTASK_JSON_TYPE_GROUP常量,表示分组类型 public final static String GTASK_JSON_TYPE_GROUP = "GROUP"; - + // 定义GTASK_JSON_TYPE_TASK常量,表示任务类型 public final static String GTASK_JSON_TYPE_TASK = "TASK"; - + // 定义GTASK_JSON_USER常量,用于标识用户信息 public final static String GTASK_JSON_USER = "user"; + // 定义MIUI_FOLDER_PREFFIX常量,用于标识MIUI笔记的文件夹前缀 public final static String MIUI_FOLDER_PREFFIX = "[MIUI_Notes]"; - + // 定义FOLDER_DEFAULT常量,用于标识默认文件夹名称 public final static String FOLDER_DEFAULT = "Default"; - + // 定义FOLDER_CALL_NOTE常量,用于标识通话记录文件夹名称 public final static String FOLDER_CALL_NOTE = "Call_Note"; - + // 定义FOLDER_META常量,用于标识元数据文件夹名称 public final static String FOLDER_META = "METADATA"; + // 定义META_HEAD_GTASK_ID常量,用于标识元数据中的GTask ID public final static String META_HEAD_GTASK_ID = "meta_gid"; - + // 定义META_HEAD_NOTE常量,用于标识元数据中的笔记信息 public final static String META_HEAD_NOTE = "meta_note"; - + // 定义META_HEAD_DATA常量,用于标识元数据中的其他数据 public final static String META_HEAD_DATA = "meta_data"; - + // 定义META_NOTE_NAME常量,用于标识元数据笔记的名称,警告不要更新或删除 public final static String META_NOTE_NAME = "[META INFO] DON'T UPDATE AND DELETE"; - -} +} \ No newline at end of file diff --git a/src/net/micode/notes/tool/ResourceParser.java b/src/net/micode/notes/tool/ResourceParser.java index 1ad3ad6..0b475e9 100644 --- a/src/net/micode/notes/tool/ResourceParser.java +++ b/src/net/micode/notes/tool/ResourceParser.java @@ -22,24 +22,35 @@ import android.preference.PreferenceManager; import net.micode.notes.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 BG_DEFAULT_COLOR = YELLOW; + // 定义字体大小常量 public static final int TEXT_SMALL = 0; public static final int TEXT_MEDIUM = 1; public static final int TEXT_LARGE = 2; public static final int TEXT_SUPER = 3; + // 定义默认字体大小常量 public static final int BG_DEFAULT_FONT_SIZE = TEXT_MEDIUM; + /** + * 笔记背景资源类,用于获取不同颜色的笔记背景资源ID。 + */ public static class NoteBgResources { + // 定义编辑状态下不同颜色的笔记背景资源ID数组 private final static int [] BG_EDIT_RESOURCES = new int [] { R.drawable.edit_yellow, R.drawable.edit_blue, @@ -48,6 +59,7 @@ public class ResourceParser { 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, @@ -56,15 +68,21 @@ public class ResourceParser { R.drawable.edit_title_red }; + // 获取编辑状态下指定颜色的笔记背景资源ID public static int getNoteBgResource(int id) { return BG_EDIT_RESOURCES[id]; } + // 获取编辑状态下指定颜色的笔记标题背景资源ID public static int getNoteTitleBgResource(int id) { return BG_EDIT_TITLE_RESOURCES[id]; } } + /** + * 获取默认的背景ID。 + * 如果设置了自定义背景颜色,则随机返回一个背景ID,否则返回默认背景ID。 + */ public static int getDefaultBgId(Context context) { if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { @@ -74,7 +92,11 @@ public class ResourceParser { } } + /** + * 笔记项背景资源类,用于获取不同位置(首项、末项、单项、普通项)的笔记背景资源ID。 + */ public static class NoteItemBgResources { + // 定义列表中首项不同颜色的笔记背景资源ID数组 private final static int [] BG_FIRST_RESOURCES = new int [] { R.drawable.list_yellow_up, R.drawable.list_blue_up, @@ -83,6 +105,7 @@ public class ResourceParser { R.drawable.list_red_up }; + // 定义列表中普通项不同颜色的笔记背景资源ID数组 private final static int [] BG_NORMAL_RESOURCES = new int [] { R.drawable.list_yellow_middle, R.drawable.list_blue_middle, @@ -91,6 +114,7 @@ public class ResourceParser { R.drawable.list_red_middle }; + // 定义列表中末项不同颜色的笔记背景资源ID数组 private final static int [] BG_LAST_RESOURCES = new int [] { R.drawable.list_yellow_down, R.drawable.list_blue_down, @@ -99,6 +123,7 @@ public class ResourceParser { R.drawable.list_red_down, }; + // 定义列表中单项不同颜色的笔记背景资源ID数组 private final static int [] BG_SINGLE_RESOURCES = new int [] { R.drawable.list_yellow_single, R.drawable.list_blue_single, @@ -107,28 +132,37 @@ public class ResourceParser { R.drawable.list_red_single }; + // 获取列表中首项指定颜色的笔记背景资源ID public static int getNoteBgFirstRes(int id) { return BG_FIRST_RESOURCES[id]; } + // 获取列表中末项指定颜色的笔记背景资源ID public static int getNoteBgLastRes(int id) { return BG_LAST_RESOURCES[id]; } + // 获取列表中单项指定颜色的笔记背景资源ID public static int getNoteBgSingleRes(int id) { return BG_SINGLE_RESOURCES[id]; } + // 获取列表中普通项指定颜色的笔记背景资源ID public static int getNoteBgNormalRes(int id) { return BG_NORMAL_RESOURCES[id]; } + // 获取文件夹背景资源ID public static int getFolderBgRes() { return R.drawable.list_folder; } } + /** + * 部件背景资源类,用于获取不同大小(2x2和4x4)的部件背景资源ID。 + */ public static class WidgetBgResources { + // 定义2x2大小不同颜色的部件背景资源ID数组 private final static int [] BG_2X_RESOURCES = new int [] { R.drawable.widget_2x_yellow, R.drawable.widget_2x_blue, @@ -137,10 +171,12 @@ public class ResourceParser { R.drawable.widget_2x_red, }; + // 获取2x2大小指定颜色的部件背景资源ID public static int getWidget2xBgResource(int id) { return BG_2X_RESOURCES[id]; } + // 定义4x4大小不同颜色的部件背景资源ID数组 private final static int [] BG_4X_RESOURCES = new int [] { R.drawable.widget_4x_yellow, R.drawable.widget_4x_blue, @@ -149,12 +185,17 @@ public class ResourceParser { R.drawable.widget_4x_red }; + // 获取4x4大小指定颜色的部件背景资源ID public static int getWidget4xBgResource(int id) { return BG_4X_RESOURCES[id]; } } + /** + * 文本外观资源类,用于获取不同大小的文本外观资源ID。 + */ public static class TextAppearanceResources { + // 定义不同大小的文本外观资源ID数组 private final static int [] TEXTAPPEARANCE_RESOURCES = new int [] { R.style.TextAppearanceNormal, R.style.TextAppearanceMedium, @@ -162,6 +203,7 @@ public class ResourceParser { R.style.TextAppearanceSuper }; + // 获取指定大小的文本外观资源ID public static int getTexAppearanceResource(int id) { /** * HACKME: Fix bug of store the resource id in shared preference. @@ -174,8 +216,9 @@ public class ResourceParser { return TEXTAPPEARANCE_RESOURCES[id]; } + // 获取文本外观资源数组的大小 public static int getResourcesSize() { return TEXTAPPEARANCE_RESOURCES.length; } } -} +} \ No newline at end of file