|
|
/*
|
|
|
* 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;
|
|
|
|
|
|
|
|
|
public class WorkingNote {
|
|
|
// 工作笔记类,处理笔记的创建、加载和修改
|
|
|
|
|
|
// Note for the working note
|
|
|
// 工作笔记的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;
|
|
|
// 修改日期
|
|
|
|
|
|
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, // 数据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;
|
|
|
// 模式列索引(对应DATA1字段)
|
|
|
|
|
|
// 笔记投影列索引
|
|
|
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;
|
|
|
// 保存上下文
|
|
|
mAlertDate = 0;
|
|
|
// 提醒日期初始为0
|
|
|
mModifiedDate = System.currentTimeMillis();
|
|
|
// 修改日期为当前时间
|
|
|
mFolderId = folderId;
|
|
|
// 设置文件夹ID
|
|
|
mNote = new Note();
|
|
|
// 创建Note对象
|
|
|
mNoteId = 0;
|
|
|
// 笔记ID初始为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;
|
|
|
// 设置笔记ID
|
|
|
mFolderId = folderId;
|
|
|
// 设置文件夹ID
|
|
|
mIsDeleted = false;
|
|
|
// 未删除
|
|
|
mNote = new Note();
|
|
|
// 创建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();
|
|
|
// 关闭游标
|
|
|
} 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);
|
|
|
// 获取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));
|
|
|
// 设置文本数据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();
|
|
|
// 关闭游标
|
|
|
} 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);
|
|
|
// 设置小部件ID
|
|
|
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) {
|
|
|
// 获取新笔记ID失败
|
|
|
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;
|
|
|
// 笔记ID大于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) {
|
|
|
// 设置背景颜色ID
|
|
|
if (id != mBgColorId) {
|
|
|
// 如果颜色ID发生变化
|
|
|
mBgColorId = id;
|
|
|
// 更新背景颜色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) {
|
|
|
// 设置小部件ID
|
|
|
if (id != mWidgetId) {
|
|
|
// 如果ID发生变化
|
|
|
mWidgetId = id;
|
|
|
// 更新小部件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);
|
|
|
// 提醒日期大于0表示有提醒
|
|
|
}
|
|
|
|
|
|
// 以下为获取属性的方法
|
|
|
public String getContent() {
|
|
|
// 获取内容
|
|
|
return mContent;
|
|
|
}
|
|
|
|
|
|
public long getAlertDate() {
|
|
|
// 获取提醒日期
|
|
|
return mAlertDate;
|
|
|
}
|
|
|
|
|
|
public long getModifiedDate() {
|
|
|
// 获取修改日期
|
|
|
return mModifiedDate;
|
|
|
}
|
|
|
|
|
|
public int getBgColorResId() {
|
|
|
// 获取背景颜色资源ID
|
|
|
return NoteBgResources.getNoteBgResource(mBgColorId);
|
|
|
// 通过资源工具类获取
|
|
|
}
|
|
|
|
|
|
public int getBgColorId() {
|
|
|
// 获取背景颜色ID
|
|
|
return mBgColorId;
|
|
|
}
|
|
|
|
|
|
public int getTitleBgResId() {
|
|
|
// 获取标题背景颜色资源ID
|
|
|
return NoteBgResources.getNoteTitleBgResource(mBgColorId);
|
|
|
// 通过资源工具类获取
|
|
|
}
|
|
|
|
|
|
public int getCheckListMode() {
|
|
|
// 获取清单模式
|
|
|
return mMode;
|
|
|
}
|
|
|
|
|
|
public long getNoteId() {
|
|
|
// 获取笔记ID
|
|
|
return mNoteId;
|
|
|
}
|
|
|
|
|
|
public long getFolderId() {
|
|
|
// 获取文件夹ID
|
|
|
return mFolderId;
|
|
|
}
|
|
|
|
|
|
public int getWidgetId() {
|
|
|
// 获取小部件ID
|
|
|
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
|
|
|
*/
|
|
|
// 在清单模式和普通模式之间切换时调用
|
|
|
// oldMode是变化前的模式
|
|
|
// newMode是新模式
|
|
|
void onCheckListModeChanged(int oldMode, int newMode);
|
|
|
}
|
|
|
} |