From: Thad Hughes Date: Tue, 17 Jan 2012 20:58:42 +0000 (-0800) Subject: Fixed themes, fixing DictionaryInfo, DictionaryVersion 2. Better X-Git-Url: http://gitweb.fperrin.net/?p=Dictionary.git;a=commitdiff_plain;h=00fb6fcb966f25e64a14e64491e2c417801128d8 Fixed themes, fixing DictionaryInfo, DictionaryVersion 2. Better dictionary switching (just started). --- diff --git a/res/layout/select_dictionary_dialog.xml b/res/layout/select_dictionary_dialog.xml new file mode 100644 index 0000000..54fe608 --- /dev/null +++ b/res/layout/select_dictionary_dialog.xml @@ -0,0 +1,18 @@ + + + + + + + diff --git a/res/values/themes.xml b/res/values/themes.xml index 5ac84e6..9963d6a 100644 --- a/res/values/themes.xml +++ b/res/values/themes.xml @@ -7,7 +7,7 @@ #FFFFFF - diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index 4d8d6ed..cb814f7 100644 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -20,7 +20,9 @@ import java.io.IOException; import java.io.PrintWriter; import java.io.RandomAccessFile; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; +import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; @@ -28,6 +30,8 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.regex.Matcher; import java.util.regex.Pattern; +import android.app.Activity; +import android.app.Dialog; import android.app.ListActivity; import android.content.Context; import android.content.Intent; @@ -62,6 +66,7 @@ import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TableLayout; @@ -82,7 +87,7 @@ public class DictionaryActivity extends ListActivity { static final String LOG = "QuickDic"; - int dictIndex = 0; + String dictFile = null; RandomAccessFile dictRaf = null; Dictionary dictionary = null; int indexIndex = 0; @@ -258,6 +263,13 @@ public class DictionaryActivity extends ListActivity { onLanguageButton(); } }); + langButton.setOnLongClickListener(new OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + onLanguageButtonLongClick(); + return true; + } + }); updateLangButton(); final Button upButton = (Button) findViewById(R.id.UpButton); @@ -368,7 +380,7 @@ public class DictionaryActivity extends ListActivity { } void updateLangButton() { - langButton.setText(index.shortName.toUpperCase()); + langButton.setText(index.shortName); } void onLanguageButton() { @@ -380,6 +392,72 @@ public class DictionaryActivity extends ListActivity { changeIndex((indexIndex + 1)% dictionary.indices.size()); onSearchTextChange(searchText.getText().toString()); } + + static class OpenIndexButton extends Button implements OnClickListener { + + final Activity activity; + final int dictionaryIndex; + final int indexIndex; + + public OpenIndexButton(final Context context, final Activity activity, final String text, final int dictionaryIndex, final int indexIndex) { + super(context); + this.activity = activity; + this.dictionaryIndex = dictionaryIndex; + this.indexIndex = indexIndex; + setOnClickListener(this); + setText(text, BufferType.NORMAL); + } + + @Override + public void onClick(View v) { + activity.finish(); + getContext().startActivity(DictionaryActivity.getIntent(getContext(), dictionaryIndex, indexIndex, "")); + } + + } + + void onLanguageButtonLongClick() { + Context mContext = getApplicationContext(); + Dialog dialog = new Dialog(mContext); + + dialog.setContentView(R.layout.select_dictionary_dialog); + dialog.setTitle(R.string.selectADictionary); + + ListView listView = (ListView) dialog.findViewById(android.R.id.list); + + QuickDicConfig quickDicConfig = PersistentObjectCache.init( + this).read(C.DICTIONARY_CONFIGS, QuickDicConfig.class); + final List dictionaryInfos = new ArrayList(); + for (final DictionaryInfo dictionaryInfo : quickDicConfig.dictionaryInfos) { + if (new File(dictionaryInfo.localFile).canRead()) { + dictionaryInfos.add(dictionaryInfo); + } + } + listView.setAdapter(new BaseAdapter() { + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + final LinearLayout result = new LinearLayout(parent.getContext()); + result.addView(new Butt) + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public Object getItem(int position) { + return dictionaryInfos.get(position); + } + + @Override + public int getCount() { + return dictionaryInfos.size(); + } + }); + } + private void changeIndex(final int newIndex) { indexIndex = newIndex; diff --git a/src/com/hughes/android/dictionary/DictionaryInfo.java b/src/com/hughes/android/dictionary/DictionaryInfo.java index 46aafea..e965994 100644 --- a/src/com/hughes/android/dictionary/DictionaryInfo.java +++ b/src/com/hughes/android/dictionary/DictionaryInfo.java @@ -15,43 +15,77 @@ package com.hughes.android.dictionary; import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; public class DictionaryInfo implements Serializable { private static final long serialVersionUID = -6850863377577700388L; + public static final class IndexInfo { + public IndexInfo(String langIso, int allTokenCount, int mainTokenCount) { + this.langIso = langIso; + this.allTokenCount = allTokenCount; + this.mainTokenCount = mainTokenCount; + } + public final String langIso; + public final int allTokenCount; + public final int mainTokenCount; + + public static final int SIZE = 3; + + public StringBuilder append(StringBuilder result) { + result.append("\t").append(langIso); + result.append("\t").append(allTokenCount); + result.append("\t").append(mainTokenCount); + return result; + } + + public IndexInfo(final String[] fields, int i) { + langIso = fields[i++]; + allTokenCount = Integer.parseInt(fields[i++]); + mainTokenCount = Integer.parseInt(fields[i++]); + } + + } + // Stuff populated from the text file. - public final String[] langIsos = new String[2]; public String uncompressedFilename; public String downloadUrl; public long uncompressedSize; public long creationMillis; - public final int[] allTokenCounts = new int[2]; - public final int[] mainTokenCounts = new int[2]; + public String dictInfo; + public final List indexInfos = new ArrayList(); - String name; // Determined at runtime based on locale on device--user editable. + String name; // Determined at runtime based on locale on device--user editable? String localFile; // Determined based on device's Environment. - public String toTabSeparatedString() { - return String.format("%s\t%s\t%s\t%s\t%d\t%d\t%d\t%d\t%d\t%d", langIsos[0], - langIsos[1], uncompressedFilename, downloadUrl, creationMillis, uncompressedSize, - mainTokenCounts[0], mainTokenCounts[1], allTokenCounts[0], - allTokenCounts[1]); + public StringBuilder append(final StringBuilder result) { + result.append(uncompressedFilename).append("\t"); + result.append(downloadUrl).append("\t"); + result.append(creationMillis).append("\t"); + result.append(uncompressedSize).append("\t"); + result.append(dictInfo).append("\t"); + result.append(indexInfos.size()).append("\t"); + for (final IndexInfo indexInfo : indexInfos) { + indexInfo.append(result); + } + return result; } public DictionaryInfo(final String line) { final String[] fields = line.split("\t"); int i = 0; - langIsos[0] = fields[i++]; - langIsos[1] = fields[i++]; uncompressedFilename = fields[i++]; downloadUrl = fields[i++]; creationMillis = Long.parseLong(fields[i++]); uncompressedSize = Long.parseLong(fields[i++]); - mainTokenCounts[0] = Integer.parseInt(fields[i++]); - mainTokenCounts[1] = Integer.parseInt(fields[i++]); - allTokenCounts[0] = Integer.parseInt(fields[i++]); - allTokenCounts[1] = Integer.parseInt(fields[i++]); + dictInfo = fields[i++]; + final int size = Integer.parseInt(fields[i++]); + for (int j = 0; j < size; ++j) { + indexInfos.add(new IndexInfo(fields, i)); + i += IndexInfo.SIZE; + } } public DictionaryInfo() { diff --git a/src/com/hughes/android/dictionary/QuickDicConfig.java b/src/com/hughes/android/dictionary/QuickDicConfig.java index ac32748..4574107 100644 --- a/src/com/hughes/android/dictionary/QuickDicConfig.java +++ b/src/com/hughes/android/dictionary/QuickDicConfig.java @@ -61,9 +61,9 @@ public final class QuickDicConfig implements Serializable { } final DictionaryInfo dictionaryInfo = new DictionaryInfo(line); String name = ""; - for (int i = 0; i < dictionaryInfo.langIsos.length; ++i) { - final Integer langCode = Language.isoCodeToResourceId.get(dictionaryInfo.langIsos[i]); - final String lang = langCode != null ? context.getString(langCode) : dictionaryInfo.langIsos[i]; + for (int i = 0; i < dictionaryInfo.indexInfos.size(); ++i) { + final Integer langCode = Language.isoCodeToResourceId.get(dictionaryInfo.indexInfos.get(i).langIso); + final String lang = langCode != null ? context.getString(langCode) : dictionaryInfo.indexInfos.get(i).langIso; if (i > 0) { name += "-"; } diff --git a/src/com/hughes/android/dictionary/engine/Dictionary.java b/src/com/hughes/android/dictionary/engine/Dictionary.java index c662998..b00868e 100644 --- a/src/com/hughes/android/dictionary/engine/Dictionary.java +++ b/src/com/hughes/android/dictionary/engine/Dictionary.java @@ -20,6 +20,7 @@ import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.List; +import com.hughes.android.dictionary.DictionaryInfo; import com.hughes.util.CachingList; import com.hughes.util.raf.RAFList; import com.hughes.util.raf.RAFListSerializer; @@ -30,6 +31,7 @@ public class Dictionary implements RAFSerializable { static final int CACHE_SIZE = 5000; + static final int CURRENT_DICT_VERSION = 2; static final String END_OF_DICTIONARY = "END OF DICTIONARY"; // persisted @@ -43,12 +45,14 @@ public class Dictionary implements RAFSerializable { /** * dictFileVersion 1 adds: - *
  • counts of tokens in indices. *
  • links to sources? + * + * dictFileVersion 2 adds: + *
  • counts of tokens in indices. */ public Dictionary(final String dictInfo) { - this.dictFileVersion = 1; + this.dictFileVersion = CURRENT_DICT_VERSION; this.creationMillis = System.currentTimeMillis(); this.dictInfo = dictInfo; pairEntries = new ArrayList(); @@ -59,7 +63,7 @@ public class Dictionary implements RAFSerializable { public Dictionary(final RandomAccessFile raf) throws IOException { dictFileVersion = raf.readInt(); - if (dictFileVersion < 0 || dictFileVersion > 1) { + if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) { throw new IOException("Invalid dictionary version: " + dictFileVersion); } creationMillis = raf.readLong(); @@ -110,5 +114,15 @@ public class Dictionary implements RAFSerializable { } } + public DictionaryInfo getDictionaryInfo() { + final DictionaryInfo result = new DictionaryInfo(); + result.creationMillis = this.creationMillis; + result.dictInfo = this.dictInfo; + for (final Index index : indices) { + result.indexInfos.add(index.getIndexInfo()); + } + return result; + } + } \ No newline at end of file diff --git a/src/com/hughes/android/dictionary/engine/Index.java b/src/com/hughes/android/dictionary/engine/Index.java index 39fcab5..19e0ecc 100644 --- a/src/com/hughes/android/dictionary/engine/Index.java +++ b/src/com/hughes/android/dictionary/engine/Index.java @@ -25,6 +25,8 @@ import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; +import com.hughes.android.dictionary.DictionaryInfo; +import com.hughes.android.dictionary.DictionaryInfo.IndexInfo; import com.hughes.util.CachingList; import com.hughes.util.raf.RAFList; import com.hughes.util.raf.RAFSerializable; @@ -39,7 +41,7 @@ public final class Index implements RAFSerializable { final Dictionary dict; - public final String shortName; + public final String shortName; // Typically the ISO code for the language. public final String longName; // persisted: tells how the entries are sorted. @@ -56,9 +58,11 @@ public final class Index implements RAFSerializable { // Various sub-types. // persisted public final List rows; - public final boolean swapPairEntries; + // Version 2: + int mainTokenCount = -1; + // -------------------------------------------------------------------------- public Index(final Dictionary dict, final String shortName, final String longName, final Language sortLanguage, final String normalizerRules, final boolean swapPairEntries) { @@ -92,6 +96,9 @@ public final class Index implements RAFSerializable { if (sortLanguage == null) { throw new IOException("Unsupported language: " + languageCode); } + if (dict.dictFileVersion >= 2) { + mainTokenCount = raf.readInt(); + } sortedIndexEntries = CachingList.create(RAFList.create(raf, IndexEntry.SERIALIZER, raf.getFilePointer()), CACHE_SIZE); rows = CachingList.create(UniformRAFList.create(raf, new RowBase.Serializer(this), raf.getFilePointer()), CACHE_SIZE); } @@ -103,6 +110,9 @@ public final class Index implements RAFSerializable { raf.writeUTF(sortLanguage.getIsoCode()); raf.writeUTF(normalizerRules); raf.writeBoolean(swapPairEntries); + if (dict.dictFileVersion >= 2) { + raf.writeInt(mainTokenCount); + } RAFList.write(raf, sortedIndexEntries, IndexEntry.SERIALIZER); UniformRAFList.write(raf, (Collection) rows, new RowBase.Serializer(this), 5); } @@ -168,8 +178,8 @@ public final class Index implements RAFSerializable { } public IndexEntry findInsertionPoint(String token, final AtomicBoolean interrupted) { - final Transliterator normalizer = normalizer(); if (TransliteratorManager.init(null)) { + final Transliterator normalizer = normalizer(); token = normalizer.transliterate(token); } else { // Do our best since the Transliterators aren't up yet. @@ -216,4 +226,8 @@ public final class Index implements RAFSerializable { return result; } + public IndexInfo getIndexInfo() { + return new DictionaryInfo.IndexInfo(shortName, sortedIndexEntries.size(), mainTokenCount); + } + } \ No newline at end of file