From: thadh Date: Fri, 17 Apr 2009 07:29:02 +0000 (-0700) Subject: go X-Git-Url: http://gitweb.fperrin.net/?p=Dictionary.git;a=commitdiff_plain;h=0203ae094b137ad89eb8656afba661e708f1310d go --- diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 479231d..5b132d5 100755 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -1,17 +1,16 @@ - - - - - - - - - - + package="com.hughes.android.dictionary" android:versionCode="1" + android:versionName="1.0.0"> + + + + + + + + + + + \ No newline at end of file diff --git a/res/values/strings.xml b/res/values/strings.xml index 158d1cd..5234d14 100755 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -4,4 +4,8 @@ Dictionary Dictionary\nby Thad Hughes +wordListFile +dictFile + +dictFetchUrl diff --git a/res/xml/preferences.xml b/res/xml/preferences.xml new file mode 100755 index 0000000..6b6a119 --- /dev/null +++ b/res/xml/preferences.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index daa79dd..db459a9 100755 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -9,11 +9,14 @@ import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; +import android.app.AlertDialog; import android.app.ListActivity; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.Typeface; import android.os.Bundle; import android.os.Handler; +import android.preference.PreferenceManager; import android.text.Editable; import android.text.Spannable; import android.text.TextWatcher; @@ -44,32 +47,53 @@ import com.hughes.android.dictionary.Dictionary.LanguageData; import com.hughes.android.dictionary.Dictionary.Row; public class DictionaryActivity extends ListActivity { + + static final Intent aboutIntent = new Intent().setClassName(AboutActivity.class.getPackage().getName(), AboutActivity.class.getCanonicalName()); + static final Intent preferencesIntent = new Intent().setClassName(PreferenceActivity.class.getPackage().getName(), PreferenceActivity.class.getCanonicalName()); - private RandomAccessFile dictRaf = null; - private Dictionary dictionary = null; - private LanguageData activeLangaugeData = null; + 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"); - final Handler uiHandler = new Handler(); + // Can be null. + private File dictFile = null; + private RandomAccessFile dictRaf = null; + private Dictionary dictionary = null; + private LanguageData activeLangaugeData = null; - private Executor searchExecutor = Executors.newSingleThreadExecutor(); private SearchOperation searchOperation = null; - // private List entries = Collections.emptyList(); - private DictionaryListAdapter dictionaryListAdapter = new DictionaryListAdapter(); private int selectedRowIndex = -1; private int selectedTokenRowIndex = -1; - private final Intent aboutIntent = new Intent().setClassName(AboutActivity.class.getPackage().getName(), AboutActivity.class.getCanonicalName()); /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { Log.d("THAD", "onCreate"); super.onCreate(savedInstanceState); + } + + @Override + public void onResume() { + super.onResume(); + + closeCurrentDictionary(); + final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this); + wordList = new File(settings.getString(getResources().getString(R.string.wordListFile), wordList.getAbsolutePath())); + dictFile = new File(settings.getString(getResources().getString(R.string.dictFile), "/sdcard/de-en.dict")); + Log.d("THAD", "wordList=" + wordList); + Log.d("THAD", "dictFile=" + dictFile); + + if (!dictFile.canRead()) { + return; + } + try { - dictRaf = new RandomAccessFile("/sdcard/de-en.dict", "r"); + dictRaf = new RandomAccessFile(dictFile, "r"); dictionary = new Dictionary(dictRaf); activeLangaugeData = dictionary.languageDatas[Entry.LANG1]; } catch (Exception e) { @@ -82,8 +106,6 @@ public class DictionaryActivity extends ListActivity { setListAdapter(dictionaryListAdapter); - onSearchTextChange(""); - // Language button. final Button langButton = (Button) findViewById(R.id.LangButton); langButton.setOnClickListener(new OnClickListener() { @@ -146,6 +168,28 @@ public class DictionaryActivity extends ListActivity { return false; } })); + + onSearchTextChange(""); + } + + + @Override + public void onStop() { + super.onStop(); + closeCurrentDictionary(); + } + + private void closeCurrentDictionary() { + dictionary = null; + activeLangaugeData = null; + try { + if (dictRaf != null) { + dictRaf.close(); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + dictRaf = null; } public String getSelectedRowText() { @@ -171,6 +215,13 @@ public class DictionaryActivity extends ListActivity { return false; }}); + final MenuItem preferences = menu.add("Preferences..."); + preferences.setOnMenuItemClickListener(new OnMenuItemClickListener(){ + public boolean onMenuItemClick(final MenuItem menuItem) { + startActivity(preferencesIntent); + return false; + }}); + final MenuItem about = menu.add("About..."); about.setOnMenuItemClickListener(new OnMenuItemClickListener(){ public boolean onMenuItemClick(final MenuItem menuItem) { @@ -183,7 +234,10 @@ public class DictionaryActivity extends ListActivity { @Override public boolean onPrepareOptionsMenu(final Menu menu) { - switchLanguageMenuItem.setTitle(String.format("Switch to %s", dictionary.languageDatas[Entry.otherLang(activeLangaugeData.lang)].language.symbol)); + if (dictionary != null) { + switchLanguageMenuItem.setTitle(String.format("Switch to %s", dictionary.languageDatas[Entry.otherLang(activeLangaugeData.lang)].language.symbol)); + } + switchLanguageMenuItem.setEnabled(dictionary != null); return super.onPrepareOptionsMenu(menu); } @@ -221,7 +275,9 @@ public class DictionaryActivity extends ListActivity { out.write((rawText + "\n").getBytes()); out.close(); } catch (IOException e) { - throw new RuntimeException(e); + final AlertDialog alert = new AlertDialog.Builder(DictionaryActivity.this).create(); + alert.setMessage("Failed to append to file: " + wordList.getAbsolutePath()); + alert.show(); } return false; } @@ -306,7 +362,7 @@ public class DictionaryActivity extends ListActivity { private class DictionaryListAdapter extends BaseAdapter { public int getCount() { - return activeLangaugeData.rows.size(); + return dictionary != null ? activeLangaugeData.rows.size() : 0; } public Dictionary.Row getItem(int rowIndex) { @@ -330,7 +386,6 @@ public class DictionaryActivity extends ListActivity { } else { result = new TextView(parent.getContext()); } -// result.setBackgroundColor(Color.WHITE); result.setText(activeLangaugeData.rowToString(row)); result.setTextAppearance(parent.getContext(), android.R.style.TextAppearance_Large); @@ -345,9 +400,6 @@ public class DictionaryActivity extends ListActivity { final int rowCount = entry.getRowCount(); for (int r = 0; r < rowCount; ++r) { final TableRow tableRow = new TableRow(result.getContext()); -// if (r > 0) { -// tableRow.setBackgroundColor(Color.DKGRAY); -// } TextView column1 = new TextView(tableRow.getContext()); TextView column2 = new TextView(tableRow.getContext()); diff --git a/src/com/hughes/android/dictionary/PreferenceActivity.java b/src/com/hughes/android/dictionary/PreferenceActivity.java new file mode 100755 index 0000000..4863eaf --- /dev/null +++ b/src/com/hughes/android/dictionary/PreferenceActivity.java @@ -0,0 +1,11 @@ +package com.hughes.android.dictionary; + +import android.os.Bundle; + +public class PreferenceActivity extends android.preference.PreferenceActivity { + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + addPreferencesFromResource(R.xml.preferences); + } +} diff --git a/src/com/hughes/android/dictionary/R.java b/src/com/hughes/android/dictionary/R.java index 4ec8d9b..19ea99e 100755 --- a/src/com/hughes/android/dictionary/R.java +++ b/src/com/hughes/android/dictionary/R.java @@ -14,28 +14,34 @@ public final class R { public static final int icon=0x7f020000; } public static final class id { - public static final int DownButton=0x7f05000d; - public static final int ImageView01=0x7f050002; - public static final int LangButton=0x7f05000c; - public static final int LinearLayout01=0x7f050000; - public static final int LinearLayout02=0x7f050001; - public static final int SearchBarLinearLayout=0x7f050008; - public static final int SearchBarTableLayout=0x7f050009; - public static final int SearchBarTableRow=0x7f05000a; - public static final int SearchText=0x7f05000b; - public static final int UpButton=0x7f05000e; - public static final int author=0x7f050006; - public static final int copyright=0x7f050005; - public static final int email=0x7f050007; - public static final int space1=0x7f050004; - public static final int title=0x7f050003; + public static final int DownButton=0x7f06000d; + public static final int ImageView01=0x7f060002; + public static final int LangButton=0x7f06000c; + public static final int LinearLayout01=0x7f060000; + public static final int LinearLayout02=0x7f060001; + public static final int SearchBarLinearLayout=0x7f060008; + public static final int SearchBarTableLayout=0x7f060009; + public static final int SearchBarTableRow=0x7f06000a; + public static final int SearchText=0x7f06000b; + public static final int UpButton=0x7f06000e; + public static final int author=0x7f060006; + public static final int copyright=0x7f060005; + public static final int email=0x7f060007; + public static final int space1=0x7f060004; + public static final int title=0x7f060003; } public static final class layout { public static final int about=0x7f030000; public static final int main=0x7f030001; } public static final class string { - public static final int about_text=0x7f040001; - public static final int app_name=0x7f040000; + public static final int about_text=0x7f050001; + public static final int app_name=0x7f050000; + public static final int dictFetchUrl=0x7f050004; + public static final int dictFile=0x7f050003; + public static final int wordListFile=0x7f050002; + } + public static final class xml { + public static final int preferences=0x7f040000; } }