]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryApplication.java
Add option to sort downloaded dictionaries.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryApplication.java
index 61fd524ef6ada1c729a087779a4911545f571406..d6db166faa576b34e9b734c95b377819b78d3ab9 100644 (file)
@@ -283,7 +283,7 @@ public class DictionaryApplication extends Application {
         final List<String> dictionaryFilesOrdered = new ArrayList<String>();
 
         final Map<String, DictionaryInfo> uncompressedFilenameToDictionaryInfo = new LinkedHashMap<String, DictionaryInfo>();
-        
+
         /**
          * Sometimes a deserialized version of this data structure isn't valid.
          * @return
@@ -402,14 +402,40 @@ public class DictionaryApplication extends Application {
         });
     }
 
+    private String selectDefaultDir() {
+        final File defaultDictDir = new File(Environment.getExternalStorageDirectory(), "quickDic");
+        String dir = defaultDictDir.getAbsolutePath();
+        File dictDir = new File(dir);
+        if (dictDir.isDirectory() && dictDir.list().length > 0) {
+            return dir;
+        }
+        File efd = null;
+        try {
+            efd = getApplicationContext().getExternalFilesDir(null);
+        } catch (Exception e) {
+        }
+        if (efd != null) {
+            efd.mkdirs();
+            if (!dictDir.isDirectory() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
+                getApplicationContext().getExternalFilesDirs(null);
+            }
+            if (efd.isDirectory() && efd.canWrite() && checkFileCreate(efd)) {
+                return efd.getAbsolutePath();
+            }
+        }
+        if (!dictDir.isDirectory() && !dictDir.mkdirs()) {
+            return getApplicationContext().getFilesDir().getAbsolutePath();
+        }
+        return dir;
+    }
+
     public synchronized File getDictDir() {
         // This metaphor doesn't work, because we've already reset
         // prefsMightHaveChanged.
         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
         String dir = prefs.getString(getString(R.string.quickdicDirectoryKey), "");
         if (dir.isEmpty()) {
-            final File defaultDictDir = new File(Environment.getExternalStorageDirectory(), "quickDic");
-            dir = defaultDictDir.getAbsolutePath();
+            dir = selectDefaultDir();
         }
         dictDir = new File(dir);
         dictDir.mkdirs();
@@ -538,6 +564,11 @@ public class DictionaryApplication extends Application {
         PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
     }
 
+    public synchronized void sortDictionaries() {
+        Collections.sort(dictionaryConfig.dictionaryFilesOrdered, uncompressedFilenameComparator);
+        PersistentObjectCache.getInstance().write(C.DICTIONARY_CONFIGS, dictionaryConfig);
+    }
+
     public synchronized void deleteDictionary(final DictionaryInfo dictionaryInfo) {
         while (dictionaryConfig.dictionaryFilesOrdered.remove(dictionaryInfo.uncompressedFilename)) {
         }