]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryApplication.java
Work around Android crash bug in getExternalFilesDir.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryApplication.java
index a94ed53736425dbf36713aa2a026527b66fb3121..99e3088c0c18acdcdfe88bb299adf9584df6d0f5 100644 (file)
@@ -276,7 +276,7 @@ public class DictionaryApplication extends Application {
 
     }
 
-    static final class DictionaryConfig implements Serializable {
+    public static final class DictionaryConfig implements Serializable {
         private static final long serialVersionUID = -1444177164708201263L;
         // User-ordered list, persisted, just the ones that are/have been
         // present.
@@ -314,10 +314,10 @@ public class DictionaryApplication extends Application {
                 DOWNLOADABLE_UNCOMPRESSED_FILENAME_NAME_TO_DICTIONARY_INFO.put(
                         dictionaryInfo.uncompressedFilename, dictionaryInfo);
             }
-            reader.close();
         } catch (IOException e) {
             Log.e(LOG, "Failed to load downloadable dictionary lists.", e);
         }
+        try { reader.close(); } catch (IOException e) {}
     }
 
     private File dictDir;
@@ -402,14 +402,37 @@ 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();
+            }
+        }
+        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();
@@ -582,7 +605,7 @@ public class DictionaryApplication extends Application {
             @Override
             public void run() {
                 final DictionaryConfig oldDictionaryConfig = new DictionaryConfig();
-                synchronized (this) {
+                synchronized (DictionaryApplication.this) {
                     oldDictionaryConfig.dictionaryFilesOrdered
                             .addAll(dictionaryConfig.dictionaryFilesOrdered);
                 }
@@ -590,7 +613,7 @@ public class DictionaryApplication extends Application {
                 for (final String uncompressedFilename : oldDictionaryConfig.dictionaryFilesOrdered) {
                     final File dictFile = getPath(uncompressedFilename);
                     final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(dictFile);
-                    if (dictionaryInfo.isValid()) {
+                    if (dictionaryInfo.isValid() || dictFile.exists()) {
                         newDictionaryConfig.dictionaryFilesOrdered.add(uncompressedFilename);
                         newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(
                                 uncompressedFilename, dictionaryInfo);
@@ -642,7 +665,7 @@ public class DictionaryApplication extends Application {
                     Log.e(LOG, "Failed persisting dictionary configs", e);
                 }
 
-                synchronized (this) {
+                synchronized (DictionaryApplication.this) {
                     dictionaryConfig = newDictionaryConfig;
                 }