]> gitweb.fperrin.net Git - Dictionary.git/blobdiff - src/com/hughes/android/dictionary/DictionaryApplication.java
Put user's locale first in name.
[Dictionary.git] / src / com / hughes / android / dictionary / DictionaryApplication.java
index d36e99d718cdbfe77f7438b622668b6d89384132..3670a1fd10dddf0598181da8260085fec6c15dc3 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Collections;
 import java.util.Comparator;
 import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Locale;
 import java.util.Map;
 
 import android.app.Application;
@@ -31,16 +32,19 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
+import android.net.Uri;
 import android.preference.PreferenceManager;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.MenuItem.OnMenuItemClickListener;
 
+import com.hughes.android.dictionary.DictionaryInfo.IndexInfo;
 import com.hughes.android.dictionary.engine.Dictionary;
 import com.hughes.android.dictionary.engine.Language;
 import com.hughes.android.dictionary.engine.TransliteratorManager;
 import com.hughes.android.util.PersistentObjectCache;
+import com.hughes.util.ListUtil;
 import com.ibm.icu.text.Collator;
 
 public class DictionaryApplication extends Application {
@@ -110,7 +114,7 @@ public class DictionaryApplication extends Application {
       @Override
       public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
           String key) {
-        Log.d("THAD", "prefs changed: " + key);
+        Log.d("QuickDic", "prefs changed: " + key);
         if (key.equals(getString(R.string.themeKey))) {
           setTheme(getSelectedTheme().themeId);
         }
@@ -148,6 +152,17 @@ public class DictionaryApplication extends Application {
         return false;
       }
     });
+    
+    
+    final MenuItem reportIssue = menu.add(getString(R.string.reportIssue));
+    reportIssue.setOnMenuItemClickListener(new OnMenuItemClickListener() {
+      public boolean onMenuItemClick(final MenuItem menuItem) {
+        final Intent intent = new Intent(Intent.ACTION_VIEW);
+        intent.setData(Uri.parse("http://code.google.com/p/quickdic-dictionary/issues/entry"));
+        context.startActivity(intent);
+        return false;
+      }
+    });
   }
   
   public synchronized File getDictDir() {
@@ -176,6 +191,8 @@ public class DictionaryApplication extends Application {
   }
   
   
+  
+  String defaultLangISO2 = Locale.getDefault().getLanguage().toLowerCase();
   final Map<String, String> fileToNameCache = new LinkedHashMap<String, String>();
 
   public String getLanguageName(final String isoCode) {
@@ -183,8 +200,15 @@ public class DictionaryApplication extends Application {
     final String lang = languageResources != null ? getApplicationContext().getString(languageResources.nameId) : isoCode;
     return lang;
   }
+  
 
   public synchronized String getDictionaryName(final String uncompressedFilename) {
+    final String currentLocale = Locale.getDefault().getLanguage().toLowerCase(); 
+    if (!currentLocale.equals(defaultLangISO2)) {
+      defaultLangISO2 = currentLocale;
+      fileToNameCache.clear();
+    }
+    
     String name = fileToNameCache.get(uncompressedFilename);
     if (name != null) {
       return name;
@@ -193,12 +217,23 @@ public class DictionaryApplication extends Application {
     final DictionaryInfo dictionaryInfo = DOWNLOADABLE_NAME_TO_INFO.get(uncompressedFilename);
     if (dictionaryInfo != null) {
       final StringBuilder nameBuilder = new StringBuilder();
+
+      // Hack to put the default locale first in the name.
+      boolean swapped = false;
+      if (dictionaryInfo.indexInfos.size() > 1 && 
+          dictionaryInfo.indexInfos.get(1).shortName.toLowerCase().equals(defaultLangISO2)) {
+        ListUtil.swap(dictionaryInfo.indexInfos, 0, 1);
+        swapped = true;
+      }
       for (int i = 0; i < dictionaryInfo.indexInfos.size(); ++i) {
         if (i > 0) {
           nameBuilder.append("-");
         }
         nameBuilder.append(getLanguageName(dictionaryInfo.indexInfos.get(i).shortName));
       }
+      if (swapped) {
+        ListUtil.swap(dictionaryInfo.indexInfos, 0, 1);
+      }
       name = nameBuilder.toString();
     } else {
       name = uncompressedFilename.replace(".quickdic", "");
@@ -256,27 +291,31 @@ public class DictionaryApplication extends Application {
         // Pick them up and put them at the end of the list.
         final List<String> toAddSorted = new ArrayList<String>();
         final File[] dictDirFiles = getDictDir().listFiles();
-        for (final File file : dictDirFiles) {
-          if (file.getName().endsWith(".zip")) {
-            if (DOWNLOADABLE_NAME_TO_INFO.containsKey(file.getName().replace(".zip", ""))) {
-              file.delete();
+        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);
           }
-          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);