|
|
/*
|
|
|
* 版权所有 (c) 2010-2011,The MiCode 开源社区 (www.micode.net)
|
|
|
*
|
|
|
* 本软件根据 Apache 许可证 2.0 版("许可证")发布;
|
|
|
* 除非符合许可证,否则不得使用此文件。
|
|
|
* 您可以在以下网址获取许可证副本:
|
|
|
*
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
*
|
|
|
* 除非法律要求或书面同意,软件
|
|
|
* 根据许可证分发的内容按"原样"提供,
|
|
|
* 不附带任何明示或暗示的保证或条件。
|
|
|
* 请参阅许可证,了解有关权限和限制的具体语言。
|
|
|
*/
|
|
|
|
|
|
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;
|
|
|
|
|
|
/**
|
|
|
* 笔记列表项数据模型
|
|
|
* 功能:
|
|
|
* 1. 从Cursor中提取笔记/文件夹/通话记录的数据
|
|
|
* 2. 提供格式化的显示数据(如联系人姓名、摘要内容)
|
|
|
* 3. 判断笔记在列表中的位置状态(用于背景样式处理)
|
|
|
* 4. 提供便捷的数据访问方法
|
|
|
*/
|
|
|
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;
|
|
|
private static final int ALERTED_DATE_COLUMN = 1;
|
|
|
private static final int BG_COLOR_ID_COLUMN = 2;
|
|
|
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;
|
|
|
private static final int SNIPPET_COLUMN = 8;
|
|
|
private static final int TYPE_COLUMN = 9;
|
|
|
private static final int WIDGET_ID_COLUMN = 10;
|
|
|
private static final int WIDGET_TYPE_COLUMN = 11;
|
|
|
|
|
|
// 核心数据字段
|
|
|
private long mId; // 笔记/文件夹ID
|
|
|
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; // 类型(Notes.TYPE_NOTE/Notes.TYPE_FOLDER等)
|
|
|
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 context 上下文
|
|
|
* @param cursor 包含笔记数据的Cursor
|
|
|
*/
|
|
|
public NoteItemData(Context context, Cursor 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);
|
|
|
mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN);
|
|
|
mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN);
|
|
|
mParentId = cursor.getLong(PARENT_ID_COLUMN);
|
|
|
mSnippet = cursor.getString(SNIPPET_COLUMN);
|
|
|
mType = cursor.getInt(TYPE_COLUMN);
|
|
|
mWidgetId = cursor.getInt(WIDGET_ID_COLUMN);
|
|
|
mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN);
|
|
|
|
|
|
// 处理摘要内容(移除编辑标记)
|
|
|
mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace(
|
|
|
NoteEditActivity.TAG_UNCHECKED, "");
|
|
|
|
|
|
// 通话记录特殊处理:获取联系人信息
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 检查并设置列表项的位置状态
|
|
|
* 用于确定列表项的背景样式(圆角、分隔线等)
|
|
|
* @param cursor 当前Cursor
|
|
|
*/
|
|
|
private void checkPostion(Cursor cursor) {
|
|
|
mIsLastItem = cursor.isLast(); // 是否为最后一项
|
|
|
mIsFirstItem = cursor.isFirst(); // 是否为第一项
|
|
|
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; // 无后续笔记
|
|
|
}
|
|
|
}
|
|
|
// 恢复Cursor位置
|
|
|
if (!cursor.moveToNext()) {
|
|
|
throw new IllegalStateException("cursor move to previous but can't move back");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 位置状态判断方法(用于UI渲染时选择合适的背景样式)
|
|
|
public boolean isOneFollowingFolder() { return mIsOneNoteFollowingFolder; }
|
|
|
public boolean isMultiFollowingFolder() { return mIsMultiNotesFollowingFolder; }
|
|
|
public boolean isLast() { return mIsLastItem; }
|
|
|
public boolean isFirst() { return mIsFirstItem; }
|
|
|
public boolean isSingle() { return mIsOnlyOneItem; }
|
|
|
|
|
|
// 数据访问方法
|
|
|
public long getId() { return mId; }
|
|
|
public long getAlertDate() { return mAlertDate; }
|
|
|
public int getBgColorId() { return mBgColorId; }
|
|
|
public long getCreatedDate() { return mCreatedDate; }
|
|
|
public boolean hasAttachment() { return mHasAttachment; }
|
|
|
public long getModifiedDate() { return mModifiedDate; }
|
|
|
public int getNotesCount() { return mNotesCount; }
|
|
|
public long getParentId() { return mParentId; }
|
|
|
public String getSnippet() { return mSnippet; }
|
|
|
public int getType() { return mType; }
|
|
|
public int getWidgetId() { return mWidgetId; }
|
|
|
public int getWidgetType() { return mWidgetType; }
|
|
|
|
|
|
// 通话记录专用方法
|
|
|
public String getCallName() { return mName; }
|
|
|
public boolean isCallRecord() {
|
|
|
return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
|
|
|
}
|
|
|
|
|
|
// 辅助方法:获取笔记类型(静态方法,无需实例化对象)
|
|
|
public static int getNoteType(Cursor cursor) {
|
|
|
return cursor.getInt(TYPE_COLUMN);
|
|
|
}
|
|
|
} |