diff --git a/doc/小米便签精读报告.docx b/doc/小米便签精读报告.docx index de597a1..e05ddc0 100644 Binary files a/doc/小米便签精读报告.docx and b/doc/小米便签精读报告.docx differ diff --git a/src/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java b/src/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java index a674d7a..439955c 100644 --- a/src/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java +++ b/src/app/src/main/java/net/micode/notes/ui/NoteEditActivity.java @@ -564,21 +564,36 @@ public class NoteEditActivity extends Activity implements OnClickListener, } /** - * Share note to apps that support {@link Intent#ACTION_SEND} action - * and {@text/plain} type + * 将指定的文本信息分享到支持 {@link Intent#ACTION_SEND} 操作和 {@text/plain} 类型的应用程序。 + * + * @param context 上下文对象,用于启动新的 Activity。 + * @param info 要分享的文本信息,将被作为 {@link Intent#EXTRA_TEXT} 传递给目标应用程序。 */ private void sendTo(Context context, String info) { + // 创建一个新的 Intent,设置操作为 ACTION_SEND,用于分享文本信息 Intent intent = new Intent(Intent.ACTION_SEND); + + // 将要分享的文本信息作为 EXTRA_TEXT 添加到 Intent 中 intent.putExtra(Intent.EXTRA_TEXT, info); + + // 设置 Intent 的 MIME 类型为 text/plain,表示分享的是纯文本 intent.setType("text/plain"); + + // 启动目标应用程序的 Activity,分享文本信息 context.startActivity(intent); } + + /** + * 创建一个新的笔记。 + * 该函数首先保存当前正在编辑的笔记,然后启动一个新的NoteEditActivity以安全地创建新笔记。 + * 通过调用finish()结束当前Activity,并传递必要的Intent参数来启动新的NoteEditActivity。 + */ private void createNewNote() { - // Firstly, save current editing notes + // 首先保存当前正在编辑的笔记,确保数据不会丢失 saveNote(); - // For safety, start a new NoteEditActivity + // 结束当前Activity,并启动一个新的NoteEditActivity以创建新笔记 finish(); Intent intent = new Intent(this, NoteEditActivity.class); intent.setAction(Intent.ACTION_INSERT_OR_EDIT); @@ -586,25 +601,44 @@ public class NoteEditActivity extends Activity implements OnClickListener, startActivity(intent); } + /** + * 删除当前正在处理的笔记。如果笔记存在于数据库中,则根据同步模式决定是直接删除还是移动到回收站。 + * 如果笔记不存在于数据库中,则仅标记为已删除状态。 + * + * 该函数首先检查笔记是否存在于数据库中。如果存在,则根据笔记ID决定是否将其添加到待删除的ID集合中。 + * 如果当前不在同步模式,则直接批量删除这些笔记;如果在同步模式,则将笔记移动到回收站。 + * 最后,无论笔记是否存在于数据库中,都会将其标记为已删除状态。 + */ private void deleteCurrentNote() { + // 检查当前笔记是否存在于数据库中 if (mWorkingNote.existInDatabase()) { + // 创建一个HashSet来存储待删除的笔记ID HashSet ids = new HashSet(); long id = mWorkingNote.getNoteId(); + + // 如果笔记ID不是根文件夹的ID,则将其添加到待删除的ID集合中 if (id != Notes.ID_ROOT_FOLDER) { ids.add(id); } else { + // 如果笔记ID是根文件夹的ID,则记录错误日志 Log.d(TAG, "Wrong note id, should not happen"); } + + // 根据是否处于同步模式决定是直接删除还是移动到回收站 if (!isSyncMode()) { + // 如果不在同步模式,则尝试批量删除笔记 if (!DataUtils.batchDeleteNotes(getContentResolver(), ids)) { Log.e(TAG, "Delete Note error"); } } else { + // 如果在同步模式,则尝试将笔记移动到回收站 if (!DataUtils.batchMoveToFolder(getContentResolver(), ids, Notes.ID_TRASH_FOLER)) { Log.e(TAG, "Move notes to trash folder error, should not happens"); } } } + + // 无论笔记是否存在于数据库中,都将其标记为已删除状态 mWorkingNote.markDeleted(true); } @@ -612,14 +646,27 @@ public class NoteEditActivity extends Activity implements OnClickListener, return NotesPreferenceActivity.getSyncAccountName(this).trim().length() > 0; } + /** + * 处理闹钟提醒的更改事件。 + * 根据传入的日期和设置状态,为当前笔记设置或取消闹钟提醒。 + * 如果笔记尚未保存到数据库,则先保存笔记。 + * + * @param date 闹钟提醒的日期和时间,以毫秒为单位的时间戳 + * @param set 是否设置闹钟提醒,true 表示设置,false 表示取消 + */ public void onClockAlertChanged(long date, boolean set) { /** - * User could set clock to an unsaved note, so before setting the - * alert clock, we should save the note first + * 如果当前笔记尚未保存到数据库,则先保存笔记。 + * 因为用户可能会为未保存的笔记设置闹钟提醒,所以需要确保笔记已保存。 */ if (!mWorkingNote.existInDatabase()) { saveNote(); } + + /** + * 如果笔记已保存(即笔记ID大于0),则根据设置状态设置或取消闹钟提醒。 + * 使用 AlarmManager 来管理闹钟,并通过 PendingIntent 触发 AlarmReceiver。 + */ if (mWorkingNote.getNoteId() > 0) { Intent intent = new Intent(this, AlarmReceiver.class); intent.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mWorkingNote.getNoteId())); @@ -633,9 +680,8 @@ public class NoteEditActivity extends Activity implements OnClickListener, } } else { /** - * There is the condition that user has input nothing (the note is - * not worthy saving), we have no note id, remind the user that he - * should input something + * 如果笔记尚未保存且用户未输入任何内容(笔记不值得保存), + * 则提醒用户需要输入内容才能设置闹钟提醒。 */ Log.e(TAG, "Clock alert setting error"); showToast(R.string.error_note_empty_for_clock); @@ -646,18 +692,30 @@ public class NoteEditActivity extends Activity implements OnClickListener, updateWidget(); } + /** + * 处理编辑文本删除操作。 + * 当删除某个编辑框时,调整后续编辑框的索引,并将删除的文本内容追加到前一个或后一个编辑框中。 + * + * @param index 被删除的编辑框的索引 + * @param text 被删除的文本内容 + */ public void onEditTextDelete(int index, String text) { int childCount = mEditTextList.getChildCount(); + // 如果只有一个编辑框,直接返回,不允许删除 if (childCount == 1) { return; } + // 调整后续编辑框的索引 for (int i = index + 1; i < childCount; i++) { ((NoteEditText) mEditTextList.getChildAt(i).findViewById(R.id.et_edit_text)) .setIndex(i - 1); } + // 移除指定索引的编辑框 mEditTextList.removeViewAt(index); + + // 获取前一个或后一个编辑框,并将删除的文本内容追加到其中 NoteEditText edit = null; if(index == 0) { edit = (NoteEditText) mEditTextList.getChildAt(0).findViewById( diff --git a/src/app/src/main/java/net/micode/notes/ui/NoteItemData.java b/src/app/src/main/java/net/micode/notes/ui/NoteItemData.java index 0f5a878..33c3ae6 100644 --- a/src/app/src/main/java/net/micode/notes/ui/NoteItemData.java +++ b/src/app/src/main/java/net/micode/notes/ui/NoteItemData.java @@ -76,39 +76,76 @@ public class NoteItemData { private boolean mIsOneNoteFollowingFolder; private boolean mIsMultiNotesFollowingFolder; - public NoteItemData(Context context, Cursor cursor) { - mId = cursor.getLong(ID_COLUMN); - mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); - mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); - mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN); - mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0) ? true : false; - mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN); - mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN); - mParentId = cursor.getLong(PARENT_ID_COLUMN); - mSnippet = cursor.getString(SNIPPET_COLUMN); - mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace( - NoteEditActivity.TAG_UNCHECKED, ""); - mType = cursor.getInt(TYPE_COLUMN); - mWidgetId = cursor.getInt(WIDGET_ID_COLUMN); - mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN); - - mPhoneNumber = ""; - if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { - mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); - if (!TextUtils.isEmpty(mPhoneNumber)) { - mName = Contact.getContact(context, mPhoneNumber); - if (mName == null) { - mName = mPhoneNumber; - } + /** + * NoteItemData 的构造函数,用于从数据库 Cursor 中初始化 NoteItemData 对象。 + * + * @param context 上下文对象,用于获取内容解析器和联系人信息。 + * @param cursor 数据库查询结果的 Cursor,包含笔记的相关信息。 + */ +public NoteItemData(Context context, Cursor cursor) { + // 从 Cursor 中提取笔记的 ID + mId = cursor.getLong(ID_COLUMN); + + // 从 Cursor 中提取笔记的提醒日期 + mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); + + // 从 Cursor 中提取笔记的背景颜色 ID + mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); + + // 从 Cursor 中提取笔记的创建日期 + mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN); + + // 从 Cursor 中提取笔记是否包含附件的信息,并转换为布尔值 + mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0) ? true : false; + + // 从 Cursor 中提取笔记的最后修改日期 + mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN); + + // 从 Cursor 中提取笔记下的子笔记数量 + mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN); + + // 从 Cursor 中提取笔记的父级 ID(即所属文件夹的 ID) + mParentId = cursor.getLong(PARENT_ID_COLUMN); + + // 从 Cursor 中提取笔记的摘要信息,并移除标记符号(TAG_CHECKED 和 TAG_UNCHECKED) + mSnippet = cursor.getString(SNIPPET_COLUMN); + mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace(NoteEditActivity.TAG_UNCHECKED, ""); + + // 从 Cursor 中提取笔记的类型(如普通笔记、文件夹等) + mType = cursor.getInt(TYPE_COLUMN); + + // 从 Cursor 中提取笔记的小部件 ID + mWidgetId = cursor.getInt(WIDGET_ID_COLUMN); + + // 从 Cursor 中提取笔记的小部件类型 + mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN); + + // 初始化电话号码为空字符串 + mPhoneNumber = ""; + + // 如果笔记属于通话记录文件夹,则获取其对应的电话号码 + if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { + mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); + if (!TextUtils.isEmpty(mPhoneNumber)) { + // 根据电话号码获取联系人姓名 + mName = Contact.getContact(context, mPhoneNumber); + if (mName == null) { + // 如果未找到联系人姓名,则使用电话号码作为名称 + mName = mPhoneNumber; } } + } - if (mName == null) { - mName = ""; - } - checkPostion(cursor); + // 如果 mName 仍为 null,则将其设置为空字符串 + if (mName == null) { + mName = ""; } + // 检查笔记在 Cursor 中的位置信息(如是否为第一个、最后一个等) + checkPostion(cursor); +} + + private void checkPostion(Cursor cursor) { mIsLastItem = cursor.isLast() ? true : false; mIsFirstItem = cursor.isFirst() ? true : false; @@ -158,61 +195,131 @@ public class NoteItemData { return mIsOnlyOneItem; } - public long getId() { - return mId; - } + /** + * 获取笔记的唯一标识符。 + * + * @return 笔记的 ID。 + */ +public long getId() { + return mId; +} - public long getAlertDate() { - return mAlertDate; - } +/** + * 获取笔记的提醒日期。 + * + * @return 提醒日期,如果未设置则返回 0。 + */ +public long getAlertDate() { + return mAlertDate; +} - public long getCreatedDate() { - return mCreatedDate; - } +/** + * 获取笔记的创建日期。 + * + * @return 创建日期的时间戳。 + */ +public long getCreatedDate() { + return mCreatedDate; +} - public boolean hasAttachment() { - return mHasAttachment; - } +/** + * 检查笔记是否包含附件。 + * + * @return 如果包含附件则返回 true,否则返回 false。 + */ +public boolean hasAttachment() { + return mHasAttachment; +} - public long getModifiedDate() { - return mModifiedDate; - } +/** + * 获取笔记的最后修改日期。 + * + * @return 最后修改日期的时间戳。 + */ +public long getModifiedDate() { + return mModifiedDate; +} - public int getBgColorId() { - return mBgColorId; - } +/** + * 获取笔记的背景颜色 ID。 + * + * @return 背景颜色 ID。 + */ +public int getBgColorId() { + return mBgColorId; +} - public long getParentId() { - return mParentId; - } +/** + * 获取笔记的父级 ID,即所属文件夹的 ID。 + * + * @return 父级 ID。 + */ +public long getParentId() { + return mParentId; +} - public int getNotesCount() { - return mNotesCount; - } +/** + * 获取笔记下的子笔记数量。 + * + * @return 子笔记数量。 + */ +public int getNotesCount() { + return mNotesCount; +} - public long getFolderId () { - return mParentId; - } +/** + * 获取笔记的文件夹 ID,等同于 getParentId() 方法。 + * + * @return 文件夹 ID。 + */ +public long getFolderId() { + return mParentId; +} - public int getType() { - return mType; - } +/** + * 获取笔记的类型。 + * + * @return 笔记类型,如普通笔记、文件夹等。 + */ +public int getType() { + return mType; +} - public int getWidgetType() { - return mWidgetType; - } +/** + * 获取笔记的小部件类型。 + * + * @return 小部件类型。 + */ +public int getWidgetType() { + return mWidgetType; +} - public int getWidgetId() { - return mWidgetId; - } +/** + * 获取笔记的小部件 ID。 + * + * @return 小部件 ID。 + */ +public int getWidgetId() { + return mWidgetId; +} - public String getSnippet() { - return mSnippet; - } +/** + * 获取笔记的摘要信息。 + * + * @return 笔记摘要。 + */ +public String getSnippet() { + return mSnippet; +} - public boolean hasAlert() { - return (mAlertDate > 0); - } +/** + * 检查笔记是否设置了提醒。 + * + * @return 如果设置了提醒则返回 true,否则返回 false。 + */ +public boolean hasAlert() { + return (mAlertDate > 0); +} public boolean isCallRecord() { return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));