diff --git a/Notes-master/src/net/micode/notes/model/WorkingNote.java b/Notes-master/src/net/micode/notes/model/WorkingNote.java index be081e4..2d108a6 100644 --- a/Notes-master/src/net/micode/notes/model/WorkingNote.java +++ b/Notes-master/src/net/micode/notes/model/WorkingNote.java @@ -32,105 +32,143 @@ 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, - DataColumns.MIME_TYPE, - DataColumns.DATA1, - DataColumns.DATA2, - DataColumns.DATA3, - DataColumns.DATA4, + DataColumns.ID, // 数据ID + DataColumns.CONTENT, // 数据内容 + DataColumns.MIME_TYPE, // 数据MIME类型 + DataColumns.DATA1, // 数据字段1 + DataColumns.DATA2, // 数据字段2 + DataColumns.DATA3, // 数据字段3 + DataColumns.DATA4 // 数据字段4 }; + /** + * 笔记查询时需要的列。 + */ public static final String[] NOTE_PROJECTION = new String[] { - NoteColumns.PARENT_ID, - NoteColumns.ALERTED_DATE, - NoteColumns.BG_COLOR_ID, - NoteColumns.WIDGET_ID, - NoteColumns.WIDGET_TYPE, - NoteColumns.MODIFIED_DATE + NoteColumns.PARENT_ID, // 笔记父ID(所属文件夹ID) + NoteColumns.ALERTED_DATE, // 笔记提醒日期 + NoteColumns.BG_COLOR_ID, // 笔记背景颜色ID + NoteColumns.WIDGET_ID, // 笔记小部件ID + NoteColumns.WIDGET_TYPE, // 笔记小部件类型 + 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; + /** + * 数据查询结果列的索引。 + */ + private static final int DATA_ID_COLUMN = 0; // 数据ID列索引 + private static final int DATA_CONTENT_COLUMN = 1; // 数据内容列索引 + private static final int DATA_MIME_TYPE_COLUMN = 2; // 数据MIME类型列索引 + private static final int DATA_MODE_COLUMN = 3; // 数据模式列索引 + private static final int NOTE_PARENT_ID_COLUMN = 0; // 笔记父ID列索引 + private static final int NOTE_ALERTED_DATE_COLUMN = 1; // 笔记提醒日期列索引 + private static final int NOTE_BG_COLOR_ID_COLUMN = 2; // 笔记背景颜色ID列索引 + private static final int NOTE_WIDGET_ID_COLUMN = 3; // 笔记小部件ID列索引 + private static final int NOTE_WIDGET_TYPE_COLUMN = 4; // 笔记小部件类型列索引 + private static final int NOTE_MODIFIED_DATE_COLUMN = 5; // 笔记最后修改日期列索引 +} - // New note construct private WorkingNote(Context context, long folderId) { + // 初始化上下文对象,用于后续访问应用的资源和类 mContext = context; + // 初始化提醒日期为0,表示没有设置提醒 mAlertDate = 0; + // 初始化最后修改日期为当前时间戳 mModifiedDate = System.currentTimeMillis(); + // 设置笔记所属的文件夹ID mFolderId = folderId; + // 创建一个新的Note对象,用于存储笔记的详细信息 mNote = new Note(); + // 初始化笔记ID为0,表示这是一个新笔记,尚未保存到数据库 mNoteId = 0; + // 标记笔记为未删除状态 mIsDeleted = false; + // 初始化笔记模式为0,具体模式根据应用需求定义 mMode = 0; + // 设置小部件类型为无效类型,表示这个笔记还没有关联的小部件 mWidgetType = Notes.TYPE_WIDGET_INVALIDE; } // Existing note construct private WorkingNote(Context context, long noteId, long folderId) { + // 初始化上下文对象,用于后续访问应用的资源和类 mContext = context; + // 设置笔记ID mNoteId = noteId; + // 设置笔记所属的文件夹ID mFolderId = folderId; + // 标记笔记为未删除状态 mIsDeleted = false; + // 创建一个新的Note对象,用于存储笔记的详细信息 mNote = new Note(); + // 加载笔记的详细信息 loadNote(); } - + + /** + * 加载笔记的详细信息。 + */ private void loadNote() { + // 根据笔记ID查询笔记的详细信息 Cursor cursor = mContext.getContentResolver().query( - ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null, - null, null); - + ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), // 构建笔记的内容URI + 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,84 +176,104 @@ public class WorkingNote { mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN); mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN); } - cursor.close(); + cursor.close(); // 关闭游标 } else { + // 如果查询结果为空,记录错误日志并抛出异常 Log.e(TAG, "No note with id:" + mNoteId); throw new IllegalArgumentException("Unable to find note with id " + mNoteId); } + // 加载笔记的数据内容 loadNoteData(); } private void loadNoteData() { + // 通过内容解析器查询笔记数据URI,获取与当前笔记ID关联的数据 Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, DataColumns.NOTE_ID + "=?", new String[] { - String.valueOf(mNoteId) + String.valueOf(mNoteId) // 查询参数,当前笔记的ID }, null); - + + // 如果查询结果不为空 if (cursor != null) { + // 如果查询结果至少有一条数据 if (cursor.moveToFirst()) { + // 遍历查询结果 do { + // 获取数据项的MIME类型 String type = cursor.getString(DATA_MIME_TYPE_COLUMN); + // 根据MIME类型处理数据 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()); + } while (cursor.moveToNext()); // 移动到下一条数据 } - cursor.close(); + cursor.close(); // 关闭游标 } else { + // 如果查询结果为空,记录错误日志并抛出异常 Log.e(TAG, "No data with id:" + mNoteId); throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId); } } - public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId, - int widgetType, int defaultBgColorId) { - WorkingNote note = new WorkingNote(context, folderId); - note.setBgColorId(defaultBgColorId); - note.setWidgetId(widgetId); - note.setWidgetType(widgetType); - return note; + public static WorkingNote createEmptyNote(Context context, long folderId, int widgetId,int widgetType, int defaultBgColorId) { + WorkingNote note = new WorkingNote(context, folderId); // 创建一个新的WorkingNote实例 + note.setBgColorId(defaultBgColorId); // 设置笔记的背景颜色ID + note.setWidgetId(widgetId); // 设置笔记的小部件ID + note.setWidgetType(widgetType); // 设置笔记的小部件类型 + return note; // 返回新创建的笔记实例 } - public static WorkingNote load(Context context, long id) { - return new WorkingNote(context, id, 0); + return new WorkingNote(context, id, 0); // 创建并返回一个新的WorkingNote实例 } - public synchronized boolean saveNote() { + // 检查笔记是否值得保存 if (isWorthSaving()) { + // 如果笔记在数据库中不存在 if (!existInDatabase()) { + // 尝试获取一个新的笔记ID if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { - Log.e(TAG, "Create new note fail with id:" + mNoteId); - return false; + Log.e(TAG, "Create new note fail with id:" + mNoteId); // 如果创建失败,记录错误日志 + return false; // 返回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(); + mNoteSettingStatusListener.onWidgetChanged(); // 通知小部件更改 } - return true; + return true; // 返回true,表示保存成功 } else { - return false; + return false; // 如果笔记不值得保存,返回false } } + /** + * 检查笔记ID是否大于0,即笔记是否存在于数据库中。 + */ public boolean existInDatabase() { return mNoteId > 0; } + /** + * 判断笔记是否值得保存。 + * @return 如果笔记被标记为删除,或者内容为空(对于新笔记),或者笔记未被修改(对于已存在的笔记),则返回false;否则返回true。 + */ private boolean isWorthSaving() { if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) || (existInDatabase() && !mNote.isLocalModified())) { @@ -225,10 +283,16 @@ public class WorkingNote { } } + /** + * 设置笔记设置状态改变的监听器。 + */ public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) { mNoteSettingStatusListener = l; } + /** + * 设置提醒日期并根据需要更新小部件。 + */ public void setAlertDate(long date, boolean set) { if (date != mAlertDate) { mAlertDate = date; @@ -239,14 +303,20 @@ public class WorkingNote { } } + /** + * 标记笔记为删除或未删除状态,并更新小部件。 + */ public void markDeleted(boolean mark) { mIsDeleted = mark; if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { - mNoteSettingStatusListener.onWidgetChanged(); + mNoteSettingStatusListener.onWidgetChanged(); } } + /** + * 设置笔记的背景颜色ID,并更新小部件。 + */ public void setBgColorId(int id) { if (id != mBgColorId) { mBgColorId = id; @@ -257,6 +327,9 @@ public class WorkingNote { } } + /** + * 设置笔记的检查列表模式,并更新小部件。 + */ public void setCheckListMode(int mode) { if (mMode != mode) { if (mNoteSettingStatusListener != null) { @@ -267,6 +340,9 @@ public class WorkingNote { } } + /** + * 设置笔记的小部件类型。 + */ public void setWidgetType(int type) { if (type != mWidgetType) { mWidgetType = type; @@ -274,6 +350,9 @@ public class WorkingNote { } } + /** + * 设置笔记的小部件ID。 + */ public void setWidgetId(int id) { if (id != mWidgetId) { mWidgetId = id; @@ -281,6 +360,9 @@ public class WorkingNote { } } + /** + * 设置笔记的工作文本,并更新数据库。 + */ public void setWorkingText(String text) { if (!TextUtils.equals(mContent, text)) { mContent = text; @@ -288,80 +370,120 @@ public class WorkingNote { } } + /** + * 将笔记转换为通话记录笔记。 + */ 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)); } + /** + * 检查笔记是否有时钟提醒。 + */ public boolean hasClockAlert() { return (mAlertDate > 0 ? true : false); } + /** + * 获取笔记的内容。 + */ public String getContent() { return mContent; } + /** + * 获取笔记的提醒日期。 + */ public long getAlertDate() { return mAlertDate; } + /** + * 获取笔记的最后修改日期。 + */ public long getModifiedDate() { return mModifiedDate; } + /** + * 获取笔记背景颜色的资源ID。 + */ public int getBgColorResId() { return NoteBgResources.getNoteBgResource(mBgColorId); } + /** + * 获取笔记的背景颜色ID。 + */ public int getBgColorId() { return mBgColorId; } + /** + * 获取笔记标题背景的资源ID。 + */ public int getTitleBgResId() { return NoteBgResources.getNoteTitleBgResource(mBgColorId); } + /** + * 获取笔记的检查列表模式。 + */ public int getCheckListMode() { return mMode; } + /** + * 获取笔记的ID。 + */ public long getNoteId() { return mNoteId; } + /** + * 获取笔记所属文件夹的ID。 + */ public long getFolderId() { return mFolderId; } + /** + * 获取笔记的小部件ID。 + */ public int getWidgetId() { return mWidgetId; } + /** + * 获取笔记的小部件类型。 + */ 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 + * 在检查列表模式和普通模式之间切换时调用。 */ void onCheckListModeChanged(int oldMode, int newMode); }