+ 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 (DictionaryApplication.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.isValid() || dictFile.exists()) {
+ 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<>();
+ 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.isValid()) {
+ Log.e(LOG, "Unable to parse dictionary: " + file.getPath());
+ }
+
+ toAddSorted.add(file.getName());
+ newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(
+ file.getName(), dictionaryInfo);
+ }
+ } else {
+ Log.w(LOG, "dictDir is not a directory: " + getDictDir().getPath());
+ }
+ if (!toAddSorted.isEmpty()) {
+ toAddSorted.sort(uncompressedFilenameComparator);
+ newDictionaryConfig.dictionaryFilesOrdered.addAll(toAddSorted);
+ }
+
+ try {
+ PersistentObjectCache.getInstance()
+ .write(C.DICTIONARY_CONFIGS, newDictionaryConfig);
+ } catch (Exception e) {
+ Log.e(LOG, "Failed persisting dictionary configs", e);
+ }
+
+ synchronized (DictionaryApplication.this) {
+ dictionaryConfig = newDictionaryConfig;
+ }
+
+ try {
+ onUpdateFinished.run();
+ } catch (Exception e) {
+ Log.e(LOG, "Exception running callback.", e);
+ }