You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Notes/src/ui/NoteItemData.java

346 lines
12 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* 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.ui;
import android.content.Context;
import android.database.Cursor;
import android.text.TextUtils;
import net.micode.notes.data.Contact;
import net.micode.notes.data.Notes;
import net.micode.notes.data.Notes.NoteColumns;
import net.micode.notes.tool.DataUtils;
/**
* 笔记列表项的数据模型类负责从数据库Cursor中读取数据并封装成Java对象
* 包含笔记的基本属性、通话记录信息和列表位置状态
*/
public class NoteItemData {
/**
* 数据库查询的列投影,定义需要从数据库读取的字段
*/
static final String [] PROJECTION = new String [] {
NoteColumns.ID, // 笔记ID
NoteColumns.ALERTED_DATE, // 提醒日期
NoteColumns.BG_COLOR_ID, // 背景颜色ID
NoteColumns.CREATED_DATE, // 创建日期
NoteColumns.HAS_ATTACHMENT, // 是否有附件
NoteColumns.MODIFIED_DATE, // 修改日期
NoteColumns.NOTES_COUNT, // 文件夹包含的笔记数量
NoteColumns.PARENT_ID, // 父文件夹ID
NoteColumns.SNIPPET, // 笔记内容摘要
NoteColumns.TYPE, // 笔记类型
NoteColumns.WIDGET_ID, // 关联的小部件ID
NoteColumns.WIDGET_TYPE, // 小部件类型
};
/**
* 投影数组的列索引常量用于从Cursor中快速获取对应字段
*/
private static final int ID_COLUMN = 0; // 笔记ID
private static final int ALERTED_DATE_COLUMN = 1; // 提醒日期
private static final int BG_COLOR_ID_COLUMN = 2; // 背景颜色ID
private static final int CREATED_DATE_COLUMN = 3; // 创建日期
private static final int HAS_ATTACHMENT_COLUMN = 4; // 是否有附件
private static final int MODIFIED_DATE_COLUMN = 5; // 修改日期
private static final int NOTES_COUNT_COLUMN = 6; // 文件夹包含的笔记数量
private static final int PARENT_ID_COLUMN = 7; // 父文件夹ID
private static final int SNIPPET_COLUMN = 8; // 笔记内容摘要
private static final int TYPE_COLUMN = 9; // 笔记类型
private static final int WIDGET_ID_COLUMN = 10; // 小部件ID
private static final int WIDGET_TYPE_COLUMN = 11; // 小部件类型
// 笔记基本属性
private long mId; // 笔记唯一标识符
private long mAlertDate; // 提醒日期时间
private int mBgColorId; // 背景颜色ID
private long mCreatedDate; // 创建日期时间
private boolean mHasAttachment; // 是否包含附件
private long mModifiedDate; // 最后修改日期时间
private int mNotesCount; // 文件夹包含的笔记数量
private long mParentId; // 所属父文件夹ID
private String mSnippet; // 笔记内容摘要
private int mType; // 笔记类型(普通笔记/文件夹/系统文件夹)
private int mWidgetId; // 关联的小部件ID
private int mWidgetType; // 小部件类型
// 通话记录相关属性
private String mName; // 通话记录联系人姓名
private String mPhoneNumber; // 通话记录电话号码
// 列表位置状态
private boolean mIsLastItem; // 是否为列表最后一项
private boolean mIsFirstItem; // 是否为列表第一项
private boolean mIsOnlyOneItem; // 是否为列表唯一一项
private boolean mIsOneNoteFollowingFolder; // 是否是文件夹下的唯一笔记
private boolean mIsMultiNotesFollowingFolder; // 是否是文件夹下的多个笔记之一
/**
* 构造函数从Cursor中读取数据并初始化成员变量
* @param cursor 包含笔记数据的Cursor
*/
public NoteItemData(Context context, Cursor cursor) {
// 读取基本笔记属性
mId = cursor.getLong(ID_COLUMN);
mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN);
mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN);
mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN);
mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0) ? true : false;
mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN);
mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN);
mParentId = cursor.getLong(PARENT_ID_COLUMN);
mSnippet = cursor.getString(SNIPPET_COLUMN);
mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace(
NoteEditActivity.TAG_UNCHECKED, "");// 清除清单模式的标签标记
mType = cursor.getInt(TYPE_COLUMN);
mWidgetId = cursor.getInt(WIDGET_ID_COLUMN);
mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN);
// 处理通话记录相关属性
mPhoneNumber = "";
if (mParentId == Notes.ID_CALL_RECORD_FOLDER) {
// 获取通话记录的电话号码
mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId);
if (!TextUtils.isEmpty(mPhoneNumber)) {
// 获取联系人姓名
mName = Contact.getContact(context, mPhoneNumber);
if (mName == null) {
mName = mPhoneNumber; // 无联系人时显示电话号码
}
}
}
if (mName == null) {
mName = "";
}
// 检查并设置当前项在列表中的位置关系
checkPostion(cursor);
}
/**
* 检查当前项在列表中的位置关系,并设置相应的状态标志
*/
private void checkPostion(Cursor cursor) {
// 检查基本位置状态
mIsLastItem = cursor.isLast() ? true : false;
mIsFirstItem = cursor.isFirst() ? true : false;
mIsOnlyOneItem = (cursor.getCount() == 1);
mIsMultiNotesFollowingFolder = false;
mIsOneNoteFollowingFolder = false;
// 检查是否跟随在文件夹之后
if (mType == Notes.TYPE_NOTE && !mIsFirstItem) {
int position = cursor.getPosition();
// 移动到前一项检查
if (cursor.moveToPrevious()) {
// 如果前一项是文件夹或系统文件夹
if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER
|| cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) {
// 检查后续是否还有更多笔记
if (cursor.getCount() > (position + 1)) {
mIsMultiNotesFollowingFolder = true; // 文件夹后有多个笔记
} else {
mIsOneNoteFollowingFolder = true; // 文件夹后只有一个笔记
}
}
// 恢复到原位置
if (!cursor.moveToNext()) {
throw new IllegalStateException("cursor move to previous but can't move back");
}
}
}
}
/**
* 是否是文件夹下的唯一笔记
* @return true如果是文件夹下的唯一笔记否则false
*/
public boolean isOneFollowingFolder() {
return mIsOneNoteFollowingFolder;
}
/**
* 是否是文件夹下的多个笔记之一
* @return true如果是文件夹下的多个笔记之一否则false
*/
public boolean isMultiFollowingFolder() {
return mIsMultiNotesFollowingFolder;
}
/**
* 是否为列表最后一项
* @return true如果是最后一项否则false
*/
public boolean isLast() {
return mIsLastItem;
}
/**
* 获取通话记录联系人姓名
* @return 联系人姓名,无联系人时返回电话号码
*/
public String getCallName() {
return mName;
}
/**
* 是否为列表第一项
* @return true如果是第一项否则false
*/
public boolean isFirst() {
return mIsFirstItem;
}
/**
* 是否为列表唯一一项
* @return true如果是唯一一项否则false
*/
public boolean isSingle() {
return mIsOnlyOneItem;
}
/**
* 获取笔记ID
* @return 笔记唯一标识符
*/
public long getId() {
return mId;
}
/**
* 获取提醒日期
* @return 提醒日期时间戳(毫秒)
*/
public long getAlertDate() {
return mAlertDate;
}
/**
* 获取创建日期
* @return 创建日期时间戳(毫秒)
*/
public long getCreatedDate() {
return mCreatedDate;
}
/**
* 是否包含附件
* @return true如果包含附件否则false
*/
public boolean hasAttachment() {
return mHasAttachment;
}
/**
* 获取最后修改日期
* @return 最后修改日期时间戳(毫秒)
*/
public long getModifiedDate() {
return mModifiedDate;
}
/**
* 获取背景颜色ID
* @return 背景颜色ID
*/
public int getBgColorId() {
return mBgColorId;
}
/**
* 获取父文件夹ID
* @return 所属父文件夹ID
*/
public long getParentId() {
return mParentId;
}
/**
* 获取文件夹包含的笔记数量
* @return 笔记数量
*/
public int getNotesCount() {
return mNotesCount;
}
/**
* 获取文件夹ID与getParentId()相同)
* @return 文件夹ID
*/
public long getFolderId () {
return mParentId;
}
/**
* 获取笔记类型
* @return 笔记类型(普通笔记/文件夹/系统文件夹)
*/
public int getType() {
return mType;
}
/**
* 获取小部件类型
* @return 小部件类型
*/
public int getWidgetType() {
return mWidgetType;
}
/**
* 获取关联的小部件ID
* @return 小部件ID
*/
public int getWidgetId() {
return mWidgetId;
}
/**
* 获取笔记内容摘要
* @return 内容摘要
*/
public String getSnippet() {
return mSnippet;
}
/**
* 是否设置了提醒
* @return true如果设置了提醒否则false
*/
public boolean hasAlert() {
return (mAlertDate > 0);
}
/**
* 是否为通话记录
* @return true如果是通话记录否则false
*/
public boolean isCallRecord() {
return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
}
/**
* 静态方法从Cursor中直接获取笔记类型
* @param cursor 包含笔记数据的Cursor
* @return 笔记类型
*/
public static int getNoteType(Cursor cursor) {
return cursor.getInt(TYPE_COLUMN);
}
}