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.

409 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.
*/
/**
* 文件: NoteItemData.java
* 描述: 便签列表项数据模型类
* 作用: 封装便签列表中单个便签或文件夹的数据信息,提供数据访问接口
* 功能:
* 1. 从数据库游标中提取便签数据
* 2. 提供便签属性的访问方法
* 3. 判断便签在列表中的位置状态(首项、末项等)
* 4. 处理通话记录便签的特殊逻辑
*/
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;
/**
* 便签列表项数据类
*
* 封装便签列表中单个便签或文件夹的数据信息
* 从数据库游标中提取数据,并提供各种属性的访问方法
* 处理便签在列表中的位置关系,支持不同类型便签的显示逻辑
*/
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,
};
/** 投影中ID列的索引 */
private static final int ID_COLUMN = 0;
/** 投影中提醒日期列的索引 */
private static final int ALERTED_DATE_COLUMN = 1;
/** 投影中背景颜色ID列的索引 */
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;
/** 投影中父ID列的索引 */
private static final int PARENT_ID_COLUMN = 7;
/** 投影中摘要列的索引 */
private static final int SNIPPET_COLUMN = 8;
/** 投影中类型列的索引 */
private static final int TYPE_COLUMN = 9;
/** 投影中小部件ID列的索引 */
private static final int WIDGET_ID_COLUMN = 10;
/** 投影中小部件类型列的索引 */
private static final int WIDGET_TYPE_COLUMN = 11;
/** 便签ID */
private long mId;
/** 提醒日期 */
private long mAlertDate;
/** 背景颜色ID */
private int mBgColorId;
/** 创建日期 */
private long mCreatedDate;
/** 是否有附件 */
private boolean mHasAttachment;
/** 修改日期 */
private long mModifiedDate;
/** 便签数量(对于文件夹) */
private int mNotesCount;
/** 父ID所属文件夹ID */
private long mParentId;
/** 便签摘要(显示内容) */
private String mSnippet;
/** 便签类型 */
private int mType;
/** 小部件ID */
private int mWidgetId;
/** 小部件类型 */
private int mWidgetType;
/** 联系人姓名(通话记录便签) */
private String mName;
/** 电话号码(通话记录便签) */
private String mPhoneNumber;
/** 是否为列表中的最后一项 */
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);
}
/**
* 检查便签在列表中的位置状态
* 确定便签是否为首项、末项、单项等
*
* @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否则返回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 便签ID
*/
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所属文件夹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));
}
/**
* 从游标中获取便签类型的静态方法
*
* @param cursor 包含便签数据的游标
* @return 便签类型
*/
public static int getNoteType(Cursor cursor) {
return cursor.getInt(TYPE_COLUMN);
}
}