刚刚提交的代码作者搞错了,现在先恢复到之前的作者再重新提交

src
WisHua 8 months ago
parent 7f380e120f
commit 59306edd5b

@ -7,97 +7,94 @@
*
* 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; // 定义包名表示该类文件位于net.micode.notes.ui这个包中
package net.micode.notes.ui;
import java.util.Calendar; // 导入Java的Calendar类用于操作日期和时间
import java.util.Calendar;
import net.micode.notes.R; // 导入R资源文件用于访问应用中的资源
import net.micode.notes.ui.DateTimePicker; // 导入自定义的日期时间选择器控件
import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener; // 导入日期时间选择器控件的监听器接口
import net.micode.notes.R;
import net.micode.notes.ui.DateTimePicker;
import net.micode.notes.ui.DateTimePicker.OnDateTimeChangedListener;
import android.app.AlertDialog; // 导入Android的AlertDialog类用于创建对话框
import android.content.Context; // 导入Android的Context类用于访问系统资源
import android.content.DialogInterface; // 导入Android的DialogInterface类用于处理对话框中的按钮点击事件
import android.content.DialogInterface.OnClickListener; // 导入DialogInterface的OnClickListener接口
import android.text.format.DateFormat; // 导入Android的DateFormat工具类用于格式化日期和时间
import android.text.format.DateUtils; // 导入Android的DateUtils工具类用于处理日期和时间相关的操作
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.format.DateFormat;
import android.text.format.DateUtils;
// 自定义的日期时间选择对话框继承自AlertDialog并实现了OnClickListener接口
public class DateTimePickerDialog extends AlertDialog implements OnClickListener {
// 存储当前选择的日期时间
private Calendar mDate = Calendar.getInstance(); // 创建一个Calendar实例初始化为当前时间
private Calendar mDate = Calendar.getInstance();
// 是否使用24小时制
private boolean mIs24HourView; // 布尔变量用于判断是否使用24小时制显示时间
private boolean mIs24HourView;
// 回调接口,当日期时间设置完成后调用
private OnDateTimeSetListener mOnDateTimeSetListener; // 定义一个回调接口,当用户选择好日期时间后进行回调
private OnDateTimeSetListener mOnDateTimeSetListener;
// 日期时间选择器控件
private DateTimePicker mDateTimePicker; // 定义一个日期时间选择器控件实例,用于在对话框中选择日期时间
private DateTimePicker mDateTimePicker;
// 定义日期时间设置完成后的回调接口
public interface OnDateTimeSetListener {
void OnDateTimeSet(AlertDialog dialog, long date); // 定义回调方法传入当前对话框和选择的日期时间long类型
void OnDateTimeSet(AlertDialog dialog, long date);
}
// 构造函数,初始化对话框并设置初始日期时间
public DateTimePickerDialog(Context context, long date) {
super(context); // 调用父类AlertDialog的构造函数传入上下文环境
mDateTimePicker = new DateTimePicker(context); // 创建日期时间选择器控件实例
setView(mDateTimePicker); // 将日期时间选择器控件添加到对话框中
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() { // 设置日期时间选择器控件的监听器
public void onDateTimeChanged(DateTimePicker view, int year, int month, // 定义监听器回调方法,当日期时间发生变化时调用
super(context);
mDateTimePicker = new DateTimePicker(context);
setView(mDateTimePicker);
mDateTimePicker.setOnDateTimeChangedListener(new OnDateTimeChangedListener() {
public void onDateTimeChanged(DateTimePicker view, int year, int month,
int dayOfMonth, int hourOfDay, int minute) {
mDate.set(Calendar.YEAR, year); // 更新mDate中的年份为选择的年份
mDate.set(Calendar.MONTH, month); // 更新mDate中的月份为选择的月份
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth); // 更新mDate中的日期为选择的日期
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay); // 更新mDate中的小时为选择的小时
mDate.set(Calendar.MINUTE, minute); // 更新mDate中的分钟为选择的分钟
mDate.set(Calendar.SECOND, 0); // 将秒设置为0忽略秒的选择
updateTitle(mDate.getTimeInMillis()); // 更新对话框标题,显示当前选择的日期时间
mDate.set(Calendar.YEAR, year);
mDate.set(Calendar.MONTH, month);
mDate.set(Calendar.DAY_OF_MONTH, dayOfMonth);
mDate.set(Calendar.HOUR_OF_DAY, hourOfDay);
mDate.set(Calendar.MINUTE, minute);
updateTitle(mDate.getTimeInMillis());
}
});
mDate.setTimeInMillis(date); // 将mDate设置为传入的初始日期时间
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis()); // 将日期时间选择器控件的当前日期时间设置为传入的初始日期时间
setButton(context.getString(R.string.datetime_dialog_ok), this); // 设置确认按钮,按钮文本为资源文件中的"datetime_dialog_ok"定义的文本,点击事件为当前类
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null); // 设置取消按钮,按钮文本为资源文件中的"datetime_dialog_cancel"定义的文本,点击事件为空
set24HourView(DateFormat.is24HourFormat(this.getContext())); // 根据系统设置判断是否使用24小时制显示时间
updateTitle(mDate.getTimeInMillis()); // 更新对话框标题,显示初始选择的日期时间
mDate.setTimeInMillis(date);
mDate.set(Calendar.SECOND, 0);
mDateTimePicker.setCurrentDate(mDate.getTimeInMillis());
setButton(context.getString(R.string.datetime_dialog_ok), this);
setButton2(context.getString(R.string.datetime_dialog_cancel), (OnClickListener)null);
set24HourView(DateFormat.is24HourFormat(this.getContext()));
updateTitle(mDate.getTimeInMillis());
}
// 设置是否使用24小时制显示时间
public void set24HourView(boolean is24HourView) {
mIs24HourView = is24HourView; // 更新是否使用24小时制的布尔值
mIs24HourView = is24HourView;
}
// 设置日期时间选择完成后的回调监听器
public void setOnDateTimeSetListener(OnDateTimeSetListener callBack) {
mOnDateTimeSetListener = callBack; // 设置回调监听器为传入的参数
mOnDateTimeSetListener = callBack;
}
// 更新对话框的标题以显示当前选择的日期时间
private void updateTitle(long date) {
int flag =
DateUtils.FORMAT_SHOW_YEAR | // 格式化标志,显示年份
DateUtils.FORMAT_SHOW_DATE | // 格式化标志,显示日期
DateUtils.FORMAT_SHOW_TIME; // 格式化标志,显示时间
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_12HOUR; // 根据是否使用24小时制添加相应的格式化标志
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag)); // 使用DateUtils.formatDateTime方法格式化日期时间并设置为对话框的标题
DateUtils.FORMAT_SHOW_YEAR |
DateUtils.FORMAT_SHOW_DATE |
DateUtils.FORMAT_SHOW_TIME;
flag |= mIs24HourView ? DateUtils.FORMAT_24HOUR : DateUtils.FORMAT_24HOUR;
setTitle(DateUtils.formatDateTime(this.getContext(), date, flag));
}
// 处理用户点击对话框按钮的事件,如果是确认按钮则调用回调监听器
public void onClick(DialogInterface arg0, int arg1) {
if (mOnDateTimeSetListener != null) { // 检查回调监听器是否为空
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis()); // 调用回调监听器的OnDateTimeSet方法传入当前对话框和选择的日期时间
if (mOnDateTimeSetListener != null) {
mOnDateTimeSetListener.OnDateTimeSet(this, mDate.getTimeInMillis());
}
}

