diff --git a/src/xiaomi/Notes-master/src/net/micode/notes/tool/BackupUtils.java b/src/xiaomi/Notes-master/src/net/micode/notes/tool/BackupUtils.java index 39f6ec4..1028880 100644 --- a/src/xiaomi/Notes-master/src/net/micode/notes/tool/BackupUtils.java +++ b/src/xiaomi/Notes-master/src/net/micode/notes/tool/BackupUtils.java @@ -34,53 +34,90 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; - +/** + * @aPackage : net.micode.notes.tool + * @aClassName: BackupUtils + * @Description: + * 主要功能:备份工具类,用于数据备份读取、显示 + * 使用单例模式,确保在整个应用程序只有一个实例 + * @aAuthor: Li Qiushi + * @createdate: 12/23/2023 10:31 AM + */ public class BackupUtils { private static final String TAG = "BackupUtils"; // Singleton stuff private static BackupUtils sInstance; + /** + * 作用:用于获取BackupUtils类的实例 + * 使用synchronized关键字确保在多线程环境下的安全性 + * @param context + * @return sInstance + */ public static synchronized BackupUtils getInstance(Context context) { if (sInstance == null) { - sInstance = new BackupUtils(context); + sInstance = new BackupUtils(context);//创建一个新的BackupUtils实例并将其赋值给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; + /** + * 作用:接收一个Context参数,并使用该参数创建一个TextExport实例 + * @param context + */ private BackupUtils(Context context) { mTextExport = new TextExport(context); } + /** + * 作用: 用于检查外部存储是否可用 + * @return boolean + */ private static boolean externalStorageAvailable() { return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } + /** + * 作用:用于将数据导出到文本文件。 + * @return int + */ public int exportToText() { return mTextExport.exportToText(); } + /** + * 作用:用于获取导出的文本文件名 + * @return String + */ public String getExportedTextFileName() { return mTextExport.mFileName; } + /** + * 作用:用于获取导出的文本文件目录 + * @return String + */ public String getExportedTextFileDir() { return mTextExport.mFileDirectory; } @@ -125,6 +162,10 @@ public class BackupUtils { private String mFileName; private String mFileDirectory; + /** + * 作用:接收一个Context对象作为参数,并初始化一些成员变量,如TEXT_FORMAT、mContext、mFileName和mFileDirectory + * @param context + */ public TextExport(Context context) { TEXT_FORMAT = context.getResources().getStringArray(R.array.format_for_exported_note); mContext = context; @@ -132,12 +173,19 @@ public class BackupUtils { mFileDirectory = ""; } + /** + * 作用:根据传入的id返回对应的文本格式 + * @param id + * @return String + */ private String getFormat(int id) { return TEXT_FORMAT[id]; } /** - * Export the folder identified by folder id to text + * 作用:用于将指定文件夹下的所有笔记导出为文本文件 + * @param folderId + * @param ps */ private void exportFolderToText(String folderId, PrintStream ps) { // Query notes belong to this folder @@ -146,6 +194,9 @@ public class BackupUtils { folderId }, null); + /* + 查询该文件夹下的所有笔记,然后遍历每个笔记,打印其最后修改日期和内容 + */ if (notesCursor != null) { if (notesCursor.moveToFirst()) { do { @@ -155,7 +206,7 @@ public class BackupUtils { notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); // Query data belong to this note String noteId = notesCursor.getString(NOTE_COLUMN_ID); - exportNoteToText(noteId, ps); + exportNoteToText(noteId, ps);//将笔记的内容导出为文本 } while (notesCursor.moveToNext()); } notesCursor.close(); @@ -163,9 +214,12 @@ public class BackupUtils { } /** - * Export note identified by id to a print stream + * 作用:将指定ID的笔记导出到文本文件中,包括电话记录和普通笔记的内容 + * @param noteId + * @param ps */ private void exportNoteToText(String noteId, PrintStream ps) { + //查询与给定noteId相关的数据,并将结果存储在Cursor对象dataCursor中 Cursor dataCursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { noteId @@ -175,6 +229,9 @@ public class BackupUtils { if (dataCursor.moveToFirst()) { do { String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); + /* + 表示该记录是一个电话记录,此时,获取电话记录中的电话号码、通话日期和位置信息,并将其格式化后输出到PrintStream对象ps中 + */ if (DataConstants.CALL_NOTE.equals(mimeType)) { // Print phone number String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); @@ -194,7 +251,7 @@ public class BackupUtils { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), location)); } - } else if (DataConstants.NOTE.equals(mimeType)) { + } else if (DataConstants.NOTE.equals(mimeType)) {//表示该记录是一个普通笔记,此时,获取笔记的内容,并将其格式化后输出到PrintStream对象ps中 String content = dataCursor.getString(DATA_COLUMN_CONTENT); if (!TextUtils.isEmpty(content)) { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), @@ -205,7 +262,7 @@ public class BackupUtils { } dataCursor.close(); } - // print a line separator between note + // 向PrintStream对象ps中写入一个换行符,用于分隔不同笔记之间的内容 try { ps.write(new byte[] { Character.LINE_SEPARATOR, Character.LETTER_NUMBER @@ -216,14 +273,21 @@ public class BackupUtils { } /** - * Note will be exported as text which is user readable + * 作用:将笔记导出到文本文件中 + * @return int */ public int exportToText() { + /* + 如果外部存储未挂载,则记录一条日志并返回一个表示外部存储未挂载的状态码(STATE_SD_CARD_UNMOUONTED) + */ if (!externalStorageAvailable()) { Log.d(TAG, "Media was not mounted"); return STATE_SD_CARD_UNMOUONTED; } - + /* + 获取一个用于输出到文本文件的打印流(PrintStream),并将其赋值给变量ps + 如果获取打印流失败,则记录一条错误日志并返回一个表示系统错误的状态码(STATE_SYSTEM_ERROR) + */ PrintStream ps = getExportToTextPrintStream(); if (ps == null) { Log.e(TAG, "get print stream error"); @@ -237,7 +301,9 @@ 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()) { do { // Print folder's name @@ -247,6 +313,7 @@ public class BackupUtils { } else { folderName = folderCursor.getString(NOTE_COLUMN_SNIPPET); } + //将格式化后的文件夹名称写入打印流ps if (!TextUtils.isEmpty(folderName)) { ps.println(String.format(getFormat(FORMAT_FOLDER_NAME), folderName)); } @@ -264,6 +331,10 @@ public class BackupUtils { NoteColumns.TYPE + "=" + +Notes.TYPE_NOTE + " AND " + NoteColumns.PARENT_ID + "=0", null, null); + /*如果noteCursor不为空,则遍历查询结果 + 将格式化后的笔记修改日期写入打印流ps + 获取笔记的ID,并调用exportNoteToText方法将笔记的内容导出到文本文件中 + */ if (noteCursor != null) { if (noteCursor.moveToFirst()) { do { @@ -283,16 +354,21 @@ public class BackupUtils { } /** - * Get a print stream pointed to the file {@generateExportedTextFile} + * 作用:建一个用于导出文本的PrintStream对象 + * @return PrintStream */ private PrintStream getExportToTextPrintStream() { + //生成一个文件,该文件位于SD卡上 File file = generateFileMountedOnSDcard(mContext, R.string.file_path, R.string.file_name_txt_format); + /* + 表示创建文件失败,此时会记录一条错误日志(Log.e(TAG, "create file to exported failed")) + */ if (file == null) { Log.e(TAG, "create file to exported failed"); return null; } - mFileName = file.getName(); + mFileName = file.getName();//文件名赋值给成员变量mFileName mFileDirectory = mContext.getString(R.string.file_path); PrintStream ps = null; try { @@ -310,19 +386,27 @@ public class BackupUtils { } /** - * Generate the text file to store imported data + * 作用:用于在Android设备上生成一个文件 + * @param context + * @param filePathResId + * @param fileNameFormatResId + * @return File */ private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { StringBuilder sb = new StringBuilder(); - sb.append(Environment.getExternalStorageDirectory()); - sb.append(context.getString(filePathResId)); + sb.append(Environment.getExternalStorageDirectory()); //获取外部存储设备的根目录 + sb.append(context.getString(filePathResId)); //根据传入的filePathResId参数,将指定的路径添加到根目录下 File filedir = new File(sb.toString()); + //根据传入的fileNameFormatResId参数和当前日期时间,生成文件名,并将其添加到目录路径中 sb.append(context.getString( fileNameFormatResId, DateFormat.format(context.getString(R.string.format_date_ymd), System.currentTimeMillis()))); File file = new File(sb.toString()); - + /* + 尝试创建文件所在的目录(如果不存在),并创建文件本身(如果不存在) + 如果成功创建了文件,返回该文件的引用;否则,返回null + */ try { if (!filedir.exists()) { filedir.mkdir(); @@ -338,7 +422,7 @@ public class BackupUtils { } return null; + } } -} diff --git a/src/xiaomi/Notes-master/src/net/micode/notes/tool/DataUtils.java b/src/xiaomi/Notes-master/src/net/micode/notes/tool/DataUtils.java index 2a14982..4065280 100644 --- a/src/xiaomi/Notes-master/src/net/micode/notes/tool/DataUtils.java +++ b/src/xiaomi/Notes-master/src/net/micode/notes/tool/DataUtils.java @@ -33,21 +33,38 @@ import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute; import java.util.ArrayList; import java.util.HashSet; - +/** + * @aPackage : net.micode.notes.tool + * @aClassName: DataUtils + * @Description: + * 主要功能:实现便签数据处理工具类,封装如查找、移动、删除数据等操作 + * @aAuthor: Li Qiushi + * @createdate: 12/30/2023 08:31 AM + */ public class DataUtils { public static final String TAG = "DataUtils"; + + /** + * 作用:用于批量删除笔记 + * @param resolver + * @param ids + * @return boolean + */ public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + //判断ids是否为空 if (ids == null) { Log.d(TAG, "the ids is null"); return true; } + //判断ids大小是否为0 if (ids.size() == 0) { Log.d(TAG, "no id is in the hashset"); return true; } - + //创建类存储删除的动作 ArrayList operationList = new ArrayList(); + //遍历ID,如果发现是根文件夹,则不删除 for (long id : ids) { if(id == Notes.ID_ROOT_FOLDER) { Log.e(TAG, "Don't delete system folder root"); @@ -57,9 +74,10 @@ public class DataUtils { .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) { + if (results.length == 0 || results[0] == null) { Log.d(TAG, "delete notes failed, ids:" + ids.toString()); return false; } @@ -72,14 +90,29 @@ public class DataUtils { return false; } + /** + * 作用:将一个笔记从一个文件夹移动到另一个文件夹 + * @param resolver + * @param id + * @param srcFolderId + * @param desFolderId + */ 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); + //对需要移动的便签进行数据更新,然后用update实现 resolver.update(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id), values, null, null); } + /** + * 作用:用于将一组笔记(由id集合表示)批量移动到指定的文件夹 + * @param resolver + * @param ids + * @param folderId + * @return boolean + */ public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { if (ids == null) { @@ -89,13 +122,15 @@ public class DataUtils { ArrayList operationList = new ArrayList(); for (long id : ids) { + //通过withAppendedId方法,为该Uri加上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()); - } + }//将ids里包含的每一列的数据逐次加入到operationList中,等待最后的批量处理 + //使用ContentResolver的applyBatch方法执行批量更新操作 try { ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); if (results == null || results.length == 0 || results[0] == null) { @@ -112,39 +147,50 @@ public class DataUtils { } /** - * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} + * 作用 :获取除系统文件夹之外的所有文件夹数量 + * @param resolver + * @return count */ public static int getUserFolderCount(ContentResolver resolver) { Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, new String[] { "COUNT(*)" }, NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>?", + //筛选条件:源文件不为trash folder,即垃圾文件 new String[] { String.valueOf(Notes.TYPE_FOLDER), String.valueOf(Notes.ID_TRASH_FOLER)}, null); int count = 0; - if(cursor != null) { - if(cursor.moveToFirst()) { - try { - count = cursor.getInt(0); - } catch (IndexOutOfBoundsException e) { - Log.e(TAG, "get folder count failed:" + e.toString()); - } finally { - cursor.close(); - } + if(cursor != null && cursor.moveToFirst()) { + try { + //获取文件夹的数量,并将其赋值给count变量 + count = cursor.getInt(0); + } catch (IndexOutOfBoundsException e) { + Log.e(TAG, "get folder count failed:" + e.toString()); + } finally { + cursor.close(); } } return count; } + /** + * 作用:用于检查一个笔记是否在Note数据库中可见 + * @param resolver + * @param noteId + * @param type + * @return boolean + */ public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, + //查询条件:type符合,且不属于垃圾文件夹 NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, new String [] {String.valueOf(type)}, null); boolean exist = false; if (cursor != null) { + //用getCount函数判断cursor是否为空 if (cursor.getCount() > 0) { exist = true; } @@ -153,6 +199,12 @@ public class DataUtils { return exist; } + /** + * 作用:用于检查一个笔记是否存在于Note数据库中 + * @param resolver + * @param noteId + * @return boolean + */ public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, null, null, null); @@ -167,6 +219,12 @@ public class DataUtils { return exist; } + /** + * 作用:用于检查给定的数据ID是否存在于数据数据库中(检查方法同上) + * @param resolver + * @param dataId + * @return boolean + */ public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null, null, null, null); @@ -181,6 +239,12 @@ public class DataUtils { return exist; } + /** + * 作用:用于检查给定的文件夹名称是否在Notes数据库中可见 + * @param resolver + * @param name + * @return boolean + */ public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, NoteColumns.TYPE + "=" + Notes.TYPE_FOLDER + @@ -188,6 +252,7 @@ public class DataUtils { " AND " + NoteColumns.SNIPPET + "=?", new String[] { name }, null); boolean exist = false; + //通过名字查询文件是否存在 if(cursor != null) { if(cursor.getCount() > 0) { exist = true; @@ -197,7 +262,14 @@ public class DataUtils { return exist; } + /** + * 作用:用于从Notes数据库中获取指定文件夹ID下的所有小部件属性 + * @param resolver + * @param folderId + * @return HashSet + */ public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { + //查询条件:父ID是传入的folderId Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, NoteColumns.PARENT_ID + "=?", @@ -211,6 +283,10 @@ public class DataUtils { do { try { AppWidgetAttribute widget = new AppWidgetAttribute(); + /* + 0对应NoteColumns.WIDGET_ID + 1行应的NoteColumns.WIDGET_TYPE + */ widget.widgetId = c.getInt(0); widget.widgetType = c.getInt(1); set.add(widget); @@ -224,6 +300,12 @@ public class DataUtils { return set; } + /** + * 作用:用于根据笔记ID获取通话记录中的电话号码 + * @param resolver + * @param noteId + * @return String + */ public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.PHONE_NUMBER }, @@ -243,6 +325,13 @@ public class DataUtils { return ""; } + /** + * 作用:用于根据电话号码和通话日期从Android的ContentResolver中查询Notes数据库中的记录 + * @param resolver + * @param phoneNumber + * @param callDate + * @return long + */ public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.NOTE_ID }, @@ -264,7 +353,14 @@ public class DataUtils { return 0; } + /** + * 作用:根据给定的noteId从ContentResolver中获取对应的笔记片段(snippet) + * @param resolver + * @param noteId + * @return String + */ public static String getSnippetById(ContentResolver resolver, long noteId) { + //查询条件:noteId Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, new String [] { NoteColumns.SNIPPET }, NoteColumns.ID + "=?", @@ -282,6 +378,11 @@ public class DataUtils { throw new IllegalArgumentException("Note is not found with id: " + noteId); } + /** + * 作用:对输入的字符串进行处理,返回处理后的字符串 + * @param snippet + * @return String + */ public static String getFormattedSnippet(String snippet) { if (snippet != null) { snippet = snippet.trim(); diff --git a/src/xiaomi/Notes-master/src/net/micode/notes/tool/GTaskStringUtils.java b/src/xiaomi/Notes-master/src/net/micode/notes/tool/GTaskStringUtils.java index 666b729..85480ee 100644 --- a/src/xiaomi/Notes-master/src/net/micode/notes/tool/GTaskStringUtils.java +++ b/src/xiaomi/Notes-master/src/net/micode/notes/tool/GTaskStringUtils.java @@ -15,9 +15,19 @@ */ package net.micode.notes.tool; +/** + * @aPackage : net.micode.notes.tool + * @aClassName: GTaskStringUtils + * @Description: + * 主要功能:同步中使用的字符串工具类,为jsonObject提供string对象 + * 定义了很多的静态字符串,目的就是为了提供jsonObject中相应字符串的"key"。把这些静态的定义单独写到了一个类里面,方便查看管理 + * @aAuthor: Li Qiushi + * @createdate: 12/30/2023 10:31 AM + */ public class GTaskStringUtils { + public final static String GTASK_JSON_ACTION_ID = "action_id"; public final static String GTASK_JSON_ACTION_LIST = "action_list"; diff --git a/src/xiaomi/Notes-master/src/net/micode/notes/tool/ResourceParser.java b/src/xiaomi/Notes-master/src/net/micode/notes/tool/ResourceParser.java index 1ad3ad6..552bb89 100644 --- a/src/xiaomi/Notes-master/src/net/micode/notes/tool/ResourceParser.java +++ b/src/xiaomi/Notes-master/src/net/micode/notes/tool/ResourceParser.java @@ -21,7 +21,22 @@ import android.preference.PreferenceManager; import net.micode.notes.R; import net.micode.notes.ui.NotesPreferenceActivity; - +/** + * @aPackage : net.micode.notes.tool + * @aClassName: ResourceParser + * @Description: + * 主要功能:界面元素的解析工具类,利用R.java这个类来获取资源供程序调用 + * 字面意义是资源分析器,实际上就是获取资源并且在程序中使用,比如颜色图片等 + * 实现方法:实现方法:主要利用R.java这个类,其中包括 + * R.id 组件资源引用 + * R.drawable 图片资源 (被使用) + * R.layout 布局资源 + * R.menu 菜单资源 + * R.String 文字资源 + * R.style 主题资源 (被使用) + * @aAuthor: Li Qiushi + * @createdate: 12/30/2023 10:31 AM + */ public class ResourceParser { public static final int YELLOW = 0; @@ -39,6 +54,9 @@ public class ResourceParser { 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,