+ \r
+ @Override\r
+ public boolean onCreateOptionsMenu(final Menu menu) {\r
+ application.onCreateGlobalOptionsMenu(this, menu);\r
+\r
+ {\r
+ final MenuItem dictionaryList = menu.add(getString(R.string.dictionaryManager));\r
+ dictionaryList.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
+ public boolean onMenuItemClick(final MenuItem menuItem) {\r
+ startActivity(DictionaryManagerActivity.getLaunchIntent());\r
+ finish();\r
+ return false;\r
+ }\r
+ });\r
+ }\r
+\r
+ {\r
+ final MenuItem aboutDictionary = menu.add(getString(R.string.aboutDictionary));\r
+ aboutDictionary.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
+ public boolean onMenuItemClick(final MenuItem menuItem) {\r
+ final Context context = getListView().getContext();\r
+ final Dialog dialog = new Dialog(context);\r
+ dialog.setContentView(R.layout.about_dictionary_dialog);\r
+ final TextView textView = (TextView) dialog.findViewById(R.id.text);\r
+\r
+ final String name = application.getDictionaryName(dictFile.getName());\r
+ dialog.setTitle(name);\r
+ \r
+ final StringBuilder builder = new StringBuilder();\r
+ final DictionaryInfo dictionaryInfo = dictionary.getDictionaryInfo();\r
+ dictionaryInfo.uncompressedBytes = dictFile.length();\r
+ if (dictionaryInfo != null) {\r
+ builder.append(dictionaryInfo.dictInfo).append("\n\n");\r
+ builder.append(getString(R.string.dictionaryPath, dictFile.getPath())).append("\n");\r
+ builder.append(getString(R.string.dictionarySize, dictionaryInfo.uncompressedBytes)).append("\n");\r
+ builder.append(getString(R.string.dictionaryCreationTime, dictionaryInfo.creationMillis)).append("\n");\r
+ for (final IndexInfo indexInfo : dictionaryInfo.indexInfos) {\r
+ builder.append("\n");\r
+ builder.append(getString(R.string.indexName, indexInfo.shortName)).append("\n");\r
+ builder.append(getString(R.string.mainTokenCount, indexInfo.mainTokenCount)).append("\n");\r
+ }\r
+ builder.append("\n");\r
+ builder.append(getString(R.string.sources)).append("\n");\r
+ for (final EntrySource source : dictionary.sources) {\r
+ builder.append(getString(R.string.sourceInfo, source.getName(), source.getNumEntries())).append("\n");\r
+ }\r
+ }\r
+// } else {\r
+// builder.append(getString(R.string.invalidDictionary));\r
+// }\r
+ textView.setText(builder.toString());\r
+ \r
+ dialog.show();\r
+ final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();\r
+ layoutParams.width = WindowManager.LayoutParams.FILL_PARENT;\r
+ layoutParams.height = WindowManager.LayoutParams.FILL_PARENT;\r
+ dialog.getWindow().setAttributes(layoutParams);\r
+ return false;\r
+ }\r
+ });\r
+ }\r
+\r
+ return true;\r
+ }\r
+\r
+\r
+ // --------------------------------------------------------------------------\r
+ // Context Menu + clicks\r
+ // --------------------------------------------------------------------------\r
+\r
+ @Override\r
+ public void onCreateContextMenu(ContextMenu menu, View v,\r
+ ContextMenuInfo menuInfo) {\r
+ AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;\r
+ final RowBase row = (RowBase) getListAdapter().getItem(adapterContextMenuInfo.position);\r
+\r
+ final MenuItem addToWordlist = menu.add(getString(R.string.addToWordList, wordList.getName()));\r
+ addToWordlist.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
+ public boolean onMenuItemClick(MenuItem item) {\r
+ onAppendToWordList(row);\r
+ return false;\r
+ }\r
+ });\r
+\r
+ final MenuItem copy = menu.add(android.R.string.copy);\r
+ copy.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
+ public boolean onMenuItemClick(MenuItem item) {\r
+ onCopy(row);\r
+ return false;\r
+ }\r
+ });\r
+ \r
+ if (selectedSpannableText != null) {\r
+ final String selectedText = selectedSpannableText;\r
+ final MenuItem searchForSelection = menu.add(getString(R.string.searchForSelection, selectedSpannableText));\r
+ searchForSelection.setOnMenuItemClickListener(new OnMenuItemClickListener() {\r
+ public boolean onMenuItemClick(MenuItem item) {\r
+ int indexToUse = -1;\r
+ for (int i = 0; i < dictionary.indices.size(); ++i) {\r
+ final Index index = dictionary.indices.get(i);\r
+ if (indexPrepFinished) {\r
+ System.out.println("Doing index lookup: on " + selectedText);\r
+ final IndexEntry indexEntry = index.findExact(selectedText);\r
+ if (indexEntry != null) {\r
+ final TokenRow tokenRow = index.rows.get(indexEntry.startRow).getTokenRow(false);\r
+ if (tokenRow != null && tokenRow.hasMainEntry) {\r
+ indexToUse = i;\r
+ break;\r
+ }\r
+ }\r
+ } else {\r
+ Log.w(LOG, "Skipping findExact on index " + index.shortName);\r
+ }\r
+ }\r
+ if (indexToUse == -1) {\r
+ indexToUse = selectedSpannableIndex;\r
+ }\r
+ final boolean changeIndex = indexIndex != indexToUse;\r
+ setSearchText(selectedText, !changeIndex); // If we're not changing index, we have to triggerSearch.\r
+ if (changeIndex) {\r
+ changeIndexGetFocusAndResearch(indexToUse);\r
+ }\r
+ // Give focus back to list view because typing is done.\r
+ getListView().requestFocus();\r
+ return false;\r
+ }\r
+ });\r
+ }\r
+ \r
+\r
+ }\r
+ \r
+ @Override\r
+ protected void onListItemClick(ListView l, View v, int row, long id) {\r
+ defocusSearchText();\r
+ if (clickOpensContextMenu && dictRaf != null) {\r
+ openContextMenu(v);\r
+ }\r
+ }\r
+ \r
+ void onAppendToWordList(final RowBase row) {\r
+ defocusSearchText();\r
+ \r
+ final StringBuilder rawText = new StringBuilder();\r
+ rawText.append(\r
+ new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(new Date()))\r
+ .append("\t");\r
+ rawText.append(index.longName).append("\t");\r
+ rawText.append(row.getTokenRow(true).getToken()).append("\t");\r
+ rawText.append(row.getRawText(saveOnlyFirstSubentry));\r
+ Log.d(LOG, "Writing : " + rawText);\r
+\r
+ try {\r
+ wordList.getParentFile().mkdirs();\r
+ final PrintWriter out = new PrintWriter(\r
+ new FileWriter(wordList, true));\r
+ out.println(rawText.toString());\r
+ out.close();\r
+ } catch (IOException e) {\r
+ Log.e(LOG, "Unable to append to " + wordList.getAbsolutePath(), e);\r
+ Toast.makeText(this, getString(R.string.failedAddingToWordList, wordList.getAbsolutePath()), Toast.LENGTH_LONG);\r
+ }\r
+ return;\r
+ }\r
+ \r
+ /**\r
+ * Called when user clicks outside of search text, so that they can start\r
+ * typing again immediately.\r
+ */\r
+ void defocusSearchText() {\r
+ //Log.d(LOG, "defocusSearchText");\r
+ // Request focus so that if we start typing again, it clears the text input.\r
+ getListView().requestFocus();\r
+ \r
+ // Visual indication that a new keystroke will clear the search text.\r
+ searchText.selectAll();\r
+ }\r
+\r
+ void onCopy(final RowBase row) {\r
+ defocusSearchText();\r
+\r
+ Log.d(LOG, "Copy, row=" + row);\r
+ final StringBuilder result = new StringBuilder();\r
+ result.append(row.getRawText(false));\r
+ final ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);\r
+ clipboardManager.setText(result.toString());\r
+ Log.d(LOG, "Copied: " + result);\r
+ }\r
+\r
+ @Override\r
+ public boolean onKeyDown(final int keyCode, final KeyEvent event) {\r
+ if (event.getUnicodeChar() != 0) {\r
+ if (!searchText.hasFocus()) {\r
+ setSearchText("" + (char) event.getUnicodeChar(), true);\r
+ }\r
+ return true;\r
+ }\r
+ if (keyCode == KeyEvent.KEYCODE_BACK) {\r
+ Log.d(LOG, "Clearing dictionary prefs.");\r
+ DictionaryActivity.clearDictionaryPrefs(this);\r
+ }\r
+ if (keyCode == KeyEvent.KEYCODE_ENTER) {\r
+ Log.d(LOG, "Trying to hide soft keyboard.");\r
+ final InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);\r
+ inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);\r
+ return true;\r
+ }\r
+ return super.onKeyDown(keyCode, event);\r
+ }\r
+\r
+ private void setSearchText(final String text, final boolean triggerSearch) {\r
+ if (!triggerSearch) {\r
+ getListView().requestFocus();\r
+ }\r
+ searchText.setText(text);\r
+ searchText.requestFocus();\r
+ if (searchText.getLayout() != null) {\r
+ // Surprising, but this can crash when you rotate...\r
+ Selection.moveToRightEdge(searchText.getText(), searchText.getLayout());\r
+ }\r
+ if (triggerSearch) {\r
+ onSearchTextChange(text);\r
+ }\r
+ }\r
+\r