-
- // 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<DictionaryInfo> toAddSorted = new ArrayList<DictionaryInfo>();
- final File[] dictDirFiles = DICT_DIR.listFiles();
- for (final File file : dictDirFiles) {
- if (!file.getName().endsWith(".quickdic")) {
- continue;
- }
- if (known.contains(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;
+ };
+
+ 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.dictionaryInfoCache.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_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);
+ }
+ } 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);
+ }
+ }}).start();
+ }
+
+ public synchronized List<DictionaryInfo> getUsableDicts() {
+ final List<DictionaryInfo> result = new ArrayList<DictionaryInfo>(dictionaryConfig.dictionaryFilesOrdered.size());
+ for (final String uncompressedFilename : dictionaryConfig.dictionaryFilesOrdered) {
+ final DictionaryInfo dictionaryInfo = dictionaryConfig.dictionaryInfoCache.get(uncompressedFilename);
+ if (dictionaryInfo != null) {
+ result.add(dictionaryInfo);