|
|
/*
|
|
|
* 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.
|
|
|
*/
|
|
|
|
|
|
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;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
public class WorkingNote {
|
|
|
// 笔记对象
|
|
|
private Note mNote;
|
|
|
// 笔记ID
|
|
|
private long mNoteId;
|
|
|
// 笔记内容
|
|
|
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 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;
|
|
|
|
|
|
// 新建笔记构造函数
|
|
|
private WorkingNote(Context context, long folderId) {
|
|
|
mContext = context; // 保存上下文
|
|
|
mAlertDate = 0; // 初始化提醒日期为0
|
|
|
mModifiedDate = System.currentTimeMillis(); // 获取当前时间作为修改日期
|
|
|
mFolderId = folderId; // 设置文件夹ID
|
|
|
mNote = new Note(); // 创建新的笔记对象
|
|
|
mNoteId = 0; // 初始化笔记ID为0
|
|
|
mIsDeleted = false; // 初始化删除标志为false
|
|
|
mMode = 0; // 初始化模式为0
|
|
|
mWidgetType = Notes.TYPE_WIDGET_INVALIDE; // 初始化小部件类型为无效
|
|
|
}
|
|
|
|
|
|
// 已存在笔记构造函数
|
|
|
private WorkingNote(Context context, long noteId, long folderId) {
|
|
|
mContext = context; // 保存上下文
|
|
|
mNoteId = noteId; // 设置笔记ID
|
|
|
mFolderId = folderId; // 设置文件夹ID
|
|
|
mIsDeleted = false; // 初始化删除标志为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); // 获取父ID
|
|
|
mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN); // 获取背景颜色ID
|
|
|
mWidgetId = cursor.getInt(NOTE_WIDGET_ID_COLUMN); // 获取小部件ID
|
|
|
mWidgetType = cursor.getInt(NOTE_WIDGET_TYPE_COLUMN); // 获取小部件类型
|
|
|
mAlertDate = cursor.getLong(NOTE_ALERTED_DATE_COLUMN); // 获取提醒日期
|
|
|
mModifiedDate = cursor.getLong(NOTE_MODIFIED_DATE_COLUMN); // 获取修改日期
|
|
|
}
|
|
|
cursor.close(); // 关闭Cursor
|
|
|
} 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)); // 设置文本数据ID
|
|
|
} else if (DataConstants.CALL_NOTE.equals(type)) { // 如果是通话笔记
|
|
|
mNote.setCallDataId(cursor.getLong(DATA_ID_COLUMN)); // 设置通话数据ID
|
|
|
} else {
|
|
|
Log.d(TAG, "Wrong note type with type:" + type); // 记录错误
|
|
|
}
|
|
|
} while (cursor.moveToNext()); // 遍历所有结果
|
|
|
}
|
|
|
cursor.close(); // 关闭Cursor
|
|
|
} 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); // 设置默认背景颜色ID
|
|
|
note.setWidgetId(widgetId); // 设置小部件ID
|
|
|
note.setWidgetType(widgetType); // 设置小部件类型
|
|
|
return note; // 返回新创建的笔记
|
|
|
}
|
|
|
|
|
|
// 根据ID加载笔记
|
|
|
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) { // 创建新笔记ID
|
|
|
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; // 返回失败
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 检查笔记是否存在于数据库中
|
|
|
public boolean existInDatabase() {
|
|
|
return mNoteId > 0; // 如果笔记ID大于0,则存在
|
|
|
}
|
|
|
|
|
|
// 检查笔记是否值得保存
|
|
|
private boolean isWorthSaving() {
|
|
|
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent))
|
|
|
|| (existInDatabase() && !mNote.isLocalModified())) {
|
|
|
return false; // 如果被删除或内容为空,返回false
|
|
|
} else {
|
|
|
return true; // 返回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(); // 通知小部件已更改
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设置背景颜色ID
|
|
|
public void setBgColorId(int id) {
|
|
|
if (id != mBgColorId) {
|
|
|
mBgColorId = id; // 更新背景颜色ID
|
|
|
if (mNoteSettingStatusListener != null) {
|
|
|
mNoteSettingStatusListener.onBackgroundColorChanged(); // 通知背景颜色已更改
|
|
|
}
|
|
|
mNote.setNoteValue(NoteColumns.BG_COLOR_ID, String.valueOf(id)); // 设置笔记的背景颜色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)); // 设置笔记的小部件类型
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设置小部件ID
|
|
|
public void setWidgetId(int id) {
|
|
|
if (id != mWidgetId) {
|
|
|
mWidgetId = id; // 更新小部件ID
|
|
|
mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); // 设置笔记的小部件ID
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 设置工作文本
|
|
|
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)); // 设置父ID为通话记录文件夹ID
|
|
|
}
|
|
|
|
|
|
// 检查是否有提醒
|
|
|
public boolean hasClockAlert() {
|
|
|
return (mAlertDate > 0); // 如果提醒日期大于0,返回true
|
|
|
}
|
|
|
|
|
|
// 获取笔记内容
|
|
|
public String getContent() {
|
|
|
return mContent; // 返回笔记内容
|
|
|
}
|
|
|
|
|
|
// 获取提醒日期
|
|
|
public long getAlertDate() {
|
|
|
return mAlertDate; // 返回提醒日期
|
|
|
}
|
|
|
|
|
|
// 获取修改日期
|
|
|
public long getModifiedDate() {
|
|
|
return mModifiedDate; // 返回修改日期
|
|
|
}
|
|
|
|
|
|
// 获取背景颜色资源ID
|
|
|
public int getBgColorResId() {
|
|
|
return NoteBgResources.getNoteBgResource(mBgColorId); // 返回背景颜色资源ID
|
|
|
}
|
|
|
|
|
|
// 获取背景颜色ID
|
|
|
public int getBgColorId() {
|
|
|
return mBgColorId; // 返回背景颜色ID
|
|
|
}
|
|
|
|
|
|
// 获取标题背景资源ID
|
|
|
public int getTitleBgResId() {
|
|
|
return NoteBgResources.getNoteTitleBgResource(mBgColorId); // 返回标题背景资源ID
|
|
|
}
|
|
|
|
|
|
// 获取检查列表模式
|
|
|
public int getCheckListMode() {
|
|
|
return mMode; // 返回模式
|
|
|
}
|
|
|
|
|
|
// 获取笔记ID
|
|
|
public long getNoteId() {
|
|
|
return mNoteId; // 返回笔记ID
|
|
|
}
|
|
|
|
|
|
// 获取文件夹ID
|
|
|
public long getFolderId() {
|
|
|
return mFolderId; // 返回文件夹ID
|
|
|
}
|
|
|
|
|
|
// 获取小部件ID
|
|
|
public int getWidgetId() {
|
|
|
return mWidgetId; // 返回小部件ID
|
|
|
}
|
|
|
|
|
|
// 获取小部件类型
|
|
|
public int getWidgetType() {
|
|
|
return mWidgetType; // 返回小部件类型
|
|
|
}
|
|
|
|
|
|
// 笔记设置改变监听器接口
|
|
|
public interface NoteSettingChangedListener {
|
|
|
/**
|
|
|
* 当当前笔记的背景颜色刚刚改变时调用
|
|
|
*/
|
|
|
void onBackgroundColorChanged();
|
|
|
|
|
|
/**
|
|
|
* 当用户设置时钟时调用
|
|
|
*/
|
|
|
void onClockAlertChanged(long date, boolean set);
|
|
|
|
|
|
/**
|
|
|
* 当用户从小部件创建笔记时调用
|
|
|
*/
|
|
|
void onWidgetChanged();
|
|
|
|
|
|
/**
|
|
|
* 当在检查列表模式和普通模式之间切换时调用
|
|
|
* @param oldMode 是切换前的模式
|
|
|
* @param newMode 是切换后的新模式
|
|
|
*/
|
|
|
void onCheckListModeChanged(int oldMode, int newMode);
|
|
|
}
|
|
|
}
|
|
|
|