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.
git.text/src/java/net/micode/notes/ui/NoteItemData.java

319 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;
/**
* 小米便签列表项的数据模型类封装单个便签的核心信息及列表位置属性用于支撑列表UI展示
*/
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, // 桌面插件类型
};
// 上述PROJECTION数组中各列的索引常量用于从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; // 便签唯一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便签TYPE_FOLDER文件夹等
private int mWidgetId; // 关联的桌面插件ID无则为无效值
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) {
// 若游标总数大于当前位置+1说明文件夹后有多个便签
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");
}
}
}
}
/**
* 是否为文件夹后的唯一便签项
*/
public boolean isOneFollowingFolder() {
return mIsOneNoteFollowingFolder;
}
/**
* 是否为文件夹后的多个便签项之一
*/
public boolean isMultiFollowingFolder() {
return mIsMultiNotesFollowingFolder;
}
/**
* 是否为列表最后一项
*/
public boolean isLast() {
return mIsLastItem;
}
/**
* 获取通话记录便签的联系人姓名(非通话记录返回空)
*/
public String getCallName() {
return mName;
}
/**
* 是否为列表第一项
*/
public boolean isFirst() {
return mIsFirstItem;
}
/**
* 是否为列表中唯一一项
*/
public boolean isSingle() {
return mIsOnlyOneItem;
}
/**
* 获取便签ID
*/
public long getId() {
return mId;
}
/**
* 获取提醒时间(毫秒时间戳)
*/
public long getAlertDate() {
return mAlertDate;
}
/**
* 获取创建时间(毫秒时间戳)
*/
public long getCreatedDate() {
return mCreatedDate;
}
/**
* 是否有附件
*/
public boolean hasAttachment() {
return mHasAttachment;
}
/**
* 获取最后修改时间(毫秒时间戳)
*/
public long getModifiedDate() {
return mModifiedDate;
}
/**
* 获取背景色ID
*/
public int getBgColorId() {
return mBgColorId;
}
/**
* 获取所属文件夹ID
*/
public long getParentId() {
return mParentId;
}
/**
* 获取文件夹包含的便签数量(仅文件夹类型有效)
*/
public int getNotesCount() {
return mNotesCount;
}
/**
* 获取所属文件夹ID与getParentId一致兼容不同调用场景
*/
public long getFolderId () {
return mParentId;
}
/**
* 获取类型(便签/文件夹/系统文件夹)
*/
public int getType() {
return mType;
}
/**
* 获取桌面插件类型
*/
public int getWidgetType() {
return mWidgetType;
}
/**
* 获取桌面插件ID
*/
public int getWidgetId() {
return mWidgetId;
}
/**
* 获取内容摘要(文件夹名或便签内容片段)
*/
public String getSnippet() {
return mSnippet;
}
/**
* 是否设置了提醒(提醒时间>0
*/
public boolean hasAlert() {
return (mAlertDate > 0);
}
/**
* 是否为通话记录便签(属于通话记录文件夹且有电话号码)
*/
public boolean isCallRecord() {
return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
}
/**
* 从游标中获取便签类型
* @param cursor 数据库查询结果游标
* @return 便签类型Notes.TYPE_NOTE等
*/
public static int getNoteType(Cursor cursor) {
return cursor.getInt(TYPE_COLUMN);
}
}