]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
WebView links starting to work (still timing problem).
authorthadh <thadh@dhcp-172-19-19-34.mtv.corp.google.com>
Mon, 1 Oct 2012 17:40:08 +0000 (10:40 -0700)
committerthadh <thadh@dhcp-172-19-19-34.mtv.corp.google.com>
Mon, 1 Oct 2012 17:40:08 +0000 (10:40 -0700)
src/com/hughes/android/dictionary/C.java
src/com/hughes/android/dictionary/DictionaryActivity.java
src/com/hughes/android/dictionary/HtmlDisplayActivity.java
src/com/hughes/android/dictionary/MyWebView.java
src/com/hughes/android/dictionary/engine/HtmlEntry.java
src/com/hughes/android/dictionary/engine/Language.java

index abf55f47e1bf7ef8cde94fd3a9bce5f6637e7a5f..e2f03e0d79836a920d66c8d5d5ec4813e60bf34d 100644 (file)
@@ -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";
index 1516ce4d5e69f626e18579de6d2ada76bc4867ed..afe2696e1afbf63524627b616644a69ded63645a 100644 (file)
@@ -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(
-                                "<html><head></head><body>%s</body></html>", html), htmlTextToHighlight, false));
+                        final String html = HtmlEntry.htmlBody(htmlEntries, index.shortName);
+                        startActivityForResult(
+                                HtmlDisplayActivity.getHtmlIntent(String.format("<html><head></head><body>%s</body></html>", html), htmlTextToHighlight, false),
+                                0);
                     }
                 });
                 tableRow.addView(button);
index 0a65290772756bb635372dd5a6cbf73ba0455bfe..565bfce14650feb52169fef89617599a8cdd61fc 100644 (file)
@@ -73,8 +73,9 @@ public final class HtmlDisplayActivity extends Activity {
     } else {\r
       html = getIntent().getStringExtra(HTML);\r
     }\r
-    final WebView webView = (WebView) findViewById(R.id.webView);\r
+    final MyWebView webView = (MyWebView) findViewById(R.id.webView);\r
     webView.loadData(html, "text/html", "utf-8");\r
+    webView.activity = this;\r
     \r
     final String textToHighlight = getIntent().getStringExtra(TEXT_TO_HIGHLIGHT);\r
     if (textToHighlight != null && !"".equals(textToHighlight)) {\r
index 5f396978af57b951ba8f0151c9ffb6bdd2d2fbc3..17dc974a6c778ca98e537a412a2f8661a9fbf869 100644 (file)
@@ -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);
     }
 
+    
+    
+
 }
index 7e24a441f49b307ed885a9bddcb0690a81680018..4412397e429f8b0bfde443fe2cc86a28d6017fd3 100644 (file)
@@ -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<HtmlEntry>, Comparable<HtmlEntry> {
   
-  // 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<HtmlEntr
     @Override
     public void print(PrintStream out) {
       final HtmlEntry entry = getEntry();
-      out.println("HtmlEntry (shortened): " + entry.title);
+      out.println("See also HtmlEntry:" + entry.title);
     }
 
     @Override
@@ -135,17 +139,34 @@ public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntr
       }
       return RowMatchType.BAG_OF_WORDS_MATCH;
     }
-    
   }
 
-    public static String htmlBody(final List<HtmlEntry> htmlEntries) {
+    public static String htmlBody(final List<HtmlEntry> 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("<h1><a href=\"%s\">%s</a></h1>\n(%s)\n<p>%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));
+    }
 
 }
index a731319473ad003a4a9bd42947b00b67f34f3b1d..cfceae9988d375de53af7e1f487ce2eee59b97cc 100644 (file)
@@ -124,6 +124,11 @@ public class Language {
     isoCodeToResources.put("LB", new LanguageResources("Luxembourgish", R.string.LB));\r
     isoCodeToResources.put("MK", new LanguageResources("Macedonian", R.string.MK));\r
 \r
+    isoCodeToResources.put("LO", new LanguageResources("Lao", R.string.LO));\r
+    isoCodeToResources.put("ML", new LanguageResources("Malayalam", R.string.ML));\r
+    isoCodeToResources.put("SL", new LanguageResources("Slovenian", R.string.SL));\r
+    isoCodeToResources.put("TA", new LanguageResources("Tamil", R.string.TA));\r
+\r
     // Hack to allow lower-case ISO codes to work:\r
     for (final String isoCode : new ArrayList<String>(isoCodeToResources.keySet())) {\r
       isoCodeToResources.put(isoCode.toLowerCase(), isoCodeToResources.get(isoCode));\r