/* * 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 类封装了从数据库中读取的笔记项数据。它提供了对笔记项的各种属性和状态的访问, * 包括笔记的创建日期、修改日期、是否有附件、是否有提醒、笔记的类型等。 */ 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, // 小部件类型 }; // 各列索引 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; // 笔记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; // 笔记类型 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; // 是否有多个笔记跟随文件夹 /** * 构造函数,根据给定的 Context 和 Cursor 从数据库中读取笔记项数据,并初始化该对象。 * @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); // 判断是否有附件 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(); // 判断是否是最后一项 mIsFirstItem = cursor.isFirst(); // 判断是否是第一项 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"); } } } } // 以下是公开的访问方法,用于获取该笔记项的各个属性值 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() { 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() { return mBgColorId; } public long getParentId() { return mParentId; } public int getNotesCount() { return mNotesCount; } public long getFolderId () { return mParentId; } public int getType() { return mType; } public int getWidgetType() { return mWidgetType; } public int getWidgetId() { return mWidgetId; } public String getSnippet() { return mSnippet; } public boolean hasAlert() { return (mAlertDate > 0); } 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); } }