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

573 lines
14 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;
/**
* 笔记列表项数据类
* <p>
* 该类用于存储和管理小米便签应用中笔记列表项的数据,包括笔记的基本信息、
* 文件夹信息、提醒信息等
* </p>
*/
public class NoteItemData {
/**
* 笔记数据查询的投影列数组
* <p>
* 定义了从数据库查询笔记数据时需要获取的列
* </p>
*/
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,
NoteColumns.PINNED,
NoteColumns.IS_LOCKED,
NoteColumns.LOCK_PASSWORD,
NoteColumns.LOCK_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;
/**
* 查询结果中置顶状态列的索引
*/
private static final int PINNED_COLUMN = 12;
/**
* 查询结果中锁定状态列的索引
*/
private static final int IS_LOCKED_COLUMN = 13;
/**
* 查询结果中锁定密码列的索引
*/
private static final int LOCK_PASSWORD_COLUMN = 14;
/**
* 查询结果中锁定类型列的索引
*/
private static final int LOCK_TYPE_COLUMN = 15;
/**
* 笔记ID
*/
private long mId;
/**
* 置顶状态
*/
private boolean mPinned;
/**
* 锁定状态
*/
private boolean mIsLocked;
/**
* 锁定密码
*/
private String mLockPassword;
/**
* 锁定类型
*/
private int mLockType;
/**
* 提醒日期
*/
private long mAlertDate;
/**
* 背景颜色ID
*/
private int mBgColorId;
/**
* 创建日期
*/
private long mCreatedDate;
/**
* 是否有附件
*/
private boolean mHasAttachment;
/**
* 修改日期
*/
private long mModifiedDate;
/**
* 笔记数量(用于文件夹)
*/
private int mNotesCount;
/**
* 父文件夹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);
// 先替换标签保留HTML格式以便在列表中显示富文本
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);
mPinned = (cursor.getInt(PINNED_COLUMN) > 0) ? true : false;
mIsLocked = (cursor.getInt(IS_LOCKED_COLUMN) > 0) ? true : false;
mLockPassword = cursor.getString(LOCK_PASSWORD_COLUMN);
mLockType = cursor.getInt(LOCK_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);
}
/**
* 检查当前项在列表中的位置
* <p>
* 该方法用于判断当前项是否为第一项、最后一项、唯一一项,以及是否为文件夹后的笔记
* </p>
* @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 是否为文件夹后的唯一笔记
*/
public boolean isOneFollowingFolder() {
return mIsOneNoteFollowingFolder;
}
/**
* 判断是否为文件夹后的多个笔记
* @return 是否为文件夹后的多个笔记
*/
public boolean isMultiFollowingFolder() {
return mIsMultiNotesFollowingFolder;
}
/**
* 判断是否为最后一项
* @return 是否为最后一项
*/
public boolean isLast() {
return mIsLastItem;
}
/**
* 获取通话记录联系人名称
* @return 通话记录联系人名称
*/
public String getCallName() {
return mName;
}
/**
* 判断是否为第一项
* @return 是否为第一项
*/
public boolean isFirst() {
return mIsFirstItem;
}
/**
* 判断是否为唯一一项
* @return 是否为唯一一项
*/
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 是否有附件
*/
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
* @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 是否有提醒
*/
public boolean hasAlert() {
return (mAlertDate > 0);
}
/**
* 判断是否为通话记录
* @return 是否为通话记录
*/
public boolean isCallRecord() {
return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
}
/**
* 获取置顶状态
* @return 置顶状态true表示已置顶false表示未置顶
*/
public boolean isPinned() {
return mPinned;
}
/**
* 设置置顶状态
* @param pinned 置顶状态true表示置顶false表示取消置顶
*/
public void setPinned(boolean pinned) {
mPinned = pinned;
}
/**
* 获取锁定状态
* @return 锁定状态true表示已锁定false表示未锁定
*/
public boolean isLocked() {
return mIsLocked;
}
/**
* 设置锁定状态
* @param locked 锁定状态true表示锁定false表示解锁
*/
public void setLocked(boolean locked) {
mIsLocked = locked;
}
/**
* 获取锁定密码
* @return 锁定密码
*/
public String getLockPassword() {
return mLockPassword;
}
/**
* 设置锁定密码
* @param password 锁定密码
*/
public void setLockPassword(String password) {
mLockPassword = password;
}
/**
* 获取锁定类型
* @return 锁定类型0表示笔记1表示文件夹
*/
public int getLockType() {
return mLockType;
}
/**
* 设置锁定类型
* @param type 锁定类型0表示笔记1表示文件夹
*/
public void setLockType(int type) {
mLockType = type;
}
/**
* 获取笔记类型(静态方法)
* @param cursor 包含笔记数据的游标
* @return 笔记类型
*/
public static int getNoteType(Cursor cursor) {
return cursor.getInt(TYPE_COLUMN);
}
}