diff --git a/doc/PPT_8.pptx b/doc/PPT_8.pptx new file mode 100644 index 0000000..98e0989 Binary files /dev/null and b/doc/PPT_8.pptx differ diff --git a/doc/实践模板-开源软件泛读、标注和维护报告文档 .docx b/doc/实践模板-开源软件泛读、标注和维护报告文档 .docx new file mode 100644 index 0000000..82e4803 Binary files /dev/null and b/doc/实践模板-开源软件泛读、标注和维护报告文档 .docx differ diff --git a/doc/表格.xlsx b/doc/表格.xlsx new file mode 100644 index 0000000..8ad654d Binary files /dev/null and b/doc/表格.xlsx differ diff --git a/src/net/micode/notes/model/Note.java b/src/net/micode/notes/model/Note.java index 6706cf6..26caf20 100644 --- a/src/net/micode/notes/model/Note.java +++ b/src/net/micode/notes/model/Note.java @@ -42,11 +42,11 @@ public class Note { * Create a new note id for adding a new note to databases */ public static synchronized long getNewNoteId(Context context, long folderId) { - // Create a new note in the database + //为给定文件夹生成新的笔记ID ContentValues values = new ContentValues(); long createdTime = System.currentTimeMillis(); - values.put(NoteColumns.CREATED_DATE, createdTime); - values.put(NoteColumns.MODIFIED_DATE, createdTime); + values.put(NoteColumns.CREATED_DATE, createdTime);//创建日期 + values.put(NoteColumns.MODIFIED_DATE, createdTime);//修改日期 values.put(NoteColumns.TYPE, Notes.TYPE_NOTE); values.put(NoteColumns.LOCAL_MODIFIED, 1); values.put(NoteColumns.PARENT_ID, folderId); @@ -71,6 +71,7 @@ public class Note { } public void setNoteValue(String key, String value) { + //此方法设置笔记的值,标记为本地修改 mNoteDiffValues.put(key, value); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); @@ -79,11 +80,12 @@ public class Note { public void setTextData(String key, String value) { mNoteData.setTextData(key, value); } + //为笔记设置文本数据。 public void setTextDataId(long id) { mNoteData.setTextDataId(id); } - + //setTextDataId、getTextDataId、setCallDataId、setCallData ,这些方法处理与笔记相关的文本和通话信息的ID和数据的设置和获取。 public long getTextDataId() { return mNoteData.mTextDataId; } @@ -99,7 +101,7 @@ public class Note { public boolean isLocalModified() { return mNoteDiffValues.size() > 0 || mNoteData.isLocalModified(); } - + //根据值和数据的差异检查笔记是否具有本地修改。 public boolean syncNote(Context context, long noteId) { if (noteId <= 0) { throw new IllegalArgumentException("Wrong note id:" + noteId); @@ -108,6 +110,8 @@ public class Note { if (!isLocalModified()) { return true; } + //尝试将笔记与提供的上下文和笔记ID同步。 + //如果笔记ID小于或等于0,或者没有本地修改,则返回true。 /** * In theory, once data changed, the note should be updated on {@link NoteColumns#LOCAL_MODIFIED} and @@ -147,17 +151,19 @@ public class Note { mTextDataId = 0; mCallDataId = 0; } - + //初始化 mTextDataValues 和 mCallDataValues 为 ContentValues 对象,用于存储文本数据和通话数据。 + //初始化 mTextDataId 和 mCallDataId 为 0。 boolean isLocalModified() { return mTextDataValues.size() > 0 || mCallDataValues.size() > 0; } - + //检查文本或通话数据是否有本地修改 void setTextDataId(long id) { if(id <= 0) { throw new IllegalArgumentException("Text data id should larger than 0"); } mTextDataId = id; } + //设置文本数据的ID,如果提供的ID小于等于0,报告异常 void setCallDataId(long id) { if (id <= 0) { @@ -165,52 +171,57 @@ public class Note { } mCallDataId = id; } - + //设置通话数据的键值对,并标记笔记为本地修改,更新修改日期 void setCallData(String key, String value) { mCallDataValues.put(key, value); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); } - + //设置文本数据的键值对,并标记笔记为本地修改,更新修改日期 void setTextData(String key, String value) { mTextDataValues.put(key, value); mNoteDiffValues.put(NoteColumns.LOCAL_MODIFIED, 1); mNoteDiffValues.put(NoteColumns.MODIFIED_DATE, System.currentTimeMillis()); } - + //将数据推送到ContentResolver的方法 Uri pushIntoContentResolver(Context context, long noteId) { - /** - * Check for safety - */ + //检查noteID的有效性,如果小于等于0,抛出IllegalArgumentException if (noteId <= 0) { throw new IllegalArgumentException("Wrong note id:" + noteId); } - + //用于保存ContentProvider操作列表 ArrayList operationList = new ArrayList(); ContentProviderOperation.Builder builder = null; - + //处理文本数据的操作 if(mTextDataValues.size() > 0) { + //设置文本数据的关联noteID mTextDataValues.put(DataColumns.NOTE_ID, noteId); + // 如果mTextDataId为0,表示需要插入新的文本数据 if (mTextDataId == 0) { mTextDataValues.put(DataColumns.MIME_TYPE, TextNote.CONTENT_ITEM_TYPE); + //插入数据并获取新的Uri Uri uri = context.getContentResolver().insert(Notes.CONTENT_DATA_URI, mTextDataValues); try { + // 解析Uri中的数据,获取新插入数据的id,并设置给mTextDataId setTextDataId(Long.valueOf(uri.getPathSegments().get(1))); } catch (NumberFormatException e) { + // 插入数据失败,记录错误日志并清空数据 Log.e(TAG, "Insert new text data fail with noteId" + noteId); mTextDataValues.clear(); return null; } } else { + // mTextDataId不为0,表示需要更新已存在的文本数据 builder = ContentProviderOperation.newUpdate(ContentUris.withAppendedId( Notes.CONTENT_DATA_URI, mTextDataId)); builder.withValues(mTextDataValues); operationList.add(builder.build()); } + // 清空文本数据,以便下一次使用 mTextDataValues.clear(); } - + // 处理通话数据的操作,逻辑类似于文本数据 if(mCallDataValues.size() > 0) { mCallDataValues.put(DataColumns.NOTE_ID, noteId); if (mCallDataId == 0) { @@ -232,17 +243,20 @@ public class Note { } mCallDataValues.clear(); } - + // 如果有待执行的操作,将它们应用到ContentResolver if (operationList.size() > 0) { try { ContentProviderResult[] results = context.getContentResolver().applyBatch( Notes.AUTHORITY, operationList); + // 返回插入或更新后的笔记的Uri,如果操作失败则返回null return (results == null || results.length == 0 || results[0] == null) ? null : ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId); } catch (RemoteException e) { + // 捕获远程异常并记录错误日志 Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); return null; } catch (OperationApplicationException e) { + // 捕获操作应用异常并记录错误日志 Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); return null; } @@ -251,3 +265,5 @@ public class Note { } } } + + diff --git a/src/net/micode/notes/model/WorkingNote.java b/src/net/micode/notes/model/WorkingNote.java index be081e4..dc807f7 100644 --- a/src/net/micode/notes/model/WorkingNote.java +++ b/src/net/micode/notes/model/WorkingNote.java @@ -128,9 +128,11 @@ public class WorkingNote { Cursor cursor = mContext.getContentResolver().query( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null, null, null); - + // 检查查询结果是否为空 if (cursor != null) { + // 移动到查询结果的第一行(如果有多行数据) if (cursor.moveToFirst()) { + // 从查询结果中获取笔记的相关信息并存储到相应的成员变量中 mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN); mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN); mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN); @@ -138,37 +140,49 @@ public class WorkingNote { mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN); mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN); } + // 关闭查询结果的游标 cursor.close(); } else { + // 如果查询结果为空,则输出错误日志并抛出异常 Log.e(TAG, "No note with id:" + mNoteId); throw new IllegalArgumentException("Unable to find note with id " + mNoteId); } + // 调用loadNoteData()方法加载笔记的其他数据 loadNoteData(); } private void loadNoteData() { + // 通过内容解析器(ContentResolver)查询笔记的数据信息 Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { String.valueOf(mNoteId) }, null); - + // 检查查询结果是否为空 if (cursor != null) { + // 移动到查询结果的第一行(如果有多行数据) if (cursor.moveToFirst()) { do { + // 获取数据类型 String type = cursor.getString(DATA_MIME_TYPE_COLUMN); + // 根据数据类型进行不同的处理 if (DataConstants.NOTE.equals(type)) { + // 如果是笔记类型的数据,获取内容和模式,并设置到相应的成员变量中 mContent = cursor.getString(DATA_CONTENT_COLUMN); mMode = cursor.getInt(DATA_MODE_COLUMN); mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN)); } else if (DataConstants.CALL_NOTE.equals(type)) { + // 如果是通话笔记类型的数据,设置通话数据ID到相应的成员变量中 mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); } else { + // 如果是未知类型的数据,输出调试信息 Log.d(TAG, "Wrong note type with type:" + type); } } while (cursor.moveToNext()); } + // 关闭查询结果的游标 cursor.close(); } else { + // 如果查询结果为空,则输出错误日志并抛出异常 Log.e(TAG, "No data with id:" + mNoteId); throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId); } @@ -188,27 +202,32 @@ public class WorkingNote { } public synchronized boolean saveNote() { + // 检查笔记是否值得保存 if (isWorthSaving()) { + // 检查笔记是否存在于数据库中 if (!existInDatabase()) { + // 如果笔记在数据库中不存在,尝试创建一个新的笔记ID if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { + // 如果创建新的笔记ID失败,输出错误日志并返回保存失败 Log.e(TAG, "Create new note fail with id:" + mNoteId); return false; } } - + // 同步笔记信息到数据库中 mNote.syncNote(mContext, mNoteId); /** * Update widget content if there exist any widget of this note */ + // 如果存在与该笔记相关的小部件,并且小部件类型有效,并且存在小部件设置状态监听器,则通知小部件内容已更改 if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { mNoteSettingStatusListener.onWidgetChanged(); } - return true; + return true;//保存成功 } else { - return false; + return false;//保存失败 } } @@ -230,38 +249,55 @@ public class WorkingNote { } public void setAlertDate(long date, boolean set) { + // 检查传入的日期是否与当前提醒日期不同 if (date != mAlertDate) { + // 如果不同,更新当前提醒日期为传入的日期,并通过 mNote 对象将更新后的提醒日期同步到数据库中 mAlertDate = date; mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); } + // 检查是否存在笔记设置状态监听器 if (mNoteSettingStatusListener != null) { + // 如果存在,调用监听器的 onClockAlertChanged 方法,通知提醒日期已更改,并传递新的日期和开关状态 mNoteSettingStatusListener.onClockAlertChanged(date, set); } } public void markDeleted(boolean mark) { + // 将笔记的删除状态更新为传入的标记值 mIsDeleted = mark; + // 检查是否存在与该笔记相关的小部件,并且小部件类型有效,并且存在笔记设置状态监听器 if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { + // 如果条件满足,调用监听器的 onWidgetChanged 方法,通知小部件内容已更改 mNoteSettingStatusListener.onWidgetChanged(); } } public void setBgColorId(int id) { + // 检查传入的颜色ID是否与当前背景颜色ID不同 if (id != mBgColorId) { + // 如果不同,更新当前背景颜色ID为传入的颜色ID mBgColorId = id; + // 检查是否存在笔记设置状态监听器 if (mNoteSettingStatusListener != null) { + // 如果存在,调用监听器的 onBackgroundColorChanged 方法,通知背景颜色已更改 mNoteSettingStatusListener.onBackgroundColorChanged(); } + // 通过 mNote 对象将更新后的背景颜色ID同步到数据库中 mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); } } public void setCheckListMode(int mode) { if (mMode != mode) { + // 如果不同,更新当前模式为传入的模式 + + // 检查是否存在笔记设置状态监听器 if (mNoteSettingStatusListener != null) { + // 如果存在,调用监听器的 onCheckListModeChanged 方法,通知检查清单模式已更改,并传递旧模式和新模式 mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); } + // 将当前模式更新为传入的模式,并通过 mNote 对象将更新后的模式同步到数据库中 mMode = mode; mNote.setTextData(TextNote.MODE, String.valueOf(mMode)); } @@ -275,15 +311,23 @@ public class WorkingNote { } public void setWidgetId(int id) { + // 检查传入的小部件类型是否与当前小部件类型不同 if (id != mWidgetId) { + // 如果不同,更新当前小部件类型为传入的小部件类型 mWidgetId = id; + + // 通过 mNote 对象将更新后的小部件类型同步到数据库中 mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); } } public void setWorkingText(String text) { + // 检查传入的文本内容是否与当前内容不同 if (!TextUtils.equals(mContent, text)) { + // 如果不同,更新当前内容为传入的文本内容 + mContent = text; + // 通过 mNote 对象将更新后的文本内容同步到数据库中 mNote.setTextData(DataColumns.CONTENT, mContent); } } diff --git a/src/net/micode/notes/tool/BackupUtils.java b/src/net/micode/notes/tool/BackupUtils.java index 39f6ec4..af53fa7 100644 --- a/src/net/micode/notes/tool/BackupUtils.java +++ b/src/net/micode/notes/tool/BackupUtils.java @@ -1,18 +1,3 @@ -/* - * Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * 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; @@ -38,7 +23,7 @@ import java.io.PrintStream; public class BackupUtils { private static final String TAG = "BackupUtils"; - // Singleton stuff + // 单例模式 private static BackupUtils sInstance; public static synchronized BackupUtils getInstance(Context context) { @@ -52,15 +37,15 @@ public class BackupUtils { * 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; @@ -137,10 +122,10 @@ public class BackupUtils { } /** - * 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 @@ -149,11 +134,11 @@ public class BackupUtils { if (notesCursor != null) { if (notesCursor.moveToFirst()) { do { - // Print note's last modified date + // 打印便签的最后修改日期 ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( mContext.getString(R.string.format_datetime_mdhm), notesCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); - // Query data belong to this note + // 查询属于此便签的数据 String noteId = notesCursor.getString(NOTE_COLUMN_ID); exportNoteToText(noteId, ps); } while (notesCursor.moveToNext()); @@ -163,7 +148,7 @@ public class BackupUtils { } /** - * 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, @@ -176,7 +161,7 @@ public class BackupUtils { do { String mimeType = dataCursor.getString(DATA_COLUMN_MIME_TYPE); if (DataConstants.CALL_NOTE.equals(mimeType)) { - // Print phone number + // 打印电话号码 String phoneNumber = dataCursor.getString(DATA_COLUMN_PHONE_NUMBER); long callDate = dataCursor.getLong(DATA_COLUMN_CALL_DATE); String location = dataCursor.getString(DATA_COLUMN_CONTENT); @@ -185,11 +170,11 @@ public class BackupUtils { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), phoneNumber)); } - // Print call date + // 打印通话日期 ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), DateFormat .format(mContext.getString(R.string.format_datetime_mdhm), callDate))); - // Print call attachment location + // 打印通话附件位置 if (!TextUtils.isEmpty(location)) { ps.println(String.format(getFormat(FORMAT_NOTE_CONTENT), location)); @@ -205,7 +190,7 @@ public class BackupUtils { } dataCursor.close(); } - // print a line separator between note + // 在便签之间打印一行分隔符 try { ps.write(new byte[] { Character.LINE_SEPARATOR, Character.LETTER_NUMBER @@ -240,7 +225,7 @@ public class BackupUtils { if (folderCursor != null) { if (folderCursor.moveToFirst()) { do { - // Print folder's name + // 打印文件夹的名称 String folderName = ""; if(folderCursor.getLong(NOTE_COLUMN_ID) == Notes.ID_CALL_RECORD_FOLDER) { folderName = mContext.getString(R.string.call_record_folder_name); @@ -270,7 +255,7 @@ public class BackupUtils { ps.println(String.format(getFormat(FORMAT_NOTE_DATE), DateFormat.format( mContext.getString(R.string.format_datetime_mdhm), noteCursor.getLong(NOTE_COLUMN_MODIFIED_DATE)))); - // Query data belong to this note + // 查询数据属于此 String noteId = noteCursor.getString(NOTE_COLUMN_ID); exportNoteToText(noteId, ps); } while (noteCursor.moveToNext()); @@ -283,7 +268,7 @@ public class BackupUtils { } /** - * Get a print stream pointed to the file {@generateExportedTextFile} + *获取指向文件的打印流 {@generateExportedTextFile} */ private PrintStream getExportToTextPrintStream() { File file = generateFileMountedOnSDcard(mContext, R.string.file_path, @@ -310,7 +295,7 @@ public class BackupUtils { } /** - * Generate the text file to store imported data + * 生成文本文件以存储导入的数据 */ private static File generateFileMountedOnSDcard(Context context, int filePathResId, int fileNameFormatResId) { StringBuilder sb = new StringBuilder(); diff --git a/src/net/micode/notes/tool/DataUtils.java b/src/net/micode/notes/tool/DataUtils.java index 2a14982..052fcb9 100644 --- a/src/net/micode/notes/tool/DataUtils.java +++ b/src/net/micode/notes/tool/DataUtils.java @@ -37,39 +37,49 @@ import java.util.HashSet; public class DataUtils { public static final String TAG = "DataUtils"; + + // 批量删除笔记的静态方法 public static boolean batchDeleteNotes(ContentResolver resolver, HashSet ids) { + // 检查传入的笔记ID集合是否为空 if (ids == null) { Log.d(TAG, "the ids is null"); - return true; + return true;// 如果为空,返回删除成功 } + // 检查传入的笔记ID集合是否为空 if (ids.size() == 0) { Log.d(TAG, "no id is in the hashset"); - return true; + return true;// 如果为空,返回删除成功 } - + // 创建一个操作列表,用于存储批量操作的删除操作 ArrayList operationList = new ArrayList(); + // 遍历传入的笔记ID集合,为每个ID创建一个删除操作,并添加到操作列表中 for (long id : ids) { + // 检查是否为系统文件夹的ID,如果是,跳过不删除 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()); } 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; + return false;// 如果为空,返回删除失败 } - return true; + 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;// 返回删除失败 } public static void moveNoteToFoler(ContentResolver resolver, long id, long srcFolderId, long desFolderId) { @@ -82,27 +92,34 @@ public class DataUtils { public static boolean batchMoveToFolder(ContentResolver resolver, HashSet ids, long folderId) { + // 检查传入的笔记ID集合是否为空 if (ids == null) { Log.d(TAG, "the ids is null"); - return true; + return true;// 如果为空,返回移动成功 } - + // 创建一个操作列表,用于存储批量操作的更新操作 ArrayList operationList = new ArrayList(); + // 遍历传入的笔记ID集合,为每个ID创建一个更新操作,并设置新的父文件夹ID和本地修改标志 for (long id : ids) { + // 创建更新操作的构建器 ContentProviderOperation.Builder builder = ContentProviderOperation .newUpdate(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, id)); + // 设置更新操作的值,包括新的父文件夹ID和本地修改标志 builder.withValue(NoteColumns.PARENT_ID, folderId); builder.withValue(NoteColumns.LOCAL_MODIFIED, 1); + // 将更新操作添加到操作列表中 operationList.add(builder.build()); } try { + // 应用批量操作,更新笔记的父文件夹ID和本地修改标志 ContentProviderResult[] results = resolver.applyBatch(Notes.AUTHORITY, operationList); + // 检查批量操作结果是否为空或第一个结果是否为空 if (results == null || results.length == 0 || results[0] == null) { Log.d(TAG, "delete notes failed, ids:" + ids.toString()); - return false; + return false;// 如果为空,返回移动失败 } - return true; + return true;// 返回移动成功 } catch (RemoteException e) { Log.e(TAG, String.format("%s: %s", e.toString(), e.getMessage())); } catch (OperationApplicationException e) { @@ -115,89 +132,108 @@ public class DataUtils { * Get the all folder count except system folders {@link Notes#TYPE_SYSTEM}} */ public static int getUserFolderCount(ContentResolver resolver) { + // 查询用户文件夹的数量 Cursor cursor =resolver.query(Notes.CONTENT_NOTE_URI, new String[] { "COUNT(*)" }, + // 查询条件:类型为文件夹且父文件夹ID不是回收站文件夹ID 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()) { try { + // 获取查询结果中的文件夹数量 count = cursor.getInt(0); } catch (IndexOutOfBoundsException e) { + // 捕获异常,输出错误日志 Log.e(TAG, "get folder count failed:" + e.toString()); } finally { + // 关闭游标 cursor.close(); } } } - return count; + return count;// 返回文件夹数量 } public static boolean visibleInNoteDatabase(ContentResolver resolver, long noteId, int type) { + // 查询指定笔记ID的可见性 Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, + // 查询条件:类型为指定类型且父文件夹ID不是回收站文件夹ID NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>" + Notes.ID_TRASH_FOLER, new String [] {String.valueOf(type)}, null); boolean exist = false; if (cursor != null) { + // 如果查询结果非空,检查结果数量是否大于0,表示存在 if (cursor.getCount() > 0) { exist = true; } + // 关闭游标 cursor.close(); } - return exist; + return exist;// 返回是否存在 } public static boolean existInNoteDatabase(ContentResolver resolver, long noteId) { + // 查询指定笔记ID是否存在于笔记数据库中 Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, noteId), null, null, null, null); boolean exist = false; if (cursor != null) { + // 如果查询结果非空,检查结果数量是否大于0,表示存在 if (cursor.getCount() > 0) { exist = true; } + // 关闭游标 cursor.close(); } - return exist; + return exist;// 返回是否存在 } public static boolean existInDataDatabase(ContentResolver resolver, long dataId) { + // 查询指定数据ID是否存在于数据数据库中 Cursor cursor = resolver.query(ContentUris.withAppendedId(Notes.CONTENT_DATA_URI, dataId), null, null, null, null); boolean exist = false; if (cursor != null) { + // 如果查询结果非空,检查结果数量是否大于0,表示存在 if (cursor.getCount() > 0) { exist = true; } cursor.close(); } - return exist; + return exist;// 返回是否存在 } public static boolean checkVisibleFolderName(ContentResolver resolver, String name) { + // 查询是否存在具有指定名称的可见文件夹 Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, null, + // 查询条件:类型为文件夹、父文件夹ID不是回收站文件夹ID、且摘要等于指定名称 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) { + // 如果查询结果非空,检查结果数量是否大于0,表示存在 if(cursor.getCount() > 0) { exist = true; } cursor.close(); } - return exist; + return exist; // 返回是否存在 } public static HashSet getFolderNoteWidget(ContentResolver resolver, long folderId) { + // 查询指定文件夹ID下的笔记小部件信息 Cursor c = resolver.query(Notes.CONTENT_NOTE_URI, new String[] { NoteColumns.WIDGET_ID, NoteColumns.WIDGET_TYPE }, NoteColumns.PARENT_ID + "=?", @@ -206,25 +242,30 @@ public class DataUtils { HashSet set = null; if (c != null) { + // 如果查询结果非空,移动到第一行 if (c.moveToFirst()) { set = new HashSet(); do { try { + // 创建 AppWidgetAttribute 对象,获取小部件ID和小部件类型,添加到集合中 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; + return set;// 返回包含小部件信息的集合 + } public static String getCallNumberByNoteId(ContentResolver resolver, long noteId) { + // 查询指定笔记ID对应的通话笔记的电话号码 Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.PHONE_NUMBER }, CallNote.NOTE_ID + "=? AND " + CallNote.MIME_TYPE + "=?", @@ -233,17 +274,20 @@ public class DataUtils { 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 ""; + return "";// 如果查询结果为空,返回空字符串 } public static long getNoteIdByPhoneNumberAndCallDate(ContentResolver resolver, String phoneNumber, long callDate) { + // 查询指定电话号码和通话日期对应的通话笔记的笔记ID Cursor cursor = resolver.query(Notes.CONTENT_DATA_URI, new String [] { CallNote.NOTE_ID }, CallNote.CALL_DATE + "=? AND " + CallNote.MIME_TYPE + "=? AND PHONE_NUMBERS_EQUAL(" @@ -252,19 +296,24 @@ public class DataUtils { null); if (cursor != null) { + // 如果查询结果非空,移动到第一行 if (cursor.moveToFirst()) { try { + // 获取查询结果中的笔记ID return cursor.getLong(0); } catch (IndexOutOfBoundsException e) { + // 捕获异常,输出错误日志 Log.e(TAG, "Get call note id fails " + e.toString()); } } cursor.close(); } - return 0; + return 0; // 如果查询结果为空,返回0 + } public static String getSnippetById(ContentResolver resolver, long noteId) { + // 查询指定笔记ID的摘要信息 Cursor cursor = resolver.query(Notes.CONTENT_NOTE_URI, new String [] { NoteColumns.SNIPPET }, NoteColumns.ID + "=?", @@ -272,17 +321,20 @@ public class DataUtils { null); if (cursor != null) { + // 如果查询结果非空,移动到第一行 String snippet = ""; if (cursor.moveToFirst()) { + // 获取查询结果中的摘要信息 snippet = cursor.getString(0); } cursor.close(); - return snippet; + 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'); @@ -290,6 +342,7 @@ public class DataUtils { snippet = snippet.substring(0, index); } } - return snippet; + return snippet;// 返回格式化后的摘要信息 + } } diff --git a/src/net/micode/notes/tool/ResourceParser.java b/src/net/micode/notes/tool/ResourceParser.java index 1ad3ad6..a6cc390 100644 --- a/src/net/micode/notes/tool/ResourceParser.java +++ b/src/net/micode/notes/tool/ResourceParser.java @@ -66,15 +66,19 @@ public class ResourceParser { } public static int getDefaultBgId(Context context) { + // 获取默认背景ID if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean( NotesPreferenceActivity.PREFERENCE_SET_BG_COLOR_KEY, false)) { + // 如果用户已设置背景颜色,则返回随机选择的编辑界面背景资源ID return (int) (Math.random() * NoteBgResources.BG_EDIT_RESOURCES.length); } else { + // 如果用户未设置背景颜色,则返回默认颜色的背景资源ID return BG_DEFAULT_COLOR; } } public static class NoteItemBgResources { + // 定义笔记列表项背景资源数组 private final static int [] BG_FIRST_RESOURCES = new int [] { R.drawable.list_yellow_up, R.drawable.list_blue_up,