]> gitweb.fperrin.net Git - Dictionary.git/commitdiff
go
authorthadh <thadh@THADH-MTV.ad.corp.google.com>
Thu, 30 Jul 2009 22:00:24 +0000 (15:00 -0700)
committerthadh <thadh@THADH-MTV.ad.corp.google.com>
Thu, 30 Jul 2009 22:00:24 +0000 (15:00 -0700)
.classpath
AndroidManifest.xml
res/layout/about.xml
res/layout/main.xml
res/values/strings.xml
res/xml/preferences.xml
src/com/hughes/android/dictionary/Dictionary.java
src/com/hughes/android/dictionary/DictionaryActivity.java
src/com/hughes/android/dictionary/DownloadActivity.java
src/com/hughes/android/dictionary/Entry.java
src/com/hughes/android/dictionary/R.java [deleted file]

index cc507f464e79bc96d786c85b510e517f4880ebc4..323ccea1b446b9c4f0c4130397b3772e0f10c81e 100755 (executable)
@@ -3,5 +3,6 @@
        <classpathentry kind="src" path="src"/>\r
        <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>\r
        <classpathentry combineaccessrules="false" exported="true" kind="src" path="/Util"/>\r
+       <classpathentry kind="src" path="gen"/>\r
        <classpathentry kind="output" path="bin"/>\r
 </classpath>\r
index e67877a4ce4013fed61a3837fc448923a08ee082..368dd8070f2a0fd1cc16457af73ad5865274a83f 100755 (executable)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-       package="com.hughes.android.dictionary" android:versionCode="1"
-       android:versionName="1.0.0">
+       package="com.hughes.android.dictionary" android:versionCode="4"
+       android:versionName="1.1">
        
        <uses-sdk android:minSdkVersion="2" />
        <uses-permission android:name="android.permission.INTERNET"/>
@@ -14,9 +14,9 @@
                        </intent-filter>
                </activity>
 
-               <activity android:name="AboutActivity"></activity>
-               <activity android:name="PreferenceActivity"></activity>
-               <activity android:name="DownloadActivity"></activity>
+               <activity android:name="AboutActivity"/>
+               <activity android:name="PreferenceActivity"/>
+               <activity android:name="DownloadActivity"/>
        </application>
 
 </manifest> 
\ No newline at end of file
index 1bf8972cfa352d907c17ccf74a267d8b0d2d9611..29e070120af3159acc727b498aef5a3c80baf1ae 100755 (executable)
@@ -1,58 +1,82 @@
-<LinearLayout android:id="@+id/LinearLayout01"
-       android:layout_height="wrap_content" xmlns:android="http://schemas.android.com/apk/res/android"
-       android:orientation="vertical" android:layout_width="fill_parent">
-
-       <LinearLayout android:id="@+id/LinearLayout02"
-               android:layout_width="wrap_content" android:layout_height="wrap_content"
-               android:layout_gravity="center_horizontal">
-
-               <ImageView android:id="@+id/ImageView01"
-                       android:layout_width="wrap_content" android:layout_height="wrap_content"
-                       android:layout_gravity="center_horizontal" android:background="@drawable/icon"></ImageView>
-
-               <TextView android:id="@+id/title"
+<ScrollView android:id="@+id/ScrollView01"
+       xmlns:android="http://schemas.android.com/apk/res/android"
+       android:layout_width="wrap_content" 
+       android:layout_height="wrap_content">
+       
+<LinearLayout 
+       android:id="@+id/LinearLayout01"
+       android:orientation="vertical"
+       android:layout_width="fill_parent"
+       android:layout_height="wrap_content"
+       >
+
+       <LinearLayout 
+               android:id="@+id/LinearLayout02"
+               android:layout_width="wrap_content"
+               android:layout_height="wrap_content"
+               android:layout_gravity="center_horizontal"
+               android:paddingBottom="10dip">
+
+               <ImageView 
+                       android:id="@+id/ImageView01"
+                       android:layout_width="wrap_content" 
+                       android:layout_height="wrap_content"
+                       android:layout_gravity="center_horizontal"
+                       android:background="@drawable/icon"/>
+
+               <TextView 
+                       android:id="@+id/title" 
+                       android:text="@string/titleWithVersion"
                        android:textAppearance="?android:attr/textAppearanceLarge"
-                       android:layout_width="wrap_content" android:layout_height="wrap_content"
-                       android:layout_gravity="center_vertical|fill_vertical" android:text="QuickDic 1.0"></TextView>
-
+                       android:layout_width="wrap_content" 
+                       android:layout_height="wrap_content"
+                       android:layout_gravity="center_vertical|fill_vertical" />
        </LinearLayout>
 
