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.
note/src/NoteItemData.java

259 lines
8.3 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; // 导入数据工具类
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;
// 定义位置相关的成员变量
private boolean mIsLastItem;
private boolean mIsFirstItem;
private boolean mIsOnlyOneItem;
private boolean mIsOneNoteFollowingFolder;
private boolean mIsMultiNotesFollowingFolder;
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); // 检查位置
}
private void checkPostion(Cursor cursor) {
// 检查是否是最后一项
mIsLastItem = cursor.isLast() ? true : false;
// 检查是否是第一项
mIsFirstItem = cursor.isFirst() ? true : false;
// 检查是否只有一项
mIsOnlyOneItem = (cursor.getCount() == 1);
mIsMultiNotesFollowingFolder = false; // 初始化多笔记标志为false
mIsOneNoteFollowingFolder = false; // 初始化单笔记标志为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; // 设置多笔记标志为true
} else {
mIsOneNoteFollowingFolder = true; // 设置单笔记标志为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;
}
public long getId() {
// 返回笔记ID
return mId;
}
public long getAlertDate() {
// 返回提醒日期
return mAlertDate;
}
public long getCreatedDate() {
// 返回创建日期
return mCreatedDate;
}
public boolean hasAttachment() {
// 返回是否有附件
return mHasAttachment;
}
public long getModifiedDate() {
// 返回修改日期
return mModifiedDate;
}
public int getBgColorId() {
// 返回背景颜色ID
return mBgColorId;
}
public long getParentId() {
// 返回父ID通常指文件夹ID
return mParentId;
}
public int getNotesCount() {
// 返回笔记计数
return mNotesCount;
}
public long getFolderId () {
// 返回文件夹ID与getParentId方法相同
return mParentId;
}
public int getType() {
// 返回笔记类型
return mType;
}
public int getWidgetType() {
// 返回小部件类型
return mWidgetType;
}
public int getWidgetId() {
// 返回小部件ID
return mWidgetId;
}
public String getSnippet() {
// 返回笔记摘要
return mSnippet;
}
public boolean hasAlert() {
// 返回是否有提醒即提醒日期是否大于0
return (mAlertDate > 0);
}
public boolean isCallRecord() {
// 返回是否是通话记录,即是否在通话记录文件夹中且电话号码不为空
return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
}
public static int getNoteType(Cursor cursor) {
// 从游标中获取笔记类型
return cursor.getInt(TYPE_COLUMN);
}