]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
go
authorthadh <thadh@THADH-LAPTOP.ad.corp.google.com>
Tue, 15 Dec 2009 05:51:18 +0000 (21:51 -0800)
committerthadh <thadh@THADH-LAPTOP.ad.corp.google.com>
Tue, 15 Dec 2009 05:51:18 +0000 (21:51 -0800)
src/com/hughes/android/dictionary/DictionaryActivity.java
src/com/hughes/android/dictionary/Language.java
src/com/hughes/android/dictionary/NoDictionaryActivity.java

index 76a79970ecdbf26d55928142fac56447ac21dbf2..61061d57f1363b4432a450b33c0adbad591b77fb 100755 (executable)
@@ -6,6 +6,7 @@ import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.RandomAccessFile;
 import java.text.SimpleDateFormat;
+import java.util.Arrays;
 import java.util.Date;
 import java.util.concurrent.Executor;
 import java.util.concurrent.Executors;
@@ -48,6 +49,7 @@ import android.widget.Toast;
 import com.hughes.android.dictionary.Dictionary.IndexEntry;
 import com.hughes.android.dictionary.Dictionary.LanguageData;
 import com.hughes.android.dictionary.Dictionary.Row;
+import com.ibm.icu.text.Collator;
 
 public class DictionaryActivity extends ListActivity {
   
@@ -82,6 +84,24 @@ public class DictionaryActivity extends ListActivity {
   // Visible for testing.
   LanguageListAdapter languageList = null;
   private SearchOperation searchOperation = null;
+  
+  public DictionaryActivity() {
+
+    searchExecutor.execute(new Runnable() {
+      public void run() {
+        final long startMillis = System.currentTimeMillis();
+        for (final String lang : Arrays.asList("EN", "DE")) {
+          Language.lookup(lang).getFindCollator(); 
+          final Collator c = Language.lookup(lang).getSortCollator(); 
+          if (c.compare("pre-print", "preppy") >= 0) {
+            Log.e(LOG, c.getClass() + " is buggy, lookups may not work properly.");
+          }
+        }
+        Log.d(LOG, "Loading collators took:" + (System.currentTimeMillis() - startMillis));
+      }
+    });
+
+  }
 
   /** Called when the activity is first created. */
   @Override
@@ -89,12 +109,6 @@ public class DictionaryActivity extends ListActivity {
     super.onCreate(savedInstanceState);
     Log.d(LOG, "onCreate:" + this);
 
-    if (Language.EN.sortCollator.compare("pre-print", "preppy") >= 0) {
-      Log
-          .e(LOG,
-              Language.EN.sortCollator.getClass() + " is buggy, lookups may not work properly.");
-    }
-
     try {
       initDictionaryAndPrefs();
     } catch (Exception e) {
@@ -184,12 +198,14 @@ public class DictionaryActivity extends ListActivity {
     Log.d(LOG, "dictFile=" + dictFile);
     
     try {
-    if (!dictFile.canRead()) {
-      throw new IOException("Unable to read dictionary file.");
-    }
-    
-    dictRaf = new RandomAccessFile(dictFile, "r");
-    dictionary = new Dictionary(dictRaf);
+      if (!dictFile.canRead()) {
+        throw new IOException("Unable to read dictionary file.");
+      }
+      
+      dictRaf = new RandomAccessFile(dictFile, "r");
+      final long startMillis = System.currentTimeMillis();
+      dictionary = new Dictionary(dictRaf);
+      Log.d(LOG, "Read dictionary millis: " + (System.currentTimeMillis() - startMillis));
     } catch (IOException e) {
       Log.e(LOG, "Couldn't open dictionary.", e);
       this.startActivity(new Intent(this, NoDictionaryActivity.class));
@@ -248,9 +264,13 @@ public class DictionaryActivity extends ListActivity {
 
   private void closeCurrentDictionary() {
     Log.i(LOG, "closeCurrentDictionary");
+    if (dictionary == null) {
+      return;
+    }
     waitForSearchEnd();
     languageList = null;
     setListAdapter(null);
+    Log.d(LOG, "setListAdapter finished.");
     dictionary = null;
     try {
       if (dictRaf != null) {
@@ -684,6 +704,7 @@ public class DictionaryActivity extends ListActivity {
   void waitForSearchEnd() {
     synchronized (this) {
       while (searchOperation != null) {
+        Log.d(LOG, "waitForSearchEnd");
         try {
           this.wait();
         } catch (InterruptedException e) {
index 440a7d011be20d2032930d4d02b2b5f7ac66d478..d8fa3606e71eec80c52418353e78de6338c23d58 100755 (executable)
@@ -12,31 +12,25 @@ public class Language {
   final String symbol;\r
   final Locale locale;\r
 \r
-  final Collator sortCollator;\r
+  private Collator sortCollator;\r
   final Comparator<String> sortComparator;\r
 \r
-  final Collator findCollator;\r
+  private Collator findCollator;\r
   final Comparator<String> findComparator;\r
 \r
   public Language(final String symbol, final Locale locale) {\r
     this.symbol = symbol;\r
     this.locale = locale;\r
 \r
-    this.sortCollator = Collator.getInstance(locale);\r
-    this.sortCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
-    this.sortCollator.setStrength(Collator.IDENTICAL);\r
     this.sortComparator = new Comparator<String>() {\r
       public int compare(final String s1, final String s2) {\r
-        return sortCollator.compare(textNorm(s1), textNorm(s2));\r
+        return getSortCollator().compare(textNorm(s1), textNorm(s2));\r
       }\r
     };\r
 \r
-    this.findCollator = Collator.getInstance(locale);\r
-    this.findCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
-    this.findCollator.setStrength(Collator.SECONDARY);\r
     this.findComparator = new Comparator<String>() {\r
       public int compare(final String s1, final String s2) {\r
-        return findCollator.compare(textNorm(s1), textNorm(s2));\r
+        return getFindCollator().compare(textNorm(s1), textNorm(s2));\r
       }\r
     };\r
 \r
@@ -50,6 +44,24 @@ public class Language {
   public String toString() {\r
     return symbol;\r
   }\r
+  \r
+  synchronized Collator getFindCollator() {\r
+    if (findCollator == null) {\r
+      findCollator = Collator.getInstance(locale);\r
+      findCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
+      findCollator.setStrength(Collator.SECONDARY);\r
+    }\r
+    return findCollator;\r
+  }\r
+\r
+  synchronized Collator getSortCollator() {\r
+    if (sortCollator == null) {\r
+      sortCollator = Collator.getInstance(locale);\r
+      sortCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
+      sortCollator.setStrength(Collator.IDENTICAL);\r
+    }\r
+    return sortCollator;\r
+  }\r
 \r
   // ----------------------------------------------------------------\r
 \r
index bc8499f146bdb3c64a45b2ccf1b2fed47efd505d..f7fb10f73757c372e010ec261dc88e5f5810a2a2 100755 (executable)
@@ -49,9 +49,9 @@ public class NoDictionaryActivity extends Activity {
 \r
     final TextView statusText = (TextView) findViewById(R.id.statusTextId);\r
     if (!canReadDict) {\r
-      statusText.setText(String.format(getString(R.string.unableToReadDictionaryFile), dictFile));\r
+      statusText.setText(getString(R.string.unableToReadDictionaryFile, dictFile));\r
     } else {\r
-      statusText.setText(String.format(getString(R.string.unableToReadDictionaryFile), dictFile));\r
+      statusText.setText(getString(R.string.dictionaryFileExists));\r
     }\r
 \r
     final Button launchButton = (Button) findViewById(R.id.launchDict);\r