-
-
-       <TextView android:text=" " android:id="@+id/space1"
-               android:layout_width="wrap_content" android:layout_height="wrap_content"
-               android:layout_gravity="center_horizontal"></TextView>
-
-       <TextView android:text="(c) 2009" android:id="@+id/copyright"
-               android:layout_width="wrap_content" android:layout_height="wrap_content"
-               android:layout_gravity="center_horizontal"></TextView>
-
-       <TextView android:text="by Thad Hughes" android:id="@+id/author"
-               android:layout_width="wrap_content" android:layout_height="wrap_content"
-               android:layout_gravity="center_horizontal"></TextView>
-
-       <TextView android:text=" " android:id="@+id/space3"
-               android:layout_width="wrap_content" android:layout_height="wrap_content"
-               android:layout_gravity="center_horizontal"></TextView>
-
-       <TextView android:layout_width="wrap_content"
-               android:layout_height="wrap_content" android:id="@+id/emailMe"
-               android:text="If you're using QuickDic, I'd love to hear from you.  Please send comments, suggestions, bug reports, or just a quick hello to:"></TextView>
-
-       <TextView android:id="@+id/email" android:layout_width="wrap_content"
-               android:layout_height="wrap_content" android:autoLink="email" android:clickable="true"
-               android:text="thad.hughes+quickdic@gmail.com" android:layout_gravity="left"></TextView>
-
-       <TextView android:text=" " android:id="@+id/space2"
-               android:layout_width="wrap_content" android:layout_height="wrap_content"
-               android:layout_gravity="center_horizontal"></TextView>
-
-       <TextView android:id="@+id/currentDictInfoTitle"
+       <TextView 
+               android:id="@+id/copyright"
+               android:text="(c) 2009" 
+               android:layout_width="wrap_content" 
+               android:layout_height="wrap_content"
+               android:layout_gravity="center_horizontal"/>
+
+       <TextView 
+               android:id="@+id/author"
+               android:text="Thad Hughes" 
+               android:layout_width="wrap_content" 
+               android:layout_height="wrap_content"
+               android:layout_gravity="center_horizontal"
+               android:paddingBottom="10dip"/>
+
+       <TextView 
+               android:id="@+id/emailMe"
+               android:layout_width="wrap_content"
+               android:layout_height="wrap_content" 
+               android:text="@string/contactMe"/>
+
+       <TextView 
+               android:id="@+id/email" 
+               android:text="@string/myEmail"
+               android:autoLink="email"
+               android:layout_width="wrap_content"
+               android:layout_height="wrap_content" 
+               android:clickable="true" 
+               android:layout_gravity="left"
+               android:paddingBottom="10dip"/>
+
+       <TextView 
+               android:id="@+id/currentDictInfoTitle"
                android:textAppearance="?android:attr/textAppearanceLarge"
-               android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Current dictionary info:"></TextView>
-
-       <TextView android:text="No dictionary loaded." android:id="@+id/currentDictInfo"
-               android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView>
+               android:layout_width="fill_parent" 
+               android:layout_height="wrap_content"
+               android:text="@string/currentDictInfo"/>
 
+       <TextView 
+               android:id="@+id/currentDictInfo"
+               android:text="@string/noDictLoaded" 
+               android:layout_width="fill_parent" 
+               android:layout_height="wrap_content"/>
 
 </LinearLayout>
+
+</ScrollView>
\ No newline at end of file
index 3e19caa48d4916ea63735e4d38cfbddc6af1e2a5..256ce67228ae13e9110a7683cb094dd82a83ba84 100755 (executable)
@@ -1,18 +1,54 @@
 <?xml version="1.0" encoding="utf-8"?>
-<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+
+<LinearLayout 
+       xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
 
-       <LinearLayout android:layout_height="wrap_content" android:id="@+id/SearchBarLinearLayout" android:layout_width="wrap_content">
-
-
-</LinearLayout><TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/SearchBarTableLayout" android:stretchColumns="0">
-<TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/SearchBarTableRow"><EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/SearchText" android:hint="Search Text" android:singleLine="true"></EditText><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/LangButton" android:text="LANG"></Button><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/DownButton" android:text="v"></Button><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/UpButton" android:text="^"></Button></TableRow>
-</TableLayout>
-
-       <ListView android:id="@id/android:list"
-               android:layout_width="fill_parent" android:layout_height="fill_parent"
-               android:choiceMode="singleChoice" android:clickable="true" android:focusableInTouchMode="true" android:focusable="true"></ListView>
+       <LinearLayout 
+               android:id="@+id/SearchBarLinearLayout"
+               android:layout_height="wrap_content"
+               android:layout_width="fill_parent">
+               
+                       <EditText 
+                               android:id="@+id/SearchText"
+                               android:hint="@string/searchText"
+                               android:layout_width="wrap_content"
+                               android:layout_height="wrap_content" 
+                               android:inputType="text" 
+                               android:layout_weight="1.0"/>
+                               
+                       <Button 
+                               android:id="@+id/LangButton"
+                               android:text="LANG"
+                               android:minWidth="50dip"
+                               android:layout_width="wrap_content"
+                               android:layout_height="wrap_content"
+                               />
+                       <Button
+                               android:id="@+id/DownButton"
+                               android:text="v"
+                               android:minWidth="50dip"
+                               android:layout_width="wrap_content"
+                               android:layout_height="wrap_content" 
+                               />
+                       <Button 
+                               android:id="@+id/UpButton"
+                               android:text="^"
+                               android:minWidth="50dip"
+                               android:layout_width="wrap_content"
+                               android:layout_height="wrap_content" 
+                               />
+               
+       </LinearLayout>
 
+       <ListView 
+               android:id="@id/android:list" 
+               android:layout_width="fill_parent"
+               android:layout_height="fill_parent" 
+               android:choiceMode="singleChoice"
+               android:clickable="true" 
+               android:focusableInTouchMode="true"
+               android:focusable="true"/>
 
 </LinearLayout>
index 6ba160f82f961b3733c2c1ea6c1e408d300f0931..db1481a27b3f7d3ee0ee5f96aa49e00fe701c70f 100755 (executable)
@@ -1,15 +1,45 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
-    
-    <string name="app_name">QuickDic</string>
-<string name="about_text">QuickDic\nby Thad Hughes</string>
 
-<string name="wordListFileKey">wordListFile</string>
-<string name="wordListFile">/sdcard/wordList.txt</string>
+       <string name="app_name">QuickDic</string>
+       <string name="about_text">QuickDic\nby Thad Hughes</string>
+       
+       <!-- Main -->
+       <string name="searchText">Search Text</string>
+       <string name="unableToReadDictionaryFile">Unable to read dictionary file: %s</string>
+       <string name="downloadDictionary">Download dictionary...</string>
+       <string name="switchToLanguage">Switch to %s</string>
+       <string name="preferences">Preferences...</string>
+       <string name="about">About...</string>
+       <string name="addToWordList">Add to word list: %s</string>
 
