1 // Copyright 2011 Google Inc. All Rights Reserved.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 package com.hughes.android.dictionary;
17 import android.app.Application;
18 import android.content.Context;
19 import android.content.Intent;
20 import android.content.SharedPreferences;
21 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
22 import android.net.Uri;
23 import android.os.Build;
24 import android.os.Environment;
25 import android.preference.PreferenceManager;
26 import android.support.v4.view.MenuItemCompat;
27 import android.util.Log;
28 import android.util.TypedValue;
29 import android.view.Menu;
30 import android.view.MenuItem;
31 import android.view.MenuItem.OnMenuItemClickListener;
32 import android.view.View;
33 import android.widget.Button;
34 import android.widget.ImageButton;
35 import android.widget.ImageView.ScaleType;
36 import android.widget.Toast;
38 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
39 import com.hughes.android.dictionary.engine.Dictionary;
40 import com.hughes.android.dictionary.engine.Language;
41 import com.hughes.android.dictionary.engine.Language.LanguageResources;
42 import com.hughes.android.dictionary.engine.TransliteratorManager;
43 import com.hughes.android.util.PersistentObjectCache;
44 import com.hughes.util.ListUtil;
45 import com.ibm.icu.text.Collator;
47 import java.io.BufferedReader;
49 import java.io.IOException;
50 import java.io.InputStreamReader;
51 import java.io.Serializable;
52 import java.util.ArrayList;
53 import java.util.Collections;
54 import java.util.Comparator;
55 import java.util.LinkedHashMap;
56 import java.util.List;
57 import java.util.Locale;
60 public class DictionaryApplication extends Application {
62 static final String LOG = "QuickDicApp";
64 // If set to false, avoid use of ICU collator
65 // Works well enough for most european languages,
66 // gives faster startup and avoids crashes on some
67 // devices due to Dalvik bugs (e.g. ARMv6, S5570i, CM11).
68 // Leave it enabled by default for correctness.
69 static public final boolean USE_COLLATOR = true;
71 // Static, determined by resources (and locale).
73 static Map<String, DictionaryInfo> DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO = null;
76 DEFAULT(R.style.Theme_Default,
77 R.style.Theme_Default_TokenRow_Fg,
78 R.color.theme_default_token_row_fg,
79 R.drawable.theme_default_token_row_main_bg,
80 R.drawable.theme_default_token_row_other_bg,
81 R.drawable.theme_default_normal_row_bg),
83 LIGHT(R.style.Theme_Light,
84 R.style.Theme_Light_TokenRow_Fg,
85 R.color.theme_light_token_row_fg,
86 R.drawable.theme_light_token_row_main_bg,
87 R.drawable.theme_light_token_row_other_bg,
88 R.drawable.theme_light_normal_row_bg);
90 Theme(final int themeId, final int tokenRowFg,
91 final int tokenRowFgColor,
92 final int tokenRowMainBg, final int tokenRowOtherBg,
93 final int normalRowBg) {
94 this.themeId = themeId;
95 this.tokenRowFg = tokenRowFg;
96 this.tokenRowFgColor = tokenRowFgColor;
97 this.tokenRowMainBg = tokenRowMainBg;
98 this.tokenRowOtherBg = tokenRowOtherBg;
99 this.normalRowBg = normalRowBg;
103 final int tokenRowFg;
104 final int tokenRowFgColor;
105 final int tokenRowMainBg;
106 final int tokenRowOtherBg;
107 final int normalRowBg;
111 // http://www.loc.gov/standards/iso639-2/php/code_list.php
112 public static final Map<String, LanguageResources> isoCodeToResources = new LinkedHashMap<String, LanguageResources>();
114 isoCodeToResources.put("AF", new LanguageResources("Afrikaans", R.string.AF,
115 R.drawable.flag_of_south_africa));
116 isoCodeToResources.put("SQ", new LanguageResources("Albanian", R.string.SQ,
117 R.drawable.flag_of_albania));
118 isoCodeToResources.put("AR",
119 new LanguageResources("Arabic", R.string.AR, R.drawable.arabic));
120 isoCodeToResources.put("HY", new LanguageResources("Armenian", R.string.HY,
121 R.drawable.flag_of_armenia));
122 isoCodeToResources.put("BE", new LanguageResources("Belarusian", R.string.BE,
123 R.drawable.flag_of_belarus));
124 isoCodeToResources.put("BN", new LanguageResources("Bengali", R.string.BN));
125 isoCodeToResources.put("BS", new LanguageResources("Bosnian", R.string.BS,
126 R.drawable.flag_of_bosnia_and_herzegovina));
127 isoCodeToResources.put("BG", new LanguageResources("Bulgarian", R.string.BG,
128 R.drawable.flag_of_bulgaria));
129 isoCodeToResources.put("MY", new LanguageResources("Burmese", R.string.MY,
130 R.drawable.flag_of_myanmar));
131 isoCodeToResources.put("ZH", new LanguageResources("Chinese", R.string.ZH,
132 R.drawable.flag_of_the_peoples_republic_of_china));
133 isoCodeToResources.put("cmn", new LanguageResources("Mandarin", R.string.cmn,
134 R.drawable.flag_of_the_peoples_republic_of_china));
135 isoCodeToResources.put("yue", new LanguageResources("Cantonese", R.string.yue,
136 R.drawable.flag_of_hong_kong));
137 isoCodeToResources.put("CA", new LanguageResources("Catalan", R.string.CA));
138 isoCodeToResources.put("HR", new LanguageResources("Croatian", R.string.HR,
139 R.drawable.flag_of_croatia));
140 isoCodeToResources.put("CS", new LanguageResources("Czech", R.string.CS,
141 R.drawable.flag_of_the_czech_republic));
142 isoCodeToResources.put("DA", new LanguageResources("Danish", R.string.DA,
143 R.drawable.flag_of_denmark));
144 isoCodeToResources.put("NL", new LanguageResources("Dutch", R.string.NL,
145 R.drawable.flag_of_the_netherlands));
146 isoCodeToResources.put("EN", new LanguageResources("English", R.string.EN,
147 R.drawable.flag_of_the_united_kingdom));
148 isoCodeToResources.put("EO", new LanguageResources("Esperanto", R.string.EO,
149 R.drawable.flag_of_esperanto));
150 isoCodeToResources.put("ET", new LanguageResources("Estonian", R.string.ET,
151 R.drawable.flag_of_estonia));
152 isoCodeToResources.put("FI", new LanguageResources("Finnish", R.string.FI,
153 R.drawable.flag_of_finland));
154 isoCodeToResources.put("FR", new LanguageResources("French", R.string.FR,
155 R.drawable.flag_of_france));
156 isoCodeToResources.put("DE", new LanguageResources("German", R.string.DE,
157 R.drawable.flag_of_germany));
158 isoCodeToResources.put("EL", new LanguageResources("Greek", R.string.EL,
159 R.drawable.flag_of_greece));
160 isoCodeToResources.put("grc", new LanguageResources("Ancient Greek", R.string.grc));
161 isoCodeToResources.put("haw", new LanguageResources("Hawaiian", R.string.haw,
162 R.drawable.flag_of_hawaii));
163 isoCodeToResources.put("HE", new LanguageResources("Hebrew", R.string.HE,
164 R.drawable.flag_of_israel));
165 isoCodeToResources.put("HI", new LanguageResources("Hindi", R.string.HI, R.drawable.hindi));
166 isoCodeToResources.put("HU", new LanguageResources("Hungarian", R.string.HU,
167 R.drawable.flag_of_hungary));
168 isoCodeToResources.put("IS", new LanguageResources("Icelandic", R.string.IS,
169 R.drawable.flag_of_iceland));
170 isoCodeToResources.put("ID", new LanguageResources("Indonesian", R.string.ID,
171 R.drawable.flag_of_indonesia));
172 isoCodeToResources.put("GA", new LanguageResources("Irish", R.string.GA,
173 R.drawable.flag_of_ireland));
174 isoCodeToResources.put("GD", new LanguageResources("Scottish Gaelic", R.string.GD,
175 R.drawable.flag_of_scotland));
176 isoCodeToResources.put("GV", new LanguageResources("Manx", R.string.GV,
177 R.drawable.flag_of_the_isle_of_man));
178 isoCodeToResources.put("IT", new LanguageResources("Italian", R.string.IT,
179 R.drawable.flag_of_italy));
180 isoCodeToResources.put("LA", new LanguageResources("Latin", R.string.LA));
181 isoCodeToResources.put("LV", new LanguageResources("Latvian", R.string.LV,
182 R.drawable.flag_of_latvia));
183 isoCodeToResources.put("LT", new LanguageResources("Lithuanian", R.string.LT,
184 R.drawable.flag_of_lithuania));
185 isoCodeToResources.put("JA", new LanguageResources("Japanese", R.string.JA,
186 R.drawable.flag_of_japan));
187 isoCodeToResources.put("KO", new LanguageResources("Korean", R.string.KO,
188 R.drawable.flag_of_south_korea));
189 isoCodeToResources.put("KU", new LanguageResources("Kurdish", R.string.KU));
190 isoCodeToResources.put("MS", new LanguageResources("Malay", R.string.MS,
191 R.drawable.flag_of_malaysia));
192 isoCodeToResources.put("MI", new LanguageResources("Maori", R.string.MI,
193 R.drawable.flag_of_new_zealand));
194 isoCodeToResources.put("MN", new LanguageResources("Mongolian", R.string.MN,
195 R.drawable.flag_of_mongolia));
196 isoCodeToResources.put("NE", new LanguageResources("Nepali", R.string.NE,
197 R.drawable.flag_of_nepal));
198 isoCodeToResources.put("NO", new LanguageResources("Norwegian", R.string.NO,
199 R.drawable.flag_of_norway));
200 isoCodeToResources.put("FA", new LanguageResources("Persian", R.string.FA,
201 R.drawable.flag_of_iran));
202 isoCodeToResources.put("PL", new LanguageResources("Polish", R.string.PL,
203 R.drawable.flag_of_poland));
204 isoCodeToResources.put("PT", new LanguageResources("Portuguese", R.string.PT,
205 R.drawable.flag_of_portugal));
206 isoCodeToResources.put("PA", new LanguageResources("Punjabi", R.string.PA));
207 isoCodeToResources.put("RO", new LanguageResources("Romanian", R.string.RO,
208 R.drawable.flag_of_romania));
209 isoCodeToResources.put("RU", new LanguageResources("Russian", R.string.RU,
210 R.drawable.flag_of_russia));
211 isoCodeToResources.put("SA", new LanguageResources("Sanskrit", R.string.SA));
212 isoCodeToResources.put("SR", new LanguageResources("Serbian", R.string.SR,
213 R.drawable.flag_of_serbia));
214 isoCodeToResources.put("SK", new LanguageResources("Slovak", R.string.SK,
215 R.drawable.flag_of_slovakia));
216 isoCodeToResources.put("SL", new LanguageResources("Slovenian", R.string.SL,
217 R.drawable.flag_of_slovenia));
218 isoCodeToResources.put("SO", new LanguageResources("Somali", R.string.SO,
219 R.drawable.flag_of_somalia));
220 isoCodeToResources.put("ES", new LanguageResources("Spanish", R.string.ES,
221 R.drawable.flag_of_spain));
222 isoCodeToResources.put("SW", new LanguageResources("Swahili", R.string.SW));
223 isoCodeToResources.put("SV", new LanguageResources("Swedish", R.string.SV,
224 R.drawable.flag_of_sweden));
225 isoCodeToResources.put("TL", new LanguageResources("Tagalog", R.string.TL));
226 isoCodeToResources.put("TG", new LanguageResources("Tajik", R.string.TG,
227 R.drawable.flag_of_tajikistan));
228 isoCodeToResources.put("TH", new LanguageResources("Thai", R.string.TH,
229 R.drawable.flag_of_thailand));
230 isoCodeToResources.put("BO", new LanguageResources("Tibetan", R.string.BO));
231 isoCodeToResources.put("TR", new LanguageResources("Turkish", R.string.TR,
232 R.drawable.flag_of_turkey));
233 isoCodeToResources.put("UK", new LanguageResources("Ukrainian", R.string.UK,
234 R.drawable.flag_of_ukraine));
235 isoCodeToResources.put("UR", new LanguageResources("Urdu", R.string.UR));
236 isoCodeToResources.put("VI", new LanguageResources("Vietnamese", R.string.VI,
237 R.drawable.flag_of_vietnam));
238 isoCodeToResources.put("CI", new LanguageResources("Welsh", R.string.CI,
239 R.drawable.flag_of_wales_2));
240 isoCodeToResources.put("YI", new LanguageResources("Yiddish", R.string.YI));
241 isoCodeToResources.put("ZU", new LanguageResources("Zulu", R.string.ZU));
242 isoCodeToResources.put("AZ", new LanguageResources("Azeri", R.string.AZ,
243 R.drawable.flag_of_azerbaijan));
244 isoCodeToResources.put("EU", new LanguageResources("Basque", R.string.EU,
245 R.drawable.flag_of_the_basque_country));
246 isoCodeToResources.put("BR", new LanguageResources("Breton", R.string.BR));
247 isoCodeToResources.put("MR", new LanguageResources("Marathi", R.string.MR));
248 isoCodeToResources.put("FO", new LanguageResources("Faroese", R.string.FO));
249 isoCodeToResources.put("GL", new LanguageResources("Galician", R.string.GL,
250 R.drawable.flag_of_galicia));
251 isoCodeToResources.put("KA", new LanguageResources("Georgian", R.string.KA,
252 R.drawable.flag_of_georgia));
253 isoCodeToResources.put("HT", new LanguageResources("Haitian Creole", R.string.HT,
254 R.drawable.flag_of_haiti));
255 isoCodeToResources.put("LB", new LanguageResources("Luxembourgish", R.string.LB,
256 R.drawable.flag_of_luxembourg));
257 isoCodeToResources.put("MK", new LanguageResources("Macedonian", R.string.MK,
258 R.drawable.flag_of_macedonia));
259 isoCodeToResources.put("LO", new LanguageResources("Lao", R.string.LO,
260 R.drawable.flag_of_laos));
261 isoCodeToResources.put("ML", new LanguageResources("Malayalam", R.string.ML));
262 isoCodeToResources.put("SL", new LanguageResources("Slovenian", R.string.SL,
263 R.drawable.flag_of_slovenia));
264 isoCodeToResources.put("TA", new LanguageResources("Tamil", R.string.TA));
265 isoCodeToResources.put("SH", new LanguageResources("Serbo-Croatian", R.string.SH));
266 isoCodeToResources.put("SD", new LanguageResources("Sindhi", R.string.SD));
268 // Hack to allow lower-case ISO codes to work:
269 for (final String isoCode : new ArrayList<String>(isoCodeToResources.keySet())) {
270 isoCodeToResources.put(isoCode.toLowerCase(), isoCodeToResources.get(isoCode));
275 static final class DictionaryConfig implements Serializable {
276 private static final long serialVersionUID = -1444177164708201263L;
277 // User-ordered list, persisted, just the ones that are/have been
279 final List<String> dictionaryFilesOrdered = new ArrayList<String>();
281 final Map<String, DictionaryInfo> uncompressedFilenameToDictionaryInfo = new LinkedHashMap<String, DictionaryInfo>();
284 * Sometimes a deserialized version of this data structure isn't valid.
288 return uncompressedFilenameToDictionaryInfo != null && dictionaryFilesOrdered != null;
292 DictionaryConfig dictionaryConfig = null;
294 int languageButtonPixels = -1;
296 static synchronized void staticInit(final Context context) {
297 if (DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO != null) {
300 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO = new LinkedHashMap<String, DictionaryInfo>();
301 final BufferedReader reader = new BufferedReader(
302 new InputStreamReader(context.getResources().openRawResource(R.raw.dictionary_info)));
305 while ((line = reader.readLine()) != null) {
306 if (line.startsWith("#") || line.length() == 0) {
309 final DictionaryInfo dictionaryInfo = new DictionaryInfo(line);
310 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.put(
311 dictionaryInfo.uncompressedFilename, dictionaryInfo);
314 } catch (IOException e) {
315 Log.e(LOG, "Failed to load downloadable dictionary lists.", e);
319 private File dictDir;
322 public void onCreate() {
324 Log.d("QuickDic", "Application: onCreate");
325 TransliteratorManager.init(null);
326 staticInit(getApplicationContext());
328 languageButtonPixels = (int) TypedValue.applyDimension(
329 TypedValue.COMPLEX_UNIT_DIP, 60, getResources().getDisplayMetrics());
331 // Load the dictionaries we know about.
332 dictionaryConfig = PersistentObjectCache.init(getApplicationContext()).read(
333 C.DICTIONARY_CONFIGS, DictionaryConfig.class);
334 if (dictionaryConfig == null) {
335 dictionaryConfig = new DictionaryConfig();
337 if (!dictionaryConfig.isValid()) {
338 dictionaryConfig = new DictionaryConfig();
342 setTheme(getSelectedTheme().themeId);
343 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
344 prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
346 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
348 Log.d("QuickDic", "prefs changed: " + key);
349 if (key.equals(getString(R.string.themeKey))) {
350 setTheme(getSelectedTheme().themeId);
356 public void onCreateGlobalOptionsMenu(
357 final Context context, final Menu menu) {
358 final MenuItem about = menu.add(getString(R.string.about));
359 MenuItemCompat.setShowAsAction(about, MenuItem.SHOW_AS_ACTION_NEVER);
360 about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
361 public boolean onMenuItemClick(final MenuItem menuItem) {
362 final Intent intent = new Intent(getApplicationContext(), AboutActivity.class);
363 context.startActivity(intent);
368 final MenuItem help = menu.add(getString(R.string.help));
369 MenuItemCompat.setShowAsAction(help, MenuItem.SHOW_AS_ACTION_NEVER);
370 help.setOnMenuItemClickListener(new OnMenuItemClickListener() {
371 public boolean onMenuItemClick(final MenuItem menuItem) {
372 context.startActivity(HtmlDisplayActivity.getHelpLaunchIntent(getApplicationContext()));
377 final MenuItem preferences = menu.add(getString(R.string.settings));
378 MenuItemCompat.setShowAsAction(preferences, MenuItem.SHOW_AS_ACTION_NEVER);
379 preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
380 public boolean onMenuItemClick(final MenuItem menuItem) {
381 PreferenceActivity.prefsMightHaveChanged = true;
382 final Intent intent = new Intent(getApplicationContext(), PreferenceActivity.class);
383 context.startActivity(intent);
388 final MenuItem reportIssue = menu.add(getString(R.string.reportIssue));
389 MenuItemCompat.setShowAsAction(reportIssue, MenuItem.SHOW_AS_ACTION_NEVER);
390 reportIssue.setOnMenuItemClickListener(new OnMenuItemClickListener() {
391 public boolean onMenuItemClick(final MenuItem menuItem) {
392 final Intent intent = new Intent(Intent.ACTION_VIEW);
394 .parse("http://github.com/rdoeffinger/Dictionary/issues"));
395 context.startActivity(intent);
401 public synchronized File getDictDir() {
402 // This metaphor doesn't work, because we've already reset
403 // prefsMightHaveChanged.
404 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
405 String dir = prefs.getString(getString(R.string.quickdicDirectoryKey), "");
407 final File defaultDictDir = new File(Environment.getExternalStorageDirectory(), "quickDic");
408 dir = defaultDictDir.getAbsolutePath();
410 dictDir = new File(dir);
412 if (!dictDir.isDirectory() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
413 getApplicationContext().getExternalFilesDirs(null);
418 static public boolean checkFileCreate(File dir) {
420 File testfile = new File(dir, "quickdic_writetest");
423 res = testfile.createNewFile() & testfile.delete();
424 } catch (Exception e) {
429 public File getWordListFile() {
430 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
431 String file = prefs.getString(getString(R.string.wordListFileKey), "");
432 if (file.isEmpty()) {
433 return new File(getDictDir(), "wordList.txt");
435 return new File(file);
438 public Theme getSelectedTheme() {
439 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
440 final String theme = prefs.getString(getString(R.string.themeKey), "themeLight");
441 if (theme.equals("themeLight")) {
444 return Theme.DEFAULT;
448 public File getPath(String uncompressedFilename) {
449 return new File(getDictDir(), uncompressedFilename);
452 String defaultLangISO2 = Locale.getDefault().getLanguage().toLowerCase();
453 String defaultLangName = null;
454 final Map<String, String> fileToNameCache = new LinkedHashMap<String, String>();
456 public String isoCodeToLocalizedLanguageName(final String isoCode) {
457 final Language.LanguageResources languageResources = isoCodeToResources
459 final String lang = languageResources != null ? getApplicationContext().getString(
460 languageResources.nameId) : isoCode;
464 public List<IndexInfo> sortedIndexInfos(List<IndexInfo> indexInfos) {
465 // Hack to put the default locale first in the name.
466 if (indexInfos.size() > 1 &&
467 indexInfos.get(1).shortName.toLowerCase().equals(defaultLangISO2)) {
468 List<IndexInfo> result = new ArrayList<DictionaryInfo.IndexInfo>(indexInfos);
469 ListUtil.swap(result, 0, 1);
475 public synchronized String getDictionaryName(final String uncompressedFilename) {
476 final String currentLocale = Locale.getDefault().getLanguage().toLowerCase();
477 if (!currentLocale.equals(defaultLangISO2)) {
478 defaultLangISO2 = currentLocale;
479 fileToNameCache.clear();
480 defaultLangName = null;
482 if (defaultLangName == null) {
483 defaultLangName = isoCodeToLocalizedLanguageName(defaultLangISO2);
486 String name = fileToNameCache.get(uncompressedFilename);
491 final DictionaryInfo dictionaryInfo = DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
492 .get(uncompressedFilename);
493 if (dictionaryInfo != null) {
494 final StringBuilder nameBuilder = new StringBuilder();
496 List<IndexInfo> sortedIndexInfos = sortedIndexInfos(dictionaryInfo.indexInfos);
497 for (int i = 0; i < sortedIndexInfos.size(); ++i) {
499 nameBuilder.append("-");
502 .append(isoCodeToLocalizedLanguageName(sortedIndexInfos.get(i).shortName));
504 name = nameBuilder.toString();
506 name = uncompressedFilename.replace(".quickdic", "");
508 fileToNameCache.put(uncompressedFilename, name);
512 public View createButton(final Context context, final DictionaryInfo dictionaryInfo,
513 final IndexInfo indexInfo) {
514 LanguageResources languageResources = isoCodeToResources.get(indexInfo.shortName);
517 if (languageResources == null || languageResources.flagId <= 0) {
518 Button button = new Button(context);
519 button.setText(indexInfo.shortName);
522 ImageButton button = new ImageButton(context);
523 button.setImageResource(languageResources.flagId);
524 button.setScaleType(ScaleType.FIT_CENTER);
527 result.setMinimumWidth(languageButtonPixels);
528 result.setMinimumHeight(languageButtonPixels * 2 / 3);
529 // result.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
530 // LayoutParams.WRAP_CONTENT));
534 public synchronized void moveDictionaryToTop(final DictionaryInfo dictionaryInfo) {
535 dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename);
536 dictionaryConfig.dictionaryFilesOrdered.add(0, dictionaryInfo.uncompressedFilename);
537 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
540 public synchronized void deleteDictionary(final DictionaryInfo dictionaryInfo) {
541 while (dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename)) {
543 dictionaryConfig.uncompressedFilenameToDictionaryInfo
544 .remove(dictionaryInfo.uncompressedFilename);
545 getPath(dictionaryInfo.uncompressedFilename).delete();
546 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
549 final Comparator collator = USE_COLLATOR ? Collator.getInstance() : String.CASE_INSENSITIVE_ORDER;
550 final Comparator<String> uncompressedFilenameComparator = new Comparator<String>() {
552 public int compare(String uncompressedFilename1, String uncompressedFilename2) {
553 final String name1 = getDictionaryName(uncompressedFilename1);
554 final String name2 = getDictionaryName(uncompressedFilename2);
555 if (defaultLangName.length() > 0) {
556 if (name1.startsWith(defaultLangName + "-")
557 && !name2.startsWith(defaultLangName + "-")) {
559 } else if (name2.startsWith(defaultLangName + "-")
560 && !name1.startsWith(defaultLangName + "-")) {
564 return collator.compare(name1, name2);
567 final Comparator<DictionaryInfo> dictionaryInfoComparator = new Comparator<DictionaryInfo>() {
569 public int compare(DictionaryInfo d1, DictionaryInfo d2) {
570 // Single-index dictionaries first.
571 if (d1.indexInfos.size() != d2.indexInfos.size()) {
572 return d1.indexInfos.size() - d2.indexInfos.size();
574 return uncompressedFilenameComparator.compare(d1.uncompressedFilename,
575 d2.uncompressedFilename);
579 public void backgroundUpdateDictionaries(final Runnable onUpdateFinished) {
580 new Thread(new Runnable() {
583 final DictionaryConfig oldDictionaryConfig = new DictionaryConfig();
584 synchronized (this) {
585 oldDictionaryConfig.dictionaryFilesOrdered
586 .addAll(dictionaryConfig.dictionaryFilesOrdered);
588 final DictionaryConfig newDictionaryConfig = new DictionaryConfig();
589 for (final String uncompressedFilename : oldDictionaryConfig.dictionaryFilesOrdered) {
590 final File dictFile = getPath(uncompressedFilename);
591 final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(dictFile);
592 if (dictionaryInfo != null) {
593 newDictionaryConfig.dictionaryFilesOrdered.add(uncompressedFilename);
594 newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(
595 uncompressedFilename, dictionaryInfo);
599 // Are there dictionaries on the device that we didn't know
601 // Pick them up and put them at the end of the list.
602 final List<String> toAddSorted = new ArrayList<String>();
603 final File[] dictDirFiles = getDictDir().listFiles();
604 if (dictDirFiles != null) {
605 for (final File file : dictDirFiles) {
606 if (file.getName().endsWith(".zip")) {
607 if (DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
608 .containsKey(file.getName().replace(".zip", ""))) {
612 if (!file.getName().endsWith(".quickdic")) {
615 if (newDictionaryConfig.uncompressedFilenameToDictionaryInfo
616 .containsKey(file.getName())) {
617 // We have it in our list already.
620 final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(file);
621 if (dictionaryInfo == null) {
622 Log.e(LOG, "Unable to parse dictionary: " + file.getPath());
626 toAddSorted.add(file.getName());
627 newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(
628 file.getName(), dictionaryInfo);
631 Log.w(LOG, "dictDir is not a directory: " + getDictDir().getPath());
633 if (!toAddSorted.isEmpty()) {
634 Collections.sort(toAddSorted, uncompressedFilenameComparator);
635 newDictionaryConfig.dictionaryFilesOrdered.addAll(toAddSorted);
638 PersistentObjectCache.getInstance()
639 .write(C.DICTIONARY_CONFIGS, newDictionaryConfig);
640 synchronized (this) {
641 dictionaryConfig = newDictionaryConfig;
645 onUpdateFinished.run();
646 } catch (Exception e) {
647 Log.e(LOG, "Exception running callback.", e);
653 public boolean matchesFilters(final DictionaryInfo dictionaryInfo, final String[] filters) {
654 if (filters == null) {
657 for (final String filter : filters) {
658 if (!getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(
666 public synchronized List<DictionaryInfo> getDictionariesOnDevice(String[] filters) {
667 final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(
668 dictionaryConfig.dictionaryFilesOrdered.size());
669 for (final String uncompressedFilename : dictionaryConfig.dictionaryFilesOrdered) {
670 final DictionaryInfo dictionaryInfo = dictionaryConfig.uncompressedFilenameToDictionaryInfo
671 .get(uncompressedFilename);
672 if (dictionaryInfo != null && matchesFilters(dictionaryInfo, filters)) {
673 result.add(dictionaryInfo);
679 public List<DictionaryInfo> getDownloadableDictionaries(String[] filters) {
680 final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(
681 dictionaryConfig.dictionaryFilesOrdered.size());
683 final Map<String, DictionaryInfo> remaining = new LinkedHashMap<String, DictionaryInfo>(
684 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO);
685 remaining.keySet().removeAll(dictionaryConfig.dictionaryFilesOrdered);
686 for (final DictionaryInfo dictionaryInfo : remaining.values()) {
687 if (matchesFilters(dictionaryInfo, filters)) {
688 result.add(dictionaryInfo);
691 Collections.sort(result, dictionaryInfoComparator);
695 public synchronized boolean isDictionaryOnDevice(String uncompressedFilename) {
696 return dictionaryConfig.uncompressedFilenameToDictionaryInfo.get(uncompressedFilename) != null;
699 public boolean updateAvailable(final DictionaryInfo dictionaryInfo) {
700 final DictionaryInfo downloadable =
701 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.get(
702 dictionaryInfo.uncompressedFilename);
703 return downloadable != null &&
704 downloadable.creationMillis > dictionaryInfo.creationMillis;
707 public DictionaryInfo getDownloadable(final String uncompressedFilename) {
708 final DictionaryInfo downloadable = DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
709 .get(uncompressedFilename);