|
|
/*
|
|
|
* 版权所有 (c) 2010-2011,MiCode 开源社区 (www.micode.net)
|
|
|
* 根据 Apache 许可证 2.0 版本("许可证")授权;
|
|
|
* 除非符合许可证的规定,否则不得使用本文件。
|
|
|
* 您可以从以下网址获取许可证副本:
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
* 除非适用法律要求或书面同意,本软件按"原样"分发,
|
|
|
* 没有任何明示或暗示的保证或条件。
|
|
|
* 详见许可证中规定的权限和限制。
|
|
|
* (注:这是一份标准的Apache许可证2.0版本的开源声明)
|
|
|
*/
|
|
|
// 定义工作笔记模型的包路径
|
|
|
package net.micode.notes.model;
|
|
|
|
|
|
// 导入所需的Android类库
|
|
|
import android.appwidget.AppWidgetManager; // 应用小部件管理
|
|
|
import android.content.ContentUris; // URI工具类
|
|
|
import android.content.Context; // 上下文对象
|
|
|
import android.database.Cursor; // 数据库查询结果
|
|
|
import android.os.Build;
|
|
|
import android.text.TextUtils; // 文本处理工具
|
|
|
import android.util.Log; // 日志工具
|
|
|
|
|
|
// 导入项目相关的数据类
|
|
|
import androidx.annotation.RequiresApi;
|
|
|
|
|
|
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 {
|
|
|
// 成员变量定义
|
|
|
private final Note mNote; // 基础笔记对象
|
|
|
private long mNoteId; // 笔记ID
|
|
|
private String mContent; // 笔记内容
|
|
|
private int mMode; // 笔记模式(普通/清单模式)
|
|
|
private long mAlertDate; // 提醒时间
|
|
|
private long mModifiedDate; // 最后修改时间
|
|
|
private int mBgColorId; // 背景颜色ID
|
|
|
private int mWidgetId; // 关联小部件ID
|
|
|
private int mWidgetType; // 小部件类型
|
|
|
private long mFolderId; // 所在文件夹ID
|
|
|
private final Context mContext; // 上下文对象
|
|
|
private static final String TAG = "WorkingNote"; // 日志标签
|
|
|
private boolean mIsDeleted; // 是否已删除标记
|
|
|
private NoteSettingChangedListener mNoteSettingStatusListener; // 笔记设置变更监听器
|
|
|
|
|
|
// 数据表查询列定义
|
|
|
public static final String[] DATA_PROJECTION = new String[] {
|
|
|
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, // 父文件夹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; // 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; // 修改日期列索引
|
|
|
|
|
|
/**
|
|
|
* 创建新工作笔记的构造函数(私有)
|
|
|
* @param context 上下文对象
|
|
|
* @param folderId 所属文件夹ID
|
|
|
*/
|
|
|
private WorkingNote(Context context, long folderId) {
|
|
|
mContext = context;
|
|
|
mAlertDate = 0; // 初始无提醒
|
|
|
mModifiedDate = System.currentTimeMillis(); // 设置当前时间为修改时间
|
|
|
mFolderId = folderId; // 设置所属文件夹
|
|
|
mNote = new Note(); // 创建基础笔记对象
|
|
|
mNoteId = 0; // 新笔记ID为0
|
|
|
mIsDeleted = false; // 未删除状态
|
|
|
mMode = 0; // 默认普通模式
|
|
|
mWidgetType = Notes.TYPE_WIDGET_INVALIDE; // 无效小部件类型
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加载现有工作笔记的构造函数(私有)
|
|
|
* @param context 上下文对象
|
|
|
* @param noteId 笔记ID
|
|
|
* @param folderId 所属文件夹ID
|
|
|
*/
|
|
|
private WorkingNote(Context context, long noteId, long folderId) {
|
|
|
mContext = context;
|
|
|
mNoteId = noteId; // 设置笔记ID
|
|
|
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);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 创建新的空白笔记
|
|
|
* @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);
|
|
|
note.setBgColorId(defaultBgColorId); // 设置默认背景
|
|
|
note.setWidgetId(widgetId); // 设置小部件ID
|
|
|
note.setWidgetType(widgetType); // 设置小部件类型
|
|
|
return note;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 加载指定ID的笔记
|
|
|
* @param context 上下文对象
|
|
|
* @param id 笔记ID
|
|
|
* @return 加载的工作笔记对象
|
|
|
*/
|
|
|
public static WorkingNote load(Context context, long id) {
|
|
|
return new WorkingNote(context, id, 0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 保存笔记到数据库
|
|
|
* @return 是否保存成功
|
|
|
*/
|
|
|
@RequiresApi(api = Build.VERSION_CODES.R)
|
|
|
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);
|
|
|
|
|
|
// 如果有关联小部件,通知更新
|
|
|
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID
|
|
|
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE
|
|
|
&& mNoteSettingStatusListener != null) {
|
|
|
mNoteSettingStatusListener.onWidgetChanged();
|
|
|
}
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查笔记是否已存在于数据库中
|
|
|
* @return 是否存在
|
|
|
*/
|
|
|
public boolean existInDatabase() {
|
|
|
return mNoteId > 0;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查笔记是否值得保存
|
|
|
* @return 是否需要保存
|
|
|
*/
|
|
|
@RequiresApi(api = Build.VERSION_CODES.R)
|
|
|
private boolean isWorthSaving() {
|
|
|
// 已有笔记但未修改
|
|
|
return !mIsDeleted && // 已删除
|
|
|
(existInDatabase() || !TextUtils.isEmpty(mContent)) && // 新笔记且内容为空
|
|
|
(!existInDatabase() || mNote.isLocalModified());
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置笔记设置变更监听器
|
|
|
* @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();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置背景颜色ID
|
|
|
* @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;
|
|
|
mNote.setTextData(TextNote.MODE, String.valueOf(mMode));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置小部件类型
|
|
|
* @param type 类型值
|
|
|
*/
|
|
|
public void setWidgetType(int type) {
|
|
|
if (type != mWidgetType) {
|
|
|
mWidgetType = type;
|
|
|
mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置小部件ID
|
|
|
* @param id 小部件ID
|
|
|
*/
|
|
|
public void setWidgetId(int id) {
|
|
|
if (id != mWidgetId) {
|
|
|
mWidgetId = id;
|
|
|
mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 设置工作文本内容
|
|
|
* @param text 文本内容
|
|
|
*/
|
|
|
public void setWorkingText(String text) {
|
|
|
if (!TextUtils.equals(mContent, text)) {
|
|
|
mContent = text;
|
|
|
mNote.setTextData(DataColumns.CONTENT, mContent);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 将笔记转换为通话笔记
|
|
|
* @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));
|
|
|
}
|
|
|
|
|
|
// 以下是一系列获取笔记属性的方法
|
|
|
|
|
|
public boolean hasClockAlert() {
|
|
|
return (mAlertDate > 0);
|
|
|
}
|
|
|
|
|
|
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 {
|
|
|
/**
|
|
|
* 当笔记背景颜色变更时调用
|
|
|
*/
|
|
|
void onBackgroundColorChanged();
|
|
|
|
|
|
/**
|
|
|
* 当用户设置提醒时调用
|
|
|
* @param date 提醒时间
|
|
|
* @param set 是否设置
|
|
|
*/
|
|
|
void onClockAlertChanged(long date, boolean set);
|
|
|
|
|
|
/**
|
|
|
* 当用户从小部件创建笔记时调用
|
|
|
*/
|
|
|
void onWidgetChanged();
|
|
|
|
|
|
/**
|
|
|
* 当在清单模式和普通模式间切换时调用
|
|
|
* @param oldMode 原模式
|
|
|
* @param newMode 新模式
|
|
|
*/
|
|
|
void onCheckListModeChanged(int oldMode, int newMode);
|
|
|
}
|
|
|
}
|