|
|
|
|
@ -0,0 +1,873 @@
|
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2010-2011, The MiCode Open Source Community (www.micode.net)
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package net.micode.notes.ui;//这个类在net.micode.notes.ui包里面
|
|
|
|
|
//导入各种必要的类
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.app.AlarmManager;// 安卓系统应用开发
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
|
import android.app.PendingIntent;
|
|
|
|
|
import android.app.SearchManager;//搜索管理器
|
|
|
|
|
import android.appwidget.AppWidgetManager;
|
|
|
|
|
import android.content.ContentUris;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
import android.graphics.Paint;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
|
import android.text.Spannable;
|
|
|
|
|
import android.text.SpannableString;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.text.format.DateUtils;
|
|
|
|
|
import android.text.style.BackgroundColorSpan;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.Menu;
|
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
|
import android.view.WindowManager;
|
|
|
|
|
import android.widget.CheckBox;
|
|
|
|
|
import android.widget.CompoundButton;
|
|
|
|
|
import android.widget.CompoundButton.OnCheckedChangeListener;//资源解析器
|
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
import android.widget.ImageView;
|
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
|
|
import net.micode.notes.R;
|
|
|
|
|
import net.micode.notes.data.Notes;
|
|
|
|
|
import net.micode.notes.data.Notes.TextNote;
|
|
|
|
|
import net.micode.notes.model.WorkingNote;
|
|
|
|
|
import net.micode.notes.model.WorkingNote.NoteSettingChangedListener;
|
|
|
|
|
import net.micode.notes.tool.DataUtils;
|
|
|
|
|
import net.micode.notes.tool.ResourceParser;
|
|
|
|
|
import net.micode.notes.tool.ResourceParser.TextAppearanceResources;
|
|
|
|
|
import net.micode.notes.ui.DateTimePickerDialog.OnDateTimeSetListener;
|
|
|
|
|
import net.micode.notes.ui.NoteEditText.OnTextViewChangeListener;
|
|
|
|
|
import net.micode.notes.widget.NoteWidgetProvider_2x;
|
|
|
|
|
import net.micode.notes.widget.NoteWidgetProvider_4x;
|
|
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
import java.util.regex.Matcher;
|
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
|
|
NoteSettingChangedListener, OnTextViewChangeListener {//继承Activity,实现了便签的编辑功能,同时对三个监听接口的方法进行实现
|
|
|
|
|
private class HeadViewHolder {//用于对界面进行设置,包括文本内容,图片内容等,此处是编辑界面的标题栏部分
|
|
|
|
|
public TextView tvModified;
|
|
|
|
|
|
|
|
|
|
public ImageView ivAlertIcon;//设置提醒的图标
|
|
|
|
|
|
|
|
|
|
public TextView tvAlertDate;//设置提醒的时间
|
|
|
|
|
|
|
|
|
|
public ImageView ibSetBgColor;//设置背景颜色
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sBgSelectorBtnsMap = new HashMap<Integer, Integer>();//利用map进行数据存储
|
|
|
|
|
static {//对HashMap进行初始化,主要是颜色按键与执行操作的对应
|
|
|
|
|
sBgSelectorBtnsMap.put(R.id.iv_bg_yellow, ResourceParser.YELLOW);//黄色
|
|
|
|
|
sBgSelectorBtnsMap.put(R.id.iv_bg_red, ResourceParser.RED);//红色
|
|
|
|
|
sBgSelectorBtnsMap.put(R.id.iv_bg_blue, ResourceParser.BLUE);//蓝色
|
|
|
|
|
sBgSelectorBtnsMap.put(R.id.iv_bg_green, ResourceParser.GREEN);//绿色
|
|
|
|
|
sBgSelectorBtnsMap.put(R.id.iv_bg_white, ResourceParser.WHITE);//白色
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>();//运用Hashmap,将资源解析器中的对应颜色与已选择的颜色按钮关联起来
|
|
|
|
|
static {
|
|
|
|
|
sBgSelectorSelectionMap.put(ResourceParser.YELLOW, R.id.iv_bg_yellow_select);
|
|
|
|
|
sBgSelectorSelectionMap.put(ResourceParser.RED, R.id.iv_bg_red_select);
|
|
|
|
|
sBgSelectorSelectionMap.put(ResourceParser.BLUE, R.id.iv_bg_blue_select);
|
|
|
|
|
sBgSelectorSelectionMap.put(ResourceParser.GREEN, R.id.iv_bg_green_select);
|
|
|
|
|
sBgSelectorSelectionMap.put(ResourceParser.WHITE, R.id.iv_bg_white_select);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sFontSizeBtnsMap = new HashMap<Integer, Integer>();//提供各种大小字体的选择
|
|
|
|
|
static {
|
|
|
|
|
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_normal, ResourceParser.TEXT_MEDIUM);//正常
|
|
|
|
|
sFontSizeBtnsMap.put(R.id.ll_font_super, ResourceParser.TEXT_SUPER);//超大
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>();//实现资源解析器中字号ID与字体大小按钮(已选择)对应
|
|
|
|
|
static {
|
|
|
|
|
sFontSelectorSelectionMap.put(ResourceParser.TEXT_LARGE, R.id.iv_large_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_SUPER, R.id.iv_super_select);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "NoteEditActivity";//tag定为NoteEditActivity
|
|
|
|
|
|
|
|
|
|
private HeadViewHolder mNoteHeaderHolder;//标题头的视图控制器
|
|
|
|
|
|
|
|
|
|
private View mHeadViewPanel;//对背景颜色的操作
|
|
|
|
|
|
|
|
|
|
private View mNoteBgColorSelector;//便签的背景色选择器
|
|
|
|
|
|
|
|
|
|
private View mFontSizeSelector;//便签的字体大小选择器
|
|
|
|
|
|
|
|
|
|
private EditText mNoteEditor;//便签编辑器
|
|
|
|
|
|
|
|
|
|
private View mNoteEditorPanel;//文本编辑控制板
|
|
|
|
|
|
|
|
|
|
private WorkingNote mWorkingNote;//活动便签
|
|
|
|
|
|
|
|
|
|
private SharedPreferences mSharedPrefs;//用来保存配置参数
|
|
|
|
|
private int mFontSizeId;//用于操作字体的大小
|
|
|
|
|
|
|
|
|
|
private static final String PREFERENCE_FONT_SIZE = "pref_font_size";//字体大小设置
|
|
|
|
|
|
|
|
|
|
private static final int SHORTCUT_ICON_TITLE_MAX_LEN = 10;//图标标题的最大长度为10
|
|
|
|
|
|
|
|
|
|
public static final String TAG_CHECKED = String.valueOf('\u221A');//表明已标记
|
|
|
|
|
public static final String TAG_UNCHECKED = String.valueOf('\u25A1');//表明未标记
|
|
|
|
|
|
|
|
|
|
private LinearLayout mEditTextList;//线性布局,用于编辑文本
|
|
|
|
|
|
|
|
|
|
private String mUserQuery;//用户请求
|
|
|
|
|
private Pattern mPattern;//正则表达式模式
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {//调用父类方法创建主界面
|
|
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
|
this.setContentView(R.layout.note_edit);//设置视图
|
|
|
|
|
|
|
|
|
|
if (savedInstanceState == null && !initActivityState(getIntent())) {//如果未保存实例化状态且初始化活动状态未成功,就结束这个活动
|
|
|
|
|
finish();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
initResources();//初始化资源
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected void onRestoreInstanceState(Bundle savedInstanceState) {//为防止内存不足或其他情况时程序的终止,一个保存现场的函数
|
|
|
|
|
super.onRestoreInstanceState(savedInstanceState);
|
|
|
|
|
if (savedInstanceState != null && savedInstanceState.containsKey(Intent.EXTRA_UID)) {//如果保存了关闭前的实例状态且其中包含着便签标识符,则将该标识符添加到初始化Activity的Intent中
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
|
|
intent.putExtra(Intent.EXTRA_UID, savedInstanceState.getLong(Intent.EXTRA_UID));//利用此前的extra_uid初始化一个intent
|
|
|
|
|
if (!initActivityState(intent)) {//初始化活动状态失败,则结束
|
|
|
|
|
finish();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
Log.d(TAG, "Restoring from killed activity");//记录操作信息到日志中
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean initActivityState(Intent intent) {//初始化系统的状态
|
|
|
|
|
/**
|
|
|
|
|
* If the user specified the {@link Intent#ACTION_VIEW} but not provided with id,
|
|
|
|
|
* then jump to the NotesListActivity
|
|
|
|
|
*/
|
|
|
|
|
mWorkingNote = null;//初始化便签数据
|
|
|
|
|
if (TextUtils.equals(Intent.ACTION_VIEW, intent.getAction())) {
|
|
|
|
|
long noteId = intent.getLongExtra(Intent.EXTRA_UID, 0);//获取intent中的附加信息(字符串)EXTRA_UID标识noteId,默认值为0
|
|
|
|
|
mUserQuery = "";//初始化用户请求为空字符串
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Starting from the searched result
|
|
|
|
|
*/
|
|
|
|
|
if (intent.hasExtra(SearchManager.EXTRA_DATA_KEY)) {
|
|
|
|
|
noteId = Long.parseLong(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));//获取便签id
|
|
|
|
|
mUserQuery = intent.getStringExtra(SearchManager.USER_QUERY);//将用户请求字符串赋值给mUserQuery
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!DataUtils.visibleInNoteDatabase(getContentResolver(), noteId, Notes.TYPE_NOTE)) {//如果ID在数据库中未找到
|
|
|
|
|
Intent jump = new Intent(this, NotesListActivity.class);//新建Intent,设置跳转的目标为NoteListActivity
|
|
|
|
|
startActivity(jump);//跳转
|
|
|
|
|
showToast(R.string.error_note_not_exist);// 显示消息提示框,消息为便签不存在
|
|
|
|
|
finish();
|
|
|
|
|
return false;
|
|
|
|
|
} else {//如果找到了便签的标识符,就将对应标签加载到mWorkingNote中
|
|
|
|
|
mWorkingNote = WorkingNote.load(this, noteId);
|
|
|
|
|
if (mWorkingNote == null) {
|
|
|
|
|
Log.e(TAG, "load note failed with note id" + noteId);//打印出错误信息
|
|
|
|
|
finish();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
getWindow().setSoftInputMode(//软键盘输入
|
|
|
|
|
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN//将软键盘隐藏
|
|
|
|
|
| WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//调节Edit界面,使之适应软键盘弹出
|
|
|
|
|
} else if(TextUtils.equals(Intent.ACTION_INSERT_OR_EDIT, intent.getAction())) {
|
|
|
|
|
// New note,如果传入的intent属性为ACTION_INSERT_OR_EDIT,说明需要新建一个便签
|
|
|
|
|
long folderId = intent.getLongExtra(Notes.INTENT_EXTRA_FOLDER_ID, 0);//获取intent中传入的父文件夹id,默认为0
|
|
|
|
|
int widgetId = intent.getIntExtra(Notes.INTENT_EXTRA_WIDGET_ID,
|
|
|
|
|
AppWidgetManager.INVALID_APPWIDGET_ID);//获取布局文件id,默认返回参数为无效参数
|
|
|
|
|
int widgetType = intent.getIntExtra(Notes.INTENT_EXTRA_WIDGET_TYPE,
|
|
|
|
|
Notes.TYPE_WIDGET_INVALIDE);//获取布局文件类型,默认为无效参数
|
|
|
|
|
int bgResId = intent.getIntExtra(Notes.INTENT_EXTRA_BACKGROUND_ID,
|
|
|
|
|
ResourceParser.getDefaultBgId(this));//获取背景颜色资源id,默认为随机背景色
|
|
|
|
|
|
|
|
|
|
// Parse call-record note,解析电话簿记录
|
|
|
|
|
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
|
|
|
|
|
long callDate = intent.getLongExtra(Notes.INTENT_EXTRA_CALL_DATE, 0);//获取提醒时间,默认为0
|
|
|
|
|
if (callDate != 0 && phoneNumber != null) {//如果便签的通话日期和通话号码均存在,则把其当做通话便签进行处理,否则当做普通便签进行初始化
|
|
|
|
|
if (TextUtils.isEmpty(phoneNumber)) {//若电话号码为空,则输出警告信息
|
|
|
|
|
Log.w(TAG, "The call record number is null");
|
|
|
|
|
}
|
|
|
|
|
long noteId = 0;
|
|
|
|
|
if ((noteId = DataUtils.getNoteIdByPhoneNumberAndCallDate(getContentResolver(),
|
|
|
|
|
phoneNumber, callDate)) > 0) {//根据电话号码和日期找到对应标识符,赋给noteId
|
|
|
|
|
mWorkingNote = WorkingNote.load(this, noteId);//导入这个便签
|
|
|
|
|
if (mWorkingNote == null) {//noteId对应的内容为空,输出报错信息
|
|
|
|
|
Log.e(TAG, "load call note failed with note id" + noteId);
|
|
|
|
|
finish();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId,
|
|
|
|
|
widgetType, bgResId);//重新建立空便签,使用之前默认参量对便签进行初始化
|
|
|
|
|
mWorkingNote.convertToCallNote(phoneNumber, callDate);//根据电话号码和通话日期转化为通话便签
|
|
|
|
|
}
|
|
|
|
|
} else {//否则按照普通便签进行创建
|
|
|
|
|
mWorkingNote = WorkingNote.createEmptyNote(this, folderId, widgetId, widgetType,
|
|
|
|
|
bgResId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getWindow().setSoftInputMode(//软键盘输入设置
|
|
|
|
|
WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
|
|
|
|
|
| WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
|
|
|
|
|
} else {//打印错误信息
|
|
|
|
|
Log.e(TAG, "Intent not specified action, should not support");
|
|
|
|
|
finish();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
mWorkingNote.setOnSettingStatusChangedListener(this);//设置状态改变监听器
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onResume() {//调用父类onRume函数,初始便签界面
|
|
|
|
|
super.onResume();
|
|
|
|
|
initNoteScreen();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initNoteScreen() {//对界面进行初始化的操作
|
|
|
|
|
mNoteEditor.setTextAppearance(this, TextAppearanceResources
|
|
|
|
|
.getTexAppearanceResource(mFontSizeId));//设置文字编辑控件中的字体样式
|
|
|
|
|
if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) {//如果便签的模式为清单模式,则执行处理过程
|
|
|
|
|
switchToListMode(mWorkingNote.getContent());//切换到清单模式
|
|
|
|
|
} else {
|
|
|
|
|
mNoteEditor.setText(getHighlightQueryResult(mWorkingNote.getContent(), mUserQuery));//设置查找到的高亮文本
|
|
|
|
|
mNoteEditor.setSelection(mNoteEditor.getText().length());//选取范围
|
|
|
|
|
}
|
|
|
|
|
for (Integer id : sBgSelectorSelectionMap.keySet()) {//对所有图片文件做遍历循环
|
|
|
|
|
findViewById(sBgSelectorSelectionMap.get(id)).setVisibility(View.GONE);//将背景图不显示
|
|
|
|
|
}
|
|
|
|
|
mHeadViewPanel.setBackgroundResource(mWorkingNote.getTitleBgResId());//为标题设置背景
|
|
|
|
|
mNoteEditorPanel.setBackgroundResource(mWorkingNote.getBgColorResId());///为便签编辑器设置背景
|
|
|
|
|
|
|
|
|
|
mNoteHeaderHolder.tvModified.setText(DateUtils.formatDateTime(this,
|
|
|
|
|
mWorkingNote.getModifiedDate(), DateUtils.FORMAT_SHOW_DATE
|
|
|
|
|
| DateUtils.FORMAT_NUMERIC_DATE | DateUtils.FORMAT_SHOW_TIME
|
|
|
|
|
| DateUtils.FORMAT_SHOW_YEAR));/在便签顶部显示时间
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TODO: Add the menu for setting alert. Currently disable it because the DateTimePicker
|
|
|
|
|
* is not ready
|
|
|
|
|
*/
|
|
|
|
|
showAlertHeader();//显示便签头部的时间和图标
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showAlertHeader() {设置闹钟的显示
|
|
|
|
|
if (mWorkingNote.hasClockAlert()) {//判断当前便签是否设置了闹钟
|
|
|
|
|
long time = System.currentTimeMillis();//获取当前时间
|
|
|
|
|
if (time > mWorkingNote.getAlertDate()) {//如果系统时间大于了闹钟设置的时间,那么闹钟失效
|
|
|
|
|
mNoteHeaderHolder.tvAlertDate.setText(R.string.note_alert_expired);
|
|
|
|
|
} else {//如果提醒时间未到,则设置标题中显示提醒时间
|
|
|
|
|
mNoteHeaderHolder.tvAlertDate.setText(DateUtils.getRelativeTimeSpanString(
|
|
|
|
|
mWorkingNote.getAlertDate(), time, DateUtils.MINUTE_IN_MILLIS));
|
|
|
|
|
}
|
|
|
|
|
mNoteHeaderHolder.tvAlertDate.setVisibility(View.VISIBLE);//显示闹钟开启的图标
|
|
|
|
|
mNoteHeaderHolder.ivAlertIcon.setVisibility(View.VISIBLE);//提醒时间图标显示可见
|
|
|
|
|
} else {//如果没有设置闹钟就把闹钟相关的控件设置为不可见
|
|
|
|
|
mNoteHeaderHolder.tvAlertDate.setVisibility(View.GONE);
|
|
|
|
|
mNoteHeaderHolder.ivAlertIcon.setVisibility(View.GONE);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onNewIntent(Intent intent) {//新建一个传值对象
|
|
|
|
|
super.onNewIntent(intent);//初始化活动状态
|
|
|
|
|
initActivityState(intent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onSaveInstanceState(Bundle outState) {//用于保存数据
|
|
|
|
|
super.onSaveInstanceState(outState);
|
|
|
|
|
/**
|
|
|
|
|
* For new note without note id, we should firstly save it to
|
|
|
|
|
* generate a id. If the editing note is not worth saving, there
|
|
|
|
|
* is no id which is equivalent to create new note
|
|
|
|
|
*/
|
|
|
|
|
if (!mWorkingNote.existInDatabase()) {//没有就储存在数据库
|
|
|
|
|
saveNote();
|
|
|
|
|
}
|
|
|
|
|
outState.putLong(Intent.EXTRA_UID, mWorkingNote.getNoteId());//将生成的noteId保存到outState中
|
|
|
|
|
Log.d(TAG, "Save working note id: " + mWorkingNote.getNoteId() + " onSaveInstanceState");//保存操作信息到日志中
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean dispatchTouchEvent(MotionEvent ev) {//对屏幕触控事件的判断
|
|
|
|
|
if (mNoteBgColorSelector.getVisibility() == View.VISIBLE
|
|
|
|
|
&& !inRangeOfView(mNoteBgColorSelector, ev)) {//判断如果背景颜色选择界面可见且触摸范围在背景范围外则设置选择背景界面为不可见
|
|
|
|
|
mNoteBgColorSelector.setVisibility(View.GONE);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mFontSizeSelector.getVisibility() == View.VISIBLE
|
|
|
|
|
&& !inRangeOfView(mFontSizeSelector, ev)) {//设置字体选择界面不可见
|
|
|
|
|
mFontSizeSelector.setVisibility(View.GONE);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return super.dispatchTouchEvent(ev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean inRangeOfView(View view, MotionEvent ev) {//对屏幕触控的坐标进行操作
|
|
|
|
|
int []location = new int[2];//声明location数组,用于存放屏幕位置的横纵坐标
|
|
|
|
|
view.getLocationOnScreen(location);//获取屏幕上的起点位置
|
|
|
|
|
int x = location[0];
|
|
|
|
|
int y = location[1];
|
|
|
|
|
if (ev.getX() < x
|
|
|
|
|
|| ev.getX() > (x + view.getWidth())
|
|
|
|
|
|| ev.getY() < y
|
|
|
|
|
|| ev.getY() > (y + view.getHeight())) {
|
|
|
|
|
return false;
|
|
|
|
|
}//如果触控的位置超出了给定的范围,返回false
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initResources() {//对标签各项属性内容的初始化
|
|
|
|
|
mHeadViewPanel = findViewById(R.id.note_title);//初始化便签标题栏
|
|
|
|
|
mNoteHeaderHolder = new HeadViewHolder();//创建HeadViewHolder()容器,并得到日期,图标,提醒日期,背景颜色的信息
|
|
|
|
|
mNoteHeaderHolder.tvModified = (TextView) findViewById(R.id.tv_modified_date);
|
|
|
|
|
mNoteHeaderHolder.ivAlertIcon = (ImageView) findViewById(R.id.iv_alert_icon);
|
|
|
|
|
mNoteHeaderHolder.tvAlertDate = (TextView) findViewById(R.id.tv_alert_date);
|
|
|
|
|
mNoteHeaderHolder.ibSetBgColor = (ImageView) findViewById(R.id.btn_set_bg_color);
|
|
|
|
|
mNoteHeaderHolder.ibSetBgColor.setOnClickListener(this);//设置监听器
|
|
|
|
|
mNoteEditor = (EditText) findViewById(R.id.note_edit_view);//得到文本编辑框
|
|
|
|
|
mNoteEditorPanel = findViewById(R.id.sv_note_edit);//文件编辑面板
|
|
|
|
|
mNoteBgColorSelector = findViewById(R.id.note_bg_color_selector);//设置背景颜色的选择器
|
|
|
|
|
for (int id : sBgSelectorBtnsMap.keySet()) {//对于每一个背景颜色的选择按钮都设置监听器
|
|
|
|
|
ImageView iv = (ImageView) findViewById(id);
|
|
|
|
|
iv.setOnClickListener(this);/设置点击事件监听器
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mFontSizeSelector = findViewById(R.id.font_size_selector);//初始化便签文字字号选择器
|
|
|
|
|
for (int id : sFontSizeBtnsMap.keySet()) {//对于每一个字体大小的选择按钮都设置监听器
|
|
|
|
|
View view = findViewById(id);
|
|
|
|
|
view.setOnClickListener(this);
|
|
|
|
|
};
|
|
|
|
|
mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
mFontSizeId = mSharedPrefs.getInt(PREFERENCE_FONT_SIZE, ResourceParser.BG_DEFAULT_FONT_SIZE);//得到用户的偏好信息,设置字体大小,如果大于资源的最大长度,设为默认大小
|
|
|
|
|
/**
|
|
|
|
|
* HACKME: Fix bug of store the resource id in shared preference.
|
|
|
|
|
* The id may larger than the length of resources, in this case,
|
|
|
|
|
* return the {@link ResourceParser#BG_DEFAULT_FONT_SIZE}
|
|
|
|
|
*/
|
|
|
|
|
if(mFontSizeId >= TextAppearanceResources.getResourcesSize()) {//如果获取到的字体大小值的长度大于资源文件应有长度,则将其设置为默认长度,缺省值为1
|
|
|
|
|
mFontSizeId = ResourceParser.BG_DEFAULT_FONT_SIZE;
|
|
|
|
|
}
|
|
|
|
|
mEditTextList = (LinearLayout) findViewById(R.id.note_edit_list);//获取标签编辑的列表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onPause() {//重写onPause方法,将便签编辑界面暂停
|
|
|
|
|
super.onPause();
|
|
|
|
|
if(saveNote()) {//保存便签
|
|
|
|
|
Log.d(TAG, "Note data was saved with length:" + mWorkingNote.getContent().length());
|
|
|
|
|
}//暂停时即进行便签的存储
|
|
|
|
|
clearSettingState();//清除状态设置
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateWidget() {//将便签同步到挂件
|
|
|
|
|
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);//新建一个包含改变widget动作的intent实例
|
|
|
|
|
if (mWorkingNote.getWidgetType() == Notes.TYPE_WIDGET_2X) {//根据workingnote实例中存储的widget大小,设置intent中相应连接的类
|
|
|
|
|
intent.setClass(this, NoteWidgetProvider_2x.class);
|
|
|
|
|
} else if (mWorkingNote.getWidgetType() == Notes.TYPE_WIDGET_4X) {
|
|
|
|
|
intent.setClass(this, NoteWidgetProvider_4x.class);
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Unspported widget type");//输出error信息
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {//将桌面小部件的标识符放入intent中
|
|
|
|
|
mWorkingNote.getWidgetId()
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sendBroadcast(intent);//将指定好参数的Intent对象通过广播发送出去以启动相应的Activity
|
|
|
|
|
setResult(RESULT_OK, intent);//设置当前Activity结束后将结束信息发送给其父活动
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onClick(View v) {//当点击事件发生时执行该方法
|
|
|
|
|
int id = v.getId();//获取点击目标的id
|
|
|
|
|
if (id == R.id.btn_set_bg_color) {//如果点击的是设置背景颜色的按钮
|
|
|
|
|
mNoteBgColorSelector.setVisibility(View.VISIBLE);
|
|
|
|
|
findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility(
|
|
|
|
|
- View.VISIBLE);//背景颜色选择器设置为可见
|
|
|
|
|
} else if (sBgSelectorBtnsMap.containsKey(id)) {//如果选择了颜色
|
|
|
|
|
findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility(
|
|
|
|
|
View.GONE);//背景颜色选择器设置为不可见
|
|
|
|
|
mWorkingNote.setBgColorId(sBgSelectorBtnsMap.get(id));//修改颜色
|
|
|
|
|
mNoteBgColorSelector.setVisibility(View.GONE);//将背景颜色选择器隐藏
|
|
|
|
|
} else if (sFontSizeBtnsMap.containsKey(id)) {//如果点击的是文字大小设置器中的文字大小选项按钮
|
|
|
|
|
findViewById(sFontSelectorSelectionMap.get(mFontSizeId)).setVisibility(View.GONE);//将备选字体界面设为不可见
|
|
|
|
|
mFontSizeId = sFontSizeBtnsMap.get(id);//设置字体为选中尺寸
|
|
|
|
|
mSharedPrefs.edit().putInt(PREFERENCE_FONT_SIZE, mFontSizeId).commit();//将共享设置中的字体进行修改
|
|
|
|
|
findViewById(sFontSelectorSelectionMap.get(mFontSizeId)).setVisibility(View.VISIBLE);//将备选字体界面设为可见
|
|
|
|
|
if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) {//如果便签为清单模式
|
|
|
|
|
getWorkingText();//获取正在编辑的文本
|
|
|
|
|
switchToListMode(mWorkingNote.getContent());//将便签内容显示切换到列表模式
|
|
|
|
|
} else {
|
|
|
|
|
mNoteEditor.setTextAppearance(this,
|
|
|
|
|
TextAppearanceResources.getTexAppearanceResource(mFontSizeId));//如果不是check模式,则设置文字编辑器中文字为相应字体大小
|
|
|
|
|
}
|
|
|
|
|
mFontSizeSelector.setVisibility(View.GONE);//将文字大小选择器设置为不可见
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onBackPressed() {//点击返回键时的操作
|
|
|
|
|
if(clearSettingState()) {//如果无界面打开,则直接返回,否则关闭再返回
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saveNote();//保存便签
|
|
|
|
|
super.onBackPressed();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean clearSettingState() {//清除设置状态
|
|
|
|
|
if (mNoteBgColorSelector.getVisibility() == View.VISIBLE) {//如果背景色选择器可见,设置为不可见
|
|
|
|
|
mNoteBgColorSelector.setVisibility(View.GONE);
|
|
|
|
|
return true;
|
|
|
|
|
} else if (mFontSizeSelector.getVisibility() == View.VISIBLE) {//如果字体选择菜单可见,设置为不可见
|
|
|
|
|
mFontSizeSelector.setVisibility(View.GONE);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onBackgroundColorChanged() {//背景颜色改变设置
|
|
|
|
|
findViewById(sBgSelectorSelectionMap.get(mWorkingNote.getBgColorId())).setVisibility(
|
|
|
|
|
View.VISIBLE);//从预定义哈希表中找到对应项,设置为可见
|
|
|
|
|
mNoteEditorPanel.setBackgroundResource(mWorkingNote.getBgColorResId());//设置便签编辑器的背景资源
|
|
|
|
|
mHeadViewPanel.setBackgroundResource(mWorkingNote.getTitleBgResId());//将便签头的背景资源设置为标题的背景色
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {//对选择菜单的准备
|
|
|
|
|
if (isFinishing()) {//如果窗口正在关闭,则不做处理
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
clearSettingState();//清除设置状态
|
|
|
|
|
menu.clear();//清除菜单项
|
|
|
|
|
if (mWorkingNote.getFolderId() == Notes.ID_CALL_RECORD_FOLDER) {//如果是通话记录文件夹
|
|
|
|
|
getMenuInflater().inflate(R.menu.call_note_edit, menu);//实例化Menu目录下的Menu布局文件
|
|
|
|
|
} else {//其他
|
|
|
|
|
getMenuInflater().inflate(R.menu.note_edit, menu);
|
|
|
|
|
}
|
|
|
|
|
if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) {//如果workingNote模式为核对列表模式
|
|
|
|
|
menu.findItem(R.id.menu_list_mode).setTitle(R.string.menu_normal_mode);//将菜单中的菜单列表模式的标题设置为“退出清单模式”
|
|
|
|
|
} else {//开启清单模式
|
|
|
|
|
menu.findItem(R.id.menu_list_mode).setTitle(R.string.menu_list_mode);
|
|
|
|
|
}
|
|
|
|
|
if (mWorkingNote.hasClockAlert()) {//如果便签有闹钟提醒,就在界面进行显示
|
|
|
|
|
menu.findItem(R.id.menu_alert).setVisible(false);//将添加提醒的菜单选项设置为不可见
|
|
|
|
|
} else {
|
|
|
|
|
menu.findItem(R.id.menu_delete_remind).setVisible(false);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {//选中菜单的操作
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
|
case R.id.menu_new_note:
|
|
|
|
|
createNewNote();//创建新标签
|
|
|
|
|
break;
|
|
|
|
|
case R.id.menu_delete://删除便签
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(this);//建立一个删除对话框
|
|
|
|
|
builder.setTitle(getString(R.string.alert_title_delete));//设置对话框标题
|
|
|
|
|
builder.setIcon(android.R.drawable.ic_dialog_alert);//设置图标
|
|
|
|
|
builder.setMessage(getString(R.string.alert_message_delete_note));//设置内容
|
|
|
|
|
builder.setPositiveButton(android.R.string.ok,
|
|
|
|
|
new DialogInterface.OnClickListener() {//设置一个确定按键和监听器
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {//点击所触发事件
|
|
|
|
|
deleteCurrentNote();//删除当前的便签
|
|
|
|
|
finish();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, null);//设置一个取消按键和监听器
|
|
|
|
|
builder.show();
|
|
|
|
|
break;
|
|
|
|
|
case R.id.menu_font_size://菜单字体大小的设置
|
|
|
|
|
mFontSizeSelector.setVisibility(View.VISIBLE);//将字体的选择器置为可见
|
|
|
|
|
findViewById(sFontSelectorSelectionMap.get(mFontSizeId)).setVisibility(View.VISIBLE);//通过id找到相应字体的大小
|
|
|
|
|
break;
|
|
|
|
|
case R.id.menu_list_mode://选择清单模式
|
|
|
|
|
mWorkingNote.setCheckListMode(mWorkingNote.getCheckListMode() == 0 ?
|
|
|
|
|
TextNote.MODE_CHECK_LIST : 0);//设置清单模式,若当前为清单模式,则取消;否之则设为清单模式
|
|
|
|
|
break;
|
|
|
|
|
case R.id.menu_share://分享
|
|
|
|
|
getWorkingText();//获取当前便签的文本
|
|
|
|
|
sendTo(this, mWorkingNote.getContent());//发送到遍历的本文内
|
|
|
|
|
break;
|
|
|
|
|
case R.id.menu_send_to_desktop://发送到桌面
|
|
|
|
|
sendToDesktop();
|
|
|
|
|
break;
|
|
|
|
|
case R.id.menu_alert://设置闹钟提醒
|
|
|
|
|
setReminder();
|
|
|
|
|
break;
|
|
|
|
|
case R.id.menu_delete_remind://选择删除提醒
|
|
|
|
|
mWorkingNote.setAlertDate(0, false);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setReminder() {//建立事件提醒器
|
|
|
|
|
DateTimePickerDialog d = new DateTimePickerDialog(this, System.currentTimeMillis());//建立修改时间日期的对话框
|
|
|
|
|
d.setOnDateTimeSetListener(new OnDateTimeSetListener() {//设置日期时间设定的监听器
|
|
|
|
|
public void OnDateTimeSet(AlertDialog dialog, long date) {//设置日期提醒
|
|
|
|
|
mWorkingNote.setAlertDate(date , true);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
d.show();//显示该控件
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Share note to apps that support {@link Intent#ACTION_SEND} action
|
|
|
|
|
* and {@text/plain} type
|
|
|
|
|
*/
|
|
|
|
|
private void sendTo(Context context, String info) {//分享功能
|
|
|
|
|
Intent intent = new Intent(Intent.ACTION_SEND);//新建发送活动的传值对象
|
|
|
|
|
intent.putExtra(Intent.EXTRA_TEXT, info);//将需要传递的便签信息放入text文件中
|
|
|
|
|
intent.setType("text/plain");//设置发送内容的类型
|
|
|
|
|
context.startActivity(intent);//开始活动
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createNewNote() {//创建一个新的便签
|
|
|
|
|
// Firstly, save current editing notes
|
|
|
|
|
saveNote();
|
|
|
|
|
|
|
|
|
|
// For safety, start a new NoteEditActivity
|
|
|
|
|
finish();
|
|
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);//设置一个新的便签活动编辑器
|
|
|
|
|
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);//将当前活动设置为插入或者编辑
|
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_FOLDER_ID, mWorkingNote.getFolderId());//将文件夹id附加到intent里
|
|
|
|
|
startActivity(intent);//开始activity并链接
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void deleteCurrentNote() {//删除当前的便签
|
|
|
|
|
if (mWorkingNote.existInDatabase()) {//先判断便签是否在数据库中
|
|
|
|
|
HashSet<Long> ids = new HashSet<Long>();
|
|
|
|
|
long id = mWorkingNote.getNoteId();//获得当前便签的ID
|
|
|
|
|
if (id != Notes.ID_ROOT_FOLDER) {//如果不是头文件夹建立一个hash表把便签id存起来
|
|
|
|
|
ids.add(id);
|
|
|
|
|
} else {//否则输出错误
|
|
|
|
|
Log.d(TAG, "Wrong note id, should not happen");
|
|
|
|
|
}
|
|
|
|
|
if (!isSyncMode()) {//在非同步模式情况下
|
|
|
|
|
if (!DataUtils.batchDeleteNotes(getContentResolver(), ids)) {
|
|
|
|
|
Log.e(TAG, "Delete Note error");
|
|
|
|
|
}
|
|
|
|
|
} else {//将便签放入回收站
|
|
|
|
|
if (!DataUtils.batchMoveToFolder(getContentResolver(), ids, Notes.ID_TRASH_FOLER)) {
|
|
|
|
|
Log.e(TAG, "Move notes to trash folder error, should not happens");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mWorkingNote.markDeleted(true);//将删除的便签标记为true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isSyncMode() {//判断是否为同步模式
|
|
|
|
|
return NotesPreferenceActivity.getSyncAccountName(this).trim().length() > 0;
|
|
|
|
|
}//返回同步的用户名长度,如果大于0则有同步状态,否则非同步
|
|
|
|
|
|
|
|
|
|
public void onClockAlertChanged(long date, boolean set) {//设置同步时间
|
|
|
|
|
/**
|
|
|
|
|
* User could set clock to an unsaved note, so before setting the
|
|
|
|
|
* alert clock, we should save the note first
|
|
|
|
|
*/
|
|
|
|
|
if (!mWorkingNote.existInDatabase()) {//若当前编辑的便签不在数据库中,则先保存便签
|
|
|
|
|
saveNote();
|
|
|
|
|
}
|
|
|
|
|
if (mWorkingNote.getNoteId() > 0) {//若当前编辑的便签的id>0,则执行下面函数
|
|
|
|
|
Intent intent = new Intent(this, AlarmReceiver.class);//新建一个intent,指向活动AlarmReceiver
|
|
|
|
|
intent.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mWorkingNote.getNoteId()));//若有运行的便签就将便签的ID放置在URI中去
|
|
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);//将intent设置为延时广播
|
|
|
|
|
AlarmManager alarmManager = ((AlarmManager) getSystemService(ALARM_SERVICE));//新建一个闹钟管理器,获取系统的闹钟服务
|
|
|
|
|
showAlertHeader();//设置提醒管理器
|
|
|
|
|
if(!set) {//取消闹钟
|
|
|
|
|
alarmManager.cancel(pendingIntent);
|
|
|
|
|
} else {//设置闹钟
|
|
|
|
|
alarmManager.set(AlarmManager.RTC_WAKEUP, date, pendingIntent);
|
|
|
|
|
}
|
|
|
|
|
} else {//如果没有设置就报错
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
* should input something
|
|
|
|
|
*/
|
|
|
|
|
Log.e(TAG, "Clock alert setting error");
|
|
|
|
|
showToast(R.string.error_note_empty_for_clock);//显示消息提示框,为未编辑的便签设置闹钟
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onWidgetChanged() {//Widget发生改变的所触发的事件
|
|
|
|
|
updateWidget();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onEditTextDelete(int index, String text) {//删除编辑文本框时的操作
|
|
|
|
|
int childCount = mEditTextList.getChildCount();//获取EditText列表里EditText项的数量
|
|
|
|
|
if (childCount == 1) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = index + 1; i < childCount; i++) {//将index后面的每一个项覆盖前面的项,即将index的项删除,后面补上去
|
|
|
|
|
((NoteEditText) mEditTextList.getChildAt(i).findViewById(R.id.et_edit_text))
|
|
|
|
|
.setIndex(i - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mEditTextList.removeViewAt(index);//移除特定位置的视图
|
|
|
|
|
NoteEditText edit = null;
|
|
|
|
|
if(index == 0) {//如果index为0,则将edit定位到第一个EditText项
|
|
|
|
|
edit = (NoteEditText) mEditTextList.getChildAt(0).findViewById(
|
|
|
|
|
R.id.et_edit_text);
|
|
|
|
|
} else {//编辑上一行
|
|
|
|
|
edit = (NoteEditText) mEditTextList.getChildAt(index - 1).findViewById(
|
|
|
|
|
R.id.et_edit_text);
|
|
|
|
|
}
|
|
|
|
|
int length = edit.length();//获取当前项的长度
|
|
|
|
|
edit.append(text);//在当前位置后附加文本
|
|
|
|
|
edit.requestFocus();//获取焦点
|
|
|
|
|
edit.setSelection(length);//定位到length位置处的条目
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onEditTextEnter(int index, String text) {//进入编辑文本框所触发的事件
|
|
|
|
|
/**
|
|
|
|
|
* Should not happen, check for debug
|
|
|
|
|
*/
|
|
|
|
|
if(index > mEditTextList.getChildCount()) {//如果越界就报错
|
|
|
|
|
Log.e(TAG, "Index out of mEditTextList boundrary, should not happen");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
View view = getListItem(text, index);//获得列表项
|
|
|
|
|
mEditTextList.addView(view, index);//添加视图
|
|
|
|
|
NoteEditText edit = (NoteEditText) view.findViewById(R.id.et_edit_text);//建立一个新的视图并添加到编辑文本框内
|
|
|
|
|
edit.requestFocus();//获取焦点
|
|
|
|
|
edit.setSelection(0);//定位到起始位置
|
|
|
|
|
for (int i = index + 1; i < mEditTextList.getChildCount(); i++) {//遍历子文本框并设置对应对下标
|
|
|
|
|
((NoteEditText) mEditTextList.getChildAt(i).findViewById(R.id.et_edit_text))
|
|
|
|
|
.setIndex(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void switchToListMode(String text) {//切换到清单模式
|
|
|
|
|
mEditTextList.removeAllViews();//移除所有子视图
|
|
|
|
|
String[] items = text.split("\n");//根据换行符分割字符串
|
|
|
|
|
int index = 0;//初始化下标
|
|
|
|
|
for (String item : items) {//遍历所有文本单元
|
|
|
|
|
if(!TextUtils.isEmpty(item)) {//如果item中的文本不为空
|
|
|
|
|
mEditTextList.addView(getListItem(item, index));//向EditTextList中添加该字符串
|
|
|
|
|
index++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mEditTextList.addView(getListItem("", index));//最后加入一空条目
|
|
|
|
|
mEditTextList.getChildAt(index).findViewById(R.id.et_edit_text).requestFocus();//获取当前列表项焦点
|
|
|
|
|
|
|
|
|
|
mNoteEditor.setVisibility(View.GONE);//便签编辑器不可见
|
|
|
|
|
mEditTextList.setVisibility(View.VISIBLE);//将文本编辑框置为可见
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Spannable getHighlightQueryResult(String fullText, String userQuery) {//获取高亮效果的反馈情况
|
|
|
|
|
SpannableString spannable = new SpannableString(fullText == null ? "" : fullText);//新建一个效果选项
|
|
|
|
|
if (!TextUtils.isEmpty(userQuery)) {//如果搜索结果不为空
|
|
|
|
|
mPattern = Pattern.compile(userQuery);//为搜索条件设置正则表达式
|
|
|
|
|
Matcher m = mPattern.matcher(fullText);//建立一个状态机检查Pattern并进行匹配
|
|
|
|
|
int start = 0;
|
|
|
|
|
while (m.find(start)) {//将匹配到的部分显示为高亮
|
|
|
|
|
spannable.setSpan(//将目标设置为高亮
|
|
|
|
|
new BackgroundColorSpan(this.getResources().getColor(
|
|
|
|
|
R.color.user_query_highlight)), m.start(), m.end(),
|
|
|
|
|
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
|
|
|
|
|
start = m.end();//更新起始位置
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return spannable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private View getListItem(String item, int index) {//获取列表项
|
|
|
|
|
View view = LayoutInflater.from(this).inflate(R.layout.note_edit_list_item, null);//创建一个视图
|
|
|
|
|
final NoteEditText edit = (NoteEditText) view.findViewById(R.id.et_edit_text);//获得编辑文本
|
|
|
|
|
edit.setTextAppearance(this, TextAppearanceResources.getTexAppearanceResource(mFontSizeId));//创建一个文本编辑框并置为可见
|
|
|
|
|
CheckBox cb = ((CheckBox) view.findViewById(R.id.cb_edit_item));//建立一个打钩框并设置监听器
|
|
|
|
|
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {//设置监听器
|
|
|
|
|
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
|
|
|
|
|
if (isChecked) {//如果已经被标记
|
|
|
|
|
edit.setPaintFlags(edit.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
|
|
|
|
|
} else {
|
|
|
|
|
edit.setPaintFlags(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (item.startsWith(TAG_CHECKED)) {//判断是否勾选
|
|
|
|
|
cb.setChecked(true);
|
|
|
|
|
edit.setPaintFlags(edit.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
|
|
|
|
|
item = item.substring(TAG_CHECKED.length(), item.length()).trim();//去掉TAG_CHECKED和空格
|
|
|
|
|
} else if (item.startsWith(TAG_UNCHECKED)) {//如果未勾选
|
|
|
|
|
cb.setChecked(false);
|
|
|
|
|
edit.setPaintFlags(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
|
|
|
|
|
item = item.substring(TAG_UNCHECKED.length(), item.length()).trim();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
edit.setOnTextViewChangeListener(this);//为edit设置监听文本视图改变的监听器
|
|
|
|
|
edit.setIndex(index);//运行编辑框的监听器对该行为作出反应,并设置下标及文本内容
|
|
|
|
|
edit.setText(getHighlightQueryResult(item, mUserQuery));//设置高亮
|
|
|
|
|
return view;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onTextChange(int index, boolean hasText) {//便签内容发生改变
|
|
|
|
|
if (index >= mEditTextList.getChildCount()) {//越界报错
|
|
|
|
|
Log.e(TAG, "Wrong index, should not happen");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if(hasText) {//如果内容不为空则将其子编辑框可见性置为可见,否则不可见
|
|
|
|
|
mEditTextList.getChildAt(index).findViewById(R.id.cb_edit_item).setVisibility(View.VISIBLE);
|
|
|
|
|
} else {
|
|
|
|
|
mEditTextList.getChildAt(index).findViewById(R.id.cb_edit_item).setVisibility(View.GONE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onCheckListModeChanged(int oldMode, int newMode) {//清单模式和列表模式的切换
|
|
|
|
|
if (newMode == TextNote.MODE_CHECK_LIST) {//新的模式是清单模式时
|
|
|
|
|
switchToListMode(mNoteEditor.getText().toString());//切换清单模式
|
|
|
|
|
} else {
|
|
|
|
|
if (!getWorkingText()) {//.若获取到文本就改变其检查标记
|
|
|
|
|
mWorkingNote.setWorkingText(mWorkingNote.getContent().replace(TAG_UNCHECKED + " ",
|
|
|
|
|
""));//设置工作文本
|
|
|
|
|
}
|
|
|
|
|
mNoteEditor.setText(getHighlightQueryResult(mWorkingNote.getContent(), mUserQuery));//设置高亮文本
|
|
|
|
|
mEditTextList.setVisibility(View.GONE);//清单的视图不可见
|
|
|
|
|
mNoteEditor.setVisibility(View.VISIBLE);//便签编辑的视图可见
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean getWorkingText() {//设置勾选选项表并返回是否勾选的标记
|
|
|
|
|
boolean hasChecked = false;//初始化check标记
|
|
|
|
|
if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) {//判断是否是清单模式
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
for (int i = 0; i < mEditTextList.getChildCount(); i++) {//遍历所有子编辑框的视图
|
|
|
|
|
View view = mEditTextList.getChildAt(i);
|
|
|
|
|
NoteEditText edit = (NoteEditText) view.findViewById(R.id.et_edit_text);//获得文本编辑框
|
|
|
|
|
if (!TextUtils.isEmpty(edit.getText())) {//若文本不为空
|
|
|
|
|
if (((CheckBox) view.findViewById(R.id.cb_edit_item)).isChecked()) {//判断是否勾选
|
|
|
|
|
sb.append(TAG_CHECKED).append(" ").append(edit.getText()).append("\n");//扩展字符串为已打钩并把标记置true
|
|
|
|
|
hasChecked = true;
|
|
|
|
|
} else {
|
|
|
|
|
sb.append(TAG_UNCHECKED).append(" ").append(edit.getText()).append("\n");//字符串设置为未勾选
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mWorkingNote.setWorkingText(sb.toString());//利用编辑好的字符串设置运行便签的内容
|
|
|
|
|
} else {//若不是该模式直接用编辑器中的内容设置运行中标签的内容
|
|
|
|
|
mWorkingNote.setWorkingText(mNoteEditor.getText().toString());
|
|
|
|
|
}
|
|
|
|
|
return hasChecked;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean saveNote() {//保存便签
|
|
|
|
|
getWorkingText();//获取到当前编辑便签的文本
|
|
|
|
|
boolean saved = mWorkingNote.saveNote();
|
|
|
|
|
if (saved) {//如果workingNote已经保存,则将结果设置为ok
|
|
|
|
|
/**
|
|
|
|
|
* There are two modes from List view to edit view, open one note,
|
|
|
|
|
* create/edit a node. Opening node requires to the original
|
|
|
|
|
* position in the list when back from edit view, while creating a
|
|
|
|
|
* new node requires to the top of the list. This code
|
|
|
|
|
* {@link #RESULT_OK} is used to identify the create/edit state
|
|
|
|
|
*/
|
|
|
|
|
setResult(RESULT_OK);
|
|
|
|
|
}
|
|
|
|
|
return saved;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void sendToDesktop() {//将便签发送至桌面
|
|
|
|
|
/**
|
|
|
|
|
* Before send message to home, we should make sure that current
|
|
|
|
|
* editing note is exists in databases. So, for new note, firstly
|
|
|
|
|
* save it
|
|
|
|
|
*/
|
|
|
|
|
if (!mWorkingNote.existInDatabase()) {//若不存在于数据库,就保存
|
|
|
|
|
saveNote();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (mWorkingNote.getNoteId() > 0) {//若是有内容
|
|
|
|
|
Intent sender = new Intent();//建立一个连接器
|
|
|
|
|
Intent shortcutIntent = new Intent(this, NoteEditActivity.class);//建立一个连接器
|
|
|
|
|
shortcutIntent.setAction(Intent.ACTION_VIEW);//链接为一个视图
|
|
|
|
|
shortcutIntent.putExtra(Intent.EXTRA_UID, mWorkingNote.getNoteId());//快捷方式的名称
|
|
|
|
|
sender.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);//设置快捷访问的Intent,将其保存至sender中,以便直接快速调用NoteEditActivity
|
|
|
|
|
sender.putExtra(Intent.EXTRA_SHORTCUT_NAME,
|
|
|
|
|
makeShortcutIconTitle(mWorkingNote.getContent()));//设置图标
|
|
|
|
|
sender.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
|
|
|
|
|
Intent.ShortcutIconResource.fromContext(this, R.drawable.icon_app));//将便签的相关信息都添加到要发送的文件里
|
|
|
|
|
sender.putExtra("duplicate", true);
|
|
|
|
|
sender.setAction("com.android.launcher.action.INSTALL_SHORTCUT");//设置sneder的行为是发送
|
|
|
|
|
showToast(R.string.info_note_enter_desktop);//显示消息提示框,表示文本已发送至桌面
|
|
|
|
|
sendBroadcast(sender);
|
|
|
|
|
} else {
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
|
* should input something
|
|
|
|
|
*/
|
|
|
|
|
Log.e(TAG, "Send to desktop error");//空便签直接报错
|
|
|
|
|
showToast(R.string.error_note_empty_for_send_to_desktop);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String makeShortcutIconTitle(String content) {//编辑小图标的标题
|
|
|
|
|
content = content.replace(TAG_CHECKED, "");//去掉勾选标记
|
|
|
|
|
content = content.replace(TAG_UNCHECKED, "");
|
|
|
|
|
return content.length() > SHORTCUT_ICON_TITLE_MAX_LEN ? content.substring(0,
|
|
|
|
|
SHORTCUT_ICON_TITLE_MAX_LEN) : content;//如果超出最大长度则截取前面最长长度段
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showToast(int resId) {//显示提示的视图
|
|
|
|
|
showToast(resId, Toast.LENGTH_SHORT);//短时间显示消息提示框
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showToast(int resId, int duration) {//持续显示提示的视图
|
|
|
|
|
Toast.makeText(this, resId, duration).show();//设置toast消息的文本内容
|
|
|
|
|
}
|
|
|
|
|
}
|