import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
+import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.StyleSpan;
+import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.ContextMenu;
}
Log.d(LOG, "Loading index " + indexIndex);
index = dictionary.indices.get(indexIndex);
+ getListView().setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
getListView().setEmptyView(findViewById(android.R.id.empty));
+ getListView().setOnItemClickListener(new OnItemClickListener() {
+ @Override
+ public void onItemClick(AdapterView<?> parent, View view, int row, long id) {
+ onListItemClick(getListView(), view, row, id);
+ }
+ });
+
setListAdapter(new IndexAdapter(index));
+ // Pre-load the Transliterator (will spawn its own thread)
+ TransliteratorManager.init(new TransliteratorManager.Callback() {
+ @Override
+ public void onTransliteratorReady() {
+ uiHandler.post(new Runnable() {
+ @Override
+ public void run() {
+ onSearchTextChange(searchView.getQuery().toString());
+ }
+ });
+ }
+ }, DictionaryApplication.threadBackground);
+
// Pre-load the collators.
new Thread(new Runnable() {
public void run() {
- android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_BACKGROUND);
+ android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LESS_FAVORABLE);
final long startMillis = System.currentTimeMillis();
try {
- TransliteratorManager.init(new TransliteratorManager.Callback() {
- @Override
- public void onTransliteratorReady() {
- uiHandler.post(new Runnable() {
- @Override
- public void run() {
- onSearchTextChange(searchView.getQuery().toString());
- }
- });
- }
- });
-
for (final Index index : dictionary.indices) {
final String searchToken = index.sortedIndexEntries.get(0).token;
final IndexEntry entry = index.findExact(searchToken);
saveOnlyFirstSubentry = prefs.getBoolean(getString(R.string.saveOnlyFirstSubentryKey),
false);
clickOpensContextMenu = prefs.getBoolean(getString(R.string.clickOpensContextMenuKey),
- false);
+ !getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN));
Log.d(LOG, "wordList=" + wordList + ", saveOnlyFirstSubentry=" + saveOnlyFirstSubentry);
onCreateSetupActionBarAndSearchView();
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
customSearchView.setLayoutParams(layoutParams);
- listView.setOnItemClickListener(new OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int row, long id) {
- onListItemClick(getListView(), view, row, id);
- }
- });
-
languageButton = new ImageButton(customSearchView.getContext());
+ languageButton.setId(R.id.languageButton);
languageButton.setScaleType(ScaleType.FIT_CENTER);
languageButton.setOnClickListener(new OnClickListener() {
@Override
customSearchView.addView(languageButton, lpb);
searchView = new SearchView(getSupportActionBar().getThemedContext());
+ searchView.setId(R.id.searchView);
// Get rid of search icon, it takes up too much space.
// There is still text saying "search" in the search field.
// Avoid wasting space on large left inset
Toolbar tb = (Toolbar)customSearchView.getParent();
tb.setContentInsetsRelative(0, 0);
+
+ getListView().setNextFocusLeftId(R.id.searchView);
+ findViewById(R.id.floatSwapButton).setNextFocusRightId(R.id.languageButton);
+ languageButton.setNextFocusLeftId(R.id.floatSwapButton);
}
@Override
button.setOnClickListener(intentLauncher);
listView.addHeaderView(button);
+ listView.setItemsCanFocus(true);
listView.setAdapter(new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
- AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
+ final AdapterContextMenuInfo adapterContextMenuInfo = (AdapterContextMenuInfo) menuInfo;
final RowBase row = (RowBase) getListAdapter().getItem(adapterContextMenuInfo.position);
+ if (clickOpensContextMenu && (row instanceof HtmlEntry.Row ||
+ (row instanceof TokenRow && ((TokenRow)row).getIndexEntry().htmlEntries.size() > 0))) {
+ final List<HtmlEntry> html = row instanceof TokenRow ? ((TokenRow)row).getIndexEntry().htmlEntries : Collections.singletonList(((HtmlEntry.Row)row).getEntry());
+ final String highlight = row instanceof HtmlEntry.Row ? ((HtmlEntry.Row)row).getTokenRow(true).getToken() : null;
+ final MenuItem open = menu.add("Open");
+ open.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
+ public boolean onMenuItemClick(MenuItem item) {
+ showHtml(html, highlight);
+ return false;
+ }
+ });
+ }
+
final android.view.MenuItem addToWordlist = menu.add(getString(R.string.addToWordList,
wordList.getName()));
addToWordlist
}
});
}
+ if (row instanceof PairEntry.Row && ttsReady) {
+ final List<Pair> pairs = ((PairEntry.Row)row).getEntry().pairs;
+ final MenuItem speakLeft = menu.add(R.string.speak_left);
+ speakLeft.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
+ @Override
+ public boolean onMenuItemClick(android.view.MenuItem item) {
+ int idx = index.swapPairEntries ? 1 : 0;
+ updateTTSLanguage(idx);
+ String text = "";
+ for (Pair p : pairs) text += p.get(idx);
+ text = text.replaceAll("\\{[^{}]*\\}", "").replace("{", "").replace("}", "");
+ textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH,
+ new HashMap<String, String>());
+ return false;
+ }
+ });
+ final MenuItem speakRight = menu.add(R.string.speak_right);
+ speakRight.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
+ @Override
+ public boolean onMenuItemClick(android.view.MenuItem item) {
+ int idx = index.swapPairEntries ? 0 : 1;
+ updateTTSLanguage(idx);
+ String text = "";
+ for (Pair p : pairs) text += p.get(idx);
+ text = text.replaceAll("\\{[^{}]*\\}", "").replace("{", "").replace("}", "");
+ textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH,
+ new HashMap<String, String>());
+ return false;
+ }
+ });
+ }
}
private void jumpToTextFromHyperLink(
// searchView.selectAll();
}
- protected void onListItemClick(ListView l, View v, int row, long id) {
+ protected void onListItemClick(ListView l, View v, int rowIdx, long id) {
defocusSearchText();
if (clickOpensContextMenu && dictRaf != null) {
openContextMenu(v);
+ } else {
+ final RowBase row = (RowBase)getListAdapter().getItem(rowIdx);
+ if (!(row instanceof PairEntry.Row)) {
+ v.performClick();
+ }
}
}
// IndexAdapter
// --------------------------------------------------------------------------
+ private void showHtml(final List<HtmlEntry> htmlEntries, final String htmlTextToHighlight) {
+ String html = HtmlEntry.htmlBody(htmlEntries, index.shortName);
+ // Log.d(LOG, "html=" + html);
+ startActivityForResult(
+ HtmlDisplayActivity.getHtmlIntent(getApplicationContext(), String.format(
+ "<html><head><meta name=\"viewport\" content=\"width=device-width\"></head><body>%s</body></html>", html),
+ htmlTextToHighlight, false),
+ 0);
+ }
+
static ViewGroup.LayoutParams WEIGHT_1 = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1.0f);
private void getMetrics() {
// Get the screen's density scale
- final float scale = getResources().getDisplayMetrics().density;
+ // The previous method getResources().getDisplayMetrics()
+ // used to occasionally trigger a null pointer exception,
+ // so try this instead.
+ DisplayMetrics dm = new DisplayMetrics();
+ getWindowManager().getDefaultDisplay().getMetrics(dm);
+ final float scale = dm.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);
// 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.setFocusable(false);
result.setLongClickable(true);
// result.setBackgroundResource(android.R.drawable.menuitem_background);
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(getApplicationContext(), String.format(
- "<html><head></head><body>%s</body></html>", html),
- htmlTextToHighlight, false),
- 0);
+ showHtml(htmlEntries, htmlTextToHighlight);
+ }
+ });
+ result.setOnClickListener(new OnClickListener() {
+ @Override
+ public void onClick(View v) {
+ textView.performClick();
}
});
}
+ result.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
return result;
}