-<string name="dictFileKey">dictFile</string>
-<string name="dictFile">/sdcard/de-en.dict</string>
+       <!-- About. -->
+       <string name="titleWithVersion">QuickDic 1.2</string>
+       <string name="thadHughes">Thad Hughes</string>
+       <string name="contactMe">If you're using QuickDic, I'd love to hear from you.  Please send comments, suggestions, bug reports, or just a quick hello to:</string>
+       <string name="myEmail">thad.hughes+quickdic@gmail.com</string>
+       <string name="currentDictInfo">Current dictionary info:</string>
+       <string name="noDictLoaded">No dictionary loaded.</string>
+       
+       <!-- Download. -->
+       <string name="downloading">Downloading, %d bytes so far</string>
+       <string name="downloadFinished">Download finished, %d bytes downloaded.</string>
+       <string name="errorDownloadingFile">"Error downloading file: \n%s"</string>
 
-<string name="dictFetchUrlKey">dictFetchUrl</string>
-<string name="dictFetchUrl">http://www.stanford.edu/~egirard/dict/de-en.dict</string>
+       <!-- Preferences -->
+       <string name="wordListFileKey">wordListFile</string>
+       <string name="wordListFileTitle">Word list file</string>
+       <string name="wordListFileSummary">The local file where the word list will be appended.</string>
+       <string name="wordListFileDefault">/sdcard/wordList.txt</string>
+
+       <string name="dictFileKey">dictFile</string>
+       <string name="dictFileTitle">Dictionary file</string>
+       <string name="dictFileSummary">The local filename where the dictionary is stored (and downloaded to).</string>
+       <string name="dictFileDefault">/sdcard/de-en.dict</string>
+
+       <string name="dictFetchUrlKey">dictFetchUrl</string>
+       <string name="dictFetchUrlTitle">Dictionary fetch URL</string>
+       <string name="dictFetchUrlSummary">URL to use to download the dictionary from the Internet.</string>
+       <string name="dictFetchUrlDefault">http://www.stanford.edu/~egirard/dict/de-en.dict</string>
+       
 </resources>
index f7a6feb97fca6d40ff6344fa65cb71bbc8ef924e..b43d391ba797bc62c74f4669b52c9db8c26a747b 100755 (executable)
@@ -1,7 +1,27 @@
 <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">\r
-       \r
-<EditTextPreference android:key="@string/dictFileKey" android:persistent="true" android:title="Dictionary file" android:summary="The local filename where the dictionary is stored." android:defaultValue="@string/dictFile"></EditTextPreference>
-<EditTextPreference android:key="@string/wordListFileKey" android:summary="The local file where the word list will be appended." android:title="Word list file" android:persistent="true" android:defaultValue="@string/wordListFile"></EditTextPreference>
-<EditTextPreference android:key="@string/dictFetchUrlKey" android:summary="URL to use to download the dictionary from the Internet." android:persistent="true" android:title="Dictionary fetch URL" android:defaultValue="@string/dictFetchUrl"></EditTextPreference>
+\r
+       <EditTextPreference\r
+           android:key="@string/wordListFileKey"\r
+               android:title="@string/wordListFileTitle" \r
+               android:summary="@string/wordListFileSummary"\r
+               android:defaultValue="@string/wordListFileDefault"\r
+               android:persistent="true"\r
+       />\r
+               \r
+       <EditTextPreference \r
+               android:key="@string/dictFileKey"\r
+               android:title="@string/dictFileTitle"\r
+               android:summary="@string/dictFileSummary"\r
+               android:defaultValue="@string/dictFileDefault"\r
+               android:persistent="true" \r
+               />\r
+               
+       <EditTextPreference \r
+           android:key="@string/dictFetchUrlKey"\r
+           android:title="@string/dictFetchUrlTitle"
+               android:summary="@string/dictFetchUrlSummary"\r
+               android:defaultValue="@string/dictFetchUrlDefault"
+               android:persistent="true"\r
+       />
 
 </PreferenceScreen>  
\ No newline at end of file
index 7fd38f530514997370f09e056ee804b15980b64a..cc31105d2438f1fb533a5e426376f9bd47162d66 100755 (executable)
@@ -99,6 +99,9 @@ public final class Dictionary implements RAFSerializable<Dictionary> {
           return mid;\r
         }\r
         final IndexEntry midEntry = sortedIndex.get(mid);\r
+        if (midEntry.word.equals("pre-print")) {\r
+          System.out.println();\r
+        }\r
 \r
         final int comp = language.sortComparator.compare(word, midEntry.word.toLowerCase());\r
         if (comp == 0) {\r
@@ -111,8 +114,10 @@ public final class Dictionary implements RAFSerializable<Dictionary> {
           }\r
           return result;\r
         } else if (comp < 0) {\r
+//          Log.d("THAD", "Upper bound: " + midEntry);\r
           end = mid;\r
         } else {\r
+//          Log.d("THAD", "Lower bound: " + midEntry);\r
           start = mid + 1;\r
         }\r
       }\r
@@ -134,7 +139,7 @@ public final class Dictionary implements RAFSerializable<Dictionary> {
       for (; r <= rowIndex; ++r) {\r
         rows.get(r).indexEntry = indexEntry;\r
       }\r
-      assert false && rows.get(indexEntry.startRow).isToken();\r
+      assert rows.get(indexEntry.startRow).isToken();\r
       return indexEntry;\r
     }\r
   }\r
index bc6ad406b74b5f57b192c407db800419bf0be33f..b1488312e5e68d8ab0d8f303fb1de45e9af9ee04 100755 (executable)
@@ -13,13 +13,16 @@ import java.util.concurrent.atomic.AtomicBoolean;
 
 import android.app.AlertDialog;
 import android.app.ListActivity;
