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

265 lines
12 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.
* 总体分析
这段 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是通话记录文件夹的IDNotes.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);
}
}