\r
public final class AboutActivity extends Activity {\r
\r
- public static final String CURRENT_DICT_INFO = "currentDictInfo";\r
+ public static final String CURRENT_DICT_INFO = "currentDictInfo";\r
\r
- /** Called when the activity is first created. */\r
- @Override\r
- public void onCreate(final Bundle savedInstanceState) {\r
- setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);\r
+ /** Called when the activity is first created. */\r
+ @Override\r
+ public void onCreate(final Bundle savedInstanceState) {\r
+ setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);\r
\r
- super.onCreate(savedInstanceState);\r
- setContentView(R.layout.about_activity);\r
- }\r
+ super.onCreate(savedInstanceState);\r
+ setContentView(R.layout.about_activity);\r
+ }\r
\r
}\r
List<RowBase> rowsToShow = null; // if not null, just show these rows.
final Handler uiHandler = new Handler();
-
+
private final Executor searchExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
@Override
protected void onSaveInstanceState(final Bundle outState) {
- super.onSaveInstanceState(outState);
+ super.onSaveInstanceState(outState);
Log.d(LOG, "onSaveInstanceState: " + searchView.getQuery().toString());
outState.putString(C.INDEX_SHORT_NAME, index.shortName);
outState.putString(C.SEARCH_TOKEN, searchView.getQuery().toString());
public void onCreate(Bundle savedInstanceState) {
Log.d(LOG, "onCreate:" + this);
super.onCreate(savedInstanceState);
-
+
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
-
+
// Don't auto-launch if this fails.
- prefs.edit().remove(C.DICT_FILE).commit();
+ prefs.edit().remove(C.DICT_FILE).commit();
setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
final Intent intent = getIntent();
String intentAction = intent.getAction();
/**
- * @author Dominik Köppl
- * Querying the Intent
- * com.hughes.action.ACTION_SEARCH_DICT is the advanced query
- * Arguments:
- * SearchManager.QUERY -> the phrase to search
- * from -> language in which the phrase is written
- * to -> to which language shall be translated
+ * @author Dominik Köppl Querying the Intent
+ * com.hughes.action.ACTION_SEARCH_DICT is the advanced query
+ * Arguments: SearchManager.QUERY -> the phrase to search from
+ * -> language in which the phrase is written to -> to which
+ * language shall be translated
*/
- if(intentAction != null && intentAction.equals("com.hughes.action.ACTION_SEARCH_DICT"))
+ if (intentAction != null && intentAction.equals("com.hughes.action.ACTION_SEARCH_DICT"))
{
- String query = intent.getStringExtra(SearchManager.QUERY);
- String from = intent.getStringExtra("from");
- if(from != null) from = from.toLowerCase(Locale.US);
- String to = intent.getStringExtra("to");
- if(to != null) to = to.toLowerCase(Locale.US);
- if(query != null)
- {
- getIntent().putExtra(C.SEARCH_TOKEN, query);
- }
- if(intent.getStringExtra(C.DICT_FILE) == null && (from != null || to != null))
- {
- Log.d(LOG, "DictSearch: from: " + from + " to " + to);
- List<DictionaryInfo> dicts = application.getDictionariesOnDevice(null);
- for(DictionaryInfo info : dicts)
- {
- boolean hasFrom = from == null;
- boolean hasTo = to == null;
- for(IndexInfo index : info.indexInfos)
- {
- if(!hasFrom && index.shortName.toLowerCase(Locale.US).equals(from)) hasFrom = true;
- if(!hasTo && index.shortName.toLowerCase(Locale.US).equals(to)) hasTo = true;
- }
- if(hasFrom && hasTo)
- {
- if(from != null)
- {
- int which_index = 0;
- for(;which_index < info.indexInfos.size(); ++which_index)
- {
- if(info.indexInfos.get(which_index).shortName.toLowerCase(Locale.US).equals(from))
- break;
- }
- intent.putExtra(C.INDEX_SHORT_NAME, info.indexInfos.get(which_index).shortName);
-
- }
- intent.putExtra(C.DICT_FILE, application.getPath(info.uncompressedFilename).toString());
- break;
- }
- }
-
- }
+ String query = intent.getStringExtra(SearchManager.QUERY);
+ String from = intent.getStringExtra("from");
+ if (from != null)
+ from = from.toLowerCase(Locale.US);
+ String to = intent.getStringExtra("to");
+ if (to != null)
+ to = to.toLowerCase(Locale.US);
+ if (query != null)
+ {
+ getIntent().putExtra(C.SEARCH_TOKEN, query);
+ }
+ if (intent.getStringExtra(C.DICT_FILE) == null && (from != null || to != null))
+ {
+ Log.d(LOG, "DictSearch: from: " + from + " to " + to);
+ List<DictionaryInfo> dicts = application.getDictionariesOnDevice(null);
+ for (DictionaryInfo info : dicts)
+ {
+ boolean hasFrom = from == null;
+ boolean hasTo = to == null;
+ for (IndexInfo index : info.indexInfos)
+ {
+ if (!hasFrom && index.shortName.toLowerCase(Locale.US).equals(from))
+ hasFrom = true;
+ if (!hasTo && index.shortName.toLowerCase(Locale.US).equals(to))
+ hasTo = true;
+ }
+ if (hasFrom && hasTo)
+ {
+ if (from != null)
+ {
+ int which_index = 0;
+ for (; which_index < info.indexInfos.size(); ++which_index)
+ {
+ if (info.indexInfos.get(which_index).shortName.toLowerCase(
+ Locale.US).equals(from))
+ break;
+ }
+ intent.putExtra(C.INDEX_SHORT_NAME,
+ info.indexInfos.get(which_index).shortName);
+
+ }
+ intent.putExtra(C.DICT_FILE, application.getPath(info.uncompressedFilename)
+ .toString());
+ break;
+ }
+ }
+
+ }
}
/**
- * @author Dominik Köppl
- * Querying the Intent
- * Intent.ACTION_SEARCH is a simple query
- * Arguments follow from android standard (see documentation)
+ * @author Dominik Köppl Querying the Intent Intent.ACTION_SEARCH is a
+ * simple query Arguments follow from android standard (see
+ * documentation)
*/
- if (intentAction != null && intentAction.equals(Intent.ACTION_SEARCH))
+ if (intentAction != null && intentAction.equals(Intent.ACTION_SEARCH))
{
String query = intent.getStringExtra(SearchManager.QUERY);
- if(query != null) getIntent().putExtra(C.SEARCH_TOKEN,query);
+ if (query != null)
+ getIntent().putExtra(C.SEARCH_TOKEN, query);
}
/**
- * @author Dominik Köppl
- * If no dictionary is chosen, use the default dictionary specified in the preferences
- * If this step does fail (no default directory specified), show a toast and abort.
+ * @author Dominik Köppl If no dictionary is chosen, use the default
+ * dictionary specified in the preferences If this step does
+ * fail (no default directory specified), show a toast and
+ * abort.
*/
- if(intent.getStringExtra(C.DICT_FILE) == null)
+ if (intent.getStringExtra(C.DICT_FILE) == null)
{
- String dictfile = prefs.getString(getString(R.string.defaultDicKey), null);
- if(dictfile != null) intent.putExtra(C.DICT_FILE, application.getPath(dictfile).toString());
+ String dictfile = prefs.getString(getString(R.string.defaultDicKey), null);
+ if (dictfile != null)
+ intent.putExtra(C.DICT_FILE, application.getPath(dictfile).toString());
}
String dictFilename = intent.getStringExtra(C.DICT_FILE);
-
- if(dictFilename == null)
+
+ if (dictFilename == null)
{
Toast.makeText(this, getString(R.string.no_dict_file), Toast.LENGTH_LONG).show();
startActivity(DictionaryManagerActivity.getLaunchIntent());
finish();
return;
}
- if(dictFilename != null) dictFile = new File(dictFilename);
+ if (dictFilename != null)
+ dictFile = new File(dictFilename);
ttsReady = false;
textToSpeech = new TextToSpeech(getApplicationContext(), new OnInitListener() {
updateTTSLanguage();
}
});
-
+
try {
final String name = application.getDictionaryName(dictFile.getName());
this.setTitle("QuickDic: " + name);
Log.d(LOG, "wordList=" + wordList + ", saveOnlyFirstSubentry=" + saveOnlyFirstSubentry);
onCreateSetupActionBarAndSearchView();
-
+
// Set the search text from the intent, then the saved state.
String text = getIntent().getStringExtra(C.SEARCH_TOKEN);
if (savedInstanceState != null) {
private void onCreateSetupActionBarAndSearchView() {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
-
+
searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setIconifiedByDefault(false);
// searchView.setIconified(false); // puts the magnifying glass in the
private void showKeyboard() {
// For some reason, this doesn't always work the first time.
// One way to replicate the problem:
- // Press the "task switch" button repeatedly to pause and resume
+ // Press the "task switch" button repeatedly to pause and resume
for (int delay = 1; delay <= 101; delay += 100) {
searchView.postDelayed(new Runnable() {
@Override
if (!searchTextHadFocus) {
defocusSearchText();
}
- }}, delay);
+ }
+ }, delay);
}
}
}
}
updateTTSLanguage();
- }
+ }
private void updateTTSLanguage() {
if (!ttsReady || index == null || textToSpeech == null) {
final Locale locale = new Locale(index.sortLanguage.getIsoCode());
Log.d(LOG, "Setting TTS locale to: " + locale);
final int ttsResult = textToSpeech.setLanguage(locale);
- if (ttsResult != TextToSpeech.LANG_AVAILABLE ||
- ttsResult != TextToSpeech.LANG_COUNTRY_AVAILABLE) {
+ if (ttsResult != TextToSpeech.LANG_AVAILABLE ||
+ ttsResult != TextToSpeech.LANG_COUNTRY_AVAILABLE) {
Log.e(LOG, "TTS not available in this language: ttsResult=" + ttsResult);
}
}
currentSearchOperation.interrupted.set(true);
currentSearchOperation = null;
}
- setIndexAndSearchText((indexIndex + 1) % dictionary.indices.size(),
+ setIndexAndSearchText((indexIndex + 1) % dictionary.indices.size(),
searchView.getQuery().toString());
}
for (int i = 0; i < dictionaryInfo.indexInfos.size(); ++i) {
final IndexInfo indexInfo = dictionaryInfo.indexInfos.get(i);
- final View button = application.createButton(parent.getContext(), dictionaryInfo, indexInfo);
+ final View button = application.createButton(parent.getContext(),
+ dictionaryInfo, indexInfo);
final IntentLauncher intentLauncher = new IntentLauncher(parent.getContext(),
getLaunchIntent(
application.getPath(dictionaryInfo.uncompressedFilename),
// --------------------------------------------------------------------------
final Random random = new Random();
-
+
@Override
public boolean onCreateOptionsMenu(final Menu menu) {
-
+
if (PreferenceManager.getDefaultSharedPreferences(this)
.getBoolean(getString(R.string.showPrevNextButtonsKey), true)) {
// Next word.
nextWordMenuItem = menu.add(getString(R.string.nextWord))
- .setIcon(R.drawable.arrow_down_float);
+ .setIcon(R.drawable.arrow_down_float);
nextWordMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
nextWordMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
- @Override
- public boolean onMenuItemClick(MenuItem item) {
- onUpDownButton(false);
- return true;
- }
- });
-
+ @Override
+ public boolean onMenuItemClick(MenuItem item) {
+ onUpDownButton(false);
+ return true;
+ }
+ });
+
// Previous word.
previousWordMenuItem = menu.add(getString(R.string.previousWord))
- .setIcon(R.drawable.arrow_up_float);
+ .setIcon(R.drawable.arrow_up_float);
previousWordMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
previousWordMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
- @Override
- public boolean onMenuItemClick(MenuItem item) {
- onUpDownButton(true);
- return true;
- }
- });
+ @Override
+ public boolean onMenuItemClick(MenuItem item) {
+ onUpDownButton(true);
+ return true;
+ }
+ });
}
application.onCreateGlobalOptionsMenu(this, menu);
final android.view.MenuItem addToWordlist = menu.add(getString(R.string.addToWordList,
wordList.getName()));
- addToWordlist.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
- public boolean onMenuItemClick(android.view.MenuItem item) {
- onAppendToWordList(row);
- return false;
- }
- });
+ addToWordlist
+ .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
+ public boolean onMenuItemClick(android.view.MenuItem item) {
+ onAppendToWordList(row);
+ return false;
+ }
+ });
final android.view.MenuItem share = menu.add("Share");
share.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(android.view.MenuItem item) {
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
- shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, row.getTokenRow(true).getToken());
- shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, row.getRawText(saveOnlyFirstSubentry));
+ shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, row.getTokenRow(true)
+ .getToken());
+ shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
+ row.getRawText(saveOnlyFirstSubentry));
startActivity(shareIntent);
return false;
}
if (selectedSpannableText != null) {
final String selectedText = selectedSpannableText;
- final android.view.MenuItem searchForSelection = menu.add(getString(R.string.searchForSelection,
+ final android.view.MenuItem searchForSelection = menu.add(getString(
+ R.string.searchForSelection,
selectedSpannableText));
- searchForSelection.setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
- public boolean onMenuItemClick(android.view.MenuItem item) {
- jumpToTextFromHyperLink(selectedText, selectedSpannableIndex);
- return false;
- }
- });
+ searchForSelection
+ .setOnMenuItemClickListener(new android.view.MenuItem.OnMenuItemClickListener() {
+ public boolean onMenuItemClick(android.view.MenuItem item) {
+ jumpToTextFromHyperLink(selectedText, selectedSpannableIndex);
+ return false;
+ }
+ });
}
if (row instanceof TokenRow && ttsReady) {
}
}, 100);
}
-
+
/**
* Called when user clicks outside of search text, so that they can start
* typing again immediately.
// Visual indication that a new keystroke will clear the search text.
// Doesn't seem to work unless earchText has focus.
-// searchView.selectAll();
+ // searchView.selectAll();
}
@Override
}
return super.onKeyDown(keyCode, event);
}
-
+
private void setIndexAndSearchText(int newIndex, String newSearchText) {
Log.d(LOG, "Changing index to: " + newIndex);
if (newIndex == -1) {
}
}
-// private long cursorDelayMillis = 100;
+ // private long cursorDelayMillis = 100;
private void moveCursorToRight() {
-// if (searchText.getLayout() != null) {
-// cursorDelayMillis = 100;
-// // Surprising, but this can crash when you rotate...
-// Selection.moveToRightEdge(searchView.getQuery(), searchText.getLayout());
-// } else {
-// uiHandler.postDelayed(new Runnable() {
-// @Override
-// public void run() {
-// moveCursorToRight();
-// }
-// }, cursorDelayMillis);
-// cursorDelayMillis = Math.min(10 * 1000, 2 * cursorDelayMillis);
-// }
+ // if (searchText.getLayout() != null) {
+ // cursorDelayMillis = 100;
+ // // Surprising, but this can crash when you rotate...
+ // Selection.moveToRightEdge(searchView.getQuery(),
+ // searchText.getLayout());
+ // } else {
+ // uiHandler.postDelayed(new Runnable() {
+ // @Override
+ // public void run() {
+ // moveCursorToRight();
+ // }
+ // }, cursorDelayMillis);
+ // cursorDelayMillis = Math.min(10 * 1000, 2 * cursorDelayMillis);
+ // }
}
// --------------------------------------------------------------------------
private final void jumpToRow(final int row) {
Log.d(LOG, "jumpToRow: " + row + ", refocusSearchText=" + false);
-// getListView().requestFocusFromTouch();
+ // getListView().requestFocusFromTouch();
getListView().setSelectionFromTop(row, 0);
getListView().setSelected(true);
}
searchResult = index.findInsertionPoint(searchText, interrupted);
} else {
searchTokens = Arrays.asList(searchTokenArray);
- multiWordSearchResult = index.multiWordSearch(searchText, searchTokens, interrupted);
+ multiWordSearchResult = index.multiWordSearch(searchText, searchTokens,
+ interrupted);
}
Log.d(LOG,
"searchText=" + searchText + ", searchDuration="
0);
textView.setOnLongClickListener(textViewLongClickListenerIndex0);
result.setLongClickable(true);
-
+
// Doesn't work:
// textView.setTextColor(android.R.color.secondary_text_light);
textView.setTypeface(typeface);
public void onClick(View widget) {
}
};
- ((Spannable) textView.getText()).setSpan(clickableSpan, 0, text.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
+ ((Spannable) textView.getText()).setSpan(clickableSpan, 0, text.length(),
+ Spannable.SPAN_INCLUSIVE_INCLUSIVE);
result.setClickable(true);
textView.setClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
@Override
public void onClick(View v) {
String html = HtmlEntry.htmlBody(htmlEntries, index.shortName);
- //Log.d(LOG, "html=" + html);
+ // Log.d(LOG, "html=" + html);
startActivityForResult(
HtmlDisplayActivity.getHtmlIntent(String.format(
"<html><head></head><body>%s</body></html>", html),
}
static final Pattern CHAR_DASH = Pattern.compile("['\\p{L}\\p{M}\\p{N}]+");
-
+
private void createTokenLinkSpans(final TextView textView, final Spannable spannable,
final String text) {
// Saw from the source code that LinkMovementMethod sets the selection!
textView.setMovementMethod(LinkMovementMethod.getInstance());
final Matcher matcher = CHAR_DASH.matcher(text);
while (matcher.find()) {
- spannable.setSpan(new NonLinkClickableSpan(textColorFg), matcher.start(), matcher.end(),
+ spannable.setSpan(new NonLinkClickableSpan(textColorFg), matcher.start(),
+ matcher.end(),
Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
Log.d(LOG, "searchText changed during shutdown, doing nothing.");
return;
}
-// if (!searchView.hasFocus()) {
-// Log.d(LOG, "searchText changed without focus, doing nothing.");
-// return;
-// }
+ // if (!searchView.hasFocus()) {
+ // Log.d(LOG, "searchText changed without focus, doing nothing.");
+ // return;
+ // }
Log.d(LOG, "onSearchTextChange: " + text);
if (currentSearchOperation != null) {
Log.d(LOG, "Interrupting currentSearchOperation.");
import java.util.Map;
public class DictionaryApplication extends Application {
-
- static final String LOG = "QuickDicApp";
-
- // Static, determined by resources (and locale).
- // Unordered.
- static Map<String,DictionaryInfo> DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO = null;
-
- static final class DictionaryConfig implements Serializable {
- private static final long serialVersionUID = -1444177164708201263L;
- // User-ordered list, persisted, just the ones that are/have been present.
- final List<String> dictionaryFilesOrdered = new ArrayList<String>();
-
- final Map<String, DictionaryInfo> uncompressedFilenameToDictionaryInfo = new LinkedHashMap<String, DictionaryInfo>();
- }
- DictionaryConfig dictionaryConfig = null;
-
-
- int languageButtonPixels = 22;
-
-// static final class DictionaryHistory implements Serializable {
-// private static final long serialVersionUID = -4842995032541390284L;
-// // User-ordered list, persisted, just the ones that are/have been present.
-// final List<DictionaryLink> dictionaryLinks = new ArrayList<DictionaryLink>();
-// }
-// DictionaryHistory dictionaryHistory = null;
-
- static synchronized void staticInit(final Context context) {
- if (DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO != null) {
- return;
+
+ static final String LOG = "QuickDicApp";
+
+ // Static, determined by resources (and locale).
+ // Unordered.
+ static Map<String, DictionaryInfo> DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO = null;
+
+ static final class DictionaryConfig implements Serializable {
+ private static final long serialVersionUID = -1444177164708201263L;
+ // User-ordered list, persisted, just the ones that are/have been
+ // present.
+ final List<String> dictionaryFilesOrdered = new ArrayList<String>();
+
+ final Map<String, DictionaryInfo> uncompressedFilenameToDictionaryInfo = new LinkedHashMap<String, DictionaryInfo>();
}
- DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO = new LinkedHashMap<String,DictionaryInfo>();
- final BufferedReader reader = new BufferedReader(
- new InputStreamReader(context.getResources().openRawResource(R.raw.dictionary_info)));
- try {
- String line;
- while ((line = reader.readLine()) != null) {
- if (line.startsWith("#") || line.length() == 0) {
- continue;
+
+ DictionaryConfig dictionaryConfig = null;
+
+ int languageButtonPixels = 22;
+
+ // static final class DictionaryHistory implements Serializable {
+ // private static final long serialVersionUID = -4842995032541390284L;
+ // // User-ordered list, persisted, just the ones that are/have been
+ // present.
+ // final List<DictionaryLink> dictionaryLinks = new
+ // ArrayList<DictionaryLink>();
+ // }
+ // DictionaryHistory dictionaryHistory = null;
+
+ static synchronized void staticInit(final Context context) {
+ if (DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO != null) {
+ return;
+ }
+ DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO = new LinkedHashMap<String, DictionaryInfo>();
+ final BufferedReader reader = new BufferedReader(
+ new InputStreamReader(context.getResources().openRawResource(R.raw.dictionary_info)));
+ try {
+ String line;
+ while ((line = reader.readLine()) != null) {
+ if (line.startsWith("#") || line.length() == 0) {
+ continue;
+ }
+ final DictionaryInfo dictionaryInfo = new DictionaryInfo(line);
+ DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.put(
+ dictionaryInfo.uncompressedFilename, dictionaryInfo);
+ }
+ reader.close();
+ } catch (IOException e) {
+ Log.e(LOG, "Failed to load downloadable dictionary lists.", e);
}
- final DictionaryInfo dictionaryInfo = new DictionaryInfo(line);
- DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.put(
- dictionaryInfo.uncompressedFilename, dictionaryInfo);
- }
- reader.close();
- } catch (IOException e) {
- Log.e(LOG, "Failed to load downloadable dictionary lists.", e);
}
- }
-
-
- private File dictDir;
-
- @Override
- public void onCreate() {
- super.onCreate();
- Log.d("QuickDic", "Application: onCreate");
- TransliteratorManager.init(null);
- staticInit(getApplicationContext());
-
- languageButtonPixels = (int) TypedValue.applyDimension(
- TypedValue.COMPLEX_UNIT_DIP, 60, getResources().getDisplayMetrics());
-
- // Load the dictionaries we know about.
- dictionaryConfig = PersistentObjectCache.init(getApplicationContext()).read(
- C.DICTIONARY_CONFIGS, DictionaryConfig.class);
- if (dictionaryConfig == null) {
- dictionaryConfig = new DictionaryConfig();
+
+ private File dictDir;
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ Log.d("QuickDic", "Application: onCreate");
+ TransliteratorManager.init(null);
+ staticInit(getApplicationContext());
+
+ languageButtonPixels = (int) TypedValue.applyDimension(
+ TypedValue.COMPLEX_UNIT_DIP, 60, getResources().getDisplayMetrics());
+
+ // Load the dictionaries we know about.
+ dictionaryConfig = PersistentObjectCache.init(getApplicationContext()).read(
+ C.DICTIONARY_CONFIGS, DictionaryConfig.class);
+ if (dictionaryConfig == null) {
+ dictionaryConfig = new DictionaryConfig();
+ }
+
+ // Theme stuff.
+ setTheme(getSelectedTheme().themeId);
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
+ @Override
+ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
+ String key) {
+ Log.d("QuickDic", "prefs changed: " + key);
+ if (key.equals(getString(R.string.themeKey))) {
+ setTheme(getSelectedTheme().themeId);
+ }
+ }
+ });
}
-
- // Theme stuff.
- setTheme(getSelectedTheme().themeId);
- final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
- @Override
- public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
- String key) {
- Log.d("QuickDic", "prefs changed: " + key);
- if (key.equals(getString(R.string.themeKey))) {
- setTheme(getSelectedTheme().themeId);
+
+ public void onCreateGlobalOptionsMenu(
+ final Context context, final Menu menu) {
+ final MenuItem about = menu.add(getString(R.string.about));
+ about.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+ about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+ public boolean onMenuItemClick(final MenuItem menuItem) {
+ final Intent intent = new Intent().setClassName(AboutActivity.class
+ .getPackage().getName(), AboutActivity.class.getCanonicalName());
+ context.startActivity(intent);
+ return false;
+ }
+ });
+
+ final MenuItem help = menu.add(getString(R.string.help));
+ help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+ help.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+ public boolean onMenuItemClick(final MenuItem menuItem) {
+ context.startActivity(HtmlDisplayActivity.getHelpLaunchIntent());
+ return false;
+ }
+ });
+
+ final MenuItem preferences = menu.add(getString(R.string.settings));
+ preferences.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+ preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+ public boolean onMenuItemClick(final MenuItem menuItem) {
+ PreferenceActivity.prefsMightHaveChanged = true;
+ final Intent intent = new Intent().setClassName(PreferenceActivity.class
+ .getPackage().getName(), PreferenceActivity.class.getCanonicalName());
+ context.startActivity(intent);
+ return false;
+ }
+ });
+
+ final MenuItem reportIssue = menu.add(getString(R.string.reportIssue));
+ reportIssue.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
+ reportIssue.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+ public boolean onMenuItemClick(final MenuItem menuItem) {
+ final Intent intent = new Intent(Intent.ACTION_VIEW);
+ intent.setData(Uri
+ .parse("http://code.google.com/p/quickdic-dictionary/issues/entry"));
+ context.startActivity(intent);
+ return false;
+ }
+ });
+ }
+
+ public synchronized File getDictDir() {
+ // This metaphor doesn't work, because we've already reset
+ // prefsMightHaveChanged.
+ // if (dictDir == null || PreferenceActivity.prefsMightHaveChanged) {
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ final File defaultDictDir = new File(Environment.getExternalStorageDirectory(), "quickDic");
+ String dir = prefs.getString(getString(R.string.quickdicDirectoryKey),
+ defaultDictDir.getAbsolutePath());
+ if (dir.isEmpty()) {
+ dir = defaultDictDir.getAbsolutePath();
}
- }
- });
- }
-
- public void onCreateGlobalOptionsMenu(
- final Context context, final Menu menu) {
- final MenuItem about = menu.add(getString(R.string.about));
- about.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
- about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
- public boolean onMenuItemClick(final MenuItem menuItem) {
- final Intent intent = new Intent().setClassName(AboutActivity.class
- .getPackage().getName(), AboutActivity.class.getCanonicalName());
- context.startActivity(intent);
- return false;
- }
- });
-
- final MenuItem help = menu.add(getString(R.string.help));
- help.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
- help.setOnMenuItemClickListener(new OnMenuItemClickListener() {
- public boolean onMenuItemClick(final MenuItem menuItem) {
- context.startActivity(HtmlDisplayActivity.getHelpLaunchIntent());
- return false;
- }
- });
-
- final MenuItem preferences = menu.add(getString(R.string.settings));
- preferences.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
- preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
- public boolean onMenuItemClick(final MenuItem menuItem) {
- PreferenceActivity.prefsMightHaveChanged = true;
- final Intent intent = new Intent().setClassName(PreferenceActivity.class
- .getPackage().getName(), PreferenceActivity.class.getCanonicalName());
- context.startActivity(intent);
- return false;
- }
- });
-
-
- final MenuItem reportIssue = menu.add(getString(R.string.reportIssue));
- reportIssue.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
- reportIssue.setOnMenuItemClickListener(new OnMenuItemClickListener() {
- public boolean onMenuItemClick(final MenuItem menuItem) {
- final Intent intent = new Intent(Intent.ACTION_VIEW);
- intent.setData(Uri.parse("http://code.google.com/p/quickdic-dictionary/issues/entry"));
- context.startActivity(intent);
- return false;
- }
- });
- }
-
- public synchronized File getDictDir() {
- // This metaphor doesn't work, because we've already reset prefsMightHaveChanged.
-// if (dictDir == null || PreferenceActivity.prefsMightHaveChanged) {
- final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- final File defaultDictDir = new File(Environment.getExternalStorageDirectory(), "quickDic");
- String dir = prefs.getString(getString(R.string.quickdicDirectoryKey), defaultDictDir.getAbsolutePath());
- if (dir.isEmpty()) {
- dir = defaultDictDir.getAbsolutePath();
- }
- dictDir = new File(dir);
- dictDir.mkdirs();
- return dictDir;
- }
-
- public C.Theme getSelectedTheme() {
- final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- final String theme = prefs.getString(getString(R.string.themeKey), "themeLight");
- if (theme.equals("themeLight")) {
- return C.Theme.LIGHT;
- } else {
- return C.Theme.DEFAULT;
+ dictDir = new File(dir);
+ dictDir.mkdirs();
+ return dictDir;
}
- }
-
- public File getPath(String uncompressedFilename) {
- return new File(getDictDir(), uncompressedFilename);
- }
-
-
- String defaultLangISO2 = Locale.getDefault().getLanguage().toLowerCase();
- String defaultLangName = null;
- final Map<String, String> fileToNameCache = new LinkedHashMap<String, String>();
-
- public String isoCodeToLocalizedLanguageName(final String isoCode) {
- final Language.LanguageResources languageResources = Language.isoCodeToResources.get(isoCode);
- final String lang = languageResources != null ? getApplicationContext().getString(languageResources.nameId) : isoCode;
- return lang;
- }
-
- public List<IndexInfo> sortedIndexInfos(List<IndexInfo> indexInfos) {
- // Hack to put the default locale first in the name.
- if (indexInfos.size() > 1 &&
- indexInfos.get(1).shortName.toLowerCase().equals(defaultLangISO2)) {
- List<IndexInfo> result = new ArrayList<DictionaryInfo.IndexInfo>(indexInfos);
- ListUtil.swap(result, 0, 1);
- return result;
- }
- return indexInfos;
- }
-
-
- public synchronized String getDictionaryName(final String uncompressedFilename) {
- final String currentLocale = Locale.getDefault().getLanguage().toLowerCase();
- if (!currentLocale.equals(defaultLangISO2)) {
- defaultLangISO2 = currentLocale;
- fileToNameCache.clear();
- defaultLangName = null;
+
+ public C.Theme getSelectedTheme() {
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ final String theme = prefs.getString(getString(R.string.themeKey), "themeLight");
+ if (theme.equals("themeLight")) {
+ return C.Theme.LIGHT;
+ } else {
+ return C.Theme.DEFAULT;
+ }
}
- if (defaultLangName == null) {
- defaultLangName = isoCodeToLocalizedLanguageName(defaultLangISO2);
+
+ public File getPath(String uncompressedFilename) {
+ return new File(getDictDir(), uncompressedFilename);
}
-
- String name = fileToNameCache.get(uncompressedFilename);
- if (name != null) {
- return name;
+
+ String defaultLangISO2 = Locale.getDefault().getLanguage().toLowerCase();
+ String defaultLangName = null;
+ final Map<String, String> fileToNameCache = new LinkedHashMap<String, String>();
+
+ public String isoCodeToLocalizedLanguageName(final String isoCode) {
+ final Language.LanguageResources languageResources = Language.isoCodeToResources
+ .get(isoCode);
+ final String lang = languageResources != null ? getApplicationContext().getString(
+ languageResources.nameId) : isoCode;
+ return lang;
}
-
- final DictionaryInfo dictionaryInfo = DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.get(uncompressedFilename);
- if (dictionaryInfo != null) {
- final StringBuilder nameBuilder = new StringBuilder();
-
- List<IndexInfo> sortedIndexInfos = sortedIndexInfos(dictionaryInfo.indexInfos);
- for (int i = 0; i < sortedIndexInfos.size(); ++i) {
- if (i > 0) {
- nameBuilder.append("-");
+
+ public List<IndexInfo> sortedIndexInfos(List<IndexInfo> indexInfos) {
+ // Hack to put the default locale first in the name.
+ if (indexInfos.size() > 1 &&
+ indexInfos.get(1).shortName.toLowerCase().equals(defaultLangISO2)) {
+ List<IndexInfo> result = new ArrayList<DictionaryInfo.IndexInfo>(indexInfos);
+ ListUtil.swap(result, 0, 1);
+ return result;
}
- nameBuilder.append(isoCodeToLocalizedLanguageName(sortedIndexInfos.get(i).shortName));
- }
- name = nameBuilder.toString();
- } else {
- name = uncompressedFilename.replace(".quickdic", "");
+ return indexInfos;
}
- fileToNameCache.put(uncompressedFilename, name);
- return name;
- }
-
- public View createButton(final Context context, final DictionaryInfo dictionaryInfo,
- final IndexInfo indexInfo) {
- LanguageResources languageResources = Language.isoCodeToResources.get(indexInfo.shortName);
- View result;
-
- if (languageResources == null || languageResources.flagId <= 0) {
- Button button = new Button(context);
- button.setText(indexInfo.shortName);
- result = button;
- } else {
- ImageButton button = new ImageButton(context);
- button.setImageResource(languageResources.flagId);
- button.setScaleType(ScaleType.FIT_CENTER);
- result = button;
- }
- result.setMinimumWidth(languageButtonPixels);
- result.setMinimumHeight(languageButtonPixels * 2 / 3);
-// result.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
- return result;
- }
-
-
- public synchronized void moveDictionaryToTop(final DictionaryInfo dictionaryInfo) {
- dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename);
- dictionaryConfig.dictionaryFilesOrdered.add(0, dictionaryInfo.uncompressedFilename);
- PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
- }
-
- public synchronized void deleteDictionary(final DictionaryInfo dictionaryInfo) {
- while (dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename)) {};
- dictionaryConfig.uncompressedFilenameToDictionaryInfo.remove(dictionaryInfo.uncompressedFilename);
- getPath(dictionaryInfo.uncompressedFilename).delete();
- PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
- }
-
- final Collator collator = Collator.getInstance();
- final Comparator<String> uncompressedFilenameComparator = new Comparator<String>() {
- @Override
- public int compare(String uncompressedFilename1, String uncompressedFilename2) {
- final String name1 = getDictionaryName(uncompressedFilename1);
- final String name2 = getDictionaryName(uncompressedFilename2);
- if (defaultLangName.length() > 0) {
- if (name1.startsWith(defaultLangName + "-") && !name2.startsWith(defaultLangName + "-")) {
- return -1;
- } else if (name2.startsWith(defaultLangName + "-") && !name1.startsWith(defaultLangName + "-")) {
- return 1;
+
+ public synchronized String getDictionaryName(final String uncompressedFilename) {
+ final String currentLocale = Locale.getDefault().getLanguage().toLowerCase();
+ if (!currentLocale.equals(defaultLangISO2)) {
+ defaultLangISO2 = currentLocale;
+ fileToNameCache.clear();
+ defaultLangName = null;
}
- }
- return collator.compare(name1, name2);
- }
- };
- final Comparator<DictionaryInfo> dictionaryInfoComparator = new Comparator<DictionaryInfo>() {
- @Override
- public int compare(DictionaryInfo d1, DictionaryInfo d2) {
- // Single-index dictionaries first.
- if (d1.indexInfos.size() != d2.indexInfos.size()) {
- return d1.indexInfos.size() - d2.indexInfos.size();
- }
- return uncompressedFilenameComparator.compare(d1.uncompressedFilename, d2.uncompressedFilename);
- }
- };
-
- public void backgroundUpdateDictionaries(final Runnable onUpdateFinished) {
- new Thread(new Runnable() {
- @Override
- public void run() {
- final DictionaryConfig oldDictionaryConfig = new DictionaryConfig();
- synchronized(this) {
- oldDictionaryConfig.dictionaryFilesOrdered.addAll(dictionaryConfig.dictionaryFilesOrdered);
+ if (defaultLangName == null) {
+ defaultLangName = isoCodeToLocalizedLanguageName(defaultLangISO2);
}
- final DictionaryConfig newDictionaryConfig = new DictionaryConfig();
- for (final String uncompressedFilename : oldDictionaryConfig.dictionaryFilesOrdered) {
- final File dictFile = getPath(uncompressedFilename);
- final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(dictFile);
- if (dictionaryInfo != null) {
- newDictionaryConfig.dictionaryFilesOrdered.add(uncompressedFilename);
- newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(uncompressedFilename, dictionaryInfo);
- }
+
+ String name = fileToNameCache.get(uncompressedFilename);
+ if (name != null) {
+ return name;
}
-
- // Are there dictionaries on the device that we didn't know about already?
- // Pick them up and put them at the end of the list.
- final List<String> toAddSorted = new ArrayList<String>();
- final File[] dictDirFiles = getDictDir().listFiles();
- if (dictDirFiles != null) {
- for (final File file : dictDirFiles) {
- if (file.getName().endsWith(".zip")) {
- if (DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.containsKey(file.getName().replace(".zip", ""))) {
- file.delete();
- }
+
+ final DictionaryInfo dictionaryInfo = DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
+ .get(uncompressedFilename);
+ if (dictionaryInfo != null) {
+ final StringBuilder nameBuilder = new StringBuilder();
+
+ List<IndexInfo> sortedIndexInfos = sortedIndexInfos(dictionaryInfo.indexInfos);
+ for (int i = 0; i < sortedIndexInfos.size(); ++i) {
+ if (i > 0) {
+ nameBuilder.append("-");
+ }
+ nameBuilder
+ .append(isoCodeToLocalizedLanguageName(sortedIndexInfos.get(i).shortName));
}
- if (!file.getName().endsWith(".quickdic")) {
- continue;
+ name = nameBuilder.toString();
+ } else {
+ name = uncompressedFilename.replace(".quickdic", "");
+ }
+ fileToNameCache.put(uncompressedFilename, name);
+ return name;
+ }
+
+ public View createButton(final Context context, final DictionaryInfo dictionaryInfo,
+ final IndexInfo indexInfo) {
+ LanguageResources languageResources = Language.isoCodeToResources.get(indexInfo.shortName);
+ View result;
+
+ if (languageResources == null || languageResources.flagId <= 0) {
+ Button button = new Button(context);
+ button.setText(indexInfo.shortName);
+ result = button;
+ } else {
+ ImageButton button = new ImageButton(context);
+ button.setImageResource(languageResources.flagId);
+ button.setScaleType(ScaleType.FIT_CENTER);
+ result = button;
+ }
+ result.setMinimumWidth(languageButtonPixels);
+ result.setMinimumHeight(languageButtonPixels * 2 / 3);
+ // result.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
+ // LayoutParams.WRAP_CONTENT));
+ return result;
+ }
+
+ public synchronized void moveDictionaryToTop(final DictionaryInfo dictionaryInfo) {
+ dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename);
+ dictionaryConfig.dictionaryFilesOrdered.add(0, dictionaryInfo.uncompressedFilename);
+ PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
+ }
+
+ public synchronized void deleteDictionary(final DictionaryInfo dictionaryInfo) {
+ while (dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename)) {
+ }
+ ;
+ dictionaryConfig.uncompressedFilenameToDictionaryInfo
+ .remove(dictionaryInfo.uncompressedFilename);
+ getPath(dictionaryInfo.uncompressedFilename).delete();
+ PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
+ }
+
+ final Collator collator = Collator.getInstance();
+ final Comparator<String> uncompressedFilenameComparator = new Comparator<String>() {
+ @Override
+ public int compare(String uncompressedFilename1, String uncompressedFilename2) {
+ final String name1 = getDictionaryName(uncompressedFilename1);
+ final String name2 = getDictionaryName(uncompressedFilename2);
+ if (defaultLangName.length() > 0) {
+ if (name1.startsWith(defaultLangName + "-")
+ && !name2.startsWith(defaultLangName + "-")) {
+ return -1;
+ } else if (name2.startsWith(defaultLangName + "-")
+ && !name1.startsWith(defaultLangName + "-")) {
+ return 1;
+ }
}
- if (newDictionaryConfig.uncompressedFilenameToDictionaryInfo.containsKey(file.getName())) {
- // We have it in our list already.
- continue;
+ return collator.compare(name1, name2);
+ }
+ };
+ final Comparator<DictionaryInfo> dictionaryInfoComparator = new Comparator<DictionaryInfo>() {
+ @Override
+ public int compare(DictionaryInfo d1, DictionaryInfo d2) {
+ // Single-index dictionaries first.
+ if (d1.indexInfos.size() != d2.indexInfos.size()) {
+ return d1.indexInfos.size() - d2.indexInfos.size();
}
- final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(file);
- if (dictionaryInfo == null) {
- Log.e(LOG, "Unable to parse dictionary: " + file.getPath());
- continue;
+ return uncompressedFilenameComparator.compare(d1.uncompressedFilename,
+ d2.uncompressedFilename);
+ }
+ };
+
+ public void backgroundUpdateDictionaries(final Runnable onUpdateFinished) {
+ new Thread(new Runnable() {
+ @Override
+ public void run() {
+ final DictionaryConfig oldDictionaryConfig = new DictionaryConfig();
+ synchronized (this) {
+ oldDictionaryConfig.dictionaryFilesOrdered
+ .addAll(dictionaryConfig.dictionaryFilesOrdered);
+ }
+ final DictionaryConfig newDictionaryConfig = new DictionaryConfig();
+ for (final String uncompressedFilename : oldDictionaryConfig.dictionaryFilesOrdered) {
+ final File dictFile = getPath(uncompressedFilename);
+ final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(dictFile);
+ if (dictionaryInfo != null) {
+ newDictionaryConfig.dictionaryFilesOrdered.add(uncompressedFilename);
+ newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(
+ uncompressedFilename, dictionaryInfo);
+ }
+ }
+
+ // Are there dictionaries on the device that we didn't know
+ // about already?
+ // Pick them up and put them at the end of the list.
+ final List<String> toAddSorted = new ArrayList<String>();
+ final File[] dictDirFiles = getDictDir().listFiles();
+ if (dictDirFiles != null) {
+ for (final File file : dictDirFiles) {
+ if (file.getName().endsWith(".zip")) {
+ if (DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
+ .containsKey(file.getName().replace(".zip", ""))) {
+ file.delete();
+ }
+ }
+ if (!file.getName().endsWith(".quickdic")) {
+ continue;
+ }
+ if (newDictionaryConfig.uncompressedFilenameToDictionaryInfo
+ .containsKey(file.getName())) {
+ // We have it in our list already.
+ continue;
+ }
+ final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(file);
+ if (dictionaryInfo == null) {
+ Log.e(LOG, "Unable to parse dictionary: " + file.getPath());
+ continue;
+ }
+
+ toAddSorted.add(file.getName());
+ newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(
+ file.getName(), dictionaryInfo);
+ }
+ } else {
+ Log.w(LOG, "dictDir is not a diretory: " + getDictDir().getPath());
+ }
+ if (!toAddSorted.isEmpty()) {
+ Collections.sort(toAddSorted, uncompressedFilenameComparator);
+ newDictionaryConfig.dictionaryFilesOrdered.addAll(toAddSorted);
+ }
+
+ PersistentObjectCache.getInstance()
+ .write(C.DICTIONARY_CONFIGS, newDictionaryConfig);
+ synchronized (this) {
+ dictionaryConfig = newDictionaryConfig;
+ }
+
+ try {
+ onUpdateFinished.run();
+ } catch (Exception e) {
+ Log.e(LOG, "Exception running callback.", e);
+ }
}
-
- toAddSorted.add(file.getName());
- newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(file.getName(), dictionaryInfo);
- }
- } else {
- Log.w(LOG, "dictDir is not a diretory: " + getDictDir().getPath());
+ }).start();
+ }
+
+ public boolean matchesFilters(final DictionaryInfo dictionaryInfo, final String[] filters) {
+ if (filters == null) {
+ return true;
}
- if (!toAddSorted.isEmpty()) {
- Collections.sort(toAddSorted, uncompressedFilenameComparator);
- newDictionaryConfig.dictionaryFilesOrdered.addAll(toAddSorted);
+ for (final String filter : filters) {
+ if (!getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(
+ filter)) {
+ return false;
+ }
}
+ return true;
+ }
- PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, newDictionaryConfig);
- synchronized (this) {
- dictionaryConfig = newDictionaryConfig;
+ public synchronized List<DictionaryInfo> getDictionariesOnDevice(String[] filters) {
+ final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(
+ dictionaryConfig.dictionaryFilesOrdered.size());
+ for (final String uncompressedFilename : dictionaryConfig.dictionaryFilesOrdered) {
+ final DictionaryInfo dictionaryInfo = dictionaryConfig.uncompressedFilenameToDictionaryInfo
+ .get(uncompressedFilename);
+ if (dictionaryInfo != null && matchesFilters(dictionaryInfo, filters)) {
+ result.add(dictionaryInfo);
+ }
}
-
- try {
- onUpdateFinished.run();
- } catch (Exception e) {
- Log.e(LOG, "Exception running callback.", e);
+ return result;
+ }
+
+ public List<DictionaryInfo> getDownloadableDictionaries(String[] filters) {
+ final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(
+ dictionaryConfig.dictionaryFilesOrdered.size());
+
+ final Map<String, DictionaryInfo> remaining = new LinkedHashMap<String, DictionaryInfo>(
+ DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO);
+ remaining.keySet().removeAll(dictionaryConfig.dictionaryFilesOrdered);
+ for (final DictionaryInfo dictionaryInfo : remaining.values()) {
+ if (matchesFilters(dictionaryInfo, filters)) {
+ result.add(dictionaryInfo);
+ }
}
- }}).start();
- }
-
- public boolean matchesFilters(final DictionaryInfo dictionaryInfo, final String[] filters) {
- if (filters == null) {
- return true;
- }
- for (final String filter : filters) {
- if (!getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(filter)) {
- return false;
- }
- }
- return true;
- }
-
- public synchronized List<DictionaryInfo> getDictionariesOnDevice(String[] filters) {
- final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(dictionaryConfig.dictionaryFilesOrdered.size());
- for (final String uncompressedFilename : dictionaryConfig.dictionaryFilesOrdered) {
- final DictionaryInfo dictionaryInfo = dictionaryConfig.uncompressedFilenameToDictionaryInfo.get(uncompressedFilename);
- if (dictionaryInfo != null && matchesFilters(dictionaryInfo, filters)) {
- result.add(dictionaryInfo);
- }
+ Collections.sort(result, dictionaryInfoComparator);
+ return result;
+ }
+
+ public synchronized boolean isDictionaryOnDevice(String uncompressedFilename) {
+ return dictionaryConfig.uncompressedFilenameToDictionaryInfo.get(uncompressedFilename) != null;
+ }
+
+ public boolean updateAvailable(final DictionaryInfo dictionaryInfo) {
+ final DictionaryInfo downloadable =
+ DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.get(
+ dictionaryInfo.uncompressedFilename);
+ return downloadable != null &&
+ downloadable.creationMillis > dictionaryInfo.creationMillis;
+ }
+
+ public DictionaryInfo getDownloadable(final String uncompressedFilename) {
+ final DictionaryInfo downloadable = DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
+ .get(uncompressedFilename);
+ return downloadable;
}
- return result;
- }
-
- public List<DictionaryInfo> getDownloadableDictionaries(String[] filters) {
- final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(dictionaryConfig.dictionaryFilesOrdered.size());
-
- final Map<String, DictionaryInfo> remaining = new LinkedHashMap<String, DictionaryInfo>(DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO);
- remaining.keySet().removeAll(dictionaryConfig.dictionaryFilesOrdered);
- for (final DictionaryInfo dictionaryInfo : remaining.values()) {
- if (matchesFilters(dictionaryInfo, filters)) {
- result.add(dictionaryInfo);
- }
- }
- Collections.sort(result, dictionaryInfoComparator);
- return result;
- }
-
- public synchronized boolean isDictionaryOnDevice(String uncompressedFilename) {
- return dictionaryConfig.uncompressedFilenameToDictionaryInfo.get(uncompressedFilename) != null;
- }
-
- public boolean updateAvailable(final DictionaryInfo dictionaryInfo) {
- final DictionaryInfo downloadable =
- DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.get(
- dictionaryInfo.uncompressedFilename);
- return downloadable != null &&
- downloadable.creationMillis > dictionaryInfo.creationMillis;
- }
-
- public DictionaryInfo getDownloadable(final String uncompressedFilename) {
- final DictionaryInfo downloadable = DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.get(uncompressedFilename);
- return downloadable;
- }
}
import java.util.List;
public class DictionaryInfo implements Serializable {
-
- private static final long serialVersionUID = -6850863377577700388L;
-
- public static final class IndexInfo implements Serializable {
- private static final long serialVersionUID = 6524751236198309438L;
-
- public static final int NUM_CSV_FIELDS = 3;
-
- public final String shortName; // Often LangISO.
- public final int allTokenCount;
- public final int mainTokenCount;
-
- public IndexInfo(String shortName, int allTokenCount, int mainTokenCount) {
- this.shortName = shortName;
- this.allTokenCount = allTokenCount;
- this.mainTokenCount = mainTokenCount;
- }
-
- public StringBuilder append(StringBuilder result) {
- result.append(shortName);
- result.append("\t").append(allTokenCount);
- result.append("\t").append(mainTokenCount);
- return result;
- }
- public IndexInfo(final String[] fields, int i) {
- shortName = fields[i++];
- allTokenCount = Integer.parseInt(fields[i++]);
- mainTokenCount = Integer.parseInt(fields[i++]);
+ private static final long serialVersionUID = -6850863377577700388L;
+
+ public static final class IndexInfo implements Serializable {
+ private static final long serialVersionUID = 6524751236198309438L;
+
+ public static final int NUM_CSV_FIELDS = 3;
+
+ public final String shortName; // Often LangISO.
+ public final int allTokenCount;
+ public final int mainTokenCount;
+
+ public IndexInfo(String shortName, int allTokenCount, int mainTokenCount) {
+ this.shortName = shortName;
+ this.allTokenCount = allTokenCount;
+ this.mainTokenCount = mainTokenCount;
+ }
+
+ public StringBuilder append(StringBuilder result) {
+ result.append(shortName);
+ result.append("\t").append(allTokenCount);
+ result.append("\t").append(mainTokenCount);
+ return result;
+ }
+
+ public IndexInfo(final String[] fields, int i) {
+ shortName = fields[i++];
+ allTokenCount = Integer.parseInt(fields[i++]);
+ mainTokenCount = Integer.parseInt(fields[i++]);
+ }
}
- }
-
- // Stuff populated from the text file.
- public String uncompressedFilename; // used as a key throughout the program.
- public String downloadUrl;
- public long uncompressedBytes;
- public long zipBytes;
- public long creationMillis;
- public final List<IndexInfo> indexInfos = new ArrayList<DictionaryInfo.IndexInfo>();
- public String dictInfo;
-
- public DictionaryInfo() {
- // Blank object.
- }
-
- public StringBuilder append(final StringBuilder result) {
- result.append(uncompressedFilename);
- result.append("\t").append(downloadUrl);
- result.append("\t").append(creationMillis);
- result.append("\t").append(uncompressedBytes);
- result.append("\t").append(zipBytes);
- result.append("\t").append(indexInfos.size());
- for (final IndexInfo indexInfo : indexInfos) {
- indexInfo.append(result.append("\t"));
+
+ // Stuff populated from the text file.
+ public String uncompressedFilename; // used as a key throughout the program.
+ public String downloadUrl;
+ public long uncompressedBytes;
+ public long zipBytes;
+ public long creationMillis;
+ public final List<IndexInfo> indexInfos = new ArrayList<DictionaryInfo.IndexInfo>();
+ public String dictInfo;
+
+ public DictionaryInfo() {
+ // Blank object.
}
- result.append("\t").append(dictInfo.replaceAll("\n", "\\\\n"));
- return result;
- }
-
- public DictionaryInfo(final String line) {
- final String[] fields = line.split("\t");
- int i = 0;
- uncompressedFilename = fields[i++];
- downloadUrl = fields[i++];
- creationMillis = Long.parseLong(fields[i++]);
- uncompressedBytes = Long.parseLong(fields[i++]);
- zipBytes = Long.parseLong(fields[i++]);
- final int size = Integer.parseInt(fields[i++]);
- for (int j = 0; j < size; ++j) {
- indexInfos.add(new IndexInfo(fields, i));
- i += IndexInfo.NUM_CSV_FIELDS;
+
+ public StringBuilder append(final StringBuilder result) {
+ result.append(uncompressedFilename);
+ result.append("\t").append(downloadUrl);
+ result.append("\t").append(creationMillis);
+ result.append("\t").append(uncompressedBytes);
+ result.append("\t").append(zipBytes);
+ result.append("\t").append(indexInfos.size());
+ for (final IndexInfo indexInfo : indexInfos) {
+ indexInfo.append(result.append("\t"));
+ }
+ result.append("\t").append(dictInfo.replaceAll("\n", "\\\\n"));
+ return result;
}
- dictInfo = fields[i++].replaceAll("\\\\n", "\n");
- }
- @Override
- public String toString() {
- return uncompressedFilename;
- }
+ public DictionaryInfo(final String line) {
+ final String[] fields = line.split("\t");
+ int i = 0;
+ uncompressedFilename = fields[i++];
+ downloadUrl = fields[i++];
+ creationMillis = Long.parseLong(fields[i++]);
+ uncompressedBytes = Long.parseLong(fields[i++]);
+ zipBytes = Long.parseLong(fields[i++]);
+ final int size = Integer.parseInt(fields[i++]);
+ for (int j = 0; j < size; ++j) {
+ indexInfos.add(new IndexInfo(fields, i));
+ i += IndexInfo.NUM_CSV_FIELDS;
+ }
+ dictInfo = fields[i++].replaceAll("\\\\n", "\n");
+ }
+ @Override
+ public String toString() {
+ return uncompressedFilename;
+ }
}
+
package com.hughes.android.dictionary;
import java.io.Serializable;
public class DictionaryLink implements Serializable {
-
- private static final long serialVersionUID = -3842984045642836981L;
- final String uncompressedFilename;
- final int index;
- final String searchText;
+ private static final long serialVersionUID = -3842984045642836981L;
+
+ final String uncompressedFilename;
+ final int index;
+ final String searchText;
- private DictionaryLink(String uncompressedFilename, int index,
- String searchText) {
- this.uncompressedFilename = uncompressedFilename;
- this.index = index;
- this.searchText = searchText;
- }
+ private DictionaryLink(String uncompressedFilename, int index,
+ String searchText) {
+ this.uncompressedFilename = uncompressedFilename;
+ this.index = index;
+ this.searchText = searchText;
+ }
}
LinearLayout downloadableDictionariesHeaderRow;
Handler uiHandler;
-
+
Runnable dictionaryUpdater = new Runnable() {
@Override
public void run() {
});
}
};
-
+
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(LOG, "onCreate:" + this);
application = (DictionaryApplication) getApplication();
-
+
blockAutoLaunch = false;
-
+
// UI init.
setContentView(R.layout.dictionary_manager_activity);
- dictionariesOnDeviceHeaderRow = (LinearLayout) LayoutInflater.from(getListView().getContext()).inflate(
+ dictionariesOnDeviceHeaderRow = (LinearLayout) LayoutInflater.from(
+ getListView().getContext()).inflate(
R.layout.dictionary_manager_header_row_on_device, getListView(), false);
- downloadableDictionariesHeaderRow = (LinearLayout) LayoutInflater.from(getListView().getContext()).inflate(
+ downloadableDictionariesHeaderRow = (LinearLayout) LayoutInflater.from(
+ getListView().getContext()).inflate(
R.layout.dictionary_manager_header_row_downloadable, getListView(), false);
- showDownloadable = (ToggleButton) downloadableDictionariesHeaderRow.findViewById(R.id.hideDownloadable);
+ showDownloadable = (ToggleButton) downloadableDictionariesHeaderRow
+ .findViewById(R.id.hideDownloadable);
showDownloadable.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
prefs.edit().putString(C.THANKS_FOR_UPDATING_VERSION, thanksForUpdatingLatestVersion)
.commit();
}
-
-
+
registerReceiver(broadcastReceiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
-
+
setListAdapater();
registerForContextMenu(getListView());
-
+
final File dictDir = application.getDictDir();
if (!dictDir.canRead() || !dictDir.canExecute()) {
blockAutoLaunch = true;
-
+
AlertDialog.Builder builder = new AlertDialog.Builder(getListView().getContext());
builder.setTitle(getString(R.string.error));
builder.setMessage(getString(
- R.string.unableToReadDictionaryDir,
- dictDir.getAbsolutePath(),
+ R.string.unableToReadDictionaryDir,
+ dictDir.getAbsolutePath(),
Environment.getExternalStorageDirectory()));
builder.create().show();
}
-
+
onCreateSetupActionBar();
}
-
+
private void onCreateSetupActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowTitleEnabled(false);
-
+
filterSearchView = new SearchView(getSupportActionBar().getThemedContext());
filterSearchView.setIconifiedByDefault(false);
- // filterSearchView.setIconified(false); // puts the magnifying glass in the
+ // filterSearchView.setIconified(false); // puts the magnifying glass in
+ // the
// wrong place.
filterSearchView.setQueryHint(getString(R.string.searchText));
filterSearchView.setSubmitButtonEnabled(false);
// 11
EditorInfo.IME_MASK_ACTION |
EditorInfo.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
-
+
filterSearchView.setOnQueryTextListener(new OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return true;
}
-
+
@Override
public boolean onQueryTextChange(String filterText) {
setListAdapater();
actionBar.setDisplayShowCustomEnabled(true);
}
-
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(broadcastReceiver);
}
-
+
private static int copyStream(final InputStream in, final OutputStream out)
throws IOException {
int bytesRead;
prefs.contains(C.INDEX_SHORT_NAME)) {
Log.d(LOG, "Skipping DictionaryManager, going straight to dictionary.");
startActivity(DictionaryActivity.getLaunchIntent(
- new File(prefs.getString(C.DICT_FILE, "")), prefs.getString(C.INDEX_SHORT_NAME, ""),
+ new File(prefs.getString(C.DICT_FILE, "")),
+ prefs.getString(C.INDEX_SHORT_NAME, ""),
prefs.getString(C.SEARCH_TOKEN, "")));
finish();
return;
}
-
+
// Remove the active dictionary from the prefs so we won't autolaunch
// next time.
final Editor editor = prefs.edit();
(AdapterContextMenuInfo) menuInfo;
final int position = adapterContextMenuInfo.position;
final MyListAdapter.Row row = (MyListAdapter.Row) getListAdapter().getItem(position);
-
+
if (row.dictionaryInfo == null) {
return;
}
setListAdapater();
return true;
}
- });
+ });
}
}
List<DictionaryInfo> dictionariesOnDevice;
List<DictionaryInfo> downloadableDictionaries;
-
+
class Row {
DictionaryInfo dictionaryInfo;
boolean onDevice;
-
+
private Row(DictionaryInfo dictinoaryInfo, boolean onDevice) {
this.dictionaryInfo = dictinoaryInfo;
this.onDevice = onDevice;
}
}
-
private MyListAdapter(final String[] filters) {
dictionariesOnDevice = application.getDictionariesOnDevice(filters);
if (showDownloadable.isChecked()) {
return new Row(null, true);
}
position -= 1;
-
+
if (position < dictionariesOnDevice.size()) {
return new Row(dictionariesOnDevice.get(position), true);
}
position -= dictionariesOnDevice.size();
-
+
if (position == 0) {
return new Row(null, false);
}
position -= 1;
-
+
assert position < downloadableDictionaries.size();
return new Row(downloadableDictionaries.get(position), false);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
- if (convertView instanceof LinearLayout &&
- convertView != dictionariesOnDeviceHeaderRow &&
+ if (convertView instanceof LinearLayout &&
+ convertView != dictionariesOnDeviceHeaderRow &&
convertView != downloadableDictionariesHeaderRow) {
- /* This is done to try to avoid leaking memory that used to
- * happen on Android 4.0.3 */
- ((LinearLayout)convertView).removeAllViews();
+ /*
+ * This is done to try to avoid leaking memory that used to
+ * happen on Android 4.0.3
+ */
+ ((LinearLayout) convertView).removeAllViews();
}
-
+
final Row row = getItem(position);
-
+
if (row.onDevice) {
if (row.dictionaryInfo == null) {
return dictionariesOnDeviceHeaderRow;
}
return createDictionaryRow(row.dictionaryInfo, parent, true);
}
-
+
if (row.dictionaryInfo == null) {
return downloadableDictionariesHeaderRow;
}
return createDictionaryRow(row.dictionaryInfo, parent, false);
}
-
+
}
-
+
private void setListAdapater() {
- final String filter = filterSearchView == null ? "" : filterSearchView.getQuery().toString();
+ final String filter = filterSearchView == null ? "" : filterSearchView.getQuery()
+ .toString();
final String[] filters = filter.trim().toLowerCase().split("(\\s|-)+");
setListAdapter(new MyListAdapter(filters));
}
- private View createDictionaryRow(final DictionaryInfo dictionaryInfo,
+ private View createDictionaryRow(final DictionaryInfo dictionaryInfo,
final ViewGroup parent, final boolean canLaunch) {
-
+
View row = LayoutInflater.from(parent.getContext()).inflate(
R.layout.dictionary_manager_row, parent, false);
final TextView name = (TextView) row.findViewById(R.id.dictionaryName);
final boolean updateAvailable = application.updateAvailable(dictionaryInfo);
final Button downloadButton = (Button) row.findViewById(R.id.downloadButton);
if (!canLaunch || updateAvailable) {
- downloadButton.setText(getString(R.string.downloadButton, application.getDownloadable(dictionaryInfo.uncompressedFilename).zipBytes / 1024.0 / 1024.0));
+ downloadButton
+ .setText(getString(
+ R.string.downloadButton,
+ application.getDownloadable(dictionaryInfo.uncompressedFilename).zipBytes / 1024.0 / 1024.0));
downloadButton.setMinWidth(application.languageButtonPixels * 3 / 2);
downloadButton.setOnClickListener(new OnClickListener() {
@Override
}
LinearLayout buttons = (LinearLayout) row.findViewById(R.id.dictionaryLauncherButtons);
- final List<IndexInfo> sortedIndexInfos = application.sortedIndexInfos(dictionaryInfo.indexInfos);
+ final List<IndexInfo> sortedIndexInfos = application
+ .sortedIndexInfos(dictionaryInfo.indexInfos);
final StringBuilder builder = new StringBuilder();
if (updateAvailable) {
builder.append(getString(R.string.updateButton));
}
for (IndexInfo indexInfo : sortedIndexInfos) {
- final View button = application.createButton(buttons.getContext(), dictionaryInfo, indexInfo);
+ final View button = application.createButton(buttons.getContext(), dictionaryInfo,
+ indexInfo);
buttons.addView(button);
-
+
if (canLaunch) {
button.setOnClickListener(
- new IntentLauncher(buttons.getContext(),
- DictionaryActivity.getLaunchIntent(
- application.getPath(dictionaryInfo.uncompressedFilename),
- indexInfo.shortName, "")));
+ new IntentLauncher(buttons.getContext(),
+ DictionaryActivity.getLaunchIntent(
+ application.getPath(dictionaryInfo.uncompressedFilename),
+ indexInfo.shortName, "")));
} else {
button.setEnabled(false);
if (builder.length() != 0) {
builder.append("; ");
}
- builder.append(getString(R.string.indexInfo, indexInfo.shortName, indexInfo.mainTokenCount));
+ builder.append(getString(R.string.indexInfo, indexInfo.shortName,
+ indexInfo.mainTokenCount));
}
details.setText(builder.toString());
-
+
if (canLaunch) {
row.setClickable(true);
- row.setOnClickListener(new IntentLauncher(parent.getContext(),
- DictionaryActivity.getLaunchIntent(
- application.getPath(dictionaryInfo.uncompressedFilename),
- dictionaryInfo.indexInfos.get(0).shortName, "")));
+ row.setOnClickListener(new IntentLauncher(parent.getContext(),
+ DictionaryActivity.getLaunchIntent(
+ application.getPath(dictionaryInfo.uncompressedFilename),
+ dictionaryInfo.indexInfos.get(0).shortName, "")));
row.setFocusable(true);
row.setLongClickable(true);
}
row.setBackgroundResource(android.R.drawable.menuitem_background);
-
+
return row;
}
-
+
private void downloadDictionary(final DictionaryInfo dictionaryInfo) {
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse(dictionaryInfo.downloadUrl));
try {
- final String destFile = new File(new URL(dictionaryInfo.downloadUrl).getFile()).getName();
+ final String destFile = new File(new URL(dictionaryInfo.downloadUrl).getFile())
+ .getName();
Log.d(LOG, "Downloading to: " + destFile);
-
- request.setDestinationUri(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), destFile)));
+
+ request.setDestinationUri(Uri.fromFile(new File(Environment
+ .getExternalStorageDirectory(), destFile)));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
import com.hughes.util.StringUtil;\r
\r
public final class HtmlDisplayActivity extends Activity {\r
- \r
- static final String LOG = "QuickDic";\r
\r
- \r
- static final String HTML_RES = "html_res";\r
- static final String HTML = "html";\r
- static final String TEXT_TO_HIGHLIGHT = "textToHighlight";\r
- static final String SHOW_OK_BUTTON = "showOKButton";\r
- \r
- public static Intent getHelpLaunchIntent() {\r
- final Intent intent = new Intent();\r
- intent.setClassName(HtmlDisplayActivity.class.getPackage().getName(), HtmlDisplayActivity.class.getName());\r
- intent.putExtra(HTML_RES, R.raw.help);\r
- return intent;\r
- }\r
+ static final String LOG = "QuickDic";\r
\r
- public static Intent getWhatsNewLaunchIntent() {\r
- final Intent intent = new Intent();\r
- intent.setClassName(HtmlDisplayActivity.class.getPackage().getName(), HtmlDisplayActivity.class.getName());\r
- intent.putExtra(HTML_RES, R.raw.whats_new);\r
- return intent;\r
- }\r
+ static final String HTML_RES = "html_res";\r
+ static final String HTML = "html";\r
+ static final String TEXT_TO_HIGHLIGHT = "textToHighlight";\r
+ static final String SHOW_OK_BUTTON = "showOKButton";\r
\r
- public static Intent getHtmlIntent(final String html, final String textToHighlight, final boolean showOkButton) {\r
- final Intent intent = new Intent();\r
- intent.setClassName(HtmlDisplayActivity.class.getPackage().getName(), HtmlDisplayActivity.class.getName());\r
- intent.putExtra(HTML, html);\r
- intent.putExtra(TEXT_TO_HIGHLIGHT, textToHighlight);\r
- intent.putExtra(SHOW_OK_BUTTON, showOkButton);\r
- return intent;\r
- }\r
-\r
- /** Called when the activity is first created. */\r
- @Override\r
- public void onCreate(final Bundle savedInstanceState) {\r
- setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);\r
+ public static Intent getHelpLaunchIntent() {\r
+ final Intent intent = new Intent();\r
+ intent.setClassName(HtmlDisplayActivity.class.getPackage().getName(),\r
+ HtmlDisplayActivity.class.getName());\r
+ intent.putExtra(HTML_RES, R.raw.help);\r
+ return intent;\r
+ }\r
\r
- super.onCreate(savedInstanceState);\r
- setContentView(R.layout.html_display_activity);\r
- \r
- final int htmlRes = getIntent().getIntExtra(HTML_RES, -1);\r
- final String html;\r
- if (htmlRes != -1) {\r
- html = StringUtil.readToString(getResources().openRawResource(htmlRes));\r
- } else {\r
- html = getIntent().getStringExtra(HTML);\r
+ public static Intent getWhatsNewLaunchIntent() {\r
+ final Intent intent = new Intent();\r
+ intent.setClassName(HtmlDisplayActivity.class.getPackage().getName(),\r
+ HtmlDisplayActivity.class.getName());\r
+ intent.putExtra(HTML_RES, R.raw.whats_new);\r
+ return intent;\r
}\r
- final MyWebView webView = (MyWebView) findViewById(R.id.webView);\r
- webView.loadData(html, "text/html", "utf-8");\r
- webView.activity = this;\r
- \r
- final String textToHighlight = getIntent().getStringExtra(TEXT_TO_HIGHLIGHT);\r
- if (textToHighlight != null && !"".equals(textToHighlight)) {\r
- Log.d(LOG, "NOT Highlighting text: " + textToHighlight);\r
- // This isn't working:\r
- // webView.findAll(textToHighlight);\r
- //webView.showFindDialog(textToHighlight, false);\r
+\r
+ public static Intent getHtmlIntent(final String html, final String textToHighlight,\r
+ final boolean showOkButton) {\r
+ final Intent intent = new Intent();\r
+ intent.setClassName(HtmlDisplayActivity.class.getPackage().getName(),\r
+ HtmlDisplayActivity.class.getName());\r
+ intent.putExtra(HTML, html);\r
+ intent.putExtra(TEXT_TO_HIGHLIGHT, textToHighlight);\r
+ intent.putExtra(SHOW_OK_BUTTON, showOkButton);\r
+ return intent;\r
}\r
- \r
- final Button okButton = (Button) findViewById(R.id.okButton);\r
- okButton.setOnClickListener(new OnClickListener() {\r
- @Override\r
- public void onClick(View v) {\r
- finish();\r
- }\r
- });\r
- if (!getIntent().getBooleanExtra(SHOW_OK_BUTTON, true)) {\r
- okButton.setVisibility(Button.GONE);\r
+\r
+ /** Called when the activity is first created. */\r
+ @Override\r
+ public void onCreate(final Bundle savedInstanceState) {\r
+ setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);\r
+\r
+ super.onCreate(savedInstanceState);\r
+ setContentView(R.layout.html_display_activity);\r
+\r
+ final int htmlRes = getIntent().getIntExtra(HTML_RES, -1);\r
+ final String html;\r
+ if (htmlRes != -1) {\r
+ html = StringUtil.readToString(getResources().openRawResource(htmlRes));\r
+ } else {\r
+ html = getIntent().getStringExtra(HTML);\r
+ }\r
+ final MyWebView webView = (MyWebView) findViewById(R.id.webView);\r
+ webView.loadData(html, "text/html", "utf-8");\r
+ webView.activity = this;\r
+\r
+ final String textToHighlight = getIntent().getStringExtra(TEXT_TO_HIGHLIGHT);\r
+ if (textToHighlight != null && !"".equals(textToHighlight)) {\r
+ Log.d(LOG, "NOT Highlighting text: " + textToHighlight);\r
+ // This isn't working:\r
+ // webView.findAll(textToHighlight);\r
+ // webView.showFindDialog(textToHighlight, false);\r
+ }\r
+\r
+ final Button okButton = (Button) findViewById(R.id.okButton);\r
+ okButton.setOnClickListener(new OnClickListener() {\r
+ @Override\r
+ public void onClick(View v) {\r
+ finish();\r
+ }\r
+ });\r
+ if (!getIntent().getBooleanExtra(SHOW_OK_BUTTON, true)) {\r
+ okButton.setVisibility(Button.GONE);\r
+ }\r
}\r
- }\r
\r
}\r
+
package com.hughes.android.dictionary;
import android.app.Activity;
import com.hughes.android.dictionary.engine.HtmlEntry;
public class MyWebView extends WebView {
-
+
static final String LOG = "MyWebView";
-
+
HtmlDisplayActivity activity;
public MyWebView(Context context) {
super(context);
}
-
+
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
getSettings().setSupportZoom(true);
getSettings().setBuiltInZoomControls(true);
-
+
final WebViewClient webViewClient = new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
import android.preference.ListPreference;
public class PreferenceActivity extends android.preference.PreferenceActivity {
-
- static boolean prefsMightHaveChanged = false;
-
- @SuppressWarnings("deprecation")
- @Override
- public void onCreate(Bundle savedInstanceState) {
- setTheme(((DictionaryApplication)getApplication()).getSelectedTheme().themeId);
-
- /**
- * @author Dominik Köppl
- * Preference: select default dictionary
- * As this list is dynamically generated, we have to do it in this fashion
- */
- super.onCreate(savedInstanceState);
- addPreferencesFromResource(R.xml.preferences);
- ListPreference defaultDic = (ListPreference) findPreference(getResources().getString(R.string.defaultDicKey));
- DictionaryApplication application = (DictionaryApplication) getApplication();
- List<DictionaryInfo> dicts = application.getDictionariesOnDevice(null);
-
- final CharSequence[] entries = new CharSequence[dicts.size()];
- final CharSequence[] entryvalues = new CharSequence[dicts.size()];
-
- for(int i = 0; i < entries.length; ++i)
- {
- entries[i] = dicts.get(i).dictInfo;
- entryvalues[i] = dicts.get(i).uncompressedFilename;
- }
-
- defaultDic.setEntries(entries);
- defaultDic.setEntryValues(entryvalues);
- }
-
- @Override
- public void onContentChanged() {
- super.onContentChanged();
- }
+
+ static boolean prefsMightHaveChanged = false;
+
+ @SuppressWarnings("deprecation")
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ setTheme(((DictionaryApplication) getApplication()).getSelectedTheme().themeId);
+
+ /**
+ * @author Dominik Köppl Preference: select default dictionary As this
+ * list is dynamically generated, we have to do it in this
+ * fashion
+ */
+ super.onCreate(savedInstanceState);
+ addPreferencesFromResource(R.xml.preferences);
+ ListPreference defaultDic = (ListPreference) findPreference(getResources().getString(
+ R.string.defaultDicKey));
+ DictionaryApplication application = (DictionaryApplication) getApplication();
+ List<DictionaryInfo> dicts = application.getDictionariesOnDevice(null);
+
+ final CharSequence[] entries = new CharSequence[dicts.size()];
+ final CharSequence[] entryvalues = new CharSequence[dicts.size()];
+
+ for (int i = 0; i < entries.length; ++i)
+ {
+ entries[i] = dicts.get(i).dictInfo;
+ entryvalues[i] = dicts.get(i).uncompressedFilename;
+ }
+
+ defaultDic.setEntries(entries);
+ defaultDic.setEntryValues(entryvalues);
+ }
+
+ @Override
+ public void onContentChanged() {
+ super.onContentChanged();
+ }
}
import java.io.IOException;
import java.io.RandomAccessFile;
-
public abstract class AbstractEntry extends IndexedObject {
-
- public final EntrySource entrySource;
-
- protected AbstractEntry(EntrySource entrySource) {
- super(-1);
- this.entrySource = entrySource;
- }
-
- public AbstractEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException {
- super(index);
- if (dictionary.dictFileVersion >= 1) {
- final int entrySouceIdx = raf.readShort();
- this.entrySource = dictionary.sources.get(entrySouceIdx);
- } else {
- this.entrySource = null;
+
+ public final EntrySource entrySource;
+
+ protected AbstractEntry(EntrySource entrySource) {
+ super(-1);
+ this.entrySource = entrySource;
}
- }
- public void write(RandomAccessFile raf) throws IOException {
- raf.writeShort(entrySource.index());
- }
+ public AbstractEntry(Dictionary dictionary, RandomAccessFile raf, final int index)
+ throws IOException {
+ super(index);
+ if (dictionary.dictFileVersion >= 1) {
+ final int entrySouceIdx = raf.readShort();
+ this.entrySource = dictionary.sources.get(entrySouceIdx);
+ } else {
+ this.entrySource = null;
+ }
+ }
+
+ public void write(RandomAccessFile raf) throws IOException {
+ raf.writeShort(entrySource.index());
+ }
- public abstract void addToDictionary(final Dictionary dictionary);
+ public abstract void addToDictionary(final Dictionary dictionary);
- public abstract RowBase CreateRow(int rowIndex, Index dictionaryIndex);
+ public abstract RowBase CreateRow(int rowIndex, Index dictionaryIndex);
}
import java.util.Collections;
import java.util.List;
-
public class Dictionary implements RAFSerializable<Dictionary> {
-
- static final int CACHE_SIZE = 5000;
-
- static final int CURRENT_DICT_VERSION = 6;
- static final String END_OF_DICTIONARY = "END OF DICTIONARY";
-
- // persisted
- final int dictFileVersion;
- final long creationMillis;
- public final String dictInfo;
- public final List<PairEntry> pairEntries;
- public final List<TextEntry> textEntries;
- public final List<HtmlEntry> htmlEntries;
- public final List<EntrySource> sources;
- public final List<Index> indices;
-
- /**
- * dictFileVersion 1 adds:
- * <li> links to sources?
- *
- * dictFileVersion 2 adds:
- * <li> counts of tokens in indices.
- */
-
- public Dictionary(final String dictInfo) {
- this.dictFileVersion = CURRENT_DICT_VERSION;
- this.creationMillis = System.currentTimeMillis();
- this.dictInfo = dictInfo;
- pairEntries = new ArrayList<PairEntry>();
- textEntries = new ArrayList<TextEntry>();
- htmlEntries = new ArrayList<HtmlEntry>();
- sources = new ArrayList<EntrySource>();
- indices = new ArrayList<Index>();
- }
-
- public Dictionary(final RandomAccessFile raf) throws IOException {
- dictFileVersion = raf.readInt();
- if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
- throw new IOException("Invalid dictionary version: " + dictFileVersion);
- }
- creationMillis = raf.readLong();
- dictInfo = raf.readUTF();
-
- // Load the sources, then seek past them, because reading them later disrupts the offset.
- try {
- final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(this), raf.getFilePointer());
- sources = new ArrayList<EntrySource>(rafSources);
- raf.seek(rafSources.getEndOffset());
-
- pairEntries = CachingList.create(RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
- textEntries = CachingList.create(RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
- if (dictFileVersion >= 5) {
- htmlEntries = CachingList.create(RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
- } else {
- htmlEntries = Collections.emptyList();
- }
- indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer, raf.getFilePointer()));
- } catch (RuntimeException e) {
- final IOException ioe = new IOException("RuntimeException loading dictionary");
- ioe.initCause(e);
- throw ioe;
+
+ static final int CACHE_SIZE = 5000;
+
+ static final int CURRENT_DICT_VERSION = 6;
+ static final String END_OF_DICTIONARY = "END OF DICTIONARY";
+
+ // persisted
+ final int dictFileVersion;
+ final long creationMillis;
+ public final String dictInfo;
+ public final List<PairEntry> pairEntries;
+ public final List<TextEntry> textEntries;
+ public final List<HtmlEntry> htmlEntries;
+ public final List<EntrySource> sources;
+ public final List<Index> indices;
+
+ /**
+ * dictFileVersion 1 adds: <li>links to sources? dictFileVersion 2 adds: <li>
+ * counts of tokens in indices.
+ */
+
+ public Dictionary(final String dictInfo) {
+ this.dictFileVersion = CURRENT_DICT_VERSION;
+ this.creationMillis = System.currentTimeMillis();
+ this.dictInfo = dictInfo;
+ pairEntries = new ArrayList<PairEntry>();
+ textEntries = new ArrayList<TextEntry>();
+ htmlEntries = new ArrayList<HtmlEntry>();
+ sources = new ArrayList<EntrySource>();
+ indices = new ArrayList<Index>();
}
- final String end = raf.readUTF();
- if (!end.equals(END_OF_DICTIONARY)) {
- throw new IOException("Dictionary seems corrupt: " + end);
+
+ public Dictionary(final RandomAccessFile raf) throws IOException {
+ dictFileVersion = raf.readInt();
+ if (dictFileVersion < 0 || dictFileVersion > CURRENT_DICT_VERSION) {
+ throw new IOException("Invalid dictionary version: " + dictFileVersion);
+ }
+ creationMillis = raf.readLong();
+ dictInfo = raf.readUTF();
+
+ // Load the sources, then seek past them, because reading them later
+ // disrupts the offset.
+ try {
+ final RAFList<EntrySource> rafSources = RAFList.create(raf, new EntrySource.Serializer(
+ this), raf.getFilePointer());
+ sources = new ArrayList<EntrySource>(rafSources);
+ raf.seek(rafSources.getEndOffset());
+
+ pairEntries = CachingList.create(
+ RAFList.create(raf, new PairEntry.Serializer(this), raf.getFilePointer()),
+ CACHE_SIZE);
+ textEntries = CachingList.create(
+ RAFList.create(raf, new TextEntry.Serializer(this), raf.getFilePointer()),
+ CACHE_SIZE);
+ if (dictFileVersion >= 5) {
+ htmlEntries = CachingList.create(
+ RAFList.create(raf, new HtmlEntry.Serializer(this), raf.getFilePointer()),
+ CACHE_SIZE);
+ } else {
+ htmlEntries = Collections.emptyList();
+ }
+ indices = CachingList.createFullyCached(RAFList.create(raf, indexSerializer,
+ raf.getFilePointer()));
+ } catch (RuntimeException e) {
+ final IOException ioe = new IOException("RuntimeException loading dictionary");
+ ioe.initCause(e);
+ throw ioe;
+ }
+ final String end = raf.readUTF();
+ if (!end.equals(END_OF_DICTIONARY)) {
+ throw new IOException("Dictionary seems corrupt: " + end);
+ }
}
- }
-
- @Override
- public void write(RandomAccessFile raf) throws IOException {
- raf.writeInt(dictFileVersion);
- raf.writeLong(creationMillis);
- raf.writeUTF(dictInfo);
- RAFList.write(raf, sources, new EntrySource.Serializer(this));
- RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
- RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
- RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this));
- RAFList.write(raf, indices, indexSerializer);
- raf.writeUTF(END_OF_DICTIONARY);
- }
-
- private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
+
@Override
- public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
- return new Index(Dictionary.this, raf);
+ public void write(RandomAccessFile raf) throws IOException {
+ raf.writeInt(dictFileVersion);
+ raf.writeLong(creationMillis);
+ raf.writeUTF(dictInfo);
+ RAFList.write(raf, sources, new EntrySource.Serializer(this));
+ RAFList.write(raf, pairEntries, new PairEntry.Serializer(this));
+ RAFList.write(raf, textEntries, new TextEntry.Serializer(this));
+ RAFList.write(raf, htmlEntries, new HtmlEntry.Serializer(this));
+ RAFList.write(raf, indices, indexSerializer);
+ raf.writeUTF(END_OF_DICTIONARY);
}
- @Override
- public void write(RandomAccessFile raf, Index t) throws IOException {
- t.write(raf);
- }};
-
+
+ private final RAFListSerializer<Index> indexSerializer = new RAFListSerializer<Index>() {
+ @Override
+ public Index read(RandomAccessFile raf, final int readIndex) throws IOException {
+ return new Index(Dictionary.this, raf);
+ }
+
+ @Override
+ public void write(RandomAccessFile raf, Index t) throws IOException {
+ t.write(raf);
+ }
+ };
+
final RAFListSerializer<HtmlEntry> htmlEntryIndexSerializer = new RAFListSerializer<HtmlEntry>() {
@Override
public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
- if (t.index() == -1) throw new IndexOutOfBoundsException();
+ if (t.index() == -1)
+ throw new IndexOutOfBoundsException();
raf.writeInt(t.index());
}
+
@Override
public HtmlEntry read(RandomAccessFile raf, int readIndex) throws IOException {
return htmlEntries.get(raf.readInt());
- }};
-
+ }
+ };
+
public void print(final PrintStream out) {
- out.println("dictInfo=" + dictInfo);
- for (final EntrySource entrySource : sources) {
- out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
- }
- out.println();
- for (final Index index : indices) {
- out.printf("Index: %s %s\n", index.shortName, index.longName);
- index.print(out);
+ out.println("dictInfo=" + dictInfo);
+ for (final EntrySource entrySource : sources) {
+ out.printf("EntrySource: %s %d\n", entrySource.name, entrySource.numEntries);
+ }
out.println();
- }
+ for (final Index index : indices) {
+ out.printf("Index: %s %s\n", index.shortName, index.longName);
+ index.print(out);
+ out.println();
+ }
}
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;
+ 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;
}
-
+
public static DictionaryInfo getDictionaryInfo(final File file) {
- RandomAccessFile raf = null;
- try {
- raf = new RandomAccessFile(file, "r");
- final Dictionary dict = new Dictionary(raf);
- final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
- dictionaryInfo.uncompressedFilename = file.getName();
- dictionaryInfo.uncompressedBytes = file.length();
- raf.close();
- return dictionaryInfo;
- } catch (IOException e) {
- return null;
- } finally {
- if (raf != null) {
- try {
+ RandomAccessFile raf = null;
+ try {
+ raf = new RandomAccessFile(file, "r");
+ final Dictionary dict = new Dictionary(raf);
+ final DictionaryInfo dictionaryInfo = dict.getDictionaryInfo();
+ dictionaryInfo.uncompressedFilename = file.getName();
+ dictionaryInfo.uncompressedBytes = file.length();
raf.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
+ return dictionaryInfo;
+ } catch (IOException e) {
+ return null;
+ } finally {
+ if (raf != null) {
+ try {
+ raf.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
}
- }
}
-}
\ No newline at end of file
+}
import java.io.Serializable;
public class EntrySource extends IndexedObject implements Serializable {
-
- private static final long serialVersionUID = -1323165134846120269L;
-
- final String name;
- int numEntries;
-
- public EntrySource(final int index, final String name, int numEntries) {
- super(index);
- this.name = name;
- this.numEntries = numEntries;
- }
-
- @Override
- public String toString() {
- return name;
- }
-
-
-
-
- public int getNumEntries() {
- return numEntries;
- }
-
- public String getName() {
- return name;
- }
-
-
- public static final class Serializer implements RAFListSerializer<EntrySource> {
-
- final Dictionary dictionary;
-
- Serializer(Dictionary dictionary) {
- this.dictionary = dictionary;
+
+ private static final long serialVersionUID = -1323165134846120269L;
+
+ final String name;
+ int numEntries;
+
+ public EntrySource(final int index, final String name, int numEntries) {
+ super(index);
+ this.name = name;
+ this.numEntries = numEntries;
}
@Override
- public EntrySource read(RandomAccessFile raf, int readIndex)
- throws IOException {
- final String name = raf.readUTF();
- final int numEntries = dictionary.dictFileVersion >= 3 ? raf.readInt() : 0;
- return new EntrySource(readIndex, name, numEntries);
+ public String toString() {
+ return name;
}
- @Override
- public void write(RandomAccessFile raf, EntrySource t) throws IOException {
- raf.writeUTF(t.name);
- raf.writeInt(t.numEntries);
- }
- };
-
+ public int getNumEntries() {
+ return numEntries;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public static final class Serializer implements RAFListSerializer<EntrySource> {
+
+ final Dictionary dictionary;
+
+ Serializer(Dictionary dictionary) {
+ this.dictionary = dictionary;
+ }
+
+ @Override
+ public EntrySource read(RandomAccessFile raf, int readIndex)
+ throws IOException {
+ final String name = raf.readUTF();
+ final int numEntries = dictionary.dictFileVersion >= 3 ? raf.readInt() : 0;
+ return new EntrySource(readIndex, name, numEntries);
+ }
+
+ @Override
+ public void write(RandomAccessFile raf, EntrySource t) throws IOException {
+ raf.writeUTF(t.name);
+ raf.writeInt(t.numEntries);
+ }
+ };
+
}
package com.hughes.android.dictionary.engine;
-
public enum EntryTypeName {
- WIKTIONARY_TITLE_SINGLE_DETAIL(true, true, null),
- WIKTIONARY_TITLE_SINGLE(true, true, null),
- WIKTIONARY_INFLECTD_FORM_SINGLE(false, true, null),
+ WIKTIONARY_TITLE_SINGLE_DETAIL(true, true, null),
+ WIKTIONARY_TITLE_SINGLE(true, true, null),
+ WIKTIONARY_INFLECTD_FORM_SINGLE(false, true, null),
+
+ ONE_WORD(true, true, null),
+ MULTIROW_HEAD_ONE_WORD(true, true, null),
+ MULTIROW_TAIL_ONE_WORD(false, true, null),
+ SYNONYM_SINGLE(false, true, null),
+ ANTONYM_SINGLE(false, true, null),
- ONE_WORD(true, true, null),
- MULTIROW_HEAD_ONE_WORD(true, true, null),
- MULTIROW_TAIL_ONE_WORD(false, true, null),
+ WIKTIONARY_TITLE_MULTI_DETAIL(false, true, WIKTIONARY_TITLE_SINGLE_DETAIL),
+ WIKTIONARY_TITLE_MULTI(false, true, WIKTIONARY_TITLE_SINGLE),
+ WIKTIONARY_TRANSLITERATION(),
+ // How we file "casa {f}, case {pl}" under "case"
+ WIKTIONARY_INFLECTED_FORM_MULTI(false, true, WIKTIONARY_INFLECTD_FORM_SINGLE),
+ WIKTIONARY_ENGLISH_DEF_WIKI_LINK(),
+ WIKTIONARY_ENGLISH_DEF_OTHER_LANG(),
+ WIKTIONARY_ENGLISH_DEF(),
- SYNONYM_SINGLE(false, true, null),
- ANTONYM_SINGLE(false, true, null),
+ SYNONYM_MULTI(false, true, SYNONYM_SINGLE),
+ ANTONYM_MULTI(false, true, ANTONYM_SINGLE),
+ DERIVED_TERM(false, true, null),
- WIKTIONARY_TITLE_MULTI_DETAIL(false, true, WIKTIONARY_TITLE_SINGLE_DETAIL),
- WIKTIONARY_TITLE_MULTI(false, true, WIKTIONARY_TITLE_SINGLE),
- WIKTIONARY_TRANSLITERATION(),
- // How we file "casa {f}, case {pl}" under "case"
- WIKTIONARY_INFLECTED_FORM_MULTI(false, true, WIKTIONARY_INFLECTD_FORM_SINGLE),
- WIKTIONARY_ENGLISH_DEF_WIKI_LINK(),
- WIKTIONARY_ENGLISH_DEF_OTHER_LANG(),
- WIKTIONARY_ENGLISH_DEF(),
-
- SYNONYM_MULTI(false, true, SYNONYM_SINGLE),
- ANTONYM_MULTI(false, true, ANTONYM_SINGLE),
- DERIVED_TERM(false, true, null),
+ TWO_WORDS(),
+ THREE_WORDS(),
+ FOUR_WORDS(),
+ FIVE_OR_MORE_WORDS(),
+ WIKTIONARY_TRANSLATION_WIKI_TEXT(),
+ WIKTIONARY_TRANSLATION_OTHER_TEXT(),
- TWO_WORDS(),
- THREE_WORDS(),
- FOUR_WORDS(),
- FIVE_OR_MORE_WORDS(),
- WIKTIONARY_TRANSLATION_WIKI_TEXT(),
- WIKTIONARY_TRANSLATION_OTHER_TEXT(),
+ // How we file entries like: "sono: {form of|essere}" under "sono.".
+ WIKTIONARY_IS_FORM_OF_SOMETHING_ELSE(false, true, null),
- // How we file entries like: "sono: {form of|essere}" under "sono.".
- WIKTIONARY_IS_FORM_OF_SOMETHING_ELSE(false, true, null),
+ MULTIROW_HEAD_MANY_WORDS(),
+ MULTIROW_TAIL_MANY_WORDS(),
+ WIKTIONARY_EXAMPLE(),
- MULTIROW_HEAD_MANY_WORDS(),
- MULTIROW_TAIL_MANY_WORDS(),
- WIKTIONARY_EXAMPLE(),
+ // The next two are how we file entries like: "sono: {form of|essere}" under
+ // "essere".
+ WIKTIONARY_BASE_FORM_SINGLE(), // These two should be eligible for removal
+ // if the links are otherwise present.
+ WIKTIONARY_BASE_FORM_MULTI(false, false, WIKTIONARY_BASE_FORM_SINGLE),
+ PART_OF_HYPHENATED(),
+ BRACKETED(),
+ PARENTHESIZED(),
+ WIKTIONARY_TRANSLATION_SENSE(),
+ SEE_ALSO(),
+ WIKTIONARY_MENTIONED(false, true, null), ;
- // The next two are how we file entries like: "sono: {form of|essere}" under "essere".
- WIKTIONARY_BASE_FORM_SINGLE(), // These two should be eligible for removal if the links are otherwise present.
- WIKTIONARY_BASE_FORM_MULTI(false, false, WIKTIONARY_BASE_FORM_SINGLE),
- PART_OF_HYPHENATED(),
- BRACKETED(),
- PARENTHESIZED(),
- WIKTIONARY_TRANSLATION_SENSE(),
- SEE_ALSO(),
- WIKTIONARY_MENTIONED(false, true, null),
- ;
+ final boolean mainWord;
+ final boolean overridesStopList;
+ final EntryTypeName singleWordInstance;
- final boolean mainWord;
- final boolean overridesStopList;
- final EntryTypeName singleWordInstance;
-
- private EntryTypeName() {
- this(false, false, null);
- }
+ private EntryTypeName() {
+ this(false, false, null);
+ }
- private EntryTypeName(final boolean mainWord, final boolean overridesStopList, final EntryTypeName singleWordInstance) {
- this.mainWord = mainWord;
- this.overridesStopList = overridesStopList;
- this.singleWordInstance = singleWordInstance == null ? this : singleWordInstance;
- }
+ private EntryTypeName(final boolean mainWord, final boolean overridesStopList,
+ final EntryTypeName singleWordInstance) {
+ this.mainWord = mainWord;
+ this.overridesStopList = overridesStopList;
+ this.singleWordInstance = singleWordInstance == null ? this : singleWordInstance;
+ }
}
+
package com.hughes.android.dictionary.engine;
import android.content.Intent;
import java.util.List;
import java.util.regex.Pattern;
-public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>, Comparable<HtmlEntry> {
-
- // Title is not HTML escaped.
- public final String title;
- public final LazyHtmlLoader lazyHtmlLoader;
- public String html;
-
- public HtmlEntry(final EntrySource entrySource, String title) {
- super(entrySource);
- this.title = title;
- lazyHtmlLoader = null;
- }
-
- public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index) throws IOException {
- super(dictionary, raf, index);
- title = raf.readUTF();
- lazyHtmlLoader = new LazyHtmlLoader(raf);
- html = null;
- }
-
- @Override
- public void write(RandomAccessFile raf) throws IOException {
- super.write(raf);
- raf.writeUTF(title);
-
- final byte[] bytes = getHtml().getBytes("UTF-8");
- final byte[] zipBytes = StringUtil.zipBytes(bytes);
- raf.writeInt(bytes.length);
- raf.writeInt(zipBytes.length);
- raf.write(zipBytes);
- }
-
- String getHtml() {
- return html != null ? html : lazyHtmlLoader.getHtml();
- }
-
- @Override
- public void addToDictionary(Dictionary dictionary) {
- assert index == -1;
- dictionary.htmlEntries.add(this);
- index = dictionary.htmlEntries.size() - 1;
- }
-
- @Override
- public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
- return new Row(this.index, rowIndex, dictionaryIndex);
- }
-
- static final class Serializer implements RAFListSerializer<HtmlEntry> {
-
- final Dictionary dictionary;
-
- Serializer(Dictionary dictionary) {
- this.dictionary = dictionary;
- }
+public class HtmlEntry extends AbstractEntry implements RAFSerializable<HtmlEntry>,
+ Comparable<HtmlEntry> {
- @Override
- public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
- return new HtmlEntry(dictionary, raf, index);
+ // Title is not HTML escaped.
+ public final String title;
+ public final LazyHtmlLoader lazyHtmlLoader;
+ public String html;
+
+ public HtmlEntry(final EntrySource entrySource, String title) {
+ super(entrySource);
+ this.title = title;
+ lazyHtmlLoader = null;
}
- @Override
- public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
- t.write(raf);
+ public HtmlEntry(Dictionary dictionary, RandomAccessFile raf, final int index)
+ throws IOException {
+ super(dictionary, raf, index);
+ title = raf.readUTF();
+ lazyHtmlLoader = new LazyHtmlLoader(raf);
+ html = null;
}
- };
- public String getRawText(final boolean compact) {
- return title + ":\n" + getHtml();
- }
+ @Override
+ public void write(RandomAccessFile raf) throws IOException {
+ super.write(raf);
+ raf.writeUTF(title);
-
- @Override
- public int compareTo(HtmlEntry another) {
- if (title.compareTo(another.title) != 0) {
- return title.compareTo(another.title);
+ final byte[] bytes = getHtml().getBytes("UTF-8");
+ final byte[] zipBytes = StringUtil.zipBytes(bytes);
+ raf.writeInt(bytes.length);
+ raf.writeInt(zipBytes.length);
+ raf.write(zipBytes);
}
- return getHtml().compareTo(another.getHtml());
- }
-
- @Override
- public String toString() {
- return getRawText(false);
- }
-
- // --------------------------------------------------------------------
-
-
- public static class Row extends RowBase {
-
- boolean isExpanded = false;
-
- Row(final RandomAccessFile raf, final int thisRowIndex,
- final Index index) throws IOException {
- super(raf, thisRowIndex, index);
+
+ String getHtml() {
+ return html != null ? html : lazyHtmlLoader.getHtml();
}
- Row(final int referenceIndex, final int thisRowIndex,
- final Index index) {
- super(referenceIndex, thisRowIndex, index);
+ @Override
+ public void addToDictionary(Dictionary dictionary) {
+ assert index == -1;
+ dictionary.htmlEntries.add(this);
+ index = dictionary.htmlEntries.size() - 1;
}
-
+
@Override
- public String toString() {
- return getRawText(false);
+ public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
+ return new Row(this.index, rowIndex, dictionaryIndex);
}
- public HtmlEntry getEntry() {
- return index.dict.htmlEntries.get(referenceIndex);
+ static final class Serializer implements RAFListSerializer<HtmlEntry> {
+
+ final Dictionary dictionary;
+
+ Serializer(Dictionary dictionary) {
+ this.dictionary = dictionary;
+ }
+
+ @Override
+ public HtmlEntry read(RandomAccessFile raf, final int index) throws IOException {
+ return new HtmlEntry(dictionary, raf, index);
+ }
+
+ @Override
+ public void write(RandomAccessFile raf, HtmlEntry t) throws IOException {
+ t.write(raf);
+ }
+ };
+
+ public String getRawText(final boolean compact) {
+ return title + ":\n" + getHtml();
}
-
+
@Override
- public void print(PrintStream out) {
- final HtmlEntry entry = getEntry();
- out.println("See also HtmlEntry:" + entry.title);
+ public int compareTo(HtmlEntry another) {
+ if (title.compareTo(another.title) != 0) {
+ return title.compareTo(another.title);
+ }
+ return getHtml().compareTo(another.getHtml());
}
@Override
- public String getRawText(boolean compact) {
- final HtmlEntry entry = getEntry();
- return entry.getRawText(compact);
+ public String toString() {
+ return getRawText(false);
}
- @Override
- public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
- final String text = normalizer.transform(getRawText(false));
- if (orderedMatchPattern.matcher(text).find()) {
- return RowMatchType.ORDERED_MATCH;
- }
- for (int i = searchTokens.size() - 1; i >= 0; --i) {
- final String searchToken = searchTokens.get(i);
- if (!text.contains(searchToken)) {
- return RowMatchType.NO_MATCH;
+ // --------------------------------------------------------------------
+
+ public static class Row extends RowBase {
+
+ boolean isExpanded = false;
+
+ Row(final RandomAccessFile raf, final int thisRowIndex,
+ final Index index) throws IOException {
+ super(raf, thisRowIndex, index);
+ }
+
+ Row(final int referenceIndex, final int thisRowIndex,
+ final Index index) {
+ super(referenceIndex, thisRowIndex, index);
+ }
+
+ @Override
+ public String toString() {
+ return getRawText(false);
+ }
+
+ public HtmlEntry getEntry() {
+ return index.dict.htmlEntries.get(referenceIndex);
+ }
+
+ @Override
+ public void print(PrintStream out) {
+ final HtmlEntry entry = getEntry();
+ out.println("See also HtmlEntry:" + entry.title);
+ }
+
+ @Override
+ public String getRawText(boolean compact) {
+ final HtmlEntry entry = getEntry();
+ return entry.getRawText(compact);
+ }
+
+ @Override
+ public RowMatchType matches(final List<String> searchTokens,
+ final Pattern orderedMatchPattern, final Transliterator normalizer,
+ final boolean swapPairEntries) {
+ final String text = normalizer.transform(getRawText(false));
+ if (orderedMatchPattern.matcher(text).find()) {
+ return RowMatchType.ORDERED_MATCH;
+ }
+ for (int i = searchTokens.size() - 1; i >= 0; --i) {
+ final String searchToken = searchTokens.get(i);
+ if (!text.contains(searchToken)) {
+ return RowMatchType.NO_MATCH;
+ }
+ }
+ return RowMatchType.BAG_OF_WORDS_MATCH;
}
- }
- return RowMatchType.BAG_OF_WORDS_MATCH;
}
- }
public static String htmlBody(final List<HtmlEntry> htmlEntries, final String indexShortName) {
final StringBuilder result = new StringBuilder();
for (final HtmlEntry htmlEntry : htmlEntries) {
final String titleEscaped = StringUtil.escapeUnicodeToPureHtml(htmlEntry.title);
- result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n<p>%s\n",
+ result.append(String.format("<h1><a href=\"%s\">%s</a></h1>\n<p>%s\n",
formatQuickdicUrl(indexShortName, htmlEntry.title), titleEscaped,
htmlEntry.getHtml()));
}
return result.toString();
}
-
+
public static String formatQuickdicUrl(final String indexShortName, final String text) {
assert !indexShortName.contains(":");
assert text.length() > 0;
return String.format("q://d?%s&%s", indexShortName, StringUtil.encodeForUrl(text));
}
-
+
public static boolean isQuickdicUrl(String url) {
return url.startsWith("q://d?");
}
-
+
public static void quickdicUrlToIntent(final String url, final Intent intent) {
int firstColon = url.indexOf("?");
- if (firstColon == -1) return;
+ if (firstColon == -1)
+ return;
int secondColon = url.indexOf("&", firstColon + 1);
- if (secondColon == -1) return;
+ if (secondColon == -1)
+ return;
intent.putExtra(C.SEARCH_TOKEN, StringUtil.decodeFromUrl(url.substring(secondColon + 1)));
}
-
+
// --------------------------------------------------------------------
-
+
public static final class LazyHtmlLoader {
final RandomAccessFile raf;
final long offset;
final int numBytes;
final int numZipBytes;
-
+
// Not sure this volatile is right, but oh well.
volatile SoftReference<String> htmlRef = new SoftReference<String>(null);
-
+
private LazyHtmlLoader(final RandomAccessFile raf) throws IOException {
this.raf = raf;
numBytes = raf.readInt();
offset = raf.getFilePointer();
raf.skipBytes(numZipBytes);
}
-
+
public String getHtml() {
String html = htmlRef.get();
if (html != null) {
return html;
}
- System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes=" + numZipBytes);
+ System.out.println("Loading Html: numBytes=" + numBytes + ", numZipBytes="
+ + numZipBytes);
final byte[] bytes = new byte[numBytes];
final byte[] zipBytes = new byte[numZipBytes];
synchronized (raf) {
/**
*
*/
+
package com.hughes.android.dictionary.engine;
import com.hughes.android.dictionary.DictionaryInfo;
import java.util.regex.Pattern;
public final class Index implements RAFSerializable<Index> {
-
- static final int CACHE_SIZE = 5000;
-
- public final Dictionary dict;
-
- public final String shortName; // Typically the ISO code for the language.
- public final String longName;
-
- // persisted: tells how the entries are sorted.
- public final Language sortLanguage;
- final String normalizerRules;
-
- // Built from the two above.
- private Transliterator normalizer;
-
- // persisted
- public final List<IndexEntry> sortedIndexEntries;
-
- // persisted.
- public final Set<String> stoplist;
-
- // One big list!
- // Various sub-types.
- // persisted
- public final List<RowBase> 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, final Set<String> stoplist) {
- this.dict = dict;
- this.shortName = shortName;
- this.longName = longName;
- this.sortLanguage = sortLanguage;
- this.normalizerRules = normalizerRules;
- this.swapPairEntries = swapPairEntries;
- sortedIndexEntries = new ArrayList<IndexEntry>();
- this.stoplist = stoplist;
- rows = new ArrayList<RowBase>();
-
- normalizer = null;
- }
-
- /**
- * Deferred initialization because it can be slow.
- */
- public synchronized Transliterator normalizer() {
- if (normalizer == null) {
- normalizer = Transliterator.createFromRules("", normalizerRules, Transliterator.FORWARD);
- }
- return normalizer;
- }
-
- /**
- * Note that using this comparator probably involves doing too many text normalizations.
- */
- public NormalizeComparator getSortComparator() {
- return new NormalizeComparator(normalizer(), sortLanguage.getCollator());
- }
-
- public Index(final Dictionary dict, final RandomAccessFile raf) throws IOException {
- this.dict = dict;
- shortName = raf.readUTF();
- longName = raf.readUTF();
- final String languageCode = raf.readUTF();
- sortLanguage = Language.lookup(languageCode);
- normalizerRules = raf.readUTF();
- swapPairEntries = raf.readBoolean();
- if (sortLanguage == null) {
- throw new IOException("Unsupported language: " + languageCode);
- }
- if (dict.dictFileVersion >= 2) {
- mainTokenCount = raf.readInt();
- }
- sortedIndexEntries = CachingList.create(RAFList.create(raf, indexEntrySerializer, raf.getFilePointer()), CACHE_SIZE);
- if (dict.dictFileVersion >= 4) {
- stoplist = new SerializableSerializer<Set<String>>().read(raf);
- } else {
- stoplist = Collections.emptySet();
- }
- rows = CachingList.create(UniformRAFList.create(raf, new RowBase.Serializer(this), raf.getFilePointer()), CACHE_SIZE);
- }
-
- @Override
- public void write(final RandomAccessFile raf) throws IOException {
- raf.writeUTF(shortName);
- raf.writeUTF(longName);
- raf.writeUTF(sortLanguage.getIsoCode());
- raf.writeUTF(normalizerRules);
- raf.writeBoolean(swapPairEntries);
- if (dict.dictFileVersion >= 2) {
- raf.writeInt(mainTokenCount);
+
+ static final int CACHE_SIZE = 5000;
+
+ public final Dictionary dict;
+
+ public final String shortName; // Typically the ISO code for the language.
+ public final String longName;
+
+ // persisted: tells how the entries are sorted.
+ public final Language sortLanguage;
+ final String normalizerRules;
+
+ // Built from the two above.
+ private Transliterator normalizer;
+
+ // persisted
+ public final List<IndexEntry> sortedIndexEntries;
+
+ // persisted.
+ public final Set<String> stoplist;
+
+ // One big list!
+ // Various sub-types.
+ // persisted
+ public final List<RowBase> 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, final Set<String> stoplist) {
+ this.dict = dict;
+ this.shortName = shortName;
+ this.longName = longName;
+ this.sortLanguage = sortLanguage;
+ this.normalizerRules = normalizerRules;
+ this.swapPairEntries = swapPairEntries;
+ sortedIndexEntries = new ArrayList<IndexEntry>();
+ this.stoplist = stoplist;
+ rows = new ArrayList<RowBase>();
+
+ normalizer = null;
}
- RAFList.write(raf, sortedIndexEntries, indexEntrySerializer);
- new SerializableSerializer<Set<String>>().write(raf, stoplist);
- UniformRAFList.write(raf, (Collection<RowBase>) rows, new RowBase.Serializer(this), 5 /* bytes per entry */);
- }
-
- public void print(final PrintStream out) {
- for (final RowBase row : rows) {
- row.print(out);
+
+ /**
+ * Deferred initialization because it can be slow.
+ */
+ public synchronized Transliterator normalizer() {
+ if (normalizer == null) {
+ normalizer = Transliterator
+ .createFromRules("", normalizerRules, Transliterator.FORWARD);
+ }
+ return normalizer;
}
- }
-
- private final RAFSerializer<IndexEntry> indexEntrySerializer = new RAFSerializer<IndexEntry> () {
- @Override
- public IndexEntry read(RandomAccessFile raf) throws IOException {
- return new IndexEntry(Index.this, raf);
- }
- @Override
- public void write(RandomAccessFile raf, IndexEntry t) throws IOException {
- t.write(raf);
- }};
-
-
- public static final class IndexEntry implements RAFSerializable<Index.IndexEntry> {
- private final Index index;
- public final String token;
- private final String normalizedToken;
- public final int startRow;
- public final int numRows; // doesn't count the token row!
- public final List<HtmlEntry> htmlEntries;
-
-
- public IndexEntry(final Index index, final String token, final String normalizedToken, final int startRow, final int numRows) {
- this.index = index;
- assert token.equals(token.trim());
- assert token.length() > 0;
- this.token = token;
- this.normalizedToken = normalizedToken;
- this.startRow = startRow;
- this.numRows = numRows;
- this.htmlEntries = new ArrayList<HtmlEntry>();
+
+ /**
+ * Note that using this comparator probably involves doing too many text
+ * normalizations.
+ */
+ public NormalizeComparator getSortComparator() {
+ return new NormalizeComparator(normalizer(), sortLanguage.getCollator());
}
-
- public IndexEntry(final Index index, final RandomAccessFile raf) throws IOException {
- this.index = index;
- token = raf.readUTF();
- startRow = raf.readInt();
- numRows = raf.readInt();
- final boolean hasNormalizedForm = raf.readBoolean();
- normalizedToken = hasNormalizedForm ? raf.readUTF() : token;
- if (index.dict.dictFileVersion >= 6) {
- this.htmlEntries = CachingList.create(RAFList.create(raf, index.dict.htmlEntryIndexSerializer, raf.getFilePointer()), 1);
- } else {
- this.htmlEntries = Collections.emptyList();
- }
+
+ public Index(final Dictionary dict, final RandomAccessFile raf) throws IOException {
+ this.dict = dict;
+ shortName = raf.readUTF();
+ longName = raf.readUTF();
+ final String languageCode = raf.readUTF();
+ sortLanguage = Language.lookup(languageCode);
+ normalizerRules = raf.readUTF();
+ swapPairEntries = raf.readBoolean();
+ if (sortLanguage == null) {
+ throw new IOException("Unsupported language: " + languageCode);
+ }
+ if (dict.dictFileVersion >= 2) {
+ mainTokenCount = raf.readInt();
+ }
+ sortedIndexEntries = CachingList.create(
+ RAFList.create(raf, indexEntrySerializer, raf.getFilePointer()), CACHE_SIZE);
+ if (dict.dictFileVersion >= 4) {
+ stoplist = new SerializableSerializer<Set<String>>().read(raf);
+ } else {
+ stoplist = Collections.emptySet();
+ }
+ rows = CachingList.create(
+ UniformRAFList.create(raf, new RowBase.Serializer(this), raf.getFilePointer()),
+ CACHE_SIZE);
}
-
- public void write(RandomAccessFile raf) throws IOException {
- raf.writeUTF(token);
- raf.writeInt(startRow);
- raf.writeInt(numRows);
- final boolean hasNormalizedForm = !token.equals(normalizedToken);
- raf.writeBoolean(hasNormalizedForm);
- if (hasNormalizedForm) {
- raf.writeUTF(normalizedToken);
- }
- RAFList.write(raf, htmlEntries, index.dict.htmlEntryIndexSerializer);
+
+ @Override
+ public void write(final RandomAccessFile raf) throws IOException {
+ raf.writeUTF(shortName);
+ raf.writeUTF(longName);
+ raf.writeUTF(sortLanguage.getIsoCode());
+ raf.writeUTF(normalizerRules);
+ raf.writeBoolean(swapPairEntries);
+ if (dict.dictFileVersion >= 2) {
+ raf.writeInt(mainTokenCount);
+ }
+ RAFList.write(raf, sortedIndexEntries, indexEntrySerializer);
+ new SerializableSerializer<Set<String>>().write(raf, stoplist);
+ UniformRAFList.write(raf, (Collection<RowBase>) rows, new RowBase.Serializer(this), 5 /*
+ * bytes
+ * per
+ * entry
+ */);
}
- public String toString() {
- return String.format("%s@%d(%d)", token, startRow, numRows);
+ public void print(final PrintStream out) {
+ for (final RowBase row : rows) {
+ row.print(out);
+ }
}
- public String normalizedToken() {
- return normalizedToken;
+ private final RAFSerializer<IndexEntry> indexEntrySerializer = new RAFSerializer<IndexEntry>() {
+ @Override
+ public IndexEntry read(RandomAccessFile raf) throws IOException {
+ return new IndexEntry(Index.this, raf);
+ }
+
+ @Override
+ public void write(RandomAccessFile raf, IndexEntry t) throws IOException {
+ t.write(raf);
+ }
+ };
+
+ public static final class IndexEntry implements RAFSerializable<Index.IndexEntry> {
+ private final Index index;
+ public final String token;
+ private final String normalizedToken;
+ public final int startRow;
+ public final int numRows; // doesn't count the token row!
+ public final List<HtmlEntry> htmlEntries;
+
+ public IndexEntry(final Index index, final String token, final String normalizedToken,
+ final int startRow, final int numRows) {
+ this.index = index;
+ assert token.equals(token.trim());
+ assert token.length() > 0;
+ this.token = token;
+ this.normalizedToken = normalizedToken;
+ this.startRow = startRow;
+ this.numRows = numRows;
+ this.htmlEntries = new ArrayList<HtmlEntry>();
+ }
+
+ public IndexEntry(final Index index, final RandomAccessFile raf) throws IOException {
+ this.index = index;
+ token = raf.readUTF();
+ startRow = raf.readInt();
+ numRows = raf.readInt();
+ final boolean hasNormalizedForm = raf.readBoolean();
+ normalizedToken = hasNormalizedForm ? raf.readUTF() : token;
+ if (index.dict.dictFileVersion >= 6) {
+ this.htmlEntries = CachingList.create(
+ RAFList.create(raf, index.dict.htmlEntryIndexSerializer,
+ raf.getFilePointer()), 1);
+ } else {
+ this.htmlEntries = Collections.emptyList();
+ }
+ }
+
+ public void write(RandomAccessFile raf) throws IOException {
+ raf.writeUTF(token);
+ raf.writeInt(startRow);
+ raf.writeInt(numRows);
+ final boolean hasNormalizedForm = !token.equals(normalizedToken);
+ raf.writeBoolean(hasNormalizedForm);
+ if (hasNormalizedForm) {
+ raf.writeUTF(normalizedToken);
+ }
+ RAFList.write(raf, htmlEntries, index.dict.htmlEntryIndexSerializer);
+ }
+
+ public String toString() {
+ return String.format("%s@%d(%d)", token, startRow, numRows);
+ }
+
+ public String normalizedToken() {
+ return normalizedToken;
+ }
}
- }
-
- static final TransformingList.Transformer<IndexEntry, String> INDEX_ENTRY_TO_TOKEN = new TransformingList.Transformer<IndexEntry, String>() {
- @Override
- public String transform(IndexEntry t1) {
- return t1.token;
+
+ static final TransformingList.Transformer<IndexEntry, String> INDEX_ENTRY_TO_TOKEN = new TransformingList.Transformer<IndexEntry, String>() {
+ @Override
+ public String transform(IndexEntry t1) {
+ return t1.token;
+ }
+ };
+
+ public IndexEntry findExact(final String exactToken) {
+ final int result = Collections.binarySearch(
+ TransformingList.create(sortedIndexEntries, INDEX_ENTRY_TO_TOKEN), exactToken,
+ getSortComparator());
+ if (result >= 0) {
+ return sortedIndexEntries.get(result);
+ }
+ return null;
}
- };
-
- public IndexEntry findExact(final String exactToken) {
- final int result = Collections.binarySearch(TransformingList.create(sortedIndexEntries, INDEX_ENTRY_TO_TOKEN), exactToken, getSortComparator());
- if (result >= 0) {
- return sortedIndexEntries.get(result);
+
+ public IndexEntry findInsertionPoint(String token, final AtomicBoolean interrupted) {
+ final int index = findInsertionPointIndex(token, interrupted);
+ return index != -1 ? sortedIndexEntries.get(index) : null;
}
- return null;
- }
-
- public IndexEntry findInsertionPoint(String token, final AtomicBoolean interrupted) {
- final int index = findInsertionPointIndex(token, interrupted);
- return index != -1 ? sortedIndexEntries.get(index) : null;
- }
-
- public int findInsertionPointIndex(String token, final AtomicBoolean interrupted) {
- token = normalizeToken(token);
-
- int start = 0;
- int end = sortedIndexEntries.size();
-
- final Collator sortCollator = sortLanguage.getCollator();
- while (start < end) {
- final int mid = (start + end) / 2;
- if (interrupted.get()) {
- return -1;
- }
- final IndexEntry midEntry = sortedIndexEntries.get(mid);
-
- final int comp = sortCollator.compare(token, midEntry.normalizedToken());
- if (comp == 0) {
- final int result = windBackCase(token, mid, interrupted);
+
+ public int findInsertionPointIndex(String token, final AtomicBoolean interrupted) {
+ token = normalizeToken(token);
+
+ int start = 0;
+ int end = sortedIndexEntries.size();
+
+ final Collator sortCollator = sortLanguage.getCollator();
+ while (start < end) {
+ final int mid = (start + end) / 2;
+ if (interrupted.get()) {
+ return -1;
+ }
+ final IndexEntry midEntry = sortedIndexEntries.get(mid);
+
+ final int comp = sortCollator.compare(token, midEntry.normalizedToken());
+ if (comp == 0) {
+ final int result = windBackCase(token, mid, interrupted);
+ return result;
+ } else if (comp < 0) {
+ // System.out.println("Upper bound: " + midEntry + ", norm=" +
+ // midEntry.normalizedToken() + ", mid=" + mid);
+ end = mid;
+ } else {
+ // System.out.println("Lower bound: " + midEntry + ", norm=" +
+ // midEntry.normalizedToken() + ", mid=" + mid);
+ start = mid + 1;
+ }
+ }
+
+ // If we search for a substring of a string that's in there, return
+ // that.
+ int result = Math.min(start, sortedIndexEntries.size() - 1);
+ result = windBackCase(sortedIndexEntries.get(result).normalizedToken(), result, interrupted);
return result;
- } else if (comp < 0) {
- // System.out.println("Upper bound: " + midEntry + ", norm=" + midEntry.normalizedToken() + ", mid=" + mid);
- end = mid;
- } else {
- // System.out.println("Lower bound: " + midEntry + ", norm=" + midEntry.normalizedToken() + ", mid=" + mid);
- start = mid + 1;
- }
}
- // If we search for a substring of a string that's in there, return that.
- int result = Math.min(start, sortedIndexEntries.size() - 1);
- result = windBackCase(sortedIndexEntries.get(result).normalizedToken(), result, interrupted);
- return result;
- }
-
- private final int windBackCase(final String token, int result, final AtomicBoolean interrupted) {
- while (result > 0 && sortedIndexEntries.get(result - 1).normalizedToken().equals(token)) {
- --result;
- if (interrupted.get()) {
+ private final int windBackCase(final String token, int result, final AtomicBoolean interrupted) {
+ while (result > 0 && sortedIndexEntries.get(result - 1).normalizedToken().equals(token)) {
+ --result;
+ if (interrupted.get()) {
+ return result;
+ }
+ }
return result;
- }
}
- return result;
- }
-
- public IndexInfo getIndexInfo() {
- return new DictionaryInfo.IndexInfo(shortName, sortedIndexEntries.size(), mainTokenCount);
- }
-
- private static final int MAX_SEARCH_ROWS = 1000;
-
- private final Map<String,Integer> prefixToNumRows = new LinkedHashMap<String, Integer>();
- private synchronized final int getUpperBoundOnRowsStartingWith(final String normalizedPrefix, final int maxRows, final AtomicBoolean interrupted) {
- final Integer numRows = prefixToNumRows.get(normalizedPrefix);
- if (numRows != null) {
- return numRows;
+
+ public IndexInfo getIndexInfo() {
+ return new DictionaryInfo.IndexInfo(shortName, sortedIndexEntries.size(), mainTokenCount);
}
- final int insertionPointIndex = findInsertionPointIndex(normalizedPrefix, interrupted);
-
- int rowCount = 0;
- for (int index = insertionPointIndex; index < sortedIndexEntries.size(); ++index) {
- if (interrupted.get()) { return -1; }
- final IndexEntry indexEntry = sortedIndexEntries.get(index);
- if (!indexEntry.normalizedToken.startsWith(normalizedPrefix)) {
- break;
- }
- rowCount += indexEntry.numRows + indexEntry.htmlEntries.size();
- if (rowCount > maxRows) {
- System.out.println("Giving up, too many words with prefix: " + normalizedPrefix);
- break;
- }
+
+ private static final int MAX_SEARCH_ROWS = 1000;
+
+ private final Map<String, Integer> prefixToNumRows = new LinkedHashMap<String, Integer>();
+
+ private synchronized final int getUpperBoundOnRowsStartingWith(final String normalizedPrefix,
+ final int maxRows, final AtomicBoolean interrupted) {
+ final Integer numRows = prefixToNumRows.get(normalizedPrefix);
+ if (numRows != null) {
+ return numRows;
+ }
+ final int insertionPointIndex = findInsertionPointIndex(normalizedPrefix, interrupted);
+
+ int rowCount = 0;
+ for (int index = insertionPointIndex; index < sortedIndexEntries.size(); ++index) {
+ if (interrupted.get()) {
+ return -1;
+ }
+ final IndexEntry indexEntry = sortedIndexEntries.get(index);
+ if (!indexEntry.normalizedToken.startsWith(normalizedPrefix)) {
+ break;
+ }
+ rowCount += indexEntry.numRows + indexEntry.htmlEntries.size();
+ if (rowCount > maxRows) {
+ System.out.println("Giving up, too many words with prefix: " + normalizedPrefix);
+ break;
+ }
+ }
+ prefixToNumRows.put(normalizedPrefix, numRows);
+ return rowCount;
}
- prefixToNumRows.put(normalizedPrefix, numRows);
- return rowCount;
- }
-
-
- public final List<RowBase> multiWordSearch(
- final String searchText, final List<String> searchTokens, final AtomicBoolean interrupted) {
- final long startMills = System.currentTimeMillis();
- final List<RowBase> result = new ArrayList<RowBase>();
-
- final Set<String> normalizedNonStoplist = new LinkedHashSet<String>();
-
- String bestPrefix = null;
- int leastRows = Integer.MAX_VALUE;
- final StringBuilder searchTokensRegex = new StringBuilder();
- for (int i = 0; i < searchTokens.size(); ++i) {
- if (interrupted.get()) { return null; }
- final String searchToken = searchTokens.get(i);
- final String normalized = normalizeToken(searchTokens.get(i));
- // Normalize them all.
- searchTokens.set(i, normalized);
-
- if (!stoplist.contains(searchToken)) {
- if (normalizedNonStoplist.add(normalized)) {
- final int numRows = getUpperBoundOnRowsStartingWith(normalized, MAX_SEARCH_ROWS, interrupted);
- if (numRows != -1 && numRows < leastRows) {
- if (numRows == 0) {
- // We really are done here.
- return Collections.emptyList();
+
+ public final List<RowBase> multiWordSearch(
+ final String searchText, final List<String> searchTokens,
+ final AtomicBoolean interrupted) {
+ final long startMills = System.currentTimeMillis();
+ final List<RowBase> result = new ArrayList<RowBase>();
+
+ final Set<String> normalizedNonStoplist = new LinkedHashSet<String>();
+
+ String bestPrefix = null;
+ int leastRows = Integer.MAX_VALUE;
+ final StringBuilder searchTokensRegex = new StringBuilder();
+ for (int i = 0; i < searchTokens.size(); ++i) {
+ if (interrupted.get()) {
+ return null;
}
- leastRows = numRows;
- bestPrefix = normalized;
- }
+ final String searchToken = searchTokens.get(i);
+ final String normalized = normalizeToken(searchTokens.get(i));
+ // Normalize them all.
+ searchTokens.set(i, normalized);
+
+ if (!stoplist.contains(searchToken)) {
+ if (normalizedNonStoplist.add(normalized)) {
+ final int numRows = getUpperBoundOnRowsStartingWith(normalized,
+ MAX_SEARCH_ROWS, interrupted);
+ if (numRows != -1 && numRows < leastRows) {
+ if (numRows == 0) {
+ // We really are done here.
+ return Collections.emptyList();
+ }
+ leastRows = numRows;
+ bestPrefix = normalized;
+ }
+ }
+ }
+
+ if (searchTokensRegex.length() > 0) {
+ searchTokensRegex.append("[\\s]*");
+ }
+ searchTokensRegex.append(Pattern.quote(normalized));
}
- }
+ final Pattern pattern = Pattern.compile(searchTokensRegex.toString());
- if (searchTokensRegex.length() > 0) {
- searchTokensRegex.append("[\\s]*");
- }
- searchTokensRegex.append(Pattern.quote(normalized));
- }
- final Pattern pattern = Pattern.compile(searchTokensRegex.toString());
-
- if (bestPrefix == null) {
- bestPrefix = searchTokens.get(0);
- System.out.println("Everything was in the stoplist!");
- }
- System.out.println("Searching using prefix: " + bestPrefix + ", leastRows=" + leastRows + ", searchTokens=" + searchTokens);
-
- // Place to store the things that match.
- final Map<RowMatchType,List<RowBase>> matches = new EnumMap<RowMatchType, List<RowBase>>(RowMatchType.class);
- for (final RowMatchType rowMatchType : RowMatchType.values()) {
- if (rowMatchType != RowMatchType.NO_MATCH) {
- matches.put(rowMatchType, new ArrayList<RowBase>());
- }
- }
-
- int matchCount = 0;
-
- final int exactMatchIndex = findInsertionPointIndex(searchText, interrupted);
- if (exactMatchIndex != -1) {
- final IndexEntry exactMatch = sortedIndexEntries.get(exactMatchIndex);
- if (pattern.matcher(exactMatch.token).find()) {
- matches.get(RowMatchType.TITLE_MATCH).add(rows.get(exactMatch.startRow));
+ if (bestPrefix == null) {
+ bestPrefix = searchTokens.get(0);
+ System.out.println("Everything was in the stoplist!");
}
- }
-
- final String searchToken = bestPrefix;
- final int insertionPointIndex = findInsertionPointIndex(searchToken, interrupted);
- final Set<RowKey> rowsAlreadySeen = new HashSet<RowBase.RowKey>();
- for (int index = insertionPointIndex;
- index < sortedIndexEntries.size() && matchCount < MAX_SEARCH_ROWS;
- ++index) {
- if (interrupted.get()) { return null; }
- final IndexEntry indexEntry = sortedIndexEntries.get(index);
- if (!indexEntry.normalizedToken.startsWith(searchToken)) {
- break;
+ System.out.println("Searching using prefix: " + bestPrefix + ", leastRows=" + leastRows
+ + ", searchTokens=" + searchTokens);
+
+ // Place to store the things that match.
+ final Map<RowMatchType, List<RowBase>> matches = new EnumMap<RowMatchType, List<RowBase>>(
+ RowMatchType.class);
+ for (final RowMatchType rowMatchType : RowMatchType.values()) {
+ if (rowMatchType != RowMatchType.NO_MATCH) {
+ matches.put(rowMatchType, new ArrayList<RowBase>());
+ }
+ }
+
+ int matchCount = 0;
+
+ final int exactMatchIndex = findInsertionPointIndex(searchText, interrupted);
+ if (exactMatchIndex != -1) {
+ final IndexEntry exactMatch = sortedIndexEntries.get(exactMatchIndex);
+ if (pattern.matcher(exactMatch.token).find()) {
+ matches.get(RowMatchType.TITLE_MATCH).add(rows.get(exactMatch.startRow));
+ }
+ }
+
+ final String searchToken = bestPrefix;
+ final int insertionPointIndex = findInsertionPointIndex(searchToken, interrupted);
+ final Set<RowKey> rowsAlreadySeen = new HashSet<RowBase.RowKey>();
+ for (int index = insertionPointIndex; index < sortedIndexEntries.size()
+ && matchCount < MAX_SEARCH_ROWS; ++index) {
+ if (interrupted.get()) {
+ return null;
+ }
+ final IndexEntry indexEntry = sortedIndexEntries.get(index);
+ if (!indexEntry.normalizedToken.startsWith(searchToken)) {
+ break;
+ }
+
+ // System.out.println("Searching indexEntry: " + indexEntry.token);
+
+ // Extra +1 to skip token row.
+ for (int rowIndex = indexEntry.startRow + 1; rowIndex < indexEntry.startRow + 1
+ + indexEntry.numRows
+ && rowIndex < rows.size(); ++rowIndex) {
+ if (interrupted.get()) {
+ return null;
+ }
+ final RowBase row = rows.get(rowIndex);
+ final RowBase.RowKey rowKey = row.getRowKey();
+ if (rowsAlreadySeen.contains(rowKey)) {
+ continue;
+ }
+ rowsAlreadySeen.add(rowKey);
+ final RowMatchType matchType = row.matches(searchTokens, pattern, normalizer(),
+ swapPairEntries);
+ if (matchType != RowMatchType.NO_MATCH) {
+ matches.get(matchType).add(row);
+ ++matchCount;
+ }
+ }
}
+ // } // searchTokens
-// System.out.println("Searching indexEntry: " + indexEntry.token);
-
- // Extra +1 to skip token row.
- for (int rowIndex = indexEntry.startRow + 1;
- rowIndex < indexEntry.startRow + 1 + indexEntry.numRows && rowIndex < rows.size();
- ++rowIndex) {
- if (interrupted.get()) { return null; }
- final RowBase row = rows.get(rowIndex);
- final RowBase.RowKey rowKey = row.getRowKey();
- if (rowsAlreadySeen.contains(rowKey)) {
- continue;
- }
- rowsAlreadySeen.add(rowKey);
- final RowMatchType matchType = row.matches(searchTokens, pattern, normalizer(), swapPairEntries);
- if (matchType != RowMatchType.NO_MATCH) {
- matches.get(matchType).add(row);
- ++matchCount;
- }
+ // Sort them into a reasonable order.
+ final RowBase.LengthComparator lengthComparator = new RowBase.LengthComparator(
+ swapPairEntries);
+ for (final Collection<RowBase> rows : matches.values()) {
+ final List<RowBase> ordered = new ArrayList<RowBase>(rows);
+ Collections.sort(ordered, lengthComparator);
+ result.addAll(ordered);
}
- }
-// } // searchTokens
-
- // Sort them into a reasonable order.
- final RowBase.LengthComparator lengthComparator = new RowBase.LengthComparator(swapPairEntries);
- for (final Collection<RowBase> rows : matches.values()) {
- final List<RowBase> ordered = new ArrayList<RowBase>(rows);
- Collections.sort(ordered, lengthComparator);
- result.addAll(ordered);
+
+ System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills));
+ return result;
}
-
- System.out.println("searchDuration: " + (System.currentTimeMillis() - startMills));
- return result;
- }
-
- private String normalizeToken(final String searchToken) {
- if (TransliteratorManager.init(null)) {
- final Transliterator normalizer = normalizer();
- return normalizer.transliterate(searchToken);
- } else {
- // Do our best since the Transliterators aren't up yet.
- return searchToken.toLowerCase();
+
+ private String normalizeToken(final String searchToken) {
+ if (TransliteratorManager.init(null)) {
+ final Transliterator normalizer = normalizer();
+ return normalizer.transliterate(searchToken);
+ } else {
+ // Do our best since the Transliterators aren't up yet.
+ return searchToken.toLowerCase();
+ }
}
- }
-}
\ No newline at end of file
+}
import java.util.regex.Pattern;\r
\r
public class Language {\r
- \r
- public static final class LanguageResources {\r
- public final String englishName;\r
- public final int nameId;\r
- public final int flagId;\r
- \r
- private LanguageResources(final String englishName, int nameId, int flagId) {\r
- this.englishName = englishName;\r
- this.nameId = nameId;\r
- this.flagId = flagId;\r
+\r
+ public static final class LanguageResources {\r
+ public final String englishName;\r
+ public final int nameId;\r
+ public final int flagId;\r
+\r
+ private LanguageResources(final String englishName, int nameId, int flagId) {\r
+ this.englishName = englishName;\r
+ this.nameId = nameId;\r
+ this.flagId = flagId;\r
+ }\r
+\r
+ private LanguageResources(final String englishName, int nameId) {\r
+ this(englishName, nameId, 0);\r
+ }\r
}\r
\r
- private LanguageResources(final String englishName, int nameId) {\r
- this(englishName, nameId, 0);\r
+ // Useful:\r
+ // http://www.loc.gov/standards/iso639-2/php/code_list.php\r
+ public static final Map<String, LanguageResources> isoCodeToResources = new LinkedHashMap<String, LanguageResources>();\r
+ static {\r
+ isoCodeToResources.put("AF", new LanguageResources("Afrikaans", R.string.AF,\r
+ R.drawable.flag_of_south_africa));\r
+ isoCodeToResources.put("SQ", new LanguageResources("Albanian", R.string.SQ,\r
+ R.drawable.flag_of_albania));\r
+ isoCodeToResources.put("AR",\r
+ new LanguageResources("Arabic", R.string.AR, R.drawable.arabic));\r
+ isoCodeToResources.put("HY", new LanguageResources("Armenian", R.string.HY,\r
+ R.drawable.flag_of_armenia));\r
+ isoCodeToResources.put("BE", new LanguageResources("Belarusian", R.string.BE,\r
+ R.drawable.flag_of_belarus));\r
+ isoCodeToResources.put("BN", new LanguageResources("Bengali", R.string.BN));\r
+ isoCodeToResources.put("BS", new LanguageResources("Bosnian", R.string.BS,\r
+ R.drawable.flag_of_bosnia_and_herzegovina));\r
+ isoCodeToResources.put("BG", new LanguageResources("Bulgarian", R.string.BG,\r
+ R.drawable.flag_of_bulgaria));\r
+ isoCodeToResources.put("MY", new LanguageResources("Burmese", R.string.MY,\r
+ R.drawable.flag_of_myanmar));\r
+ isoCodeToResources.put("ZH", new LanguageResources("Chinese", R.string.ZH,\r
+ R.drawable.flag_of_the_peoples_republic_of_china));\r
+ isoCodeToResources.put("cmn", new LanguageResources("Mandarin", R.string.cmn,\r
+ R.drawable.flag_of_the_peoples_republic_of_china));\r
+ isoCodeToResources.put("yue", new LanguageResources("Cantonese", R.string.yue,\r
+ R.drawable.flag_of_hong_kong));\r
+ isoCodeToResources.put("CA", new LanguageResources("Catalan", R.string.CA));\r
+ isoCodeToResources.put("HR", new LanguageResources("Croatian", R.string.HR,\r
+ R.drawable.flag_of_croatia));\r
+ isoCodeToResources.put("CS", new LanguageResources("Czech", R.string.CS,\r
+ R.drawable.flag_of_the_czech_republic));\r
+ isoCodeToResources.put("DA", new LanguageResources("Danish", R.string.DA,\r
+ R.drawable.flag_of_denmark));\r
+ isoCodeToResources.put("NL", new LanguageResources("Dutch", R.string.NL,\r
+ R.drawable.flag_of_the_netherlands));\r
+ isoCodeToResources.put("EN", new LanguageResources("English", R.string.EN,\r
+ R.drawable.flag_of_the_united_kingdom));\r
+ isoCodeToResources.put("EO", new LanguageResources("Esperanto", R.string.EO,\r
+ R.drawable.flag_of_esperanto));\r
+ isoCodeToResources.put("ET", new LanguageResources("Estonian", R.string.ET,\r
+ R.drawable.flag_of_estonia));\r
+ isoCodeToResources.put("FI", new LanguageResources("Finnish", R.string.FI,\r
+ R.drawable.flag_of_finland));\r
+ isoCodeToResources.put("FR", new LanguageResources("French", R.string.FR,\r
+ R.drawable.flag_of_france));\r
+ isoCodeToResources.put("DE", new LanguageResources("German", R.string.DE,\r
+ R.drawable.flag_of_germany));\r
+ isoCodeToResources.put("EL", new LanguageResources("Greek", R.string.EL,\r
+ R.drawable.flag_of_greece));\r
+ isoCodeToResources.put("grc", new LanguageResources("Ancient Greek", R.string.grc));\r
+ isoCodeToResources.put("haw", new LanguageResources("Hawaiian", R.string.haw,\r
+ R.drawable.flag_of_hawaii));\r
+ isoCodeToResources.put("HE", new LanguageResources("Hebrew", R.string.HE,\r
+ R.drawable.flag_of_israel));\r
+ isoCodeToResources.put("HI", new LanguageResources("Hindi", R.string.HI, R.drawable.hindi));\r
+ isoCodeToResources.put("HU", new LanguageResources("Hungarian", R.string.HU,\r
+ R.drawable.flag_of_hungary));\r
+ isoCodeToResources.put("IS", new LanguageResources("Icelandic", R.string.IS,\r
+ R.drawable.flag_of_iceland));\r
+ isoCodeToResources.put("ID", new LanguageResources("Indonesian", R.string.ID,\r
+ R.drawable.flag_of_indonesia));\r
+ isoCodeToResources.put("GA", new LanguageResources("Irish", R.string.GA,\r
+ R.drawable.flag_of_ireland));\r
+ isoCodeToResources.put("GD", new LanguageResources("Scottish Gaelic", R.string.GD,\r
+ R.drawable.flag_of_scotland));\r
+ isoCodeToResources.put("GV", new LanguageResources("Manx", R.string.GV,\r
+ R.drawable.flag_of_the_isle_of_man));\r
+ isoCodeToResources.put("IT", new LanguageResources("Italian", R.string.IT,\r
+ R.drawable.flag_of_italy));\r
+ isoCodeToResources.put("LA", new LanguageResources("Latin", R.string.LA));\r
+ isoCodeToResources.put("LV", new LanguageResources("Latvian", R.string.LV,\r
+ R.drawable.flag_of_latvia));\r
+ isoCodeToResources.put("LT", new LanguageResources("Lithuanian", R.string.LT,\r
+ R.drawable.flag_of_lithuania));\r
+ isoCodeToResources.put("JA", new LanguageResources("Japanese", R.string.JA,\r
+ R.drawable.flag_of_japan));\r
+ isoCodeToResources.put("KO", new LanguageResources("Korean", R.string.KO,\r
+ R.drawable.flag_of_south_korea));\r
+ isoCodeToResources.put("KU", new LanguageResources("Kurdish", R.string.KU));\r
+ isoCodeToResources.put("MS", new LanguageResources("Malay", R.string.MS,\r
+ R.drawable.flag_of_malaysia));\r
+ isoCodeToResources.put("MI", new LanguageResources("Maori", R.string.MI,\r
+ R.drawable.flag_of_new_zealand));\r
+ isoCodeToResources.put("MN", new LanguageResources("Mongolian", R.string.MN,\r
+ R.drawable.flag_of_mongolia));\r
+ isoCodeToResources.put("NE", new LanguageResources("Nepali", R.string.NE,\r
+ R.drawable.flag_of_nepal));\r
+ isoCodeToResources.put("NO", new LanguageResources("Norwegian", R.string.NO,\r
+ R.drawable.flag_of_norway));\r
+ isoCodeToResources.put("FA", new LanguageResources("Persian", R.string.FA,\r
+ R.drawable.flag_of_iran));\r
+ isoCodeToResources.put("PL", new LanguageResources("Polish", R.string.PL,\r
+ R.drawable.flag_of_poland));\r
+ isoCodeToResources.put("PT", new LanguageResources("Portuguese", R.string.PT,\r
+ R.drawable.flag_of_portugal));\r
+ isoCodeToResources.put("PA", new LanguageResources("Punjabi", R.string.PA));\r
+ isoCodeToResources.put("RO", new LanguageResources("Romanian", R.string.RO,\r
+ R.drawable.flag_of_romania));\r
+ isoCodeToResources.put("RU", new LanguageResources("Russian", R.string.RU,\r
+ R.drawable.flag_of_russia));\r
+ isoCodeToResources.put("SA", new LanguageResources("Sanskrit", R.string.SA));\r
+ isoCodeToResources.put("SR", new LanguageResources("Serbian", R.string.SR,\r
+ R.drawable.flag_of_serbia));\r
+ isoCodeToResources.put("SK", new LanguageResources("Slovak", R.string.SK,\r
+ R.drawable.flag_of_slovakia));\r
+ isoCodeToResources.put("SL", new LanguageResources("Slovenian", R.string.SL,\r
+ R.drawable.flag_of_slovenia));\r
+ isoCodeToResources.put("SO", new LanguageResources("Somali", R.string.SO,\r
+ R.drawable.flag_of_somalia));\r
+ isoCodeToResources.put("ES", new LanguageResources("Spanish", R.string.ES,\r
+ R.drawable.flag_of_spain));\r
+ isoCodeToResources.put("SW", new LanguageResources("Swahili", R.string.SW));\r
+ isoCodeToResources.put("SV", new LanguageResources("Swedish", R.string.SV,\r
+ R.drawable.flag_of_sweden));\r
+ isoCodeToResources.put("TL", new LanguageResources("Tagalog", R.string.TL));\r
+ isoCodeToResources.put("TG", new LanguageResources("Tajik", R.string.TG,\r
+ R.drawable.flag_of_tajikistan));\r
+ isoCodeToResources.put("TH", new LanguageResources("Thai", R.string.TH,\r
+ R.drawable.flag_of_thailand));\r
+ isoCodeToResources.put("BO", new LanguageResources("Tibetan", R.string.BO));\r
+ isoCodeToResources.put("TR", new LanguageResources("Turkish", R.string.TR,\r
+ R.drawable.flag_of_turkey));\r
+ isoCodeToResources.put("UK", new LanguageResources("Ukrainian", R.string.UK,\r
+ R.drawable.flag_of_ukraine));\r
+ isoCodeToResources.put("UR", new LanguageResources("Urdu", R.string.UR));\r
+ isoCodeToResources.put("VI", new LanguageResources("Vietnamese", R.string.VI,\r
+ R.drawable.flag_of_vietnam));\r
+ isoCodeToResources.put("CI", new LanguageResources("Welsh", R.string.CI,\r
+ R.drawable.flag_of_wales_2));\r
+ isoCodeToResources.put("YI", new LanguageResources("Yiddish", R.string.YI));\r
+ isoCodeToResources.put("ZU", new LanguageResources("Zulu", R.string.ZU));\r
+ isoCodeToResources.put("AZ", new LanguageResources("Azeri", R.string.AZ,\r
+ R.drawable.flag_of_azerbaijan));\r
+ isoCodeToResources.put("EU", new LanguageResources("Basque", R.string.EU,\r
+ R.drawable.flag_of_the_basque_country));\r
+ isoCodeToResources.put("BR", new LanguageResources("Breton", R.string.BR));\r
+ isoCodeToResources.put("MR", new LanguageResources("Marathi", R.string.MR));\r
+ isoCodeToResources.put("FO", new LanguageResources("Faroese", R.string.FO));\r
+ isoCodeToResources.put("GL", new LanguageResources("Galician", R.string.GL,\r
+ R.drawable.flag_of_galicia));\r
+ isoCodeToResources.put("KA", new LanguageResources("Georgian", R.string.KA,\r
+ R.drawable.flag_of_georgia));\r
+ isoCodeToResources.put("HT", new LanguageResources("Haitian Creole", R.string.HT,\r
+ R.drawable.flag_of_haiti));\r
+ isoCodeToResources.put("LB", new LanguageResources("Luxembourgish", R.string.LB,\r
+ R.drawable.flag_of_luxembourg));\r
+ isoCodeToResources.put("MK", new LanguageResources("Macedonian", R.string.MK,\r
+ R.drawable.flag_of_macedonia));\r
+ isoCodeToResources.put("LO", new LanguageResources("Lao", R.string.LO,\r
+ R.drawable.flag_of_laos));\r
+ isoCodeToResources.put("ML", new LanguageResources("Malayalam", R.string.ML));\r
+ isoCodeToResources.put("SL", new LanguageResources("Slovenian", R.string.SL,\r
+ R.drawable.flag_of_slovenia));\r
+ isoCodeToResources.put("TA", new LanguageResources("Tamil", R.string.TA));\r
+ isoCodeToResources.put("SH", new LanguageResources("Serbo-Croatian", R.string.SH));\r
+\r
+ // Hack to allow lower-case ISO codes to work:\r
+ for (final String isoCode : new ArrayList<String>(isoCodeToResources.keySet())) {\r
+ isoCodeToResources.put(isoCode.toLowerCase(), isoCodeToResources.get(isoCode));\r
+ }\r
+\r
}\r
- }\r
-\r
- // Useful:\r
- // http://www.loc.gov/standards/iso639-2/php/code_list.php\r
- public static final Map<String,LanguageResources> isoCodeToResources = new LinkedHashMap<String,LanguageResources>();\r
- static {\r
- isoCodeToResources.put("AF", new LanguageResources("Afrikaans", R.string.AF, R.drawable.flag_of_south_africa));\r
- isoCodeToResources.put("SQ", new LanguageResources("Albanian", R.string.SQ, R.drawable.flag_of_albania));\r
- isoCodeToResources.put("AR", new LanguageResources("Arabic", R.string.AR, R.drawable.arabic));\r
- isoCodeToResources.put("HY", new LanguageResources("Armenian", R.string.HY, R.drawable.flag_of_armenia));\r
- isoCodeToResources.put("BE", new LanguageResources("Belarusian", R.string.BE, R.drawable.flag_of_belarus));\r
- isoCodeToResources.put("BN", new LanguageResources("Bengali", R.string.BN));\r
- isoCodeToResources.put("BS", new LanguageResources("Bosnian", R.string.BS, R.drawable.flag_of_bosnia_and_herzegovina));\r
- isoCodeToResources.put("BG", new LanguageResources("Bulgarian", R.string.BG, R.drawable.flag_of_bulgaria));\r
- isoCodeToResources.put("MY", new LanguageResources("Burmese", R.string.MY, R.drawable.flag_of_myanmar));\r
- isoCodeToResources.put("ZH", new LanguageResources("Chinese", R.string.ZH, R.drawable.flag_of_the_peoples_republic_of_china));\r
- isoCodeToResources.put("cmn", new LanguageResources("Mandarin", R.string.cmn, R.drawable.flag_of_the_peoples_republic_of_china));\r
- isoCodeToResources.put("yue", new LanguageResources("Cantonese", R.string.yue, R.drawable.flag_of_hong_kong));\r
- isoCodeToResources.put("CA", new LanguageResources("Catalan", R.string.CA));\r
- isoCodeToResources.put("HR", new LanguageResources("Croatian", R.string.HR, R.drawable.flag_of_croatia));\r
- isoCodeToResources.put("CS", new LanguageResources("Czech", R.string.CS, R.drawable.flag_of_the_czech_republic));\r
- isoCodeToResources.put("DA", new LanguageResources("Danish", R.string.DA, R.drawable.flag_of_denmark));\r
- isoCodeToResources.put("NL", new LanguageResources("Dutch", R.string.NL, R.drawable.flag_of_the_netherlands));\r
- isoCodeToResources.put("EN", new LanguageResources("English", R.string.EN, R.drawable.flag_of_the_united_kingdom));\r
- isoCodeToResources.put("EO", new LanguageResources("Esperanto", R.string.EO, R.drawable.flag_of_esperanto));\r
- isoCodeToResources.put("ET", new LanguageResources("Estonian", R.string.ET, R.drawable.flag_of_estonia));\r
- isoCodeToResources.put("FI", new LanguageResources("Finnish", R.string.FI, R.drawable.flag_of_finland));\r
- isoCodeToResources.put("FR", new LanguageResources("French", R.string.FR, R.drawable.flag_of_france));\r
- isoCodeToResources.put("DE", new LanguageResources("German", R.string.DE, R.drawable.flag_of_germany));\r
- isoCodeToResources.put("EL", new LanguageResources("Greek", R.string.EL, R.drawable.flag_of_greece));\r
- isoCodeToResources.put("grc", new LanguageResources("Ancient Greek", R.string.grc));\r
- isoCodeToResources.put("haw", new LanguageResources("Hawaiian", R.string.haw, R.drawable.flag_of_hawaii));\r
- isoCodeToResources.put("HE", new LanguageResources("Hebrew", R.string.HE, R.drawable.flag_of_israel));\r
- isoCodeToResources.put("HI", new LanguageResources("Hindi", R.string.HI, R.drawable.hindi));\r
- isoCodeToResources.put("HU", new LanguageResources("Hungarian", R.string.HU, R.drawable.flag_of_hungary));\r
- isoCodeToResources.put("IS", new LanguageResources("Icelandic", R.string.IS, R.drawable.flag_of_iceland));\r
- isoCodeToResources.put("ID", new LanguageResources("Indonesian", R.string.ID, R.drawable.flag_of_indonesia));\r
- isoCodeToResources.put("GA", new LanguageResources("Irish", R.string.GA, R.drawable.flag_of_ireland));\r
- isoCodeToResources.put("GD", new LanguageResources("Scottish Gaelic", R.string.GD, R.drawable.flag_of_scotland));\r
- isoCodeToResources.put("GV", new LanguageResources("Manx", R.string.GV, R.drawable.flag_of_the_isle_of_man));\r
- isoCodeToResources.put("IT", new LanguageResources("Italian", R.string.IT, R.drawable.flag_of_italy));\r
- isoCodeToResources.put("LA", new LanguageResources("Latin", R.string.LA));\r
- isoCodeToResources.put("LV", new LanguageResources("Latvian", R.string.LV, R.drawable.flag_of_latvia));\r
- isoCodeToResources.put("LT", new LanguageResources("Lithuanian", R.string.LT, R.drawable.flag_of_lithuania));\r
- isoCodeToResources.put("JA", new LanguageResources("Japanese", R.string.JA, R.drawable.flag_of_japan));\r
- isoCodeToResources.put("KO", new LanguageResources("Korean", R.string.KO, R.drawable.flag_of_south_korea));\r
- isoCodeToResources.put("KU", new LanguageResources("Kurdish", R.string.KU));\r
- isoCodeToResources.put("MS", new LanguageResources("Malay", R.string.MS, R.drawable.flag_of_malaysia));\r
- isoCodeToResources.put("MI", new LanguageResources("Maori", R.string.MI, R.drawable.flag_of_new_zealand));\r
- isoCodeToResources.put("MN", new LanguageResources("Mongolian", R.string.MN, R.drawable.flag_of_mongolia));\r
- isoCodeToResources.put("NE", new LanguageResources("Nepali", R.string.NE, R.drawable.flag_of_nepal));\r
- isoCodeToResources.put("NO", new LanguageResources("Norwegian", R.string.NO, R.drawable.flag_of_norway));\r
- isoCodeToResources.put("FA", new LanguageResources("Persian", R.string.FA, R.drawable.flag_of_iran));\r
- isoCodeToResources.put("PL", new LanguageResources("Polish", R.string.PL, R.drawable.flag_of_poland));\r
- isoCodeToResources.put("PT", new LanguageResources("Portuguese", R.string.PT, R.drawable.flag_of_portugal));\r
- isoCodeToResources.put("PA", new LanguageResources("Punjabi", R.string.PA));\r
- isoCodeToResources.put("RO", new LanguageResources("Romanian", R.string.RO, R.drawable.flag_of_romania));\r
- isoCodeToResources.put("RU", new LanguageResources("Russian", R.string.RU, R.drawable.flag_of_russia));\r
- isoCodeToResources.put("SA", new LanguageResources("Sanskrit", R.string.SA));\r
- isoCodeToResources.put("SR", new LanguageResources("Serbian", R.string.SR, R.drawable.flag_of_serbia));\r
- isoCodeToResources.put("SK", new LanguageResources("Slovak", R.string.SK, R.drawable.flag_of_slovakia));\r
- isoCodeToResources.put("SL", new LanguageResources("Slovenian", R.string.SL, R.drawable.flag_of_slovenia));\r
- isoCodeToResources.put("SO", new LanguageResources("Somali", R.string.SO, R.drawable.flag_of_somalia));\r
- isoCodeToResources.put("ES", new LanguageResources("Spanish", R.string.ES, R.drawable.flag_of_spain));\r
- isoCodeToResources.put("SW", new LanguageResources("Swahili", R.string.SW));\r
- isoCodeToResources.put("SV", new LanguageResources("Swedish", R.string.SV, R.drawable.flag_of_sweden));\r
- isoCodeToResources.put("TL", new LanguageResources("Tagalog", R.string.TL));\r
- isoCodeToResources.put("TG", new LanguageResources("Tajik", R.string.TG, R.drawable.flag_of_tajikistan));\r
- isoCodeToResources.put("TH", new LanguageResources("Thai", R.string.TH, R.drawable.flag_of_thailand));\r
- isoCodeToResources.put("BO", new LanguageResources("Tibetan", R.string.BO));\r
- isoCodeToResources.put("TR", new LanguageResources("Turkish", R.string.TR, R.drawable.flag_of_turkey));\r
- isoCodeToResources.put("UK", new LanguageResources("Ukrainian", R.string.UK, R.drawable.flag_of_ukraine));\r
- isoCodeToResources.put("UR", new LanguageResources("Urdu", R.string.UR));\r
- isoCodeToResources.put("VI", new LanguageResources("Vietnamese", R.string.VI, R.drawable.flag_of_vietnam));\r
- isoCodeToResources.put("CI", new LanguageResources("Welsh", R.string.CI, R.drawable.flag_of_wales_2));\r
- isoCodeToResources.put("YI", new LanguageResources("Yiddish", R.string.YI));\r
- isoCodeToResources.put("ZU", new LanguageResources("Zulu", R.string.ZU));\r
- isoCodeToResources.put("AZ", new LanguageResources("Azeri", R.string.AZ, R.drawable.flag_of_azerbaijan));\r
- isoCodeToResources.put("EU", new LanguageResources("Basque", R.string.EU, R.drawable.flag_of_the_basque_country));\r
- isoCodeToResources.put("BR", new LanguageResources("Breton", R.string.BR));\r
- isoCodeToResources.put("MR", new LanguageResources("Marathi", R.string.MR));\r
- isoCodeToResources.put("FO", new LanguageResources("Faroese", R.string.FO));\r
- isoCodeToResources.put("GL", new LanguageResources("Galician", R.string.GL, R.drawable.flag_of_galicia));\r
- isoCodeToResources.put("KA", new LanguageResources("Georgian", R.string.KA, R.drawable.flag_of_georgia));\r
- isoCodeToResources.put("HT", new LanguageResources("Haitian Creole", R.string.HT, R.drawable.flag_of_haiti));\r
- isoCodeToResources.put("LB", new LanguageResources("Luxembourgish", R.string.LB, R.drawable.flag_of_luxembourg));\r
- isoCodeToResources.put("MK", new LanguageResources("Macedonian", R.string.MK, R.drawable.flag_of_macedonia));\r
- isoCodeToResources.put("LO", new LanguageResources("Lao", R.string.LO, R.drawable.flag_of_laos));\r
- isoCodeToResources.put("ML", new LanguageResources("Malayalam", R.string.ML));\r
- isoCodeToResources.put("SL", new LanguageResources("Slovenian", R.string.SL, R.drawable.flag_of_slovenia));\r
- isoCodeToResources.put("TA", new LanguageResources("Tamil", R.string.TA));\r
- isoCodeToResources.put("SH", new LanguageResources("Serbo-Croatian", R.string.SH));\r
-\r
- // Hack to allow lower-case ISO codes to work:\r
- for (final String isoCode : new ArrayList<String>(isoCodeToResources.keySet())) {\r
- isoCodeToResources.put(isoCode.toLowerCase(), isoCodeToResources.get(isoCode));\r
+\r
+ private static final Map<String, Language> registry = new LinkedHashMap<String, Language>();\r
+\r
+ final String isoCode;\r
+ final Locale locale;\r
+\r
+ private Collator collator;\r
+\r
+ private Language(final Locale locale, final String isoCode) {\r
+ this.locale = locale;\r
+ this.isoCode = isoCode;\r
+\r
+ registry.put(isoCode.toLowerCase(), this);\r
}\r
\r
- }\r
-\r
-\r
- private static final Map<String, Language> registry = new LinkedHashMap<String, Language>();\r
-\r
- final String isoCode;\r
- final Locale locale;\r
- \r
- private Collator collator;\r
-\r
- private Language(final Locale locale, final String isoCode) {\r
- this.locale = locale;\r
- this.isoCode = isoCode;\r
-\r
- registry.put(isoCode.toLowerCase(), this);\r
- }\r
-\r
- @Override\r
- public String toString() {\r
- return locale.toString();\r
- }\r
- \r
- public String getIsoCode() {\r
- return isoCode;\r
- }\r
- \r
- public synchronized Collator getCollator() {\r
- // Don't think this is thread-safe...\r
-// if (collator == null) {\r
- this.collator = Collator.getInstance(locale);\r
- this.collator.setStrength(Collator.IDENTICAL);\r
-// }\r
- return collator;\r
- }\r
- \r
- public String getDefaultNormalizerRules() {\r
- return ":: Any-Latin; ' ' > ; :: Lower; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC ;";\r
- }\r
- \r
- /**\r
- * A practical pattern to identify strong RTL characters. This pattern is not\r
- * completely correct according to the Unicode standard. It is simplified for\r
- * performance and small code size.\r
- */\r
- private static final String rtlChars =\r
- "\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC";\r
-\r
- private static final String puncChars =\r
- "\\[\\]\\(\\)\\{\\}\\=";\r
-\r
- private static final Pattern RTL_LEFT_BOUNDARY = Pattern.compile("(["+ puncChars +"])([" + rtlChars + "])");\r
- private static final Pattern RTL_RIGHT_BOUNDARY = Pattern.compile("([" + rtlChars + "])(["+ puncChars +"])");\r
- \r
- public static String fixBidiText(String text) {\r
-// text = RTL_LEFT_BOUNDARY.matcher(text).replaceAll("$1\u200e $2");\r
-// text = RTL_RIGHT_BOUNDARY.matcher(text).replaceAll("$1 \u200e$2");\r
- return text;\r
- }\r
- \r
- // ----------------------------------------------------------------\r
-\r
- public static final Language en = new Language(Locale.ENGLISH, "EN");\r
- public static final Language fr = new Language(Locale.FRENCH, "FR");\r
- public static final Language it = new Language(Locale.ITALIAN, "IT");\r
-\r
- public static final Language de = new Language(Locale.GERMAN, "DE") {\r
@Override\r
+ public String toString() {\r
+ return locale.toString();\r
+ }\r
+\r
+ public String getIsoCode() {\r
+ return isoCode;\r
+ }\r
+\r
+ public synchronized Collator getCollator() {\r
+ // Don't think this is thread-safe...\r
+ // if (collator == null) {\r
+ this.collator = Collator.getInstance(locale);\r
+ this.collator.setStrength(Collator.IDENTICAL);\r
+ // }\r
+ return collator;\r
+ }\r
+\r
public String getDefaultNormalizerRules() {\r
- return ":: Lower; 'ae' > 'ä'; 'oe' > 'ö'; 'ue' > 'ü'; 'ß' > 'ss'; ";\r
+ return ":: Any-Latin; ' ' > ; :: Lower; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC ;";\r
}\r
- };\r
- \r
- // ----------------------------------------------------------------\r
-\r
- public static synchronized Language lookup(final String isoCode) {\r
- Language lang = registry.get(isoCode.toLowerCase());\r
- if (lang == null) {\r
- lang = new Language(new Locale(isoCode), isoCode);\r
+\r
+ /**\r
+ * A practical pattern to identify strong RTL characters. This pattern is\r
+ * not completely correct according to the Unicode standard. It is\r
+ * simplified for performance and small code size.\r
+ */\r
+ private static final String rtlChars =\r
+ "\u0591-\u07FF\uFB1D-\uFDFD\uFE70-\uFEFC";\r
+\r
+ private static final String puncChars =\r
+ "\\[\\]\\(\\)\\{\\}\\=";\r
+\r
+ private static final Pattern RTL_LEFT_BOUNDARY = Pattern.compile("([" + puncChars + "])(["\r
+ + rtlChars + "])");\r
+ private static final Pattern RTL_RIGHT_BOUNDARY = Pattern.compile("([" + rtlChars + "])(["\r
+ + puncChars + "])");\r
+\r
+ public static String fixBidiText(String text) {\r
+ // text = RTL_LEFT_BOUNDARY.matcher(text).replaceAll("$1\u200e $2");\r
+ // text = RTL_RIGHT_BOUNDARY.matcher(text).replaceAll("$1 \u200e$2");\r
+ return text;\r
+ }\r
+\r
+ // ----------------------------------------------------------------\r
+\r
+ public static final Language en = new Language(Locale.ENGLISH, "EN");\r
+ public static final Language fr = new Language(Locale.FRENCH, "FR");\r
+ public static final Language it = new Language(Locale.ITALIAN, "IT");\r
+\r
+ public static final Language de = new Language(Locale.GERMAN, "DE") {\r
+ @Override\r
+ public String getDefaultNormalizerRules() {\r
+ return ":: Lower; 'ae' > 'ä'; 'oe' > 'ö'; 'ue' > 'ü'; 'ß' > 'ss'; ";\r
+ }\r
+ };\r
+\r
+ // ----------------------------------------------------------------\r
+\r
+ public static synchronized Language lookup(final String isoCode) {\r
+ Language lang = registry.get(isoCode.toLowerCase());\r
+ if (lang == null) {\r
+ lang = new Language(new Locale(isoCode), isoCode);\r
+ }\r
+ return lang;\r
}\r
- return lang;\r
- }\r
\r
}\r
import java.util.Comparator;
public class NormalizeComparator implements Comparator<String> {
-
- final Transliterator normalizer;
- final Comparator<Object> comparator;
-
- public NormalizeComparator(final Transliterator normalizer,
- final Comparator<Object> comparator) {
- this.normalizer = normalizer;
- this.comparator = comparator;
- }
-
- @Override
- public int compare(final String s1, final String s2) {
- final String n1 = normalizer.transform(s1);
- final String n2 = normalizer.transform(s2);
- final int cn = comparator.compare(n1, n2);
- if (cn != 0) {
- return cn;
+
+ final Transliterator normalizer;
+ final Comparator<Object> comparator;
+
+ public NormalizeComparator(final Transliterator normalizer,
+ final Comparator<Object> comparator) {
+ this.normalizer = normalizer;
+ this.comparator = comparator;
+ }
+
+ @Override
+ public int compare(final String s1, final String s2) {
+ final String n1 = normalizer.transform(s1);
+ final String n2 = normalizer.transform(s2);
+ final int cn = comparator.compare(n1, n2);
+ if (cn != 0) {
+ return cn;
+ }
+ return comparator.compare(s1, s2);
}
- return comparator.compare(s1, s2);
- }
}
import java.util.List;
import java.util.regex.Pattern;
-public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>, Comparable<PairEntry> {
-
- public final List<Pair> pairs;
-
- public PairEntry(final EntrySource entrySource) {
- super(entrySource);
- pairs = new ArrayList<Pair>(1);
- }
-
- public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
- this(entrySource);
- this.pairs.add(new Pair(lang1, lang2));
- }
-
- public PairEntry(final Dictionary dictionary, final RandomAccessFile raf, final int index) throws IOException {
- super(dictionary, raf, index);
- final int size = raf.readInt();
- pairs = new ArrayList<PairEntry.Pair>(size);
- for (int i = 0; i < size; ++i) {
- pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
+public class PairEntry extends AbstractEntry implements RAFSerializable<PairEntry>,
+ Comparable<PairEntry> {
+
+ public final List<Pair> pairs;
+
+ public PairEntry(final EntrySource entrySource) {
+ super(entrySource);
+ pairs = new ArrayList<Pair>(1);
}
- }
- @Override
- public void write(RandomAccessFile raf) throws IOException {
- super.write(raf);
- // TODO: this could be a short.
- raf.writeInt(pairs.size());
- for (int i = 0; i < pairs.size(); ++i) {
- assert pairs.get(i).lang1.length() > 0;
- raf.writeUTF(pairs.get(i).lang1);
- raf.writeUTF(pairs.get(i).lang2);
+
+ public PairEntry(final EntrySource entrySource, final String lang1, final String lang2) {
+ this(entrySource);
+ this.pairs.add(new Pair(lang1, lang2));
}
- }
-
- static final class Serializer implements RAFListSerializer<PairEntry> {
-
- final Dictionary dictionary;
-
- Serializer(Dictionary dictionary) {
- this.dictionary = dictionary;
+
+ public PairEntry(final Dictionary dictionary, final RandomAccessFile raf, final int index)
+ throws IOException {
+ super(dictionary, raf, index);
+ final int size = raf.readInt();
+ pairs = new ArrayList<PairEntry.Pair>(size);
+ for (int i = 0; i < size; ++i) {
+ pairs.add(new Pair(raf.readUTF(), raf.readUTF()));
+ }
}
@Override
- public PairEntry read(RandomAccessFile raf, int index) throws IOException {
- return new PairEntry(dictionary, raf, index);
+ public void write(RandomAccessFile raf) throws IOException {
+ super.write(raf);
+ // TODO: this could be a short.
+ raf.writeInt(pairs.size());
+ for (int i = 0; i < pairs.size(); ++i) {
+ assert pairs.get(i).lang1.length() > 0;
+ raf.writeUTF(pairs.get(i).lang1);
+ raf.writeUTF(pairs.get(i).lang2);
+ }
}
+ static final class Serializer implements RAFListSerializer<PairEntry> {
+
+ final Dictionary dictionary;
+
+ Serializer(Dictionary dictionary) {
+ this.dictionary = dictionary;
+ }
+
+ @Override
+ public PairEntry read(RandomAccessFile raf, int index) throws IOException {
+ return new PairEntry(dictionary, raf, index);
+ }
+
+ @Override
+ public void write(RandomAccessFile raf, PairEntry t) throws IOException {
+ t.write(raf);
+ }
+ };
+
@Override
- public void write(RandomAccessFile raf, PairEntry t) throws IOException {
- t.write(raf);
- }
- };
-
- @Override
- public void addToDictionary(final Dictionary dictionary) {
- assert index == -1;
- dictionary.pairEntries.add(this);
- index = dictionary.pairEntries.size() - 1;
- }
-
- @Override
- public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
- return new Row(this.index, rowIndex, dictionaryIndex);
- }
-
-
- // --------------------------------------------------------------------
-
-
- public static class Row extends RowBase {
-
- Row(final RandomAccessFile raf, final int thisRowIndex,
- final Index index) throws IOException {
- super(raf, thisRowIndex, index);
+ public void addToDictionary(final Dictionary dictionary) {
+ assert index == -1;
+ dictionary.pairEntries.add(this);
+ index = dictionary.pairEntries.size() - 1;
}
- Row(final int referenceIndex, final int thisRowIndex,
- final Index index) {
- super(referenceIndex, thisRowIndex, index);
- }
-
@Override
- public String toString() {
- return getRawText(false);
+ public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
+ return new Row(this.index, rowIndex, dictionaryIndex);
}
- public PairEntry getEntry() {
- return index.dict.pairEntries.get(referenceIndex);
- }
-
- @Override
- public void print(PrintStream out) {
- final PairEntry pairEntry = getEntry();
- for (int i = 0; i < pairEntry.pairs.size(); ++i) {
- out.print((i == 0 ? " " : " ") + pairEntry.pairs.get(i));
- out.println();
- }
+ // --------------------------------------------------------------------
+
+ public static class Row extends RowBase {
+
+ Row(final RandomAccessFile raf, final int thisRowIndex,
+ final Index index) throws IOException {
+ super(raf, thisRowIndex, index);
+ }
+
+ Row(final int referenceIndex, final int thisRowIndex,
+ final Index index) {
+ super(referenceIndex, thisRowIndex, index);
+ }
+
+ @Override
+ public String toString() {
+ return getRawText(false);
+ }
+
+ public PairEntry getEntry() {
+ return index.dict.pairEntries.get(referenceIndex);
+ }
+
+ @Override
+ public void print(PrintStream out) {
+ final PairEntry pairEntry = getEntry();
+ for (int i = 0; i < pairEntry.pairs.size(); ++i) {
+ out.print((i == 0 ? " " : " ") + pairEntry.pairs.get(i));
+ out.println();
+ }
+ }
+
+ @Override
+ public String getRawText(boolean compact) {
+ final PairEntry pairEntry = getEntry();
+ return pairEntry.getRawText(compact);
+ }
+
+ @Override
+ public RowMatchType matches(final List<String> searchTokens,
+ final Pattern orderedMatchPattern, final Transliterator normalizer,
+ final boolean swapPairEntries) {
+ final int side = swapPairEntries ? 1 : 0;
+ final List<Pair> pairs = getEntry().pairs;
+ final String[] pairSides = new String[pairs.size()];
+ for (int i = 0; i < pairs.size(); ++i) {
+ pairSides[i] = normalizer.transform(pairs.get(i).get(side));
+ }
+ for (int i = searchTokens.size() - 1; i >= 0; --i) {
+ final String searchToken = searchTokens.get(i);
+ boolean found = false;
+ for (final String pairSide : pairSides) {
+ found |= pairSide.contains(searchToken);
+ }
+ if (!found) {
+ return RowMatchType.NO_MATCH;
+ }
+ }
+ for (final String pairSide : pairSides) {
+ if (orderedMatchPattern.matcher(pairSide).find()) {
+ return RowMatchType.ORDERED_MATCH;
+ }
+ }
+ return RowMatchType.BAG_OF_WORDS_MATCH;
+ }
+
+ @Override
+ public int getSideLength(boolean swapPairEntries) {
+ int result = 0;
+ final int side = swapPairEntries ? 1 : 0;
+ for (final Pair pair : getEntry().pairs) {
+ result += pair.get(side).length();
+ }
+ return result;
+ }
+
}
- @Override
- public String getRawText(boolean compact) {
- final PairEntry pairEntry = getEntry();
- return pairEntry.getRawText(compact);
+ public String getRawText(final boolean compact) {
+ if (compact) {
+ return this.pairs.get(0).toStringTab();
+ }
+ final StringBuilder builder = new StringBuilder();
+ for (int i = 0; i < this.pairs.size(); ++i) {
+ if (i > 0) {
+ builder.append(" | ");
+ }
+ builder.append(this.pairs.get(i).lang1);
+ }
+ builder.append("\t");
+ for (int i = 0; i < this.pairs.size(); ++i) {
+ if (i > 0) {
+ builder.append(" | ");
+ }
+ builder.append(this.pairs.get(i).lang2);
+ }
+ return builder.toString();
}
@Override
- public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, final Transliterator normalizer, final boolean swapPairEntries) {
- final int side = swapPairEntries ? 1 : 0;
- final List<Pair> pairs = getEntry().pairs;
- final String[] pairSides = new String[pairs.size()];
- for (int i = 0; i < pairs.size(); ++i) {
- pairSides[i] = normalizer.transform(pairs.get(i).get(side));
- }
- for (int i = searchTokens.size() - 1; i >= 0; --i) {
- final String searchToken = searchTokens.get(i);
- boolean found = false;
- for (final String pairSide : pairSides) {
- found |= pairSide.contains(searchToken);
- }
- if (!found) {
- return RowMatchType.NO_MATCH;
- }
- }
- for (final String pairSide : pairSides) {
- if (orderedMatchPattern.matcher(pairSide).find()) {
- return RowMatchType.ORDERED_MATCH;
- }
- }
- return RowMatchType.BAG_OF_WORDS_MATCH;
+ public int compareTo(final PairEntry that) {
+ return this.getRawText(false).compareTo(that.getRawText(false));
}
-
+
@Override
- public int getSideLength(boolean swapPairEntries) {
- int result = 0;
- final int side = swapPairEntries ? 1 : 0;
- for (final Pair pair : getEntry().pairs) {
- result += pair.get(side).length();
- }
- return result;
+ public String toString() {
+ return getRawText(false);
}
-
- }
- public String getRawText(final boolean compact) {
- if (compact) {
- return this.pairs.get(0).toStringTab();
- }
- final StringBuilder builder = new StringBuilder();
- for (int i = 0; i < this.pairs.size(); ++i) {
- if (i > 0) {
- builder.append(" | ");
- }
- builder.append(this.pairs.get(i).lang1);
- }
- builder.append("\t");
- for (int i = 0; i < this.pairs.size(); ++i) {
- if (i > 0) {
- builder.append(" | ");
- }
- builder.append(this.pairs.get(i).lang2);
- }
- return builder.toString();
- }
-
- @Override
- public int compareTo(final PairEntry that) {
- return this.getRawText(false).compareTo(that.getRawText(false));
- }
-
- @Override
- public String toString() {
- return getRawText(false);
- }
-
- // -----------------------------------------------------------------------
-
- public static final class Pair {
-
- public final String lang1;
- public final String lang2;
-
- public Pair(final String lang1, final String lang2) {
- this.lang1 = lang1;
- this.lang2 = lang2;
- if (!(lang1.trim().length() > 0 && lang2.trim().length() > 0)) {
- System.err.println("poop");
- }
- assert lang1.trim().length() > 0 || lang2.trim().length() > 0 : "Empty pair!!!";
- assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
- }
+ // -----------------------------------------------------------------------
- public Pair(final String lang1, final String lang2, final boolean swap) {
- this(swap ? lang2 : lang1, swap ? lang1 : lang2);
- }
+ public static final class Pair {
- public String toString() {
- return lang1 + " :: " + lang2;
- }
+ public final String lang1;
+ public final String lang2;
- public String toStringTab() {
- return lang1 + "\t" + lang2;
- }
+ public Pair(final String lang1, final String lang2) {
+ this.lang1 = lang1;
+ this.lang2 = lang2;
+ if (!(lang1.trim().length() > 0 && lang2.trim().length() > 0)) {
+ System.err.println("poop");
+ }
+ assert lang1.trim().length() > 0 || lang2.trim().length() > 0 : "Empty pair!!!";
+ assert lang1.trim().length() > 0 && lang2.trim().length() > 0 : "Empty pair!!!";
+ }
- public String get(int i) {
- if (i == 0) {
- return lang1;
- } else if (i == 1) {
- return lang2;
- }
- throw new IllegalArgumentException();
- }
+ public Pair(final String lang1, final String lang2, final boolean swap) {
+ this(swap ? lang2 : lang1, swap ? lang1 : lang2);
+ }
- }
+ public String toString() {
+ return lang1 + " :: " + lang2;
+ }
+
+ public String toStringTab() {
+ return lang1 + "\t" + lang2;
+ }
+
+ public String get(int i) {
+ if (i == 0) {
+ return lang1;
+ } else if (i == 1) {
+ return lang2;
+ }
+ throw new IllegalArgumentException();
+ }
+
+ }
}
import java.util.regex.Pattern;
public abstract class RowBase extends IndexedObject {
- /**
- * the Index owning this RowBase.
- */
- public final Index index;
-
- /**
- * Where this RowBase points to.
- */
- public final int referenceIndex;
-
- /**
- * the TokenRow above this RowBase, populated on demand.
- */
- private TokenRow tokenRow = null;
-
- RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index) throws IOException {
- super(thisRowIndex);
- this.index = index;
- this.referenceIndex = raf.readInt(); // what this points to.
- }
-
- public RowBase(final int referenceIndex, final int thisRowIndex, final Index index) {
- super(thisRowIndex);
- this.index = index;
- this.referenceIndex = referenceIndex;
- }
-
- static final class RowKey {
- final Class<? extends RowBase> rowClass;
- final int referenceIndex;
-
- private RowKey(Class<? extends RowBase> rowClass, int referenceIndex) {
- this.rowClass = rowClass;
- this.referenceIndex = referenceIndex;
- }
-
- @Override
- public boolean equals(Object o) {
- if (!(o instanceof RowKey)) {
- return false;
- }
- final RowKey that = (RowKey) o;
- return this.referenceIndex == that.referenceIndex && this.rowClass.equals(that.rowClass);
+ /**
+ * the Index owning this RowBase.
+ */
+ public final Index index;
+
+ /**
+ * Where this RowBase points to.
+ */
+ public final int referenceIndex;
+
+ /**
+ * the TokenRow above this RowBase, populated on demand.
+ */
+ private TokenRow tokenRow = null;
+
+ RowBase(final RandomAccessFile raf, final int thisRowIndex, final Index index)
+ throws IOException {
+ super(thisRowIndex);
+ this.index = index;
+ this.referenceIndex = raf.readInt(); // what this points to.
}
-
- @Override
- public int hashCode() {
- return rowClass.hashCode() + referenceIndex;
+
+ public RowBase(final int referenceIndex, final int thisRowIndex, final Index index) {
+ super(thisRowIndex);
+ this.index = index;
+ this.referenceIndex = referenceIndex;
}
- }
-
- public RowKey getRowKey() {
- return new RowKey(this.getClass(), referenceIndex);
- }
-
- /**
- * @return the TokenRow that this row is "filed under".
- */
- public TokenRow getTokenRow(final boolean search) {
- if (tokenRow == null && search) {
- int r = index() - 1;
- int rUp = index() + 1;
- while (r >= 0) {
- final RowBase row = index.rows.get(r);
- final TokenRow candidate = row.getTokenRow(false);
- if (candidate != null) {
- for (++r; r <= index(); ++r) {
- index.rows.get(r).setTokenRow(candidate);
- }
- break;
+
+ static final class RowKey {
+ final Class<? extends RowBase> rowClass;
+ final int referenceIndex;
+
+ private RowKey(Class<? extends RowBase> rowClass, int referenceIndex) {
+ this.rowClass = rowClass;
+ this.referenceIndex = referenceIndex;
}
- if (rUp < index.rows.size()) {
- final RowBase rowUp = index.rows.get(rUp);
- TokenRow candidateUp = rowUp.getTokenRow(false);
- if (candidateUp != null) {
- // Did we hit the next set of TokenRows?
- if (candidateUp.index() > this.index()) {
- final int tokenIndex = index.sortedIndexEntries.get(candidateUp.referenceIndex - 1).startRow;
- candidateUp = (TokenRow) index.rows.get(tokenIndex);
- }
- for (--rUp; rUp >= index(); --rUp) {
- index.rows.get(rUp).setTokenRow(candidateUp);
+
+ @Override
+ public boolean equals(Object o) {
+ if (!(o instanceof RowKey)) {
+ return false;
}
- break;
- }
- rUp++;
+ final RowKey that = (RowKey) o;
+ return this.referenceIndex == that.referenceIndex
+ && this.rowClass.equals(that.rowClass);
}
- --r;
- }
- assert tokenRow != null;
+
+ @Override
+ public int hashCode() {
+ return rowClass.hashCode() + referenceIndex;
+ }
+ }
+
+ public RowKey getRowKey() {
+ return new RowKey(this.getClass(), referenceIndex);
}
- return tokenRow;
- }
-
- public void setTokenRow(TokenRow tokenRow) {
- assert this.tokenRow == null;
- assert tokenRow != null;
- this.tokenRow = tokenRow;
- }
-
- public abstract void print(PrintStream out);
-
- public abstract String getRawText(final boolean compact);
-
- public abstract RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatch, final Transliterator normalizer, boolean swapPairEntries);
-
- // RowBase must manage "disk-based" polymorphism. All other polymorphism is
- // dealt with in the normal manner.
- static class Serializer implements RAFListSerializer<RowBase> {
-
- final Index index;
-
- Serializer(final Index index) {
- this.index = index;
+
+ /**
+ * @return the TokenRow that this row is "filed under".
+ */
+ public TokenRow getTokenRow(final boolean search) {
+ if (tokenRow == null && search) {
+ int r = index() - 1;
+ int rUp = index() + 1;
+ while (r >= 0) {
+ final RowBase row = index.rows.get(r);
+ final TokenRow candidate = row.getTokenRow(false);
+ if (candidate != null) {
+ for (++r; r <= index(); ++r) {
+ index.rows.get(r).setTokenRow(candidate);
+ }
+ break;
+ }
+ if (rUp < index.rows.size()) {
+ final RowBase rowUp = index.rows.get(rUp);
+ TokenRow candidateUp = rowUp.getTokenRow(false);
+ if (candidateUp != null) {
+ // Did we hit the next set of TokenRows?
+ if (candidateUp.index() > this.index()) {
+ final int tokenIndex = index.sortedIndexEntries
+ .get(candidateUp.referenceIndex - 1).startRow;
+ candidateUp = (TokenRow) index.rows.get(tokenIndex);
+ }
+ for (--rUp; rUp >= index(); --rUp) {
+ index.rows.get(rUp).setTokenRow(candidateUp);
+ }
+ break;
+ }
+ rUp++;
+ }
+ --r;
+ }
+ assert tokenRow != null;
+ }
+ return tokenRow;
}
- @Override
- public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
- final byte rowType = raf.readByte();
- if (rowType == 0) {
- return new PairEntry.Row(raf, listIndex, index);
- } else if (rowType == 1 || rowType == 3) {
- return new TokenRow(raf, listIndex, index, /* hasMainEntry */ rowType == 1);
- } else if (rowType == 2) {
- return new TextEntry.Row(raf, listIndex, index);
- } else if (rowType == 4) {
- return new HtmlEntry.Row(raf, listIndex, index);
- }
- throw new RuntimeException("Invalid rowType:" + rowType);
+ public void setTokenRow(TokenRow tokenRow) {
+ assert this.tokenRow == null;
+ assert tokenRow != null;
+ this.tokenRow = tokenRow;
}
- @Override
- public void write(RandomAccessFile raf, RowBase t) throws IOException {
- if (t instanceof PairEntry.Row) {
- raf.writeByte(0);
- } else if (t instanceof TokenRow) {
- final TokenRow tokenRow = (TokenRow) t;
- raf.writeByte(tokenRow.hasMainEntry ? 1 : 3);
- } else if (t instanceof TextEntry.Row) {
- raf.writeByte(2);
- } else if (t instanceof HtmlEntry.Row) {
- raf.writeByte(4);
- }
- raf.writeInt(t.referenceIndex);
+ public abstract void print(PrintStream out);
+
+ public abstract String getRawText(final boolean compact);
+
+ public abstract RowMatchType matches(final List<String> searchTokens,
+ final Pattern orderedMatch, final Transliterator normalizer, boolean swapPairEntries);
+
+ // RowBase must manage "disk-based" polymorphism. All other polymorphism is
+ // dealt with in the normal manner.
+ static class Serializer implements RAFListSerializer<RowBase> {
+
+ final Index index;
+
+ Serializer(final Index index) {
+ this.index = index;
+ }
+
+ @Override
+ public RowBase read(RandomAccessFile raf, final int listIndex) throws IOException {
+ final byte rowType = raf.readByte();
+ if (rowType == 0) {
+ return new PairEntry.Row(raf, listIndex, index);
+ } else if (rowType == 1 || rowType == 3) {
+ return new TokenRow(raf, listIndex, index, /* hasMainEntry */rowType == 1);
+ } else if (rowType == 2) {
+ return new TextEntry.Row(raf, listIndex, index);
+ } else if (rowType == 4) {
+ return new HtmlEntry.Row(raf, listIndex, index);
+ }
+ throw new RuntimeException("Invalid rowType:" + rowType);
+ }
+
+ @Override
+ public void write(RandomAccessFile raf, RowBase t) throws IOException {
+ if (t instanceof PairEntry.Row) {
+ raf.writeByte(0);
+ } else if (t instanceof TokenRow) {
+ final TokenRow tokenRow = (TokenRow) t;
+ raf.writeByte(tokenRow.hasMainEntry ? 1 : 3);
+ } else if (t instanceof TextEntry.Row) {
+ raf.writeByte(2);
+ } else if (t instanceof HtmlEntry.Row) {
+ raf.writeByte(4);
+ }
+ raf.writeInt(t.referenceIndex);
+ }
}
- }
-
- public static final class LengthComparator implements Comparator<RowBase> {
-
- final boolean swapPairEntries;
-
- public LengthComparator(boolean swapPairEntries) {
- this.swapPairEntries = swapPairEntries;
+
+ public static final class LengthComparator implements Comparator<RowBase> {
+
+ final boolean swapPairEntries;
+
+ public LengthComparator(boolean swapPairEntries) {
+ this.swapPairEntries = swapPairEntries;
+ }
+
+ @Override
+ public int compare(RowBase row1, RowBase row2) {
+ final int l1 = row1.getSideLength(swapPairEntries);
+ final int l2 = row2.getSideLength(swapPairEntries);
+ return l1 < l2 ? -1 : l1 == l2 ? 0 : 1;
+ }
}
- @Override
- public int compare(RowBase row1, RowBase row2) {
- final int l1 = row1.getSideLength(swapPairEntries);
- final int l2 = row2.getSideLength(swapPairEntries);
- return l1 < l2 ? -1 : l1 == l2 ? 0 : 1;
+ public int getSideLength(boolean swapPairEntries) {
+ return getRawText(false).length();
}
- }
- public int getSideLength(boolean swapPairEntries) {
- return getRawText(false).length();
- }
-
}
import java.util.regex.Pattern;
public class TextEntry extends AbstractEntry implements RAFSerializable<TextEntry> {
-
- final String text;
-
- public TextEntry(final Dictionary dictionary, final RandomAccessFile raf, final int index) throws IOException {
- super(dictionary, raf, index);
- text = raf.readUTF();
- throw new RuntimeException();
- }
- @Override
- public void write(RandomAccessFile raf) throws IOException {
- super.write(raf);
- raf.writeUTF(text);
- }
-
- static final class Serializer implements RAFListSerializer<TextEntry> {
-
- final Dictionary dictionary;
-
- Serializer(Dictionary dictionary) {
- this.dictionary = dictionary;
- }
- @Override
- public TextEntry read(RandomAccessFile raf, final int index) throws IOException {
- return new TextEntry(dictionary, raf, index);
- }
+ final String text;
- @Override
- public void write(RandomAccessFile raf, TextEntry t) throws IOException {
- t.write(raf);
- }
- };
-
-
- @Override
- public void addToDictionary(final Dictionary dictionary) {
- assert index == -1;
- dictionary.textEntries.add(this);
- index = dictionary.textEntries.size() - 1;
- }
-
- @Override
- public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
- throw new UnsupportedOperationException("TextEntry's don't really exist.");
- }
-
- public static class Row extends RowBase {
-
- Row(final RandomAccessFile raf, final int thisRowIndex,
- final Index index) throws IOException {
- super(raf, thisRowIndex, index);
+ public TextEntry(final Dictionary dictionary, final RandomAccessFile raf, final int index)
+ throws IOException {
+ super(dictionary, raf, index);
+ text = raf.readUTF();
+ throw new RuntimeException();
}
-
- public TextEntry getEntry() {
- return index.dict.textEntries.get(referenceIndex);
- }
-
+
@Override
- public void print(PrintStream out) {
- out.println(" " + getEntry().text);
+ public void write(RandomAccessFile raf) throws IOException {
+ super.write(raf);
+ raf.writeUTF(text);
}
+ static final class Serializer implements RAFListSerializer<TextEntry> {
+
+ final Dictionary dictionary;
+
+ Serializer(Dictionary dictionary) {
+ this.dictionary = dictionary;
+ }
+
+ @Override
+ public TextEntry read(RandomAccessFile raf, final int index) throws IOException {
+ return new TextEntry(dictionary, raf, index);
+ }
+
+ @Override
+ public void write(RandomAccessFile raf, TextEntry t) throws IOException {
+ t.write(raf);
+ }
+ };
+
@Override
- public String getRawText(boolean compact) {
- return getEntry().text;
+ public void addToDictionary(final Dictionary dictionary) {
+ assert index == -1;
+ dictionary.textEntries.add(this);
+ index = dictionary.textEntries.size() - 1;
}
-
+
@Override
- public RowMatchType matches(final List<String> searchTokens, final Pattern orderedMatchPattern, Transliterator normalizer, boolean swapPairEntries) {
- return null;
+ public RowBase CreateRow(int rowIndex, Index dictionaryIndex) {
+ throw new UnsupportedOperationException("TextEntry's don't really exist.");
}
- }
+ public static class Row extends RowBase {
+
+ Row(final RandomAccessFile raf, final int thisRowIndex,
+ final Index index) throws IOException {
+ super(raf, thisRowIndex, index);
+ }
+ public TextEntry getEntry() {
+ return index.dict.textEntries.get(referenceIndex);
+ }
+
+ @Override
+ public void print(PrintStream out) {
+ out.println(" " + getEntry().text);
+ }
+
+ @Override
+ public String getRawText(boolean compact) {
+ return getEntry().text;
+ }
+
+ @Override
+ public RowMatchType matches(final List<String> searchTokens,
+ final Pattern orderedMatchPattern, Transliterator normalizer,
+ boolean swapPairEntries) {
+ return null;
+ }
+ }
}
import java.util.regex.Pattern;
public class TokenRow extends RowBase {
-
- public final boolean hasMainEntry;
-
- TokenRow(final RandomAccessFile raf, final int thisRowIndex, final Index index, final boolean hasMainEntry) throws IOException {
- super(raf, thisRowIndex, index);
- this.hasMainEntry = hasMainEntry;
- }
-
- TokenRow(final int referenceIndex, final int thisRowIndex, final Index index, final boolean hasMainEntry) {
- super(referenceIndex, thisRowIndex, index);
- this.hasMainEntry = hasMainEntry;
- }
-
- public String toString() {
- return getToken() + "@" + referenceIndex;
- }
-
- @Override
- public TokenRow getTokenRow(final boolean search) {
- return this;
- }
-
- @Override
- public void setTokenRow(TokenRow tokenRow) {
- throw new RuntimeException("Shouldn't be setting TokenRow's TokenRow!");
- }
-
- public String getToken() {
- return getIndexEntry().token;
- }
-
- public IndexEntry getIndexEntry() {
- return index.sortedIndexEntries.get(referenceIndex);
- }
-
- @Override
- public void print(final PrintStream out) {
- final String surrounder = hasMainEntry ? "***" : "===";
- out.println(surrounder + getToken() + surrounder);
- for (final HtmlEntry htmlEntry : index.sortedIndexEntries.get(referenceIndex).htmlEntries) {
- out.println("HtmlEntry: " + htmlEntry.title + " <<<" + htmlEntry.getHtml() + ">>>");
+
+ public final boolean hasMainEntry;
+
+ TokenRow(final RandomAccessFile raf, final int thisRowIndex, final Index index,
+ final boolean hasMainEntry) throws IOException {
+ super(raf, thisRowIndex, index);
+ this.hasMainEntry = hasMainEntry;
}
- }
- @Override
- public String getRawText(boolean compact) {
- return getToken();
- }
+ TokenRow(final int referenceIndex, final int thisRowIndex, final Index index,
+ final boolean hasMainEntry) {
+ super(referenceIndex, thisRowIndex, index);
+ this.hasMainEntry = hasMainEntry;
+ }
- @Override
- public RowMatchType matches(List<String> searchTokens, final Pattern orderedMatchPattern, Transliterator normalizer, boolean swapPairEntries) {
- return RowMatchType.NO_MATCH;
- }
+ public String toString() {
+ return getToken() + "@" + referenceIndex;
+ }
+ @Override
+ public TokenRow getTokenRow(final boolean search) {
+ return this;
+ }
+
+ @Override
+ public void setTokenRow(TokenRow tokenRow) {
+ throw new RuntimeException("Shouldn't be setting TokenRow's TokenRow!");
+ }
+
+ public String getToken() {
+ return getIndexEntry().token;
+ }
+
+ public IndexEntry getIndexEntry() {
+ return index.sortedIndexEntries.get(referenceIndex);
+ }
+
+ @Override
+ public void print(final PrintStream out) {
+ final String surrounder = hasMainEntry ? "***" : "===";
+ out.println(surrounder + getToken() + surrounder);
+ for (final HtmlEntry htmlEntry : index.sortedIndexEntries.get(referenceIndex).htmlEntries) {
+ out.println("HtmlEntry: " + htmlEntry.title + " <<<" + htmlEntry.getHtml() + ">>>");
+ }
+ }
+
+ @Override
+ public String getRawText(boolean compact) {
+ return getToken();
+ }
+
+ @Override
+ public RowMatchType matches(List<String> searchTokens, final Pattern orderedMatchPattern,
+ Transliterator normalizer, boolean swapPairEntries) {
+ return RowMatchType.NO_MATCH;
+ }
}
public class TransliteratorManager {
- private static boolean starting = false;
- private static boolean ready = false;
-
- // Whom to notify when we're all set up and ready to go.
- private static List<Callback> callbacks = new ArrayList<TransliteratorManager.Callback>();
-
- public static synchronized boolean init(final Callback callback) {
- if (ready) {
- return true;
- }
- if (callback != null) {
- callbacks.add(callback);
- }
- if (!starting) {
- starting = true;
- new Thread(init).start();
+ private static boolean starting = false;
+ private static boolean ready = false;
+
+ // Whom to notify when we're all set up and ready to go.
+ private static List<Callback> callbacks = new ArrayList<TransliteratorManager.Callback>();
+
+ public static synchronized boolean init(final Callback callback) {
+ if (ready) {
+ return true;
+ }
+ if (callback != null) {
+ callbacks.add(callback);
+ }
+ if (!starting) {
+ starting = true;
+ new Thread(init).start();
+ }
+ return false;
}
- return false;
- }
-
- private static final Runnable init = new Runnable() {
- @Override
- public void run() {
- System.out.println("Starting Transliterator load.");
- final String transliterated =
- Transliterator.createFromRules("", ":: Any-Latin; :: Lower; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC ;",
- Transliterator.FORWARD).transliterate("Îñţérñåţîöñåļîžåţîờñ");
- if (!"internationalization".equals(transliterated)) {
- System.out.println("Wrong transliteratation: " + transliterated);
- }
- final List<Callback> callbacks = new ArrayList<TransliteratorManager.Callback>();
- synchronized (TransliteratorManager.class) {
- callbacks.addAll(TransliteratorManager.callbacks);
- ready = true;
- }
- for (final Callback callback : callbacks) {
- callback.onTransliteratorReady();
- }
+ private static final Runnable init = new Runnable() {
+ @Override
+ public void run() {
+ System.out.println("Starting Transliterator load.");
+ final String transliterated =
+ Transliterator
+ .createFromRules(
+ "",
+ ":: Any-Latin; :: Lower; :: NFD; :: [:Nonspacing Mark:] Remove; :: NFC ;",
+ Transliterator.FORWARD).transliterate("Îñţérñåţîöñåļîžåţîờñ");
+ if (!"internationalization".equals(transliterated)) {
+ System.out.println("Wrong transliteratation: " + transliterated);
+ }
+
+ final List<Callback> callbacks = new ArrayList<TransliteratorManager.Callback>();
+ synchronized (TransliteratorManager.class) {
+ callbacks.addAll(TransliteratorManager.callbacks);
+ ready = true;
+ }
+ for (final Callback callback : callbacks) {
+ callback.onTransliteratorReady();
+ }
+ }
+ };
+
+ public interface Callback {
+ void onTransliteratorReady();
}
- };
-
-
- public interface Callback {
- void onTransliteratorReady();
- }
}
+
package com.hughes.android.util;
import android.content.Context;
import android.view.View.OnClickListener;
public class IntentLauncher implements OnClickListener {
-
- final Context context;
- final Intent intent;
-
- public IntentLauncher(final Context context, final Intent intent) {
- this.context = context;
- this.intent = intent;
- }
-
- protected void onGo() {
- }
-
-
- private void go() {
- onGo();
- context.startActivity(intent);
- }
-
- @Override
- public void onClick(View v) {
- go();
- }
+
+ final Context context;
+ final Intent intent;
+
+ public IntentLauncher(final Context context, final Intent intent) {
+ this.context = context;
+ this.intent = intent;
+ }
+
+ protected void onGo() {
+ }
+
+ private void go() {
+ onGo();
+ context.startActivity(intent);
+ }
+
+ @Override
+ public void onClick(View v) {
+ go();
+ }
}
// each span.
final int color;
-
+
public NonLinkClickableSpan(int color) {
this.color = color;
}
// Won't see these on a long-click.
@Override
public void onClick(View widget) {
- // Don't need to do anything. These spans are just used to see where
+ // Don't need to do anything. These spans are just used to see where
// the user long-pressed.
}
public class PersistentObjectCache {
- private final File dir;
- private final Map<String, Object> objects = new LinkedHashMap<String, Object>();
-
- public synchronized <T> T read(final String filename, final Class<T> resultClass) {
- try {
- Object object = (objects.get(filename));
- if (object != null) {
- return resultClass.cast(object);
- }
- Log.d(getClass().getSimpleName(), "Cache miss.");
- final File src = new File(dir, filename);
- if (!src.canRead()) {
- Log.d(getClass().getSimpleName(), "File empty: " + src);
- return null;
- }
- try {
- final ObjectInputStream in = new ObjectInputStream(new FileInputStream(src));
- object = in.readObject();
- in.close();
- } catch (Exception e) {
- Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e);
- return null;
- }
- objects.put(filename, object);
- return resultClass.cast(object);
- } catch (ClassCastException e) {
- return null;
+ private final File dir;
+ private final Map<String, Object> objects = new LinkedHashMap<String, Object>();
+
+ public synchronized <T> T read(final String filename, final Class<T> resultClass) {
+ try {
+ Object object = (objects.get(filename));
+ if (object != null) {
+ return resultClass.cast(object);
+ }
+ Log.d(getClass().getSimpleName(), "Cache miss.");
+ final File src = new File(dir, filename);
+ if (!src.canRead()) {
+ Log.d(getClass().getSimpleName(), "File empty: " + src);
+ return null;
+ }
+ try {
+ final ObjectInputStream in = new ObjectInputStream(new FileInputStream(src));
+ object = in.readObject();
+ in.close();
+ } catch (Exception e) {
+ Log.e(getClass().getSimpleName(), "Deserialization failed: " + src, e);
+ return null;
+ }
+ objects.put(filename, object);
+ return resultClass.cast(object);
+ } catch (ClassCastException e) {
+ return null;
+ }
}
- }
-
- public synchronized void write(final String filename, final Object object) {
- objects.put(filename, object);
- final File dest = new File(dir, filename);
- try {
- final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest));
- out.writeObject(object);
- out.close();
- } catch (Exception e) {
- Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e);
+
+ public synchronized void write(final String filename, final Object object) {
+ objects.put(filename, object);
+ final File dest = new File(dir, filename);
+ try {
+ final ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(dest));
+ out.writeObject(object);
+ out.close();
+ } catch (Exception e) {
+ Log.e(getClass().getSimpleName(), "Serialization failed: " + dest, e);
+ }
}
- }
- private PersistentObjectCache(final Context context) {
- final File filesDir = context.getFilesDir();
- dir = filesDir != null ? filesDir : Environment.getExternalStorageDirectory();
- if (dir == null) {
- throw new RuntimeException("context.getFilesDir() == " + context.getFilesDir() + ", Environment.getExternalStorageDirectory()=" + Environment.getExternalStorageDirectory());
+ private PersistentObjectCache(final Context context) {
+ final File filesDir = context.getFilesDir();
+ dir = filesDir != null ? filesDir : Environment.getExternalStorageDirectory();
+ if (dir == null) {
+ throw new RuntimeException("context.getFilesDir() == " + context.getFilesDir()
+ + ", Environment.getExternalStorageDirectory()="
+ + Environment.getExternalStorageDirectory());
+ }
}
- }
-
- public static synchronized PersistentObjectCache getInstance() {
- if (instance == null) {
- throw new RuntimeException("getInstance called before init.");
+
+ public static synchronized PersistentObjectCache getInstance() {
+ if (instance == null) {
+ throw new RuntimeException("getInstance called before init.");
+ }
+ return instance;
}
- return instance;
- }
- public static synchronized PersistentObjectCache init(final Context context) {
- if (instance == null) {
- instance = new PersistentObjectCache(context);
- } else {
- if (!instance.dir.equals(context.getFilesDir())) {
- throw new RuntimeException("File dir changed. old=" + instance.dir + ", new=" + context.getFilesDir());
+ public static synchronized PersistentObjectCache init(final Context context) {
+ if (instance == null) {
+ instance = new PersistentObjectCache(context);
+ } else {
+ if (!instance.dir.equals(context.getFilesDir())) {
+ throw new RuntimeException("File dir changed. old=" + instance.dir + ", new="
+ + context.getFilesDir());
+ }
}
- }
- return instance;
- }
-
- private static PersistentObjectCache instance = null;
+ return instance;
+ }
+
+ private static PersistentObjectCache instance = null;
}