From 4f9af3906020f72d890c213a33b77e1afef54959 Mon Sep 17 00:00:00 2001 From: thadh Date: Sat, 9 May 2009 09:34:47 -0700 Subject: [PATCH] go --- res/layout/about.xml | 22 +++-- res/layout/download.xml | 4 +- res/values/strings.xml | 4 +- .../hughes/android/dictionary/Dictionary.java | 6 +- .../dictionary/DictionaryActivity.java | 21 +++-- .../android/dictionary/DownloadActivity.java | 84 ++++++++++++------- src/com/hughes/android/dictionary/Entry.java | 9 +- .../hughes/android/dictionary/Language.java | 43 ++++++++-- src/com/hughes/android/dictionary/R.java | 42 +++++----- 9 files changed, 156 insertions(+), 79 deletions(-) diff --git a/res/layout/about.xml b/res/layout/about.xml index d6c3440..1bf8972 100755 --- a/res/layout/about.xml +++ b/res/layout/about.xml @@ -10,10 +10,10 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:background="@drawable/icon"> - + android:layout_gravity="center_vertical|fill_vertical" android:text="QuickDic 1.0"> @@ -31,20 +31,28 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> - + android:layout_gravity="center_horizontal"> + + + + - + android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Current dictionary info:"> + diff --git a/res/layout/download.xml b/res/layout/download.xml index 7b1e43b..a0edb5a 100755 --- a/res/layout/download.xml +++ b/res/layout/download.xml @@ -27,7 +27,7 @@ - + diff --git a/res/values/strings.xml b/res/values/strings.xml index 5234d14..da6dc83 100755 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -1,8 +1,8 @@ - Dictionary -Dictionary\nby Thad Hughes + QuickDic +QuickDic\nby Thad Hughes wordListFile dictFile diff --git a/src/com/hughes/android/dictionary/Dictionary.java b/src/com/hughes/android/dictionary/Dictionary.java index a80c874..8a68d6c 100755 --- a/src/com/hughes/android/dictionary/Dictionary.java +++ b/src/com/hughes/android/dictionary/Dictionary.java @@ -23,16 +23,19 @@ public final class Dictionary implements RAFSerializable { static final RAFSerializer INDEX_ENTRY_SERIALIZER = new RAFSerializableSerializer( IndexEntry.RAF_FACTORY); + final String dictionaryInfo; final List entries; final LanguageData[] languageDatas = new LanguageData[2]; - public Dictionary(final Language language0, final Language language1) { + public Dictionary(final String dictionaryInfo, final Language language0, final Language language1) { + this.dictionaryInfo = dictionaryInfo; languageDatas[0] = new LanguageData(this, language0, Entry.LANG1); languageDatas[1] = new LanguageData(this, language1, Entry.LANG2); entries = new ArrayList(); } public Dictionary(final RandomAccessFile raf) throws IOException { + dictionaryInfo = raf.readUTF(); entries = CachingList.create(FileList.create(raf, ENTRY_SERIALIZER, raf .getFilePointer()), 10000); languageDatas[0] = new LanguageData(this, raf, Entry.LANG1); @@ -40,6 +43,7 @@ public final class Dictionary implements RAFSerializable { } public void write(RandomAccessFile raf) throws IOException { + raf.writeUTF(dictionaryInfo); FileList.write(raf, entries, ENTRY_SERIALIZER); languageDatas[0].write(raf); languageDatas[1].write(raf); diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index c6d3972..fb0b5fd 100755 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -84,8 +84,6 @@ public class DictionaryActivity extends ListActivity { DICT_FETCH_URL = getResources().getString(R.string.dictFetchUrl); Log.d("THAD", "onCreate"); - - } @Override @@ -106,8 +104,8 @@ public class DictionaryActivity extends ListActivity { 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: " + wordList.getAbsolutePath()); - alert.setButton("Download dictionary from Internet", new DialogInterface.OnClickListener() { + alert.setMessage("Unable to read dictionary file: " + dictFile.getAbsolutePath()); + alert.setButton("Download dictionary", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { startDownloadDictActivity(); }}); @@ -141,6 +139,9 @@ public class DictionaryActivity extends ListActivity { final Button upButton = (Button) findViewById(R.id.UpButton); upButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { + if (dictionary == null) { + return; + } final int destRowIndex; final Row tokenRow = activeLangaugeData.rows.get(selectedTokenRowIndex); assert tokenRow.isToken(); @@ -155,6 +156,9 @@ public class DictionaryActivity extends ListActivity { 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); assert tokenRow.isToken(); final int nextTokenIndex = tokenRow.getIndex() + 1; @@ -255,6 +259,7 @@ public class DictionaryActivity extends ListActivity { if (dictionary == null) { currentDictInfo.append("No dictionary loaded."); } else { + currentDictInfo.append(dictionary.dictionaryInfo).append("\n\n"); currentDictInfo.append("Entry count: " + dictionary.entries.size()).append("\n"); for (int i = 0; i < 2; ++i) { final LanguageData languageData = dictionary.languageDatas[i]; @@ -288,6 +293,9 @@ public class DictionaryActivity extends ListActivity { } void switchLanguage() { + if (dictionary == null) { + return; + } activeLangaugeData = dictionary.languageDatas[(activeLangaugeData == dictionary.languageDatas[0]) ? 1 : 0]; selectedRowIndex = 0; selectedTokenRowIndex = 0; @@ -359,6 +367,9 @@ public class DictionaryActivity extends ListActivity { void onSearchTextChange(final String searchText) { Log.d("THAD", "onSearchTextChange: " + searchText); + if (dictionary == null) { + return; + } if (searchOperation != null) { searchOperation.interrupted.set(true); } @@ -475,7 +486,7 @@ public class DictionaryActivity extends ListActivity { tableRow.addView(spacer); } tableRow.addView(column1, layoutParams); - if (r>0){ + if (r > 0) { final TextView spacer = new TextView(tableRow.getContext()); spacer.setText(r == 0 ? "• " : " • "); tableRow.addView(spacer); diff --git a/src/com/hughes/android/dictionary/DownloadActivity.java b/src/com/hughes/android/dictionary/DownloadActivity.java index 385933b..b580c4d 100755 --- a/src/com/hughes/android/dictionary/DownloadActivity.java +++ b/src/com/hughes/android/dictionary/DownloadActivity.java @@ -1,5 +1,10 @@ package com.hughes.android.dictionary; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; import java.util.concurrent.Executor; import java.util.concurrent.Executors; @@ -8,7 +13,6 @@ import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.util.Log; -import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; @@ -19,7 +23,7 @@ public class DownloadActivity extends Activity { String source; String dest; - + private final Executor downloadExecutor = Executors.newSingleThreadExecutor(); private final Handler uiHandler = new Handler(); @@ -34,10 +38,10 @@ public class DownloadActivity extends Activity { throw new RuntimeException("null source or dest."); } setContentView(R.layout.download); - + final TextView sourceTextView = (TextView) findViewById(R.id.source); sourceTextView.setText(source); - + final TextView destTextView = (TextView) findViewById(R.id.dest); destTextView.setText(dest); @@ -45,37 +49,55 @@ public class DownloadActivity extends Activity { progressBar.setIndeterminate(false); progressBar.setMax(100); + final InputStream in; + final FileOutputStream out; + + final File destFile = new File(dest); + final File destTmpFile; + try { + destTmpFile = File.createTempFile("dictionaryDownload", "tmp", destFile.getParentFile()); + final URL uri = new URL(source); + in = uri.openStream(); + out = new FileOutputStream(destTmpFile); + } catch (Exception e) { + Log.e("THAD", "Error downloading file", e); + setDownloadStatus("Error downloading file: \n" + e.getLocalizedMessage()); + return; + } + final Runnable runnable = new Runnable() { public void run() { - - for (int i = 0; i < 100; ++i) { - - final int progress = i; - uiHandler.post(new Runnable() { - public void run() { - Log.d("THAD", "Setting progress: " + progress); - progressBar.setProgress(progress); - } - }); - - try { - Thread.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); + try { + long byteCount = 0; + int bytesRead; + final byte[] bytes = new byte[4096]; + while ((bytesRead = in.read(bytes)) != -1) { + out.write(bytes, 0, bytesRead); + byteCount += bytesRead; + setDownloadStatus(String.format("Downloading: %d bytes so far", byteCount)); } + in.close(); + out.close(); + destFile.delete(); + destTmpFile.renameTo(destFile); + setDownloadStatus(String.format("Downloaded finished: %d bytes", byteCount)); + } catch (IOException e) { + Log.e("THAD", "Error downloading file", e); + setDownloadStatus("Error downloading file: \n" + e.getLocalizedMessage()); } - - final TextView downloadComplete = (TextView) findViewById(R.id.downloadComplete); - uiHandler.post(new Runnable() { - public void run() { - progressBar.setProgress(100); - downloadComplete.setVisibility(View.VISIBLE); - } - }); - - }}; + } + }; + downloadExecutor.execute(runnable); - } - + + private void setDownloadStatus(final String status) { + final TextView downloadStatus = (TextView) findViewById(R.id.downloadStatus); + uiHandler.post(new Runnable() { + public void run() { + downloadStatus.setText(status); + } + }); + } + } diff --git a/src/com/hughes/android/dictionary/Entry.java b/src/com/hughes/android/dictionary/Entry.java index cd62f06..6143b49 100755 --- a/src/com/hughes/android/dictionary/Entry.java +++ b/src/com/hughes/android/dictionary/Entry.java @@ -140,13 +140,18 @@ public final class Entry implements RAFSerializable { public Set getIndexableTokens(final byte lang) { final Set result = new LinkedHashSet(); - String text = Arrays.asList(getAllText(lang)).toString(); + String text = " "; + for (final String subentry : getAllText(lang)) { + text += subentry + " "; + } text = text.replaceAll("fig\\.", " "); text = text.replaceAll("\\{[^\\}]+}", " "); text = text.replaceAll("\"-", "-"); text = text.replaceAll("-\"", "-"); - text = text.replaceAll("[\":/\\()<>\\[\\],;?!.]", " "); + text = text.replaceAll("[\"/\\()<>\\[\\],;?!.]", " "); + text = text.replaceAll("[:] ", " "); + text = text.replaceAll(" [:]", " "); result.addAll(Arrays.asList(WHITESPACE.split(text))); text = text.replaceAll("[-]", " "); diff --git a/src/com/hughes/android/dictionary/Language.java b/src/com/hughes/android/dictionary/Language.java index 947eba1..122372d 100755 --- a/src/com/hughes/android/dictionary/Language.java +++ b/src/com/hughes/android/dictionary/Language.java @@ -21,7 +21,7 @@ public abstract class Language { if (c != 0) { return c; } - return StringUtil.reverse(s1).compareTo(StringUtil.reverse(s2)); + return StringUtil.flipCase(StringUtil.reverse(s1)).compareTo(StringUtil.flipCase(StringUtil.reverse(s2))); }}; } @@ -34,23 +34,48 @@ public abstract class Language { // ---------------------------------------------------------------- + + static final String normalizeTokenForSort(final String token, final boolean vowelETranslation) { + final StringBuilder result = new StringBuilder(); + for (int i = 0; i < token.length(); ++i) { + Character c = token.charAt(i); + c = Character.toLowerCase(c); + // only check for lowercase 'e' in subsequent position means don't treat acronyms as umlauted: SAE. + if (vowelETranslation && (c == 'a' || c == 'o' || c == 'u') && i + 1 < token.length() && token.charAt(i + 1) == 'e') { + if (c == 'a') { + result.append('ä'); + } else if (c == 'o') { + result.append('ö'); + } else if (c == 'u') { + result.append('ü'); + } + ++i; + } else if (c >= 'a' && c <= 'z' || c >= '0' && c <= '9') { + result.append(c); + } else if (c == 'ß') { + result.append("ss"); + } else if (c == 'ä') { + result.append(c); + } else if (c == 'ö') { + result.append(c); + } else if (c == 'ü') { + result.append(c); + } + } + return result.toString(); + } public static final Language EN = new Language("EN") { @Override public String normalizeTokenForSort(final String token) { - return token.toLowerCase().replaceAll("ß", "ss").replaceAll("ä", "a") - .replaceAll("ö", "o").replaceAll("ü", "u").replaceAll("[^A-Za-z0-9]", - ""); + return Language.normalizeTokenForSort(token, false); } }; - + public static final Language DE = new Language("DE") { @Override String normalizeTokenForSort(final String token) { - return token.toLowerCase().replaceAll("ß", "ss").replaceAll("ä", "a") - .replaceAll("ae", "a").replaceAll("ö", "o").replaceAll("oe", "o") - .replaceAll("ü", "u").replaceAll("ue", "u").replaceAll( - "[^A-Za-z0-9]", ""); + return Language.normalizeTokenForSort(token, true); } }; diff --git a/src/com/hughes/android/dictionary/R.java b/src/com/hughes/android/dictionary/R.java index 667eeba..4d21c96 100755 --- a/src/com/hughes/android/dictionary/R.java +++ b/src/com/hughes/android/dictionary/R.java @@ -14,32 +14,34 @@ public final class R { public static final int icon=0x7f020000; } public static final class id { - public static final int DownButton=0x7f060019; + public static final int DownButton=0x7f06001b; public static final int ImageView01=0x7f060002; - public static final int LangButton=0x7f060018; + public static final int LangButton=0x7f06001a; public static final int LinearLayout01=0x7f060000; public static final int LinearLayout02=0x7f060001; - public static final int SearchBarLinearLayout=0x7f060014; - public static final int SearchBarTableLayout=0x7f060015; - public static final int SearchBarTableRow=0x7f060016; - public static final int SearchText=0x7f060017; - public static final int TextView01=0x7f06000b; - public static final int UpButton=0x7f06001a; + public static final int SearchBarLinearLayout=0x7f060016; + public static final int SearchBarTableLayout=0x7f060017; + public static final int SearchBarTableRow=0x7f060018; + public static final int SearchText=0x7f060019; + public static final int TextView01=0x7f06000d; + public static final int UpButton=0x7f06001c; public static final int author=0x7f060006; public static final int copyright=0x7f060005; - public static final int currentDictInfo=0x7f06000a; - public static final int currentDictInfoTitle=0x7f060009; - public static final int dest=0x7f060011; - public static final int destLayout=0x7f06000f; - public static final int destTitle=0x7f060010; - public static final int downloadComplete=0x7f060013; - public static final int downloadProgressBar=0x7f060012; - public static final int email=0x7f060007; - public static final int source=0x7f06000e; - public static final int sourceLayout=0x7f06000c; - public static final int sourceTitle=0x7f06000d; + public static final int currentDictInfo=0x7f06000c; + public static final int currentDictInfoTitle=0x7f06000b; + public static final int dest=0x7f060013; + public static final int destLayout=0x7f060011; + public static final int destTitle=0x7f060012; + public static final int downloadProgressBar=0x7f060014; + public static final int downloadStatus=0x7f060015; + public static final int email=0x7f060009; + public static final int emailMe=0x7f060008; + public static final int source=0x7f060010; + public static final int sourceLayout=0x7f06000e; + public static final int sourceTitle=0x7f06000f; public static final int space1=0x7f060004; - public static final int space2=0x7f060008; + public static final int space2=0x7f06000a; + public static final int space3=0x7f060007; public static final int title=0x7f060003; } public static final class layout { -- 2.43.0