|
|
|
|
@ -0,0 +1,368 @@
|
|
|
|
|
/*//当前活动便签项
|
|
|
|
|
* 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;//在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 {//声明一个workingnote类
|
|
|
|
|
// Note for the working note
|
|
|
|
|
private Note mNote;//声明一个Note类型的变量
|
|
|
|
|
// Note Id
|
|
|
|
|
private long mNoteId;//声明便签content
|
|
|
|
|
// Note content
|
|
|
|
|
private String mContent;//声明便签mode
|
|
|
|
|
// 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";//声明 DATA_PROJECTION字符串数组
|
|
|
|
|
|
|
|
|
|
private boolean mIsDeleted;//是否应该被删除
|
|
|
|
|
|
|
|
|
|
private NoteSettingChangedListener mNoteSettingStatusListener;//一个用来监听设置是否有变化的接口
|
|
|
|
|
|
|
|
|
|
public static final String[] DATA_PROJECTION = new String[] {//声明 NOTE_PROJECTION字符串数组
|
|
|
|
|
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;//以下6个常量表示便签投影的0-5列
|
|
|
|
|
|
|
|
|
|
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;//WorkingNote的构造函数
|
|
|
|
|
mAlertDate = 0;//默认提醒日期为0
|
|
|
|
|
mModifiedDate = System.currentTimeMillis();//系统现在的时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数
|
|
|
|
|
mFolderId = folderId;//默认最后一次修改日期为当前时间
|
|
|
|
|
mNote = new 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) {//加载Note
|
|
|
|
|
mContext = context;//调用query函数找到第一个条目
|
|
|
|
|
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) {//通过数据库调用query函数找到第一个条目
|
|
|
|
|
if (cursor.moveToFirst()) {//如果游标移动到第一行
|
|
|
|
|
mFolderId = cursor.getLong(NOTE_PARENT_ID_COLUMN);//存储各项信息
|
|
|
|
|
mBgColorId = cursor.getInt(NOTE_BG_COLOR_ID_COLUMN);//关闭cursor游标
|
|
|
|
|
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);//未能找到此note,返回异常
|
|
|
|
|
throw new IllegalArgumentException("Unable to find note with id " + mNoteId);//抛出异常,找不到NoteID
|
|
|
|
|
}
|
|
|
|
|
loadNoteData();//调用方法loadNoteData,导入便签的内容。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void loadNoteData() {//加载NoteData
|
|
|
|
|
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 while循环将便签数据全部读取出来
|
|
|
|
|
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));//否则,在日志中标志错误类型为note type错误
|
|
|
|
|
} else {//如果类型错误,则提示异常
|
|
|
|
|
Log.d(TAG, "Wrong note type with type:" + type);//再否则就是有错误
|
|
|
|
|
}
|
|
|
|
|
} while (cursor.moveToNext());//查阅所有项,直到为空
|
|
|
|
|
}
|
|
|
|
|
cursor.close();//查找失败时,在日志中记录错误信息为无法找到id号为mNoteId的便签
|
|
|
|
|
} else {//否则记录异常日志,没有ID为mNoteId的数据
|
|
|
|
|
Log.e(TAG, "No data with id:" + mNoteId);//否则记录异常日志,没有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);//根据环境和id创建一个空的便签
|
|
|
|
|
note.setBgColorId(defaultBgColorId);//设定相关属性
|
|
|
|
|
note.setWidgetId(widgetId);//设定widget id
|
|
|
|
|
note.setWidgetType(widgetType);//设置窗口类型
|
|
|
|
|
return note;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static WorkingNote load(Context context, long id) {//导入一个新的正在写入的便签(WorkingNote)
|
|
|
|
|
return new WorkingNote(context, id, 0);//返回便签
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public synchronized boolean saveNote() {//保存Note
|
|
|
|
|
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);//同步便签内容及便签id
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update widget content if there exist any widget of this note
|
|
|
|
|
*/
|
|
|
|
|
if (mWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID//判断是否存在widget可以上传
|
|
|
|
|
&& mWidgetType != Notes.TYPE_WIDGET_INVALIDE
|
|
|
|
|
&& mNoteSettingStatusListener != null) {
|
|
|
|
|
mNoteSettingStatusListener.onWidgetChanged();
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean existInDatabase() {
|
|
|
|
|
return mNoteId > 0;
|
|
|
|
|
}//是否在数据库中存在判断是否在数据库中存储过,直接判定id大小即可
|
|
|
|
|
|
|
|
|
|
private boolean isWorthSaving() {//判断是否需要保存
|
|
|
|
|
if (mIsDeleted || (!existInDatabase() && TextUtils.isEmpty(mContent))//被删除,或(不在数据库中 内容为空),或 本地已保存过
|
|
|
|
|
|| (existInDatabase() && !mNote.isLocalModified())) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
return true;//其余情况,要保存,返回true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setOnSettingStatusChangedListener(NoteSettingChangedListener l) {//设置常量
|
|
|
|
|
mNoteSettingStatusListener = l;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setAlertDate(long date, boolean set) {//设置AlertDate;若 mAlertDate与data不同,则更改mAlertDate并设定NoteValue
|
|
|
|
|
if (date != mAlertDate) {//发现date与mAlertDate不一致时,设置mAlertDate为date
|
|
|
|
|
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.onWidgetChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setBgColorId(int id) {//设定背景颜色
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
mMode = mode;//把当前便签蛇者为清单模式的便签
|
|
|
|
|
mNote.setTextData(TextNote.MODE, String.valueOf(mMode));//语句:重设文本数据,改变MODE项
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setWidgetType(int type) {//设定WidgetType
|
|
|
|
|
if (type != mWidgetType) {//设定条件
|
|
|
|
|
mWidgetType = type;//调用Note的setNoteValue方法更改WidgetType
|
|
|
|
|
mNote.setNoteValue(NoteColumns.WIDGET_TYPE, String.valueOf(mWidgetType));
|
|
|
|
|
}//调用Note的setNoteValue方法更改WidgetType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setWidgetId(int id) {//设定WidgetId
|
|
|
|
|
if (id != mWidgetId) {//设定条件
|
|
|
|
|
mWidgetId = id;//调用Note的setNoteValue方法更改WidgetId
|
|
|
|
|
mNote.setNoteValue(NoteColumns.WIDGET_ID, String.valueOf(mWidgetId));
|
|
|
|
|
}//调用Note的setNoteValue方法更改WidgetId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setWorkingText(String text) {//设定WorkingText
|
|
|
|
|
if (!TextUtils.equals(mContent, text)) {//判断条件
|
|
|
|
|
mContent = text;//调用Note的setTextData方法更改WorkingText
|
|
|
|
|
mNote.setTextData(DataColumns.CONTENT, mContent);//调用Note的setTextData方法更改WorkingText
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void convertToCallNote(String phoneNumber, long callDate) {//转变mNote的CallData及CallNote信息
|
|
|
|
|
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() {
|
|
|
|
|
return NoteBgResources.getNoteBgResource(mBgColorId);
|
|
|
|
|
}//获取背景颜色id
|
|
|
|
|
|
|
|
|
|
public int getBgColorId() {
|
|
|
|
|
return mBgColorId;
|
|
|
|
|
}//获取背景颜色id
|
|
|
|
|
|
|
|
|
|
public int getTitleBgResId() {
|
|
|
|
|
return NoteBgResources.getNoteTitleBgResource(mBgColorId);
|
|
|
|
|
}//获取标题背景颜色id
|
|
|
|
|
|
|
|
|
|
public int getCheckListMode() {
|
|
|
|
|
return mMode;
|
|
|
|
|
}//获取CheckListMode
|
|
|
|
|
|
|
|
|
|
public long getNoteId() {
|
|
|
|
|
return mNoteId;
|
|
|
|
|
}//获取便签id
|
|
|
|
|
|
|
|
|
|
public long getFolderId() {
|
|
|
|
|
return mFolderId;
|
|
|
|
|
}//获取文件夹id
|
|
|
|
|
|
|
|
|
|
public int getWidgetId() {
|
|
|
|
|
return mWidgetId;
|
|
|
|
|
}//获取WidgetType
|
|
|
|
|
|
|
|
|
|
public int getWidgetType() {
|
|
|
|
|
return mWidgetType;
|
|
|
|
|
}//创建接口 NoteSettingChangedListener,便签更新监视;为NoteEditActivity提供接口
|
|
|
|
|
|
|
|
|
|
public interface NoteSettingChangedListener {//该类用于侦听关于Note的设置的改变
|
|
|
|
|
/**
|
|
|
|
|
* 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);//mode的改变按键
|
|
|
|
|
}
|
|
|
|
|
}
|