+import android.content.Context;
 import android.content.DialogInterface;
 import android.content.Intent;
 import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
 import android.graphics.Typeface;
 import android.os.Bundle;
 import android.os.Handler;
 import android.preference.PreferenceManager;
+import android.text.ClipboardManager;
 import android.text.Editable;
 import android.text.Spannable;
 import android.text.TextWatcher;
@@ -51,24 +54,24 @@ import com.hughes.android.dictionary.Dictionary.Row;
 
 public class DictionaryActivity extends ListActivity {
 
-  String WORD_LIST_FILE; 
-  String DICT_FILE; 
-  String DICT_FETCH_URL; 
-  
   static final Intent preferencesIntent = new Intent().setClassName(PreferenceActivity.class.getPackage().getName(), PreferenceActivity.class.getCanonicalName());
+  
+  static final String LOG = "QuickDic";
+  static final String PREF_DICT_ACTIVE_LANG = "DICT_DIR_PREF";
+  static final String PREF_ACTIVE_SEARCH_TEXT = "ACTIVE_WORD_PREF";
 
   private final Handler uiHandler = new Handler();
   private final Executor searchExecutor = Executors.newSingleThreadExecutor();
   private final DictionaryListAdapter dictionaryListAdapter = new DictionaryListAdapter();
 
   // Never null.
-  private File wordList = new File("/sdcard/wordList.txt");
+  private File wordList;
 
   // Can be null.
   private File dictFile = null;
   private RandomAccessFile dictRaf = null;
   private Dictionary dictionary = null;
-  private LanguageData activeLangaugeData = null;
+  private LanguageData activeLanguageData = null;
 
   private SearchOperation searchOperation = null;
   private int selectedRowIndex = -1;
@@ -79,52 +82,15 @@ public class DictionaryActivity extends ListActivity {
   @Override
   public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
-    WORD_LIST_FILE = getResources().getString(R.string.wordListFileKey); 
-    DICT_FILE = getResources().getString(R.string.dictFileKey); 
-    DICT_FETCH_URL = getResources().getString(R.string.dictFetchUrlKey); 
-
-    Log.d("THAD", "onCreate");
-  }
-  
-  @Override
-  public void onResume() {
-    super.onResume();
-
-    // Have to close, because we might have downloaded a new copy of the dictionary.
-    closeCurrentDictionary();
-
-    final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
-    wordList = new File(settings.getString(WORD_LIST_FILE, wordList.getAbsolutePath()));
-    final File newDictFile = new File(settings.getString(DICT_FILE, "/sdcard/de-en.dict"));
-    dictFile = newDictFile;
-    Log.d("THAD", "wordList=" + wordList);
-    Log.d("THAD", "dictFile=" + dictFile);
-
-    if (!dictFile.canRead()) {
-      dictionaryListAdapter.notifyDataSetChanged();
-      Log.d("THAD", "Unable to read dictionary file.");
-      final AlertDialog alert = new AlertDialog.Builder(DictionaryActivity.this).create();
-      alert.setMessage("Unable to read dictionary file: " + dictFile.getAbsolutePath());
-      alert.setButton("Download dictionary", new DialogInterface.OnClickListener() {
-        public void onClick(DialogInterface dialog, int which) {
-          startDownloadDictActivity();
-        }});
-      alert.show();
-      return;
-    }
+    Log.d(LOG, "onCreate");
     
-    try {
-      dictRaf = new RandomAccessFile(dictFile, "r");
-      dictionary = new Dictionary(dictRaf);
-      activeLangaugeData = dictionary.languageDatas[Entry.LANG1];
-      dictionaryListAdapter.notifyDataSetChanged();
-    } catch (Exception e) {
-      throw new RuntimeException(e);
+    if (Language.EN.sortCollator.compare("preppy", "pre-print") >= 0) {
+      Log.e(LOG, "Android java.text.Collator is buggy, lookups may not work properly.");
     }
-
+    
     setContentView(R.layout.main);
 
-    getSearchText().addTextChangedListener(new DictionaryTextWatcher());
+    getSearchText().addTextChangedListener(new SearchTextWatcher());
 
     setListAdapter(dictionaryListAdapter);
 
@@ -134,7 +100,6 @@ public class DictionaryActivity extends ListActivity {
       public void onClick(View v) {
         switchLanguage();
       }});
-    updateLangButton();
 
     final Button upButton = (Button) findViewById(R.id.UpButton);
     upButton.setOnClickListener(new OnClickListener() {
@@ -143,30 +108,31 @@ public class DictionaryActivity extends ListActivity {
           return;
         }
         final int destRowIndex;
-        final Row tokenRow = activeLangaugeData.rows.get(selectedTokenRowIndex);
+        final Row tokenRow = activeLanguageData.rows.get(selectedTokenRowIndex);
         assert tokenRow.isToken();
         final int prevTokenIndex = tokenRow.getIndex() - 1;
         if (selectedRowIndex == selectedTokenRowIndex && selectedRowIndex > 0) {
-          destRowIndex = activeLangaugeData.sortedIndex.get(prevTokenIndex).startRow;
+          destRowIndex = activeLanguageData.sortedIndex.get(prevTokenIndex).startRow;
         } else {
           destRowIndex = selectedTokenRowIndex;
         }
         jumpToRow(destRowIndex);
       }});
+    
     final Button downButton = (Button) findViewById(R.id.DownButton);
     downButton.setOnClickListener(new OnClickListener() {
       public void onClick(View v) {
         if (dictionary == null) {
           return;
         }
-        final Row tokenRow = activeLangaugeData.rows.get(selectedTokenRowIndex);
+        final Row tokenRow = activeLanguageData.rows.get(selectedTokenRowIndex);
         assert tokenRow.isToken();
         final int nextTokenIndex = tokenRow.getIndex() + 1;
         final int destRowIndex;
-        if (nextTokenIndex < activeLangaugeData.sortedIndex.size()) {
-          destRowIndex = activeLangaugeData.sortedIndex.get(nextTokenIndex).startRow;
+        if (nextTokenIndex < activeLanguageData.sortedIndex.size()) {
+          destRowIndex = activeLanguageData.sortedIndex.get(nextTokenIndex).startRow;
         } else {
-          destRowIndex = activeLangaugeData.rows.size() - 1;
+          destRowIndex = activeLanguageData.rows.size() - 1;
         }
         jumpToRow(destRowIndex);
       }});
@@ -178,9 +144,12 @@ public class DictionaryActivity extends ListActivity {
     getListView().setOnItemSelectedListener(new OnItemSelectedListener() {
       public void onItemSelected(AdapterView<?> arg0, View arg1, int rowIndex,
           long arg3) {
-        Log.d("THAD", "onItemSelected: " + rowIndex);
+        if (activeLanguageData == null) {
+          return;
+        }
+        Log.d(LOG, "onItemSelected: " + rowIndex);        
         selectedRowIndex = rowIndex;
-        selectedTokenRowIndex = activeLangaugeData.getIndexEntryForRow(rowIndex).startRow;
+        selectedTokenRowIndex = activeLanguageData.getIndexEntryForRow(rowIndex).startRow;
         updateSearchText();
       }
 
@@ -197,7 +166,62 @@ public class DictionaryActivity extends ListActivity {
       }
     }));
 
-    onSearchTextChange("");
+  }
+  
+  @Override
+  public void onResume() {
+    super.onResume();
+
+    // Have to close, because we might have downloaded a new copy of the dictionary.
+    closeCurrentDictionary();
+
+    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+    wordList = new File(prefs.getString(getString(R.string.wordListFileKey), getString(R.string.wordListFileDefault)));
+    final File newDictFile = new File(prefs.getString(getString(R.string.dictFileKey), getString(R.string.dictFileDefault)));
+    dictFile = newDictFile;
+    Log.d(LOG, "wordList=" + wordList);
+    Log.d(LOG, "dictFile=" + dictFile);
+
+    if (!dictFile.canRead()) {
+      dictionaryListAdapter.notifyDataSetChanged();
+      Log.d(LOG, "Unable to read dictionary file.");
+      final AlertDialog alert = new AlertDialog.Builder(DictionaryActivity.this).create();
+      alert.setMessage(String.format(getString(R.string.unableToReadDictionaryFile), dictFile.getAbsolutePath()));
+      alert.setButton(getString(R.string.downloadDictionary), new DialogInterface.OnClickListener() {
+        public void onClick(DialogInterface dialog, int which) {
+          startDownloadDictActivity();
+        }});
+      alert.show();
+      return;
+    }
+    
+    final byte lang = prefs.getInt(PREF_DICT_ACTIVE_LANG, Entry.LANG1) == Entry.LANG1 ? Entry.LANG1 : Entry.LANG2;
+      
+    try {
+      dictRaf = new RandomAccessFile(dictFile, "r");
+      dictionary = new Dictionary(dictRaf);
+      activeLanguageData = dictionary.languageDatas[lang];
+      dictionaryListAdapter.notifyDataSetChanged();
+    } catch (Exception e) {
+      throw new RuntimeException(e);
+    }
+
+    updateLangButton();
+
+    final String searchText = prefs.getString(PREF_ACTIVE_SEARCH_TEXT, "");
+    getSearchText().setText(searchText);
+    onSearchTextChange(searchText);
+  }
+  
+  @Override
+  public void onPause() {
+    super.onPause();
+    if (activeLanguageData != null) {
+      final Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
+      prefs.putInt(PREF_DICT_ACTIVE_LANG, activeLanguageData.lang);
+      prefs.putString(PREF_ACTIVE_SEARCH_TEXT, getSearchText().getText().toString());
+      prefs.commit();
+    }
   }
   
   
@@ -209,7 +233,7 @@ public class DictionaryActivity extends ListActivity {
 
   private void closeCurrentDictionary() {
     dictionary = null;
-    activeLangaugeData = null;
+    activeLanguageData = null;
     try {
       if (dictRaf != null) {
         dictRaf.close();
@@ -221,7 +245,7 @@ public class DictionaryActivity extends ListActivity {
   }
   
   public String getSelectedRowRawText() {
-    return activeLangaugeData.rowToString(activeLangaugeData.rows.get(selectedRowIndex));
+    return activeLanguageData.rowToString(activeLanguageData.rows.get(selectedRowIndex));
   }
   
   public EditText getSearchText() {
@@ -237,27 +261,27 @@ public class DictionaryActivity extends ListActivity {
 
   @Override
   public boolean onCreateOptionsMenu(final Menu menu) {
-    switchLanguageMenuItem = menu.add("Switch to language.");
+    switchLanguageMenuItem = menu.add(getString(R.string.switchToLanguage));
     switchLanguageMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
       public boolean onMenuItemClick(final MenuItem menuItem) {
         switchLanguage();
         return false;
       }});
 
-    final MenuItem preferences = menu.add("Preferences...");
+    final MenuItem preferences = menu.add(getString(R.string.preferences));
     preferences.setOnMenuItemClickListener(new OnMenuItemClickListener(){
       public boolean onMenuItemClick(final MenuItem menuItem) {
         startActivity(preferencesIntent);
         return false;
       }});
 
-    final MenuItem about = menu.add("About...");
+    final MenuItem about = menu.add(getString(R.string.about));
     about.setOnMenuItemClickListener(new OnMenuItemClickListener(){
       public boolean onMenuItemClick(final MenuItem menuItem) {
         final Intent intent = new Intent().setClassName(AboutActivity.class.getPackage().getName(), AboutActivity.class.getCanonicalName());
         final StringBuilder currentDictInfo = new StringBuilder();
         if (dictionary == null) {
-          currentDictInfo.append("No dictionary loaded.");
+          currentDictInfo.append(getString(R.string.noDictLoaded));
         } else {
           currentDictInfo.append(dictionary.dictionaryInfo).append("\n\n");
           currentDictInfo.append("Entry count: " + dictionary.entries.size()).append("\n");
@@ -273,7 +297,7 @@ public class DictionaryActivity extends ListActivity {
         return false;
       }});
 
-    final MenuItem download = menu.add("Download dictionary...");
+    final MenuItem download = menu.add(getString(R.string.downloadDictionary));
     download.setOnMenuItemClickListener(new OnMenuItemClickListener(){
       public boolean onMenuItemClick(final MenuItem menuItem) {
         startDownloadDictActivity();
@@ -286,7 +310,9 @@ public class DictionaryActivity extends ListActivity {
   @Override
   public boolean onPrepareOptionsMenu(final Menu menu) {
     if (dictionary != null) {
-      switchLanguageMenuItem.setTitle(String.format("Switch to %s", dictionary.languageDatas[Entry.otherLang(activeLangaugeData.lang)].language.symbol));
+      switchLanguageMenuItem.setTitle(String.format(
+          getString(R.string.switchToLanguage), dictionary.languageDatas[Entry
+              .otherLang(activeLanguageData.lang)].language.symbol));
     }
     switchLanguageMenuItem.setEnabled(dictionary != null);
     return super.onPrepareOptionsMenu(menu);
@@ -296,7 +322,7 @@ public class DictionaryActivity extends ListActivity {
     if (dictionary == null) {
       return;
     }
-    activeLangaugeData = dictionary.languageDatas[(activeLangaugeData == dictionary.languageDatas[0]) ? 1 : 0];
+    activeLanguageData = dictionary.languageDatas[(activeLanguageData == dictionary.languageDatas[0]) ? 1 : 0];
     selectedRowIndex = 0;
     selectedTokenRowIndex = 0;
     updateLangButton();
@@ -306,7 +332,7 @@ public class DictionaryActivity extends ListActivity {
   
   void updateLangButton() {
     final Button langButton = (Button) findViewById(R.id.LangButton);
-    langButton.setText(activeLangaugeData.language.symbol);
+    langButton.setText(activeLanguageData.language.symbol);
   }
   
   // ----------------------------------------------------------------
@@ -319,22 +345,23 @@ public class DictionaryActivity extends ListActivity {
     if (selectedRowIndex == -1) {
       return;
     }
-    final MenuItem addToWordlist = menu.add("Add to wordlist: " + wordList.getName());
+    
+    final MenuItem addToWordlist = menu.add(String.format(getString(R.string.addToWordList), wordList.getName()));
     addToWordlist.setOnMenuItemClickListener(new OnMenuItemClickListener() {
       public boolean onMenuItemClick(MenuItem item) {
         final StringBuilder rawText = new StringBuilder();
-        final String word = activeLangaugeData.getIndexEntryForRow(selectedRowIndex).word;
+        final String word = activeLanguageData.getIndexEntryForRow(selectedRowIndex).word;
         rawText.append(new SimpleDateFormat("yyyy.MM.dd HH:mm:ss").format(new Date())).append("\t");
         rawText.append(word).append("\t");
         rawText.append(getSelectedRowRawText());
-        Log.d("THAD", "Writing : " + rawText);
+        Log.d(LOG, "Writing : " + rawText);
         try {
           wordList.getParentFile().mkdirs();
           final PrintWriter out = new PrintWriter(new FileWriter(wordList, true));
           out.println(rawText.toString());
           out.close();
         } catch (IOException e) {
-          Log.e("THAD", "Unable to append to " + wordList.getAbsolutePath(), e);
+          Log.e(LOG, "Unable to append to " + wordList.getAbsolutePath(), e);
           final AlertDialog alert = new AlertDialog.Builder(DictionaryActivity.this).create();
           alert.setMessage("Failed to append to file: " + wordList.getAbsolutePath());
           alert.show();
@@ -342,6 +369,19 @@ public class DictionaryActivity extends ListActivity {
         return false;
       }
     });
+
+    final MenuItem copy = menu.add(android.R.string.copy);
+    copy.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+      public boolean onMenuItemClick(MenuItem item) {
+        Log.d(LOG, "Copy.");
+        final StringBuilder result = new StringBuilder();
+        result.append(getSelectedRowRawText());
+        final ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
+        clipboardManager.setText(result.toString());
+        return false;
+      }
+    });
+
   }
   
   @Override
@@ -361,12 +401,12 @@ public class DictionaryActivity extends ListActivity {
   @Override
   protected void onListItemClick(ListView l, View v, int row, long id) {
     selectedRowIndex = row;
-    Log.d("THAD", "Clicked: " + getSelectedRowRawText());
+    Log.d(LOG, "Clicked: " + getSelectedRowRawText());
     openContextMenu(getListView());
   }
 
   void onSearchTextChange(final String searchText) {
-    Log.d("THAD", "onSearchTextChange: " + searchText);
+    Log.d(LOG, "onSearchTextChange: " + searchText);
     if (dictionary == null) {
       return;
     }
@@ -378,9 +418,9 @@ public class DictionaryActivity extends ListActivity {
   }
   
   private void jumpToRow(final int rowIndex) {
-    Log.d("THAD", "jumpToRow: " + rowIndex);
+    Log.d(LOG, "jumpToRow: " + rowIndex);
     selectedRowIndex = rowIndex;
-    selectedTokenRowIndex = activeLangaugeData.getIndexEntryForRow(rowIndex).startRow;
+    selectedTokenRowIndex = activeLanguageData.getIndexEntryForRow(rowIndex).startRow;
     getListView().setSelection(rowIndex);
     getListView().setSelected(true);  // TODO: is this doing anything?
     updateSearchText();
@@ -389,9 +429,9 @@ public class DictionaryActivity extends ListActivity {
   private void updateSearchText() {
     final EditText searchText = getSearchText();
     if (!searchText.hasFocus()) {
-      final String word = activeLangaugeData.getIndexEntryForRow(selectedRowIndex).word;
+      final String word = activeLanguageData.getIndexEntryForRow(selectedRowIndex).word;
       if (!word.equals(searchText.getText().toString())) {
-        Log.d("THAD", "updateSearchText: setText: " + word);
+        Log.d(LOG, "updateSearchText: setText: " + word);
         searchText.setText(word);
       }
     }
@@ -402,8 +442,8 @@ public class DictionaryActivity extends ListActivity {
         DownloadActivity.class.getPackage().getName(),
         DownloadActivity.class.getCanonicalName());
     final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(DictionaryActivity.this);
-    final String dictFetchUrl = settings.getString(DICT_FETCH_URL, getResources().getString(R.string.dictFetchUrl));
-    final String dictFileName = settings.getString(DICT_FILE, getResources().getString(R.string.dictFile));
+    final String dictFetchUrl = settings.getString(getString(R.string.dictFetchUrlKey), getString(R.string.dictFetchUrlDefault));
+    final String dictFileName = settings.getString(getString(R.string.dictFileKey), getString(R.string.dictFileDefault));
     intent.putExtra(DownloadActivity.SOURCE, dictFetchUrl);
     intent.putExtra(DownloadActivity.DEST, dictFileName);
     startActivity(intent);
@@ -418,13 +458,14 @@ public class DictionaryActivity extends ListActivity {
     }
 
     public void run() {
-      Log.d("THAD", "SearchOperation: " + searchText);
-      final int indexLocation = activeLangaugeData.lookup(searchText, interrupted);
+      Log.d(LOG, "SearchOperation: " + searchText);
+      final int indexLocation = activeLanguageData.lookup(searchText, interrupted);
       if (interrupted.get()) {
         return;
       }
-      final IndexEntry indexEntry = activeLangaugeData.sortedIndex
+      final IndexEntry indexEntry = activeLanguageData.sortedIndex
           .get(indexLocation);
+      Log.d(LOG, "SearchOperation completed: " + indexEntry.toString());
       uiHandler.post(new Runnable() {
         public void run() {
           jumpToRow(indexEntry.startRow);
@@ -436,12 +477,19 @@ public class DictionaryActivity extends ListActivity {
   private class DictionaryListAdapter extends BaseAdapter {
 
     public int getCount() {
-      return dictionary != null ? activeLangaugeData.rows.size() : 0;
+      if (dictionary == null) {
+        return 0;
+      }
+      return activeLanguageData.rows.size();
     }
 
     public Dictionary.Row getItem(int rowIndex) {
-      assert rowIndex < activeLangaugeData.rows.size();
-      return activeLangaugeData.rows.get(rowIndex);
+      final LanguageData activeLanguageData = DictionaryActivity.this.activeLanguageData;
+      if (activeLanguageData == null) {
+        return null;
+      }
+      assert rowIndex < activeLanguageData.rows.size();
+      return activeLanguageData.rows.get(rowIndex);
     }
 
     public long getItemId(int rowIndex) {
@@ -451,16 +499,19 @@ public class DictionaryActivity extends ListActivity {
     public View getView(final int rowIndex, final View convertView,
         final ViewGroup parent) {
       final Row row = getItem(rowIndex);
-      
+
       // Token row.
-      if (row.isToken()) {
+      if (row == null || row.isToken()) {
         TextView result = null;
         if (convertView instanceof TextView) {
           result = (TextView) convertView;
         } else {
           result = new TextView(parent.getContext());
         }
-        result.setText(activeLangaugeData.rowToString(row));
+        if (row == null) {
+          return result;
+        }
+        result.setText(activeLanguageData.rowToString(row));
         result.setTextAppearance(parent.getContext(),
             android.R.style.TextAppearance_Large);
         result.setClickable(false);
@@ -498,17 +549,17 @@ public class DictionaryActivity extends ListActivity {
         // column1.setTextAppearance(parent.getContext(), android.R.style.Text);
         
         // TODO: color words by gender
-        final String col1Text = entry.getAllText(activeLangaugeData.lang)[r]; 
+        final String col1Text = entry.getAllText(activeLanguageData.lang)[r]; 
         column1.setText(col1Text, TextView.BufferType.SPANNABLE);
         final Spannable col1Spannable = (Spannable) column1.getText();
         int startPos = 0;
-        final String token = activeLangaugeData.getIndexEntryForRow(rowIndex).word;
+        final String token = activeLanguageData.getIndexEntryForRow(rowIndex).word;
         while ((startPos = col1Text.indexOf(token, startPos)) != -1) {
           col1Spannable.setSpan(new StyleSpan(Typeface.BOLD), startPos, startPos + token.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
          startPos += token.length();
         }
         
-        column2.setText(entry.getAllText(Entry.otherLang(activeLangaugeData.lang))[r], TextView.BufferType.NORMAL);
+        column2.setText(entry.getAllText(Entry.otherLang(activeLanguageData.lang))[r], TextView.BufferType.NORMAL);
         
         result.addView(tableRow);
       }
@@ -517,7 +568,7 @@ public class DictionaryActivity extends ListActivity {
     }
   }  // DictionaryListAdapter
 
-  private class DictionaryTextWatcher implements TextWatcher {
+  private class SearchTextWatcher implements TextWatcher {
     public void afterTextChanged(final Editable searchText) {
       if (getSearchText().hasFocus()) {
         // If they were typing to cause the change, update the UI.
index 681f129eb15138afb249f169c9d4114642620b77..5723eef3ff247045d59036cae0e64d721b08f96d 100755 (executable)
@@ -65,7 +65,7 @@ public class DownloadActivity extends Activity {
       out = new FileOutputStream(destTmpFile);\r
     } catch (Exception e) {\r
       Log.e("THAD", "Error downloading file", e);\r
-      setDownloadStatus("Error downloading file: \n" + e.getLocalizedMessage());\r
+      setDownloadStatus(String.format(getString(R.string.errorDownloadingFile), e.getLocalizedMessage()));\r
       return;\r
     }\r
 \r
@@ -80,7 +80,7 @@ public class DownloadActivity extends Activity {
             out.write(bytes, 0, bytesRead);\r
             byteCount += bytesRead;\r
             if (count++ % 20 == 0) {\r
-              setDownloadStatus(String.format("Downloading: %d bytes so far",\r
+              setDownloadStatus(String.format(getString(R.string.downloading),\r
                   byteCount));\r
             }\r
           }\r
@@ -92,12 +92,11 @@ public class DownloadActivity extends Activity {
           } else {\r
             Log.d("THAD", "Stopped downloading file.");\r
           }\r
-          setDownloadStatus(String.format("Downloaded finished: %d bytes",\r
+          setDownloadStatus(String.format(getString(R.string.downloadFinished),\r
               byteCount));\r
         } catch (IOException e) {\r
           Log.e("THAD", "Error downloading file", e);\r
-          setDownloadStatus("Error downloading file: \n"\r
-              + e.getLocalizedMessage());\r
+          setDownloadStatus(String.format(getString(R.string.errorDownloadingFile), e.getLocalizedMessage()));\r
         }\r
       }\r
     };\r
index 3facbf5752e5fc8e7c98167bdc8d1f65e62d0243..b2f8c45261835bf91fe0dd4ccd620be6d698396f 100755 (executable)
@@ -92,7 +92,7 @@ public final class Entry implements RAFSerializable<Entry> {
     for (int i = 0; i < lang1.length; ++i) {\r
       result.append(i == 0 ? "" : " | ").append(lang1[i]);\r
     }\r
-    result.append(" :: ");\r
+    result.append("\t");\r
     for (int i = 0; i < lang2.length; ++i) {\r
       result.append(i == 0 ? "" : " | ").append(lang2[i]);\r
     }\r
diff --git a/src/com/hughes/android/dictionary/R.java b/src/com/hughes/android/dictionary/R.java
deleted file mode 100755 (executable)
index e7f2542..0000000
+++ /dev/null
@@ -1,65 +0,0 @@
-/* AUTO-GENERATED FILE.  DO NOT MODIFY.\r
- *\r
- * This class was automatically generated by the\r
- * aapt tool from the resource data it found.  It\r
- * should not be modified by hand.\r
- */\r
-\r
-package com.hughes.android.dictionary;\r
-\r
-public final class R {\r
-    public static final class attr {\r
-    }\r
-    public static final class drawable {\r
-        public static final int icon=0x7f020000;\r
-    }\r
-    public static final class id {\r
-        public static final int DownButton=0x7f06001b;\r
-        public static final int ImageView01=0x7f060002;\r
-        public static final int LangButton=0x7f06001a;\r
-        public static final int LinearLayout01=0x7f060000;\r
-        public static final int LinearLayout02=0x7f060001;\r
-        public static final int SearchBarLinearLayout=0x7f060016;\r
-        public static final int SearchBarTableLayout=0x7f060017;\r
-        public static final int SearchBarTableRow=0x7f060018;\r
-        public static final int SearchText=0x7f060019;\r
-        public static final int TextView01=0x7f06000d;\r
-        public static final int UpButton=0x7f06001c;\r
-        public static final int author=0x7f060006;\r
-        public static final int copyright=0x7f060005;\r
-        public static final int currentDictInfo=0x7f06000c;\r
-        public static final int currentDictInfoTitle=0x7f06000b;\r
-        public static final int dest=0x7f060013;\r
-        public static final int destLayout=0x7f060011;\r
-        public static final int destTitle=0x7f060012;\r
-        public static final int downloadProgressBar=0x7f060014;\r
-        public static final int downloadStatus=0x7f060015;\r
-        public static final int email=0x7f060009;\r
-        public static final int emailMe=0x7f060008;\r
-        public static final int source=0x7f060010;\r
-        public static final int sourceLayout=0x7f06000e;\r
-        public static final int sourceTitle=0x7f06000f;\r
-        public static final int space1=0x7f060004;\r
-        public static final int space2=0x7f06000a;\r
-        public static final int space3=0x7f060007;\r
-        public static final int title=0x7f060003;\r
-    }\r
-    public static final class layout {\r
-        public static final int about=0x7f030000;\r
-        public static final int download=0x7f030001;\r
-        public static final int main=0x7f030002;\r
-    }\r
-    public static final class string {\r
-        public static final int about_text=0x7f050001;\r
-        public static final int app_name=0x7f050000;\r
-        public static final int dictFetchUrl=0x7f050007;\r
-        public static final int dictFetchUrlKey=0x7f050006;\r
-        public static final int dictFile=0x7f050005;\r
-        public static final int dictFileKey=0x7f050004;\r
-        public static final int wordListFile=0x7f050003;\r
-        public static final int wordListFileKey=0x7f050002;\r
-    }\r
-    public static final class xml {\r
-        public static final int preferences=0x7f040000;\r
-    }\r
-}\r