diff --git a/1.cpp b/1.cpp deleted file mode 100644 index 265d60b..0000000 --- a/1.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -int main() -{ - -} - diff --git a/WorkingNote.java b/WorkingNote.java new file mode 100644 index 0000000..887cc61 --- /dev/null +++ b/WorkingNote.java @@ -0,0 +1,526 @@ +/* + * 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 com.example.notesmaster.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 com.example.notesmaster.data.Notes; +import com.example.notesmaster.data.Notes.CallNote; +import com.example.notesmaster.data.Notes.DataColumns; +import com.example.notesmaster.data.Notes.DataConstants; +import com.example.notesmaster.data.Notes.NoteColumns; +import com.example.notesmaster.data.Notes.TextNote; +import com.example.notesmaster.tool.ResourceParser.NoteBgResources; + + +/** + * 工作笔记类 - 用于管理当前正在编辑的笔记 + * 负责笔记的加载、修改、保存等操作 + */ +public class WorkingNote { + // 当前工作的笔记对象 + private Note mNote; + // 笔记ID + private long mNoteId; + // 笔记内容 + private String mContent; + // 笔记模式(普通模式或清单模式) + private int mMode; + + // 提醒日期 + private long mAlertDate; + + // 最后修改日期 + private long mModifiedDate; + + // 背景颜色ID + private int mBgColorId; + + // 桌面小部件ID + private int mWidgetId; + + // 桌面小部件类型 + private int mWidgetType; + + // 文件夹ID + 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, // 数据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; + 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 是否保存成功 + */ + 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 true表示已存在,false表示是新笔记 + */ + public boolean existInDatabase() { + return mNoteId > 0; + } + + /** + * 判断笔记是否值得保存 + * 不值得保存的情况:已删除、新笔记且内容为空、已存在但未修改 + * @return true表示值得保存 + */ + private boolean isWorthSaving() { + if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent)) + || (existInDatabase() && !mNote.isLocalModified())) { + return false; + } else { + return true; + } + } + + /** + * 设置笔记状态变化监听器 + * @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 true表示已删除,false表示未删除 + */ + public void markDeleted(boolean mark) { + mIsDeleted = mark; + // 如果有小部件,通知更新 + if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID + && mWidgetType != Notes.TYPE_WIDGET_INVALIDE && mNoteSettingStatusListener != null) { + mNoteSettingStatusListener.onWidgetChanged(); + } + } + + /** + * 设置笔记背景颜色 + * @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 模式值(0=普通模式,1=清单模式) + */ + 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)); + } + + /** + * 检查是否有闹钟提醒 + * @return true表示有提醒,false表示无提醒 + */ + public boolean hasClockAlert() { + return (mAlertDate > 0 ? true : false); + } + + /** + * 获取笔记内容 + * @return 笔记文本内容 + */ + public String getContent() { + return mContent; + } + + /** + * 获取提醒日期 + * @return 提醒日期(毫秒时间戳) + */ + public long getAlertDate() { + return mAlertDate; + } + + /** + * 获取最后修改日期 + * @return 修改日期(毫秒时间戳) + */ + public long getModifiedDate() { + return mModifiedDate; + } + + /** + * 获取背景颜色资源ID + * @return 背景颜色的资源ID + */ + public int getBgColorResId() { + return NoteBgResources.getNoteBgResource(mBgColorId); + } + + /** + * 获取背景颜色ID + * @return 背景颜色ID + */ + public int getBgColorId() { + return mBgColorId; + } + + /** + * 获取标题栏背景颜色资源ID + * @return 标题栏背景的资源ID + */ + public int getTitleBgResId() { + return NoteBgResources.getNoteTitleBgResource(mBgColorId); + } + + /** + * 获取当前模式(普通模式或清单模式) + * @return 模式值 + */ + public int getCheckListMode() { + return mMode; + } + + /** + * 获取笔记ID + * @return 笔记ID + */ + public long getNoteId() { + return mNoteId; + } + + /** + * 获取所属文件夹ID + * @return 文件夹ID + */ + public long getFolderId() { + return mFolderId; + } + + /** + * 获取关联的桌面小部件ID + * @return 小部件ID + */ + public int getWidgetId() { + return mWidgetId; + } + + /** + * 获取桌面小部件类型 + * @return 小部件类型 + */ + public int getWidgetType() { + return mWidgetType; + } + + /** + * 笔记设置变化监听器接口 + * 用于监听笔记的各种属性变化事件 + */ + public interface NoteSettingChangedListener { + /** + * 当笔记的背景颜色改变时调用 + */ + void onBackgroundColorChanged(); + + /** + * 当用户设置或取消闹钟提醒时调用 + * @param date 提醒日期 + * @param set true表示设置提醒,false表示取消提醒 + */ + void onClockAlertChanged(long date, boolean set); + + /** + * 当从小部件创建笔记或小部件内容需要更新时调用 + */ + void onWidgetChanged(); + + /** + * 当在普通模式和清单模式之间切换时调用 + * @param oldMode 切换前的模式 + * @param newMode 切换后的新模式 + */ + void onCheckListModeChanged(int oldMode, int newMode); + } +}