package com.hughes.android.dictionary;
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.LinkedHashMap;
-import java.util.List;
-import java.util.Map;
-
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
+import android.net.Uri;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import com.hughes.android.dictionary.engine.Language;
import com.hughes.android.dictionary.engine.TransliteratorManager;
import com.hughes.android.util.PersistentObjectCache;
+import com.hughes.util.ListUtil;
import com.ibm.icu.text.Collator;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+
public class DictionaryApplication extends Application {
static final String LOG = "QuickDicApp";
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
- Log.d("THAD", "prefs changed: " + key);
+ Log.d("QuickDic", "prefs changed: " + key);
if (key.equals(getString(R.string.themeKey))) {
setTheme(getSelectedTheme().themeId);
}
final MenuItem help = menu.add(getString(R.string.help));
help.setOnMenuItemClickListener(new OnMenuItemClickListener() {
public boolean onMenuItemClick(final MenuItem menuItem) {
- context.startActivity(HelpActivity.getLaunchIntent());
+ context.startActivity(HtmlDisplayActivity.getHelpLaunchIntent());
return false;
}
});
return false;
}
});
+
+
+ final MenuItem reportIssue = menu.add(getString(R.string.reportIssue));
+ 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() {
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 getLanguageName(final String isoCode) {
final String lang = languageResources != null ? getApplicationContext().getString(languageResources.nameId) : isoCode;
return lang;
}
+
public synchronized String getDictionaryName(final String uncompressedFilename) {
+ final String currentLocale = Locale.getDefault().getLanguage().toLowerCase();
+ if (!currentLocale.equals(defaultLangISO2)) {
+ defaultLangISO2 = currentLocale;
+ fileToNameCache.clear();
+ defaultLangName = null;
+ }
+ if (defaultLangName == null) {
+ defaultLangName = getLanguageName(defaultLangISO2);
+ }
+
String name = fileToNameCache.get(uncompressedFilename);
if (name != null) {
return name;
final DictionaryInfo dictionaryInfo = DOWNLOADABLE_NAME_TO_INFO.get(uncompressedFilename);
if (dictionaryInfo != null) {
final StringBuilder nameBuilder = new StringBuilder();
+
+ // Hack to put the default locale first in the name.
+ boolean swapped = false;
+ if (dictionaryInfo.indexInfos.size() > 1 &&
+ dictionaryInfo.indexInfos.get(1).shortName.toLowerCase().equals(defaultLangISO2)) {
+ ListUtil.swap(dictionaryInfo.indexInfos, 0, 1);
+ swapped = true;
+ }
for (int i = 0; i < dictionaryInfo.indexInfos.size(); ++i) {
if (i > 0) {
nameBuilder.append("-");
}
nameBuilder.append(getLanguageName(dictionaryInfo.indexInfos.get(i).shortName));
}
+ if (swapped) {
+ ListUtil.swap(dictionaryInfo.indexInfos, 0, 1);
+ }
name = nameBuilder.toString();
} else {
name = uncompressedFilename.replace(".quickdic", "");
final Comparator<String> uncompressedFilenameComparator = new Comparator<String>() {
@Override
public int compare(String uncompressedFilename1, String uncompressedFilename2) {
- return collator.compare(getDictionaryName(uncompressedFilename1), getDictionaryName(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;
+ }
+ }
+ 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);
}
};
// Pick them up and put them at the end of the list.
final List<String> toAddSorted = new ArrayList<String>();
final File[] dictDirFiles = getDictDir().listFiles();
- for (final File file : dictDirFiles) {
- if (file.getName().endsWith(".zip")) {
- if (DOWNLOADABLE_NAME_TO_INFO.containsKey(file.getName().replace(".zip", ""))) {
- file.delete();
+ if (dictDirFiles != null) {
+ for (final File file : dictDirFiles) {
+ if (file.getName().endsWith(".zip")) {
+ if (DOWNLOADABLE_NAME_TO_INFO.containsKey(file.getName().replace(".zip", ""))) {
+ file.delete();
+ }
}
+ if (!file.getName().endsWith(".quickdic")) {
+ continue;
+ }
+ if (newDictionaryConfig.dictionaryInfoCache.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.dictionaryInfoCache.put(file.getName(), dictionaryInfo);
}
- if (!file.getName().endsWith(".quickdic")) {
- continue;
- }
- if (newDictionaryConfig.dictionaryInfoCache.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.dictionaryInfoCache.put(file.getName(), dictionaryInfo);
+ } else {
+ Log.w(LOG, "dictDir is not a diretory: " + getDictDir().getPath());
}
if (!toAddSorted.isEmpty()) {
Collections.sort(toAddSorted, uncompressedFilenameComparator);