进一步详细注释了部分内容

src
WisHua 8 months ago
parent 2764b08768
commit 7f380e120f

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

File diff suppressed because it is too large Load Diff

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

@ -7,11 +7,10 @@
* *
* http://www.apache.org/licenses/LICENSE-2.0 * 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; package net.micode.notes.ui;
@ -27,22 +26,23 @@ import net.micode.notes.tool.DataUtils;
// 该类用于从数据库游标中提取笔记项数据,并处理与笔记位置相关的逻辑 // 该类用于从数据库游标中提取笔记项数据,并处理与笔记位置相关的逻辑
public class NoteItemData { public class NoteItemData {
// 定义了查询数据库时要获取的列
static final String [] PROJECTION = new String [] { static final String [] PROJECTION = new String [] {
NoteColumns.ID, NoteColumns.ID, // 笔记项的唯一标识ID
NoteColumns.ALERTED_DATE, NoteColumns.ALERTED_DATE, // 笔记项的提醒日期
NoteColumns.BG_COLOR_ID, NoteColumns.BG_COLOR_ID, // 笔记项的背景颜色ID
NoteColumns.CREATED_DATE, NoteColumns.CREATED_DATE, // 笔记项的创建日期
NoteColumns.HAS_ATTACHMENT, NoteColumns.HAS_ATTACHMENT, // 笔记项是否有附件
NoteColumns.MODIFIED_DATE, NoteColumns.MODIFIED_DATE, // 笔记项的修改日期
NoteColumns.NOTES_COUNT, NoteColumns.NOTES_COUNT, // 笔记项包含的笔记数量
NoteColumns.PARENT_ID, NoteColumns.PARENT_ID, // 笔记项的父ID如果是文件夹下的笔记则是文件夹ID
NoteColumns.SNIPPET, NoteColumns.SNIPPET, // 笔记项的摘要信息
NoteColumns.TYPE, NoteColumns.TYPE, // 笔记项的类型,例如笔记、文件夹等
NoteColumns.WIDGET_ID, NoteColumns.WIDGET_ID, // 与笔记项关联的小部件ID
NoteColumns.WIDGET_TYPE, NoteColumns.WIDGET_TYPE, // 小部件的类型
}; };
// 定义了游标中各个列的索引位置 // 定义了游标中各个列的索引位置,便于后续从游标中获取数据时使用
private static final int ID_COLUMN = 0; private static final int ID_COLUMN = 0;
private static final int ALERTED_DATE_COLUMN = 1; private static final int ALERTED_DATE_COLUMN = 1;
private static final int BG_COLOR_ID_COLUMN = 2; private static final int BG_COLOR_ID_COLUMN = 2;
@ -57,83 +57,84 @@ public class NoteItemData {
private static final int WIDGET_TYPE_COLUMN = 11; private static final int WIDGET_TYPE_COLUMN = 11;
// 笔记项的各种属性 // 笔记项的各种属性
private long mId; private long mId; // 笔记项的唯一标识ID
private long mAlertDate; private long mAlertDate; // 笔记项的提醒日期
private int mBgColorId; private int mBgColorId; // 笔记项的背景颜色ID
private long mCreatedDate; private long mCreatedDate; // 笔记项的创建日期
private boolean mHasAttachment; private boolean mHasAttachment; // 笔记项是否有附件
private long mModifiedDate; private long mModifiedDate; // 笔记项的修改日期
private int mNotesCount; private int mNotesCount; // 笔记项包含的笔记数量
private long mParentId; private long mParentId; // 笔记项的父ID如果是文件夹下的笔记则是文件夹ID
private String mSnippet; private String mSnippet; // 笔记项的摘要信息
private int mType; private int mType; // 笔记项的类型,例如笔记、文件夹等
private int mWidgetId; private int mWidgetId; // 与笔记项关联的小部件ID
private int mWidgetType; private int mWidgetType; // 小部件的类型
private String mName; private String mName; // 笔记项关联的联系人名称
private String mPhoneNumber; private String mPhoneNumber; // 笔记项关联的联系人电话号码
// 笔记项在列表中的位置信息 // 笔记项在列表中的位置信息
private boolean mIsLastItem; private boolean mIsLastItem; // 是否是列表中的最后一个项
private boolean mIsFirstItem; private boolean mIsFirstItem; // 是否是列表中的第一个项
private boolean mIsOnlyOneItem; private boolean mIsOnlyOneItem; // 是否是列表中唯一的项
private boolean mIsOneNoteFollowingFolder; private boolean mIsOneNoteFollowingFolder; // 是否是单个笔记跟在一个文件夹后
private boolean mIsMultiNotesFollowingFolder; private boolean mIsMultiNotesFollowingFolder; // 是否是多个笔记跟在一个文件夹后
// 构造函数,从游标中提取笔记项数据 // 构造函数,从游标中提取笔记项数据
public NoteItemData(Context context, Cursor cursor) { public NoteItemData(Context context, Cursor cursor) {
mId = cursor.getLong(ID_COLUMN); mId = cursor.getLong(ID_COLUMN); // 获取笔记项的ID
mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); mAlertDate = cursor.getLong(ALERTED_DATE_COLUMN); // 获取笔记项的提醒日期
mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); mBgColorId = cursor.getInt(BG_COLOR_ID_COLUMN); // 获取笔记项的背景颜色ID
mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN); mCreatedDate = cursor.getLong(CREATED_DATE_COLUMN); // 获取笔记项的创建日期
mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0) ? true : false; mHasAttachment = (cursor.getInt(HAS_ATTACHMENT_COLUMN) > 0) ? true : false; // 判断笔记项是否有附件
mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN); mModifiedDate = cursor.getLong(MODIFIED_DATE_COLUMN); // 获取笔记项的修改日期
mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN); mNotesCount = cursor.getInt(NOTES_COUNT_COLUMN); // 获取笔记项包含的笔记数量
mParentId = cursor.getLong(PARENT_ID_COLUMN); mParentId = cursor.getLong(PARENT_ID_COLUMN); // 获取笔记项的父ID
mSnippet = cursor.getString(SNIPPET_COLUMN); mSnippet = cursor.getString(SNIPPET_COLUMN); // 获取笔记项的摘要信息
mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace( mSnippet = mSnippet.replace(NoteEditActivity.TAG_CHECKED, "").replace(NoteEditActivity.TAG_UNCHECKED, ""); // 清理摘要中的特定标签
NoteEditActivity.TAG_UNCHECKED, ""); mType = cursor.getInt(TYPE_COLUMN); // 获取笔记项的类型
mType = cursor.getInt(TYPE_COLUMN); mWidgetId = cursor.getInt(WIDGET_ID_COLUMN); // 获取笔记项的小部件ID
mWidgetId = cursor.getInt(WIDGET_ID_COLUMN); mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN); // 获取笔记项的小部件类型
mWidgetType = cursor.getInt(WIDGET_TYPE_COLUMN);
mPhoneNumber = ""; // 初始化电话号码为空字符串
mPhoneNumber = ""; if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { // 如果笔记项的父ID是呼叫记录文件夹的ID
if (mParentId == Notes.ID_CALL_RECORD_FOLDER) { mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); // 通过笔记ID获取对应的呼叫记录的电话号码
mPhoneNumber = DataUtils.getCallNumberByNoteId(context.getContentResolver(), mId); if (!TextUtils.isEmpty(mPhoneNumber)) { // 如果电话号码不为空
if (!TextUtils.isEmpty(mPhoneNumber)) { mName = Contact.getContact(context, mPhoneNumber); // 通过电话号码查询联系人信息
mName = Contact.getContact(context, mPhoneNumber); if (mName == null) { // 如果没有找到对应的联系人名称
if (mName == null) { mName = mPhoneNumber; // 则将电话号码作为名称
mName = mPhoneNumber;
} }
} }
} }
if (mName == null) { if (mName == null) { // 如果联系人名称依然为null
mName = ""; mName = ""; // 初始化为空字符串
} }
checkPostion(cursor); checkPostion(cursor); // 检查笔记项在列表中的位置信息
} }
// 检查笔记项在列表中的位置信息 // 检查笔记项在列表中的位置信息
private void checkPostion(Cursor cursor) { private void checkPostion(Cursor cursor) {
mIsLastItem = cursor.isLast() ? true : false; mIsLastItem = cursor.isLast() ? true : false; // 设置是否是列表中的最后一个项
mIsFirstItem = cursor.isFirst() ? true : false; mIsFirstItem = cursor.isFirst() ? true : false; // 设置是否是列表中的第一个项
mIsOnlyOneItem = (cursor.getCount() == 1); mIsOnlyOneItem = (cursor.getCount() == 1); // 如果列表中的项数为1则设置为唯一项
mIsMultiNotesFollowingFolder = false; mIsMultiNotesFollowingFolder = false; // 初始化是否是多个笔记跟在一个文件夹后为false
mIsOneNoteFollowingFolder = false; mIsOneNoteFollowingFolder = false; // 初始化是否是单个笔记跟在一个文件夹后为false
// 如果笔记项类型是笔记且不是列表中的第一个项
if (mType == Notes.TYPE_NOTE && !mIsFirstItem) { if (mType == Notes.TYPE_NOTE && !mIsFirstItem) {
int position = cursor.getPosition(); int position = cursor.getPosition(); // 获取当前笔记项的位置
if (cursor.moveToPrevious()) { if (cursor.moveToPrevious()) { // 将游标移动到前一行
if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER // 如果前一行的类型是文件夹或系统类型
|| cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) { if (cursor.getInt(TYPE_COLUMN) == Notes.TYPE_FOLDER || cursor.getInt(TYPE_COLUMN) == Notes.TYPE_SYSTEM) {
// 如果列表中的项数大于当前位置加1
if (cursor.getCount() > (position + 1)) { if (cursor.getCount() > (position + 1)) {
mIsMultiNotesFollowingFolder = true; mIsMultiNotesFollowingFolder = true; // 设置为多个笔记跟在一个文件夹后
} else { } else {
mIsOneNoteFollowingFolder = true; mIsOneNoteFollowingFolder = true; // 设置为单个笔记跟在一个文件夹后
} }
} }
if (!cursor.moveToNext()) { if (!cursor.moveToNext()) { // 将游标移动回原来的位置
throw new IllegalStateException("cursor move to previous but can't move back"); throw new IllegalStateException("cursor move to previous but can't move back"); // 如果移动失败,抛出异常
} }
} }
} }
@ -141,111 +142,112 @@ public class NoteItemData {
// 判断该笔记项是否是单个笔记跟在一个文件夹后 // 判断该笔记项是否是单个笔记跟在一个文件夹后
public boolean isOneFollowingFolder() { public boolean isOneFollowingFolder() {
return mIsOneNoteFollowingFolder; return mIsOneNoteFollowingFolder; // 返回是否是单个笔记跟在一个文件夹后的结果
} }
// 判断该笔记项是否是多个笔记跟在一个文件夹后 // 判断该笔记项是否是多个笔记跟在一个文件夹后
public boolean isMultiFollowingFolder() { public boolean isMultiFollowingFolder() {
return mIsMultiNotesFollowingFolder; return mIsMultiNotesFollowingFolder; // 返回是否是多个笔记跟在一个文件夹后的结果
} }
// 判断该笔记项是否是列表中的最后一个项 // 判断该笔记项是否是列表中的最后一个项
public boolean isLast() { public boolean isLast() {
return mIsLastItem; return mIsLastItem; // 返回是否是列表中最后一个项的结果
} }
// 获取与该笔记项关联的呼叫记录的联系人名称 // 获取与该笔记项关联的呼叫记录的联系人名称
public String getCallName() { public String getCallName() {
return mName; return mName; // 返回关联的联系人名称
} }
// 判断该笔记项是否是列表中的第一个项 // 判断该笔记项是否是列表中的第一个项
public boolean isFirst() { public boolean isFirst() {
return mIsFirstItem; return mIsFirstItem; // 返回是否是列表中第一个项的结果
} }
// 判断该笔记项是否是列表中唯一的项 // 判断该笔记项是否是列表中唯一的项
public boolean isSingle() { public boolean isSingle() {
return mIsOnlyOneItem; return mIsOnlyOneItem; // 返回是否是列表中唯一项的结果
} }
// 获取笔记项的ID // 获取笔记项的ID
public long getId() { public long getId() {
return mId; return mId; // 返回笔记项的ID
} }
// 获取笔记项的提醒日期 // 获取笔记项的提醒日期
public long getAlertDate() { public long getAlertDate() {
return mAlertDate; return mAlertDate; // 返回笔记项的提醒日期
} }
// 获取笔记项的创建日期 // 获取笔记项的创建日期
public long getCreatedDate() { public long getCreatedDate() {
return mCreatedDate; return mCreatedDate; // 返回笔记项的创建日期
} }
// 判断该笔记项是否有附件 // 判断该笔记项是否有附件
public boolean hasAttachment() { public boolean hasAttachment() {
return mHasAttachment; return mHasAttachment; // 返回笔记项是否有附件的结果
} }
// 获取笔记项的修改日期 // 获取笔记项的修改日期
public long getModifiedDate() { public long getModifiedDate() {
return mModifiedDate; return mModifiedDate; // 返回笔记项的修改日期
} }
// 获取笔记项的背景颜色ID // 获取笔记项的背景颜色ID
public int getBgColorId() { public int getBgColorId() {
return mBgColorId; return mBgColorId; // 返回笔记项的背景颜色ID
} }
// 获取笔记项的父ID // 获取笔记项的父ID
public long getParentId() { public long getParentId() {
return mParentId; return mParentId; // 返回笔记项的父ID
} }
// 获取笔记项包含的笔记数量 // 获取笔记项包含的笔记数量
public int getNotesCount() { public int getNotesCount() {
return mNotesCount; return mNotesCount; // 返回笔记项包含的笔记数量
} }
// 获取笔记项所在的文件夹ID // 获取笔记项所在的文件夹ID
public long getFolderId () { public long getFolderId () {
return mParentId; return mParentId; // 返回笔记项所在的文件夹ID与getParentId相同
} }
// 获取笔记项的类型 // 获取笔记项的类型
public int getType() { public int getType() {
return mType; return mType; // 返回笔记项的类型
} }
// 获取笔记项的小部件类型 // 获取笔记项的小部件类型
public int getWidgetType() { public int getWidgetType() {
return mWidgetType; return mWidgetType; // 返回笔记项的小部件类型
} }
// 获取笔记项的小部件ID // 获取笔记项的小部件ID
public int getWidgetId() { public int getWidgetId() {
return mWidgetId; return mWidgetId; // 返回笔记项的小部件ID
} }
// 获取笔记项的摘要 // 获取笔记项的摘要
public String getSnippet() { public String getSnippet() {
return mSnippet; return mSnippet; // 返回笔记项的摘要
} }
// 判断该笔记项是否有提醒 // 判断该笔记项是否有提醒
public boolean hasAlert() { public boolean hasAlert() {
return (mAlertDate > 0); return (mAlertDate > 0); // 如果提醒日期大于0表示有提醒
} }
// 判断该笔记项是否是呼叫记录类型 // 判断该笔记项是否是呼叫记录类型
public boolean isCallRecord() { public boolean isCallRecord() {
// 如果父ID是呼叫记录文件夹的ID且电话号码不为空则是呼叫记录类型
return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber)); return (mParentId == Notes.ID_CALL_RECORD_FOLDER && !TextUtils.isEmpty(mPhoneNumber));
} }
// 静态方法,从游标中获取笔记项的类型 // 静态方法,从游标中获取笔记项的类型
public static int getNoteType(Cursor cursor) { 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,200 +1,193 @@
/* /*
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net) * (c) 2010-2011, MiCode (www.micode.net)
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Apache2.0
* 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; package net.micode.notes.ui;
import android.content.Context; import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.CursorAdapter; import android.widget.CursorAdapter;
import net.micode.notes.data.Notes; import net.micode.notes.data.Notes;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
// 自定义的CursorAdapter用于显示笔记列表 // 自定义的CursorAdapter用于显示笔记列表
public class NotesListAdapter extends CursorAdapter { public class NotesListAdapter extends CursorAdapter {
private static final String TAG = "NotesListAdapter"; private static final String TAG = "NotesListAdapter";
private Context mContext; private Context mContext; // 上下文环境
private HashMap<Integer, Boolean> mSelectedIndex; private HashMap<Integer, Boolean> mSelectedIndex; // 记录选中的笔记位置
private int mNotesCount; private int mNotesCount; // 笔记总数
private boolean mChoiceMode; private boolean mChoiceMode; // 是否处于多选模式
// 用于存储小部件属性的内部类 // 用于存储小部件属性的内部类
public static class AppWidgetAttribute { public static class AppWidgetAttribute {
public int widgetId; public int widgetId; // 小部件ID
public int widgetType; public int widgetType; // 小部件类型
}; };
// 构造函数,初始化上下文和选择索引 // 构造函数,初始化上下文和选择索引
public NotesListAdapter(Context context) { public NotesListAdapter(Context context) {
super(context, null); super(context, null); // 调用父类构造函数,不立即绑定任何游标
mSelectedIndex = new HashMap<Integer, Boolean>(); mSelectedIndex = new HashMap<Integer, Boolean>(); // 初始化选择索引
mContext = context; mContext = context; // 初始化上下文
mNotesCount = 0; mNotesCount = 0; // 初始化笔记数量为0
} }
// 创建新的视图项 // 创建新的视图项
@Override @Override
public View newView(Context context, Cursor cursor, ViewGroup parent) { public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new NotesListItem(context); return new NotesListItem(context); // 创建一个新的NotesListItem视图
} }
// 绑定数据到视图项 // 绑定数据到视图项
@Override @Override
public void bindView(View view, Context context, Cursor cursor) { public void bindView(View view, Context context, Cursor cursor) {
if (view instanceof NotesListItem) { if (view instanceof NotesListItem) { // 检查视图是否为NotesListItem类型
NoteItemData itemData = new NoteItemData(context, cursor); NoteItemData itemData = new NoteItemData(context, cursor); // 创建NoteItemData对象用于封装笔记数据
((NotesListItem) view).bind(context, itemData, mChoiceMode, ((NotesListItem) view).bind(context, itemData, mChoiceMode, // 调用NotesListItem的bind方法绑定数据
isSelectedItem(cursor.getPosition())); isSelectedItem(cursor.getPosition())); // 传递当前项是否被选中的信息
} }
} }
// 设置指定位置的项是否被选中,并通知数据集发生变化 // 设置指定位置的项是否被选中,并通知数据集发生变化
public void setCheckedItem(final int position, final boolean checked) { public void setCheckedItem(final int position, final boolean checked) {
mSelectedIndex.put(position, checked); mSelectedIndex.put(position, checked); // 更新选中状态
notifyDataSetChanged(); notifyDataSetChanged(); // 通知数据集发生变化刷新UI
} }
// 检查当前是否处于多选模式 // 检查当前是否处于多选模式
public boolean isInChoiceMode() { public boolean isInChoiceMode() {
return mChoiceMode; return mChoiceMode; // 返回当前的多选模式状态
} }
// 设置多选模式,清空选择索引 // 设置多选模式,清空选择索引
public void setChoiceMode(boolean mode) { public void setChoiceMode(boolean mode) {
mSelectedIndex.clear(); mSelectedIndex.clear(); // 清空选择索引
mChoiceMode = mode; mChoiceMode = mode; // 更新多选模式状态
} }
// 全选或全不选所有笔记 // 全选或全不选所有笔记
public void selectAll(boolean checked) { public void selectAll(boolean checked) {
Cursor cursor = getCursor(); Cursor cursor = getCursor(); // 获取当前绑定的游标
for (int i = 0; i < getCount(); i++) { for (int i = 0; i < getCount(); i++) { // 遍历所有笔记
if (cursor.moveToPosition(i)) { if (cursor.moveToPosition(i)) { // 移动到指定位置
if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { if (NoteItemData.getNoteType(cursor) == Notes.TYPE_NOTE) { // 检查是否为笔记类型
setCheckedItem(i, checked); setCheckedItem(i, checked); // 设置选中状态
} }
} }
} }
} }
// 获取所有选中的笔记ID集合 // 获取所有选中的笔记ID集合
public HashSet<Long> getSelectedItemIds() { public HashSet<Long> getSelectedItemIds() {
HashSet<Long> itemSet = new HashSet<Long>(); HashSet<Long> itemSet = new HashSet<Long>(); // 初始化选中笔记ID集合
for (Integer position : mSelectedIndex.keySet()) { for (Integer position : mSelectedIndex.keySet()) { // 遍历所有选中的位置
if (mSelectedIndex.get(position) == true) { if (mSelectedIndex.get(position) == true) { // 检查位置是否被选中
Long id = getItemId(position); Long id = getItemId(position); // 获取笔记ID
if (id == Notes.ID_ROOT_FOLDER) { if (id == Notes.ID_ROOT_FOLDER) { // 检查是否为根文件夹ID错误情况
Log.d(TAG, "Wrong item id, should not happen"); Log.d(TAG, "Wrong item id, should not happen"); // 记录日志
} else { } else {
itemSet.add(id); itemSet.add(id); // 将笔记ID添加到集合
} }
} }
} }
return itemSet; // 返回选中笔记ID集合
return itemSet;
} }
// 获取所有选中的小部件属性集合 // 获取所有选中的小部件属性集合
public HashSet<AppWidgetAttribute> getSelectedWidget() { public HashSet<AppWidgetAttribute> getSelectedWidget() {
HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>(); HashSet<AppWidgetAttribute> itemSet = new HashSet<AppWidgetAttribute>(); // 初始化选中小部件属性集合
for (Integer position : mSelectedIndex.keySet()) { for (Integer position : mSelectedIndex.keySet()) { // 遍历所有选中的位置
if (mSelectedIndex.get(position) == true) { if (mSelectedIndex.get(position) == true) { // 检查位置是否被选中
Cursor c = (Cursor) getItem(position); Cursor c = (Cursor) getItem(position); // 获取对应位置的游标
if (c != null) { if (c != null) { // 检查游标是否有效
AppWidgetAttribute widget = new AppWidgetAttribute(); AppWidgetAttribute widget = new AppWidgetAttribute(); // 创建一个新的小部件属性对象
NoteItemData item = new NoteItemData(mContext, c); NoteItemData item = new NoteItemData(mContext, c); // 创建NoteItemData对象用于封装笔记数据
widget.widgetId = item.getWidgetId(); widget.widgetId = item.getWidgetId(); // 设置小部件ID
widget.widgetType = item.getWidgetType(); widget.widgetType = item.getWidgetType(); // 设置小部件类型
itemSet.add(widget); itemSet.add(widget); // 将小部件属性对象添加到集合
/** /**
* Don't close cursor here, only the adapter could close it * Don't close cursor here, only the adapter could close it
*
*/ */
} else { } else {
Log.e(TAG, "Invalid cursor"); Log.e(TAG, "Invalid cursor"); // 记录无效游标日志
return null; return null; // 返回null
} }
} }
} }
return itemSet; return itemSet; // 返回选中小部件属性集合
} }
// 获取选中的笔记数量 // 获取选中的笔记数量
public int getSelectedCount() { public int getSelectedCount() {
Collection<Boolean> values = mSelectedIndex.values(); Collection<Boolean> values = mSelectedIndex.values(); // 获取所有选中状态值
if (null == values) { if (null == values) {
return 0; return 0; // 如果选中状态值为空返回0
} }
Iterator<Boolean> iter = values.iterator(); Iterator<Boolean> iter = values.iterator(); // 创建迭代器
int count = 0; int count = 0; // 初始化计数器
while (iter.hasNext()) { while (iter.hasNext()) {
if (true == iter.next()) { if (true == iter.next()) { // 检查是否为选中状态
count++; count++; // 计数器加一
} }
} }
return count; return count; // 返回选中的笔记数量
} }
// 检查是否所有笔记都被选中 // 检查是否所有笔记都被选中
public boolean isAllSelected() { public boolean isAllSelected() {
int checkedCount = getSelectedCount(); int checkedCount = getSelectedCount(); // 获取选中的笔记数量
return (checkedCount != 0 && checkedCount == mNotesCount); return (checkedCount != 0 && checkedCount == mNotesCount); // 检查选中的笔记数量是否等于笔记总数
} }
// 检查指定位置的项是否被选中 // 检查指定位置的项是否被选中
public boolean isSelectedItem(final int position) { public boolean isSelectedItem(final int position) {
if (null == mSelectedIndex.get(position)) { if (null == mSelectedIndex.get(position)) { // 检查指定位置是否被选中
return false; return false; // 如果没有被选中返回false
} }
return mSelectedIndex.get(position); return mSelectedIndex.get(position); // 返回选中状态
} }
// 当数据内容发生变化时,更新笔记数量 // 当数据内容发生变化时,更新笔记数量
@Override @Override
protected void onContentChanged() { protected void onContentChanged() {
super.onContentChanged(); super.onContentChanged(); // 调用父类方法
calcNotesCount(); calcNotesCount(); // 计算笔记数量
} }
// 更改Cursor时更新笔记数量 // 更改Cursor时更新笔记数量
@Override @Override
public void changeCursor(Cursor cursor) { public void changeCursor(Cursor cursor) {
super.changeCursor(cursor); super.changeCursor(cursor); // 调用父类方法
calcNotesCount(); calcNotesCount(); // 计算笔记数量
} }
// 计算笔记数量 // 计算笔记数量
private void calcNotesCount() { private void calcNotesCount() {
mNotesCount = 0; mNotesCount = 0; // 初始化笔记数量为0
for (int i = 0; i < getCount(); i++) { for (int i = 0; i < getCount(); i++) { // 遍历所有笔记
Cursor c = (Cursor) getItem(i); Cursor c = (Cursor) getItem(i); // 获取对应位置的游标
if (c != null) { if (c != null) { // 检查游标是否有效
if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { if (NoteItemData.getNoteType(c) == Notes.TYPE_NOTE) { // 检查是否为笔记类型
mNotesCount++; mNotesCount++; // 计数器加一
} }
} else { } else {
Log.e(TAG, "Invalid cursor"); Log.e(TAG, "Invalid cursor"); // 记录无效游标日志
return; return; // 返回
} }
} }
} }
} }

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

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

@ -1,7 +1,7 @@
张晋菡: 张晋菡:
ui中14段代码 ui中后11段代码
程星桦: 程星桦:
ui中前3段代码
张鹏展: 张鹏展:
gtask中exception和remote中共6段代码 gtask中exception和remote中共6段代码
tool中4段代码 tool中4段代码

Loading…
Cancel
Save