From: thadh Date: Mon, 1 Oct 2012 17:40:08 +0000 (-0700) Subject: WebView links starting to work (still timing problem). X-Git-Url: http://gitweb.fperrin.net/?p=Dictionary.git;a=commitdiff_plain;h=f347fe0263dba56272d0d60bbe9d75541239d61b WebView links starting to work (still timing problem). --- diff --git a/src/com/hughes/android/dictionary/C.java b/src/com/hughes/android/dictionary/C.java index abf55f4..e2f03e0 100644 --- a/src/com/hughes/android/dictionary/C.java +++ b/src/com/hughes/android/dictionary/C.java @@ -18,10 +18,10 @@ public class C { static final String DICTIONARY_CONFIGS = "dictionaryConfigs2"; - static final String DICT_FILE = "dictFile"; - static final String INDEX_INDEX = "indexIndex"; - static final String SEARCH_TOKEN = "searchToken"; - static final String CAN_AUTO_LAUNCH_DICT = "canAutoLaunch"; + public static final String DICT_FILE = "dictFile"; + public static final String INDEX_INDEX = "indexIndex"; + public static final String SEARCH_TOKEN = "searchToken"; + public static final String CAN_AUTO_LAUNCH_DICT = "canAutoLaunch"; public static final String SHOW_LOCAL = "showLocal"; public static final String THANKS_FOR_UPDATING_VERSION = "thanksForUpdatingVersion"; diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index 1516ce4..afe2696 100644 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -103,8 +103,6 @@ public class DictionaryActivity extends ListActivity { static final String LOG = "QuickDic"; - private String initialSearchText; - DictionaryApplication application; File dictFile = null; @@ -409,9 +407,6 @@ public class DictionaryActivity extends ListActivity { finish(); startActivity(getIntent()); } - if (initialSearchText != null) { - setSearchText(initialSearchText, true); - } showKeyboard(); } @@ -419,6 +414,15 @@ public class DictionaryActivity extends ListActivity { protected void onPause() { super.onPause(); } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent result) { + super.onActivityResult(requestCode, resultCode, result); + if (result != null && result.hasExtra(C.SEARCH_TOKEN)) { + Log.d(LOG, "onActivityResult: " + result.toString()); + jumpToTextFromHyperLink(result.getStringExtra(C.SEARCH_TOKEN), indexIndex); + } + } private static void setDictionaryPrefs(final Context context, final File dictFile, final int indexIndex, final String searchToken) { @@ -600,7 +604,12 @@ public class DictionaryActivity extends ListActivity { dialog.show(); } - private void changeIndexGetFocusAndResearch(final int newIndex) { + private void changeIndexGetFocusAndResearch(int newIndex) { + Log.d(LOG, "Changing index to: " + newIndex); + if (newIndex == -1) { + Log.e(LOG, "Invalid index."); + newIndex = 0; + } indexIndex = newIndex; index = dictionary.indices.get(indexIndex); indexAdapter = new IndexAdapter(index); @@ -766,35 +775,7 @@ public class DictionaryActivity extends ListActivity { selectedSpannableText)); searchForSelection.setOnMenuItemClickListener(new OnMenuItemClickListener() { public boolean onMenuItemClick(MenuItem item) { - int indexToUse = -1; - for (int i = 0; i < dictionary.indices.size(); ++i) { - final Index index = dictionary.indices.get(i); - if (indexPrepFinished) { - System.out.println("Doing index lookup: on " + selectedText); - final IndexEntry indexEntry = index.findExact(selectedText); - if (indexEntry != null) { - final TokenRow tokenRow = index.rows.get(indexEntry.startRow) - .getTokenRow(false); - if (tokenRow != null && tokenRow.hasMainEntry) { - indexToUse = i; - break; - } - } - } else { - Log.w(LOG, "Skipping findExact on index " + index.shortName); - } - } - if (indexToUse == -1) { - indexToUse = selectedSpannableIndex; - } - final boolean changeIndex = indexIndex != indexToUse; - // If we're not changing index, we have to trigger search: - setSearchText(selectedText, !changeIndex); - if (changeIndex) { - changeIndexGetFocusAndResearch(indexToUse); - } - // Give focus back to list view because typing is done. - getListView().requestFocus(); + jumpToTextFromHyperLink(selectedText, selectedSpannableIndex); return false; } }); @@ -810,7 +791,38 @@ public class DictionaryActivity extends ListActivity { } }); } - + } + + private void jumpToTextFromHyperLink(final String selectedText, final int defaultIndexToUse) { + int indexToUse = -1; + for (int i = 0; i < dictionary.indices.size(); ++i) { + final Index index = dictionary.indices.get(i); + if (indexPrepFinished) { + System.out.println("Doing index lookup: on " + selectedText); + final IndexEntry indexEntry = index.findExact(selectedText); + if (indexEntry != null) { + final TokenRow tokenRow = index.rows.get(indexEntry.startRow) + .getTokenRow(false); + if (tokenRow != null && tokenRow.hasMainEntry) { + indexToUse = i; + break; + } + } + } else { + Log.w(LOG, "Skipping findExact on index " + index.shortName); + } + } + if (indexToUse == -1) { + indexToUse = defaultIndexToUse; + } + final boolean changeIndex = indexIndex != indexToUse; + // If we're not changing index, we have to trigger search: + setSearchText(selectedText, !changeIndex); + if (changeIndex) { + changeIndexGetFocusAndResearch(indexToUse); + } + // Give focus back to list view because typing is done. + getListView().requestFocus(); } @Override @@ -963,10 +975,10 @@ public class DictionaryActivity extends ListActivity { } } }, 20); - } - + private final void jumpToRow(final int row) { + Log.d(LOG, "jumpToRow: " + row); setSelection(row); getListView().setSelected(true); } @@ -1022,6 +1034,8 @@ public class DictionaryActivity extends ListActivity { searchFinished(SearchOperation.this); } }); + } else { + Log.d(LOG, "interrupted, skipping searchFinished."); } } catch (Exception e) { Log.e(LOG, "Failure during search (can happen during Activity close."); @@ -1248,9 +1262,10 @@ public class DictionaryActivity extends ListActivity { button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { - final String html = HtmlEntry.htmlBody(htmlEntries); - startActivity(HtmlDisplayActivity.getHtmlIntent(String.format( - "%s", html), htmlTextToHighlight, false)); + final String html = HtmlEntry.htmlBody(htmlEntries, index.shortName); + startActivityForResult( + HtmlDisplayActivity.getHtmlIntent(String.format("%s", html), htmlTextToHighlight, false), + 0); } }); tableRow.addView(button); diff --git a/src/com/hughes/android/dictionary/HtmlDisplayActivity.java b/src/com/hughes/android/dictionary/HtmlDisplayActivity.java index 0a65290..565bfce 100644 --- a/src/com/hughes/android/dictionary/HtmlDisplayActivity.java +++ b/src/com/hughes/android/dictionary/HtmlDisplayActivity.java @@ -73,8 +73,9 @@ public final class HtmlDisplayActivity extends Activity { } else { html = getIntent().getStringExtra(HTML); } - final WebView webView = (WebView) findViewById(R.id.webView); + final MyWebView webView = (MyWebView) findViewById(R.id.webView); webView.loadData(html, "text/html", "utf-8"); + webView.activity = this; final String textToHighlight = getIntent().getStringExtra(TEXT_TO_HIGHLIGHT); if (textToHighlight != null && !"".equals(textToHighlight)) { diff --git a/src/com/hughes/android/dictionary/MyWebView.java b/src/com/hughes/android/dictionary/MyWebView.java index 5f39697..17dc974 100644 --- a/src/com/hughes/android/dictionary/MyWebView.java +++ b/src/com/hughes/android/dictionary/MyWebView.java @@ -1,11 +1,21 @@ package com.hughes.android.dictionary; +import android.app.Activity; import android.content.Context; +import android.content.Intent; import android.util.AttributeSet; +import android.util.Log; import android.view.ContextMenu; import android.webkit.WebView; +import android.webkit.WebViewClient; + +import com.hughes.android.dictionary.engine.HtmlEntry; public class MyWebView extends WebView { + + static final String LOG = "MyWebView"; + + HtmlDisplayActivity activity; public MyWebView(Context context) { super(context); @@ -13,6 +23,25 @@ public class MyWebView extends WebView { public MyWebView(Context context, AttributeSet attrs) { super(context, attrs); + getSettings().setSupportZoom(true); + getSettings().setBuiltInZoomControls(true); + + final WebViewClient webViewClient = new WebViewClient() { + @Override + public boolean shouldOverrideUrlLoading(WebView view, String url) { + if (HtmlEntry.isQuickdicUrl(url)) { + Log.d(LOG, "Handling Quickdic URL: " + url); + final Intent result = new Intent(); + HtmlEntry.quickdicUrlToIntent(url, result); + activity.setResult(Activity.RESULT_OK, result); + activity.finish(); + return true; + } + // TODO Auto-generated method stub + return super.shouldOverrideUrlLoading(view, url); + } + }; + setWebViewClient(webViewClient); } @@ -21,4 +50,7 @@ public class MyWebView extends WebView { super.onCreateContextMenu(menu); } + + + } diff --git a/src/com/hughes/android/dictionary/engine/HtmlEntry.java b/src/com/hughes/android/dictionary/engine/HtmlEntry.java index 7e24a44..4412397 100644 --- a/src/com/hughes/android/dictionary/engine/HtmlEntry.java +++ b/src/com/hughes/android/dictionary/engine/HtmlEntry.java @@ -1,5 +1,9 @@ package com.hughes.android.dictionary.engine; +import android.content.Intent; + +import com.hughes.android.dictionary.C; +import com.hughes.util.StringUtil; import com.hughes.util.raf.RAFListSerializer; import com.hughes.util.raf.RAFSerializable; import com.ibm.icu.text.Transliterator; @@ -12,7 +16,7 @@ import java.util.regex.Pattern; public class HtmlEntry extends AbstractEntry implements RAFSerializable, Comparable { - // Both are HTML escaped already. + // Title is not HTML escaped. public final String title; public String html; @@ -112,7 +116,7 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable htmlEntries) { + public static String htmlBody(final List htmlEntries, final String indexShortName) { final StringBuilder result = new StringBuilder(); for (final HtmlEntry htmlEntry : htmlEntries) { + final String titleEscaped = StringUtil.escapeToPureHtmlUnicode(htmlEntry.title); result.append(String.format("