File diff suppressed because it is too large Load Diff

@ -7,9 +7,11 @@
*
* 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;
@ -37,15 +39,14 @@ import java.util.Map;
// 自定义的EditText用于笔记应用中支持删除、添加文本事件监听
public class NoteEditText extends EditText {
private static final String TAG = "NoteEditText"; // 日志标签
private int mIndex; // 当前文本框的索引
private int mSelectionStartBeforeDelete; // 记录删除操作前的光标位置
private static final String TAG = "NoteEditText";
private int mIndex;
private int mSelectionStartBeforeDelete;
private static final String SCHEME_TEL = "tel:"; // 电话号码URL前缀
private static final String SCHEME_HTTP = "http:"; // 网络链接URL前缀
private static final String SCHEME_EMAIL = "mailto:"; // 邮件链接URL前缀
private static final String SCHEME_TEL = "tel:" ;
private static final String SCHEME_HTTP = "http:" ;
private static final String SCHEME_EMAIL = "mailto:" ;
// URL方案与对应的字符串资源ID的映射
private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>();
static {
sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel);
@ -54,30 +55,32 @@ public class NoteEditText extends EditText {
}
/**
* {@link NoteEditActivity}
* Call by the {@link NoteEditActivity} to delete or add edit text
*/
public interface OnTextViewChangeListener {
/**
* {@link KeyEvent#KEYCODE_DEL}
* Delete current edit text when {@link KeyEvent#KEYCODE_DEL} happens
* and the text is null
*/
void onEditTextDelete(int index, String text);
/**
* {@link KeyEvent#KEYCODE_ENTER}
* Add edit text after current edit text when {@link KeyEvent#KEYCODE_ENTER}
* happen
*/
void onEditTextEnter(int index, String text);
/**
*
* Hide or show item option when text change
*/
void onTextChange(int index, boolean hasText);
}
private OnTextViewChangeListener mOnTextViewChangeListener; // 文本变化监听器实例
private OnTextViewChangeListener mOnTextViewChangeListener;
public NoteEditText(Context context) {
super(context, null); // 调用父类构造函数
mIndex = 0; // 初始化文本框索引为0
super(context, null);
mIndex = 0;
}
// 设置当前文本框的索引
@ -91,11 +94,11 @@ public class NoteEditText extends EditText {
}
public NoteEditText(Context context, AttributeSet attrs) {
super(context, attrs, android.R.attr.editTextStyle); // 调用父类构造函数并设置默认样式
super(context, attrs, android.R.attr.editTextStyle);
}
public NoteEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle); // 调用父类构造函数并设置自定义样式
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@ -103,120 +106,120 @@ public class NoteEditText extends EditText {
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // 触摸屏幕时
int x = (int) event.getX(); // 获取触摸点的X坐标
int y = (int) event.getY(); // 获取触摸点的Y坐标
x -= getTotalPaddingLeft(); // 减去左内边距
y -= getTotalPaddingTop(); // 减去上内边距
x += getScrollX(); // 加上水平滚动位置
y += getScrollY(); // 加上垂直滚动位置
Layout layout = getLayout(); // 获取文本布局信息
int line = layout.getLineForVertical(y); // 根据Y坐标获取行号
int off = layout.getOffsetForHorizontal(line, x); // 根据行号和X坐标获取偏移量
Selection.setSelection(getText(), off); // 更新光标位置
case MotionEvent.ACTION_DOWN:
int x = (int) event.getX();
int y = (int) event.getY();
x -= getTotalPaddingLeft();
y -= getTotalPaddingTop();
x += getScrollX();
y += getScrollY();
Layout layout = getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
Selection.setSelection(getText(), off);
break;
}
return super.onTouchEvent(event); // 调用父类的触摸事件处理方法
return super.onTouchEvent(event);
}
// 处理按键按下事件,记录删除操作前的光标位置
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER: // 按下回车键
case KeyEvent.KEYCODE_ENTER:
if (mOnTextViewChangeListener != null) {
return false; // 如果设置了监听器返回false以拦截按键事件
return false;
}
break;
case KeyEvent.KEYCODE_DEL: // 按下删除键
mSelectionStartBeforeDelete = getSelectionStart(); // 记录删除操作前的光标位置
case KeyEvent.KEYCODE_DEL:
mSelectionStartBeforeDelete = getSelectionStart();
break;
default: // 其他按键
default:
break;
}
return super.onKeyDown(keyCode, event); // 调用父类的按键按下事件处理方法
return super.onKeyDown(keyCode, event);
}
// 处理按键弹起事件,根据按键类型执行相应操作
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch(keyCode) {
case KeyEvent.KEYCODE_DEL: // 弹起删除键
case KeyEvent.KEYCODE_DEL:
if (mOnTextViewChangeListener != null) {
if (0 == mSelectionStartBeforeDelete && mIndex != 0) { // 如果光标在文本开头且不是第一个文本框
mOnTextViewChangeListener.onEditTextDelete(mIndex, getText().toString()); // 通知监听器删除文本框
return true; // 拦截按键事件
if (0 == mSelectionStartBeforeDelete && mIndex != 0) {
mOnTextViewChangeListener.onEditTextDelete(mIndex, getText().toString());
return true;
}
} else {
Log.d(TAG, "OnTextViewChangeListener was not seted"); // 如果未设置监听器,记录日志
Log.d(TAG, "OnTextViewChangeListener was not seted");
}
break;
case KeyEvent.KEYCODE_ENTER: // 弹起回车键
case KeyEvent.KEYCODE_ENTER:
if (mOnTextViewChangeListener != null) {
int selectionStart = getSelectionStart(); // 获取光标位置
String text = getText().subSequence(selectionStart, length()).toString(); // 获取光标后的文本
setText(getText().subSequence(0, selectionStart)); // 将光标后的文本清空
mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text); // 通知监听器添加新的文本框
int selectionStart = getSelectionStart();
String text = getText().subSequence(selectionStart, length()).toString();
setText(getText().subSequence(0, selectionStart));
mOnTextViewChangeListener.onEditTextEnter(mIndex + 1, text);
} else {
Log.d(TAG, "OnTextViewChangeListener was not seted"); // 如果未设置监听器,记录日志
Log.d(TAG, "OnTextViewChangeListener was not seted");
}
break;
default: // 其他按键
default:
break;
}
return super.onKeyUp(keyCode, event); // 调用父类的按键弹起事件处理方法
return super.onKeyUp(keyCode, event);
}
// 当EditText焦点发生变化时调用通知监听器文本是否有内容
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if (mOnTextViewChangeListener != null) {
if (!focused && TextUtils.isEmpty(getText())) { // 如果失去焦点且文本为空
mOnTextViewChangeListener.onTextChange(mIndex, false); // 通知监听器文本内容为空
if (!focused && TextUtils.isEmpty(getText())) {
mOnTextViewChangeListener.onTextChange(mIndex, false);
} else {
mOnTextViewChangeListener.onTextChange(mIndex, true); // 通知监听器文本内容不为空
mOnTextViewChangeListener.onTextChange(mIndex, true);
}
}
super.onFocusChanged(focused, direction, previouslyFocusedRect); // 调用父类的焦点变化处理方法
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
// 创建上下文菜单处理URL点击事件
@Override
protected void onCreateContextMenu(ContextMenu menu) {
if (getText() instanceof Spanned) { // 如果文本是Spanned类型
int selStart = getSelectionStart(); // 获取选择的起始位置
int selEnd = getSelectionEnd(); // 获取选择的结束位置
if (getText() instanceof Spanned) {
int selStart = getSelectionStart();
int selEnd = getSelectionEnd();
int min = Math.min(selStart, selEnd); // 计算选择的最小位置
int max = Math.max(selStart, selEnd); // 计算选择的最大位置
int min = Math.min(selStart, selEnd);
int max = Math.max(selStart, selEnd);
final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class); // 获取选择区域内的URLSpan
if (urls.length == 1) { // 如果只有一个URLSpan
final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class);
if (urls.length == 1) {
int defaultResId = 0;
for(String schema: sSchemaActionResMap.keySet()) {
if (urls[0].getURL().indexOf(schema) >= 0) { // 根据URL前缀匹配对应的资源ID
if(urls[0].getURL().indexOf(schema) >= 0) {
defaultResId = sSchemaActionResMap.get(schema);
break;
}
}
if (defaultResId == 0) { // 如果没有匹配到前缀
defaultResId = R.string.note_link_other; // 使用默认的资源ID
if (defaultResId == 0) {
defaultResId = R.string.note_link_other;
}
// 添加上下文菜单项并设置点击监听器
menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener(
new OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// 点击菜单项时触发URLSpan的点击事件
// goto a new intent
urls[0].onClick(NoteEditText.this);
return true; // 拦截点击事件
return true;
}
});
}
}
super.onCreateContextMenu(menu); // 调用父类的创建上下文菜单方法
super.onCreateContextMenu(menu);
}
}

