package net.micode.notes.ui;
 
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.appwidget.AppWidgetManager;
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;
//主界面，一进入就是这个界面
/**
 * @author k
 *
 */
public class NotesListActivity extends Activity 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 String PREFERENCE_ADD_INTRODUCTION = "net.micode.notes.introduction";  //单行超过80个字符
 
    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;  // 分发Y坐标

    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(final Bundle savedInstanceState) {  //需要是final类型   根据程序上下文环境，Java关键字final有“这是无法改变的”或者“终态的”含义，它可以修饰非抽象类、非抽象类成员方法和变量。你可能出于两种理解而需要阻止改变：设计或效率。
        // final类不能被继承，没有子类，final类中的方法默认是final的。
        //final方法不能被子类的方法覆盖，但可以被继承。
        //final成员变量表示常量，只能被赋值一次，赋值后值不再改变。
        //final不能用于修饰构造方法。
        super.onCreate(savedInstanceState); // 调用父类的onCreate函数
        setContentView(R.layout.note_list);
        initResources();
 
        /**
         * Insert an introduction when user firstly use this application
         */
        setAppInfoFromRawRes();// 初始化应用信息
    }
 
    @Override
    // 返回一些子模块完成的数据交给主Activity处理
    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.onActivityResult(requestCode, resultCode, data);
            // 调用 Activity 的onActivityResult（）
        }
    }
 
    private void setAppInfoFromRawRes() {
    	// Android平台给我们提供了一个SharedPreferences类，它是一个轻量级的存储类，特别适合用于保存软件配置参数。
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
        if (!sp.getBoolean(PREFERENCE_ADD_INTRODUCTION, false)) {// 判断是否已添加介绍
            StringBuilder sb = new StringBuilder();
            InputStream in = null;
            try {
                // 把资源文件放到应用程序的/raw/raw下，那么就可以在应用中使用getResources获取资源后,
            	// 以openRawResource方法（不带后缀的资源文件名）打开这个文件。 
            	in = getResources().openRawResource(R.raw.introduction);
                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) {
                e.printStackTrace();
                return;
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
            
            // 创建空的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");
                return;
            }
        }
    }
 
    @Override
    protected void onStart() {
        super.onStart();
        startAsyncNotesListQuery();
    }
 
    // 初始化资源
    private void initResources() {
        mContentResolver = this.getContentResolver(); // 获取应用程序的数据，得到类似数据表的东西
     // 初始化BackgroundQueryHandler
        mBackgroundQueryHandler = new BackgroundQueryHandler(this.getContentResolver());
        mCurrentFolderId = Notes.ID_ROOT_FOLDER;
        
        // findViewById 是安卓编程的定位函数，主要是引用.R文件里的引用名
        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());
        mNotesListView.setOnItemLongClickListener(this);
        mNotesListAdapter = new NotesListAdapter(this);
        mNotesListView.setAdapter(mNotesListAdapter);
        mAddNewNote = (Button) findViewById(R.id.btn_new_note);// 在activity中要获取该按钮
        mAddNewNote.setOnClickListener(this);
        mAddNewNote.setOnTouchListener(new NewNoteOnTouchListener());
        mDispatch = false;
        mDispatchY = 0;
        mOriginY = 0;
        mTitleBar = (TextView) findViewById(R.id.tv_title_bar);
        mState = ListEditState.NOTE_LIST;
        mModeCallBack = new ModeCallback();
    }
 
    // 继承自ListView.MultiChoiceModeListener 和 OnMenuItemClickListener
    private class ModeCallback implements ListView.MultiChoiceModeListener, OnMenuItemClickListener {
        private DropdownMenu mDropDownMenu;
        private ActionMode mActionMode;
        private MenuItem mMoveMenu;
     // 创建ActionMode时触发
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            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);
            } 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);
            mDropDownMenu.setOnDropdownMenuItemClickListener(new PopupMenu.OnMenuItemClickListener(){
                public boolean onMenuItemClick(final MenuItem item) {
                    mNotesListAdapter.selectAll(!mNotesListAdapter.isAllSelected());
                    updateMenu();// 更新菜单
                    return true;
                }
 
            });
            return true;
        }
 
        // 更新菜单
        private void updateMenu() {
            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;
        }
        // 销毁ActionMode时触发
        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.LENGTH_SHORT).show();
                return true;
            }
 
            switch (item.getItemId()) {
                case R.id.delete:
                	// 弹出删除对话框
                    AlertDialog.Builder builder = new AlertDialog.Builder(NotesListActivity.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_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:
                    startQueryDestinationFolders();// 查询目标文件夹
                    break;
                default:
                    return false;
            }
            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();
                    int start = screenHeight - newNoteViewHeight;
                    int eventY = start + (int) event.getY();
                    /**
                     * 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锛圲nit: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
                                - 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() {
    	// 构建查询语句的selection，根据当前文件夹id选择不同的查询条件 	
        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");
    }
 // 异步查询处理器类
    private final class BackgroundQueryHandler extends AsyncQueryHandler {
        public BackgroundQueryHandler(ContentResolver contentResolver) {
            super(contentResolver);
        }
 
        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
            switch (token) {
                case FOLDER_NOTE_LIST_QUERY_TOKEN:
                	// 查询完成后更新列表适配器的Cursor数据源
                    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);
        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);
        intent.setAction(Intent.ACTION_INSERT_OR_EDIT);
        intent.putExtra(Notes.INTENT_EXTRA_FOLDER_ID, mCurrentFolderId);
        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())) {
                    } 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) {
                if (widgets != null) {
                    for (AppWidgetAttribute widget : widgets) {
                        if (widget.widgetId != AppWidgetManager.INVALID_APPWIDGET_ID
                                && widget.widgetType != Notes.TYPE_WIDGET_INVALIDE) {
                            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);
        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
                        && widget.widgetType != Notes.TYPE_WIDGET_INVALIDE) {
                    updateWidget(widget.widgetId, widget.widgetType);
                }
            }
        }
    }
 // 打开笔记详情界面
    private void openNode(NoteItemData data) {
        Intent intent = new Intent(this, NoteEditActivity.class);
        intent.setAction(Intent.ACTION_VIEW);
        intent.putExtra(Intent.EXTRA_UID, data.getId());
        this.startActivityForResult(intent, REQUEST_CODE_OPEN_NODE);
    }
 
    private void openFolder(NoteItemData data) {
    	// 更新当前文件夹id，并进行异步查询笔记列表
        mCurrentFolderId = data.getId();
        startAsyncNotesListQuery();
        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 {
            mTitleBar.setText(data.getSnippet());
        }
        mTitleBar.setVisibility(View.VISIBLE);
    }
 
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_new_note:
            	// 点击新建笔记按钮，触发创建新的笔记操作
                createNewNote();
                break;
            default:
                break;
        }
    }
 // 显示输入法软键盘
    private void showSoftInput() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            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) {
        final AlertDialog.Builder builder = new AlertDialog.Builder(this);// 创建对话框构建器
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_text, null);//加载布局文件
        final EditText etName = (EditText) view.findViewById(R.id.et_foler_name);// 获取输入框
        showSoftInput();// 显示软键盘
        if (!create) { // 如果是修改文件夹
            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"); // 输出错误日志
                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(); // 获取输入的文件夹名称

                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); // 更新文件夹名称
                        values.put(NoteColumns.TYPE, Notes.TYPE_FOLDER); // 设置类型为文件夹
                        values.put(NoteColumns.LOCAL_MODIFIED, 1); // 标记本地修改
                        mContentResolver.update(Notes.CONTENT_NOTE_URI, values, NoteColumns.ID
                                + "=?", new String[] {
                            String.valueOf(mFocusNoteDataItem.getId())
                        }); // 更新数据库中的文件夹信息
                    }
                } 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(); // 关闭对话框
            }
        });
        if (TextUtils.isEmpty(etName.getText())) { // 如果输入框内容为空
            positive.setEnabled(false); // 禁用确定按钮
        }
        /**
         * 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);
                }
            }
 
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
 
            }
        });
    }
 
    /* (non-Javadoc)
     * @see android.app.Activity#onBackPressed()
     * 按返回键时根据情况更改类中的数据
     */
    @Override
    public void onBackPressed() { switch (mState) {
            case SUB_FOLDER:
                mCurrentFolderId = Notes.ID_ROOT_FOLDER;
                mState = ListEditState.NOTE_LIST;
                startAsyncNotesListQuery();
                mTitleBar.setVisibility(View.GONE);
                break;
            case CALL_RECORD_FOLDER:
                mCurrentFolderId = Notes.ID_ROOT_FOLDER;
                mState = ListEditState.NOTE_LIST;
                mAddNewNote.setVisibility(View.VISIBLE);
                mTitleBar.setVisibility(View.GONE);
                startAsyncNotesListQuery();
                break;
            case NOTE_LIST:
                super.onBackPressed();
                break;
            default:
                break;
        }
    }
 
    /**
     * @param appWidgetId
     * @param appWidgetType
     * 根据不同类型的widget更新插件，通过intent传送数据
     */
    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;
        }
 
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] {
            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);
    }
 
    /* (non-Javadoc)
     * @see android.app.Activity#onContextItemSelected(android.view.MenuItem)
     * 针对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()) {
            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);
                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);   // 加载便签列表的菜单布局
            // 设置同步或取消同步选项的标题
            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);   // 加载子文件夹的菜单布局
        } else if (mState == ListEditState.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: {   // 如果选择了新建文件夹选项
                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))) {
                        GTaskSyncService.startSync(this);   // 启动同步服务
                    } else {
                        GTaskSyncService.cancelSync(this);   // 取消同步服务
                    }
                } else {
                    startPreferenceActivity();   // 跳转到设置界面
                }
                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;
    }
 
    /* (non-Javadoc)
     * @see android.app.Activity#onSearchRequested()
     * 直接调用startSearch函数
     */
    @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));   // 设置对话框内容为SD卡未挂载
                    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) {   // 如果系统错误
                    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();
    }
 
    /**
     * @return
     * 功能：判断是否正在同步
     */
    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);
    }
 
    /**
     * @author k
     * 函数功能：实现对便签列表项的点击事件（短按）
     */
    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();   // 获取点击项的数据
                if (mNotesListAdapter.isInChoiceMode()) {   // 如果处于选择模式
                    if (item.getType() == Notes.TYPE_NOTE) {
                        position = position - mNotesListView.getHeaderViewsCount();
                        mModeCallBack.onItemCheckedStateChanged(null, position, id,
                                !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,
                FoldersListAdapter.PROJECTION,
                selection,
                new String[] {
                        String.valueOf(Notes.TYPE_FOLDER),
                        String.valueOf(Notes.ID_TRASH_FOLER),
                        String.valueOf(mCurrentFolderId)
                },
                NoteColumns.MODIFIED_DATE + " DESC");
    }
 
    /* (non-Javadoc)
     * @see android.widget.AdapterView.OnItemLongClickListener#onItemLongClick(android.widget.AdapterView, android.view.View, int, long)
     * 长按某一项时进行的操作
     * 如果长按的是便签，则通过ActionMode菜单实现；如果长按的是文件夹，则通过ContextMenu菜单实现；
     *        具体ActionMOde菜单和ContextMenu菜单的详细见精度笔记
     */
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        if (view instanceof NotesListItem) {
            mFocusNoteDataItem = ((NotesListItem) view).getItemData();// 获取当前长按项的数据
            if (mFocusNoteDataItem.getType() == Notes.TYPE_NOTE && !mNotesListAdapter.isInChoiceMode()) {
                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) {
                mNotesListView.setOnCreateContextMenuListener(mFolderOnCreateContextMenuListener);// 设置创建上下文菜单监听器
            }
        }
        return false;
    }
}