/* * 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; /** android.util.Log常用的方法有以下5个:Log.v() Log.d() Log.i() Log.w() 以及 Log.e() 。根据首字母对应VERBOSE,DEBUG,INFO, WARN,ERROR。 Log.v 的调试颜色为黑色的,任何消息都会输出,这里的v代表verbose啰嗦的意思,平时使用就是Log.v("",""); Log.d的输出颜色是蓝色的,仅输出debug调试的意思,但他会输出上层的信息,过滤起来可以通过DDMS的Logcat标签来选择. Log.i的输出为绿色,一般提示性的消息information,它不会输出Log.v和Log.d的信息,但会显示i、w和e的信息 Log.w的意思为橙色,可以看作为warning警告,一般需要我们注意优化Android代码,同时选择它后还会输出Log.e的信息。 Log.e为红色,可以想到error错误,这里仅显示红色的错误信息,这些错误就需要我们认真的分析,查看栈的信息了。 */ 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, };//新建字符串数组DATA_PROJECTION 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 };//新建字符串数组NOTE_PROJECTION 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() {//加载便签调用query函数找到第一个项目 Cursor cursor = mContext.getContentResolver().query( ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mNoteId), NOTE_PROJECTION, null, null, null); //query是一个查询参数类,封装了查询条件,分页,排序等功能 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()); }//do-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()) {//调用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();//调用mNoteSettingStatusListener } } public void setBgColorId(int id) {//shezhi背景颜色 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) {//设置窗口小部件ID if (id != mWidgetId) { mWidgetId = id; mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId)); } } public void setWorkingText(String text) {//设置WorkingText if (!TextUtils.equals(mContent, text)) { mContent = text; mNote.setTextData(DataColumns.CONTENT, mContent); } } public void convertToCallNote(String phoneNumber, long callDate) {//转变电话号码的信息:phoneNumber,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() {//获取背景颜色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 {//创建接口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); } }