|
|
|
@ -0,0 +1,966 @@
|
|
|
|
|
/*
|
|
|
|
|
* 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.AlertDialog;
|
|
|
|
|
import android.app.Dialog;
|
|
|
|
|
import android.appwidget.AppWidgetManager;//APP的界宽
|
|
|
|
|
import android.content.AsyncQueryHandler;
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
|
import android.content.Context;
|
|
|
|
|
import android.content.DialogInterface;
|
|
|
|
|
import android.content.Intent;
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
|
import android.database.Cursor;
|
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.preference.PreferenceManager;
|
|
|
|
|
import android.text.Editable;
|
|
|
|
|
import android.text.TextUtils;
|
|
|
|
|
import android.text.TextWatcher;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.ActionMode;
|
|
|
|
|
import android.view.ContextMenu;
|
|
|
|
|
import android.view.ContextMenu.ContextMenuInfo;
|
|
|
|
|
import android.view.Display;
|
|
|
|
|
import android.view.HapticFeedbackConstants;
|
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
|
import android.view.Menu;
|
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
import android.view.MenuItem.OnMenuItemClickListener;
|
|
|
|
|
import android.view.MotionEvent;
|
|
|
|
|
import android.view.View;
|
|
|
|
|
import android.view.View.OnClickListener;
|
|
|
|
|
import android.view.View.OnCreateContextMenuListener;
|
|
|
|
|
import android.view.View.OnTouchListener;
|
|
|
|
|
import android.view.inputmethod.InputMethodManager;
|
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
|
import android.widget.AdapterView.OnItemClickListener;
|
|
|
|
|
import android.widget.AdapterView.OnItemLongClickListener;
|
|
|
|
|
import android.widget.Button;
|
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
import android.widget.ListView;
|
|
|
|
|
import android.widget.PopupMenu;
|
|
|
|
|
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.NoteColumns;
|
|
|
|
|
import net.micode.notes.gtask.remote.GTaskSyncService;
|
|
|
|
|
import net.micode.notes.model.WorkingNote;
|
|
|
|
|
import net.micode.notes.tool.BackupUtils;
|
|
|
|
|
import net.micode.notes.tool.DataUtils;
|
|
|
|
|
import net.micode.notes.tool.ResourceParser;
|
|
|
|
|
import net.micode.notes.ui.NotesListAdapter.AppWidgetAttribute;
|
|
|
|
|
import net.micode.notes.widget.NoteWidgetProvider_2x;
|
|
|
|
|
import net.micode.notes.widget.NoteWidgetProvider_4x;
|
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
public class NotesListActivity extends Activity implements OnClickListener, OnItemLongClickListener {//这个类继承于Activity类,实现了两个接口,分别是点击监听器,和长按监听器,即在这个类的界面实现对这两种操作的响应,这是整个project的主类
|
|
|
|
|
private static final int FOLDER_NOTE_LIST_QUERY_TOKEN = 0;//声明并赋值一些不可更改的私有属性
|
|
|
|
|
|
|
|
|
|
private static final int FOLDER_LIST_QUERY_TOKEN = 1;//查询记号,同上,文件夹请求时使用(如:长按便签然后点移动到文件夹调用)
|
|
|
|
|
|
|
|
|
|
private static final int MENU_FOLDER_DELETE = 0;//菜单中的删除文件夹项对应的int值,(个人决定使用int类型,是方便和其他数据处理,而且处理速度更快)
|
|
|
|
|
|
|
|
|
|
private static final int MENU_FOLDER_VIEW = 1;//菜单中的查看文件夹对应的int值
|
|
|
|
|
|
|
|
|
|
private static final int MENU_FOLDER_CHANGE_NAME = 2;//菜单中改文件夹名项对应的int值
|
|
|
|
|
|
|
|
|
|
private static final String PREFERENCE_ADD_INTRODUCTION = "net.micode.notes.introduction";//用于第一次打开小米便签的判断
|
|
|
|
|
|
|
|
|
|
private enum ListEditState {//列表编辑状态类
|
|
|
|
|
NOTE_LIST, SUB_FOLDER, CALL_RECORD_FOLDER
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private ListEditState mState;//利用其它类声明一些私有对象
|
|
|
|
|
|
|
|
|
|
private BackgroundQueryHandler mBackgroundQueryHandler;//后台疑问处理
|
|
|
|
|
|
|
|
|
|
private NotesListAdapter mNotesListAdapter;//便签列表配适器
|
|
|
|
|
|
|
|
|
|
private ListView mNotesListView;//主界面的视图
|
|
|
|
|
|
|
|
|
|
private Button mAddNewNote;//最下方添加便签的按钮
|
|
|
|
|
|
|
|
|
|
private boolean mDispatch;//是否调度的判断变量
|
|
|
|
|
|
|
|
|
|
private int mOriginY;// 首次触摸时屏幕上的垂直距离(y值)
|
|
|
|
|
|
|
|
|
|
private int mDispatchY;//重新调度时的触摸的在屏幕上的垂直距离
|
|
|
|
|
|
|
|
|
|
private TextView mTitleBar;//子文件夹下的 标头
|
|
|
|
|
|
|
|
|
|
private long mCurrentFolderId;//当前文件夹的ID
|
|
|
|
|
|
|
|
|
|
private ContentResolver mContentResolver;//提供内容分析
|
|
|
|
|
|
|
|
|
|
private ModeCallback mModeCallBack;//返回调用方法
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "NotesListActivity";//名称(可用于日志文件调试)
|
|
|
|
|
|
|
|
|
|
public static final int NOTES_LISTVIEW_SCROLL_RATE = 30;//列表滚动速度
|
|
|
|
|
|
|
|
|
|
private NoteItemData mFocusNoteDataItem;//光标指向的物件的数据内容
|
|
|
|
|
|
|
|
|
|
private static final String NORMAL_SELECTION = NoteColumns.PARENT_ID + "=?";// 语句:定义私有字符串变量
|
|
|
|
|
|
|
|
|
|
private static final String ROOT_FOLDER_SELECTION = "(" + NoteColumns.TYPE + "<>"//用于表明处于父文件夹下(主列表)
|
|
|
|
|
+ Notes.TYPE_SYSTEM + " AND " + NoteColumns.PARENT_ID + "=?)" + " OR ("
|
|
|
|
|
+ NoteColumns.ID + "=" + Notes.ID_CALL_RECORD_FOLDER + " AND "
|
|
|
|
|
+ NoteColumns.NOTES_COUNT + ">0)";
|
|
|
|
|
|
|
|
|
|
private final static int REQUEST_CODE_OPEN_NODE = 102;//语句:请求代码开放节点
|
|
|
|
|
private final static int REQUEST_CODE_NEW_NODE = 103;//语句:请求代码新节点
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {//创建类
|
|
|
|
|
super.onCreate(savedInstanceState);//super相当于是指向当前对象的父类,可以用super.xxx来引用父类的成员 引用父类onCreate函数
|
|
|
|
|
setContentView(R.layout.note_list);//设置内容的视图
|
|
|
|
|
initResources();//语句:初始化资源
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Insert an introduction when user firstly use this application
|
|
|
|
|
*/
|
|
|
|
|
setAppInfoFromRawRes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onActivityResult(int requestCode, int resultCode, Intent data) {//对子模块的一些数据进行分析
|
|
|
|
|
if (resultCode == RESULT_OK//结果值与要求值正确
|
|
|
|
|
&& (requestCode == REQUEST_CODE_OPEN_NODE || requestCode == REQUEST_CODE_NEW_NODE)) {
|
|
|
|
|
mNotesListAdapter.changeCursor(null);
|
|
|
|
|
} else {//语句:super调用父类的protected函数,创建窗口时使用
|
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);//调用父类Activity的onActivityResult方法 将数据返回给父类处理
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void setAppInfoFromRawRes() {//通过原生资源设置APP信息
|
|
|
|
|
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);//Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数
|
|
|
|
|
if (!sp.getBoolean(PREFERENCE_ADD_INTRODUCTION, false)) {//其实可以进行反向判断,这样不用一段很长的代码放在一个if里面。
|
|
|
|
|
StringBuilder sb = new StringBuilder();//读取原生资源信息
|
|
|
|
|
InputStream in = null;//输入流初始设置为空
|
|
|
|
|
try {//从原始配置文件当中获取基本信息
|
|
|
|
|
in = getResources().openRawResource(R.raw.introduction);//加载Welcome to use MIUI notes!(本地xml文件)
|
|
|
|
|
if (in != null) {//如果信息不为空
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(in);//使用指定的字符集读取字节并将它们解码为字符
|
|
|
|
|
BufferedReader br = new BufferedReader(isr);//构建输入管道
|
|
|
|
|
char [] buf = new char[1024];
|
|
|
|
|
int len = 0;
|
|
|
|
|
while ((len = br.read(buf)) > 0) {
|
|
|
|
|
sb.append(buf, 0, len);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Read introduction file error");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} catch (IOException e) {//IO错误处理
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return;
|
|
|
|
|
} finally {
|
|
|
|
|
if(in != null) {
|
|
|
|
|
try {
|
|
|
|
|
in.close();
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
// TODO Auto-generated catch block
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WorkingNote note = WorkingNote.createEmptyNote(this, Notes.ID_ROOT_FOLDER,//.新建笔记
|
|
|
|
|
AppWidgetManager.INVALID_APPWIDGET_ID, Notes.TYPE_WIDGET_INVALIDE,
|
|
|
|
|
ResourceParser.RED);
|
|
|
|
|
note.setWorkingText(sb.toString());//设置文本数据
|
|
|
|
|
if (note.saveNote()) {//这是一个判断语句,判断便签是否保存成功
|
|
|
|
|
sp.edit().putBoolean(PREFERENCE_ADD_INTRODUCTION, true).commit();//保存笔记
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Save introduction note error");//如果便签保存不成功,则把错误信息打印到日志里面。
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onStart() {//功能描述:一个activity有生命周期,其中start代表生命周期开始
|
|
|
|
|
函数实现:调用父类(超类)中的onStart方法,并用startAsyncNotesListQuery同步列表的便签信息
|
|
|
|
|
super.onStart();//调用父类,启动活动
|
|
|
|
|
startAsyncNotesListQuery();//异步便签问题?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void initResources() {//初始化资源
|
|
|
|
|
mContentResolver = this.getContentResolver();//获取应用数据
|
|
|
|
|
mBackgroundQueryHandler = new BackgroundQueryHandler(this.getContentResolver());//动态创建后台请求处理器的实例
|
|
|
|
|
mCurrentFolderId = Notes.ID_ROOT_FOLDER;//当前文件夹ID是根目录ID
|
|
|
|
|
mNotesListView = (ListView) findViewById(R.id.notes_list);//根据R文件中的id值查询到相应的View,然后返回
|
|
|
|
|
mNotesListView.addFooterView(LayoutInflater.from(this).inflate(R.layout.note_list_footer, null),//使用布局填充器增加页脚视图
|
|
|
|
|
null, false);
|
|
|
|
|
mNotesListView.setOnItemClickListener(new OnListItemClickListener());//设置视图点击监听器
|
|
|
|
|
mNotesListView.setOnItemLongClickListener(this);//设置长按监听器
|
|
|
|
|
mNotesListAdapter = new NotesListAdapter(this);//创建便签视图配置器
|
|
|
|
|
mNotesListView.setAdapter(mNotesListAdapter);//设置便签视图配置器
|
|
|
|
|
mAddNewNote = (Button) findViewById(R.id.btn_new_note);//findViewById 是安卓编程的定位函数,主要是引用.R文件里的引用名,这里是在activity中获取该按钮
|
|
|
|
|
mAddNewNote.setOnClickListener(this);//屏幕点击监视
|
|
|
|
|
mAddNewNote.setOnTouchListener(new NewNoteOnTouchListener());//语句:屏幕点击监听
|
|
|
|
|
mDispatch = false;//是否调度,主要用于新建便签模块
|
|
|
|
|
mDispatchY = 0;//初始y值设置为0,
|
|
|
|
|
mOriginY = 0;//加载文件夹下的标头资源
|
|
|
|
|
mTitleBar = (TextView) findViewById(R.id.tv_title_bar);//.功能描述:设置Title bar,也就是app页面顶部的返回、选项、信息描述
|
|
|
|
|
mState = ListEditState.NOTE_LIST;// 功能描述:设置状态
|
|
|
|
|
mModeCallBack = new ModeCallback();//implements是一个类实现连接一个接口用的关键字,它是用来实现接口中定义的抽象方法
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class ModeCallback implements ListView.MultiChoiceModeListener, OnMenuItemClickListener {//implements声明自己使用一个或多个接口
|
|
|
|
|
private DropdownMenu mDropDownMenu;//下拉菜单
|
|
|
|
|
private ActionMode mActionMode;//动作方式
|
|
|
|
|
private MenuItem mMoveMenu;//移动菜单
|
|
|
|
|
|
|
|
|
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {//ActionMode 是 Android 提供的一种实现菜单方式
|
|
|
|
|
getMenuInflater().inflate(R.menu.note_list_options, menu);//语句:layout的xml布局文件实例化为View类对象
|
|
|
|
|
menu.findItem(R.id.delete).setOnMenuItemClickListener(this);//这里关联上了listerner,专门关联在菜单的按键
|
|
|
|
|
mMoveMenu = menu.findItem(R.id.move);//调用下面的更新菜单函数
|
|
|
|
|
if (mFocusNoteDataItem.getParentId() == Notes.ID_CALL_RECORD_FOLDER//如果父类id在文件夹中保存或者用户文件数量为零,设置移动菜单为不可见,否者设为可见
|
|
|
|
|
|| DataUtils.getUserFolderCount(mContentResolver) == 0) {
|
|
|
|
|
mMoveMenu.setVisible(false);
|
|
|
|
|
} else {//代码块:设置菜单项目为可见
|
|
|
|
|
mMoveMenu.setVisible(true);
|
|
|
|
|
mMoveMenu.setOnMenuItemClickListener(this);
|
|
|
|
|
}
|
|
|
|
|
mActionMode = mode;
|
|
|
|
|
mNotesListAdapter.setChoiceMode(true);//语句:进入选择模式
|
|
|
|
|
mNotesListView.setLongClickable(false);//语句:关闭长按列表项发生事件功能
|
|
|
|
|
mAddNewNote.setVisibility(View.GONE);//语句:隐藏了新增便签按钮
|
|
|
|
|
|
|
|
|
|
View customView = LayoutInflater.from(NotesListActivity.this).inflate(//设置用户界面
|
|
|
|
|
R.layout.note_list_dropdown_menu, null);//加载下拉菜单的布局
|
|
|
|
|
mode.setCustomView(customView);
|
|
|
|
|
mDropDownMenu = new DropdownMenu(NotesListActivity.this,//创建新的下拉菜单
|
|
|
|
|
(Button) customView.findViewById(R.id.selection_menu),
|
|
|
|
|
R.menu.note_list_dropdown);//语句:为view添加dropDownMenu(包含一个全选操作)
|
|
|
|
|
mDropDownMenu.setOnDropdownMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
|
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {//点击菜单时,设置为全选并更新菜单
|
|
|
|
|
mNotesListAdapter.selectAll(!mNotesListAdapter.isAllSelected());
|
|
|
|
|
updateMenu();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateMenu() {///*
|
|
|
|
|
* 作用:在多选便签/便签文件夹时调用,用于更新多选下拉菜单
|
|
|
|
|
* 实现:用便签列表管理器mNotesListAdapter中的函数getSelectedCount获取选中个数,设置菜单标题为选中个数,
|
|
|
|
|
* 当没有全选时,子菜单标题设为全选,否则,设为取消全选
|
|
|
|
|
* 参数:无
|
|
|
|
|
int selectedCount = mNotesListAdapter.getSelectedCount();//获取被勾选的条目数量
|
|
|
|
|
// Update dropdown menu
|
|
|
|
|
String format = getResources().getString(R.string.menu_select_title, selectedCount);//从原始资源中读取信息更改下拉菜单内容
|
|
|
|
|
mDropDownMenu.setTitle(format);//更改标题
|
|
|
|
|
MenuItem item = mDropDownMenu.findItem(R.id.action_select_all);//全选操作
|
|
|
|
|
if (item != null) {//代码块:当全选成功,则将“全选”菜单项改为“取消全选”菜单,否则仍保持“全选”菜单项
|
|
|
|
|
if (mNotesListAdapter.isAllSelected()) {
|
|
|
|
|
item.setChecked(true);
|
|
|
|
|
item.setTitle(R.string.menu_deselect_all);
|
|
|
|
|
} else {
|
|
|
|
|
item.setChecked(false);
|
|
|
|
|
item.setTitle(R.string.menu_select_all);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {//准备动作模式
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {//菜单动作触发标记
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onDestroyActionMode(ActionMode mode) {//销毁动作模式,设置便签可见
|
|
|
|
|
mNotesListAdapter.setChoiceMode(false);//设置笔记列表适配器选择方式
|
|
|
|
|
mNotesListView.setLongClickable(true);//长按操作
|
|
|
|
|
mAddNewNote.setVisibility(View.VISIBLE);//调整新建笔记为可见
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void finishActionMode() {
|
|
|
|
|
mActionMode.finish();
|
|
|
|
|
}//动作模式结束
|
|
|
|
|
|
|
|
|
|
public void onItemCheckedStateChanged(ActionMode mode, int position, long id,//勾选状态改变时,更改勾选标志,更新菜单
|
|
|
|
|
boolean checked) {//点击菜单选项触发操作
|
|
|
|
|
mNotesListAdapter.setCheckedItem(position, checked);
|
|
|
|
|
updateMenu();//语句:更新菜单
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {//判断菜单是否被点击
|
|
|
|
|
if (mNotesListAdapter.getSelectedCount() == 0) {//当勾选数为零时(即未点击),创建文本并显示
|
|
|
|
|
Toast.makeText(NotesListActivity.this, getString(R.string.menu_select_none),//Toast-Android系统中一种消息框类型
|
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (item.getItemId()) {//根据id号判断是删除还是移动
|
|
|
|
|
case R.id.delete://点击delete选项
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this);//警告对话框
|
|
|
|
|
builder.setTitle(getString(R.string.alert_title_delete));//设置“删除选中的便签”的title
|
|
|
|
|
builder.setIcon(android.R.drawable.ic_dialog_alert);//设置提醒删除的图片
|
|
|
|
|
builder.setMessage(getString(R.string.alert_message_delete_notes,//语句:设置警告对话框的图标
|
|
|
|
|
mNotesListAdapter.getSelectedCount()));
|
|
|
|
|
builder.setPositiveButton(android.R.string.ok,//设置否定按钮
|
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
|
public void onClick(DialogInterface dialog,
|
|
|
|
|
int which) {
|
|
|
|
|
batchDelete();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, null);//取消的按键的视图
|
|
|
|
|
builder.show();
|
|
|
|
|
break;
|
|
|
|
|
case R.id.move://点击move选项
|
|
|
|
|
startQueryDestinationFolders();//启动查询目标文件函数
|
|
|
|
|
break;
|
|
|
|
|
default://语句:default,switch语句结束
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class NewNoteOnTouchListener implements OnTouchListener {//.触摸便签监听器
|
|
|
|
|
|
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {//功能描述:响应触摸新建便签的按键的方法
|
|
|
|
|
函数实现:分为按下和移动和其他情况的判断
|
|
|
|
|
参数描述:
|
|
|
|
|
@v 视图
|
|
|
|
|
switch (event.getAction()) {//获取不同动作对应不同操作
|
|
|
|
|
case MotionEvent.ACTION_DOWN: {//如果是创建新便签,通过计算调整界面大小
|
|
|
|
|
Display display = getWindowManager().getDefaultDisplay();
|
|
|
|
|
int screenHeight = display.getHeight();//语句:获取屏幕高度
|
|
|
|
|
int newNoteViewHeight = mAddNewNote.getHeight();//语句: 获取新增便签的高度
|
|
|
|
|
int start = screenHeight - newNoteViewHeight;// 起始的y值
|
|
|
|
|
int eventY = start + (int) event.getY();//event.getY相当于点击到的地方的y值
|
|
|
|
|
/**
|
|
|
|
|
* Minus TitleBar's height
|
|
|
|
|
*/
|
|
|
|
|
if (mState == ListEditState.SUB_FOLDER) {//减去标题栏的高度
|
|
|
|
|
eventY -= mTitleBar.getHeight();
|
|
|
|
|
start -= mTitleBar.getHeight();
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* HACKME:When click the transparent part of "New Note" button, dispatch
|
|
|
|
|
* the event to the list view behind this button. The transparent part of
|
|
|
|
|
* "New Note" button could be expressed by formula y=-0.12x+94(Unit:pixel)
|
|
|
|
|
* and the line top of the button. The coordinate based on left of the "New
|
|
|
|
|
* Note" button. The 94 represents maximum height of the transparent part.
|
|
|
|
|
* Notice that, if the background of the button changes, the formula should
|
|
|
|
|
* also change. This is very bad, just for the UI designer's strong requirement.
|
|
|
|
|
*/
|
|
|
|
|
if (event.getY() < (event.getX() * (-0.12) + 94)) {//如果当前点击的位置不在“写便签”区域内
|
|
|
|
|
View view = mNotesListView.getChildAt(mNotesListView.getChildCount() - 1//最后得到最后一个元素的view(视图)
|
|
|
|
|
- mNotesListView.getFooterViewsCount());//减去页脚下元素布局数量
|
|
|
|
|
if (view != null && view.getBottom() > start//如果不在新建便签的按钮上,重新调度响应按键
|
|
|
|
|
&& (view.getTop() < (start + 94))) {
|
|
|
|
|
mOriginY = (int) event.getY();//初始按下时的垂直距离
|
|
|
|
|
mDispatchY = eventY;//调度时的垂直距离
|
|
|
|
|
event.setLocation(event.getX(), mDispatchY);//重新给触摸事件定位
|
|
|
|
|
mDispatch = true;//触摸事件定位
|
|
|
|
|
return mNotesListView.dispatchTouchEvent(event);//重新调度,即重新执行
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MotionEvent.ACTION_MOVE: {//如果是移动操作,调度动作顺序
|
|
|
|
|
if (mDispatch) {
|
|
|
|
|
mDispatchY += (int) event.getY() - mOriginY;//移动后,触摸事件位置发生变换
|
|
|
|
|
event.setLocation(event.getX(), mDispatchY);//重新赋值
|
|
|
|
|
return mNotesListView.dispatchTouchEvent(event);//重新调度,即重新执行
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {// 其他情况
|
|
|
|
|
if (mDispatch) {
|
|
|
|
|
event.setLocation(event.getX(), mDispatchY);
|
|
|
|
|
mDispatch = false;
|
|
|
|
|
return mNotesListView.dispatchTouchEvent(event);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private void startAsyncNotesListQuery() {//同步便签列表请求
|
|
|
|
|
String selection = (mCurrentFolderId == Notes.ID_ROOT_FOLDER) ? ROOT_FOLDER_SELECTION//如果当前文件id与保存在文件夹的id相同,selection为文件夹模式,否则为常规模式
|
|
|
|
|
: NORMAL_SELECTION;
|
|
|
|
|
mBackgroundQueryHandler.startQuery(FOLDER_NOTE_LIST_QUERY_TOKEN, null,//后台异步对数据库进行操作,加快数据处理速度
|
|
|
|
|
Notes.CONTENT_NOTE_URI, NoteItemData.PROJECTION, selection, new String[] {
|
|
|
|
|
String.valueOf(mCurrentFolderId)
|
|
|
|
|
}, NoteColumns.TYPE + " DESC," + NoteColumns.MODIFIED_DATE + " DESC");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final class BackgroundQueryHandler extends AsyncQueryHandler {//背景请求处理器
|
|
|
|
|
public BackgroundQueryHandler(ContentResolver contentResolver) {
|
|
|
|
|
super(contentResolver);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onQueryComplete(int token, Object cookie, Cursor cursor) {//异步查询框架AsyncQueryHandler,当单查询完毕后,会调用onQueryComplete(token, cookie, cursor)通知查询完毕,并且传回cursor
|
|
|
|
|
switch (token) {
|
|
|
|
|
case FOLDER_NOTE_LIST_QUERY_TOKEN://便签列表请求
|
|
|
|
|
mNotesListAdapter.changeCursor(cursor);//如果是便签查询被采用,更改光标位置
|
|
|
|
|
break;
|
|
|
|
|
case FOLDER_LIST_QUERY_TOKEN://文件夹请求
|
|
|
|
|
if (cursor != null && cursor.getCount() > 0) {//如果下面有便签,打开文件夹列表弹窗
|
|
|
|
|
showFolderListMenu(cursor);//显示文件夹目录
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Query folder failed");//写入错误异常日志
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showFolderListMenu(Cursor cursor) {//批量删除
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this);//语句:声明一个警告对话框
|
|
|
|
|
builder.setTitle(R.string.menu_title_select_folder);//对话框的title是“选择文件夹”
|
|
|
|
|
final FoldersListAdapter adapter = new FoldersListAdapter(this, cursor);//文件夹列表配适器
|
|
|
|
|
builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
|
|
|
|
|
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {//代码块:为对话框设置监听事件
|
|
|
|
|
DataUtils.batchMoveToFolder(mContentResolver,//移动到文件夹下
|
|
|
|
|
mNotesListAdapter.getSelectedItemIds(), adapter.getItemId(which));
|
|
|
|
|
Toast.makeText(
|
|
|
|
|
NotesListActivity.this,
|
|
|
|
|
getString(R.string.format_move_notes_to_folder,
|
|
|
|
|
mNotesListAdapter.getSelectedCount(),
|
|
|
|
|
adapter.getFolderName(NotesListActivity.this, which)),
|
|
|
|
|
Toast.LENGTH_SHORT).show();
|
|
|
|
|
mModeCallBack.finishActionMode();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
builder.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createNewNote() {//创建新便签
|
|
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);//语句:新建一个意图,与NoteEditActivity相关联
|
|
|
|
|
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);//intent的隐式调用。ACTION_INSERT_OR_EDIT选择一个新条目或插入一个新条目去编辑它。setAction即为寻找能响应这个action的activity
|
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_FOLDER_ID, mCurrentFolderId);//语句:设置键对值
|
|
|
|
|
this.startActivityForResult(intent, REQUEST_CODE_NEW_NODE);//这个活动发出请求,等待下一个活动返回数据
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void batchDelete() {//批量删除便签
|
|
|
|
|
new AsyncTask<Void, Void, HashSet<AppWidgetAttribute>>() {//功能描述: 批量删除:删除时候,会判断是否为桌面挂件
|
|
|
|
|
函数实现:调用DataUtils的batchDeleteNotes
|
|
|
|
|
protected HashSet<AppWidgetAttribute> doInBackground(Void... unused) {
|
|
|
|
|
HashSet<AppWidgetAttribute> widgets = mNotesListAdapter.getSelectedWidget();
|
|
|
|
|
if (!isSyncMode()) {//异步处理任务
|
|
|
|
|
// if not synced, delete notes directly
|
|
|
|
|
if (DataUtils.batchDeleteNotes(mContentResolver, mNotesListAdapter
|
|
|
|
|
.getSelectedItemIds())) {
|
|
|
|
|
} else {//如果不是同步模式则将笔记移到垃圾文件夹,若转移失败打印错误信息
|
|
|
|
|
Log.e(TAG, "Delete notes error, should not happens");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// in sync mode, we'll move the deleted note into the trash
|
|
|
|
|
// folder
|
|
|
|
|
if (!DataUtils.batchMoveToFolder(mContentResolver, mNotesListAdapter//同步状态先放入垃圾箱
|
|
|
|
|
.getSelectedItemIds(), Notes.ID_TRASH_FOLER)) {//同步状态先放入回收站
|
|
|
|
|
Log.e(TAG, "Move notes to trash folder error, should not happens");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return widgets;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onPostExecute(HashSet<AppWidgetAttribute> widgets) {//这是一个循环体结构,如果id不等的话,会进行更新id的操作。
|
|
|
|
|
if (widgets != null) {
|
|
|
|
|
for (AppWidgetAttribute widget : widgets) {
|
|
|
|
|
if (widget.widgetId != AppWidgetManager.INVALID_APPWIDGET_ID//此处判断是否为一个widget
|
|
|
|
|
&& widget.widgetType != Notes.TYPE_WIDGET_INVALIDE) {//更新widget信息
|
|
|
|
|
updateWidget(widget.widgetId, widget.widgetType);//更新桌面挂件
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
mModeCallBack.finishActionMode();
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void deleteFolder(long folderId) {//删除文件夹
|
|
|
|
|
if (folderId == Notes.ID_ROOT_FOLDER) {//不在列表里,输出错误信息
|
|
|
|
|
Log.e(TAG, "Wrong folder id, should not happen " + folderId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HashSet<Long> ids = new HashSet<Long>();//下面会判断删除的文件夹里是否包含与桌面挂件相关联的便签
|
|
|
|
|
ids.add(folderId);//把文件夹id加入进去
|
|
|
|
|
HashSet<AppWidgetAttribute> widgets = DataUtils.getFolderNoteWidget(mContentResolver,
|
|
|
|
|
folderId);
|
|
|
|
|
if (!isSyncMode()) {//如果不同步,直接删除
|
|
|
|
|
// if not synced, delete folder directly
|
|
|
|
|
DataUtils.batchDeleteNotes(mContentResolver, ids);
|
|
|
|
|
} else {//否则放到回收站
|
|
|
|
|
// in sync mode, we'll move the deleted folder into the trash folder
|
|
|
|
|
DataUtils.batchMoveToFolder(mContentResolver, ids, Notes.ID_TRASH_FOLER);
|
|
|
|
|
}
|
|
|
|
|
if (widgets != null) {//如果存在对应桌面挂件,那么判断挂件信息,如果挂件id有效且挂件类型有效,则更新挂件信息
|
|
|
|
|
for (AppWidgetAttribute widget : widgets) {
|
|
|
|
|
if (widget.widgetId != AppWidgetManager.INVALID_APPWIDGET_ID
|
|
|
|
|
&& widget.widgetType != Notes.TYPE_WIDGET_INVALIDE) {
|
|
|
|
|
updateWidget(widget.widgetId, widget.widgetType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void openNode(NoteItemData data) {//打开便签
|
|
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);//功能描述:打开便签,创建新的活动,并且等待返回值
|
|
|
|
|
函数实现:用intent传递数据
|
|
|
|
|
参数描述:
|
|
|
|
|
@data 要打开的便签的数据项
|
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
|
|
intent.putExtra(Intent.EXTRA_UID, data.getId());
|
|
|
|
|
this.startActivityForResult(intent, REQUEST_CODE_OPEN_NODE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void openFolder(NoteItemData data) {//打开文件夹
|
|
|
|
|
mCurrentFolderId = data.getId();//获取当前文件夹的ID
|
|
|
|
|
startAsyncNotesListQuery();//开始异步的便签列表反馈
|
|
|
|
|
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {//对状态的操作
|
|
|
|
|
mState = ListEditState.CALL_RECORD_FOLDER;//对标题栏的操作
|
|
|
|
|
mAddNewNote.setVisibility(View.GONE);//语句:将button“新建便签”置为不可见
|
|
|
|
|
} else {//不然状态设置为子文件夹
|
|
|
|
|
mState = ListEditState.SUB_FOLDER;
|
|
|
|
|
}
|
|
|
|
|
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {// 如果当前id是保存在文件夹的id,设置标题内容为文件夹名字,否则设置为文本的前部片段内容
|
|
|
|
|
mTitleBar.setText(R.string.call_record_folder_name);//title设置为call notes
|
|
|
|
|
} else {
|
|
|
|
|
mTitleBar.setText(data.getSnippet());//否则文本的前部片段内容,即文件夹名称
|
|
|
|
|
}
|
|
|
|
|
mTitleBar.setVisibility(View.VISIBLE);// 语句:将Activity的title设置为可见
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onClick(View v) {//点击时进行的响应
|
|
|
|
|
switch (v.getId()) {//得到我们所点击组件的ID号并进行判断
|
|
|
|
|
case R.id.btn_new_note://当点击的组件是btn_new_note的时候创建一个新的标签
|
|
|
|
|
createNewNote();//当点击的组件是btn_new_note的时候创建一个新的标签
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showSoftInput() {//.显示软键盘
|
|
|
|
|
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);//获得实例
|
|
|
|
|
if (inputMethodManager != null) {//对输入的响应
|
|
|
|
|
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);//使软键盘显示,第二个参数hideFlags(用来设置是否隐藏)等于0
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void hideSoftInput(View view) {//关闭键盘
|
|
|
|
|
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
|
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);//隐藏软键盘
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showCreateOrModifyFolderDialog(final boolean create) {//显示创建或者修改文件夹的对话框
|
|
|
|
|
final AlertDialog.Builder builder = new AlertDialog.Builder(this);//初始化对话框
|
|
|
|
|
View view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text, null);//加载布局文件dialog_edit_text.xml
|
|
|
|
|
final EditText etName = (EditText) view.findViewById(R.id.et_foler_name);//获得et_foler_name这个组件并将其转化为EditText类
|
|
|
|
|
showSoftInput();//显示键盘
|
|
|
|
|
if (!create) {//如果create==false将对话框标题设置为 修改文件夹名称,否则设置为新建文件夹
|
|
|
|
|
if (mFocusNoteDataItem != null) {//关注项不为空
|
|
|
|
|
etName.setText(mFocusNoteDataItem.getSnippet());//获取片段来设置文档
|
|
|
|
|
builder.setTitle(getString(R.string.menu_folder_change_name));//通过构建器设置标题
|
|
|
|
|
} else {//显示此次点击显示为空
|
|
|
|
|
Log.e(TAG, "The long click data item is null");//载入标签tag,报错
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
etName.setText("");
|
|
|
|
|
builder.setTitle(this.getString(R.string.menu_create_folder));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
builder.setPositiveButton(android.R.string.ok, null);//对话框设置确定和取消按钮
|
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {//构建器_创建取消的按键
|
|
|
|
|
hideSoftInput(etName);//隐藏输入
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final Dialog dialog = builder.setView(view).show();//语句:将上述对话框实例化并显示在屏幕上
|
|
|
|
|
final Button positive = (Button)dialog.findViewById(android.R.id.button1);//创建对话框的确认按钮
|
|
|
|
|
positive.setOnClickListener(new OnClickListener() {//设置点击监听器
|
|
|
|
|
public void onClick(View v) {//新建文件夹或者修改文件夹名同时判断其是否合法
|
|
|
|
|
hideSoftInput(etName);//隐藏键盘
|
|
|
|
|
String name = etName.getText().toString();//语句: 获取当前可编辑文本框etName的文本内容
|
|
|
|
|
if (DataUtils.checkVisibleFolderName(mContentResolver, name)) {//检测可视文件夹的名字
|
|
|
|
|
Toast.makeText(NotesListActivity.this, getString(R.string.folder_exist, name),//创建文本
|
|
|
|
|
Toast.LENGTH_LONG).show();
|
|
|
|
|
etName.setSelection(0, etName.length());//语句:全选当前字符串
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!create) {//若未创建
|
|
|
|
|
if (!TextUtils.isEmpty(name)) {//语句:如果输入不为空
|
|
|
|
|
ContentValues values = new ContentValues();//创建内容取值
|
|
|
|
|
values.put(NoteColumns.SNIPPET, name);//将片段名字添加入value
|
|
|
|
|
values.put(NoteColumns.TYPE, Notes.TYPE_FOLDER);//将文件夹类型添加入value
|
|
|
|
|
values.put(NoteColumns.LOCAL_MODIFIED, 1);//将本地修改添加入value
|
|
|
|
|
mContentResolver.update(Notes.CONTENT_NOTE_URI, values, NoteColumns.ID//将内容笔记URI及笔记专栏ID更新至内容解决器
|
|
|
|
|
+ "=?", new String[] {
|
|
|
|
|
String.valueOf(mFocusNoteDataItem.getId())
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else if (!TextUtils.isEmpty(name)) {//代码块:如果是新建文件夹操作
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.SNIPPET, name);//将SNIPPET加入内容值中
|
|
|
|
|
values.put(NoteColumns.TYPE, Notes.TYPE_FOLDER);//将TYPE加入内容值中
|
|
|
|
|
mContentResolver.insert(Notes.CONTENT_NOTE_URI, values);//将内容_笔记的URI插入到内容解决器
|
|
|
|
|
}
|
|
|
|
|
dialog.dismiss();//撤销对话框
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (TextUtils.isEmpty(etName.getText())) {//etName为空设置按键不可用
|
|
|
|
|
positive.setEnabled(false);//语句:右下的button“ok”不可用,即显示灰色不能点击
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* When the name edit text is null, disable the positive button
|
|
|
|
|
*/
|
|
|
|
|
etName.addTextChangedListener(new TextWatcher() {
|
|
|
|
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {//判断是否在文本更改之前
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onTextChanged(CharSequence s, int start, int before, int count) {//当前文本改变触发,文本为空按键不可用,不为空则可用
|
|
|
|
|
if (TextUtils.isEmpty(etName.getText())) {//判断:如果文件夹名称为空,那么便不可用
|
|
|
|
|
positive.setEnabled(false);
|
|
|
|
|
} else {
|
|
|
|
|
positive.setEnabled(true);//语句:监听输入字符串,如果大于零,则button可以点击
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void afterTextChanged(Editable s) {//.判断是否在文本更改之后
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onBackPressed() {//点击后退键时的操作
|
|
|
|
|
switch (mState) {//判断目前所处的状态
|
|
|
|
|
case SUB_FOLDER://前两个状态返回到初始界面最后一个状态返回到桌面
|
|
|
|
|
mCurrentFolderId = Notes.ID_ROOT_FOLDER;//语句:如果是在文件夹中的状态
|
|
|
|
|
mState = ListEditState.NOTE_LIST;//语句:将当前文件夹id修改为根文件夹
|
|
|
|
|
startAsyncNotesListQuery();//语句:将当前状态改为一般状态
|
|
|
|
|
mTitleBar.setVisibility(View.GONE);//语句:隐藏TitleBar,设置为不可见
|
|
|
|
|
break;
|
|
|
|
|
case CALL_RECORD_FOLDER://响应已记录文件夹的操作
|
|
|
|
|
mCurrentFolderId = Notes.ID_ROOT_FOLDER;//语句:将当前文件夹id修改为根文件夹
|
|
|
|
|
mState = ListEditState.NOTE_LIST;//便签列表的返回
|
|
|
|
|
mAddNewNote.setVisibility(View.VISIBLE);//语句:设置button“新增便签”可见
|
|
|
|
|
mTitleBar.setVisibility(View.GONE);//语句:隐藏TitleBar,设置为不可见
|
|
|
|
|
startAsyncNotesListQuery();
|
|
|
|
|
break;
|
|
|
|
|
case NOTE_LIST://从主界面退出
|
|
|
|
|
super.onBackPressed();//语句:调用原来的onBackPressed方法
|
|
|
|
|
break;
|
|
|
|
|
default://其他情况直接break
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateWidget(int appWidgetId, int appWidgetType) {//更新窗口插件
|
|
|
|
|
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);//语句:初始化部件大小管理
|
|
|
|
|
if (appWidgetType == Notes.TYPE_WIDGET_2X) {//下面是判断不同类型widget的操作并建立不同的类型
|
|
|
|
|
intent.setClass(this, NoteWidgetProvider_2x.class);//将便签插件提供器的类型设置成内容的类型
|
|
|
|
|
} else if (appWidgetType == Notes.TYPE_WIDGET_4X) {//若插件形式是_4X
|
|
|
|
|
intent.setClass(this, NoteWidgetProvider_4x.class);
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Unspported widget type");//否则显示“不支持的插件类型
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {// 语句:putExtra中两个参数为键对值,第一个参数为键名
|
|
|
|
|
appWidgetId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
sendBroadcast(intent);//设置成功的结果
|
|
|
|
|
setResult(RESULT_OK, intent);//返回给上一个活动数据
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final OnCreateContextMenuListener mFolderOnCreateContextMenuListener = new OnCreateContextMenuListener() {
|
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {//对文件夹进行操作的菜单
|
|
|
|
|
if (mFocusNoteDataItem != null) {//当笔记数据项不为空时,设置菜单
|
|
|
|
|
menu.setHeaderTitle(mFocusNoteDataItem.getSnippet());//语句:设置菜单的题目是当前文件夹名字
|
|
|
|
|
menu.add(0, MENU_FOLDER_VIEW, 0, R.string.menu_folder_view);//语句:在菜单中新增项目“查看文件夹”
|
|
|
|
|
menu.add(0, MENU_FOLDER_DELETE, 0, R.string.menu_folder_delete);//语句:在菜单中新增项目“删除文件夹”
|
|
|
|
|
menu.add(0, MENU_FOLDER_CHANGE_NAME, 0, R.string.menu_folder_change_name);//语句:在菜单中新增项目“修改文件夹名称”
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onContextMenuClosed(Menu menu) {//关闭菜单
|
|
|
|
|
if (mNotesListView != null) {
|
|
|
|
|
mNotesListView.setOnCreateContextMenuListener(null);//语句:不设置监听事件
|
|
|
|
|
}
|
|
|
|
|
super.onContextMenuClosed(menu);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onContextItemSelected(MenuItem item) {//对菜单项进行选择对应的相应
|
|
|
|
|
if (mFocusNoteDataItem == null) {//若笔记数据项为空
|
|
|
|
|
Log.e(TAG, "The long click data item is null");//设置标签“长按数据项为空
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
switch (item.getItemId()) {//通过获取项目ID,进行switch
|
|
|
|
|
case MENU_FOLDER_VIEW://打开文件夹
|
|
|
|
|
openFolder(mFocusNoteDataItem);
|
|
|
|
|
break;
|
|
|
|
|
case MENU_FOLDER_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_folder));// 语句:设置警告信息文本“确认删除文件夹及所包含的便签”
|
|
|
|
|
builder.setPositiveButton(android.R.string.ok,//设置取消按键
|
|
|
|
|
new DialogInterface.OnClickListener() {
|
|
|
|
|
public void onClick(DialogInterface dialog, int which) {
|
|
|
|
|
deleteFolder(mFocusNoteDataItem.getId());
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
builder.setNegativeButton(android.R.string.cancel, null);
|
|
|
|
|
builder.show();
|
|
|
|
|
break;
|
|
|
|
|
case MENU_FOLDER_CHANGE_NAME://文件夹改名
|
|
|
|
|
showCreateOrModifyFolderDialog(false);//不显示创建or修改文件夹的对话框
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {//创建菜单这个方法在每一次调用菜单的时候都会执行
|
|
|
|
|
menu.clear();//菜单清空
|
|
|
|
|
if (mState == ListEditState.NOTE_LIST) {//当状态为当前笔记列表状态时,设置同步或取消同步
|
|
|
|
|
getMenuInflater().inflate(R.menu.note_list, menu);//加载目录的布局
|
|
|
|
|
// set sync or sync_cancel
|
|
|
|
|
menu.findItem(R.id.menu_sync).setTitle(
|
|
|
|
|
GTaskSyncService.isSyncing() ? R.string.menu_sync_cancel : R.string.menu_sync);//语句:为同步菜单项设置标题,如果正在同步中则显示“取消同步”,否则显示“同步”
|
|
|
|
|
} else if (mState == ListEditState.SUB_FOLDER) {//当状态为当前子文件夹状态时,扩充子文件夹
|
|
|
|
|
getMenuInflater().inflate(R.menu.sub_folder, menu);//语句:采用布局文件R.menu.sub_folder来构建菜单项
|
|
|
|
|
} else if (mState == ListEditState.CALL_RECORD_FOLDER) {//当状态为当调用记录文件夹时,扩充记录文件夹的菜单
|
|
|
|
|
getMenuInflater().inflate(R.menu.call_record_folder, menu);//语句:采用布局文件R.menu.call_record_folder来构建菜单项
|
|
|
|
|
} else {//否则设置标签为“错误状态”+当前状态
|
|
|
|
|
Log.e(TAG, "Wrong state:" + mState);//语句:报错日志信息
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {//当主界面中的菜单项被选中时进行的工作 (case R.id.menu_new_note)这个选项没有在菜单中显示出来
|
|
|
|
|
switch (item.getItemId()) {//选择不同的项目名称
|
|
|
|
|
case R.id.menu_new_folder: {//.新建文件夹
|
|
|
|
|
showCreateOrModifyFolderDialog(true);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case R.id.menu_export_text: {//输出文本
|
|
|
|
|
exportNoteToText();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case R.id.menu_sync: {//同步菜单
|
|
|
|
|
if (isSyncMode()) {//同步
|
|
|
|
|
if (TextUtils.equals(item.getTitle(), getString(R.string.menu_sync))) {//如果项目title与菜单同步相同,则进行同步
|
|
|
|
|
GTaskSyncService.startSync(this);//利用到GTaskSyncService的类
|
|
|
|
|
} else {//否则取消同步
|
|
|
|
|
GTaskSyncService.cancelSync(this);
|
|
|
|
|
}
|
|
|
|
|
} else {//如果不是同步模式,则开始设置动作
|
|
|
|
|
startPreferenceActivity();//否则进行Preference活动
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case R.id.menu_setting: {//设置菜单
|
|
|
|
|
startPreferenceActivity();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case R.id.menu_new_note: {//新建便签
|
|
|
|
|
createNewNote();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case R.id.menu_search://搜索
|
|
|
|
|
onSearchRequested();//查询
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onSearchRequested() {//查找请求的响应,好像没有实现?
|
|
|
|
|
startSearch(null, false, null /* appData */, false);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void exportNoteToText() {//将便签导出
|
|
|
|
|
final BackupUtils backup = BackupUtils.getInstance(NotesListActivity.this);//备份笔记信息
|
|
|
|
|
new AsyncTask<Void, Void, Integer>() {//异步任务
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected Integer doInBackground(Void... unused) {
|
|
|
|
|
return backup.exportToText();
|
|
|
|
|
}//未被占用的话后台进行
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onPostExecute(Integer result) {//设置备份的结果响应
|
|
|
|
|
if (result == BackupUtils.STATE_SD_CARD_UNMOUONTED) {//根据结果为sd卡未装载、成功、系统错误三种进行处理,均是设置对话框、标题、信息以及确认按钮状态
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this);
|
|
|
|
|
builder.setTitle(NotesListActivity.this
|
|
|
|
|
.getString(R.string.failed_sdcard_export));
|
|
|
|
|
builder.setMessage(NotesListActivity.this
|
|
|
|
|
.getString(R.string.error_sdcard_unmounted));
|
|
|
|
|
builder.setPositiveButton(android.R.string.ok, null);
|
|
|
|
|
builder.show();
|
|
|
|
|
} else if (result == BackupUtils.STATE_SUCCESS) {
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this);
|
|
|
|
|
builder.setTitle(NotesListActivity.this
|
|
|
|
|
.getString(R.string.success_sdcard_export));//导出文本
|
|
|
|
|
builder.setMessage(NotesListActivity.this.getString(
|
|
|
|
|
R.string.format_exported_file_location, backup
|
|
|
|
|
.getExportedTextFileName(), backup.getExportedTextFileDir()));
|
|
|
|
|
builder.setPositiveButton(android.R.string.ok, null);
|
|
|
|
|
builder.show();
|
|
|
|
|
} else if (result == BackupUtils.STATE_SYSTEM_ERROR) {// 如果系统错误,则显示 导出失败,请检查SD卡
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this);
|
|
|
|
|
builder.setTitle(NotesListActivity.this//设置标题
|
|
|
|
|
.getString(R.string.failed_sdcard_export));
|
|
|
|
|
builder.setMessage(NotesListActivity.this
|
|
|
|
|
.getString(R.string.error_sdcard_export));
|
|
|
|
|
builder.setPositiveButton(android.R.string.ok, null);//设置响应按钮
|
|
|
|
|
builder.show();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isSyncMode() {//判断同步
|
|
|
|
|
return NotesPreferenceActivity.getSyncAccountName(this).trim().length() > 0;//转到PreferenceActivity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startPreferenceActivity() {//跳转到设置界面
|
|
|
|
|
Activity from = getParent() != null ? getParent() : this;//设置便签函数
|
|
|
|
|
Intent intent = new Intent(from, NotesPreferenceActivity.class);
|
|
|
|
|
from.startActivityIfNeeded(intent, -1);//请求码为-1,表示此活动结束后不会通知原活动
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class OnListItemClickListener implements OnItemClickListener {//当短按标签列表项时的响应
|
|
|
|
|
|
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {//项目被点击的响应
|
|
|
|
|
if (view instanceof NotesListItem) {//判断view是否是NotesListItem的一个实例,如果是就获取他的项目信息装入item中
|
|
|
|
|
NoteItemData item = ((NotesListItem) view).getItemData();//判断view是否是NotesListItem的一个实例 是就获取他的项目信息装入item中
|
|
|
|
|
if (mNotesListAdapter.isInChoiceMode()) {//如果列表适配器被选择并且项是便签类型的,则修改位置和状态信息
|
|
|
|
|
if (item.getType() == Notes.TYPE_NOTE) {//如果点到的item是便签
|
|
|
|
|
position = position - mNotesListView.getHeaderViewsCount();//减去头部视图的元素项,得到列表的元素索引值
|
|
|
|
|
mModeCallBack.onItemCheckedStateChanged(null, position, id,//改变对应索引的Item是否被选中的状态
|
|
|
|
|
!mNotesListAdapter.isSelectedItem(position));
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (mState) {//区别情况进行处理
|
|
|
|
|
case NOTE_LIST://便签列表
|
|
|
|
|
if (item.getType() == Notes.TYPE_FOLDER
|
|
|
|
|
|| item.getType() == Notes.TYPE_SYSTEM) {
|
|
|
|
|
openFolder(item);//查询目标文件夹
|
|
|
|
|
} else if (item.getType() == Notes.TYPE_NOTE) {
|
|
|
|
|
openNode(item);//语句:如果点击的是便签类型,则执行打开便签
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Wrong note type in NOTE_LIST");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case SUB_FOLDER:
|
|
|
|
|
case CALL_RECORD_FOLDER:
|
|
|
|
|
if (item.getType() == Notes.TYPE_NOTE) {
|
|
|
|
|
openNode(item);
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Wrong note type in SUB_FOLDER");
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startQueryDestinationFolders() {//查询目标文件
|
|
|
|
|
String selection = NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>? AND " + NoteColumns.ID + "<>?";
|
|
|
|
|
selection = (mState == ListEditState.NOTE_LIST) ? selection:
|
|
|
|
|
"(" + selection + ") OR (" + NoteColumns.ID + "=" + Notes.ID_ROOT_FOLDER + ")";
|
|
|
|
|
|
|
|
|
|
mBackgroundQueryHandler.startQuery(FOLDER_LIST_QUERY_TOKEN,
|
|
|
|
|
null,
|
|
|
|
|
Notes.CONTENT_NOTE_URI,//长按某一项时进行的操作 如果长按的是便签,则通过ActionMode菜单实现;如果长按的是文件夹,则通过ContextMenu菜单实现;
|
|
|
|
|
FoldersListAdapter.PROJECTION,
|
|
|
|
|
selection,//长按某一项时进行的操作 如果长按的是便签,则通过ActionMode菜单实现;如果长按的是文件夹,则通过ContextMenu菜单实现;
|
|
|
|
|
new String[] {//新建字符列表
|
|
|
|
|
String.valueOf(Notes.TYPE_FOLDER),
|
|
|
|
|
String.valueOf(Notes.ID_TRASH_FOLER),
|
|
|
|
|
String.valueOf(mCurrentFolderId)
|
|
|
|
|
},
|
|
|
|
|
NoteColumns.MODIFIED_DATE + " DESC");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {//长按的响应
|
|
|
|
|
if (view instanceof NotesListItem) {//判断view是否是NotesListItem的一个实例,如果是就获取他的项目信息装入item中
|
|
|
|
|
mFocusNoteDataItem = ((NotesListItem) view).getItemData();//聚焦的Item对象
|
|
|
|
|
if (mFocusNoteDataItem.getType() == Notes.TYPE_NOTE && !mNotesListAdapter.isInChoiceMode()) {//长按的对象是便签时的处理,通过ActionMode实现
|
|
|
|
|
if (mNotesListView.startActionMode(mModeCallBack) != null) {
|
|
|
|
|
mModeCallBack.onItemCheckedStateChanged(null, position, id, true);//开始对单个便签进行操作
|
|
|
|
|
mNotesListView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);//语句:执行长按动作触发震动,反馈机制
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "startActionMode fails");
|
|
|
|
|
}
|
|
|
|
|
} else if (mFocusNoteDataItem.getType() == Notes.TYPE_FOLDER) {//语句:如果长按的的项目是文件夹类型,则执行ContextMenu菜单的实现
|
|
|
|
|
mNotesListView.setOnCreateContextMenuListener(mFolderOnCreateContextMenuListener);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|