NoteEditActivity精读

pull/4/head
Ryuki 2 years ago
parent 0dec88e9e3
commit d8a282d65d

@ -71,13 +71,13 @@ import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
//该类主要针对标签的编辑,是Activity的一个子类
//实现了了系统内许多与监听有关的接口
public class NoteEditActivity extends Activity implements OnClickListener, public class NoteEditActivity extends Activity implements OnClickListener,
NoteSettingChangedListener, OnTextViewChangeListener { NoteSettingChangedListener, OnTextViewChangeListener {
//该类主要针对标签的编辑,是Activity的一个子类
//继承了系统内许多与监听有关的类
private class HeadViewHolder { private class HeadViewHolder {
public TextView tvModified; public TextView tvModified; //
public ImageView ivAlertIcon; public ImageView ivAlertIcon;
@ -92,8 +92,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
sBgSelectorBtnsMap.put(R.id.iv_bg_red, ResourceParser.RED); sBgSelectorBtnsMap.put(R.id.iv_bg_red, ResourceParser.RED);
sBgSelectorBtnsMap.put(R.id.iv_bg_blue, ResourceParser.BLUE); sBgSelectorBtnsMap.put(R.id.iv_bg_blue, ResourceParser.BLUE);
sBgSelectorBtnsMap.put(R.id.iv_bg_green, ResourceParser.GREEN); sBgSelectorBtnsMap.put(R.id.iv_bg_green, ResourceParser.GREEN);
sBgSelectorBtnsMap.put(R.id.iv_bg_white, ResourceParser.WHITE); sBgSelectorBtnsMap.put(R.id.iv_bg_white, ResourceParser.WHITE); //put函数是将指定值与指定键相连
//put函数是将指定值与指定键相连
} }
private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>(); private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>();
@ -102,8 +102,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
sBgSelectorSelectionMap.put(ResourceParser.RED, R.id.iv_bg_red_select); sBgSelectorSelectionMap.put(ResourceParser.RED, R.id.iv_bg_red_select);
sBgSelectorSelectionMap.put(ResourceParser.BLUE, R.id.iv_bg_blue_select); sBgSelectorSelectionMap.put(ResourceParser.BLUE, R.id.iv_bg_blue_select);
sBgSelectorSelectionMap.put(ResourceParser.GREEN, R.id.iv_bg_green_select); sBgSelectorSelectionMap.put(ResourceParser.GREEN, R.id.iv_bg_green_select);
sBgSelectorSelectionMap.put(ResourceParser.WHITE, R.id.iv_bg_white_select); sBgSelectorSelectionMap.put(ResourceParser.WHITE, R.id.iv_bg_white_select); //put函数是将指定值与指定键相连
//put函数是将指定值与指定键相连
} }
private static final Map<Integer, Integer> sFontSizeBtnsMap = new HashMap<Integer, Integer>(); private static final Map<Integer, Integer> sFontSizeBtnsMap = new HashMap<Integer, Integer>();
@ -111,8 +111,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
sFontSizeBtnsMap.put(R.id.ll_font_large, ResourceParser.TEXT_LARGE); sFontSizeBtnsMap.put(R.id.ll_font_large, ResourceParser.TEXT_LARGE);
sFontSizeBtnsMap.put(R.id.ll_font_small, ResourceParser.TEXT_SMALL); sFontSizeBtnsMap.put(R.id.ll_font_small, ResourceParser.TEXT_SMALL);
sFontSizeBtnsMap.put(R.id.ll_font_normal, ResourceParser.TEXT_MEDIUM); sFontSizeBtnsMap.put(R.id.ll_font_normal, ResourceParser.TEXT_MEDIUM);
sFontSizeBtnsMap.put(R.id.ll_font_super, ResourceParser.TEXT_SUPER); sFontSizeBtnsMap.put(R.id.ll_font_super, ResourceParser.TEXT_SUPER); //put函数是将指定值与指定键相连
//put函数是将指定值与指定键相连
} }
private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>(); private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>();
@ -120,32 +120,24 @@ public class NoteEditActivity extends Activity implements OnClickListener,
sFontSelectorSelectionMap.put(ResourceParser.TEXT_LARGE, R.id.iv_large_select); sFontSelectorSelectionMap.put(ResourceParser.TEXT_LARGE, R.id.iv_large_select);
sFontSelectorSelectionMap.put(ResourceParser.TEXT_SMALL, R.id.iv_small_select); sFontSelectorSelectionMap.put(ResourceParser.TEXT_SMALL, R.id.iv_small_select);
sFontSelectorSelectionMap.put(ResourceParser.TEXT_MEDIUM, R.id.iv_medium_select); sFontSelectorSelectionMap.put(ResourceParser.TEXT_MEDIUM, R.id.iv_medium_select);
sFontSelectorSelectionMap.put(ResourceParser.TEXT_SUPER, R.id.iv_super_select); sFontSelectorSelectionMap.put(ResourceParser.TEXT_SUPER, R.id.iv_super_select); //put函数是将指定值与指定键相连
//put函数是将指定值与指定键相连
} }
private static final String TAG = "NoteEditActivity"; private static final String TAG = "NoteEditActivity";
private HeadViewHolder mNoteHeaderHolder; private HeadViewHolder mNoteHeaderHolder;
private View mHeadViewPanel; private View mHeadViewPanel; //私有化一个界面操作mHeadViewPanel对表头的操作
//私有化一个界面操作mHeadViewPanel对表头的操作 private View mNoteBgColorSelector; //私有化一个界面操作mNoteBgColorSelector对背景颜色的操作
private View mNoteBgColorSelector; private View mFontSizeSelector; //私有化一个界面操作mFontSizeSelector对标签字体的操作
//私有化一个界面操作mNoteBgColorSelector对背景颜色的操作 private EditText mNoteEditor; //声明编辑控件,对文本操作
private View mFontSizeSelector; private View mNoteEditorPanel; //私有化一个界面操作mNoteEditorPanel文本编辑的控制板
//私有化一个界面操作mFontSizeSelector对标签字体的操作
private EditText mNoteEditor; private WorkingNote mWorkingNote; //对模板WorkingNote的初始化
//声明编辑控件,对文本操作 private SharedPreferences mSharedPrefs; //私有化SharedPreferences的数据存储方式,它的本质是基于XML文件存储key-value键值对数据
private View mNoteEditorPanel; private int mFontSizeId; //用于操作字体的大小
//私有化一个界面操作mNoteEditorPanel文本编辑的控制板
//private WorkingNote mWorkingNote;
private WorkingNote mWorkingNote;
//对模板WorkingNote的初始化
private SharedPreferences mSharedPrefs;
//私有化SharedPreferences的数据存储方式
//它的本质是基于XML文件存储key-value键值对数据
private int mFontSizeId;
//用于操作字体的大小
private static final String PREFERENCE_FONT_SIZE = "pref_font_size"; private static final String PREFERENCE_FONT_SIZE = "pref_font_size";
private static final int SHORTCUT_ICON_TITLE_MAX_LEN = 10; private static final int SHORTCUT_ICON_TITLE_MAX_LEN = 10;
@ -153,8 +145,8 @@ public class NoteEditActivity extends Activity implements OnClickListener,
public static final String TAG_CHECKED = String.valueOf('\u221A'); public static final String TAG_CHECKED = String.valueOf('\u221A');
public static final String TAG_UNCHECKED = String.valueOf('\u25A1'); public static final String TAG_UNCHECKED = String.valueOf('\u25A1');
private LinearLayout mEditTextList; private LinearLayout mEditTextList; //线性布局
//线性布局
private String mUserQuery; private String mUserQuery;
private Pattern mPattern; private Pattern mPattern;
@ -162,8 +154,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
this.setContentView(R.layout.note_edit); this.setContentView(R.layout.note_edit);
//对数据库的访问 if (savedInstanceState == null && !initActivityState(getIntent())) { //对数据库的访问
if (savedInstanceState == null && !initActivityState(getIntent())) {
finish(); finish();
return; return;
} }
@ -185,7 +176,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
return; return;
} }
Log.d(TAG, "Restoring from killed activity"); Log.d(TAG, "Restoring from killed activity");
}//防止内存不足时的程序终止,保存现场的函数 } //防止内存不足时的程序终止,保存现场的函数
} }
private boolean initActivityState(Intent intent) { private boolean initActivityState(Intent intent) {
@ -193,31 +184,31 @@ public class NoteEditActivity extends Activity implements OnClickListener,
* If the user specified the {@link Intent#ACTION_VIEW} but not provided with id, * If the user specified the {@link Intent#ACTION_VIEW} but not provided with id,
* then jump to the NotesListActivity * then jump to the NotesListActivity
*/ */
// 在没有特定note ID的情况下Intent.ACTION VIEW被触发那么转向 NotesListActivity
mWorkingNote = null; mWorkingNote = null;
if (TextUtils.equals(Intent.ACTION_VIEW, intent.getAction())) { if (TextUtils.equals(Intent.ACTION_VIEW, intent.getAction())) {
long noteId = intent.getLongExtra(Intent.EXTRA_UID, 0); long noteId = intent.getLongExtra(Intent.EXTRA_UID, 0); //如果用户实例化标签时系统并未给出标签ID
mUserQuery = ""; mUserQuery = "";
//如果用户实例化标签时系统并未给出标签ID
/** /**
* Starting from the searched result * Starting from the searched result
*/ */
//根据键值查找ID //根据键值查找ID,如果Intent~ 中存在搜索管理器的额外键值,则根据该键值检索 ID
if (intent.hasExtra(SearchManager.EXTRA_DATA_KEY)) { if (intent.hasExtra(SearchManager.EXTRA_DATA_KEY)) {
noteId = Long.parseLong(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY)); noteId = Long.parseLong(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
mUserQuery = intent.getStringExtra(SearchManager.USER_QUERY); mUserQuery = intent.getStringExtra(SearchManager.USER_QUERY);
} }
//如果没有在数据库中找到ID // 如果该笔记ID不在数据库中则跳转至笔记列表并提示错误
if (!DataUtils.visibleInNoteDatabase(getContentResolver(), noteId, Notes.TYPE_NOTE)) { if (!DataUtils.visibleInNoteDatabase(getContentResolver(), noteId, Notes.TYPE_NOTE)) {
Intent jump = new Intent(this, NotesListActivity.class); Intent jump = new Intent(this, NotesListActivity.class);
startActivity(jump); startActivity(jump);
//程序将跳转到声明的intent-jump showToast(R.string.error_note_not_exist);//程序将跳转到声明的intent-jump
showToast(R.string.error_note_not_exist);
finish(); finish();
return false; return false;
} }
//如果在数据库中找到了ID //如果在数据库中找到了ID,则加载笔记
else { else {
mWorkingNote = WorkingNote.load(this, noteId); mWorkingNote = WorkingNote.load(this, noteId);
// 若加载失败,则记录错误并结束该 Activity
if (mWorkingNote == null) { if (mWorkingNote == null) {
Log.e(TAG, "load note failed with note id" + noteId); Log.e(TAG, "load note failed with note id" + noteId);
//打印出红色的错误信息 //打印出红色的错误信息
@ -225,7 +216,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
return false; return false;
} }
} }
//setSoftInputMode-软键盘输入模式 //setSoftInputMode-软键盘输入模式,初始化软键盘输入模式,隐藏软键盘,同时在需要时调整布局大小
getWindow().setSoftInputMode( getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
@ -234,6 +225,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
// intent.getAction() // intent.getAction()
// 大多用于broadcast发送广播时给机制intent设置一个action就是一个字符串 // 大多用于broadcast发送广播时给机制intent设置一个action就是一个字符串
// 用户可以通过receive接受intent通过 getAction得到的字符串来决定做什么 // 用户可以通过receive接受intent通过 getAction得到的字符串来决定做什么
// 在Intent.ACTION INSERT OR EDIT 的动作下创建新笔记或编辑现有笔记
long folderId = intent.getLongExtra(Notes.INTENT_EXTRA_FOLDER_ID, 0); long folderId = intent.getLongExtra(Notes.INTENT_EXTRA_FOLDER_ID, 0);
int widgetId = intent.getIntExtra(Notes.INTENT_EXTRA_WIDGET_ID, int widgetId = intent.getIntExtra(Notes.INTENT_EXTRA_WIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID); AppWidgetManager.INVALID_APPWIDGET_ID);
@ -242,7 +234,7 @@ public class NoteEditActivity extends Activity implements OnClickListener,
int bgResId = intent.getIntExtra(Notes.INTENT_EXTRA_BACKGROUND_ID, int bgResId = intent.getIntExtra(Notes.INTENT_EXTRA_BACKGROUND_ID,
ResourceParser.getDefaultBgId(this)); ResourceParser.getDefaultBgId(this));
// intent.getIntLong、StringExtra是对各变量的语法分析 // intent.getIntLong、StringExtra是对各变量的语法分析
// Parse call-record note //解析来电记录笔记相关数据
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
long callDate = intent.getLongExtra(Notes.INTENT_EXTRA_CALL_DATE, 0); long callDate = intent.getLongExtra(Notes.INTENT_EXTRA_CALL_DATE, 0);
if (callDate != 0 && phoneNumber != null) { if (callDate != 0 && phoneNumber != null) {
@ -250,33 +242,42 @@ public class NoteEditActivity extends Activity implements OnClickListener,
Log.w(TAG, "The call record number is null"); Log.w(TAG, "The call record number is null");
} }
long noteId = 0; long noteId = 0;
//通过电话号码和通话日期查询笔记ID
if ((noteId = DataUtils.getNoteIdByPhoneNumberAndCallDate(getContentResolver(), if ((noteId = DataUtils.getNoteIdByPhoneNumberAndCallDate(getContentResolver(),
phoneNumber, callDate)) > 0) { phoneNumber, callDate)) > 0) {
//如果查询成功,则加载来电记录笔记
mWorkingNote = WorkingNote.load(this, noteId); mWorkingNote = WorkingNote.load(this, noteId);
//加载失败则记录错误并结束Activity
if (mWorkingNote == null) { if (mWorkingNote == null) {
Log.e(TAG, "load call note failed with note id" + noteId); Log.e(TAG, "load call note failed with note id" + noteId);
finish(); finish();
return false; return false;
} }
//将电话号码与手机的号码簿相关 //将电话记录和联系人相关
} else { } else {
//如果没有相关来电记录笔记,则创建一个空笔记
mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId, mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId,
widgetType, bgResId); widgetType, bgResId);
//转换该空笔记记为来电记录笔记
mWorkingNote.convertToCallNote(phoneNumber, callDate); mWorkingNote.convertToCallNote(phoneNumber, callDate);
} }
} else { } else {
//如果不是来电记录,则创建一个新的空笔记
mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId, widgetType, mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId, widgetType,
bgResId); bgResId);
}//创建一个新的WorkingNote } //创建一个新的WorkingNote
// 初始化软键盘输入模式,保持软键盘可见,同时在需要时调整布局大小
getWindow().setSoftInputMode( getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
| WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE); | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
} else { } else {
//如果'Intent'不包含已知动作则记录错误并结束Activity
Log.e(TAG, "Intent not specified action, should not support"); Log.e(TAG, "Intent not specified action, should not support");
finish(); finish();
return false; return false;
} }
//为工作笔记设置状态改变监听器
mWorkingNote.setOnSettingStatusChangedListener(this); mWorkingNote.setOnSettingStatusChangedListener(this);
return true; return true;
} }
@ -284,59 +285,73 @@ public class NoteEditActivity extends Activity implements OnClickListener,
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
initNoteScreen(); initNoteScreen(); //调用方法初始化笔记屏幕
} }
//初始化笔记界面的方法
private void initNoteScreen() { private void initNoteScreen() {
//设置笔记编辑器的文字样式
mNoteEditor.setTextAppearance(this, TextAppearanceResources mNoteEditor.setTextAppearance(this, TextAppearanceResources
.getTexAppearanceResource(mFontSizeId)); .getTexAppearanceResource(mFontSizeId));
//判断是否是清单模式,进行相应的试图转换
if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) { if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) {
switchToListMode(mWorkingNote.getContent()); switchToListMode(mWorkingNote.getContent());
} else { } else {
//设置文本编辑器的文本并突出显示查询结果
mNoteEditor.setText(getHighlightQueryResult(mWorkingNote.getContent(), mUserQuery)); mNoteEditor.setText(getHighlightQueryResult(mWorkingNote.getContent(), mUserQuery));
//将光标设置到文本末尾
mNoteEditor.setSelection(mNoteEditor.getText().length()); mNoteEditor.setSelection(mNoteEditor.getText().length());
} }
//隐藏所有背景选择器试图
for (Integer id : sBgSelectorSelectionMap.keySet()) { for (Integer id : sBgSelectorSelectionMap.keySet()) {
findViewById(sBgSelectorSelectionMap.get(id)).setVisibility(View.GONE); findViewById(sBgSelectorSelectionMap.get(id)).setVisibility(View.GONE);
} }
//设置标题和笔记编辑器面板的背景资源
mHeadViewPanel.setBackgroundResource(mWorkingNote.getTitleBgResId()); mHeadViewPanel.setBackgroundResource(mWorkingNote.getTitleBgResId());
mNoteEditorPanel.setBackgroundResource(mWorkingNote.getBgColorResId()); mNoteEditorPanel.setBackgroundResource(mWorkingNote.getBgColorResId());
//设置笔记头部信息,显示修改日期
mNoteHeaderHolder.tvModified.setText(DateUtils.formatDateTime(this, mNoteHeaderHolder.tvModified.setText(DateUtils.formatDateTime(this,
mWorkingNote.getModifiedDate(), DateUtils.FORMAT_SHOW_DATE mWorkingNote.getModifiedDate(), DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_YEAR)); | DateUtils.FORMAT_SHOW_YEAR));
/** /*
* TODO: Add the menu for setting alert. Currently disable it because the DateTimePicker * TODO: Add the menu for setting alert. Currently disable it because the DateTimePicker
* is not ready * is not ready
*/ */
showAlertHeader(); showAlertHeader(); //显示或隐藏笔记的提醒图标和提醒日期
} }
//显示提醒头部的方法
private void showAlertHeader() { private void showAlertHeader() {
//判断笔记是否有提醒时间
if (mWorkingNote.hasClockAlert()) { if (mWorkingNote.hasClockAlert()) {
//如果当前时间超过了提醒时间,则显示过期信息
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
if (time > mWorkingNote.getAlertDate()) { if (time > mWorkingNote.getAlertDate()) {
mNoteHeaderHolder.tvAlertDate.setText(R.string.note_alert_expired); mNoteHeaderHolder.tvAlertDate.setText(R.string.note_alert_expired);
} else { } else {
//否则显示相对的剩余时间
mNoteHeaderHolder.tvAlertDate.setText(DateUtils.getRelativeTimeSpanString( mNoteHeaderHolder.tvAlertDate.setText(DateUtils.getRelativeTimeSpanString(
mWorkingNote.getAlertDate(), time, DateUtils.MINUTE_IN_MILLIS)); mWorkingNote.getAlertDate(), time, DateUtils.MINUTE_IN_MILLIS));
} }
//使提醒日期和图标可见
mNoteHeaderHolder.tvAlertDate.setVisibility(View.VISIBLE); mNoteHeaderHolder.tvAlertDate.setVisibility(View.VISIBLE);
mNoteHeaderHolder.ivAlertIcon.setVisibility(View.VISIBLE); mNoteHeaderHolder.ivAlertIcon.setVisibility(View.VISIBLE);
} else { } else {
//没有提醒时,隐藏提醒日期和图标
mNoteHeaderHolder.tvAlertDate.setVisibility(View.GONE); mNoteHeaderHolder.tvAlertDate.setVisibility(View.GONE);
mNoteHeaderHolder.ivAlertIcon.setVisibility(View.GONE); mNoteHeaderHolder.ivAlertIcon.setVisibility(View.GONE);
}; };
} }
//当活动通过意图重新初始化时调用
@Override @Override
protected void onNewIntent(Intent intent) { protected void onNewIntent(Intent intent) {
super.onNewIntent(intent); super.onNewIntent(intent);
//用新的意图初始化活动状态
initActivityState(intent); initActivityState(intent);
} }
//在活动可能被系统销毁前调用,保存状态
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
@ -345,34 +360,42 @@ public class NoteEditActivity extends Activity implements OnClickListener,
* generate a id. If the editing note is not worth saving, there * generate a id. If the editing note is not worth saving, there
* is no id which is equivalent to create new note * is no id which is equivalent to create new note
*/ */
//对于尚未在数据库中保存的新笔记先保存以生成ID
if (!mWorkingNote.existInDatabase()) { if (!mWorkingNote.existInDatabase()) {
saveNote(); saveNote();
} }
//将笔记ID保存到状态Bundle中
outState.putLong(Intent.EXTRA_UID, mWorkingNote.getNoteId()); outState.putLong(Intent.EXTRA_UID, mWorkingNote.getNoteId());
Log.d(TAG, "Save working note id: " + mWorkingNote.getNoteId() + " onSaveInstanceState"); Log.d(TAG, "Save working note id: " + mWorkingNote.getNoteId() + " onSaveInstanceState");
} }
//分发触摸事件的方法
@Override @Override
public boolean dispatchTouchEvent(MotionEvent ev) { public boolean dispatchTouchEvent(MotionEvent ev) {
//如果背景色选择器可见并触摸事件不在其范围内,则隐藏选择器并拦截事件
if (mNoteBgColorSelector.getVisibility() == View.VISIBLE if (mNoteBgColorSelector.getVisibility() == View.VISIBLE
&& !inRangeOfView(mNoteBgColorSelector, ev)) { && !inRangeOfView(mNoteBgColorSelector, ev)) {
mNoteBgColorSelector.setVisibility(View.GONE); mNoteBgColorSelector.setVisibility(View.GONE);
return true; return true;
} }
//如果字体大小选择器可见并触摸事件不在其范围内,则隐藏选择器并拦截事件
if (mFontSizeSelector.getVisibility() == View.VISIBLE if (mFontSizeSelector.getVisibility() == View.VISIBLE
&& !inRangeOfView(mFontSizeSelector, ev)) { && !inRangeOfView(mFontSizeSelector, ev)) {
mFontSizeSelector.setVisibility(View.GONE); mFontSizeSelector.setVisibility(View.GONE);
return true; return true;
} }
//否则,继续向下分发触摸事件
return super.dispatchTouchEvent(ev); return super.dispatchTouchEvent(ev);
} }
//判断触摸事件是否在给定的视图的范围内的方法
private boolean inRangeOfView(View view, MotionEvent ev) { private boolean inRangeOfView(View view, MotionEvent ev) {
int []location = new int[2]; int []location = new int[2];
//获取视图在屏幕上的位置
view.getLocationOnScreen(location); view.getLocationOnScreen(location);
int x = location[0]; int x = location[0];
int y = location[1]; int y = location[1];
//根据位置和触摸坐标判断是否在视图内
if (ev.getX() < x if (ev.getX() < x
|| ev.getX() > (x + view.getWidth()) || ev.getX() > (x + view.getWidth())
|| ev.getY() < y || ev.getY() < y
@ -382,7 +405,9 @@ public class NoteEditActivity extends Activity implements OnClickListener,
return true; return true;
} }
//初始化资源的方法
private void initResources() { private void initResources() {
//找到并设置相关视图的引用
mHeadViewPanel = findViewById(R.id.note_title); mHeadViewPanel = findViewById(R.id.note_title);
mNoteHeaderHolder = new HeadViewHolder(); mNoteHeaderHolder = new HeadViewHolder();
mNoteHeaderHolder.tvModified = (TextView) findViewById(R.id.tv_modified_date); mNoteHeaderHolder.tvModified = (TextView) findViewById(R.id.tv_modified_date);
@ -393,16 +418,19 @@ public class NoteEditActivity extends Activity implements OnClickListener,
mNoteEditor = (EditText) findViewById(R.id.note_edit_view); mNoteEditor = (EditText) findViewById(R.id.note_edit_view);
mNoteEditorPanel = findViewById(R.id.sv_note_edit); mNoteEditorPanel = findViewById(R.id.sv_note_edit);
mNoteBgColorSelector = findViewById(R.id.note_bg_color_selector); mNoteBgColorSelector = findViewById(R.id.note_bg_color_selector);
//为背景选择器中的按钮设置点击监听器
for (int id : sBgSelectorBtnsMap.keySet()) { for (int id : sBgSelectorBtnsMap.keySet()) {
ImageView iv = (ImageView) findViewById(id); ImageView iv = (ImageView) findViewById(id);
iv.setOnClickListener(this); iv.setOnClickListener(this);
} }
mFontSizeSelector = findViewById(R.id.font_size_selector); mFontSizeSelector = findViewById(R.id.font_size_selector);
//为字体大小选择器中的每一个选项设置点击监听器
for (int id : sFontSizeBtnsMap.keySet()) { for (int id : sFontSizeBtnsMap.keySet()) {
View view = findViewById(id); View view = findViewById(id);
view.setOnClickListener(this); view.setOnClickListener(this);
}; };
//获取共享偏好设置,用于恢复特定的笔记属性,如字体的大小
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mFontSizeId = mSharedPrefs.getInt(PREFERENCE_FONT_SIZE, ResourceParser.BG_DEFAULT_FONT_SIZE); mFontSizeId = mSharedPrefs.getInt(PREFERENCE_FONT_SIZE, ResourceParser.BG_DEFAULT_FONT_SIZE);
/** /**
@ -410,46 +438,58 @@ public class NoteEditActivity extends Activity implements OnClickListener,
* The id may larger than the length of resources, in this case, * The id may larger than the length of resources, in this case,
* return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE} * return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE}
*/ */
//如果字体大小ID超出资源数组大小设置为默认字体大小
if(mFontSizeId >= TextAppearanceResources.getResourcesSize()) { if(mFontSizeId >= TextAppearanceResources.getResourcesSize()) {
mFontSizeId = ResourceParser.BG_DEFAULT_FONT_SIZE; mFontSizeId = ResourceParser.BG_DEFAULT_FONT_SIZE;
} }
//初始化笔记编辑列表视图
mEditTextList = (LinearLayout) findViewById(R.id.note_edit_list); mEditTextList = (LinearLayout) findViewById(R.id.note_edit_list);
} }
//当活动暂停时调用
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
//尝试保存笔记,如果成功则记录保存的长度
if(saveNote()) { if(saveNote()) {
Log.d(TAG, "Note data was saved with length:" + mWorkingNote.getContent().length()); Log.d(TAG, "Note data was saved with length:" + mWorkingNote.getContent().length());
} }
//清除设置状态
clearSettingState(); clearSettingState();
} }
//用于更新小部件
private void updateWidget() { private void updateWidget() {
//创建一个更新小部件的intent
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE); Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
//根据笔记的小部件类型决定使用哪个小部件提供器
if (mWorkingNote.getWidgetType() == Notes.TYPE_WIDGET_2X) { if (mWorkingNote.getWidgetType() == Notes.TYPE_WIDGET_2X) {
intent.setClass(this, NoteWidgetProvider_2x.class); intent.setClass(this, NoteWidgetProvider_2x.class);
} else if (mWorkingNote.getWidgetType() == Notes.TYPE_WIDGET_4X) { } else if (mWorkingNote.getWidgetType() == Notes.TYPE_WIDGET_4X) {
intent.setClass(this, NoteWidgetProvider_4x.class); intent.setClass(this, NoteWidgetProvider_4x.class);
} else { } else {
//如果小部件类型不支持,则记录错误并返回
Log.e(TAG, "Unspported widget type"); Log.e(TAG, "Unspported widget type");
return; return;
} }
//添加小部件ID到intent中
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {
mWorkingNote.getWidgetId() mWorkingNote.getWidgetId()
}); });
//发送广播以通知小部件更新
sendBroadcast(intent); sendBroadcast(intent);
//设置结果为OK
setResult(RESULT_OK, intent); setResult(RESULT_OK, intent);
} }
//处理点击事件
public void onClick(View v) { public void onClick(View v) {
int id = v.getId(); int id = v.getId();
//如果点击的是设置背景颜色按钮,则显示颜色选择器
if (id == R.id.btn_set_bg_color) { if (id == R.id.btn_set_bg_color) {
mNoteBgColorSelector.setVisibility(View.VISIBLE); mNoteBgColorSelector.setVisibility(View.VISIBLE);
findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility( findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility(
- View.VISIBLE); - View.VISIBLE);
//更改背景颜色或字体大小的逻辑
} else if (sBgSelectorBtnsMap.containsKey(id)) { } else if (sBgSelectorBtnsMap.containsKey(id)) {
findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility( findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility(
View.GONE); View.GONE);
@ -471,17 +511,21 @@ public class NoteEditActivity extends Activity implements OnClickListener,
} }
} }
//当用户按下返回按钮时调用
@Override @Override
public void onBackPressed() { public void onBackPressed() {
//清除设置状态,如果已经处理则返回
if(clearSettingState()) { if(clearSettingState()) {
return; return;
} }
//保存笔记然后正常处理返回事件
saveNote(); saveNote();
super.onBackPressed(); super.onBackPressed();
} }
//清楚设置状态的辅助方法
private boolean clearSettingState() { private boolean clearSettingState() {
//如果背景颜色或字体大小选择器可见则隐藏他们并返回true表示状态已清除
if (mNoteBgColorSelector.getVisibility() == View.VISIBLE) { if (mNoteBgColorSelector.getVisibility() == View.VISIBLE) {
mNoteBgColorSelector.setVisibility(View.GONE); mNoteBgColorSelector.setVisibility(View.GONE);
return true; return true;
@ -492,15 +536,19 @@ public class NoteEditActivity extends Activity implements OnClickListener,
return false; return false;
} }
//当背景色改变时更新UI
public void onBackgroundColorChanged() { public void onBackgroundColorChanged() {
//设置编辑面板和头部面板的背景
findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility( findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility(
View.VISIBLE); View.VISIBLE);
mNoteEditorPanel.setBackgroundResource(mWorkingNote.getBgColorResId()); mNoteEditorPanel.setBackgroundResource(mWorkingNote.getBgColorResId());
mHeadViewPanel.setBackgroundResource(mWorkingNote.getTitleBgResId()); mHeadViewPanel.setBackgroundResource(mWorkingNote.getTitleBgResId());
} }
//准备选项菜单
@Override @Override
public boolean onPrepareOptionsMenu(Menu menu) { public boolean onPrepareOptionsMenu(Menu menu) {
//如果Activity正在结束则直接返回根据不同的情况填充不同的菜单
if (isFinishing()) { if (isFinishing()) {
return true; return true;
} }
@ -524,8 +572,10 @@ public class NoteEditActivity extends Activity implements OnClickListener,
return true; return true;
} }
//当选项被选中时调用
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
//处理菜单项的点击事件,如创建新笔记、删除笔记、更改字体大小等
int itemId = item.getItemId(); int itemId = item.getItemId();
if (itemId == R.id.menu_new_note) { if (itemId == R.id.menu_new_note) {
createNewNote(); createNewNote();
@ -562,13 +612,18 @@ public class NoteEditActivity extends Activity implements OnClickListener,
return true; return true;
} }
//设置提醒的辅助方法
private void setReminder() { private void setReminder() {
//显示时间日期选择器对话框,并设置监听器以保存提醒时间
DateTimePickerDialog d = new DateTimePickerDialog(this, System.currentTimeMillis()); DateTimePickerDialog d = new DateTimePickerDialog(this, System.currentTimeMillis());
//设置选择器的回调,用户选择日期时间后会调用这个监听函数
d.setOnDateTimeSetListener(new OnDateTimeSetListener() { d.setOnDateTimeSetListener(new OnDateTimeSetListener() {
public void OnDateTimeSet(AlertDialog dialog, long date) { public void OnDateTimeSet(AlertDialog dialog, long date) {
//当用户设置了日期时间后,将这个时间保存为提醒的时间
mWorkingNote.setAlertDate(date , true); mWorkingNote.setAlertDate(date , true);
} }
}); });
//显示日期时间选择对话框
d.show(); d.show();
} }
@ -577,9 +632,13 @@ public class NoteEditActivity extends Activity implements OnClickListener,
* and {@text/plain} type * and {@text/plain} type
*/ */
private void sendTo(Context context, String info) { private void sendTo(Context context, String info) {
//创建发送行为的Intent用于分享
Intent intent = new Intent(Intent.ACTION_SEND); Intent intent = new Intent(Intent.ACTION_SEND);
//将要分享的文本信息放入Intent
intent.putExtra(Intent.EXTRA_TEXT, info); intent.putExtra(Intent.EXTRA_TEXT, info);
//设置分享的类型为纯文本
intent.setType("text/plain"); intent.setType("text/plain");
//开始执行分享行为
context.startActivity(intent); context.startActivity(intent);
} }

Loading…
Cancel
Save