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 except
69 // for my known broken development/performance test device config.
70 static public final boolean USE_COLLATOR = !android.os.Build.FINGERPRINT.equals("Samsung/cm_tassve/tassve:4.4.4/KTU84Q/20150211:userdebug/release-keys");
72 // Static, determined by resources (and locale).
74 static Map<String, DictionaryInfo> DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO = null;
77 DEFAULT(R.style.Theme_Default,
78 R.style.Theme_Default_TokenRow_Fg,
79 R.color.theme_default_token_row_fg,
80 R.drawable.theme_default_token_row_main_bg,
81 R.drawable.theme_default_token_row_other_bg,
82 R.drawable.theme_default_normal_row_bg),
84 LIGHT(R.style.Theme_Light,
85 R.style.Theme_Light_TokenRow_Fg,
86 R.color.theme_light_token_row_fg,
87 R.drawable.theme_light_token_row_main_bg,
88 R.drawable.theme_light_token_row_other_bg,
89 R.drawable.theme_light_normal_row_bg);
91 Theme(final int themeId, final int tokenRowFg,
92 final int tokenRowFgColor,
93 final int tokenRowMainBg, final int tokenRowOtherBg,
94 final int normalRowBg) {
95 this.themeId = themeId;
96 this.tokenRowFg = tokenRowFg;
97 this.tokenRowFgColor = tokenRowFgColor;
98 this.tokenRowMainBg = tokenRowMainBg;
99 this.tokenRowOtherBg = tokenRowOtherBg;
100 this.normalRowBg = normalRowBg;
104 final int tokenRowFg;
105 final int tokenRowFgColor;
106 final int tokenRowMainBg;
107 final int tokenRowOtherBg;
108 final int normalRowBg;
112 // http://www.loc.gov/standards/iso639-2/php/code_list.php
113 public static final Map<String, LanguageResources> isoCodeToResources = new LinkedHashMap<String, LanguageResources>();
115 isoCodeToResources.put("AF", new LanguageResources("Afrikaans", R.string.AF,
116 R.drawable.flag_of_south_africa));
117 isoCodeToResources.put("SQ", new LanguageResources("Albanian", R.string.SQ,
118 R.drawable.flag_of_albania));
119 isoCodeToResources.put("AR",
120 new LanguageResources("Arabic", R.string.AR, R.drawable.arabic));
121 isoCodeToResources.put("HY", new LanguageResources("Armenian", R.string.HY,
122 R.drawable.flag_of_armenia));
123 isoCodeToResources.put("BE", new LanguageResources("Belarusian", R.string.BE,
124 R.drawable.flag_of_belarus));
125 isoCodeToResources.put("BN", new LanguageResources("Bengali", R.string.BN));
126 isoCodeToResources.put("BS", new LanguageResources("Bosnian", R.string.BS,
127 R.drawable.flag_of_bosnia_and_herzegovina));
128 isoCodeToResources.put("BG", new LanguageResources("Bulgarian", R.string.BG,
129 R.drawable.flag_of_bulgaria));
130 isoCodeToResources.put("MY", new LanguageResources("Burmese", R.string.MY,
131 R.drawable.flag_of_myanmar));
132 isoCodeToResources.put("ZH", new LanguageResources("Chinese", R.string.ZH,
133 R.drawable.flag_of_the_peoples_republic_of_china));
134 isoCodeToResources.put("cmn", new LanguageResources("Mandarin", R.string.cmn,
135 R.drawable.flag_of_the_peoples_republic_of_china));
136 isoCodeToResources.put("yue", new LanguageResources("Cantonese", R.string.yue,
137 R.drawable.flag_of_hong_kong));
138 isoCodeToResources.put("CA", new LanguageResources("Catalan", R.string.CA));
139 isoCodeToResources.put("HR", new LanguageResources("Croatian", R.string.HR,
140 R.drawable.flag_of_croatia));
141 isoCodeToResources.put("CS", new LanguageResources("Czech", R.string.CS,
142 R.drawable.flag_of_the_czech_republic));
143 isoCodeToResources.put("DA", new LanguageResources("Danish", R.string.DA,
144 R.drawable.flag_of_denmark));
145 isoCodeToResources.put("NL", new LanguageResources("Dutch", R.string.NL,
146 R.drawable.flag_of_the_netherlands));
147 isoCodeToResources.put("EN", new LanguageResources("English", R.string.EN,
148 R.drawable.flag_of_the_united_kingdom));
149 isoCodeToResources.put("EO", new LanguageResources("Esperanto", R.string.EO,
150 R.drawable.flag_of_esperanto));
151 isoCodeToResources.put("ET", new LanguageResources("Estonian", R.string.ET,
152 R.drawable.flag_of_estonia));
153 isoCodeToResources.put("FI", new LanguageResources("Finnish", R.string.FI,
154 R.drawable.flag_of_finland));
155 isoCodeToResources.put("FR", new LanguageResources("French", R.string.FR,
156 R.drawable.flag_of_france));
157 isoCodeToResources.put("DE", new LanguageResources("German", R.string.DE,
158 R.drawable.flag_of_germany));
159 isoCodeToResources.put("EL", new LanguageResources("Greek", R.string.EL,
160 R.drawable.flag_of_greece));
161 isoCodeToResources.put("grc", new LanguageResources("Ancient Greek", R.string.grc));
162 isoCodeToResources.put("haw", new LanguageResources("Hawaiian", R.string.haw,
163 R.drawable.flag_of_hawaii));
164 isoCodeToResources.put("HE", new LanguageResources("Hebrew", R.string.HE,
165 R.drawable.flag_of_israel));
166 isoCodeToResources.put("HI", new LanguageResources("Hindi", R.string.HI, R.drawable.hindi));
167 isoCodeToResources.put("HU", new LanguageResources("Hungarian", R.string.HU,
168 R.drawable.flag_of_hungary));
169 isoCodeToResources.put("IS", new LanguageResources("Icelandic", R.string.IS,
170 R.drawable.flag_of_iceland));
171 isoCodeToResources.put("ID", new LanguageResources("Indonesian", R.string.ID,
172 R.drawable.flag_of_indonesia));
173 isoCodeToResources.put("GA", new LanguageResources("Irish", R.string.GA,
174 R.drawable.flag_of_ireland));
175 isoCodeToResources.put("GD", new LanguageResources("Scottish Gaelic", R.string.GD,
176 R.drawable.flag_of_scotland));
177 isoCodeToResources.put("GV", new LanguageResources("Manx", R.string.GV,
178 R.drawable.flag_of_the_isle_of_man));
179 isoCodeToResources.put("IT", new LanguageResources("Italian", R.string.IT,
180 R.drawable.flag_of_italy));
181 isoCodeToResources.put("LA", new LanguageResources("Latin", R.string.LA));
182 isoCodeToResources.put("LV", new LanguageResources("Latvian", R.string.LV,
183 R.drawable.flag_of_latvia));
184 isoCodeToResources.put("LT", new LanguageResources("Lithuanian", R.string.LT,
185 R.drawable.flag_of_lithuania));
186 isoCodeToResources.put("JA", new LanguageResources("Japanese", R.string.JA,
187 R.drawable.flag_of_japan));
188 isoCodeToResources.put("KO", new LanguageResources("Korean", R.string.KO,
189 R.drawable.flag_of_south_korea));
190 isoCodeToResources.put("KU", new LanguageResources("Kurdish", R.string.KU));
191 isoCodeToResources.put("MS", new LanguageResources("Malay", R.string.MS,
192 R.drawable.flag_of_malaysia));
193 isoCodeToResources.put("MI", new LanguageResources("Maori", R.string.MI,
194 R.drawable.flag_of_new_zealand));
195 isoCodeToResources.put("MN", new LanguageResources("Mongolian", R.string.MN,
196 R.drawable.flag_of_mongolia));
197 isoCodeToResources.put("NE", new LanguageResources("Nepali", R.string.NE,
198 R.drawable.flag_of_nepal));
199 isoCodeToResources.put("NO", new LanguageResources("Norwegian", R.string.NO,
200 R.drawable.flag_of_norway));
201 isoCodeToResources.put("FA", new LanguageResources("Persian", R.string.FA,
202 R.drawable.flag_of_iran));
203 isoCodeToResources.put("PL", new LanguageResources("Polish", R.string.PL,
204 R.drawable.flag_of_poland));
205 isoCodeToResources.put("PT", new LanguageResources("Portuguese", R.string.PT,
206 R.drawable.flag_of_portugal));
207 isoCodeToResources.put("PA", new LanguageResources("Punjabi", R.string.PA));
208 isoCodeToResources.put("RO", new LanguageResources("Romanian", R.string.RO,
209 R.drawable.flag_of_romania));
210 isoCodeToResources.put("RU", new LanguageResources("Russian", R.string.RU,
211 R.drawable.flag_of_russia));
212 isoCodeToResources.put("SA", new LanguageResources("Sanskrit", R.string.SA));
213 isoCodeToResources.put("SR", new LanguageResources("Serbian", R.string.SR,
214 R.drawable.flag_of_serbia));
215 isoCodeToResources.put("SK", new LanguageResources("Slovak", R.string.SK,
216 R.drawable.flag_of_slovakia));
217 isoCodeToResources.put("SL", new LanguageResources("Slovenian", R.string.SL,
218 R.drawable.flag_of_slovenia));
219 isoCodeToResources.put("SO", new LanguageResources("Somali", R.string.SO,
220 R.drawable.flag_of_somalia));
221 isoCodeToResources.put("ES", new LanguageResources("Spanish", R.string.ES,
222 R.drawable.flag_of_spain));
223 isoCodeToResources.put("SW", new LanguageResources("Swahili", R.string.SW));
224 isoCodeToResources.put("SV", new LanguageResources("Swedish", R.string.SV,
225 R.drawable.flag_of_sweden));
226 isoCodeToResources.put("TL", new LanguageResources("Tagalog", R.string.TL));
227 isoCodeToResources.put("TG", new LanguageResources("Tajik", R.string.TG,
228 R.drawable.flag_of_tajikistan));
229 isoCodeToResources.put("TH", new LanguageResources("Thai", R.string.TH,
230 R.drawable.flag_of_thailand));
231 isoCodeToResources.put("BO", new LanguageResources("Tibetan", R.string.BO));
232 isoCodeToResources.put("TR", new LanguageResources("Turkish", R.string.TR,
233 R.drawable.flag_of_turkey));
234 isoCodeToResources.put("UK", new LanguageResources("Ukrainian", R.string.UK,
235 R.drawable.flag_of_ukraine));
236 isoCodeToResources.put("UR", new LanguageResources("Urdu", R.string.UR));
237 isoCodeToResources.put("VI", new LanguageResources("Vietnamese", R.string.VI,
238 R.drawable.flag_of_vietnam));
239 isoCodeToResources.put("CI", new LanguageResources("Welsh", R.string.CI,
240 R.drawable.flag_of_wales_2));
241 isoCodeToResources.put("YI", new LanguageResources("Yiddish", R.string.YI));
242 isoCodeToResources.put("ZU", new LanguageResources("Zulu", R.string.ZU));
243 isoCodeToResources.put("AZ", new LanguageResources("Azeri", R.string.AZ,
244 R.drawable.flag_of_azerbaijan));
245 isoCodeToResources.put("EU", new LanguageResources("Basque", R.string.EU,
246 R.drawable.flag_of_the_basque_country));
247 isoCodeToResources.put("BR", new LanguageResources("Breton", R.string.BR));
248 isoCodeToResources.put("MR", new LanguageResources("Marathi", R.string.MR));
249 isoCodeToResources.put("FO", new LanguageResources("Faroese", R.string.FO));
250 isoCodeToResources.put("GL", new LanguageResources("Galician", R.string.GL,
251 R.drawable.flag_of_galicia));
252 isoCodeToResources.put("KA", new LanguageResources("Georgian", R.string.KA,
253 R.drawable.flag_of_georgia));
254 isoCodeToResources.put("HT", new LanguageResources("Haitian Creole", R.string.HT,
255 R.drawable.flag_of_haiti));
256 isoCodeToResources.put("LB", new LanguageResources("Luxembourgish", R.string.LB,
257 R.drawable.flag_of_luxembourg));
258 isoCodeToResources.put("MK", new LanguageResources("Macedonian", R.string.MK,
259 R.drawable.flag_of_macedonia));
260 isoCodeToResources.put("LO", new LanguageResources("Lao", R.string.LO,
261 R.drawable.flag_of_laos));
262 isoCodeToResources.put("ML", new LanguageResources("Malayalam", R.string.ML));
263 isoCodeToResources.put("SL", new LanguageResources("Slovenian", R.string.SL,
264 R.drawable.flag_of_slovenia));
265 isoCodeToResources.put("TA", new LanguageResources("Tamil", R.string.TA));
266 isoCodeToResources.put("SH", new LanguageResources("Serbo-Croatian", R.string.SH));
267 isoCodeToResources.put("SD", new LanguageResources("Sindhi", R.string.SD));
269 // Hack to allow lower-case ISO codes to work:
270 for (final String isoCode : new ArrayList<String>(isoCodeToResources.keySet())) {
271 isoCodeToResources.put(isoCode.toLowerCase(), isoCodeToResources.get(isoCode));
276 static final class DictionaryConfig implements Serializable {
277 private static final long serialVersionUID = -1444177164708201263L;
278 // User-ordered list, persisted, just the ones that are/have been
280 final List<String> dictionaryFilesOrdered = new ArrayList<String>();
282 final Map<String, DictionaryInfo> uncompressedFilenameToDictionaryInfo = new LinkedHashMap<String, DictionaryInfo>();
285 * Sometimes a deserialized version of this data structure isn't valid.
289 return uncompressedFilenameToDictionaryInfo != null && dictionaryFilesOrdered != null;
293 DictionaryConfig dictionaryConfig = null;
295 int languageButtonPixels = -1;
297 static synchronized void staticInit(final Context context) {
298 if (DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO != null) {
301 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO = new LinkedHashMap<String, DictionaryInfo>();
302 final BufferedReader reader = new BufferedReader(
303 new InputStreamReader(context.getResources().openRawResource(R.raw.dictionary_info)));
306 while ((line = reader.readLine()) != null) {
307 if (line.startsWith("#") || line.length() == 0) {
310 final DictionaryInfo dictionaryInfo = new DictionaryInfo(line);
311 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.put(
312 dictionaryInfo.uncompressedFilename, dictionaryInfo);
315 } catch (IOException e) {
316 Log.e(LOG, "Failed to load downloadable dictionary lists.", e);
320 private File dictDir;
323 public void onCreate() {
325 Log.d("QuickDic", "Application: onCreate");
326 TransliteratorManager.init(null);
327 staticInit(getApplicationContext());
329 languageButtonPixels = (int) TypedValue.applyDimension(
330 TypedValue.COMPLEX_UNIT_DIP, 60, getResources().getDisplayMetrics());
332 // Load the dictionaries we know about.
333 dictionaryConfig = PersistentObjectCache.init(getApplicationContext()).read(
334 C.DICTIONARY_CONFIGS, DictionaryConfig.class);
335 if (dictionaryConfig == null) {
336 dictionaryConfig = new DictionaryConfig();
338 if (!dictionaryConfig.isValid()) {
339 dictionaryConfig = new DictionaryConfig();
343 setTheme(getSelectedTheme().themeId);
344 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
345 prefs.registerOnSharedPreferenceChangeListener(new OnSharedPreferenceChangeListener() {
347 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
349 Log.d("QuickDic", "prefs changed: " + key);
350 if (key.equals(getString(R.string.themeKey))) {
351 setTheme(getSelectedTheme().themeId);
357 public void onCreateGlobalOptionsMenu(
358 final Context context, final Menu menu) {
359 final MenuItem about = menu.add(getString(R.string.about));
360 MenuItemCompat.setShowAsAction(about, MenuItem.SHOW_AS_ACTION_NEVER);
361 about.setOnMenuItemClickListener(new OnMenuItemClickListener() {
362 public boolean onMenuItemClick(final MenuItem menuItem) {
363 final Intent intent = new Intent(getApplicationContext(), AboutActivity.class);
364 context.startActivity(intent);
369 final MenuItem help = menu.add(getString(R.string.help));
370 MenuItemCompat.setShowAsAction(help, MenuItem.SHOW_AS_ACTION_NEVER);
371 help.setOnMenuItemClickListener(new OnMenuItemClickListener() {
372 public boolean onMenuItemClick(final MenuItem menuItem) {
373 context.startActivity(HtmlDisplayActivity.getHelpLaunchIntent(getApplicationContext()));
378 final MenuItem preferences = menu.add(getString(R.string.settings));
379 MenuItemCompat.setShowAsAction(preferences, MenuItem.SHOW_AS_ACTION_NEVER);
380 preferences.setOnMenuItemClickListener(new OnMenuItemClickListener() {
381 public boolean onMenuItemClick(final MenuItem menuItem) {
382 PreferenceActivity.prefsMightHaveChanged = true;
383 final Intent intent = new Intent(getApplicationContext(), PreferenceActivity.class);
384 context.startActivity(intent);
389 final MenuItem reportIssue = menu.add(getString(R.string.reportIssue));
390 MenuItemCompat.setShowAsAction(reportIssue, MenuItem.SHOW_AS_ACTION_NEVER);
391 reportIssue.setOnMenuItemClickListener(new OnMenuItemClickListener() {
392 public boolean onMenuItemClick(final MenuItem menuItem) {
393 final Intent intent = new Intent(Intent.ACTION_VIEW);
395 .parse("http://github.com/rdoeffinger/Dictionary/issues"));
396 context.startActivity(intent);
402 public synchronized File getDictDir() {
403 // This metaphor doesn't work, because we've already reset
404 // prefsMightHaveChanged.
405 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
406 String dir = prefs.getString(getString(R.string.quickdicDirectoryKey), "");
408 final File defaultDictDir = new File(Environment.getExternalStorageDirectory(), "quickDic");
409 dir = defaultDictDir.getAbsolutePath();
411 dictDir = new File(dir);
413 if (!dictDir.isDirectory() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
414 getApplicationContext().getExternalFilesDirs(null);
419 static public boolean checkFileCreate(File dir) {
421 File testfile = new File(dir, "quickdic_writetest");
424 res = testfile.createNewFile() & testfile.delete();
425 } catch (Exception e) {
430 public File getWordListFile() {
431 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
432 String file = prefs.getString(getString(R.string.wordListFileKey), "");
433 if (file.isEmpty()) {
434 return new File(getDictDir(), "wordList.txt");
436 return new File(file);
439 public Theme getSelectedTheme() {
440 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
441 final String theme = prefs.getString(getString(R.string.themeKey), "themeLight");
442 if (theme.equals("themeLight")) {
445 return Theme.DEFAULT;
449 public File getPath(String uncompressedFilename) {
450 return new File(getDictDir(), uncompressedFilename);
453 String defaultLangISO2 = Locale.getDefault().getLanguage().toLowerCase();
454 String defaultLangName = null;
455 final Map<String, String> fileToNameCache = new LinkedHashMap<String, String>();
457 public String isoCodeToLocalizedLanguageName(final String isoCode) {
458 final Language.LanguageResources languageResources = isoCodeToResources
460 final String lang = languageResources != null ? getApplicationContext().getString(
461 languageResources.nameId) : isoCode;
465 public List<IndexInfo> sortedIndexInfos(List<IndexInfo> indexInfos) {
466 // Hack to put the default locale first in the name.
467 if (indexInfos.size() > 1 &&
468 indexInfos.get(1).shortName.toLowerCase().equals(defaultLangISO2)) {
469 List<IndexInfo> result = new ArrayList<DictionaryInfo.IndexInfo>(indexInfos);
470 ListUtil.swap(result, 0, 1);
476 public synchronized String getDictionaryName(final String uncompressedFilename) {
477 final String currentLocale = Locale.getDefault().getLanguage().toLowerCase();
478 if (!currentLocale.equals(defaultLangISO2)) {
479 defaultLangISO2 = currentLocale;
480 fileToNameCache.clear();
481 defaultLangName = null;
483 if (defaultLangName == null) {
484 defaultLangName = isoCodeToLocalizedLanguageName(defaultLangISO2);
487 String name = fileToNameCache.get(uncompressedFilename);
492 final DictionaryInfo dictionaryInfo = DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
493 .get(uncompressedFilename);
494 if (dictionaryInfo != null) {
495 final StringBuilder nameBuilder = new StringBuilder();
497 List<IndexInfo> sortedIndexInfos = sortedIndexInfos(dictionaryInfo.indexInfos);
498 for (int i = 0; i < sortedIndexInfos.size(); ++i) {
500 nameBuilder.append("-");
503 .append(isoCodeToLocalizedLanguageName(sortedIndexInfos.get(i).shortName));
505 name = nameBuilder.toString();
507 name = uncompressedFilename.replace(".quickdic", "");
509 fileToNameCache.put(uncompressedFilename, name);
513 public View createButton(final Context context, final DictionaryInfo dictionaryInfo,
514 final IndexInfo indexInfo) {
515 LanguageResources languageResources = isoCodeToResources.get(indexInfo.shortName);
518 if (languageResources == null || languageResources.flagId <= 0) {
519 Button button = new Button(context);
520 button.setText(indexInfo.shortName);
523 ImageButton button = new ImageButton(context);
524 button.setImageResource(languageResources.flagId);
525 button.setScaleType(ScaleType.FIT_CENTER);
528 result.setMinimumWidth(languageButtonPixels);
529 result.setMinimumHeight(languageButtonPixels * 2 / 3);
530 // result.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
531 // LayoutParams.WRAP_CONTENT));
535 public synchronized void moveDictionaryToTop(final DictionaryInfo dictionaryInfo) {
536 dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename);
537 dictionaryConfig.dictionaryFilesOrdered.add(0, dictionaryInfo.uncompressedFilename);
538 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
541 public synchronized void deleteDictionary(final DictionaryInfo dictionaryInfo) {
542 while (dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename)) {
544 dictionaryConfig.uncompressedFilenameToDictionaryInfo
545 .remove(dictionaryInfo.uncompressedFilename);
546 getPath(dictionaryInfo.uncompressedFilename).delete();
547 PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
550 final Comparator collator = USE_COLLATOR ? Collator.getInstance() : String.CASE_INSENSITIVE_ORDER;
551 final Comparator<String> uncompressedFilenameComparator = new Comparator<String>() {
553 public int compare(String uncompressedFilename1, String uncompressedFilename2) {
554 final String name1 = getDictionaryName(uncompressedFilename1);
555 final String name2 = getDictionaryName(uncompressedFilename2);
556 if (defaultLangName.length() > 0) {
557 if (name1.startsWith(defaultLangName + "-")
558 && !name2.startsWith(defaultLangName + "-")) {
560 } else if (name2.startsWith(defaultLangName + "-")
561 && !name1.startsWith(defaultLangName + "-")) {
565 return collator.compare(name1, name2);
568 final Comparator<DictionaryInfo> dictionaryInfoComparator = new Comparator<DictionaryInfo>() {
570 public int compare(DictionaryInfo d1, DictionaryInfo d2) {
571 // Single-index dictionaries first.
572 if (d1.indexInfos.size() != d2.indexInfos.size()) {
573 return d1.indexInfos.size() - d2.indexInfos.size();
575 return uncompressedFilenameComparator.compare(d1.uncompressedFilename,
576 d2.uncompressedFilename);
580 public void backgroundUpdateDictionaries(final Runnable onUpdateFinished) {
581 new Thread(new Runnable() {
584 final DictionaryConfig oldDictionaryConfig = new DictionaryConfig();
585 synchronized (this) {
586 oldDictionaryConfig.dictionaryFilesOrdered
587 .addAll(dictionaryConfig.dictionaryFilesOrdered);
589 final DictionaryConfig newDictionaryConfig = new DictionaryConfig();
590 for (final String uncompressedFilename : oldDictionaryConfig.dictionaryFilesOrdered) {
591 final File dictFile = getPath(uncompressedFilename);
592 final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(dictFile);
593 if (dictionaryInfo != null) {
594 newDictionaryConfig.dictionaryFilesOrdered.add(uncompressedFilename);
595 newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(
596 uncompressedFilename, dictionaryInfo);
600 // Are there dictionaries on the device that we didn't know
602 // Pick them up and put them at the end of the list.
603 final List<String> toAddSorted = new ArrayList<String>();
604 final File[] dictDirFiles = getDictDir().listFiles();
605 if (dictDirFiles != null) {
606 for (final File file : dictDirFiles) {
607 if (file.getName().endsWith(".zip")) {
608 if (DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
609 .containsKey(file.getName().replace(".zip", ""))) {
613 if (!file.getName().endsWith(".quickdic")) {
616 if (newDictionaryConfig.uncompressedFilenameToDictionaryInfo
617 .containsKey(file.getName())) {
618 // We have it in our list already.
621 final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(file);
622 if (dictionaryInfo == null) {
623 Log.e(LOG, "Unable to parse dictionary: " + file.getPath());
627 toAddSorted.add(file.getName());
628 newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(
629 file.getName(), dictionaryInfo);
632 Log.w(LOG, "dictDir is not a directory: " + getDictDir().getPath());
634 if (!toAddSorted.isEmpty()) {
635 Collections.sort(toAddSorted, uncompressedFilenameComparator);
636 newDictionaryConfig.dictionaryFilesOrdered.addAll(toAddSorted);
639 PersistentObjectCache.getInstance()
640 .write(C.DICTIONARY_CONFIGS, newDictionaryConfig);
641 synchronized (this) {
642 dictionaryConfig = newDictionaryConfig;
646 onUpdateFinished.run();
647 } catch (Exception e) {
648 Log.e(LOG, "Exception running callback.", e);
654 public boolean matchesFilters(final DictionaryInfo dictionaryInfo, final String[] filters) {
655 if (filters == null) {
658 for (final String filter : filters) {
659 if (!getDictionaryName(dictionaryInfo.uncompressedFilename).toLowerCase().contains(
667 public synchronized List<DictionaryInfo> getDictionariesOnDevice(String[] filters) {
668 final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(
669 dictionaryConfig.dictionaryFilesOrdered.size());
670 for (final String uncompressedFilename : dictionaryConfig.dictionaryFilesOrdered) {
671 final DictionaryInfo dictionaryInfo = dictionaryConfig.uncompressedFilenameToDictionaryInfo
672 .get(uncompressedFilename);
673 if (dictionaryInfo != null && matchesFilters(dictionaryInfo, filters)) {
674 result.add(dictionaryInfo);
680 public List<DictionaryInfo> getDownloadableDictionaries(String[] filters) {
681 final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(
682 dictionaryConfig.dictionaryFilesOrdered.size());
684 final Map<String, DictionaryInfo> remaining = new LinkedHashMap<String, DictionaryInfo>(
685 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO);
686 remaining.keySet().removeAll(dictionaryConfig.dictionaryFilesOrdered);
687 for (final DictionaryInfo dictionaryInfo : remaining.values()) {
688 if (matchesFilters(dictionaryInfo, filters)) {
689 result.add(dictionaryInfo);
692 Collections.sort(result, dictionaryInfoComparator);
696 public synchronized boolean isDictionaryOnDevice(String uncompressedFilename) {
697 return dictionaryConfig.uncompressedFilenameToDictionaryInfo.get(uncompressedFilename) != null;
700 public boolean updateAvailable(final DictionaryInfo dictionaryInfo) {
701 final DictionaryInfo downloadable =
702 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.get(
703 dictionaryInfo.uncompressedFilename);
704 return downloadable != null &&
705 downloadable.creationMillis > dictionaryInfo.creationMillis;
708 public DictionaryInfo getDownloadable(final String uncompressedFilename) {
709 final DictionaryInfo downloadable = DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO
710 .get(uncompressedFilename);