@ -7,10 +7,11 @@
*
* 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;
@ -26,23 +27,22 @@ import net.micode.notes.tool.DataUtils;
// 该类用于从数据库游标中提取笔记项数据,并处理与笔记位置相关的逻辑
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如果是文件夹下的笔记则是文件夹ID
NoteColumns.SNIPPET, // 笔记项的摘要信息
NoteColumns.TYPE, // 笔记项的类型,例如笔记、文件夹等
NoteColumns.WIDGET_ID, // 与笔记项关联的小部件ID
NoteColumns.WIDGET_TYPE, // 小部件的类型
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;
@ -57,84 +57,83 @@ public class NoteItemData {
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如果是文件夹下的笔记则是文件夹ID
private String mSnippet; // 笔记项的摘要信息
private int mType; // 笔记项的类型,例如笔记、文件夹等
private int mWidgetId; // 与笔记项关联的小部件ID
private int mWidgetType; // 小部件的类型
private String mName; // 笔记项关联的联系人名称
private String mPhoneNumber; // 笔记项关联的联系人电话号码
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; // 是否是多个笔记跟在一个文件夹后
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); // 获取笔记项的ID
mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); // 获取笔记项的提醒日期
mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); // 获取笔记项的背景颜色ID
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); // 获取笔记项的父ID
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); // 获取笔记项的小部件ID
mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN); // 获取笔记项的小部件类型
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) { // 如果笔记项的父ID是呼叫记录文件夹的ID
mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); // 通过笔记ID获取对应的呼叫记录的电话号码
if (!TextUtils.isEmpty(mPhoneNumber)) { // 如果电话号码不为空
mName = Contact.getContact(context, mPhoneNumber); // 通过电话号码查询联系人信息
if (mName == null) { // 如果没有找到对应的联系人名称
mName = mPhoneNumber; // 则将电话号码作为名称
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) { // 如果联系人名称依然为null
mName = ""; // 初始化为空字符串
if (mName == null) {
mName = "";
}
checkPostion(cursor); // 检查笔记项在列表中的位置信息
checkPostion(cursor);
}
// 检查笔记项在列表中的位置信息
private void checkPostion(Cursor cursor) {
mIsLastItem = cursor.isLast() ? true : false; // 设置是否是列表中的最后一个项
mIsFirstItem = cursor.isFirst() ? true : false; // 设置是否是列表中的第一个项
mIsOnlyOneItem = (cursor.getCount() == 1); // 如果列表中的项数为1则设置为唯一项
mIsMultiNotesFollowingFolder = false; // 初始化是否是多个笔记跟在一个文件夹后为false
mIsOneNoteFollowingFolder = false; // 初始化是否是单个笔记跟在一个文件夹后为false
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) {
// 如果列表中的项数大于当前位置加1
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; // 设置为多个笔记跟在一个文件夹后
mIsMultiNotesFollowingFolder = true;
} else {
mIsOneNoteFollowingFolder = true; // 设置为单个笔记跟在一个文件夹后
mIsOneNoteFollowingFolder = true;
}
}
if (!cursor.moveToNext()) { // 将游标移动回原来的位置
throw new IllegalStateException("cursor move to previous but can't move back"); // 如果移动失败,抛出异常
if (!cursor.moveToNext()) {
throw new IllegalStateException("cursor move to previous but can't move back");
}
}
}
@ -142,112 +141,111 @@ public class NoteItemData {
// 判断该笔记项是否是单个笔记跟在一个文件夹后
public boolean isOneFollowingFolder() {
return mIsOneNoteFollowingFolder; // 返回是否是单个笔记跟在一个文件夹后的结果
return mIsOneNoteFollowingFolder;
}
// 判断该笔记项是否是多个笔记跟在一个文件夹后
public boolean isMultiFollowingFolder() {
return mIsMultiNotesFollowingFolder; // 返回是否是多个笔记跟在一个文件夹后的结果
return mIsMultiNotesFollowingFolder;
}
// 判断该笔记项是否是列表中的最后一个项
public boolean isLast() {
return mIsLastItem; // 返回是否是列表中最后一个项的结果
return mIsLastItem;
}
// 获取与该笔记项关联的呼叫记录的联系人名称
public String getCallName() {
return mName; // 返回关联的联系人名称
return mName;
}
// 判断该笔记项是否是列表中的第一个项
public boolean isFirst() {
return mIsFirstItem; // 返回是否是列表中第一个项的结果
return mIsFirstItem;
}
// 判断该笔记项是否是列表中唯一的项
public boolean isSingle() {
return mIsOnlyOneItem; // 返回是否是列表中唯一项的结果
return mIsOnlyOneItem;
}
// 获取笔记项的ID
public long getId() {
return mId; // 返回笔记项的ID
return mId;
}
// 获取笔记项的提醒日期
public long getAlertDate() {
return mAlertDate; // 返回笔记项的提醒日期
return mAlertDate;
}
// 获取笔记项的创建日期
public long getCreatedDate() {
return mCreatedDate; // 返回笔记项的创建日期
return mCreatedDate;
}
// 判断该笔记项是否有附件
public boolean hasAttachment() {
return mHasAttachment; // 返回笔记项是否有附件的结果
return mHasAttachment;
}
// 获取笔记项的修改日期
public long getModifiedDate() {
return mModifiedDate; // 返回笔记项的修改日期
return mModifiedDate;
}
// 获取笔记项的背景颜色ID
public int getBgColorId() {
return mBgColorId; // 返回笔记项的背景颜色ID
return mBgColorId;
}
// 获取笔记项的父ID
public long getParentId() {
return mParentId; // 返回笔记项的父ID
return mParentId;
}
// 获取笔记项包含的笔记数量
public int getNotesCount() {
return mNotesCount; // 返回笔记项包含的笔记数量
return mNotesCount;
}
// 获取笔记项所在的文件夹ID
public long getFolderId () {
return mParentId; // 返回笔记项所在的文件夹ID与getParentId相同
return mParentId;
}
// 获取笔记项的类型
public int getType() {
return mType; // 返回笔记项的类型
return mType;
}
// 获取笔记项的小部件类型
public int getWidgetType() {
return mWidgetType; // 返回笔记项的小部件类型
return mWidgetType;
}
// 获取笔记项的小部件ID
public int getWidgetId() {
return mWidgetId; // 返回笔记项的小部件ID
return mWidgetId;
}
// 获取笔记项的摘要
public String getSnippet() {
return mSnippet; // 返回笔记项的摘要
return mSnippet;
}
// 判断该笔记项是否有提醒
public boolean hasAlert() {
return (mAlertDate > 0); // 如果提醒日期大于0表示有提醒
return (mAlertDate > 0);
}
// 判断该笔记项是否是呼叫记录类型
public boolean isCallRecord() {
// 如果父ID是呼叫记录文件夹的ID且电话号码不为空则是呼叫记录类型
return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
}
// 静态方法,从游标中获取笔记项的类型
public static int getNoteType(Cursor cursor) {
return cursor.getInt(TYPE_COLUMN); // 从游标中获取笔记项的类型
return cursor.getInt(TYPE_COLUMN);
}
}

