|
|
/*
|
|
|
* 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;
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
```java
|
|
|
// 便签编辑活动,实现多种监听器接口
|
|
|
public class NoteEditActivity extends Activity implements OnClickListener,
|
|
|
NoteSettingChangedListener, OnTextViewChangeListener {
|
|
|
|
|
|
// 头部视图持有者,用于缓存视图引用
|
|
|
private class HeadViewHolder {
|
|
|
public TextView tvModified; // 修改时间显示
|
|
|
public ImageView ivAlertIcon; // 提醒图标
|
|
|
public TextView tvAlertDate; // 提醒日期显示
|
|
|
public ImageView ibSetBgColor; // 设置背景颜色按钮
|
|
|
}
|
|
|
|
|
|
// 背景颜色选择器按钮ID与颜色ID的映射
|
|
|
private static final Map<Integer, Integer> sBgSelectorBtnsMap = new HashMap<Integer, Integer>();
|
|
|
static {
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
// 背景颜色ID与选中状态视图ID的映射
|
|
|
private static final Map<Integer, Integer> sBgSelectorSelectionMap = new HashMap<Integer, Integer>();
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
// 字体大小选择器视图ID与字体大小ID的映射
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
// 字体大小ID与选中状态视图ID的映射
|
|
|
private static final Map<Integer, Integer> sFontSelectorSelectionMap = new HashMap<Integer, Integer>();
|
|
|
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"; // 日志标签
|
|
|
|
|
|
// 视图引用
|
|
|
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; // 当前字体大小ID
|
|
|
private static final String PREFERENCE_FONT_SIZE = "pref_font_size"; // 字体大小偏好键
|
|
|
|
|
|
// 常量定义
|
|
|
private static final int SHORTCUT_ICON_TITLE_MAX_LEN = 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(); // 初始化资源
|
|
|
}
|
|
|
|
|
|
// 恢复实例状态(内存不足时可能被杀死)
|
|
|
@Override
|
|
|
protected void onRestoreInstanceState(Bundle savedInstanceState) {
|
|
|
super.onRestoreInstanceState(savedInstanceState);
|
|
|
// 从保存的状态中恢复便签ID
|
|
|
if (savedInstanceState != null && savedInstanceState.containsKey(Intent.EXTRA_UID)) {
|
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
|
intent.putExtra(Intent.EXTRA_UID, savedInstanceState.getLong(Intent.EXTRA_UID));
|
|
|
if (!initActivityState(intent)) {
|
|
|
finish();
|
|
|
return;
|
|
|
}
|
|
|
Log.d(TAG, "Restoring from killed activity");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 初始化活动状态
|
|
|
private boolean initActivityState(Intent intent) {
|
|
|
mWorkingNote = null;
|
|
|
|
|
|
// 处理查看便签的请求
|
|
|
if (TextUtils.equals(Intent.ACTION_VIEW, intent.getAction())) {
|
|
|
long noteId = intent.getLongExtra(Intent.EXTRA_UID, 0);
|
|
|
mUserQuery = "";
|
|
|
|
|
|
// 处理从搜索结果进入的情况
|
|
|
if (intent.hasExtra(SearchManager.EXTRA_DATA_KEY)) {
|
|
|
noteId = Long.parseLong(intent.getStringExtra(SearchManager.EXTRA_DATA_KEY));
|
|
|
mUserQuery = intent.getStringExtra(SearchManager.USER_QUERY);
|
|
|
}
|
|
|
|
|
|
// 检查便签是否存在
|
|
|
if (!DataUtils.visibleInNoteDatabase(getContentResolver(), noteId, Notes.TYPE_NOTE)) {
|
|
|
Intent jump = new Intent(this, NotesListActivity.class);
|
|
|
startActivity(jump);
|
|
|
showToast(R.string.error_note_not_exist);
|
|
|
finish();
|
|
|
return false;
|
|
|
} else {
|
|
|
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);
|
|
|
|
|
|
} else if(TextUtils.equals(Intent.ACTION_INSERT_OR_EDIT, intent.getAction())) {
|
|
|
// 新建或编辑便签
|
|
|
long folderId = intent.getLongExtra(Notes.INTENT_EXTRA_FOLDER_ID, 0);
|
|
|
int widgetId = intent.getIntExtra(Notes.INTENT_EXTRA_WIDGET_ID,
|
|
|
AppWidgetManager.INVALID_APPWIDGET_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));
|
|
|
|
|
|
// 解析通话记录便签
|
|
|
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
|
|
|
long callDate = intent.getLongExtra(Notes.INTENT_EXTRA_CALL_DATE, 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) {
|
|
|
mWorkingNote = WorkingNote.load(this, noteId);
|
|
|
if (mWorkingNote == null) {
|
|
|
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() {
|
|
|
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));
|
|
|
|
|
|
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);
|
|
|
};
|
|
|
}
|
|
|
|
|
|
// 处理新Intent(例如从搜索进入)
|
|
|
@Override
|
|
|
protected void onNewIntent(Intent intent) {
|
|
|
super.onNewIntent(intent);
|
|
|
initActivityState(intent); // 重新初始化活动状态
|
|
|
}
|
|
|
|
|
|
// 保存实例状态
|
|
|
@Override
|
|
|
protected void onSaveInstanceState(Bundle outState) {
|
|
|
super.onSaveInstanceState(outState);
|
|
|
// 新便签需要先保存以生成ID
|
|
|
if (!mWorkingNote.existInDatabase()) {
|
|
|
saveNote();
|
|
|
}
|
|
|
outState.putLong(Intent.EXTRA_UID, mWorkingNote.getNoteId()); // 保存便签ID
|
|
|
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];
|
|
|
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;
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
// 初始化资源(视图绑定等)
|
|
|
private void initResources() {
|
|
|
// 绑定头部视图
|
|
|
mHeadViewPanel = findViewById(R.id.note_title);
|
|
|
mNoteHeaderHolder = new 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);
|
|
|
// 修复字体大小ID超出范围的bug
|
|
|
if(mFontSizeId >= TextAppearanceResources.getResourcesSize()) {
|
|
|
mFontSizeId = ResourceParser.BG_DEFAULT_FONT_SIZE;
|
|
|
}
|
|
|
|
|
|
mEditTextList = (LinearLayout) findViewById(R.id.note_edit_list); // 列表模式容器
|
|
|
}
|
|
|
|
|
|
// 活动暂停时保存便签
|
|
|
@Override
|
|
|
protected void 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);
|
|
|
// 根据小部件类型设置对应的接收器
|
|
|
if (mWorkingNote.getWidgetType() == Notes.TYPE_WIDGET_2X) {
|
|
|
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");
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {
|
|
|
mWorkingNote.getWidgetId() // 小部件ID
|
|
|
});
|
|
|
|
|
|
sendBroadcast(intent);
|
|
|
setResult(RESULT_OK, intent);
|
|
|
}
|
|
|
|
|
|
// 点击事件处理
|
|
|
public void onClick(View v) {
|
|
|
int id = v.getId();
|
|
|
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));
|
|
|
}
|
|
|
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); // 通话记录菜单
|
|
|
} else {
|
|
|
getMenuInflater().inflate(R.menu.note_edit, menu); // 普通便签菜单
|
|
|
}
|
|
|
|
|
|
// 根据列表模式设置菜单项文本
|
|
|
if (mWorkingNote.getCheckListMode() == TextNote.MODE_CHECK_LIST) {
|
|
|
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);
|
|
|
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();
|
|
|
}
|
|
|
|
|
|
// 分享便签内容
|
|
|
private void sendTo(Context context, String info) {
|
|
|
Intent intent = new Intent(Intent.ACTION_SEND);
|
|
|
intent.putExtra(Intent.EXTRA_TEXT, info);
|
|
|
intent.setType("text/plain"); // 纯文本类型
|
|
|
context.startActivity(intent);
|
|
|
}
|
|
|
|
|
|
// 创建新便签
|
|
|
private void createNewNote() {
|
|
|
saveNote(); // 先保存当前便签
|
|
|
finish(); // 结束当前活动
|
|
|
// 启动新的便签编辑活动
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
|
|
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_FOLDER_ID, mWorkingNote.getFolderId());
|
|
|
startActivity(intent);
|
|
|
}
|
|
|
|
|
|
// 删除当前便签
|
|
|
private void deleteCurrentNote() {
|
|
|
if (mWorkingNote.existInDatabase()) {
|
|
|
HashSet<Long> ids = new HashSet<Long>();
|
|
|
long id = mWorkingNote.getNoteId();
|
|
|
if (id != Notes.ID_ROOT_FOLDER) {
|
|
|
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); // 标记为已删除
|
|
|
}
|
|
|
|
|
|
// 检查是否为同步模式
|
|
|
private boolean isSyncMode() {
|
|
|
return NotesPreferenceActivity.getSyncAccountName(this).trim().length() > 0;
|
|
|
}
|
|
|
|
|
|
// 提醒时间改变回调
|
|
|
public void onClockAlertChanged(long date, boolean set) {
|
|
|
// 未保存的便签需要先保存
|
|
|
if (!mWorkingNote.existInDatabase()) {
|
|
|
saveNote();
|
|
|
}
|
|
|
if (mWorkingNote.getNoteId() > 0) {
|
|
|
// 设置闹钟提醒
|
|
|
Intent intent = new Intent(this, AlarmReceiver.class);
|
|
|
intent.setData(ContentUris.withAppendedId(Notes.CONTENT_NOTE_URI, mWorkingNote.getNoteId()));
|
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
|
|
|
AlarmManager alarmManager = ((AlarmManager) getSystemService(ALARM_SERVICE));
|
|
|
showAlertHeader(); // 更新提醒显示
|
|
|
if(!set) {
|
|
|
alarmManager.cancel(pendingIntent); // 取消提醒
|
|
|
} else {
|
|
|
alarmManager.set(AlarmManager.RTC_WAKEUP, date, pendingIntent); // 设置提醒
|
|
|
}
|
|
|
} else {
|
|
|
// 空便签不能设置提醒
|
|
|
Log.e(TAG, "Clock alert setting error");
|
|
|
showToast(R.string.error_note_empty_for_clock);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 小部件改变回调
|
|
|
public void onWidgetChanged() {
|
|
|
updateWidget(); // 更新小部件
|
|
|
}
|
|
|
|
|
|
// 列表模式:删除文本项回调
|
|
|
public void onEditTextDelete(int index, String text) {
|
|
|
int childCount = mEditTextList.getChildCount();
|
|
|
if (childCount == 1) { // 至少保留一项
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
// 更新后续项的索引
|
|
|
for (int i = index + 1; i < childCount; i++) {
|
|
|
((NoteEditText) mEditTextList.getChildAt(i).findViewById(R.id.et_edit_text))
|
|
|
.setIndex(i - 1);
|
|
|
}
|
|
|
|
|
|
mEditTextList.removeViewAt(index); // 删除视图
|
|
|
NoteEditText edit = null;
|
|
|
// 确定焦点位置
|
|
|
if(index == 0) {
|
|
|
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);
|
|
|
}
|
|
|
|
|
|
// 列表模式:回车换行回调
|
|
|
public void onEditTextEnter(int index, String text) {
|
|
|
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)) {
|
|
|
mEditTextList.addView(getListItem(item, index)); // 添加列表项
|
|
|
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);
|
|
|
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(); // 移除标记
|
|
|
} 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.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;
|
|
|
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");
|
|
|
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) {
|
|
|
setResult(RESULT_OK); // 设置返回结果
|
|
|
}
|
|
|
return saved;
|
|
|
}
|
|
|
|
|
|
// 发送到桌面快捷方式
|
|
|
private void sendToDesktop() {
|
|
|
// 确保便签已保存
|
|
|
if (!mWorkingNote.existInDatabase()) {
|
|
|
saveNote();
|
|
|
}
|
|
|
|
|
|
if (mWorkingNote.getNoteId() > 0) {
|
|
|
// 创建快捷方式Intent
|
|
|
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);
|
|
|
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); // 允许
|