|
|
|
@ -13,9 +13,7 @@
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.example.notes_master.src.net.micode.notes.ui;
|
|
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
|
import android.app.AlertDialog;
|
|
|
|
|
import android.app.Dialog;
|
|
|
|
@ -80,78 +78,49 @@ import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
import java.io.InputStreamReader;
|
|
|
|
|
import java.util.HashSet;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//主界面
|
|
|
|
|
public class NotesListActivity extends AppCompatActivity implements OnClickListener, OnItemLongClickListener {
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
private static final int MENU_FOLDER_VIEW = 1;
|
|
|
|
|
|
|
|
|
|
private static final int MENU_FOLDER_CHANGE_NAME = 2;
|
|
|
|
|
|
|
|
|
|
private static final int MENU_FOLDER_DELETE = 0;//删除文件夹
|
|
|
|
|
private static final int MENU_FOLDER_VIEW = 1;//查看文件夹
|
|
|
|
|
private static final int MENU_FOLDER_CHANGE_NAME = 2;//修改文件夹名字
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
private int mDispatchY;
|
|
|
|
|
|
|
|
|
|
private TextView mTitleBar;
|
|
|
|
|
|
|
|
|
|
private long mCurrentFolderId;
|
|
|
|
|
|
|
|
|
|
private ContentResolver mContentResolver;
|
|
|
|
|
|
|
|
|
|
private ModeCallback mModeCallBack;
|
|
|
|
|
|
|
|
|
|
private static final String TAG = "NotesListActivity";
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
|
@ -161,14 +130,16 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
super.onActivityResult(requestCode, resultCode, data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//当用户第一次使用小米便签时,把默认的欢迎便签保存到界面上
|
|
|
|
|
private void setAppInfoFromRawRes() {
|
|
|
|
|
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
|
if (!sp.getBoolean(PREFERENCE_ADD_INTRODUCTION, false)) {
|
|
|
|
|
if (!sp.getBoolean(PREFERENCE_ADD_INTRODUCTION, false)) {//"net.micode.notes.introduction"
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
InputStream in = null;
|
|
|
|
|
// 把资源文件放到应用程序的/raw/raw下,那么就可以在应用中使用getResources获取资源后,
|
|
|
|
|
// 以openRawResource方法(不带后缀的资源文件名)打开这个文件。
|
|
|
|
|
try {
|
|
|
|
|
in = getResources().openRawResource(R.raw.introduction);
|
|
|
|
|
in = getResources().openRawResource(R.raw.introduction);//该文件内容:Welcome to use MIUI notes!
|
|
|
|
|
if (in != null) {
|
|
|
|
|
InputStreamReader isr = new InputStreamReader(in);
|
|
|
|
|
BufferedReader br = new BufferedReader(isr);
|
|
|
|
@ -194,12 +165,13 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建空的WorkingNote
|
|
|
|
|
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()) {
|
|
|
|
|
// 更新保存note的信息
|
|
|
|
|
sp.edit().putBoolean(PREFERENCE_ADD_INTRODUCTION, true).commit();
|
|
|
|
|
} else {
|
|
|
|
|
Log.e(TAG, "Save introduction note error");
|
|
|
|
@ -207,18 +179,17 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onStart() {
|
|
|
|
|
super.onStart();
|
|
|
|
|
startAsyncNotesListQuery();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化资源
|
|
|
|
|
private void initResources() {
|
|
|
|
|
mContentResolver = this.getContentResolver();
|
|
|
|
|
mContentResolver = this.getContentResolver();// 获取应用程序的数据,得到类似数据表的东西
|
|
|
|
|
mBackgroundQueryHandler = new BackgroundQueryHandler(this.getContentResolver());
|
|
|
|
|
mCurrentFolderId = Notes.ID_ROOT_FOLDER;//0
|
|
|
|
|
mNotesListView = (ListView) findViewById(R.id.notes_list);
|
|
|
|
|
mNotesListView = (ListView) findViewById(R.id.notes_list);// 绑定XML中的ListView,作为Item的容器
|
|
|
|
|
mNotesListView.addFooterView(LayoutInflater.from(this).inflate(R.layout.note_list_footer, null),
|
|
|
|
|
null, false);
|
|
|
|
|
mNotesListView.setOnItemClickListener(new OnListItemClickListener());//单击监听器
|
|
|
|
@ -236,16 +207,27 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
mModeCallBack = new ModeCallback();
|
|
|
|
|
}
|
|
|
|
|
//内部类
|
|
|
|
|
/**
|
|
|
|
|
* 私有类`ModeCallback`实现了`ListView.MultiChoiceModeListener`和`OnMenuItemClickListener`接口,
|
|
|
|
|
* 它用于处理多选模式下的操作和菜单项点击事件。
|
|
|
|
|
*/
|
|
|
|
|
private class ModeCallback implements ListView.MultiChoiceModeListener, OnMenuItemClickListener {
|
|
|
|
|
private DropdownMenu mDropDownMenu;
|
|
|
|
|
private ActionMode mActionMode;
|
|
|
|
|
private MenuItem mMoveMenu;
|
|
|
|
|
|
|
|
|
|
private DropdownMenu mDropDownMenu; // 下拉菜单对象
|
|
|
|
|
private ActionMode mActionMode; // 操作模式对象
|
|
|
|
|
private MenuItem mMoveMenu; // 移动菜单项对象
|
|
|
|
|
/**
|
|
|
|
|
* 当操作模式创建时调用。
|
|
|
|
|
* @param mode 提供操作模式的ActionMode对象
|
|
|
|
|
* @param menu 操作模式的菜单
|
|
|
|
|
* @return 返回true表示操作模式已创建
|
|
|
|
|
*/
|
|
|
|
|
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
|
|
|
|
|
getMenuInflater().inflate(R.menu.note_list_options, menu);//通过调用 getMenuInflater().inflate() 方法加载菜单布局文件 R.menu.note_list_options,
|
|
|
|
|
//通过调用 getMenuInflater().inflate() 方法加载菜单布局文件 R.menu.note_list_options,
|
|
|
|
|
// 并将菜单项添加到 menu 中。
|
|
|
|
|
menu.findItem(R.id.delete).setOnMenuItemClickListener(this);
|
|
|
|
|
mMoveMenu = menu.findItem(R.id.move);
|
|
|
|
|
getMenuInflater().inflate(R.menu.note_list_options, menu); // 加载菜单布局文件
|
|
|
|
|
menu.findItem(R.id.delete).setOnMenuItemClickListener(this); // 设置删除菜单项的点击监听器
|
|
|
|
|
mMoveMenu = menu.findItem(R.id.move); // 获取移动菜单项
|
|
|
|
|
// 根据条件设置移动菜单项的可见性和点击监听器
|
|
|
|
|
if (mFocusNoteDataItem.getParentId() == Notes.ID_CALL_RECORD_FOLDER
|
|
|
|
|
|| DataUtils.getUserFolderCount(mContentResolver) == 0) {
|
|
|
|
|
mMoveMenu.setVisible(false);
|
|
|
|
@ -254,33 +236,32 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
mMoveMenu.setOnMenuItemClickListener(this);
|
|
|
|
|
}
|
|
|
|
|
mActionMode = mode;
|
|
|
|
|
mNotesListAdapter.setChoiceMode(true);//设置列表适配器的选择模式为 true,以启用多选模式。
|
|
|
|
|
mNotesListView.setLongClickable(false);//将列表视图的长按点击功能禁用。
|
|
|
|
|
mAddNewNote.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
|
|
View customView = LayoutInflater.from(NotesListActivity.this).inflate(
|
|
|
|
|
R.layout.note_list_dropdown_menu, null);
|
|
|
|
|
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);
|
|
|
|
|
mDropDownMenu.setOnDropdownMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
|
|
|
|
|
mDropDownMenu.setOnDropdownMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
|
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
|
mNotesListAdapter.selectAll(!mNotesListAdapter.isAllSelected());
|
|
|
|
|
updateMenu();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 用于更新菜单项和下拉菜单。
|
|
|
|
|
* 更新菜单项和下拉菜单。
|
|
|
|
|
*/
|
|
|
|
|
private void updateMenu() {
|
|
|
|
|
int selectedCount = mNotesListAdapter.getSelectedCount();//多选时选的项数
|
|
|
|
|
// Update dropdown menu
|
|
|
|
|
int selectedCount = mNotesListAdapter.getSelectedCount(); // 获取选中的项数
|
|
|
|
|
// 更新下拉菜单标题
|
|
|
|
|
String format = getResources().getString(R.string.menu_select_title, selectedCount);//第二个参数是换第一个参数的占位符的
|
|
|
|
|
mDropDownMenu.setTitle(format);
|
|
|
|
|
MenuItem item = mDropDownMenu.findItem(R.id.action_select_all);
|
|
|
|
@ -294,49 +275,64 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当操作模式准备就绪时调用。
|
|
|
|
|
* @param mode 提供操作模式的ActionMode对象
|
|
|
|
|
* @param menu 操作模式的菜单
|
|
|
|
|
* @return 返回true表示操作模式已准备就绪
|
|
|
|
|
*/
|
|
|
|
|
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
// TODO: 添加准备操作模式的逻辑(如果有的话)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当操作模式的菜单项被点击时调用。
|
|
|
|
|
* @param mode 提供操作模式的ActionMode对象
|
|
|
|
|
* @param item 被点击的菜单项
|
|
|
|
|
* @return 返回true表示菜单项点击事件已处理
|
|
|
|
|
*/
|
|
|
|
|
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
// TODO: 处理菜单项的点击事件
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当操作模式结束时调用的方法。
|
|
|
|
|
* @param mode 提供操作模式的ActionMode对象
|
|
|
|
|
*/
|
|
|
|
|
public void onDestroyActionMode(ActionMode mode) {
|
|
|
|
|
mNotesListAdapter.setChoiceMode(false);
|
|
|
|
|
mNotesListView.setLongClickable(true);
|
|
|
|
|
mAddNewNote.setVisibility(View.VISIBLE);
|
|
|
|
|
mNotesListAdapter.setChoiceMode(false); // 取消多选模式
|
|
|
|
|
mNotesListView.setLongClickable(true); // 启用列表视图的长按点击功能
|
|
|
|
|
mAddNewNote.setVisibility(View.VISIBLE); // 设置添加新笔记按钮可见
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 结束操作模式,退出多选模式。
|
|
|
|
|
*/
|
|
|
|
|
public void finishActionMode() {
|
|
|
|
|
mActionMode.finish();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 被勾选了,或取消勾选了,就改变hashmap里的该项的value的boolean值
|
|
|
|
|
* @param mode The {@link ActionMode} providing the selection mode
|
|
|
|
|
* @param position Adapter position of the item that was checked or unchecked
|
|
|
|
|
* @param id Adapter ID of the item that was checked or unchecked
|
|
|
|
|
* @param checked <code>true</code> if the item is now checked, <code>false</code>
|
|
|
|
|
* if the item is now unchecked.
|
|
|
|
|
* 当列表项的选择状态改变时调用,更新HashMap中相应项的值。
|
|
|
|
|
* @param mode 提供选择模式的ActionMode对象
|
|
|
|
|
* @param position 被选中或取消选中的项的适配器位置
|
|
|
|
|
* @param id 被选中或取消选中的项的适配器ID
|
|
|
|
|
* @param checked true表示项现在被选中,false表示项现在取消选中
|
|
|
|
|
*/
|
|
|
|
|
public void onItemCheckedStateChanged(ActionMode mode, int position, long id,
|
|
|
|
|
boolean checked) {
|
|
|
|
|
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
|
|
|
|
|
mNotesListAdapter.setCheckedItem(position, checked);
|
|
|
|
|
updateMenu();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当菜单项被点击时调用。
|
|
|
|
|
* @param item 被点击的菜单项
|
|
|
|
|
* @return 返回true表示菜单项点击事件已处理
|
|
|
|
|
*/
|
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
|
if (mNotesListAdapter.getSelectedCount() == 0) {
|
|
|
|
|
Toast.makeText(NotesListActivity.this, getString(R.string.menu_select_none),
|
|
|
|
|
Toast.LENGTH_SHORT).show();//没有选中项,操作无效
|
|
|
|
|
Toast.LENGTH_SHORT).show(); // 如果没有选中项,显示提示消息
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (item.getItemId()) {//选中的下拉菜单中的菜单项
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
|
case R.id.delete:
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this);
|
|
|
|
|
builder.setTitle(getString(R.string.alert_title_delete));
|
|
|
|
@ -362,12 +358,12 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class NewNoteOnTouchListener implements OnTouchListener {
|
|
|
|
|
|
|
|
|
|
public boolean onTouch(View v, MotionEvent event) {
|
|
|
|
|
switch (event.getAction()) {
|
|
|
|
|
case MotionEvent.ACTION_DOWN: {
|
|
|
|
|
// 获取屏幕显示
|
|
|
|
|
Display display = getWindowManager().getDefaultDisplay();
|
|
|
|
|
int screenHeight = display.getHeight();
|
|
|
|
|
int newNoteViewHeight = mAddNewNote.getHeight();
|
|
|
|
@ -376,6 +372,7 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
/**
|
|
|
|
|
* Minus TitleBar's height
|
|
|
|
|
*/
|
|
|
|
|
// 如果当前状态是ListEditState.SUB_FOLDER,则减去标题栏的高度
|
|
|
|
|
if (mState == ListEditState.SUB_FOLDER) {
|
|
|
|
|
eventY -= mTitleBar.getHeight();
|
|
|
|
|
start -= mTitleBar.getHeight();
|
|
|
|
@ -389,6 +386,7 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
// 如果点击的是"New Note"按钮的透明部分(根据特定的公式判断),则将事件分发给按钮后面的列表视图
|
|
|
|
|
if (event.getY() < (event.getX() * (-0.12) + 94)) {
|
|
|
|
|
View view = mNotesListView.getChildAt(mNotesListView.getChildCount() - 1
|
|
|
|
|
- mNotesListView.getFooterViewsCount());
|
|
|
|
@ -404,6 +402,7 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case MotionEvent.ACTION_MOVE: {
|
|
|
|
|
// 如果之前已经进行了事件分发,则根据手指移动的距离更新事件的位置,并将事件继续分发给列表视图
|
|
|
|
|
if (mDispatch) {
|
|
|
|
|
mDispatchY += (int) event.getY() - mOriginY;
|
|
|
|
|
event.setLocation(event.getX(), mDispatchY);
|
|
|
|
@ -412,6 +411,7 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
// 如果之前已经进行了事件分发,则将事件的位置更新,并停止事件的分发
|
|
|
|
|
if (mDispatch) {
|
|
|
|
|
event.setLocation(event.getX(), mDispatchY);
|
|
|
|
|
mDispatch = false;
|
|
|
|
@ -422,18 +422,22 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
private void startAsyncNotesListQuery() {
|
|
|
|
|
String selection = (mCurrentFolderId == Notes.ID_ROOT_FOLDER) ? ROOT_FOLDER_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");
|
|
|
|
|
// 检查当前文件夹ID是否为根文件夹ID,确定查询的条件
|
|
|
|
|
String selection = (mCurrentFolderId == Notes.ID_ROOT_FOLDER) ? ROOT_FOLDER_SELECTION : NORMAL_SELECTION;
|
|
|
|
|
// 使用BackgroundQueryHandler开始异步查询
|
|
|
|
|
// 参数1:查询令牌,用于标识查询的唯一性
|
|
|
|
|
// 参数2:对象,用于传递额外的数据给查询结果的回调方法
|
|
|
|
|
// 参数3:查询的URI,指定要查询的数据表的内容URI
|
|
|
|
|
// 参数4:查询的投影,指定要查询的列
|
|
|
|
|
// 参数5:查询的选择条件
|
|
|
|
|
// 参数6:选择条件中的占位符的值
|
|
|
|
|
// 参数7:查询的排序方式
|
|
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
自定义异步处理查询类,用于在后台执行查询操作并处理查询结果
|
|
|
|
|
这个类是为了避免在主线程中执行耗时的数据库查询或与内容提供者交互的操作,以确保应用程序的界面响应性能。
|
|
|
|
@ -442,7 +446,6 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
public BackgroundQueryHandler(ContentResolver contentResolver) {
|
|
|
|
|
super(contentResolver);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* token 参数表示查询操作的标识符,用于区分不同的查询操作。
|
|
|
|
|
* cookie 参数是一个可选的对象,用于在发起查询时传递额外的数据,在查询完成后原样返回,可以用于标识查询操作的上下文或其他需要的信息。
|
|
|
|
@ -470,7 +473,6 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showFolderListMenu(Cursor cursor) {
|
|
|
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.this);
|
|
|
|
|
builder.setTitle(R.string.menu_title_select_folder);//选择文件夹
|
|
|
|
@ -491,38 +493,39 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
});
|
|
|
|
|
builder.show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void createNewNote() {
|
|
|
|
|
// 创建一个Intent对象,指定目标为NoteEditActivity,并设置操作为ACTION_INSERT_OR_EDIT
|
|
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
|
|
|
|
intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
|
|
|
|
|
// 将当前文件夹ID作为额外的INTENT_EXTRA_FOLDER_ID数据传递给Intent
|
|
|
|
|
intent.putExtra(Notes.INTENT_EXTRA_FOLDER_ID, mCurrentFolderId);
|
|
|
|
|
// 启动NoteEditActivity,并获取结果
|
|
|
|
|
this.startActivityForResult(intent, REQUEST_CODE_NEW_NODE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void batchDelete() {
|
|
|
|
|
// 创建一个异步任务
|
|
|
|
|
new AsyncTask<Void, Void, HashSet<AppWidgetAttribute>>() {
|
|
|
|
|
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())) {
|
|
|
|
|
// 如果不处于同步模式,直接删除选中的笔记
|
|
|
|
|
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)) {
|
|
|
|
|
// 如果处于同步模式,将选中的笔记移动到回收站文件夹
|
|
|
|
|
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) {
|
|
|
|
|
// 更新与选中笔记关联的小部件
|
|
|
|
|
if (widgets != null) {
|
|
|
|
|
for (AppWidgetAttribute widget : widgets) {
|
|
|
|
|
if (widget.widgetId != AppWidgetManager.INVALID_APPWIDGET_ID
|
|
|
|
@ -531,28 +534,30 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 完成操作模式,退出多选模式
|
|
|
|
|
mModeCallBack.finishActionMode();
|
|
|
|
|
}
|
|
|
|
|
}.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void deleteFolder(long folderId) {
|
|
|
|
|
// 检查文件夹ID是否为根文件夹的ID,如果是,则记录错误日志并返回
|
|
|
|
|
if (folderId == Notes.ID_ROOT_FOLDER) {
|
|
|
|
|
Log.e(TAG, "Wrong folder id, should not happen " + folderId);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建HashSet来存储要删除的文件夹ID
|
|
|
|
|
HashSet<Long> ids = new HashSet<Long>();
|
|
|
|
|
ids.add(folderId);
|
|
|
|
|
HashSet<AppWidgetAttribute> widgets = DataUtils.getFolderNoteWidget(mContentResolver,
|
|
|
|
|
folderId);
|
|
|
|
|
// 获取与文件夹关联的小部件信息
|
|
|
|
|
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) {
|
|
|
|
|
for (AppWidgetAttribute widget : widgets) {
|
|
|
|
|
if (widget.widgetId != AppWidgetManager.INVALID_APPWIDGET_ID
|
|
|
|
@ -562,63 +567,75 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void openNode(NoteItemData data) {
|
|
|
|
|
// 创建一个Intent对象,指定目标为NoteEditActivity,并设置操作为ACTION_VIEW
|
|
|
|
|
Intent intent = new Intent(this, NoteEditActivity.class);
|
|
|
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
|
|
|
// 将笔记项的ID作为额外的UID数据传递给Intent
|
|
|
|
|
intent.putExtra(Intent.EXTRA_UID, data.getId());
|
|
|
|
|
// 启动NoteEditActivity,并获取结果
|
|
|
|
|
this.startActivityForResult(intent, REQUEST_CODE_OPEN_NODE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void openFolder(NoteItemData data) {
|
|
|
|
|
// 将当前文件夹ID设置为给定数据项的ID
|
|
|
|
|
mCurrentFolderId = data.getId();
|
|
|
|
|
// 启动异步查询以获取当前文件夹下的笔记列表
|
|
|
|
|
startAsyncNotesListQuery();
|
|
|
|
|
// 根据文件夹ID设置状态和标题栏的文本
|
|
|
|
|
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
|
|
|
|
|
mState = ListEditState.CALL_RECORD_FOLDER;
|
|
|
|
|
mAddNewNote.setVisibility(View.GONE);
|
|
|
|
|
} else {
|
|
|
|
|
mState = ListEditState.SUB_FOLDER;
|
|
|
|
|
}
|
|
|
|
|
if (data.getId() == Notes.ID_CALL_RECORD_FOLDER) {
|
|
|
|
|
mTitleBar.setText(R.string.call_record_folder_name);
|
|
|
|
|
} else {
|
|
|
|
|
mState = ListEditState.SUB_FOLDER;
|
|
|
|
|
mTitleBar.setText(data.getSnippet());
|
|
|
|
|
}
|
|
|
|
|
// 显示标题栏
|
|
|
|
|
mTitleBar.setVisibility(View.VISIBLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onClick(View v) {
|
|
|
|
|
switch (v.getId()) {
|
|
|
|
|
case R.id.btn_new_note:
|
|
|
|
|
// 点击“新建笔记”按钮时,调用createNewNote()方法
|
|
|
|
|
createNewNote();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void showSoftInput() {
|
|
|
|
|
// 获取输入法管理器实例
|
|
|
|
|
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
|
|
|
|
|
// 检查输入法管理器是否为空
|
|
|
|
|
if (inputMethodManager != null) {
|
|
|
|
|
// 切换显示软键盘
|
|
|
|
|
// 参数1:显示软键盘的标志,使用SHOW_FORCED表示强制显示
|
|
|
|
|
// 参数2:指定额外的标志,此处为0
|
|
|
|
|
inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 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) {
|
|
|
|
|
// 创建AlertDialog.Builder实例
|
|
|
|
|
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
|
|
|
|
// 加载布局文件dialog_edit_text.xml作为对话框的内容视图
|
|
|
|
|
View view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text, null);
|
|
|
|
|
// 获取布局文件中的EditText控件
|
|
|
|
|
final EditText etName = (EditText) view.findViewById(R.id.et_foler_name);
|
|
|
|
|
// 显示软键盘
|
|
|
|
|
showSoftInput();
|
|
|
|
|
// 根据create参数确定对话框的标题和EditText的初始文本
|
|
|
|
|
if (!create) {
|
|
|
|
|
if (mFocusNoteDataItem != null) {
|
|
|
|
|
etName.setText(mFocusNoteDataItem.getSnippet());
|
|
|
|
|
builder.setTitle(getString(R.string.menu_folder_change_name));
|
|
|
|
|
} else {
|
|
|
|
|
// 如果长按的数据项mFocusNoteDataItem为null,记录错误日志并返回
|
|
|
|
|
Log.e(TAG, "The long click data item is null");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -626,27 +643,35 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
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();
|
|
|
|
|
// 检查文件夹名称是否已存在
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
// 根据create参数确定是修改文件夹还是创建新文件夹
|
|
|
|
|
if (!create) {
|
|
|
|
|
// 修改文件夹
|
|
|
|
|
if (!TextUtils.isEmpty(name)) {
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.SNIPPET, name);
|
|
|
|
@ -658,27 +683,25 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} else if (!TextUtils.isEmpty(name)) {
|
|
|
|
|
// 创建新文件夹
|
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
|
values.put(NoteColumns.SNIPPET, name);
|
|
|
|
|
values.put(NoteColumns.TYPE, Notes.TYPE_FOLDER);
|
|
|
|
|
mContentResolver.insert(Notes.CONTENT_NOTE_URI, values);
|
|
|
|
|
}
|
|
|
|
|
// 关闭对话框
|
|
|
|
|
dialog.dismiss();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 如果EditText的文本为空,禁用确定按钮
|
|
|
|
|
if (TextUtils.isEmpty(etName.getText())) {
|
|
|
|
|
positive.setEnabled(false);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* When the name edit text is null, disable the positive button
|
|
|
|
|
*/
|
|
|
|
|
// EditText文本改变时,根据文本是否为空来启用或禁用确定按钮
|
|
|
|
|
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);
|
|
|
|
@ -686,24 +709,27 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
positive.setEnabled(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void afterTextChanged(Editable s) {
|
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按返回键时根据情况更改类中的数据
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
public void onBackPressed() {
|
|
|
|
|
// 根据当前的状态进行处理
|
|
|
|
|
switch (mState) {
|
|
|
|
|
case SUB_FOLDER:
|
|
|
|
|
// 如果当前状态是SUB_FOLDER,则回退到根文件夹
|
|
|
|
|
mCurrentFolderId = Notes.ID_ROOT_FOLDER;
|
|
|
|
|
mState = ListEditState.NOTE_LIST;
|
|
|
|
|
startAsyncNotesListQuery();
|
|
|
|
|
mTitleBar.setVisibility(View.GONE);
|
|
|
|
|
break;
|
|
|
|
|
case CALL_RECORD_FOLDER:
|
|
|
|
|
// 如果当前状态是CALL_RECORD_FOLDER,则回退到根文件夹
|
|
|
|
|
mCurrentFolderId = Notes.ID_ROOT_FOLDER;
|
|
|
|
|
mState = ListEditState.NOTE_LIST;
|
|
|
|
|
mAddNewNote.setVisibility(View.VISIBLE);
|
|
|
|
@ -711,62 +737,73 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
startAsyncNotesListQuery();
|
|
|
|
|
break;
|
|
|
|
|
case NOTE_LIST:
|
|
|
|
|
// 如果当前状态是NOTE_LIST,则调用父类的onBackPressed方法
|
|
|
|
|
super.onBackPressed();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateWidget(int appWidgetId, int appWidgetType) {
|
|
|
|
|
// 创建一个广播意图,用于更新小部件
|
|
|
|
|
Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
|
|
|
|
|
// 根据小部件类型设置接收广播的类
|
|
|
|
|
if (appWidgetType == Notes.TYPE_WIDGET_2X) {
|
|
|
|
|
intent.setClass(this, NoteWidgetProvider_2x.class);
|
|
|
|
|
} else if (appWidgetType == Notes.TYPE_WIDGET_4X) {
|
|
|
|
|
intent.setClass(this, NoteWidgetProvider_4x.class);
|
|
|
|
|
} else {
|
|
|
|
|
// 未支持的小部件类型,记录错误日志并返回
|
|
|
|
|
Log.e(TAG, "Unspported widget type");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将小部件ID作为额外数据添加到意图中
|
|
|
|
|
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {
|
|
|
|
|
appWidgetId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// 发送广播意图,通知小部件进行更新
|
|
|
|
|
sendBroadcast(intent);
|
|
|
|
|
// 设置结果为RESULT_OK,表示更新小部件成功
|
|
|
|
|
setResult(RESULT_OK, intent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private final OnCreateContextMenuListener mFolderOnCreateContextMenuListener = new OnCreateContextMenuListener() {
|
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
|
|
|
|
|
// 检查当前焦点的笔记数据项是否为null
|
|
|
|
|
if (mFocusNoteDataItem != null) {
|
|
|
|
|
// 设置上下文菜单的标题为焦点笔记数据项的摘要
|
|
|
|
|
menu.setHeaderTitle(mFocusNoteDataItem.getSnippet());
|
|
|
|
|
// 添加菜单项到上下文菜单
|
|
|
|
|
// 参数解释:(groupId, itemId, order, title)
|
|
|
|
|
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的OnCreateContextMenuListener设置为null,以取消菜单监听器
|
|
|
|
|
mNotesListView.setOnCreateContextMenuListener(null);
|
|
|
|
|
}
|
|
|
|
|
super.onContextMenuClosed(menu);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onContextItemSelected(MenuItem item) {
|
|
|
|
|
// 当上下文菜单项被选中时调用该方法
|
|
|
|
|
if (mFocusNoteDataItem == null) {
|
|
|
|
|
// 检查焦点笔记数据项是否为null
|
|
|
|
|
Log.e(TAG, "The long click data item is null");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
|
case MENU_FOLDER_VIEW:
|
|
|
|
|
// 如果选中的菜单项是MENU_FOLDER_VIEW,则打开文件夹
|
|
|
|
|
openFolder(mFocusNoteDataItem);
|
|
|
|
|
break;
|
|
|
|
|
case MENU_FOLDER_DELETE:
|
|
|
|
|
// 如果选中的菜单项是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);
|
|
|
|
@ -781,65 +818,80 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
builder.show();
|
|
|
|
|
break;
|
|
|
|
|
case MENU_FOLDER_CHANGE_NAME:
|
|
|
|
|
// 如果选中的菜单项是MENU_FOLDER_CHANGE_NAME,则显示创建或修改文件夹的对话框
|
|
|
|
|
showCreateOrModifyFolderDialog(false);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onPrepareOptionsMenu(Menu menu) {
|
|
|
|
|
menu.clear();
|
|
|
|
|
// 当准备显示选项菜单时调用该方法
|
|
|
|
|
menu.clear(); // 清空菜单项
|
|
|
|
|
// 根据当前状态(mState)选择要显示的菜单项
|
|
|
|
|
if (mState == ListEditState.NOTE_LIST) {
|
|
|
|
|
// 如果状态为NOTE_LIST,则从资源文件中填充note_list菜单项
|
|
|
|
|
getMenuInflater().inflate(R.menu.note_list, menu);
|
|
|
|
|
// set sync or sync_cancel
|
|
|
|
|
// 根据同步状态设置菜单项的标题为“sync”或“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) {
|
|
|
|
|
// 如果状态为SUB_FOLDER,则从资源文件中填充sub_folder菜单项
|
|
|
|
|
getMenuInflater().inflate(R.menu.sub_folder, menu);
|
|
|
|
|
} else if (mState == ListEditState.CALL_RECORD_FOLDER) {
|
|
|
|
|
// 如果状态为CALL_RECORD_FOLDER,则从资源文件中填充call_record_folder菜单项
|
|
|
|
|
getMenuInflater().inflate(R.menu.call_record_folder, menu);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果状态不是上述已定义的状态,则记录错误日志
|
|
|
|
|
Log.e(TAG, "Wrong state:" + mState);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
|
// 当选项菜单项被选中时调用该方法
|
|
|
|
|
switch (item.getItemId()) {
|
|
|
|
|
case R.id.menu_new_folder: {
|
|
|
|
|
// 如果选中的菜单项是menu_new_folder,则显示创建或修改文件夹的对话框
|
|
|
|
|
showCreateOrModifyFolderDialog(true);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case R.id.menu_export_text: {
|
|
|
|
|
// 如果选中的菜单项是menu_export_text,则导出笔记为文本文件
|
|
|
|
|
exportNoteToText();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case R.id.menu_sync: {
|
|
|
|
|
// 如果选中的菜单项是menu_sync,则根据同步模式进行相应操作
|
|
|
|
|
if (isSyncMode()) {
|
|
|
|
|
if (TextUtils.equals(item.getTitle(), getString(R.string.menu_sync))) {
|
|
|
|
|
// 如果菜单项标题为"menu_sync",则启动同步服务
|
|
|
|
|
GTaskSyncService.startSync(this);
|
|
|
|
|
} else {
|
|
|
|
|
// 如果菜单项标题为"menu_sync_cancel",则取消同步服务
|
|
|
|
|
GTaskSyncService.cancelSync(this);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// 如果不是同步模式,则启动首选项活动
|
|
|
|
|
startPreferenceActivity();
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case R.id.menu_setting: {
|
|
|
|
|
// 如果选中的菜单项是menu_setting,则启动首选项活动
|
|
|
|
|
startPreferenceActivity();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case R.id.menu_new_note: {
|
|
|
|
|
// 如果选中的菜单项是menu_new_note,则创建新的笔记
|
|
|
|
|
createNewNote();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
case R.id.menu_search:
|
|
|
|
|
// 如果选中的菜单项是menu_search,则启动搜索
|
|
|
|
|
onSearchRequested();
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
@ -847,67 +899,61 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public boolean onSearchRequested() {
|
|
|
|
|
startSearch(null, false, null /* appData */, false);
|
|
|
|
|
// 当搜索请求时调用该方法
|
|
|
|
|
startSearch(null, false, null /* appData */, false); // 启动搜索
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void exportNoteToText() {
|
|
|
|
|
// 获取BackupUtils的实例
|
|
|
|
|
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.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.setTitle(NotesListActivity.this.getString(R.string.success_sdcard_export));
|
|
|
|
|
builder.setMessage(NotesListActivity.this.getString(
|
|
|
|
|
R.string.format_exported_file_location, backup
|
|
|
|
|
.getExportedTextFileName(), backup.getExportedTextFileDir()));
|
|
|
|
|
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) {
|
|
|
|
|
// 如果导出失败,发生系统错误,则显示失败对话框
|
|
|
|
|
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.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();
|
|
|
|
|
}.execute(); // 执行异步任务
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private boolean isSyncMode() {
|
|
|
|
|
return NotesPreferenceActivity.getSyncAccountName(this).trim().length() > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startPreferenceActivity() {
|
|
|
|
|
Activity from = getParent() != null ? getParent() : this;
|
|
|
|
|
Intent intent = new Intent(from, NotesPreferenceActivity.class);
|
|
|
|
|
from.startActivityIfNeeded(intent, -1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class OnListItemClickListener implements OnItemClickListener {
|
|
|
|
|
|
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
|
if (view instanceof NotesListItem) {
|
|
|
|
|
NoteItemData item = ((NotesListItem) view).getItemData();
|
|
|
|
@ -944,14 +990,15 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void startQueryDestinationFolders() {
|
|
|
|
|
// 构建查询条件
|
|
|
|
|
String selection = NoteColumns.TYPE + "=? AND " + NoteColumns.PARENT_ID + "<>? AND " + NoteColumns.ID + "<>?";
|
|
|
|
|
selection = (mState == ListEditState.NOTE_LIST) ? selection:
|
|
|
|
|
// 根据当前的列表编辑状态(mState)确定最终的查询条件
|
|
|
|
|
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,
|
|
|
|
@ -964,18 +1011,24 @@ public class NotesListActivity extends AppCompatActivity implements OnClickListe
|
|
|
|
|
},
|
|
|
|
|
NoteColumns.MODIFIED_DATE + " DESC");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
|
|
|
|
|
// 检查长按的视图是否为NotesListItem
|
|
|
|
|
if (view instanceof NotesListItem) {
|
|
|
|
|
// 获取长按的笔记项数据
|
|
|
|
|
mFocusNoteDataItem = ((NotesListItem) view).getItemData();
|
|
|
|
|
// 检查笔记项类型是否为Note类型且不在选择模式下
|
|
|
|
|
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) {
|
|
|
|
|
// 如果笔记项类型为Folder类型,则设置上下文菜单监听器
|
|
|
|
|
mNotesListView.setOnCreateContextMenuListener(mFolderOnCreateContextMenuListener);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|