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.
rjgc/ui/NoteItemData.java

345 lines
10 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;
// ... existing code ...
/**
* 笔记项数据模型
*
* 【功能说明】
* 1. 封装笔记列表中的单个笔记项的所有数据
* 2. 从数据库游标中提取并缓存笔记信息
* 3. 提供便捷的访问方法来获取笔记属性
* 4. 支持判断笔记在列表中的位置关系(是否为首项、末项等)
*/
public class NoteItemData {
// 查询投影:包含笔记的所有必要字段
static final String [] PROJECTION = new String [] {
NoteColumns.ID,
NoteColumns.ALERTED_DATE,
NoteColumns.BG_COLOR_ID,
NoteColumns.CREATED_DATE,
NoteColumns.HAS_ATTACHMENT,
NoteColumns.MODIFIED_DATE,
NoteColumns.NOTES_COUNT,
NoteColumns.PARENT_ID,
NoteColumns.SNIPPET,
NoteColumns.TYPE,
NoteColumns.WIDGET_ID,
NoteColumns.WIDGET_TYPE,
};
// 列索引常量定义
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;
private long mAlertDate;
private int mBgColorId;
private long mCreatedDate;
private boolean mHasAttachment;
private long mModifiedDate;
private int mNotesCount;
private long mParentId;
private String mSnippet;
private int mType;
private int mWidgetId;
private int mWidgetType;
private String mName;
private String mPhoneNumber;
// 位置标记用于UI渲染时判断显示样式
private boolean mIsLastItem;
private boolean mIsFirstItem;
private boolean mIsOnlyOneItem;
private boolean mIsOneNoteFollowingFolder;
private boolean mIsMultiNotesFollowingFolder;
/**
* 构造函数:从数据库游标中加载笔记数据
* @param context 上下文对象
* @param 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);
}
/**
* 检查并设置笔记在列表中的位置关系
* 用于UI渲染时添加适当的分隔线和圆角效果
* @param 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表示是单个笔记跟随文件夹
*/
public boolean isOneFollowingFolder() {
return mIsOneNoteFollowingFolder;
}
/**
* 判断是否为紧跟在文件夹后的多个笔记之一
* @return true表示是多个笔记跟随文件夹
*/
public boolean isMultiFollowingFolder() {
return mIsMultiNotesFollowingFolder;
}
/**
* 判断是否为列表最后一项
* @return true表示是最后一项
*/
public boolean isLast() {
return mIsLastItem;
}
/**
* 获取通话记录对应的联系人名称
* @return 联系人名称或电话号码
*/
public String getCallName() {
return mName;
}
/**
* 判断是否为列表第一项
* @return true表示是第一项
*/
public boolean isFirst() {
return mIsFirstItem;
}
/**
* 判断是否为列表中唯一的一项
* @return true表示是唯一项
*/
public boolean isSingle() {
return mIsOnlyOneItem;
}
/**
* 获取笔记ID
* @return 笔记ID
*/
public long getId() {
return mId;
}
/**
* 获取提醒日期
* @return 提醒日期的时间戳毫秒0表示未设置提醒
*/
public long getAlertDate() {
return mAlertDate;
}
/**
* 获取创建日期
* @return 创建日期的时间戳(毫秒)
*/
public long getCreatedDate() {
return mCreatedDate;
}
/**
* 判断是否有附件
* @return true表示有附件
*/
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表示已设置提醒
*/
public boolean hasAlert() {
return (mAlertDate > 0);
}
/**
* 判断是否为通话记录便签
* @return true表示是通话记录便签
*/
public boolean isCallRecord() {
return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
}
/**
* 静态方法:从游标中获取笔记类型
* @param cursor 数据库游标
* @return 笔记类型
*/
public static int getNoteType(Cursor cursor) {
return cursor.getInt(TYPE_COLUMN);
}
}