File diff suppressed because it is too large Load Diff

@ -1,10 +1,17 @@
/*
* (c) 2010-2011, MiCode (www.micode.net)
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
*
* Apache2.0
*
*
*
* 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;
@ -26,65 +33,65 @@ import java.util.Iterator;
// 自定义的CursorAdapter用于显示笔记列表
public class NotesListAdapter extends CursorAdapter {
private static final String TAG = "NotesListAdapter";
private Context mContext; // 上下文环境
private HashMap<Integer, Boolean> mSelectedIndex; // 记录选中的笔记位置
private int mNotesCount; // 笔记总数
private boolean mChoiceMode; // 是否处于多选模式
private Context mContext;
private HashMap<Integer, Boolean> mSelectedIndex;
private int mNotesCount;
private boolean mChoiceMode;
// 用于存储小部件属性的内部类
public static class AppWidgetAttribute {
public int widgetId; // 小部件ID
public int widgetType; // 小部件类型
public int widgetId;
public int widgetType;
};
// 构造函数,初始化上下文和选择索引
public NotesListAdapter(Context context) {
super(context, null); // 调用父类构造函数,不立即绑定任何游标
mSelectedIndex = new HashMap<Integer, Boolean>(); // 初始化选择索引
mContext = context; // 初始化上下文
mNotesCount = 0; // 初始化笔记数量为0
super(context, null);
mSelectedIndex = new HashMap<Integer, Boolean>();
mContext = context;
mNotesCount = 0;
}
// 创建新的视图项
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new NotesListItem(context); // 创建一个新的NotesListItem视图
return new NotesListItem(context);
}
// 绑定数据到视图项
@Override
public void bindView(View view, Context context, Cursor cursor) {
if (view instanceof NotesListItem) { // 检查视图是否为NotesListItem类型
NoteItemData itemData = new NoteItemData(context, cursor); // 创建NoteItemData对象用于封装笔记数据
((NotesListItem) view).bind(context, itemData, mChoiceMode, // 调用NotesListItem的bind方法绑定数据
isSelectedItem(cursor.getPosition())); // 传递当前项是否被选中的信息
if (view instanceof NotesListItem) {
NoteItemData itemData = new NoteItemData(context, cursor);
((NotesListItem) view).bind(context, itemData, mChoiceMode,
isSelectedItem(cursor.getPosition()));
}
}
// 设置指定位置的项是否被选中,并通知数据集发生变化
public void setCheckedItem(final int position, final boolean checked) {
mSelectedIndex.put(position, checked); // 更新选中状态
notifyDataSetChanged(); // 通知数据集发生变化刷新UI
mSelectedIndex.put(position, checked);
notifyDataSetChanged();
}
// 检查当前是否处于多选模式
public boolean isInChoiceMode() {
return mChoiceMode; // 返回当前的多选模式状态
return mChoiceMode;
}
// 设置多选模式,清空选择索引
public void setChoiceMode(boolean mode) {
mSelectedIndex.clear(); // 清空选择索引
mChoiceMode = mode; // 更新多选模式状态
mSelectedIndex.clear();
mChoiceMode = mode;
}
// 全选或全不选所有笔记
public void selectAll(boolean checked) {
Cursor cursor = getCursor(); // 获取当前绑定的游标
for (int i = 0; i < getCount(); i++) { // 遍历所有笔记
if (cursor.moveToPosition(i)) { // 移动到指定位置
if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { // 检查是否为笔记类型
setCheckedItem(i, checked); // 设置选中状态
Cursor cursor = getCursor();
for (int i = 0; i < getCount(); i++) {
if (cursor.moveToPosition(i)) {
if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) {
setCheckedItem(i, checked);
}
}
}
@ -92,101 +99,101 @@ public class NotesListAdapter extends CursorAdapter {
// 获取所有选中的笔记ID集合
public HashSet<Long> getSelectedItemIds() {
HashSet<Long> itemSet = new HashSet<Long>(); // 初始化选中笔记ID集合
for (Integer position : mSelectedIndex.keySet()) { // 遍历所有选中的位置
if (mSelectedIndex.get(position) == true) { // 检查位置是否被选中
Long id = getItemId(position); // 获取笔记ID
if (id == Notes.ID_ROOT_FOLDER) { // 检查是否为根文件夹ID错误情况
Log.d(TAG, "Wrong item id, should not happen"); // 记录日志
HashSet<Long> itemSet = new HashSet<Long>();
for (Integer position : mSelectedIndex.keySet()) {
if (mSelectedIndex.get(position) == true) {
Long id = getItemId(position);
if (id == Notes.ID_ROOT_FOLDER) {
Log.d(TAG, "Wrong item id, should not happen");
} else {
itemSet.add(id); // 将笔记ID添加到集合
itemSet.add(id);
}
}
}
return itemSet; // 返回选中笔记ID集合
return itemSet;
}
// 获取所有选中的小部件属性集合
public HashSet<AppWidgetAttribute> getSelectedWidget() {
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>(); // 初始化选中小部件属性集合
for (Integer position : mSelectedIndex.keySet()) { // 遍历所有选中的位置
if (mSelectedIndex.get(position) == true) { // 检查位置是否被选中
Cursor c = (Cursor) getItem(position); // 获取对应位置的游标
if (c != null) { // 检查游标是否有效
AppWidgetAttribute widget = new AppWidgetAttribute(); // 创建一个新的小部件属性对象
NoteItemData item = new NoteItemData(mContext, c); // 创建NoteItemData对象用于封装笔记数据
widget.widgetId = item.getWidgetId(); // 设置小部件ID
widget.widgetType = item.getWidgetType(); // 设置小部件类型
itemSet.add(widget); // 将小部件属性对象添加到集合
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>();
for (Integer position : mSelectedIndex.keySet()) {
if (mSelectedIndex.get(position) == true) {
Cursor c = (Cursor) getItem(position);
if (c != null) {
AppWidgetAttribute widget = new AppWidgetAttribute();
NoteItemData item = new NoteItemData(mContext, c);
widget.widgetId = item.getWidgetId();
widget.widgetType = item.getWidgetType();
itemSet.add(widget);
/**
* Don't close cursor here, only the adapter could close it
*
*/
} else {
Log.e(TAG, "Invalid cursor"); // 记录无效游标日志
return null; // 返回null
Log.e(TAG, "Invalid cursor");
return null;
}
}
}
return itemSet; // 返回选中小部件属性集合
return itemSet;
}
// 获取选中的笔记数量
public int getSelectedCount() {
Collection<Boolean> values = mSelectedIndex.values(); // 获取所有选中状态值
Collection<Boolean> values = mSelectedIndex.values();
if (null == values) {
return 0; // 如果选中状态值为空返回0
return 0;
}
Iterator<Boolean> iter = values.iterator(); // 创建迭代器
int count = 0; // 初始化计数器
Iterator<Boolean> iter = values.iterator();
int count = 0;
while (iter.hasNext()) {
if (true == iter.next()) { // 检查是否为选中状态
count++; // 计数器加一
if (true == iter.next()) {
count++;
}
}
return count; // 返回选中的笔记数量
return count;
}
// 检查是否所有笔记都被选中
public boolean isAllSelected() {
int checkedCount = getSelectedCount(); // 获取选中的笔记数量
return (checkedCount != 0 && checkedCount == mNotesCount); // 检查选中的笔记数量是否等于笔记总数
int checkedCount = getSelectedCount();
return (checkedCount != 0 && checkedCount == mNotesCount);
}
// 检查指定位置的项是否被选中
public boolean isSelectedItem(final int position) {
if (null == mSelectedIndex.get(position)) { // 检查指定位置是否被选中
return false; // 如果没有被选中返回false
if (null == mSelectedIndex.get(position)) {
return false;
}
return mSelectedIndex.get(position); // 返回选中状态
return mSelectedIndex.get(position);
}
// 当数据内容发生变化时,更新笔记数量
@Override
protected void onContentChanged() {
super.onContentChanged(); // 调用父类方法
calcNotesCount(); // 计算笔记数量
super.onContentChanged();
calcNotesCount();
}
// 更改Cursor时更新笔记数量
@Override
public void changeCursor(Cursor cursor) {
super.changeCursor(cursor); // 调用父类方法
calcNotesCount(); // 计算笔记数量
super.changeCursor(cursor);
calcNotesCount();
}
// 计算笔记数量
private void calcNotesCount() {
mNotesCount = 0; // 初始化笔记数量为0
for (int i = 0; i < getCount(); i++) { // 遍历所有笔记
Cursor c = (Cursor) getItem(i); // 获取对应位置的游标
if (c != null) { // 检查游标是否有效
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { // 检查是否为笔记类型
mNotesCount++; // 计数器加一
mNotesCount = 0;
for (int i = 0; i < getCount(); i++) {
Cursor c = (Cursor) getItem(i);
if (c != null) {
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) {
mNotesCount++;
}
} else {
Log.e(TAG, "Invalid cursor"); // 记录无效游标日志
return; // 返回
Log.e(TAG, "Invalid cursor");
return;
}
}
}

@ -31,101 +31,80 @@ import net.micode.notes.tool.ResourceParser.NoteItemBgResources;
// NotesListItem 类继承自 LinearLayout用于表示笔记列表中的一个项
public class NotesListItem extends LinearLayout {
private ImageView mAlert; // 用于显示笔记的提醒图标
private TextView mTitle; // 用于显示笔记的标题
private TextView mTime; // 用于显示笔记的修改时间
private TextView mCallName; // 用于显示与笔记相关的联系人姓名
private NoteItemData mItemData; // 存储当前笔记项的数据
private CheckBox mCheckBox; // 用于选择笔记项的复选框
private ImageView mAlert;
private TextView mTitle;
private TextView mTime;
private TextView mCallName;
private NoteItemData mItemData;
private CheckBox mCheckBox;
// 构造函数,初始化 NotesListItem 的视图组件
public NotesListItem(Context context) {
super(context);
// 从资源文件中加载布局文件 R.layout.note_item 到当前 NotesListItem 中
inflate(context, R.layout.note_item, this);
// 查找布局文件中的 ImageView 组件,并赋值给 mAlert
mAlert = (ImageView) findViewById(R.id.iv_alert_icon);
// 查找布局文件中的 TextView 组件,并赋值给 mTitle
mTitle = (TextView) findViewById(R.id.tv_title);
// 查找布局文件中的 TextView 组件,并赋值给 mTime
mTime = (TextView) findViewById(R.id.tv_time);
// 查找布局文件中的 TextView 组件,并赋值给 mCallName
mCallName = (TextView) findViewById(R.id.tv_name);
// 查找布局文件中的 CheckBox 组件,并赋值给 mCheckBox
mCheckBox = (CheckBox) findViewById(android.R.id.checkbox);
}
// 绑定数据到 NotesListItem 的视图组件,并设置选择模式和选中状态
public void bind(Context context, NoteItemData data, boolean choiceMode, boolean checked) {
// 如果处于选择模式并且笔记项类型为普通笔记,则显示复选框
if (choiceMode && data.getType() == Notes.TYPE_NOTE) {
mCheckBox.setVisibility(View.VISIBLE);
mCheckBox.setChecked(checked); // 设置复选框的选中状态
mCheckBox.setChecked(checked);
} else {
mCheckBox.setVisibility(View.GONE); // 否则隐藏复选框
mCheckBox.setVisibility(View.GONE);
}
mItemData = data; // 存储当前绑定的数据
// 如果当前笔记项的 ID 是通话记录文件夹的 ID
mItemData = data;
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
mCallName.setVisibility(View.GONE); // 隐藏联系人姓名 TextView
mAlert.setVisibility(View.VISIBLE); // 显示提醒图标 ImageView
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); // 设置标题 TextView 的文本样式
// 设置标题 TextView 的文本内容为通话记录文件夹名称及其中包含的笔记数量
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); // 设置提醒图标 ImageView 的图片资源为通话记录图标
}
// 如果当前笔记项的父目录 ID 是通话记录文件夹的 ID
else if (data.getParentId() == Notes.ID_CALL_RECORD_FOLDER) {
mCallName.setVisibility(View.VISIBLE); // 显示联系人姓名 TextView
mCallName.setText(data.getCallName()); // 设置联系人姓名 TextView 的文本内容
mTitle.setTextAppearance(context, R.style.TextAppearanceSecondaryItem); // 设置标题 TextView 的文本样式
// 设置标题 TextView 的文本内容为笔记内容的摘要
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()));
// 如果笔记项有提醒,则显示提醒图标 ImageView
if (data.hasAlert()) {
mAlert.setImageResource(R.drawable.clock); // 设置提醒图标 ImageView 的图片资源为时钟图标
mAlert.setImageResource(R.drawable.clock);
mAlert.setVisibility(View.VISIBLE);
} else {
mAlert.setVisibility(View.GONE); // 否则隐藏提醒图标 ImageView
mAlert.setVisibility(View.GONE);
}
}
// 如果当前笔记项既不是通话记录文件夹也不是其子项
else {
mCallName.setVisibility(View.GONE); // 隐藏联系人姓名 TextView
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem); // 设置标题 TextView 的文本样式
} else {
mCallName.setVisibility(View.GONE);
mTitle.setTextAppearance(context, R.style.TextAppearancePrimaryItem);
// 如果笔记项类型为文件夹
if (data.getType() == Notes.TYPE_FOLDER) {
mTitle.setText(data.getSnippet() // 设置标题 TextView 的文本内容为文件夹名称
mTitle.setText(data.getSnippet()
+ context.getString(R.string.format_folder_files_count,
data.getNotesCount())); // 并追加其中包含的笔记数量
mAlert.setVisibility(View.GONE); // 隐藏提醒图标 ImageView
data.getNotesCount()));
mAlert.setVisibility(View.GONE);
} else {
// 设置标题 TextView 的文本内容为笔记内容的摘要
mTitle.setText(DataUtils.getFormattedSnippet(data.getSnippet()));
// 如果笔记项有提醒,则显示提醒图标 ImageView
if (data.hasAlert()) {
mAlert.setImageResource(R.drawable.clock); // 设置提醒图标 ImageView 的图片资源为时钟图标
mAlert.setImageResource(R.drawable.clock);
mAlert.setVisibility(View.VISIBLE);
} else {
mAlert.setVisibility(View.GONE); // 否则隐藏提醒图标 ImageView
mAlert.setVisibility(View.GONE);
}
}
}
// 设置时间 TextView 的文本内容为笔记的相对修改时间如“3 小时前”)
mTime.setText(DateUtils.getRelativeTimeSpanString(data.getModifiedDate()));
setBackground(data); // 根据笔记项的数据设置背景资源
setBackground(data);
}
// 根据数据设置 NotesListItem 的背景资源
private void setBackground(NoteItemData data) {
int id = data.getBgColorId(); // 获取笔记项的背景颜色 ID
// 如果笔记项类型为普通笔记
int id = data.getBgColorId();
if (data.getType() == Notes.TYPE_NOTE) {
// 根据笔记项的位置状态设置不同的背景资源
if (data.isSingle() || data.isOneFollowingFolder()) {
setBackgroundResource(NoteItemBgResources.getNoteBgSingleRes(id));
} else if (data.isLast()) {
@ -133,16 +112,15 @@ public class NotesListItem extends LinearLayout {
} else if (data.isFirst() || data.isMultiFollowingFolder()) {
setBackgroundResource(NoteItemBgResources.getNoteBgFirstRes(id));
} else {
setBackgroundResource(NoteItemBgResources.getNoteBgMiddleRes(id));
setBackgroundResource(NoteItemBgResources.getNoteBgNormalRes(id));
}
} else {
// 如果笔记项类型为文件夹,则设置文件夹的背景资源
setBackgroundResource(NoteItemBgResources.getFolderBgRes());
}
}
// 获取绑定到此 NotesListItem 的数据
public NoteItemData getItemData() {
return mItemData; // 返回存储的当前笔记项数据
return mItemData;
}
}

