From 93243974c091b371c9ac667dc5e1f8223bd5ee83 Mon Sep 17 00:00:00 2001 From: 111 <3035318107@qq.com> Date: Wed, 11 Jun 2025 14:00:40 +0800 Subject: [PATCH] model/WorkingNote.java --- .../net/micode/notes/model/WorkingNote.java | 210 +++++++++++++++--- 1 file changed, 177 insertions(+), 33 deletions(-) diff --git a/Notes-master/src/net/micode/notes/model/WorkingNote.java b/Notes-master/src/net/micode/notes/model/WorkingNote.java index be081e4..d5f58af 100644 --- a/Notes-master/src/net/micode/notes/model/WorkingNote.java +++ b/Notes-master/src/net/micode/notes/model/WorkingNote.java @@ -31,37 +31,41 @@ import net.micode.notes.data.Notes.NoteColumns; import net.micode.notes.data.Notes.TextNote; import net.micode.notes.tool.ResourceParser.NoteBgResources; - +/** + * 工作笔记类,用于管理笔记的创建、加载、保存和属性设置 + * 封装了笔记的核心功能和状态,包括内容、背景色、提醒时间等 + */ public class WorkingNote { - // Note for the working note + // 基础笔记对象,处理底层数据操作 private Note mNote; - // Note Id + // 笔记ID private long mNoteId; - // Note content + // 笔记内容 private String mContent; - // Note mode + // 笔记模式(如普通文本模式或待办事项模式) private int mMode; - + // 提醒日期 private long mAlertDate; - + // 修改日期 private long mModifiedDate; - + // 背景颜色ID private int mBgColorId; - + // 小部件ID private int mWidgetId; - + // 小部件类型 private int mWidgetType; - + // 文件夹ID private long mFolderId; - + // 上下文对象 private Context mContext; - + // 日志标签 private static final String TAG = "WorkingNote"; - + // 是否已删除标记 private boolean mIsDeleted; - + // 笔记设置变更监听器 private NoteSettingChangedListener mNoteSettingStatusListener; + // 数据查询投影,用于查询笔记数据 public static final String[] DATA_PROJECTION = new String[] { DataColumns.ID, DataColumns.CONTENT, @@ -72,6 +76,7 @@ public class WorkingNote { DataColumns.DATA4, }; + // 笔记查询投影,用于查询笔记基本信息 public static final String[] NOTE_PROJECTION = new String[] { NoteColumns.PARENT_ID, NoteColumns.ALERTED_DATE, @@ -81,27 +86,25 @@ public class WorkingNote { NoteColumns.MODIFIED_DATE }; + // 数据列索引常量 private static final int DATA_ID_COLUMN = 0; - private static final int DATA_CONTENT_COLUMN = 1; - private static final int DATA_MIME_TYPE_COLUMN = 2; - private static final int DATA_MODE_COLUMN = 3; + // 笔记列索引常量 private static final int NOTE_PARENT_ID_COLUMN = 0; - private static final int NOTE_ALERTED_DATE_COLUMN = 1; - private static final int NOTE_BG_COLOR_ID_COLUMN = 2; - private static final int NOTE_WIDGET_ID_COLUMN = 3; - private static final int NOTE_WIDGET_TYPE_COLUMN = 4; - private static final int NOTE_MODIFIED_DATE_COLUMN = 5; - // New note construct + /** + * 私有构造函数,创建新笔记 + * @param context 上下文对象 + * @param folderId 父文件夹ID + */ private WorkingNote(Context context, long folderId) { mContext = context; mAlertDate = 0; @@ -114,7 +117,12 @@ public class WorkingNote { mWidgetType = Notes.TYPE_WIDGET_INVALIDE; } - // Existing note construct + /** + * 私有构造函数,加载现有笔记 + * @param context 上下文对象 + * @param noteId 笔记ID + * @param folderId 父文件夹ID + */ private WorkingNote(Context context, long noteId, long folderId) { mContext = context; mNoteId = noteId; @@ -124,13 +132,18 @@ public class WorkingNote { loadNote(); } + /** + * 加载笔记基本信息 + */ private void loadNote() { + // 查询笔记基本信息 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); @@ -143,10 +156,15 @@ public class WorkingNote { Log.e(TAG, "No note with id:" + mNoteId); throw new IllegalArgumentException("Unable to find note with id " + mNoteId); } + // 加载笔记数据 loadNoteData(); } + /** + * 加载笔记数据内容 + */ private void loadNoteData() { + // 查询笔记数据内容 Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { String.valueOf(mNoteId) @@ -155,12 +173,15 @@ public class WorkingNote { if (cursor != null) { if (cursor.moveToFirst()) { do { + // 根据MIME类型处理不同的数据 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)) { + // 处理通话记录笔记 mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); } else { Log.d(TAG, "Wrong note type with type:" + type); @@ -174,6 +195,15 @@ public class WorkingNote { } } + /** + * 创建空笔记的静态工厂方法 + * @param context 上下文对象 + * @param folderId 父文件夹ID + * @param widgetId 小部件ID + * @param widgetType 小部件类型 + * @param defaultBgColorId 默认背景颜色ID + * @return 创建的工作笔记对象 + */ public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, int widgetType, int defaultBgColorId) { WorkingNote note = new WorkingNote(context, folderId); @@ -183,23 +213,35 @@ public class WorkingNote { return note; } + /** + * 加载现有笔记的静态工厂方法 + * @param context 上下文对象 + * @param id 笔记ID + * @return 加载的工作笔记对象 + */ public static WorkingNote load(Context context, long id) { return new WorkingNote(context, id, 0); } + /** + * 保存笔记 + * @return 保存成功返回true,失败返回false + */ public synchronized boolean saveNote() { if (isWorthSaving()) { if (!existInDatabase()) { + // 如果笔记不存在于数据库中,创建新笔记 if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { 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 @@ -212,10 +254,18 @@ public class WorkingNote { } } + /** + * 检查笔记是否存在于数据库中 + * @return 存在返回true,不存在返回false + */ public boolean existInDatabase() { return mNoteId > 0; } + /** + * 检查笔记是否值得保存 + * @return 值得保存返回true,否则返回false + */ private boolean isWorthSaving() { if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) || (existInDatabase() && !mNote.isLocalModified())) { @@ -225,41 +275,66 @@ public class WorkingNote { } } + /** + * 设置笔记设置变更监听器 + * @param l 监听器对象 + */ public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) { mNoteSettingStatusListener = l; } + /** + * 设置提醒日期 + * @param date 提醒日期时间戳 + * @param set 是否设置提醒 + */ public void setAlertDate(long date, boolean set) { if (date != mAlertDate) { mAlertDate = date; mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); } if (mNoteSettingStatusListener != null) { + // 通知监听器提醒设置变更 mNoteSettingStatusListener.onClockAlertChanged(date, set); } } + /** + * 标记笔记为已删除或未删除 + * @param mark 是否标记为已删除 + */ public void markDeleted(boolean mark) { mIsDeleted = mark; if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { + // 通知监听器小部件变更 mNoteSettingStatusListener.onWidgetChanged(); } } + /** + * 设置笔记背景颜色 + * @param id 背景颜色ID + */ public void setBgColorId(int id) { if (id != mBgColorId) { mBgColorId = id; if (mNoteSettingStatusListener != null) { + // 通知监听器背景色变更 mNoteSettingStatusListener.onBackgroundColorChanged(); } mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); } } + /** + * 设置待办事项模式 + * @param mode 模式值 + */ public void setCheckListMode(int mode) { if (mMode != mode) { if (mNoteSettingStatusListener != null) { + // 通知监听器模式变更 mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); } mMode = mode; @@ -267,6 +342,10 @@ public class WorkingNote { } } + /** + * 设置小部件类型 + * @param type 小部件类型 + */ public void setWidgetType(int type) { if (type != mWidgetType) { mWidgetType = type; @@ -274,6 +353,10 @@ public class WorkingNote { } } + /** + * 设置小部件ID + * @param id 小部件ID + */ public void setWidgetId(int id) { if (id != mWidgetId) { mWidgetId = id; @@ -281,6 +364,10 @@ public class WorkingNote { } } + /** + * 设置笔记工作文本内容 + * @param text 文本内容 + */ public void setWorkingText(String text) { if (!TextUtils.equals(mContent, text)) { mContent = text; @@ -288,81 +375,138 @@ public class WorkingNote { } } + /** + * 将笔记转换为通话记录笔记 + * @param phoneNumber 电话号码 + * @param callDate 通话日期 + */ public void convertToCallNote(String phoneNumber, long callDate) { mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate)); mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber); mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER)); } + /** + * 检查笔记是否设置了时钟提醒 + * @return 设置了提醒返回true,否则返回false + */ public boolean hasClockAlert() { return (mAlertDate > 0 ? true : false); } + /** + * 获取笔记内容 + * @return 笔记内容字符串 + */ public String getContent() { return mContent; } + /** + * 获取提醒日期 + * @return 提醒日期时间戳 + */ public long getAlertDate() { return mAlertDate; } + /** + * 获取修改日期 + * @return 修改日期时间戳 + */ public long getModifiedDate() { return mModifiedDate; } + /** + * 获取背景颜色资源ID + * @return 背景颜色资源ID + */ public int getBgColorResId() { return NoteBgResources.getNoteBgResource(mBgColorId); } + /** + * 获取背景颜色ID + * @return 背景颜色ID + */ public int getBgColorId() { return mBgColorId; } + /** + * 获取标题背景资源ID + * @return 标题背景资源ID + */ public int getTitleBgResId() { return NoteBgResources.getNoteTitleBgResource(mBgColorId); } + /** + * 获取待办事项模式 + * @return 待办事项模式值 + */ public int getCheckListMode() { return mMode; } + /** + * 获取笔记ID + * @return 笔记ID + */ public long getNoteId() { return mNoteId; } + /** + * 获取文件夹ID + * @return 文件夹ID + */ public long getFolderId() { return mFolderId; } + /** + * 获取小部件ID + * @return 小部件ID + */ public int getWidgetId() { return mWidgetId; } + /** + * 获取小部件类型 + * @return 小部件类型 + */ public int getWidgetType() { return mWidgetType; } + /** + * 笔记设置变更监听器接口 + * 用于通知笔记属性变更事件 + */ public interface NoteSettingChangedListener { /** - * Called when the background color of current note has just changed + * 当笔记背景颜色变更时调用 */ void onBackgroundColorChanged(); /** - * Called when user set clock + * 当用户设置时钟提醒时调用 */ void onClockAlertChanged(long date, boolean set); /** - * Call when user create note from widget + * 当用户从小部件创建笔记时调用 */ void onWidgetChanged(); /** - * Call when switch between check list mode and normal mode - * @param oldMode is previous mode before change - * @param newMode is new mode + * 当切换待办事项模式和普通模式时调用 + * @param oldMode 变更前的模式 + * @param newMode 变更后的新模式 */ void onCheckListModeChanged(int oldMode, int newMode); } -} +} \ No newline at end of file