+ private void setSearchText(final String text, final boolean triggerSearch) {
+ if (!triggerSearch) {
+ searchView.setOnQueryTextListener(null);
+ }
+ searchView.setQuery(text, false);
+ moveCursorToRight();
+ if (!triggerSearch) {
+ searchView.setOnQueryTextListener(onQueryTextListener);
+ } else {
+ onQueryTextListener.onQueryTextChange(text);
+ }
+ }
+
+// private long cursorDelayMillis = 100;
+ private void moveCursorToRight() {
+// if (searchText.getLayout() != null) {
+// cursorDelayMillis = 100;
+// // Surprising, but this can crash when you rotate...
+// Selection.moveToRightEdge(searchView.getQuery(), searchText.getLayout());
+// } else {
+// uiHandler.postDelayed(new Runnable() {
+// @Override
+// public void run() {
+// moveCursorToRight();
+// }
+// }, cursorDelayMillis);
+// cursorDelayMillis = Math.min(10 * 1000, 2 * cursorDelayMillis);
+// }
+ }
+
+ // --------------------------------------------------------------------------
+ // SearchOperation
+ // --------------------------------------------------------------------------
+
+ private void searchFinished(final SearchOperation searchOperation) {
+ if (searchOperation.interrupted.get()) {
+ Log.d(LOG, "Search operation was interrupted: " + searchOperation);
+ return;
+ }
+ if (searchOperation != this.currentSearchOperation) {
+ Log.d(LOG, "Stale searchOperation finished: " + searchOperation);
+ return;
+ }
+
+ final Index.IndexEntry searchResult = searchOperation.searchResult;
+ Log.d(LOG, "searchFinished: " + searchOperation + ", searchResult=" + searchResult);
+
+ currentSearchOperation = null;
+ uiHandler.postDelayed(new Runnable() {
+ @Override
+ public void run() {
+ if (currentSearchOperation == null) {
+ if (searchResult != null) {
+ if (isFiltered()) {
+ clearFiltered();
+ }
+ jumpToRow(searchResult.startRow);
+ } else if (searchOperation.multiWordSearchResult != null) {
+ // Multi-row search....
+ setFiltered(searchOperation);
+ } else {
+ throw new IllegalStateException("This should never happen.");
+ }
+ } else {
+ Log.d(LOG, "More coming, waiting for currentSearchOperation.");
+ }
+ }
+ }, 20);
+ }
+
+ private final void jumpToRow(final int row) {
+ Log.d(LOG, "jumpToRow: " + row + ", refocusSearchText=" + false);
+// getListView().requestFocusFromTouch();
+ getListView().setSelectionFromTop(row, 0);
+ getListView().setSelected(true);
+ }
+
+ static final Pattern WHITESPACE = Pattern.compile("\\s+");
+
+ final class SearchOperation implements Runnable {
+
+ final AtomicBoolean interrupted = new AtomicBoolean(false);
+
+ final String searchText;
+
+ List<String> searchTokens; // filled in for multiWord.
+
+ final Index index;
+
+ long searchStartMillis;
+
+ Index.IndexEntry searchResult;
+
+ List<RowBase> multiWordSearchResult;
+
+ boolean done = false;
+
+ SearchOperation(final String searchText, final Index index) {
+ this.searchText = StringUtil.normalizeWhitespace(searchText);
+ this.index = index;
+ }
+
+ public String toString() {
+ return String.format("SearchOperation(%s,%s)", searchText, interrupted.toString());
+ }
+
+ @Override
+ public void run() {
+ try {
+ searchStartMillis = System.currentTimeMillis();
+ final String[] searchTokenArray = WHITESPACE.split(searchText);
+ if (searchTokenArray.length == 1) {
+ searchResult = index.findInsertionPoint(searchText, interrupted);
+ } else {
+ searchTokens = Arrays.asList(searchTokenArray);
+ multiWordSearchResult = index.multiWordSearch(searchText, searchTokens, interrupted);
+ }
+ Log.d(LOG,
+ "searchText=" + searchText + ", searchDuration="
+ + (System.currentTimeMillis() - searchStartMillis)
+ + ", interrupted=" + interrupted.get());
+ if (!interrupted.get()) {
+ uiHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ searchFinished(SearchOperation.this);
+ }
+ });
+ } else {
+ Log.d(LOG, "interrupted, skipping searchFinished.");
+ }
+ } catch (Exception e) {
+ Log.e(LOG, "Failure during search (can happen during Activity close.");
+ } finally {
+ synchronized (this) {
+ done = true;
+ this.notifyAll();
+ }
+ }
+ }
+ }
+
+ // --------------------------------------------------------------------------
+ // IndexAdapter
+ // --------------------------------------------------------------------------
+
+ static ViewGroup.LayoutParams WEIGHT_1 = new LinearLayout.LayoutParams(
+ ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
+
+ static ViewGroup.LayoutParams WEIGHT_0 = new LinearLayout.LayoutParams(
+ ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 0.0f);
+
+ final class IndexAdapter extends BaseAdapter {
+
+ private static final float PADDING_DEFAULT_DP = 8;
+
+ private static final float PADDING_LARGE_DP = 16;
+
+ final Index index;
+
+ final List<RowBase> rows;
+
+ final Set<String> toHighlight;
+
+ private int mPaddingDefault;
+
+ private int mPaddingLarge;
+
+ IndexAdapter(final Index index) {
+ this.index = index;
+ rows = index.rows;
+ this.toHighlight = null;
+ getMetrics();
+ }
+
+ IndexAdapter(final Index index, final List<RowBase> rows, final List<String> toHighlight) {
+ this.index = index;
+ this.rows = rows;
+ this.toHighlight = new LinkedHashSet<String>(toHighlight);
+ getMetrics();
+ }
+
+ private void getMetrics() {
+ // Get the screen's density scale
+ final float scale = getResources().getDisplayMetrics().density;
+ // Convert the dps to pixels, based on density scale
+ mPaddingDefault = (int) (PADDING_DEFAULT_DP * scale + 0.5f);
+ mPaddingLarge = (int) (PADDING_LARGE_DP * scale + 0.5f);
+ }
+
+ @Override
+ public int getCount() {
+ return rows.size();
+ }
+
+ @Override
+ public RowBase getItem(int position) {
+ return rows.get(position);
+ }
+
+ @Override
+ public long getItemId(int position) {
+ return getItem(position).index();
+ }
+
+ @Override
+ public TableLayout getView(int position, View convertView, ViewGroup parent) {
+ final TableLayout result;
+ if (convertView instanceof TableLayout) {
+ result = (TableLayout) convertView;
+ result.removeAllViews();
+ } else {
+ result = new TableLayout(parent.getContext());
+ }
+ final RowBase row = getItem(position);
+ if (row instanceof PairEntry.Row) {
+ return getView(position, (PairEntry.Row) row, parent, result);
+ } else if (row instanceof TokenRow) {
+ return getView((TokenRow) row, parent, result);
+ } else if (row instanceof HtmlEntry.Row) {
+ return getView((HtmlEntry.Row) row, parent, result);
+ } else {
+ throw new IllegalArgumentException("Unsupported Row type: " + row.getClass());
+ }
+ }
+
+ private TableLayout getView(final int position, PairEntry.Row row, ViewGroup parent,
+ final TableLayout result) {
+ final PairEntry entry = row.getEntry();
+ final int rowCount = entry.pairs.size();
+
+ final TableRow.LayoutParams layoutParams = new TableRow.LayoutParams();
+ layoutParams.weight = 0.5f;
+ layoutParams.leftMargin = mPaddingLarge;
+
+ for (int r = 0; r < rowCount; ++r) {
+ final TableRow tableRow = new TableRow(result.getContext());
+
+ final TextView col1 = new TextView(tableRow.getContext());
+ final TextView col2 = new TextView(tableRow.getContext());
+
+ // Set the columns in the table.
+ if (r > 0) {
+ final TextView bullet = new TextView(tableRow.getContext());
+ bullet.setText(" • ");
+ tableRow.addView(bullet);
+ }
+ tableRow.addView(col1, layoutParams);
+ final TextView margin = new TextView(tableRow.getContext());
+ margin.setText(" ");
+ tableRow.addView(margin);
+ if (r > 0) {
+ final TextView bullet = new TextView(tableRow.getContext());
+ bullet.setText(" • ");
+ tableRow.addView(bullet);
+ }
+ tableRow.addView(col2, layoutParams);
+ col1.setWidth(1);
+ col2.setWidth(1);
+
+ // Set what's in the columns.
+
+ final Pair pair = entry.pairs.get(r);
+ final String col1Text = index.swapPairEntries ? pair.lang2 : pair.lang1;
+ final String col2Text = index.swapPairEntries ? pair.lang1 : pair.lang2;
+
+ col1.setText(col1Text, TextView.BufferType.SPANNABLE);
+ col2.setText(col2Text, TextView.BufferType.SPANNABLE);
+
+ // Bold the token instances in col1.
+ final Set<String> toBold = toHighlight != null ? this.toHighlight : Collections
+ .singleton(row.getTokenRow(true).getToken());
+ final Spannable col1Spannable = (Spannable) col1.getText();
+ for (final String token : toBold) {
+ int startPos = 0;
+ while ((startPos = col1Text.indexOf(token, startPos)) != -1) {
+ col1Spannable.setSpan(new StyleSpan(Typeface.BOLD), startPos, startPos
+ + token.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
+ startPos += token.length();
+ }
+ }
+
+ createTokenLinkSpans(col1, col1Spannable, col1Text);
+ createTokenLinkSpans(col2, (Spannable) col2.getText(), col2Text);
+
+ col1.setTypeface(typeface);
+ col2.setTypeface(typeface);
+ col1.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);
+ col2.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);
+ // col2.setBackgroundResource(theme.otherLangBg);
+
+ if (index.swapPairEntries) {
+ col2.setOnLongClickListener(textViewLongClickListenerIndex0);
+ col1.setOnLongClickListener(textViewLongClickListenerIndex1);
+ } else {
+ col1.setOnLongClickListener(textViewLongClickListenerIndex0);
+ col2.setOnLongClickListener(textViewLongClickListenerIndex1);
+ }
+
+ result.addView(tableRow);
+ }
+
+ // Because we have a Button inside a ListView row:
+ // http://groups.google.com/group/android-developers/browse_thread/thread/3d96af1530a7d62a?pli=1
+ result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
+ result.setClickable(true);
+ result.setFocusable(true);
+ result.setLongClickable(true);
+ result.setBackgroundResource(android.R.drawable.menuitem_background);
+ result.setOnClickListener(new TextView.OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ DictionaryActivity.this.onListItemClick(getListView(), v, position, position);
+ }
+ });
+
+ return result;
+ }
+
+ private TableLayout getPossibleLinkToHtmlEntryView(final boolean isTokenRow,
+ final String text, final boolean hasMainEntry, final List<HtmlEntry> htmlEntries,
+ final String htmlTextToHighlight, ViewGroup parent, final TableLayout result) {
+ final Context context = parent.getContext();
+
+ final TableRow tableRow = new TableRow(result.getContext());
+ tableRow.setBackgroundResource(hasMainEntry ? theme.tokenRowMainBg
+ : theme.tokenRowOtherBg);
+ if (isTokenRow) {
+ tableRow.setPadding(mPaddingDefault, mPaddingDefault, mPaddingDefault, 0);
+ } else {
+ tableRow.setPadding(mPaddingLarge, mPaddingDefault, mPaddingDefault, 0);
+ }
+ result.addView(tableRow);
+
+ // Make it so we can long-click on these token rows, too:
+ final TextView textView = new TextView(context);
+ textView.setText(text, BufferType.SPANNABLE);
+ createTokenLinkSpans(textView, (Spannable) textView.getText(), text);
+ final TextViewLongClickListener textViewLongClickListenerIndex0 = new TextViewLongClickListener(
+ 0);
+ textView.setOnLongClickListener(textViewLongClickListenerIndex0);
+ result.setLongClickable(true);
+
+ // Doesn't work:
+ // textView.setTextColor(android.R.color.secondary_text_light);
+ textView.setTypeface(typeface);
+ TableRow.LayoutParams lp = new TableRow.LayoutParams(0);
+ if (isTokenRow) {
+ textView.setTextAppearance(context, theme.tokenRowFg);
+ textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 4 * fontSizeSp / 3);
+ } else {
+ textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, fontSizeSp);
+ }
+ lp.weight = 1.0f;
+
+ textView.setLayoutParams(lp);
+ tableRow.addView(textView);
+
+ if (!htmlEntries.isEmpty()) {
+ final ClickableSpan clickableSpan = new ClickableSpan() {
+ @Override
+ public void onClick(View widget) {
+ }
+ };
+ ((Spannable) textView.getText()).setSpan(clickableSpan, 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
+ result.setClickable(true);
+ textView.setClickable(true);
+ textView.setMovementMethod(LinkMovementMethod.getInstance());
+ textView.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ String html = HtmlEntry.htmlBody(htmlEntries, index.shortName);
+ //Log.d(LOG, "html=" + html);
+ startActivityForResult(
+ HtmlDisplayActivity.getHtmlIntent(String.format(
+ "<html><head></head><body>%s</body></html>", html),
+ htmlTextToHighlight, false),
+ 0);
+ }
+ });
+ }
+ return result;
+ }
+
+ private TableLayout getView(TokenRow row, ViewGroup parent, final TableLayout result) {
+ final IndexEntry indexEntry = row.getIndexEntry();
+ return getPossibleLinkToHtmlEntryView(true, indexEntry.token, row.hasMainEntry,
+ indexEntry.htmlEntries, null, parent, result);
+ }
+
+ private TableLayout getView(HtmlEntry.Row row, ViewGroup parent, final TableLayout result) {
+ final HtmlEntry htmlEntry = row.getEntry();
+ final TokenRow tokenRow = row.getTokenRow(true);
+ return getPossibleLinkToHtmlEntryView(false,
+ getString(R.string.seeAlso, htmlEntry.title, htmlEntry.entrySource.getName()),
+ false, Collections.singletonList(htmlEntry), tokenRow.getToken(), parent,
+ result);
+ }
+
+ }
+
+ static final Pattern CHAR_DASH = Pattern.compile("['\\p{L}\\p{M}\\p{N}]+");
+
+ private void createTokenLinkSpans(final TextView textView, final Spannable spannable,
+ final String text) {
+ // Saw from the source code that LinkMovementMethod sets the selection!
+ // http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.3.1_r1/android/text/method/LinkMovementMethod.java#LinkMovementMethod
+ textView.setMovementMethod(LinkMovementMethod.getInstance());
+ final Matcher matcher = CHAR_DASH.matcher(text);
+ while (matcher.find()) {
+ spannable.setSpan(new NonLinkClickableSpan(textColorFg), matcher.start(), matcher.end(),
+ Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
+ }
+ }
+
+ String selectedSpannableText = null;
+
+ int selectedSpannableIndex = -1;
+
+ @Override
+ public boolean onTouchEvent(MotionEvent event) {
+ selectedSpannableText = null;
+ selectedSpannableIndex = -1;
+ return super.onTouchEvent(event);
+ }
+
+ private class TextViewLongClickListener implements OnLongClickListener {
+ final int index;
+
+ private TextViewLongClickListener(final int index) {
+ this.index = index;
+ }
+
+ @Override
+ public boolean onLongClick(final View v) {
+ final TextView textView = (TextView) v;
+ final int start = textView.getSelectionStart();
+ final int end = textView.getSelectionEnd();
+ if (start >= 0 && end >= 0) {
+ selectedSpannableText = textView.getText().subSequence(start, end).toString();
+ selectedSpannableIndex = index;
+ }
+ return false;
+ }
+ }
+
+ final TextViewLongClickListener textViewLongClickListenerIndex0 = new TextViewLongClickListener(
+ 0);
+
+ final TextViewLongClickListener textViewLongClickListenerIndex1 = new TextViewLongClickListener(
+ 1);
+
+ // --------------------------------------------------------------------------
+ // SearchText
+ // --------------------------------------------------------------------------
+
+ void onSearchTextChange(final String text) {
+ if ("thadolina".equals(text)) {
+ final Dialog dialog = new Dialog(getListView().getContext());
+ dialog.setContentView(R.layout.thadolina_dialog);
+ dialog.setTitle("Ti amo, amore mio!");
+ final ImageView imageView = (ImageView) dialog.findViewById(R.id.thadolina_image);
+ imageView.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ final Intent intent = new Intent(Intent.ACTION_VIEW);
+ intent.setData(Uri.parse("https://sites.google.com/site/cfoxroxvday/vday2012"));
+ startActivity(intent);
+ }
+ });
+ dialog.show();
+ }
+ if (dictRaf == null) {
+ Log.d(LOG, "searchText changed during shutdown, doing nothing.");
+ return;
+ }
+// if (!searchView.hasFocus()) {
+// Log.d(LOG, "searchText changed without focus, doing nothing.");
+// return;
+// }
+ Log.d(LOG, "onSearchTextChange: " + text);
+ if (currentSearchOperation != null) {
+ Log.d(LOG, "Interrupting currentSearchOperation.");
+ currentSearchOperation.interrupted.set(true);
+ }
+ currentSearchOperation = new SearchOperation(text, index);
+ searchExecutor.execute(currentSearchOperation);
+ }
+
+ // --------------------------------------------------------------------------
+ // Filtered results.
+ // --------------------------------------------------------------------------
+
+ boolean isFiltered() {
+ return rowsToShow != null;
+ }
+
+ void setFiltered(final SearchOperation searchOperation) {
+ if (nextWordMenuItem != null) {
+ nextWordMenuItem.setEnabled(false);
+ previousWordMenuItem.setEnabled(false);
+ }
+ rowsToShow = searchOperation.multiWordSearchResult;
+ setListAdapter(new IndexAdapter(index, rowsToShow, searchOperation.searchTokens));
+ }
+
+ void clearFiltered() {
+ if (nextWordMenuItem != null) {
+ nextWordMenuItem.setEnabled(true);
+ previousWordMenuItem.setEnabled(true);
+ }
+ setListAdapter(new IndexAdapter(index));
+ rowsToShow = null;