曹馨语 1 month ago
parent 758794b7ef
commit 1dcde387fc

@ -1,346 +0,0 @@
/*
* 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;
/**
* NoteItemData
* ID
*
*/
public class NoteItemData {
// 定义从数据库查询笔记数据时需要的列名数组
static final String [] PROJECTION = new String [] {
NoteColumns.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;
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; // 笔记的背景颜色 ID
private long mCreatedDate; // 笔记的创建日期
private boolean mHasAttachment; // 笔记是否有附件
private long mModifiedDate; // 笔记的修改日期
private int mNotesCount; // 笔记数量
private long mParentId; // 笔记的父文件夹 ID
private String mSnippet; // 笔记的摘要
private int mType; // 笔记的类型
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 ContentResolver
* @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) ? 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 = "";
// 如果笔记的父文件夹 ID 为通话记录文件夹的 ID
if (mParentId == Notes.ID_CALL_RECORD_FOLDER) {
// 通过笔记 ID 获取通话号码
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);
}
/**
* Cursor
* @param cursor 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;
}
}
// 将 Cursor 移动回原来的位置
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;
}
/**
*
* @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
* @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));
}
/**
* Cursor
* @param cursor Cursor
* @return
*/
public static int getNoteType(Cursor cursor) {
return cursor.getInt(TYPE_COLUMN);
}
}
Loading…
Cancel
Save