]> 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 b415501fd15cdd502743c4fb374a5cc7bfb4092f..d6db166faa576b34e9b734c95b377819b78d3ab9 100644 (file)
@@ -33,6 +33,7 @@ import android.view.View;
 import android.widget.Button;
 import android.widget.ImageButton;
 import android.widget.ImageView.ScaleType;
+import android.widget.LinearLayout;
 import android.widget.Toast;
 
 import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
@@ -42,7 +43,7 @@ import com.hughes.android.dictionary.engine.Language.LanguageResources;
 import com.hughes.android.dictionary.engine.TransliteratorManager;
 import com.hughes.android.util.PersistentObjectCache;
 import com.hughes.util.ListUtil;
-import com.ibm.icu.text.Collator;
+import java.text.Collator;
 
 import java.io.BufferedReader;
 import java.io.File;
@@ -64,8 +65,11 @@ public class DictionaryApplication extends Application {
     // If set to false, avoid use of ICU collator
     // Works well enough for most european languages,
     // gives faster startup and avoids crashes on some
-    // devices due to Dalvik bugs (e.g. ARMv6, S5570i, CM11).
-    // Leave it enabled by default for correctness.
+    // devices due to Dalvik bugs (e.g. ARMv6, S5570i, CM11)
+    // when using ICU4J.
+    // Leave it enabled by default for correctness except
+    // for my known broken development/performance test device config.
+    //static public final boolean USE_COLLATOR = !android.os.Build.FINGERPRINT.equals("Samsung/cm_tassve/tassve:4.4.4/KTU84Q/20150211:userdebug/release-keys");
     static public final boolean USE_COLLATOR = true;
 
     // Static, determined by resources (and locale).
@@ -272,14 +276,14 @@ 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.
         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
@@ -310,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;
@@ -398,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();
@@ -524,10 +554,7 @@ public class DictionaryApplication extends Application {
             button.setScaleType(ScaleType.FIT_CENTER);
             result = button;
         }
-        result.setMinimumWidth(languageButtonPixels);
-        result.setMinimumHeight(languageButtonPixels * 2 / 3);
-        // result.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
-        // LayoutParams.WRAP_CONTENT));
+        result.setLayoutParams(new LinearLayout.LayoutParams(languageButtonPixels, languageButtonPixels * 2 / 3));
         return result;
     }
 
@@ -537,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)) {
         }
@@ -581,7 +613,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);
                 }
@@ -589,7 +621,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 != null) {
+                    if (dictionaryInfo.isValid() || dictFile.exists()) {
                         newDictionaryConfig.dictionaryFilesOrdered.add(uncompressedFilename);
                         newDictionaryConfig.uncompressedFilenameToDictionaryInfo.put(
                                 uncompressedFilename, dictionaryInfo);
@@ -618,9 +650,8 @@ public class DictionaryApplication extends Application {
                             continue;
                         }
                         final DictionaryInfo dictionaryInfo = Dictionary.getDictionaryInfo(file);
-                        if (dictionaryInfo == null) {
+                        if (!dictionaryInfo.isValid()) {
                             Log.e(LOG, "Unable to parse dictionary: " + file.getPath());
-                            continue;
                         }
 
                         toAddSorted.add(file.getName());
@@ -635,9 +666,14 @@ public class DictionaryApplication extends Application {
                     newDictionaryConfig.dictionaryFilesOrdered.addAll(toAddSorted);
                 }
 
-                PersistentObjectCache.getInstance()
-                        .write(C.DICTIONARY_CONFIGS, newDictionaryConfig);
-                synchronized (this) {
+                try {
+                    PersistentObjectCache.getInstance()
+                                 .write(C.DICTIONARY_CONFIGS, newDictionaryConfig);
+                } catch (Exception e) {
+                    Log.e(LOG, "Failed persisting dictionary configs", e);
+                }
+
+                synchronized (DictionaryApplication.this) {
                     dictionaryConfig = newDictionaryConfig;
                 }