@ -49,34 +49,24 @@ import net.micode.notes.gtask.remote.GTaskSyncService;
// 设置界面活动类继承自PreferenceActivity
public class NotesPreferenceActivity extends PreferenceActivity {
// 定义偏好设置文件的名称
public static final String PREFERENCE_NAME = "notes_preferences";
// 定义偏好设置中存储同步账户名称的键
public static final String PREFERENCE_SYNC_ACCOUNT_NAME = "pref_key_account_name";
// 定义偏好设置中存储上次同步时间的键
public static final String PREFERENCE_LAST_SYNC_TIME = "pref_last_sync_time";
// 定义偏好设置中背景颜色随机出现的键
public static final String PREFERENCE_SET_BG_COLOR_KEY = "pref_key_bg_random_appear";
// 定义偏好设置中同步账户键用于UI显示
private static final String PREFERENCE_SYNC_ACCOUNT_KEY = "pref_sync_account_key";
// 定义过滤器键,用于添加账户设置
private static final String AUTHORITIES_FILTER_KEY = "authorities";
// 定义账户类别在UI中用于显示账户相关的偏好设置
private PreferenceCategory mAccountCategory;
// 定义一个GTaskReceiver实例用于接收同步状态更新的广播
private GTaskReceiver mReceiver;
// 存储原始的Google账户列表
private Account[] mOriAccounts;
// 标记用户是否添加了新账户
private boolean mHasAddedAccount;
// 创建活动时初始化界面
@ -84,22 +74,17 @@ public class NotesPreferenceActivity extends PreferenceActivity {
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
// 设置ActionBar的返回按钮以便用户可以通过点击应用图标返回上一级活动
/* 使用应用图标进行导航 */
getActionBar().setDisplayHomeAsUpEnabled(true);
// 从指定的XML资源文件加载偏好设置
addPreferencesFromResource(R.xml.preferences);
// 找到XML中定义的账户类别PreferenceCategory并赋值给mAccountCategory
mAccountCategory = (PreferenceCategory) findPreference(PREFERENCE_SYNC_ACCOUNT_KEY);
// 创建GTaskReceiver实例并注册广播接收器用于接收同步服务的状态广播
mReceiver = new GTaskReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction(GTaskSyncService.GTASK_SERVICE_BROADCAST_NAME);
registerReceiver(mReceiver, filter);
// 初始化账户列表,目前为空,将在其他方法中进行填充
mOriAccounts = null;
// 创建并添加自定义的头部视图到设置界面,该视图通常用于显示额外的信息或操作
View header = LayoutInflater.from(this).inflate(R.layout.settings_header, null);
getListView().addHeaderView(header, null, true);
}
@ -129,7 +114,6 @@ public class NotesPreferenceActivity extends PreferenceActivity {
}
}
// 刷新用户界面,包括账户偏好设置和同步按钮
refreshUI();
}
@ -144,33 +128,23 @@ public class NotesPreferenceActivity extends PreferenceActivity {
// 加载账户偏好设置
private void loadAccountPreference() {
// 清空账户类别中的所有偏好设置项
mAccountCategory.removeAll();
// 创建一个新的Preference实例用于显示账户偏好设置
Preference accountPref = new Preference(this);
// 获取当前设置的同步账户名称
final String defaultAccount = getSyncAccountName(this);
// 设置偏好设置项的标题
accountPref.setTitle(getString(R.string.preferences_account_title));
// 设置偏好设置项的摘要信息
accountPref.setSummary(getString(R.string.preferences_account_summary));
// 设置偏好设置项的点击监听器
accountPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
// 如果当前没有正在进行的同步操作
if (!GTaskSyncService.isSyncing()) {
// 如果同步账户名称为空,表示第一次设置账户
if (TextUtils.isEmpty(defaultAccount)) {
// 第一次设置账户
showSelectAccountAlertDialog();
} else {
// 如果账户已经设置,提示用户切换账户的风险
showChangeAccountConfirmAlertDialog();
}
} else {
// 如果正在同步,显示提示信息,告知用户无法在同步过程中更改账户
Toast.makeText(NotesPreferenceActivity.this,
R.string.preferences_toast_cannot_change_account, Toast.LENGTH_SHORT)
.show();
@ -179,48 +153,37 @@ public class NotesPreferenceActivity extends PreferenceActivity {
}
});
// 将账户偏好设置项添加到账户类别中
mAccountCategory.addPreference(accountPref);
}
// 加载同步按钮
private void loadSyncButton() {
// 找到布局文件中定义的同步按钮
Button syncButton = (Button) findViewById(R.id.preference_sync_button);
// 找到布局文件中定义的上次同步时间显示文本
TextView lastSyncTimeView = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
// 根据同步服务的状态设置按钮的文本和点击事件
// 设置按钮状态
if (GTaskSyncService.isSyncing()) {
// 如果正在同步,设置按钮文本为取消同步,并添加点击事件以取消同步
syncButton.setText(getString(R.string.preferences_button_sync_cancel));
syncButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GTaskSyncService.cancelSync(NotesPreferenceActivity.this);
}
});
} else {
// 如果没有同步,设置按钮文本为立即同步,并添加点击事件以开始同步
syncButton.setText(getString(R.string.preferences_button_sync_immediately));
syncButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GTaskSyncService.startSync(NotesPreferenceActivity.this);
}
});
}
// 根据同步账户名称是否为空设置按钮是否可用
syncButton.setEnabled(!TextUtils.isEmpty(getSyncAccountName(this)));
// 根据同步服务的状态设置上次同步时间的显示
// 设置上次同步时间
if (GTaskSyncService.isSyncing()) {
// 如果正在同步,显示同步进度信息
lastSyncTimeView.setText(GTaskSyncService.getProgressString());
lastSyncTimeView.setVisibility(View.VISIBLE);
} else {
// 如果没有同步,显示上次同步的时间,如果没有同步过则不显示
long lastSyncTime = getLastSyncTime(this);
if (lastSyncTime != 0) {
lastSyncTimeView.setText(getString(R.string.preferences_last_sync_time,
@ -235,63 +198,43 @@ public class NotesPreferenceActivity extends PreferenceActivity {
// 刷新用户界面
private void refreshUI() {
// 加载账户偏好设置
loadAccountPreference();
// 加载同步按钮
loadSyncButton();
}
// 显示选择账户的对话框
private void showSelectAccountAlertDialog() {
// 创建一个AlertDialog.Builder实例
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// 加载自定义的对话框标题布局
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
// 设置对话框标题文本
titleTextView.setText(getString(R.string.preferences_dialog_select_account_title));
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
// 设置对话框副标题文本
subtitleTextView.setText(getString(R.string.preferences_dialog_select_account_tips));
// 设置自定义的对话框标题
dialogBuilder.setCustomTitle(titleView);
// 设置对话框的确定按钮为空,因为我们使用自定义视图来处理选择
dialogBuilder.setCustomTitle(titleView);
dialogBuilder.setPositiveButton(null, null);
// 获取Google账户列表
Account[] accounts = getGoogleAccounts();
// 获取当前设置的同步账户名称
String defAccount = getSyncAccountName(this);
// 保存原始账户列表
mOriAccounts = accounts;
// 重置标记,表示用户尚未添加新账户
mHasAddedAccount = false;
if (accounts.length > 0) {
// 创建一个CharSequence数组来存储账户名称
CharSequence[] items = new CharSequence[accounts.length];
// 由于items是CharSequence数组需要一个final引用以便在内部类中使用
final CharSequence[] itemMapping = items;
int checkedItem = -1;
int index = 0;
// 遍历账户列表将账户名称存储到items数组中并标记默认账户
for (Account account : accounts) {
if (TextUtils.equals(account.name, defAccount)) {
checkedItem = index;
}
items[index++] = account.name;
}
// 设置对话框为单选列表包含所有的Google账户
dialogBuilder.setSingleChoiceItems(items, checkedItem,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 用户选择账户后设置同步账户并刷新UI
setSyncAccount(itemMapping[which].toString());
dialog.dismiss();
refreshUI();
@ -299,16 +242,11 @@ public class NotesPreferenceActivity extends PreferenceActivity {
});
}
// 加载添加账户的文本视图布局
View addAccountView = LayoutInflater.from(this).inflate(R.layout.add_account_text, null);
// 将添加账户的文本视图添加到对话框中
dialogBuilder.setView(addAccountView);
// 显示对话框
final AlertDialog dialog = dialogBuilder.show();
// 设置添加账户文本视图的点击事件,启动系统的添加账户设置界面
addAccountView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mHasAddedAccount = true;
Intent intent = new Intent("android.settings.ADD_ACCOUNT_SETTINGS");
@ -323,72 +261,50 @@ public class NotesPreferenceActivity extends PreferenceActivity {
// 显示更改账户确认对话框
private void showChangeAccountConfirmAlertDialog() {
// 创建一个AlertDialog.Builder实例
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
// 加载自定义的对话框标题布局
View titleView = LayoutInflater.from(this).inflate(R.layout.account_dialog_title, null);
TextView titleTextView = (TextView) titleView.findViewById(R.id.account_dialog_title);
// 设置对话框标题文本,包括当前同步账户名称
titleTextView.setText(getString(R.string.preferences_dialog_change_account_title,
getSyncAccountName(this)));
TextView subtitleTextView = (TextView) titleView.findViewById(R.id.account_dialog_subtitle);
// 设置对话框副标题文本,警告用户切换账户可能带来的风险
subtitleTextView.setText(getString(R.string.preferences_dialog_change_account_warn_msg));
// 设置自定义的对话框标题
dialogBuilder.setCustomTitle(titleView);
// 创建一个CharSequence数组来存储对话框中的菜单项
CharSequence[] menuItemArray = new CharSequence[] {
getString(R.string.preferences_menu_change_account),
getString(R.string.preferences_menu_remove_account),
getString(R.string.preferences_menu_cancel)
};
// 设置对话框的菜单项
dialogBuilder.setItems(menuItemArray, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 根据用户选择的菜单项执行相应的操作
if (which == 0) {
// 如果用户选择更改账户,显示选择账户对话框
showSelectAccountAlertDialog();
} else if (which == 1) {
// 如果用户选择移除账户移除同步账户并刷新UI
removeSyncAccount();
refreshUI();
}
}
});
// 显示对话框
dialogBuilder.show();
}
// 获取Google账户列表
private Account[] getGoogleAccounts() {
// 获取AccountManager实例
AccountManager accountManager = AccountManager.get(this);
// 返回所有类型为"com.google"的账户
return accountManager.getAccountsByType("com.google");
}
// 设置同步账户
private void setSyncAccount(String account) {
// 如果当前设置的同步账户名称与新的账户名称不同
if (!getSyncAccountName(this).equals(account)) {
// 获取SharedPreferences实例
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
// 如果新的账户名称不为空,则将其存储到偏好设置中
if (account != null) {
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, account);
} else {
// 如果新的账户名称为空,则将其设置为空字符串
editor.putString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
}
// 提交偏好设置更改
editor.commit();
// 清除上次同步时间
@ -396,18 +312,14 @@ public class NotesPreferenceActivity extends PreferenceActivity {
// 清除本地Gtask相关信息
new Thread(new Runnable() {
@Override
public void run() {
ContentValues values = new ContentValues();
// 将Gtask ID和SYNC ID设置为空和0
values.put(NoteColumns.GTASK_ID, "");
values.put(NoteColumns.SYNC_ID, 0);
// 更新本地Notes数据库中的相关字段
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
}
}).start();
// 显示成功设置账户的提示信息
Toast.makeText(NotesPreferenceActivity.this,
getString(R.string.preferences_toast_success_set_accout, account),
Toast.LENGTH_SHORT).show();
@ -416,30 +328,22 @@ public class NotesPreferenceActivity extends PreferenceActivity {
// 移除同步账户
private void removeSyncAccount() {
// 获取SharedPreferences实例
SharedPreferences settings = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
// 如果偏好设置中包含同步账户名称,则将其移除
if (settings.contains(PREFERENCE_SYNC_ACCOUNT_NAME)) {
editor.remove(PREFERENCE_SYNC_ACCOUNT_NAME);
}
// 如果偏好设置中包含上次同步时间,则将其移除
if (settings.contains(PREFERENCE_LAST_SYNC_TIME)) {
editor.remove(PREFERENCE_LAST_SYNC_TIME);
}
// 提交偏好设置更改
editor.commit();
// 清除本地Gtask相关信息
new Thread(new Runnable() {
@Override
public void run() {
ContentValues values = new ContentValues();
// 将Gtask ID和SYNC ID设置为空和0
values.put(NoteColumns.GTASK_ID, "");
values.put(NoteColumns.SYNC_ID, 0);
// 更新本地Notes数据库中的相关字段
getContentResolver().update(Notes.CONTENT_NOTE_URI, values, null, null);
}
}).start();
@ -447,66 +351,51 @@ public class NotesPreferenceActivity extends PreferenceActivity {
// 获取同步账户名称
public static String getSyncAccountName(Context context) {
// 获取SharedPreferences实例
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
// 返回存储的同步账户名称,如果未设置则返回空字符串
return settings.getString(PREFERENCE_SYNC_ACCOUNT_NAME, "");
}
// 设置上次同步时间
public static void setLastSyncTime(Context context, long time) {
// 获取SharedPreferences实例
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
// 将上次同步时间存储到偏好设置中
editor.putLong(PREFERENCE_LAST_SYNC_TIME, time);
// 提交偏好设置更改
editor.commit();
}
// 获取上次同步时间
public static long getLastSyncTime(Context context) {
// 获取SharedPreferences实例
SharedPreferences settings = context.getSharedPreferences(PREFERENCE_NAME,
Context.MODE_PRIVATE);
// 返回存储的上次同步时间如果未设置则返回0
return settings.getLong(PREFERENCE_LAST_SYNC_TIME, 0);
}
// 广播接收器,用于接收同步状态更新
private class GTaskReceiver extends BroadcastReceiver {
// 接收到广播时执行的操作
@Override
public void onReceive(Context context, Intent intent) {
// 刷新用户界面,包括账户偏好设置和同步按钮
refreshUI();
// 如果广播中包含同步正在进行的标志
if (intent.getBooleanExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_IS_SYNCING, false)) {
// 找到布局文件中定义的上次同步时间显示文本
TextView syncStatus = (TextView) findViewById(R.id.prefenerece_sync_status_textview);
// 更新文本为同步进度信息
syncStatus.setText(intent
.getStringExtra(GTaskSyncService.GTASK_SERVICE_BROADCAST_PROGRESS_MSG));
}
}
}
// 选项菜单项点击事件处理
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// 如果用户点击了返回按钮启动NotesListActivity并清除当前活动栈中的其他活动
Intent intent = new Intent(this, NotesListActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
// 对于其他选项返回false表示不处理
return false;
}
}

Loading…
Cancel
Save