model/WorkingNote.java注释提交

master
田艳 2 years ago
parent 8943a6ce11
commit 2ba9efd998

@ -33,336 +33,358 @@ import net.micode.notes.tool.ResourceParser.NoteBgResources;
public class WorkingNote { public class WorkingNote {
// Note for the working note // 工作笔记的类定义
private Note mNote;
// Note Id // 笔记对象
private long mNoteId; private Note mNote;
// Note content // 笔记ID
private String mContent; private long mNoteId;
// Note mode // 笔记内容
private int mMode; private String mContent;
// 笔记模式
private long mAlertDate; private int mMode;
// 提醒日期
private long mModifiedDate; private long mAlertDate;
// 修改日期
private int mBgColorId; private long mModifiedDate;
// 背景颜色ID
private int mWidgetId; private int mBgColorId;
// 小部件ID
private int mWidgetType; private int mWidgetId;
// 小部件类型
private long mFolderId; private int mWidgetType;
// 文件夹ID
private Context mContext; private long mFolderId;
// 上下文
private static final String TAG = "WorkingNote"; private Context mContext;
// 删除标记
private boolean mIsDeleted; private boolean mIsDeleted;
// 笔记设置变更监听器
private NoteSettingChangedListener mNoteSettingStatusListener; private NoteSettingChangedListener mNoteSettingStatusListener;
public static final String[] DATA_PROJECTION = new String[] {// 声明 DATA_PROJECTION字符串数组 // 查询字符串数组
DataColumns.ID, public static final String[] DATA_PROJECTION = new String[] {
DataColumns.CONTENT, DataColumns.ID,
DataColumns.MIME_TYPE, DataColumns.CONTENT,
DataColumns.DATA1, DataColumns.MIME_TYPE,
DataColumns.DATA2, DataColumns.DATA1,
DataColumns.DATA3, DataColumns.DATA2,
DataColumns.DATA4, DataColumns.DATA3,
}; DataColumns.DATA4,
};
public static final String[] NOTE_PROJECTION = new String[] { // 声明 NOTE_PROJECTION字符串数组
NoteColumns.PARENT_ID, // 查询字符串数组
NoteColumns.ALERTED_DATE, public static final String[] NOTE_PROJECTION = new String[] {
NoteColumns.BG_COLOR_ID, NoteColumns.PARENT_ID,
NoteColumns.WIDGET_ID, NoteColumns.ALERTED_DATE,
NoteColumns.WIDGET_TYPE, NoteColumns.BG_COLOR_ID,
NoteColumns.MODIFIED_DATE NoteColumns.WIDGET_ID,
}; NoteColumns.WIDGET_TYPE,
NoteColumns.MODIFIED_DATE
private static final int DATA_ID_COLUMN = 0; };
private static final int DATA_CONTENT_COLUMN = 1; // 数据 ID 列
private static final int DATA_ID_COLUMN = 0;
private static final int DATA_MIME_TYPE_COLUMN = 2; // 数据内容列
private static final int DATA_CONTENT_COLUMN = 1;
private static final int DATA_MODE_COLUMN = 3; // 数据 MIME 类型列
private static final int DATA_MIME_TYPE_COLUMN = 2;
private static final int NOTE_PARENT_ID_COLUMN = 0; // 数据模式列
private static final int DATA_MODE_COLUMN = 3;
private static final int NOTE_ALERTED_DATE_COLUMN = 1; // 笔记父ID列
private static final int NOTE_PARENT_ID_COLUMN = 0;
private static final int NOTE_BG_COLOR_ID_COLUMN = 2; // 笔记提醒日期列
private static final int NOTE_ALERTED_DATE_COLUMN = 1;
private static final int NOTE_WIDGET_ID_COLUMN = 3; // 笔记背景颜色ID列
private static final int NOTE_BG_COLOR_ID_COLUMN = 2;
private static final int NOTE_WIDGET_TYPE_COLUMN = 4; // 笔记小部件ID列
private static final int NOTE_WIDGET_ID_COLUMN = 3;
private static final int NOTE_MODIFIED_DATE_COLUMN = 5; // 笔记小部件类型列
private static final int NOTE_WIDGET_TYPE_COLUMN = 4;
// 笔记修改日期列
private static final int NOTE_MODIFIED_DATE_COLUMN = 5;
// 新建笔记构造函数
private WorkingNote(Context context, long folderId) {
mContext = context;
mAlertDate = 0;
mModifiedDate = System.currentTimeMillis();
mFolderId = folderId;
mNote = new Note();
mNoteId = 0;
mIsDeleted = false;
mMode = 0;
mWidgetType = Notes.TYPE_WIDGET_INVALIDE;
}
// New note construct // 已有笔记构造函数
private WorkingNote(Context context, long folderId) { private WorkingNote(Context context, long noteId, long folderId) {
mContext = context; mContext = context;
mAlertDate = 0; mNoteId = noteId;
mModifiedDate = System.currentTimeMillis(); mFolderId = folderId;
mFolderId = folderId; mIsDeleted = false;
mNote = new Note(); mNote = new Note();
mNoteId = 0; loadNote();
mIsDeleted = false; }
mMode = 0;
mWidgetType = Notes.TYPE_WIDGET_INVALIDE;
}
// WorkingNote的构造函数
// Existing note construct
private WorkingNote(Context context, long noteId, long folderId) {
mContext = context;
mNoteId = noteId;
mFolderId = folderId;
mIsDeleted = false;
mNote = new Note();
loadNote();
}
private void loadNote() { // 载入笔记
Cursor cursor = mContext.getContentResolver().query( private void loadNote() {
ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null, Cursor cursor = mContext.getContentResolver().query(
null, null); ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId),
NOTE_PROJECTION,
if (cursor != null) { null,
if (cursor.moveToFirst()) { null,
mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN); null);
mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN);
mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN); if (cursor != null) {
mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN); if (cursor.moveToFirst()) {
mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN); mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN);
mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN); mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN);
} mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN);
cursor.close(); mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN);
} else { mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN);
Log.e(TAG, "No note with id:" + mNoteId); mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN);
throw new IllegalArgumentException("Unable to find note with id " + mNoteId);
} }
loadNoteData(); 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() { // 载入笔记的数据
Cursor cursor = mContext.getContentResolver().query(Notes.CONTENT_DATA_URI, DATA_PROJECTION, private void loadNoteData() {
DataColumns.NOTE_ID + "=?", new String[] { Cursor cursor = mContext.getContentResolver().query(
String.valueOf(mNoteId) Notes.CONTENT_DATA_URI,
}, null); DATA_PROJECTION,
DataColumns.NOTE_ID + "=?",
if (cursor != null) { new String[] { String.valueOf(mNoteId) },
if (cursor.moveToFirst()) { null);
do {
String type = cursor.getString(DATA_MIME_TYPE_COLUMN); if (cursor != null) {
if (DataConstants.NOTE.equals(type)) { if (cursor.moveToFirst()) {
mContent = cursor.getString(DATA_CONTENT_COLUMN); do {
mMode = cursor.getInt(DATA_MODE_COLUMN); String type = cursor.getString(DATA_MIME_TYPE_COLUMN);
mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN)); if (DataConstants.NOTE.equals(type)) {
} else if (DataConstants.CALL_NOTE.equals(type)) { mContent = cursor.getString(DATA_CONTENT_COLUMN);
mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); mMode = cursor.getInt(DATA_MODE_COLUMN);
} else { mNote.setTextDataId(cursor.getLong(DATA_ID_COLUMN));
Log.d(TAG, "Wrong note type with type:" + type); } else if (DataConstants.CALL_NOTE.equals(type)) {
} mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN));
} while (cursor.moveToNext()); } else {
} Log.d(TAG, "Wrong note type with type:" + type);
cursor.close(); }
} else { } while (cursor.moveToNext());
Log.e(TAG, "No data with id:" + mNoteId);
throw new IllegalArgumentException("Unable to find note's data with id " + mNoteId);
} }
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) { public static WorkingNote createEmptyNote(
WorkingNote note = new WorkingNote(context, folderId); Context context,
note.setBgColorId(defaultBgColorId); long folderId,
note.setWidgetId(widgetId); int widgetId,
note.setWidgetType(widgetType); int widgetType,
return note; int defaultBgColorId) {
} WorkingNote note = new WorkingNote(context, folderId);
note.setBgColorId(defaultBgColorId);
public static WorkingNote load(Context context, long id) { note.setWidgetId(widgetId);
return new WorkingNote(context, id, 0); note.setWidgetType(widgetType);
} return note;
}
public synchronized boolean saveNote() { // 载入笔记
if (isWorthSaving()) { public static WorkingNote load(Context context, long id) {
if (!existInDatabase()) { return new WorkingNote(context, id, 0);
if ((mNoteId = Note.getNewNoteId(mContext, mFolderId)) == 0) { }
Log.e(TAG, "Create new note fail with id:" + mNoteId); }
return 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);
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
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID && mNoteSettingStatusListener != null) {
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE mNoteSettingStatusListener.onWidgetChanged();
&& mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged();
}
return true;
} else {
return false;
} }
return true;
} else {
return false;
} }
}
public boolean existInDatabase() { // 判断笔记是否已存在于数据库
return mNoteId > 0; public boolean existInDatabase() {
return mNoteId > 0;
}
// 判断是否需要保存笔记
private boolean isWorthSaving() {
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent))
|| (existInDatabase() && !mNote.isLocalModified())) {
return false;
} else {
return true;
} }
}
private boolean isWorthSaving() { // 设置笔记状态变化的监听器
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) {
|| (existInDatabase() && !mNote.isLocalModified())) { mNoteSettingStatusListener = l;
return false; }
} else {
return true; // 设置提醒日期
} 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);
}
}
public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) { // 对笔记进行标记
mNoteSettingStatusListener = l; public void markDeleted(boolean mark) {
mIsDeleted = mark;
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged();
} }
}
public void setAlertDate(long date, boolean set) { // 设置背景颜色
if (date != mAlertDate) { public void setBgColorId(int id) {
mAlertDate = date; if (id != mBgColorId) {
mNote.setNoteValue(NoteColumns.ALERTED_DATE, String.valueOf(mAlertDate)); mBgColorId = id;
}
if (mNoteSettingStatusListener != null) { if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onClockAlertChanged(date, set); mNoteSettingStatusListener.onBackgroundColorChanged();
} }
mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id));
} }
}
public void markDeleted(boolean mark) { // 设置检查清单
mIsDeleted = mark; public void setCheckListMode(int mode) {
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID if (mMode != mode) {
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { if (mNoteSettingStatusListener != null) {
mNoteSettingStatusListener.onWidgetChanged(); mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode);
} }
mMode = mode;
mNote.setTextData(TextNote.MODE, String.valueOf(mMode));
} }
}
public void setBgColorId(int id) { // 设置部件类型
if (id != mBgColorId) { public void setWidgetType(int type) {
mBgColorId = id; if (type != mWidgetType) {
if (mNoteSettingStatusListener != null) { mWidgetType = type;
mNoteSettingStatusListener.onBackgroundColorChanged(); mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType));
}
mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id));
}
} }
}
public void setCheckListMode(int mode) { // 设置部件ID
if (mMode != mode) { public void setWidgetId(int id) {
if (mNoteSettingStatusListener != null) { if (id != mWidgetId) {
mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode); mWidgetId = id;
} mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId));
mMode = mode;
mNote.setTextData(TextNote.MODE, String.valueOf(mMode));
}
} }
}
public void setWidgetType(int type) { // 设置工作文本
if (type != mWidgetType) { public void setWorkingText(String text) {
mWidgetType = type; if (!TextUtils.equals(mContent, text)) {
mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType)); mContent = text;
} mNote.setTextData(DataColumns.CONTENT, mContent);
} }
}
public void setWidgetId(int id) { // 转换成通话记录
if (id != mWidgetId) { public void convertToCallNote(String phoneNumber, long callDate) {
mWidgetId = id; mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate));
mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber);
} mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER));
} }
public void setWorkingText(String text) { // 是否包含闹钟提醒
if (!TextUtils.equals(mContent, text)) { public boolean hasClockAlert() {
mContent = text; return (mAlertDate > 0 ? true : false);
mNote.setTextData(DataColumns.CONTENT, mContent); }
}
}
public void convertToCallNote(String phoneNumber, long callDate) { public String getContent() {
mNote.setCallData(CallNote.CALL_DATE, String.valueOf(callDate)); return mContent;
mNote.setCallData(CallNote.PHONE_NUMBER, phoneNumber); }
mNote.setNoteValue(NoteColumns.PARENT_ID, String.valueOf(Notes.ID_CALL_RECORD_FOLDER));
}
public boolean hasClockAlert() { public long getAlertDate() {
return (mAlertDate > 0 ? true : false); return mAlertDate;
} }
public String getContent() { public long getModifiedDate() {
return mContent; return mModifiedDate;
} }
public long getAlertDate() { public int getBgColorResId() {
return mAlertDate; return NoteBgResources.getNoteBgResource(mBgColorId);
} }
public long getModifiedDate() { public int getBgColorId() {
return mModifiedDate; return mBgColorId;
} }
public int getBgColorResId() { public int getTitleBgResId() {
return NoteBgResources.getNoteBgResource(mBgColorId); return NoteBgResources.getNoteTitleBgResource(mBgColorId);
} }
public int getBgColorId() { public int getCheckListMode() {
return mBgColorId; return mMode;
} }
public int getTitleBgResId() { public long getNoteId() {
return NoteBgResources.getNoteTitleBgResource(mBgColorId); return mNoteId;
} }
public int getCheckListMode() { public long getFolderId() {
return mMode; return mFolderId;
} }
public long getNoteId() { public int getWidgetId() {
return mNoteId; return mWidgetId;
} }
public long getFolderId() { public int getWidgetType() {
return mFolderId; return mWidgetType;
} }
public int getWidgetId() { // 笔记状态监听器接口
return mWidgetId; public interface NoteSettingChangedListener {
} // 当当前笔记的背景颜色已改变时调用
void onBackgroundColorChanged();
public int getWidgetType() { // 当用户设置闹钟时调用
return mWidgetType; void onClockAlertChanged(long date, boolean set);
}
public interface NoteSettingChangedListener { // 当从部件创建笔记时调用
/** void onWidgetChanged();
* Called when the background color of current note has just changed
*/ // 当检查清单模式与普通模式之间切换时调用
void onBackgroundColorChanged(); void onCheckListModeChanged(int oldMode, int newMode);
/**
* 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);
}
} }
}
Loading…
Cancel
Save