|
|
/*
|
|
|
* 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.
|
|
|
* 总体分析
|
|
|
这段 Java 代码定义了WorkingNote类,它是对笔记相关操作及状态管理的核心类,整合了笔记从创建、加载、保存到各种属性设置及状态变更等多方面的功能逻辑。类中通过不同的构造函数来区分新建笔记和加载已有笔记的情况,在加载过程中会从数据库获取笔记及附属数据的相关信息进行初始化。同时提供了诸多方法用于设置笔记的各类属性(如背景颜色、提醒日期、部件相关属性等)以及判断笔记是否值得保存、是否存在于数据库等,并且定义了一个接口用于监听笔记相关设置状态变化,整体围绕着笔记在应用内的完整生命周期管理及与用户交互过程中的状态响应来构建功能。
|
|
|
函数分析
|
|
|
构造函数相关
|
|
|
WorkingNote(Context context, long folderId)构造函数:
|
|
|
所属类:WorkingNote
|
|
|
功能:用于创建新笔记的初始化,接收应用上下文和所属文件夹ID,设置提醒日期、修改日期、初始化笔记对象、笔记ID、删除状态、模式等属性,为新建笔记准备基础数据。
|
|
|
WorkingNote(Context context, long noteId, long folderId)构造函数:
|
|
|
所属类:WorkingNote
|
|
|
功能:针对加载已有笔记的情况,接收应用上下文、笔记ID和所属文件夹ID,初始化相关属性后调用loadNote方法从数据库加载笔记具体信息,用于还原已有笔记的各项数据状态。
|
|
|
loadNote方法
|
|
|
所属类:WorkingNote
|
|
|
功能:通过ContentResolver根据笔记ID查询笔记的部分关键属性(如父ID、背景颜色ID等),若游标不为null且能移动到第一条记录,则获取对应属性值进行赋值,游标遍历完后关闭游标,若游标为null则抛出异常,接着调用loadNoteData方法加载笔记附属数据,整体实现从数据库加载笔记基本信息的功能。
|
|
|
loadNoteData方法
|
|
|
所属类:WorkingNote
|
|
|
功能:利用ContentResolver依据笔记ID查询笔记附属数据,若游标不为null且能移动到第一条记录,则遍历游标,根据数据的MIME_TYPE判断类型(如普通笔记、通话记录等),分别进行对应的数据处理(如设置文本数据ID、通话数据ID等),游标遍历完后关闭游标,若游标为null则抛出异常,用于加载笔记关联的具体数据内容。
|
|
|
createEmptyNote方法
|
|
|
所属类:WorkingNote
|
|
|
功能:创建一个新的空笔记对象,传入应用上下文、所属文件夹ID、部件ID、部件类型和默认背景颜色ID等参数,先实例化WorkingNote对象,再设置其背景颜色ID、部件ID和部件类型等属性,最后返回创建好的空笔记对象,用于便捷地创建具有初始属性配置的新笔记。
|
|
|
load方法
|
|
|
所属类:WorkingNote
|
|
|
功能:接收应用上下文和笔记ID,通过调用对应构造函数创建并返回一个用于加载已有笔记的WorkingNote对象,方便从外部根据ID获取对应的笔记实例进行后续操作。
|
|
|
saveNote方法
|
|
|
所属类:WorkingNote
|
|
|
功能:先判断笔记是否值得保存(通过isWorthSaving方法判断),若值得保存且笔记不存在于数据库中,则尝试创建新笔记并获取其ID,若创建失败则记录日志并返回false;接着调用笔记对象的syncNote方法将本地修改同步到数据库;若笔记存在部件且相关监听不为null,则触发部件变更监听回调,最后根据操作情况返回对应的布尔值,用于实现将笔记数据保存到数据库以及响应部件相关变更的功能。
|
|
|
existInDatabase方法
|
|
|
所属类:WorkingNote
|
|
|
功能:通过判断笔记ID是否大于0,返回笔记是否存在于数据库中的布尔值,用于简单确认笔记在数据库中的存在情况。
|
|
|
isWorthSaving方法
|
|
|
所属类:WorkingNote
|
|
|
功能:根据笔记的删除状态、在数据库中的存在情况以及内容是否为空、笔记是否有本地修改等条件综合判断笔记是否值得保存,返回对应的布尔值,为saveNote等方法决定是否执行保存操作提供依据。
|
|
|
setOnSettingStatusChangedListener方法
|
|
|
所属类:WorkingNote
|
|
|
功能:设置笔记设置状态变更的监听器对象,用于后续在笔记相关属性(如背景颜色、提醒等)变更时能触发对应的回调方法,实现外部对笔记状态变化的监听与响应。
|
|
|
setAlertDate方法
|
|
|
所属类:WorkingNote
|
|
|
功能:若传入的提醒日期与当前记录的提醒日期不同,则更新提醒日期,并通过笔记对象设置对应属性值,若监听器不为null,则触发提醒日期变更的监听回调,用于设置笔记的提醒日期并通知外部监听者。
|
|
|
markDeleted方法
|
|
|
所属类:WorkingNote
|
|
|
功能:更新笔记的删除标记状态,若笔记存在部件且相关监听不为null,则触发部件变更的监听回调,用于标记笔记是否已删除并响应部件相关变化。
|
|
|
setBgColorId方法
|
|
|
所属类:WorkingNote
|
|
|
功能:若传入的背景颜色ID与当前记录的不同,则更新背景颜色ID,触发背景颜色变更的监听回调,并通过笔记对象设置对应属性值,用于设置笔记的背景颜色ID并通知外部监听者。
|
|
|
setCheckListMode方法
|
|
|
所属类:WorkingNote
|
|
|
功能:若传入的清单模式与当前记录的不同,则更新清单模式,触发清单模式变更的监听回调,并通过笔记对象设置对应文本数据,用于设置笔记的清单模式并通知外部监听者。
|
|
|
setWidgetType方法
|
|
|
所属类:WorkingNote
|
|
|
功能:若传入的部件类型与当前记录的不同,则更新部件类型,并通过笔记对象设置对应属性值,用于设置笔记的部件类型。
|
|
|
setWidgetId方法
|
|
|
所属类:WorkingNote
|
|
|
功能:若传入的部件ID与当前记录的不同,则更新部件ID,并通过笔记对象设置对应属性值,用于设置笔记的部件ID。
|
|
|
setWorkingText方法
|
|
|
所属类:WorkingNote
|
|
|
功能:若传入的文本内容与当前记录的不同,则更新文本内容,并通过笔记对象设置对应文本数据,用于设置笔记的工作文本内容。
|
|
|
convertToCallNote方法
|
|
|
所属类:WorkingNote
|
|
|
功能:设置笔记的通话相关数据(如通话日期、电话号码)以及所属文件夹ID为通话记录文件夹ID,用于将普通笔记转换为通话记录类型的笔记。
|
|
|
hasClockAlert方法
|
|
|
所属类:WorkingNote
|
|
|
功能:通过判断提醒日期是否大于0,返回笔记是否设置了时钟提醒的布尔值,用于知晓笔记是否有提醒功能开启。
|
|
|
getContent方法
|
|
|
所属类:WorkingNote
|
|
|
功能:返回笔记的文本内容,用于获取笔记中记录的具体内容信息。
|
|
|
getAlertDate方法
|
|
|
所属类:WorkingNote
|
|
|
功能:返回笔记设置的提醒日期,用于获取笔记的提醒相关信息。
|
|
|
getModifiedDate方法
|
|
|
所属类:WorkingNote
|
|
|
功能:返回笔记的最后修改日期,用于了解笔记的修改时间情况。
|
|
|
getBgColorResId方法
|
|
|
所属类:WorkingNote
|
|
|
功能:通过调用NoteBgResources的对应方法,根据背景颜色ID获取笔记背景资源的ID,用于获取笔记对应的背景资源标识符。
|
|
|
getBgColorId方法
|
|
|
*/
|
|
|
|
|
|
package net.micode.notes.model;
|
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
import android.content.ContentUris;
|
|
|
import android.content.Context;
|
|
|
import android.database.Cursor;
|
|
|
import android.text.TextUtils;
|
|
|
import android.util.Log;
|
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
import net.micode.notes.data.Notes.CallNote;
|
|
|
import net.micode.notes.data.Notes.DataColumns;
|
|
|
import net.micode.notes.data.Notes.DataConstants;
|
|
|
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
|
|
|
private long mNoteId;
|
|
|
// Note content
|
|
|
private String mContent;
|
|
|
// Note mode
|
|
|
private int mMode;
|
|
|
|
|
|
private long mAlertDate;
|
|
|
|
|
|
private long mModifiedDate;
|
|
|
|
|
|
private int mBgColorId;
|
|
|
|
|
|
private int mWidgetId;
|
|
|
|
|
|
private int mWidgetType;
|
|
|
|
|
|
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,
|
|
|
};
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
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
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
// 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(
|
|
|
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);
|
|
|
mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN);
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
private void loadNoteData() {
|
|
|
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)) {
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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 load(Context context, long id) {
|
|
|
return new WorkingNote(context, id, 0);
|
|
|
}
|
|
|
|
|
|
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
|
|
|
&& mNoteSettingStatusListener != null) {
|
|
|
mNoteSettingStatusListener.onWidgetChanged();
|
|
|
}
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public boolean existInDatabase() {
|
|
|
return mNoteId > 0;
|
|
|
}
|
|
|
|
|
|
private boolean isWorthSaving() {
|
|
|
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent))
|
|
|
|| (existInDatabase() && !mNote.isLocalModified())) {
|
|
|
return false;
|
|
|
} else {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) {
|
|
|
mNoteSettingStatusListener = l;
|
|
|
}
|
|
|
|
|
|
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 markDeleted(boolean mark) {
|
|
|
mIsDeleted = mark;
|
|
|
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
|
|
|
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) {
|
|
|
mNoteSettingStatusListener.onWidgetChanged();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void setBgColorId(int id) {
|
|
|
if (id != mBgColorId) {
|
|
|
mBgColorId = id;
|
|
|
if (mNoteSettingStatusListener != null) {
|
|
|
mNoteSettingStatusListener.onBackgroundColorChanged();
|
|
|
}
|
|
|
mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void setCheckListMode(int mode) {
|
|
|
if (mMode != mode) {
|
|
|
if (mNoteSettingStatusListener != null) {
|
|
|
mNoteSettingStatusListener.onCheckListModeChanged(mMode, mode);
|
|
|
}
|
|
|
mMode = mode;
|
|
|
mNote.setTextData(TextNote.MODE, String.valueOf(mMode));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void setWidgetType(int type) {
|
|
|
if (type != mWidgetType) {
|
|
|
mWidgetType = type;
|
|
|
mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void setWidgetId(int id) {
|
|
|
if (id != mWidgetId) {
|
|
|
mWidgetId = id;
|
|
|
mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public void setWorkingText(String text) {
|
|
|
if (!TextUtils.equals(mContent, text)) {
|
|
|
mContent = text;
|
|
|
mNote.setTextData(DataColumns.CONTENT, mContent);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
public int getBgColorResId() {
|
|
|
return NoteBgResources.getNoteBgResource(mBgColorId);
|
|
|
}
|
|
|
|
|
|
public int getBgColorId() {
|
|
|
return mBgColorId;
|
|
|
}
|
|
|
|
|
|
public int getTitleBgResId() {
|
|
|
return NoteBgResources.getNoteTitleBgResource(mBgColorId);
|
|
|
}
|
|
|
|
|
|
public int getCheckListMode() {
|
|
|
return mMode;
|
|
|
}
|
|
|
|
|
|
public long getNoteId() {
|
|
|
return mNoteId;
|
|
|
}
|
|
|
|
|
|
public long getFolderId() {
|
|
|
return mFolderId;
|
|
|
}
|
|
|
|
|
|
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);
|
|
|
}
|
|
|
}
|