|
|
/*
|
|
|
* 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.
|
|
|
* 总体分析
|
|
|
这段 Java 代码定义了NoteItemData类,主要用于对从数据库查询获取的笔记相关数据(通过Cursor传递)进行解析和封装,提取出如笔记ID、提醒日期、背景颜色ID等各种具体的信息,并根据数据情况判断笔记在列表中的位置关系(是否是首个、最后一个、唯一的等)以及是否跟随文件夹等特殊情况,同时向外提供了一系列获取这些封装好的数据和状态判断结果的方法,方便在展示笔记相关列表或者进行其他与笔记数据交互的场景中使用。
|
|
|
函数分析
|
|
|
构造函数
|
|
|
NoteItemData(Context context, Cursor cursor):
|
|
|
所属类:NoteItemData
|
|
|
功能:
|
|
|
从传入的Cursor中按照预定义的列索引(如ID_COLUMN等常量对应各字段位置)提取笔记各项数据,比如通过cursor.getLong、cursor.getInt、cursor.getString等方法分别获取对应类型的数据,并赋值给类中的成员变量(如mId、mBgColorId、mSnippet等),以此完成基础的数据解析和封装。
|
|
|
针对笔记内容片段(mSnippet),会进行一些字符串替换操作(去掉特定的标记,像NoteEditActivity.TAG_CHECKED和NoteEditActivity.TAG_UNCHECKED)。
|
|
|
当笔记的父ID是通话记录文件夹的ID(Notes.ID_CALL_RECORD_FOLDER)时,通过DataUtils.getCallNumberByNoteId方法尝试获取电话号码,若电话号码不为空,则利用Contact.getContact方法根据电话号码获取联系人姓名,若获取失败则将电话号码作为姓名,若电话号码本身为空则将姓名设为空字符串,完成与联系人相关信息的处理。
|
|
|
最后调用checkPostion方法,根据Cursor的相关状态(如是否是首条、末条记录等)以及笔记类型等情况,判断笔记在列表中的位置相关属性(如是否是最后一个、第一个、唯一的,是否跟随文件夹等情况)并设置对应的成员变量(mIsLastItem、mIsFirstItem等)。
|
|
|
位置判断相关函数
|
|
|
checkPostion(Cursor cursor):
|
|
|
所属类:NoteItemData
|
|
|
功能:
|
|
|
首先判断Cursor当前是否处于最后一条记录,将结果赋值给mIsLastItem;判断是否处于第一条记录,赋值给mIsFirstItem;判断记录总数是否为1,来确定是否只有一个笔记项,赋值给mIsOnlyOneItem,并初始化mIsMultiNotesFollowingFolder和mIsOneNoteFollowingFolder为false。
|
|
|
接着,如果笔记类型是普通笔记类型(mType == Notes.TYPE_NOTE)且不是第一条记录,获取当前Cursor的位置,尝试将Cursor移动到上一条记录,然后判断上一条记录的类型是否是文件夹类型或者系统类型,如果是,再判断总记录数是否大于当前位置加1,若是则表示有多条笔记跟随该文件夹,将mIsMultiNotesFollowingFolder设为true,否则设为mIsOneNoteFollowingFolder设为true,最后将Cursor移回原来位置(通过moveToNext方法,如果移不回则抛出异常),以此完成对笔记与文件夹关联位置关系的判断。
|
|
|
状态获取相关函数
|
|
|
isOneFollowingFolder():
|
|
|
所属类:NoteItemData
|
|
|
功能:返回mIsOneNoteFollowingFolder的值,用于判断当前笔记是否是跟随文件夹的唯一一条笔记,外部代码可以通过调用这个方法知晓笔记的这种特殊位置关系情况。
|
|
|
isMultiFollowingFolder():
|
|
|
所属类:NoteItemData
|
|
|
功能:返回mIsMultiNotesFollowingFolder的值,用于判断当前笔记是否有多条笔记跟随所在的文件夹,方便外部代码根据这个结果进行不同的界面展示或者业务逻辑处理等。
|
|
|
isLast():
|
|
|
所属类:NoteItemData
|
|
|
功能:返回mIsLastItem的值,判断当前笔记是否是列表中的最后一项,在处理列表滚动、分页等相关逻辑时可能会用到这个判断结果。
|
|
|
isFirst():
|
|
|
所属类:NoteItemData
|
|
|
功能:返回mIsFirstItem的值,判断当前笔记是否是列表中的第一项,同样在一些涉及列表顺序相关的操作场景中会用到这个判断结果。
|
|
|
isSingle():
|
|
|
所属类:NoteItemData
|
|
|
功能:返回mIsOnlyOneItem的值,判断整个列表中是否只有当前这一个笔记项,对于一些特殊的界面显示或者数据处理逻辑(比如只有一个笔记时的特殊展示方式)可以依据这个结果来实现。
|
|
|
数据获取相关函数
|
|
|
getCallName():
|
|
|
所属类:NoteItemData
|
|
|
功能:返回mName的值,即获取与笔记关联的联系人姓名(如果有),外部代码可以利用这个方法来展示笔记对应的联系人相关信息,比如在通话记录笔记展示中显示来电人姓名等场景。
|
|
|
getId()、getAlertDate()、getCreatedDate()、getModifiedDate()、getBgColorId()、getParentId()、getNotesCount()、getFolderId()、getType()、getWidgetType()、getWidgetId()、getSnippet():
|
|
|
所属类:NoteItemData
|
|
|
功能:这些函数分别返回对应成员变量(如mId、mAlertDate等)的值,方便外部代码获取笔记的各项具体数据,例如获取笔记ID用于数据关联、获取提醒日期用于展示提醒相关信息、获取片段内容用于在列表中展示笔记的简要内容等
|
|
|
*/
|
|
|
|
|
|
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;
|
|
|
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));
|
|
|
}
|
|
|
|
|
|
public static int getNoteType(Cursor cursor) {
|
|
|
return cursor.getInt(TYPE_COLUMN);
|
|
|
}
|
|
|
}
|