小米便签源代码

master
kathy 1 year ago
parent 954e94bf81
commit 97316f5dec

@ -72,47 +72,47 @@ import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/* 便签编辑活动类 */
public class NoteEditActivity extends AppCompatActivity implements OnClickListener, public class NoteEditActivity extends AppCompatActivity implements OnClickListener,
NoteSettingChangedListener, OnTextViewChangeListener { NoteSettingChangedListener, OnTextViewChangeListener {
private class HeadViewHolder { private class HeadViewHolder { // 头部视图存储器类
public TextView tvModified; public TextView tvModified; // 最近修改时间
public ImageView ivAlertIcon; public ImageView ivAlertIcon; // 闹钟提醒图标
public TextView tvAlertDate; public TextView tvAlertDate; // 闹钟提醒时间
public ImageView ibSetBgColor; public ImageView ibSetBgColor; // 背景颜色选择图片按钮
} }
// 背景颜色选择器按钮哈希表 用于存储背景颜色和图片按钮的映射关系
private static final Map<Integer, Integer> sBgSelectorBtnsMap = new HashMap<Integer, Integer>(); private static final Map<Integer, Integer> sBgSelectorBtnsMap = new HashMap<Integer, Integer>();
static { static { // 初始化背景颜色选择器按钮哈希表数据
sBgSelectorBtnsMap.put(R.id.iv_bg_yellow, ResourceParser.YELLOW); sBgSelectorBtnsMap.put(R.id.iv_bg_yellow, ResourceParser.YELLOW);
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);
} }
// 背景颜色选择器选择项哈希表 用于存储背景颜色和颜色选择项的映射关系
private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>(); private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>();
static { static {// 初始化背景颜色选择器选择项哈希表数据
sBgSelectorSelectionMap.put(ResourceParser.YELLOW, R.id.iv_bg_yellow_select); sBgSelectorSelectionMap.put(ResourceParser.YELLOW, R.id.iv_bg_yellow_select);
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);
} }
// 字体大小选择器按钮哈希表 用于存储字体大小和图片字体按钮的映射关系
private static final Map<Integer, Integer> sFontSizeBtnsMap = new HashMap<Integer, Integer>(); private static final Map<Integer, Integer> sFontSizeBtnsMap = new HashMap<Integer, Integer>();
static { static { // 初始化字体大小选择器按钮哈希表数据
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);
} }
// 字体大小选择器项哈希表 用于存储字体大小和字体选择项的映射关系
private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>(); private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>();
static { static {// 初始化字体大小选择器项哈希表
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);
@ -122,21 +122,21 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
private static final String TAG = "NoteEditActivity"; private static final String TAG = "NoteEditActivity";
private HeadViewHolder mNoteHeaderHolder; private HeadViewHolder mNoteHeaderHolder;
//私有化一个界面操作mHeadViewPanel对表头的操作
private View mHeadViewPanel; private View mHeadViewPanel;
//私有化一个界面操作mNoteBgColorSelector对背景颜色的操作
private View mNoteBgColorSelector; private View mNoteBgColorSelector;
//私有化一个界面操作mFontSizeSelector对便签字体的操作
private View mFontSizeSelector; private View mFontSizeSelector;
//声明编辑控件,对文本操作
private EditText mNoteEditor; private EditText mNoteEditor;
//私有化一个界面操作mNoteEditorPanel文本编辑的控制板
private View mNoteEditorPanel; private View mNoteEditorPanel;
// 当前运行便签
private WorkingNote mWorkingNote; private WorkingNote mWorkingNote;
//私有化SharedPreferences的数据存储方式 它的本质是基于XML文件存储key-value键值对数据
private SharedPreferences mSharedPrefs; private SharedPreferences mSharedPrefs;
private int mFontSizeId; private int mFontSizeId;//用于操作字体的大小
private static final String PREFERENCE_FONT_SIZE = "pref_font_size"; private static final String PREFERENCE_FONT_SIZE = "pref_font_size";
@ -159,13 +159,14 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
finish(); finish();
return; return;
} }
initResources(); initResources(); // 初始化界面资源
} }
/** /**
* Current activity may be killed when the memory is low. Once it is killed, for another time * Current activity may be killed when the memory is low. Once it is killed, for another time
* user load this activity, we should restore the former state * user load this activity, we should restore the former state
*/ */
/* 防止内存不足时程序的终止 我们需保存每个实例的状态*/
@Override @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) { protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState); super.onRestoreInstanceState(savedInstanceState);
@ -179,7 +180,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
Log.d(TAG, "Restoring from killed activity"); Log.d(TAG, "Restoring from killed activity");
} }
} }
/*初始化当前活动状态*/
private boolean initActivityState(Intent intent) { private boolean initActivityState(Intent intent) {
/** /**
* 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,
@ -217,6 +218,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
} else if(TextUtils.equals(Intent.ACTION_INSERT_OR_EDIT, intent.getAction())) { } else if(TextUtils.equals(Intent.ACTION_INSERT_OR_EDIT, intent.getAction())) {
// New note // New note
//用户可以通过receive接受intent 通过getAction得到的字符串来决定做什么
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);
@ -243,7 +245,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
} }
} else { } else {
mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId, mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId,
widgetType, bgResId); widgetType, bgResId); // 创建一个新的workingNote
mWorkingNote.convertToCallNote(phoneNumber, callDate); mWorkingNote.convertToCallNote(phoneNumber, callDate);
} }
} else { } else {
@ -268,10 +270,10 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
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 {
@ -295,16 +297,17 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
*/ */
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 {
@ -318,7 +321,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
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);
@ -327,7 +330,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
* 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
*/ */
if (!mWorkingNote.existInDatabase()) { if (!mWorkingNote.existInDatabase()) { // 若便签未保存先将其保存
saveNote(); saveNote();
} }
outState.putLong(Intent.EXTRA_UID, mWorkingNote.getNoteId()); outState.putLong(Intent.EXTRA_UID, mWorkingNote.getNoteId());
@ -335,21 +338,21 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
} }
@Override @Override
public boolean dispatchTouchEvent(MotionEvent ev) { public boolean dispatchTouchEvent(MotionEvent ev) {//MotionEvent是对屏幕触控的传递机制
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);
@ -359,12 +362,13 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
|| ev.getX() > (x + view.getWidth()) || ev.getX() > (x + view.getWidth())
|| ev.getY() < y || ev.getY() < y
|| ev.getY() > (y + view.getHeight())) { || ev.getY() > (y + view.getHeight())) {
return false; return false; // 若触控的位置超出了给定范围 返回false
} }
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);
@ -385,6 +389,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
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);
/** /**
@ -406,7 +411,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
} }
clearSettingState(); clearSettingState();
} }
/*同步桌面小工具*/
private void updateWidget() { private void updateWidget() {
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) {
@ -425,7 +430,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
sendBroadcast(intent); sendBroadcast(intent);
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) {
@ -452,7 +457,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
mFontSizeSelector.setVisibility(View.GONE); mFontSizeSelector.setVisibility(View.GONE);
} }
} }
/*back键*/
@Override @Override
public void onBackPressed() { public void onBackPressed() {
if(clearSettingState()) { if(clearSettingState()) {
@ -462,7 +467,7 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
saveNote(); saveNote();
super.onBackPressed(); super.onBackPressed();
} }
/*清除设置*/
private boolean clearSettingState() { private boolean clearSettingState() {
if (mNoteBgColorSelector.getVisibility() == View.VISIBLE) { if (mNoteBgColorSelector.getVisibility() == View.VISIBLE) {
mNoteBgColorSelector.setVisibility(View.GONE); mNoteBgColorSelector.setVisibility(View.GONE);
@ -473,21 +478,22 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
} }
return false; return false;
} }
/* 设置背景颜色改变监听事件*/
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) {
if (isFinishing()) { if (isFinishing()) {
return true; return true;
} }
clearSettingState(); clearSettingState();
menu.clear(); menu.clear();
// MenuInflater是用来实例化Menu目录下的Menu布局文件的
if (mWorkingNote.getFolderId() == Notes.ID_CALL_RECORD_FOLDER) { if (mWorkingNote.getFolderId() == Notes.ID_CALL_RECORD_FOLDER) {
getMenuInflater().inflate(R.menu.call_note_edit, menu); getMenuInflater().inflate(R.menu.call_note_edit, menu);
} else { } else {
@ -505,161 +511,166 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
} }
return true; return true;
} }
/*动态改变菜单选项内容*/
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) { //根据菜单的id来编辑相关项目
case R.id.menu_new_note: case R.id.menu_new_note:
createNewNote(); createNewNote(); // 创建一个新的便签
break; break;
case R.id.menu_delete: case R.id.menu_delete: // 删除便签
AlertDialog.Builder builder = new AlertDialog.Builder(this); AlertDialog.Builder builder = new AlertDialog.Builder(this); // 创建关于删除操作的对话框
builder.setTitle(getString(R.string.alert_title_delete)); builder.setTitle(getString(R.string.alert_title_delete)); // 设置便签的标题为alert_title_delete
builder.setIcon(android.R.drawable.ic_dialog_alert); builder.setIcon(android.R.drawable.ic_dialog_alert); // 设置对话框图标
builder.setMessage(getString(R.string.alert_message_delete_note)); builder.setMessage(getString(R.string.alert_message_delete_note)); // 设置对话框内容
builder.setPositiveButton(android.R.string.ok, builder.setPositiveButton(android.R.string.ok, // 设置确定按钮单击事件监听
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) { // 点击所触发事件
deleteCurrentNote(); deleteCurrentNote(); // 删除当前便签
finish(); finish(); // 退出便签编辑界面
} }
}); });
builder.setNegativeButton(android.R.string.cancel, null); builder.setNegativeButton(android.R.string.cancel, null); // 设置取消按钮单击事件监听
builder.show(); builder.show(); // 显示对话框
break; break;
case R.id.menu_font_size: case R.id.menu_font_size: // 字体大小编辑
mFontSizeSelector.setVisibility(View.VISIBLE); mFontSizeSelector.setVisibility(View.VISIBLE); // 将字体选择器设置为可见
// 根据字体大小id从字体选择器哈希表中取出相应字体对象所对应的id 根据该id找到字体对象将其设为可见
findViewById(sFontSelectorSelectionMap.get(mFontSizeId)).setVisibility(View.VISIBLE); findViewById(sFontSelectorSelectionMap.get(mFontSizeId)).setVisibility(View.VISIBLE);
break; break;
case R.id.menu_list_mode: case R.id.menu_list_mode:
mWorkingNote.setCheckListMode(mWorkingNote.getCheckListMode() == 0 ? mWorkingNote.setCheckListMode(mWorkingNote.getCheckListMode() == 0 ?
TextNote.MODE_CHECK_LIST : 0); TextNote.MODE_CHECK_LIST : 0); // 选择列表模式
break; break;
case R.id.menu_share: case R.id.menu_share: // 菜单共享
getWorkingText(); getWorkingText();
sendTo(this, mWorkingNote.getContent()); sendTo(this, mWorkingNote.getContent()); // 用sendTo函数将运行文本发送至遍历的本文内
break; break;
case R.id.menu_send_to_desktop: case R.id.menu_send_to_desktop:
sendToDesktop(); sendToDesktop(); // 发送至桌面
break; break;
case R.id.menu_alert: case R.id.menu_alert:
setReminder(); setReminder(); // 创建提醒器
break; break;
case R.id.menu_delete_remind: case R.id.menu_delete_remind:
mWorkingNote.setAlertDate(0, false); mWorkingNote.setAlertDate(0, false); // 删除日期提醒
break; break;
default: default:
break; break;
} }
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(); // 显示时间日期对话框
} }
/** /**
* Share note to apps that support {@link Intent#ACTION_SEND} action * Share note to apps that support {@link Intent#ACTION_SEND} action
* and {@text/plain} type * and {@text/plain} type
*/ */
/* 共享便签*/
private void sendTo(Context context, String info) { private void sendTo(Context context, String info) {
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进行绑定
intent.setType("text/plain"); intent.setType("text/plain"); // 设置编辑连接器类型
context.startActivity(intent); context.startActivity(intent); // 开启链接 进行活动跳转
} }
/* 创建一个新便签*/
private void createNewNote() { private void createNewNote() {
// Firstly, save current editing notes // Firstly, save current editing notes
saveNote(); saveNote(); // 保存当前便签
// For safety, start a new NoteEditActivity // For safety, start a new NoteEditActivity
finish(); finish(); // 退出当前编辑便签界面
Intent intent = new Intent(this, NoteEditActivity.class); Intent intent = new Intent(this, NoteEditActivity.class); // 创建新的便签编辑界面意图
intent.setAction(Intent.ACTION_INSERT_OR_EDIT); intent.setAction(Intent.ACTION_INSERT_OR_EDIT); // 给该意图设置活动为创建或编辑
intent.putExtra(Notes.INTENT_EXTRA_FOLDER_ID, mWorkingNote.getFolderId()); intent.putExtra(Notes.INTENT_EXTRA_FOLDER_ID, mWorkingNote.getFolderId()); // 将运行便签的Id与意图进行绑定
startActivity(intent); startActivity(intent); // 跳转至新的便签编辑界面
} }
/* 删除当前便签 */
private void deleteCurrentNote() { private void deleteCurrentNote() {
if (mWorkingNote.existInDatabase()) { if (mWorkingNote.existInDatabase()) { // 当前运行便签的内存有数据
HashSet<Long> ids = new HashSet<Long>(); HashSet<Long> ids = new HashSet<Long>();
long id = mWorkingNote.getNoteId(); long id = mWorkingNote.getNoteId();
if (id != Notes.ID_ROOT_FOLDER) { if (id != Notes.ID_ROOT_FOLDER) { // 若不是头文件夹则建立一个hash表将便签id存起来
ids.add(id); ids.add(id);
} else { } else { // 否则报错
Log.d(TAG, "Wrong note id, should not happen"); Log.d(TAG, "Wrong note id, should not happen");
} }
if (!isSyncMode()) { if (!isSyncMode()) { // 在非同步模式情况下 进行删除操作
if (!DataUtils.batchDeleteNotes(getContentResolver(), ids)) { if (!DataUtils.batchDeleteNotes(getContentResolver(), ids)) {
Log.e(TAG, "Delete Note error"); Log.e(TAG, "Delete Note error");
} }
} else { } else { // 在同步模式下 进行移动至垃圾文件夹操作
if (!DataUtils.batchMoveToFolder(getContentResolver(), ids, Notes.ID_TRASH_FOLER)) { if (!DataUtils.batchMoveToFolder(getContentResolver(), ids, Notes.ID_TRASH_FOLER)) {
Log.e(TAG, "Move notes to trash folder error, should not happens"); Log.e(TAG, "Move notes to trash folder error, should not happens");
} }
} }
} }
mWorkingNote.markDeleted(true); mWorkingNote.markDeleted(true); // 将运行便签的删除标记设置为true
} }
/* 判断是否同步 若NotesPreferenceActivity中同步名称不为空则说明同步否则不同步*/
private boolean isSyncMode() { private boolean isSyncMode() {
return NotesPreferenceActivity.getSyncAccountName(this).trim().length() > 0; return NotesPreferenceActivity.getSyncAccountName(this).trim().length() > 0;
} }
/*
* date:
* set/ 0 1:*/
public void onClockAlertChanged(long date, boolean set) { public void onClockAlertChanged(long date, boolean set) {
/** /**
* User could set clock to an unsaved note, so before setting the * User could set clock to an unsaved note, so before setting the
* alert clock, we should save the note first * alert clock, we should save the note first
*/ */
if (!mWorkingNote.existInDatabase()) { if (!mWorkingNote.existInDatabase()) {
saveNote(); saveNote(); // 若当前运行便签并未存入数据库则先保存当前便签
} }
if (mWorkingNote.getNoteId() > 0) { if (mWorkingNote.getNoteId() > 0) { // 当前运行便签已编辑 即不为空便签
Intent intent = new Intent(this, AlarmReceiver.class); Intent intent = new Intent(this, AlarmReceiver.class); // 创建闹钟接收器界面意图
intent.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mWorkingNote.getNoteId())); intent.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mWorkingNote.getNoteId())); // 为意图设置数据
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0); //对真实Intent进行封装 在出发时根据Intent唤起目标组件
AlarmManager alarmManager = ((AlarmManager) getSystemService(ALARM_SERVICE)); AlarmManager alarmManager = ((AlarmManager) getSystemService(ALARM_SERVICE)); // 设置提醒管理器
showAlertHeader(); showAlertHeader(); // 显示头部闹钟标记
if(!set) { if(!set) { // 取消闹钟
alarmManager.cancel(pendingIntent); alarmManager.cancel(pendingIntent);
} else { } else { // 设定闹钟
alarmManager.set(AlarmManager.RTC_WAKEUP, date, pendingIntent); alarmManager.set(AlarmManager.RTC_WAKEUP, date, pendingIntent); // 通过提醒管理器设置一个监听事件
} }
} else { } else { // 当前运行便签为空便签
/** /**
* There is the condition that user has input nothing (the note is * There is the condition that user has input nothing (the note is
* not worthy saving), we have no note id, remind the user that he * not worthy saving), we have no note id, remind the user that he
* should input something * should input something
*/ */
Log.e(TAG, "Clock alert setting error"); Log.e(TAG, "Clock alert setting error");
showToast(R.string.error_note_empty_for_clock); showToast(R.string.error_note_empty_for_clock); // 显示Toast消息提示
} }
} }
/*设置Widget发生改变时所触发的事件*/
public void onWidgetChanged() { public void onWidgetChanged() {
updateWidget(); updateWidget(); // 更新widget
} }
/*删除编辑文本框所触发的事件*/
public void onEditTextDelete(int index, String text) { public void onEditTextDelete(int index, String text) {
int childCount = mEditTextList.getChildCount(); int childCount = mEditTextList.getChildCount();
if (childCount == 1) { if (childCount == 1) { // 无编辑框时直接返回
return; return;
} }
// 有编辑框时遍历便签编辑框列表并设置对应下标
for (int i = index + 1; i < childCount; i++) { for (int i = index + 1; i < childCount; i++) {
((NoteEditText) mEditTextList.getChildAt(i).findViewById(R.id.et_edit_text)) ((NoteEditText) mEditTextList.getChildAt(i).findViewById(R.id.et_edit_text))
.setIndex(i - 1); .setIndex(i - 1);
} }
mEditTextList.removeViewAt(index); mEditTextList.removeViewAt(index); // 删除特定位置的视图
NoteEditText edit = null; NoteEditText edit = null;
// 通过id将编辑框存在空的NoteEditText中
if(index == 0) { if(index == 0) {
edit = (NoteEditText) mEditTextList.getChildAt(0).findViewById( edit = (NoteEditText) mEditTextList.getChildAt(0).findViewById(
R.id.et_edit_text); R.id.et_edit_text);
@ -669,33 +680,35 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
} }
int length = edit.length(); int length = edit.length();
edit.append(text); edit.append(text);
edit.requestFocus(); edit.requestFocus(); // 请求优先完成该此编辑
edit.setSelection(length); edit.setSelection(length); // 定位至length位置处的条目
} }
/* 设置进入编辑文本框所触发的事件*/
public void onEditTextEnter(int index, String text) { public void onEditTextEnter(int index, String text) {
/** /**
* Should not happen, check for debug * Should not happen, check for debug
*/ */
if(index > mEditTextList.getChildCount()) { if(index > mEditTextList.getChildCount()) { // 先进行越界检查
Log.e(TAG, "Index out of mEditTextList boundrary, should not happen"); Log.e(TAG, "Index out of mEditTextList boundrary, should not happen");
} }
View view = getListItem(text, index); View view = getListItem(text, index); // 创建一个新视图
mEditTextList.addView(view, index); mEditTextList.addView(view, index); // 将所创建的新视图添加至编辑文本框内
NoteEditText edit = (NoteEditText) view.findViewById(R.id.et_edit_text); NoteEditText edit = (NoteEditText) view.findViewById(R.id.et_edit_text);
edit.requestFocus(); edit.requestFocus(); // 请求优先操作
edit.setSelection(0); edit.setSelection(0); // 定位至起始位置
// 遍历子文本并设置对应下标
for (int i = index + 1; i < mEditTextList.getChildCount(); i++) { for (int i = index + 1; i < mEditTextList.getChildCount(); i++) {
((NoteEditText) mEditTextList.getChildAt(i).findViewById(R.id.et_edit_text)) ((NoteEditText) mEditTextList.getChildAt(i).findViewById(R.id.et_edit_text))
.setIndex(i); .setIndex(i);
} }
} }
/* 切换列表模式 */
private void switchToListMode(String text) { private void switchToListMode(String text) {
mEditTextList.removeAllViews(); mEditTextList.removeAllViews(); // 清空所有视图
String[] items = text.split("\n"); String[] items = text.split("\n");
int index = 0; int index = 0; // 初始化下标
// 遍历所有文本单元并添加至文本框中
for (String item : items) { for (String item : items) {
if(!TextUtils.isEmpty(item)) { if(!TextUtils.isEmpty(item)) {
mEditTextList.addView(getListItem(item, index)); mEditTextList.addView(getListItem(item, index));
@ -703,33 +716,35 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
} }
} }
mEditTextList.addView(getListItem("", index)); mEditTextList.addView(getListItem("", index));
mEditTextList.getChildAt(index).findViewById(R.id.et_edit_text).requestFocus(); mEditTextList.getChildAt(index).findViewById(R.id.et_edit_text).requestFocus(); // 优先请求此操作
mNoteEditor.setVisibility(View.GONE); mNoteEditor.setVisibility(View.GONE); // 设置便签编辑器不可见
mEditTextList.setVisibility(View.VISIBLE); mEditTextList.setVisibility(View.VISIBLE); // 设置文本编辑器可见
} }
/* 获取高亮效果的反馈情况 */
private Spannable getHighlightQueryResult(String fullText, String userQuery) { private Spannable getHighlightQueryResult(String fullText, String userQuery) {
SpannableString spannable = new SpannableString(fullText == null ? "" : fullText); SpannableString spannable = new SpannableString(fullText == null ? "" : fullText); // 新建一个效果选项
if (!TextUtils.isEmpty(userQuery)) { if (!TextUtils.isEmpty(userQuery)) {
mPattern = Pattern.compile(userQuery); mPattern = Pattern.compile(userQuery); // 解析用户询问
Matcher m = mPattern.matcher(fullText); Matcher m = mPattern.matcher(fullText); // 创建一个状态机检查Pattern并进行匹配
int start = 0; int start = 0;
while (m.find(start)) { while (m.find(start)) {
spannable.setSpan( spannable.setSpan(
new BackgroundColorSpan(this.getResources().getColor( new BackgroundColorSpan(this.getResources().getColor(
R.color.user_query_highlight)), m.start(), m.end(), R.color.user_query_highlight)), m.start(), m.end(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE); Spannable.SPAN_INCLUSIVE_EXCLUSIVE); // 设置背景颜色
start = m.end(); start = m.end(); // 更新起始位置
} }
} }
return spannable; return spannable;
} }
/* 获取列表项 */
private View getListItem(String item, int index) { private View getListItem(String item, int index) {
View view = LayoutInflater.from(this).inflate(R.layout.note_edit_list_item, null); View view = LayoutInflater.from(this).inflate(R.layout.note_edit_list_item, null); // 创建子项视图
// 获取文本编辑框并将其设置为可见
final NoteEditText edit = (NoteEditText) view.findViewById(R.id.et_edit_text); final NoteEditText edit = (NoteEditText) view.findViewById(R.id.et_edit_text);
edit.setTextAppearance(this, TextAppearanceResources.getTexAppearanceResource(mFontSizeId)); edit.setTextAppearance(this, TextAppearanceResources.getTexAppearanceResource(mFontSizeId));
// 获取复选框并为其设置选中事件监听
CheckBox cb = ((CheckBox) view.findViewById(R.id.cb_edit_item)); CheckBox cb = ((CheckBox) view.findViewById(R.id.cb_edit_item));
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
@ -741,73 +756,75 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
} }
}); });
if (item.startsWith(TAG_CHECKED)) { if (item.startsWith(TAG_CHECKED)) { // 选择勾选
cb.setChecked(true); cb.setChecked(true);
edit.setPaintFlags(edit.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); edit.setPaintFlags(edit.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
item = item.substring(TAG_CHECKED.length(), item.length()).trim(); item = item.substring(TAG_CHECKED.length(), item.length()).trim();
} else if (item.startsWith(TAG_UNCHECKED)) { } else if (item.startsWith(TAG_UNCHECKED)) { // 选择不勾选
cb.setChecked(false); cb.setChecked(false);
edit.setPaintFlags(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG); edit.setPaintFlags(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
item = item.substring(TAG_UNCHECKED.length(), item.length()).trim(); item = item.substring(TAG_UNCHECKED.length(), item.length()).trim();
} }
// 运行编辑框的监听器对该行为作出反应 并设置下标及文本内容
edit.setOnTextViewChangeListener(this); edit.setOnTextViewChangeListener(this);
edit.setIndex(index); edit.setIndex(index);
edit.setText(getHighlightQueryResult(item, mUserQuery)); edit.setText(getHighlightQueryResult(item, mUserQuery));
return view; return view;
} }
/* 设置便签内容发生改变所触发的事件*/
public void onTextChange(int index, boolean hasText) { public void onTextChange(int index, boolean hasText) {
if (index >= mEditTextList.getChildCount()) { if (index >= mEditTextList.getChildCount()) { // 先进性越界检查
Log.e(TAG, "Wrong index, should not happen"); Log.e(TAG, "Wrong index, should not happen");
return; return;
} }
if(hasText) { if(hasText) { // 内容不为空时将其子编辑框设置为可见
mEditTextList.getChildAt(index).findViewById(R.id.cb_edit_item).setVisibility(View.VISIBLE); mEditTextList.getChildAt(index).findViewById(R.id.cb_edit_item).setVisibility(View.VISIBLE);
} else { } else { // 内容为空时将其子编辑框设置为不可见
mEditTextList.getChildAt(index).findViewById(R.id.cb_edit_item).setVisibility(View.GONE); mEditTextList.getChildAt(index).findViewById(R.id.cb_edit_item).setVisibility(View.GONE);
} }
} }
/* 检查模式和列表模式的切换 */
public void onCheckListModeChanged(int oldMode, int newMode) { public void onCheckListModeChanged(int oldMode, int newMode) {
if (newMode == TextNote.MODE_CHECK_LIST) { if (newMode == TextNote.MODE_CHECK_LIST) { // 若新模式为列表模式 则切换至列表模式
switchToListMode(mNoteEditor.getText().toString()); switchToListMode(mNoteEditor.getText().toString());
} else { } else { // 普通模式
if (!getWorkingText()) { if (!getWorkingText()) { // 若是获取到文本则改变其检查标记
mWorkingNote.setWorkingText(mWorkingNote.getContent().replace(TAG_UNCHECKED + " ", mWorkingNote.setWorkingText(mWorkingNote.getContent().replace(TAG_UNCHECKED + " ",
"")); ""));
} }
// 修改文本剪辑器的内容和可见性
mNoteEditor.setText(getHighlightQueryResult(mWorkingNote.getContent(), mUserQuery)); mNoteEditor.setText(getHighlightQueryResult(mWorkingNote.getContent(), mUserQuery));
mEditTextList.setVisibility(View.GONE); mEditTextList.setVisibility(View.GONE);
mNoteEditor.setVisibility(View.VISIBLE); mNoteEditor.setVisibility(View.VISIBLE);
} }
} }
/* 设置勾选选项表 返回是否勾选的标记 */
private boolean getWorkingText() { private boolean getWorkingText() {
boolean hasChecked = false; boolean hasChecked = false; // 初始化check标记为false
if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) { if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) { // 当前运行便签模式为列表模式
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(); // 创建可变字符串
for (int i = 0; i < mEditTextList.getChildCount(); i++) { for (int i = 0; i < mEditTextList.getChildCount(); i++) { // 遍历所有子编辑框的视图
View view = mEditTextList.getChildAt(i); View view = mEditTextList.getChildAt(i);
NoteEditText edit = (NoteEditText) view.findViewById(R.id.et_edit_text); NoteEditText edit = (NoteEditText) view.findViewById(R.id.et_edit_text);
if (!TextUtils.isEmpty(edit.getText())) { if (!TextUtils.isEmpty(edit.getText())) { // 文本不为空
if (((CheckBox) view.findViewById(R.id.cb_edit_item)).isChecked()) { if (((CheckBox) view.findViewById(R.id.cb_edit_item)).isChecked()) { // 已勾选该选项框
// 扩展字符串为已打钩并把标记设置为true
sb.append(TAG_CHECKED).append(" ").append(edit.getText()).append("\n"); sb.append(TAG_CHECKED).append(" ").append(edit.getText()).append("\n");
hasChecked = true; hasChecked = true;
} else { } else { // 未勾选该选项框 则扩展字符串添加未打钩
sb.append(TAG_UNCHECKED).append(" ").append(edit.getText()).append("\n"); sb.append(TAG_UNCHECKED).append(" ").append(edit.getText()).append("\n");
} }
} }
} }
mWorkingNote.setWorkingText(sb.toString()); mWorkingNote.setWorkingText(sb.toString()); // 利用编辑好的字符串设置运行便签的内容
} else { } else { // 当前运行便签模式为普通模式
mWorkingNote.setWorkingText(mNoteEditor.getText().toString()); mWorkingNote.setWorkingText(mNoteEditor.getText().toString()); // 直接用编辑器中的内容设置运行便签的内容
} }
return hasChecked; return hasChecked;
} }
/* 保存便签 */
private boolean saveNote() { private boolean saveNote() {
getWorkingText(); getWorkingText(); // 获取当前运行便签的内容
boolean saved = mWorkingNote.saveNote(); boolean saved = mWorkingNote.saveNote();
if (saved) { if (saved) {
/** /**
@ -817,36 +834,39 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
* new node requires to the top of the list. This code * new node requires to the top of the list. This code
* {@link #RESULT_OK} is used to identify the create/edit state * {@link #RESULT_OK} is used to identify the create/edit state
*/ */
setResult(RESULT_OK); setResult(RESULT_OK); // RESULT_OK用来识别保存的两种情况1.创建后保存 2.修改后保存
} }
return saved; return saved;
} }
/* 将便签发送至桌面 */
private void sendToDesktop() { private void sendToDesktop() {
/** /**
* Before send message to home, we should make sure that current * Before send message to home, we should make sure that current
* editing note is exists in databases. So, for new note, firstly * editing note is exists in databases. So, for new note, firstly
* save it * save it
*/ */
if (!mWorkingNote.existInDatabase()) { if (!mWorkingNote.existInDatabase()) { // 先检查便签是否已保存 若未保存则需先保存便签
saveNote(); saveNote();
} }
if (mWorkingNote.getNoteId() > 0) { if (mWorkingNote.getNoteId() > 0) { // 便签文本内容不为空
// 建立发送至桌面的链接器
Intent sender = new Intent(); Intent sender = new Intent();
Intent shortcutIntent = new Intent(this, NoteEditActivity.class); Intent shortcutIntent = new Intent(this, NoteEditActivity.class);
shortcutIntent.setAction(Intent.ACTION_VIEW); shortcutIntent.setAction(Intent.ACTION_VIEW); // 链接为一个视图
shortcutIntent.putExtra(Intent.EXTRA_UID, mWorkingNote.getNoteId()); shortcutIntent.putExtra(Intent.EXTRA_UID, mWorkingNote.getNoteId());
// 将便签相关信息均添加至要发送的文件里
sender.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); sender.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
sender.putExtra(Intent.EXTRA_SHORTCUT_NAME, sender.putExtra(Intent.EXTRA_SHORTCUT_NAME,
makeShortcutIconTitle(mWorkingNote.getContent())); makeShortcutIconTitle(mWorkingNote.getContent()));
sender.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, sender.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon_app)); Intent.ShortcutIconResource.fromContext(this, R.drawable.icon_app));
sender.putExtra("duplicate", true); sender.putExtra("duplicate", true);
// 设置sneder的行为是发送
sender.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); sender.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
showToast(R.string.info_note_enter_desktop); showToast(R.string.info_note_enter_desktop); // 显示Toast消息提示
sendBroadcast(sender); sendBroadcast(sender); // 显示至桌面
} else { } else { // 空便签则直接报错
/** /**
* There is the condition that user has input nothing (the note is * There is the condition that user has input nothing (the note is
* not worthy saving), we have no note id, remind the user that he * not worthy saving), we have no note id, remind the user that he
@ -856,18 +876,18 @@ public class NoteEditActivity extends AppCompatActivity implements OnClickListen
showToast(R.string.error_note_empty_for_send_to_desktop); showToast(R.string.error_note_empty_for_send_to_desktop);
} }
} }
/* 编辑小图标标题*/
private String makeShortcutIconTitle(String content) { private String makeShortcutIconTitle(String content) {
content = content.replace(TAG_CHECKED, ""); content = content.replace(TAG_CHECKED, "");
content = content.replace(TAG_UNCHECKED, ""); content = content.replace(TAG_UNCHECKED, "");
return content.length() > SHORTCUT_ICON_TITLE_MAX_LEN ? content.substring(0, return content.length() > SHORTCUT_ICON_TITLE_MAX_LEN ? content.substring(0,
SHORTCUT_ICON_TITLE_MAX_LEN) : content; SHORTCUT_ICON_TITLE_MAX_LEN) : content; // 直接设置为content中的内容并返回 有勾选和未勾选2种
} }
/*根据下标显示对应的Toast消息提示*/
private void showToast(int resId) { private void showToast(int resId) {
showToast(resId, Toast.LENGTH_SHORT); showToast(resId, Toast.LENGTH_SHORT);
} }
/*根据下标和持续时间编辑Toast消息提示并进行显示*/
private void showToast(int resId, int duration) { private void showToast(int resId, int duration) {
Toast.makeText(this, resId, duration).show(); Toast.makeText(this, resId, duration).show();
} }

@ -36,7 +36,7 @@ import net.micode.notes.R;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
/* 便签编辑文本框类 继承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;
@ -45,7 +45,7 @@ public class NoteEditText extends EditText {
private static final String SCHEME_TEL = "tel:" ; private static final String SCHEME_TEL = "tel:" ;
private static final String SCHEME_HTTP = "http:" ; private static final String SCHEME_HTTP = "http:" ;
private static final String SCHEME_EMAIL = "mailto:" ; private static final String SCHEME_EMAIL = "mailto:" ;
// 创建一个字符和整数的hash表用于链接电话网站还有邮箱
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);
@ -56,64 +56,70 @@ public class NoteEditText extends EditText {
/** /**
* Call by the {@link NoteEditActivity} to delete or add edit text * Call by the {@link NoteEditActivity} to delete or add edit text
*/ */
//在NoteEditActivity中删除或添加文本的操作可以看做是一个文本是否被变的标记
public interface OnTextViewChangeListener { public interface OnTextViewChangeListener {
/** /**
* Delete current edit text when {@link KeyEvent#KEYCODE_DEL} happens * Delete current edit text when {@link KeyEvent#KEYCODE_DEL} happens
* and the text is null * 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} * Add edit text after current edit text when {@link KeyEvent#KEYCODE_ENTER}
* happen * happen
*/ */
void onEditTextEnter(int index, String text); void onEditTextEnter(int index, String text);//处理进入按键时的操作
/** /**
* Hide or show item option when text change * 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;
// 根据context设置文本
public NoteEditText(Context context) { public NoteEditText(Context context) {
super(context, null); super(context, null);
mIndex = 0; mIndex = 0;
} }
// 设置当前光标
public void setIndex(int index) { public void setIndex(int index) {
mIndex = index; mIndex = index;
} }
//初始化文本修改标记
public void setOnTextViewChangeListener(OnTextViewChangeListener listener) { public void setOnTextViewChangeListener(OnTextViewChangeListener listener) {
mOnTextViewChangeListener = listener; mOnTextViewChangeListener = listener;
} }
// 初始化便签 AttributeSet为自定义控件属性 用于维护便签动态变化的属性
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);
} }
// 根据defstyle自动初始化便签
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
} }
@Override @Override
public boolean onTouchEvent(MotionEvent event) { /*
viewonTouchEvent
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(); int x = (int) event.getX();
int y = (int) event.getY(); int y = (int) event.getY();
x -= getTotalPaddingLeft(); x -= getTotalPaddingLeft();
y -= getTotalPaddingTop(); y -= getTotalPaddingTop();
x += getScrollX(); x += getScrollX();
y += getScrollY(); y += getScrollY();
//用布局控件layout根据x,y的新值设置新的位置
Layout layout = getLayout(); Layout layout = getLayout();
int line = layout.getLineForVertical(y); int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x); int off = layout.getOffsetForHorizontal(line, x);
//更新光标新的位置
Selection.setSelection(getText(), off); Selection.setSelection(getText(), off);
break; break;
} }
@ -122,14 +128,15 @@ public class NoteEditText extends EditText {
} }
@Override @Override
/*处理用户按下一个键盘按键时会触发的事件*/
public boolean onKeyDown(int keyCode, KeyEvent event) { public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) { switch (keyCode) { //根据按键的 Unicode 编码值来处理
case KeyEvent.KEYCODE_ENTER: case KeyEvent.KEYCODE_ENTER: //“进入”按键
if (mOnTextViewChangeListener != null) { if (mOnTextViewChangeListener != null) {
return false; return false;
} }
break; break;
case KeyEvent.KEYCODE_DEL: case KeyEvent.KEYCODE_DEL://“删除”按键
mSelectionStartBeforeDelete = getSelectionStart(); mSelectionStartBeforeDelete = getSelectionStart();
break; break;
default: default:
@ -137,27 +144,28 @@ public class NoteEditText extends EditText {
} }
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) {//根据按键的 Unicode 编码值来处理有删除和进入2种操作
case KeyEvent.KEYCODE_DEL: case KeyEvent.KEYCODE_DEL: // 删除键
if (mOnTextViewChangeListener != null) { if (mOnTextViewChangeListener != null) { // 若是被修改过
if (0 == mSelectionStartBeforeDelete && mIndex != 0) { if (0 == mSelectionStartBeforeDelete && mIndex != 0) {//若之前有被修改并且文档不为空
//调用上文OnTextViewChangeListener对KEYCODE_DEL按键情况的删除函数进行删除
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;
@ -168,9 +176,15 @@ public class NoteEditText extends EditText {
} }
@Override @Override
/*
focused:View
direction:
previouslyFocusedRect:Viewnull
*/
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);
@ -178,35 +192,36 @@ public class NoteEditText extends EditText {
} }
super.onFocusChanged(focused, direction, previouslyFocusedRect); super.onFocusChanged(focused, direction, previouslyFocusedRect);
} }
/* 生成上下文菜单 */
@Override @Override
protected void onCreateContextMenu(ContextMenu menu) { protected void onCreateContextMenu(ContextMenu menu) {
if (getText() instanceof Spanned) { if (getText() instanceof 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);
// 设置url的信息的范围值
final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class); final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class);
if (urls.length == 1) { if (urls.length == 1) {
int defaultResId = 0; int defaultResId = 0;
for(String schema: sSchemaActionResMap.keySet()) { for(String schema: sSchemaActionResMap.keySet()) {// 获取计划表中所有的key值
if(urls[0].getURL().indexOf(schema) >= 0) { if(urls[0].getURL().indexOf(schema) >= 0) {//若url可以添加则在添加后将defaultResId置为key所映射的值
defaultResId = sSchemaActionResMap.get(schema); defaultResId = sSchemaActionResMap.get(schema);
break; break;
} }
} }
if (defaultResId == 0) { if (defaultResId == 0) {//defaultResId == 0则说明url并没有添加任何东西所以置为连接其他SchemaActionResMap的值
defaultResId = R.string.note_link_other; defaultResId = R.string.note_link_other;
} }
// 建立菜单
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 // goto a new intent
urls[0].onClick(NoteEditText.this); urls[0].onClick(NoteEditText.this); // 根据相应文本设置菜单的按键
return true; return true;
} }
}); });

Loading…
Cancel
Save