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.
xiaomi/ui/NotesPreferenceActivity.java

147 lines
8.1 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");
* 按照许可要求使用此文件,否则不允许使用。
* 可以通过以下网址获取许可证副本:
* http://www.apache.org/licenses/LICENSE-2.0
*
* 除非适用法律要求或书面同意,软件依据许可证分发是“按现状”分发,
* 不附带任何明示或暗示的保证或条件。请查看许可证了解具体权限和限制。
*/
// 所在包声明表明该类属于笔记应用net.micode.notes的用户界面ui相关模块。
package net.micode.notes.ui;
import android.content.Context;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.micode.notes.R;
import net.micode.notes.data.Notes;
import net.micode.notes.tool.DataUtils;
import net.micode.notes.tool.ResourceParser.NoteItemBgResources;
// NotesListItem类继承自LinearLayout作为笔记列表中单个列表项的视图类负责呈现笔记各方面信息如标题、时间、提醒标识等
// 会依据笔记具体类型(普通笔记、通话记录文件夹、普通文件夹等)以及是否处于多选模式来灵活调整各部分的显示内容与样式。
public class NotesListItem extends LinearLayout {
// 用于展示提醒图标的ImageView根据笔记是否设置提醒决定其显示与否及显示的具体图标辅助用户知晓笔记的提醒情况。
private ImageView mAlert;
// 展示笔记标题的TextView会依照笔记类型不同展示相应格式的标题内容像普通笔记显示摘要文件夹显示名称及包含文件数量等。
private TextView mTitle;
// 显示笔记最后修改时间的TextView借助DateUtils将时间戳转换为相对时间格式如“几分钟前”等展示便于用户掌握笔记更新情况。
private TextView mTime;
// 针对通话记录相关笔记用于显示通话对方名称的TextView在其他类型笔记中通常设为不可见。
private TextView mCallName;
// 存储当前列表项对应的笔记数据对象,方便后续获取笔记详细属性来处理展示逻辑等操作。
private NoteItemData mItemData;
// 多选模式下用于表示笔记项是否被选中的CheckBox在相应模式下依据选中状态显示勾选情况非多选模式则隐藏实现多选交互的可视化。
private CheckBox mCheckBox;
// 构造方法,接收上下文对象,初始化视图相关操作:
// 1. 调用父类LinearLayout构造方法传入上下文。
// 2. 通过inflate加载指定布局文件R.layout.note_item到当前视图。
// 3. 利用findViewById找到布局内各子视图控件并赋值给对应成员变量完成视图初始化。
public NotesListItem(Context context) {
super(context);
inflate(context, R.layout.note_item, this);
mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
mTitle = (TextView) findViewById(R.id.tv_title);
mTime = (TextView) findViewById(R.id.tv_time);
mCallName = (TextView) findViewById(R.id.tv_name);
mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
}
// 绑定数据与设置视图显示状态的方法,根据传入参数调整各控件显示内容和可见性,确保正确展示笔记信息并适配交互模式外观:
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
// 根据多选模式及笔记类型决定CheckBox的显示与勾选状态
if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
mCheckBox.setVisibility(View.VISIBLE);
mCheckBox.setChecked(checked);
} else {
mCheckBox.setVisibility(View.GONE);
}
// 保存传入的笔记数据对象,供后续操作使用。
mItemData = data;
// 依据笔记数据对象的不同情况设置各控件显示内容和可见性:
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
// 当前为通话记录文件夹时的设置:
mCallName.setVisibility(View.GONE);
mAlert.setVisibility(View.VISIBLE);
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
mTitle.setText(context.getString(R.string.call_record_folder_name)
+ context.getString(R.string.format_folder_files_count, data.getNotesCount()));
mAlert.setImageResource(R.drawable.call_record);
} else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) {
// 当前为通话记录文件夹下笔记时的设置:
mCallName.setVisibility(View.VISIBLE);
mCallName.setText(data.getCallName());
mTitle.setTextAppearance(context, R.style.TextAppearanceSecondaryItem);
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
if (data.hasAlert()) {
mAlert.setImageResource(R.drawable.clock);
mAlert.setVisibility(View.VISIBLE);
} else {
mAlert.setVisibility(View.GONE);
}
} else {
// 其他普通笔记或文件夹类型笔记时的通用设置:
mCallName.setVisibility(View.GONE);
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
if (data.getType() == Notes.TYPE_FOLDER) {
// 若是文件夹类型,设置标题文本包含摘要及文件数量信息,隐藏提醒图标。
mTitle.setText(data.getSnippet()
+ context.getString(R.string.format_folder_files_count,
data.getNotesCount()));
mAlert.setVisibility(View.GONE);
} else {
// 若是普通笔记类型,设置标题为格式化摘要内容,并根据提醒设置决定提醒图标的显示。
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
if (data.hasAlert()) {
mAlert.setImageResource(R.drawable.clock);
mAlert.setVisibility(View.VISIBLE);
} else {
mAlert.setVisibility(View.GONE);
}
}
}
// 设置时间文本为相对时间格式字符串,展示笔记最后修改时间。
mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
// 根据笔记数据设置列表项背景样式,增强不同类型笔记的视觉区分度。
setBackground(data);
}
// 私有方法,依据笔记数据对象来设置当前列表项的背景资源样式,按照笔记类型及在列表中的位置等属性选用合适背景资源:
private void setBackground(NoteItemData data) {
int id = data.getBgColorId();
if (data.getType() == Notes.TYPE_NOTE) {
// 根据普通笔记在列表中的不同位置情况设置相应背景资源:
if (data.isSingle() || data.isOneFollowingFolder()) {
setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
} else if (data.isLast()) {
setBackgroundResource(NoteItemBgResources.getNoteBgLastRes(id));
} else if (data.isFirst() || data.isMultiFollowingFolder()) {
setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
} else {
setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
}
} else {
// 非普通笔记(如文件夹)统一设置对应的文件夹背景资源。
setBackgroundResource(NoteItemBgResources.getFolderBgRes());
}
}
// 对外提供获取当前列表项对应的笔记数据对象的方法,方便外部代码获取笔记详细信息用于其他逻辑处理或数据传递。
public NoteItemData getItemData() {
return mItemData;
}
}