%s

\n(%s)\n

%s\n", - htmlEntry.title, htmlEntry.title, htmlEntry.entrySource.name, + formatQuickdicUrl(indexShortName, titleEscaped), titleEscaped, htmlEntry.entrySource.name, htmlEntry.html)); } return result.toString(); } + + public static String formatQuickdicUrl(final String indexShortName, final String text) { + assert !indexShortName.contains(":"); + return String.format("qd:%s:%s", indexShortName, text); + } + + public static boolean isQuickdicUrl(String url) { + return url.startsWith("qd:"); + } + + public static void quickdicUrlToIntent(final String url, final Intent intent) { + int firstColon = url.indexOf(":"); + if (firstColon == -1) return; + int secondColon = url.indexOf(":", firstColon + 1); + if (secondColon == -1) return; + intent.putExtra(C.SEARCH_TOKEN, url.substring(secondColon + 1)); + } } diff --git a/src/com/hughes/android/dictionary/engine/Language.java b/src/com/hughes/android/dictionary/engine/Language.java index a731319..cfceae9 100644 --- a/src/com/hughes/android/dictionary/engine/Language.java +++ b/src/com/hughes/android/dictionary/engine/Language.java @@ -124,6 +124,11 @@ public class Language { isoCodeToResources.put("LB", new LanguageResources("Luxembourgish", R.string.LB)); isoCodeToResources.put("MK", new LanguageResources("Macedonian", R.string.MK)); + isoCodeToResources.put("LO", new LanguageResources("Lao", R.string.LO)); + isoCodeToResources.put("ML", new LanguageResources("Malayalam", R.string.ML)); + isoCodeToResources.put("SL", new LanguageResources("Slovenian", R.string.SL)); + isoCodeToResources.put("TA", new LanguageResources("Tamil", R.string.TA)); + // Hack to allow lower-case ISO codes to work: for (final String isoCode : new ArrayList(isoCodeToResources.keySet())) { isoCodeToResources.put(isoCode.toLowerCase(), isoCodeToResources.get(isoCode));